Virgo

Business Rule Engine

License

License

GroupId

GroupId

org.dungba
ArtifactId

ArtifactId

joo-virgo
Last Version

Last Version

0.1.1
Release Date

Release Date

Type

Type

jar
Description

Description

Virgo
Business Rule Engine
Project URL

Project URL

https://github.com/dungba88/virgo
Source Code Management

Source Code Management

https://github.com/dungba88/virgo.git

Download joo-virgo

How to add to project

<!-- https://jarcasting.com/artifacts/org.dungba/joo-virgo/ -->
<dependency>
    <groupId>org.dungba</groupId>
    <artifactId>joo-virgo</artifactId>
    <version>0.1.1</version>
</dependency>
// https://jarcasting.com/artifacts/org.dungba/joo-virgo/
implementation 'org.dungba:joo-virgo:0.1.1'
// https://jarcasting.com/artifacts/org.dungba/joo-virgo/
implementation ("org.dungba:joo-virgo:0.1.1")
'org.dungba:joo-virgo:jar:0.1.1'
<dependency org="org.dungba" name="joo-virgo" rev="0.1.1">
  <artifact name="joo-virgo" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.dungba', module='joo-virgo', version='0.1.1')
)
libraryDependencies += "org.dungba" % "joo-virgo" % "0.1.1"
[org.dungba/joo-virgo "0.1.1"]

Dependencies

compile (1)

Group / Artifact Type Version
org.dungba : joo-libra jar 2.0.0

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.18.2

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

virgo

Maven Central Javadocs Build Status Coverage Status

Virgo is a Java Business Rule Engine, based on Libra. It supports an easy-to-use syntax for defining business rule.

install

<dependency>
    <groupId>org.dungba</groupId>
    <artifactId>joo-virgo</artifactId>
    <version>0.1.0</version>
</dependency>

how to use

// create a business rule
BusinessRule rule = new DefaultBusinessRule("IF customer.age > 50 THEN SET seniorCitizen = true");

// create a rule context
RuleContext context = new RuleContext(someObject);

// execute it
ExecutionResult result = rule.execute(ruleContext).orElseThrow(() -> new NullPointerException("result is null"));

// get the result. this will get the first value in the result map
Object resultValue = result.getValue();

// get a specific result
resultValue = result.getValue("seniorCitizen");

You can also merge multiple business rules into a single rule

// create some business rules
BusinessRule rule1 = new DefaultBusinessRule("IF customer.age > 50 THEN SET seniorCitizen = true");
BusinessRule rule2 = new DefaultBusinessRule("IF order.price > 100 THEN SET discount = 0.1");

// merge into a single rule
BusinessRule rule = new MultiBusinessRule(rule1, rule2);

// execute it
ExecutionResult result = rule.execute(ruleContext).orElseThrow(() -> new NullPointerException("result is null"));

// get the merged results
boolean seniorCitizen = result.getValue("seniorCitizen");
double discount = result.getValue("discount");

grammar

These are the supported grammar of Virgo

Single condition:

IF <condition> THEN <actions>

Branched conditions:

IF <condition> THEN <actions>
ELSE IF <condition> THEN <actions>
ELSE <actions>

Nested conditions:

IF <condition> THEN
  IF <condition> THEN
    <actions>
  ELSE
    <actions>

Nested conditions with braces:

IF <condition> THEN {
  IF <condition> THEN
    <actions>
  ELSE
    <actions>
} ELSE {
    <actions>
}

No condition at all:

<action>

Multiple actions can be separated by semicolons:

IF <condition> THEN
  <action>;
  <action>;

Currently only assignment action is supported:

SET <someVariable> = <someExpression>

The syntax for condition and expression can be found in Libra repository.

Note: The line break is not required, it's just to make it easier to read

examples

The following are valid examples:

IF 1 + 1 == 2 THEN SET result = 1

IF customer.age > 50 THEN SET seniorCitizen = true ELSE SET seniorCitizen = false

SET seniorCitizen = customer.age > 50

IF order.price > 100 THEN SET discount = 0.1

IF product.brand is 'Apple' THEN SET discount = 0.2 
ELSE IF product.brand is 'Samsung' THEN SET discount = 0.3
ELSE SET discount = 0.1

license

This library is distributed under MIT license, see LICENSE

Versions

Version
0.1.1
0.1.0