Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. package io.github.evacchi;
  2.  
  3. import org.drools.model.Index;
  4. import org.drools.model.Model;
  5. import org.drools.model.Rule;
  6. import org.drools.model.Variable;
  7. import org.drools.model.impl.ModelImpl;
  8. import org.drools.modelcompiler.builder.KieBaseBuilder;
  9. import org.kie.api.KieBase;
  10. import org.kie.api.runtime.KieSession;
  11. import org.kie.api.runtime.rule.FactHandle;
  12.  
  13. import static org.drools.model.DSL.*;
  14. import static org.drools.model.PatternDSL.*;
  15.  
  16. public class A {
  17. public static void main( String[] args ) {
  18. Result result = new Result();
  19. Variable<Person> markV = declarationOf( Person.class );
  20. Variable<Person> olderV = declarationOf(Person.class );
  21.  
  22. Rule rule = rule("beta" )
  23. .build(
  24. pattern(markV)
  25. .expr("exprA", p -> p.getName().equals( "Mark" ),
  26. alphaIndexedBy(String.class, Index.ConstraintType.EQUAL, 1, p -> p.getName(), "Mark" ),
  27. reactOn( "name", "age" )),
  28. pattern(olderV)
  29. .expr("exprB", p -> !p.getName().equals("Mark"),
  30. alphaIndexedBy( String.class, Index.ConstraintType.NOT_EQUAL, 1, p -> p.getName(), "Mark" ),
  31. reactOn( "name" ))
  32. .expr("exprC", markV, (p1, p2) -> p1.getAge() > p2.getAge(),
  33. betaIndexedBy( int.class, Index.ConstraintType.GREATER_THAN, 0, p -> p.getAge(), p -> p.getAge() ),
  34. reactOn( "age" )),
  35. on(olderV, markV).execute((p1, p2) -> result.setValue( p1.getName() + " is older than " + p2.getName()))
  36. );
  37.  
  38. Model model = new ModelImpl().addRule(rule );
  39. KieBase kieBase = null; //KieBaseBuilder.createKieBaseFromModel(model );
  40.  
  41. KieSession ksession = kieBase.newKieSession();
  42.  
  43. Person mark = new Person("Mark", 37);
  44. Person edson = new Person("Edson", 35);
  45. Person mario = new Person("Mario", 40);
  46.  
  47. FactHandle markFH = ksession.insert(mark);
  48. FactHandle edsonFH = ksession.insert(edson);
  49. FactHandle marioFH = ksession.insert(mario);
  50.  
  51. ksession.fireAllRules();
  52. if (!"Mario is older than Mark".equals( result.getValue() )) {
  53. throw new IllegalStateException();
  54. }
  55.  
  56. result.setValue( null );
  57. ksession.delete( marioFH );
  58. ksession.fireAllRules();
  59. if (result.getValue() != null) {
  60. throw new IllegalStateException();
  61. }
  62.  
  63. mark.setAge( 34 );
  64. ksession.update( markFH, mark, "age" );
  65.  
  66. ksession.fireAllRules();
  67. if (!"Edson is older than Mark".equals( result.getValue() )) {
  68. throw new IllegalStateException();
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement