What is Dvare?

The dvare-framework is a Java business rule expression language. The dvare-framework contains set of predefined which helps non-technical background peoples to describe business logic in the real domain.

Dependency

The artifact dvare-framework is published is a central repository. In order to use snapshot versions, you need to add the following maven repository in your pom.xml:

<repository>
    <id>ossrh</id>
    <url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository> 

<dependency>
    <groupId>org.dvare</groupId>
    <artifactId>dvare-framework</artifactId>
    <version>2.3.0</version>
</dependency>
    

Configuration

Java config:

RuleConfiguration ruleConfiguration= new RuleConfiguration(new String[]{"org.dvare.function"});

Spring Integration:

application-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="ruleEngine" class="org.dvare.spring.DvareConfigFactoryBean">
        <property name="functions">
        </property>
            <list>
                 <value>org.dvare.function</value>
            </list>
    </bean>
</beans>

Rule Engine

ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
RuleConfiguration ruleConfiguration = context.getBean(RuleConfiguration.class);

Data Types & Predefined Operations

Data Types

The dvare framework has all common data types. Following is the table giving details about all the data types available in the dvare framework.

Sr.No Name Description Literal Syntax
1 StringType A sequence of Chars. 'dvare framework'
2 IntegerType 32 bit signed value. 10
3 FloatType 32 bit IEEE 754 single-precision float. 10.10
4 BooleanType Either the literal "true" or the literal "false". true or false
5 RegexType Java Regular Expressions for String comparison. {regex}
6 DateType Java Time LocalDate.(dd-MM-yyyy) or (yyyy-MM-dd) 30-12-2011 or 2011-12-30
7 DateTimeType Java Time LocalDateTime. (dd-MM-yyyy-HH:mm:ss) 30-12-2011-13:55:40
8 NullType Empty Value. null or NULL
9 StringListType Java List of String values. ['dvare' , 'framework']
9 IntegerListType Java List of Integer values. [10, 20]
9 FloatListType Java List of Float values. [10.10 , 20.00]
9 BooleanListType Java List of Boolean values. [true , false]


Predefined Operations

The dare framework defines a number of operations on the data types. Following is the table giving details of the predefined operations on the data types.

Sr.No Name Predefined Operations Note
1 StringType toInteger(), toDate(), append(X), append(X),prepend(X), contains(X), startsWith(X), endsWith(X), substring(Q, P) X = String Literal, Q = Integer Literal [start from 1] , P = Integer Literal
2 IntegerType toString()
3 FloatType toString(), toInteger()
4 BooleanType toString()
6 DateType toString(), getYears(), addDays(P), addMonths(P), addYears(P) P = Integer Literal
6 DateTimeType toString()

Operators

The dvare framework is rich in built-in operators and provides the arithmetic, relational, logical, assignment operations.

Arithmetic Operations

Sr.No Name Syntax Description
1 Add +, add Adds two operands.
2 Subtract -, sub Subtracts the second operand from the first.
3 Multiply *, mul Multiplies both operands.
4 Divide /, div Divides first operand by second.
5 Power ^, pow Repeated multiplication of the first operand by the second time.


Logical Operations

Sr.No Name Syntax Description
1 And &&, and If both the operands are true then condition becomes true.
2 OR ||, or If any one the operands are true then condition becomes true.
3 Not !, not If a condition is true then the operator will make condition false.
4 Implies =>, implies If the first operand is true and the second operand is false condition becomes false.


Relational Operations

Sr.No Name Syntax Description
1 Equals =, eq If two operands are equal condition becomes true.
2 Not Equals !=, ne If two operands are not equal condition becomes true.
3 Less Than <, lt If the first operand is the less than the second operand condition becomes true.
4 Less Equals <=, le If the first operand is less or equal second operand condition becomes true.
5 Greater Than >, gt If the first operand is greater than the second operand condition becomes true.
6 Greater Equals >=, ge If the first operand is greater or equal the second operand condition becomes true.
7 Between between If the first operand is between the range defined in a list inside second operand condition becomes true.
8 In in If the list of values defined inside the second operand contains the first operand condition becomes true.
9 Not In notIn If the list of values defined inside the second operand not contains the first operand condition becomes true.

Function Invocation

The Dvare Frameworkallows to invoke java methods inside the rule. For Invocation you need to register class methods. For Registration annotate Java class with @FunctionService and annotate method with @FunctionMethod with return type and parameters elements. You also need to define package in rule configuration which use to scan these classes at run time.

Rule Engine Configuration
RuleConfiguration factory = new RuleConfiguration(new String[]{"org.dvare.function"});
Function Class
package org.dvare.function;
@FunctionService
public class TestFunction {
    @FunctionMethod(returnType = DataType.IntegerType, list = false, parameters = {DataType.IntegerType})
    public Integer testFunction(Integer variable) {
        System.out.println("inside testFunction with argument value: " + variable);
        return variable;
    }
}
Operation Name Syntax DataTypes Operand
Function Call fun ( name , argements) Literal, Integer, Float, String, Boolean, Regex, Date Single or List

Example

      public class DvareRuleTest extends TestCase {
            SimpleDateFormat dateTimeFormat = new SimpleDateFormat("dd-MM-yyyy-HH:mm:ss");
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");

            @Test
             public void testApp() throws ExpressionParseException, InterpretException, ParseException {
                RuleConfiguration factory = new RuleConfiguration(new String[]{"org.dvare.function"});

                String exp = "Variable2 = (1 + 2) " +
                " And Variable0 <= fun( testFunction , 10 ) " +
                " And Variable1 in ['A','B'] " +
                " And Variable2 between [2,4]" +
                " And Variable3 > 3.0 " +
                " And Variable4 = false " +
                " And Variable5 in [12-05-2016,13-05-2016] " +
                " And Variable6 in [12-05-2016-15:30:00,13-05-2016-15:30:00] " +
                " And Variable7 in [R'B1.*',R'A1.*'] ";

                Expression expression = factory.getParser().fromString(exp, EqualOperation.class);
                Rule rule = new org.dvare.binding.rule.Rule(expression);

                EqualOperation equalOperation = new EqualOperation();
                equalOperation.setVariable0(10);
                equalOperation.setVariable1("A");
                equalOperation.setVariable2(3);
                equalOperation.setVariable3(3.2f);
                equalOperation.setVariable4(false);
                equalOperation.setVariable5(dateFormat.parse("12-05-2016"));
                equalOperation.setVariable6(dateTimeFormat.parse("12-05-2016-15:30:00"));
                equalOperation.setVariable7("A1B2");

                RuleEvaluator evaluator = factory.getEvaluator();
                boolean result = evaluator.evaluate(rule, equalOperation);
                assertTrue(result);
             }
      }
      package org.dvare.function;
      @FunctionService
      public class TestFunction {
        @FunctionMethod(returnType = DataType.IntegerType, list = false, parameters = {DataType.IntegerType})
         public Integer testFunction(Integer variable) {
               System.out.println("inside testFunction with argument value: " + variable);
               return variable;
        }
      }
Fork me on GitHub