Basic usage

Programming in 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 11.

More information in the tutorial: Connecting application

//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:

@Autowired
private HyperonEngine engine;

Getting parameter value #

Parameter is one of the core concepts of Hyperon.

More information in the tutorial: Parameters

//Defining values in Hyperon

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

PARAMETER CODE: simple.parameter

INPUT #1
Source: policy.product.code
matcher: default
VALUE TYPE
Source: policy.insured.age
matcher: between
VALUE SOURCE
Swimmer name
String
swimmer.name
OUTPUT #1 ranking VALUE TYPE Integer
OUTPUT #1
VALUE TYPE
ranking
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.

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

//Execute get method

Finally execute get method and receive parameter value for Melanie

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:

More information in the tutorial: Getting parameter value

Calling function #

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

More information in the tutorial: Functions

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.

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

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 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.

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

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: Calling function.

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.

More information in the tutorial: Domain.

//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.

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.  

HyperonContext ctx = new HyperonContext(
   "coverage.source", "BI"
);
/* Function */

The same as in calling function example.

//Getting value from domain

Execute following code:

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:

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: Accessing Domain