Skip to content

Instantly share code, notes, and snippets.

@JitendraZaa
Created August 9, 2017 03:38
Show Gist options
  • Select an option

  • Save JitendraZaa/f4389d092ec43d4ad17e2ba8aa45697d to your computer and use it in GitHub Desktop.

Select an option

Save JitendraZaa/f4389d092ec43d4ad17e2ba8aa45697d to your computer and use it in GitHub Desktop.
//Define set of all allowed operators
//Mostly we don't need to change this, it can be added in setup method
Operations opObj = Operations.getInstance();
opObj.registerOperation(OperationFactory.getInstance('&&'));
opObj.registerOperation(OperationFactory.getInstance('=='));
opObj.registerOperation(OperationFactory.getInstance('!='));
opObj.registerOperation(OperationFactory.getInstance('||'));
opObj.registerOperation(OperationFactory.getInstance('('));
opObj.registerOperation(OperationFactory.getInstance(')'));
opObj.registerOperation(OperationFactory.getInstance('<'));
opObj.registerOperation(OperationFactory.getInstance('<='));
opObj.registerOperation(OperationFactory.getInstance('>'));
opObj.registerOperation(OperationFactory.getInstance('>='));
//Define bindings, which will replace variables while
//evaluating rules
Map<String, String>bindings = new Map<String, String>();
bindings.put('Case.OwnerName__c'.toLowerCase(), 'Jitendra');
bindings.put('Case.IsEscalated'.toLowerCase(), 'false');
bindings.put('Case.age_mins__c'.toLowerCase(), '62');
//Define rule
String expr = 'Case.OwnerName__c == Minal || ( Case.age_mins__c < 75 && Case.IsEscalated == false )' ;
//Initialize Rule Engine
Rule r = new Rule().setExpression(expr);
//Evaluate rule with Binding values
Boolean retVal = r.eval(bindings) ;
//Check if expected result is correct
System.assertEquals(true, retVal);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment