Close

Programming with Hyperon

Connecting application

To run Hyperon within your application you have to add it to your framework. Hyperon can be run within any framework. We prepared example for Spring and Java 1.8.

Framework
Spring

Injecting Hyperon Engine

After connecting application you have to inject Hyperon Engine (part of Hyperon Runtime Library)  in any place your app connects with business logic:

1
2
@Autowired
private HyperonEngine engine;

Getting parameter value #

Parameter is one of the core concepts of Hyperon.

Defining values in Hyperon

Simple parameter definition (with only one input and one output):

Parameter code
simple.parameter
Input #1
Swimmer name
Value type
String
Value source
swimmer.name
Output #1
ranking
Value type
Integer

Parameters data:

Swimmer name
Ranking
Ashley
2
Celine
1
Kate
4
Melanie
3

Now imagine you would like to show ranking position for Melanie (last row in matrix above) in your application.

Prepare context

Prepare context locally in a method you want to get parameter value to. Connect value source from parameter definition within the method.

1
2
3
HyperonContext ctx = new HyperonContext(
   "swimmer.name", "Melanie"
);

Execute get method

Finally execute get method and receive parameter value for Melanie

1
2
ParamValue pv = engine.get("simple.parameter", ctx);
Integer pos = pv.getInteger("ranking");

Invoking parameter via Hyperon Engine returns instances of  ParamValue  classes. It contains evaluated values as sub-matrix from given parameter based on its input levels.

Your application received output value („3”) for given input („Melanie”). What can you do with it? It’s up to you! For more information go to the tutorial:

Calling function #

Sometimes instead of using a parameter writing a function might be a better solution:

Imagine you would like to calculate the cost for booking a room given the cost per day and  the duration of stay (in days). The formula is pretty simple.

1
total cost = cost per day * duration in days

Write function in Hyperon

Function in Hyperon are written in Groovy (JVM language).

Function code
simple.parameter
1
2
3
4
5
6
7
8
9
def costPerDay = ctx.getNumber('booking.costPerDay')

def dateFrom = ctx.getDate('booking.startDate')

def dateTo = ctx.getDate('booking.endDate')

def durationDays = date.getDayDiff(dateFrom,dateTo) + 1

return costPerDay * durationDays

ctx  is is an instance of  ParamContext  class that you can use to get any argument from context you passed to HyperonEngine.  ParamContext  is automatically passed to every function in Hyperon Engine.

date.getDayDiff  is an example of a built in method in Hyperon.

date.getDayDiff  is helpful in this scenario, because it calculates the difference in days between two dates. We add +1, because we want to include both end dates (the difference between 1st January and 3rd January is 2, so we need to add 1 to receive a duration).

Prepare context

Prepare context locally in a method you want to call the Hyperon function. Connect variables names the function within the constructor.

1
2
3
4
5
HyperonContext ctx = new HyperonContext(  
  "booking.costPerDay", "200",
  "booking.startDate","2020-01-01",
  "booking.endDate","2020-01-03"  // 3 days duration
);

Execute call method

Finally execute call method and calculate total cost of vacation

1
2
BigDecimal vacationCost = (BigDecimal) 

engine.call("simple.function", ctx);


Your application calculated value („600”) for given function. What can you do with it? It’s up to you! For more information go to the tutorial:

Accessing domain #

Domain is one of the most powerful Hyperon features. However it’s very Hyperon specific, so if you’re new to Hyperon you might be thrown away by too much abstraction. That’s why we highly recommend to start by getting familiar with the domain concept.

Defining domain in Hyperon

The goal of Hyperon domain is to map real life into the system. Let’s imagine you develop an application for selling insurance. Your domain focuses on pricing plans and its excerpt could look like this:  

Let’s say we would like to receive  BI  position on screen - POS.

Prepare context

Attributes values can be constant or parameter or function.

/* Constant */

No context needed.

/* Parameter */

The same as in getting parameter example. Prepare context locally in a method you want to get domain value to. Connect value source from parameter definition within the method.  

1
2
3
HyperonContext ctx = new HyperonContext(
   "coverage.source", "BI"
);

/* Function */

The same as in calling function example.

Getting value from domain

Execute following code:

1
2
3
4
5
6
HyperonDomainObject domainObject = engine.getDomain(
  /** Profile code **/ "DEMO",
  /** Domain path **/ "/PLANS[FULL]/COVERAGES[BI]/");

ParamValue pv = domainObject.getAttribute(
  /** Attribute code **/ "POSITION").getValue(ctx);

We received position for  BI  coverage.

Let’s modify the scenario a bit and imagine we would like to list all coverages attached to full plan. We could execute the following code:

1
2
3
4
5
6
7
8
9
10
11
HyperonDomainObject domainObject = engine.getDomain(
  /** Profile code **/ "DEMO",
  /** Domain element path **/ "/PLANS[FULL]/");

List<HyperonDomainObject> coverages = domainObject.getChildren(
  /** Collection code **/ "COVERAGES");

for(HyperonDomainObject coverage : coverages) {
    logger.info("Coverage : {}", coverage.getCode());
}

If you want to learn more about accessing domain, feel free to check the tutorial: