use of org.drools.model.Variable in project drools by kiegroup.
the class PatternDSLTest method testBeta.
@Test
public void testBeta() {
Result result = new Result();
Variable<Person> markV = declarationOf(Person.class);
Variable<Person> olderV = declarationOf(Person.class);
Rule rule = rule("beta").build(pattern(markV).expr("exprA", p -> p.getName().equals("Mark"), alphaIndexedBy(String.class, Index.ConstraintType.EQUAL, 1, p -> p.getName(), "Mark"), reactOn("name", "age")), pattern(olderV).expr("exprB", p -> !p.getName().equals("Mark"), alphaIndexedBy(String.class, Index.ConstraintType.NOT_EQUAL, 1, p -> p.getName(), "Mark"), reactOn("name")).expr("exprC", markV, (p1, p2) -> p1.getAge() > p2.getAge(), betaIndexedBy(int.class, Index.ConstraintType.GREATER_THAN, 0, p -> p.getAge(), p -> p.getAge()), reactOn("age")), on(olderV, markV).execute((p1, p2) -> result.setValue(p1.getName() + " is older than " + p2.getName())));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Person mark = new Person("Mark", 37);
Person edson = new Person("Edson", 35);
Person mario = new Person("Mario", 40);
FactHandle markFH = ksession.insert(mark);
FactHandle edsonFH = ksession.insert(edson);
FactHandle marioFH = ksession.insert(mario);
ksession.fireAllRules();
assertEquals("Mario is older than Mark", result.getValue());
result.setValue(null);
ksession.delete(marioFH);
ksession.fireAllRules();
assertNull(result.getValue());
mark.setAge(34);
ksession.update(markFH, mark, "age");
ksession.fireAllRules();
assertEquals("Edson is older than Mark", result.getValue());
}
use of org.drools.model.Variable in project drools by kiegroup.
the class PatternDSLTest method testReactiveOOPath.
@Test
public void testReactiveOOPath() {
Global<List> listG = globalOf(List.class, "defaultpkg", "list");
Variable<Man> manV = declarationOf(Man.class);
Variable<Woman> wifeV = declarationOf(Woman.class, reactiveFrom(manV, Man::getWife));
Variable<Child> childV = declarationOf(Child.class, reactiveFrom(wifeV, Woman::getChildren));
Variable<Toy> toyV = declarationOf(Toy.class, reactiveFrom(childV, Child::getToys));
Rule rule = rule("oopath").build(pattern(manV), pattern(wifeV), pattern(childV).expr("exprA", c -> c.getAge() > 10), pattern(toyV), on(toyV, listG).execute((t, l) -> l.add(t.getName())));
Model model = new ModelImpl().addGlobal(listG).addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
final List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
final Woman alice = new Woman("Alice", 38);
final Man bob = new Man("Bob", 40);
bob.setWife(alice);
final Child charlie = new Child("Charles", 12);
final Child debbie = new Child("Debbie", 10);
alice.addChild(charlie);
alice.addChild(debbie);
charlie.addToy(new Toy("car"));
charlie.addToy(new Toy("ball"));
debbie.addToy(new Toy("doll"));
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("car", "ball");
list.clear();
debbie.setAge(11);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("doll");
}
use of org.drools.model.Variable in project drools by kiegroup.
the class PatternDSLTest method testAccumuluateWithAnd2.
@Test
public void testAccumuluateWithAnd2() {
Variable<Object> var_$pattern_Object$1$ = declarationOf(Object.class, "$pattern_Object$1$");
Variable<Child> var_$c = declarationOf(Child.class, "$c");
Variable<Adult> var_$a = declarationOf(Adult.class, "$a");
Variable<Integer> var_$parentAge = declarationOf(Integer.class, "$parentAge");
Variable<Integer> var_$expr$5$ = declarationOf(Integer.class, "$expr$5$");
Rule rule = rule("R").build(accumulate(and(pattern(var_$c).expr("$expr$1$", (_this) -> _this.getAge() < 10, alphaIndexedBy(int.class, Index.ConstraintType.LESS_THAN, 0, _this -> _this.getAge(), 10), reactOn("age")), pattern(var_$a).expr("$expr$2$", var_$c, (_this, $c) -> _this.getName().equals($c.getParent()), reactOn("name")).bind(var_$expr$5$, var_$c, ($a, $c) -> $a.getAge() + $c.getAge())), accFunction(org.drools.core.base.accumulators.IntegerSumAccumulateFunction.class, var_$expr$5$).as(var_$parentAge)), on(var_$parentAge).execute((drools, $parentAge) -> {
drools.insert(new Result($parentAge));
}));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Adult a = new Adult("Mario", 43);
Child c = new Child("Sofia", 6, "Mario");
ksession.insert(a);
ksession.insert(c);
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertThat(results, hasItem(new Result(49)));
}
use of org.drools.model.Variable in project drools by kiegroup.
the class PatternDSLTest method testNegatedAfter.
@Test
public void testNegatedAfter() throws Exception {
Variable<StockTick> var_$a = declarationOf(StockTick.class, "$a");
Variable<StockTick> var_$b = declarationOf(StockTick.class, "$b");
Rule rule = rule("R").build(pattern(var_$a).expr("$expr$1$", (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCompany(), "DROO"), alphaIndexedBy(String.class, Index.ConstraintType.EQUAL, 0, _this -> _this.getCompany(), "DROO"), reactOn("company")), pattern(var_$b).expr("$expr$2$", (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCompany(), "ACME"), alphaIndexedBy(String.class, Index.ConstraintType.EQUAL, 0, _this -> _this.getCompany(), "ACME"), reactOn("company")).expr("$expr$3$", var_$a, not(after(5, java.util.concurrent.TimeUnit.SECONDS, 8, java.util.concurrent.TimeUnit.SECONDS))), execute(() -> {
System.out.println("fired");
}));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model, EventProcessingOption.STREAM);
KieSessionConfiguration conf = KieServices.get().newKieSessionConfiguration();
conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = kieBase.newKieSession(conf, null);
SessionPseudoClock clock = ksession.getSessionClock();
ksession.insert(new StockTick("DROO"));
clock.advanceTime(6, TimeUnit.SECONDS);
ksession.insert(new StockTick("ACME"));
assertEquals(0, ksession.fireAllRules());
clock.advanceTime(4, TimeUnit.SECONDS);
ksession.insert(new StockTick("ACME"));
assertEquals(1, ksession.fireAllRules());
}
use of org.drools.model.Variable in project drools by kiegroup.
the class RuleUnitTest method testRuleUnit.
@Test
public void testRuleUnit() {
List<String> result = new ArrayList<>();
Variable<Person> adult = declarationOf(Person.class, unitData("persons"));
Rule rule = rule("org.drools.retebuilder", "Adult").unit(AdultUnit.class).build(expr("$expr$1$", adult, p -> p.getAge() > 18), on(adult).execute(p -> {
System.out.println(p.getName());
result.add(p.getName());
}));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kieBase);
executor.newDataSource("persons", new Person("Mario", 43), new Person("Marilena", 44), new Person("Sofia", 5));
executor.run(AdultUnit.class);
assertTrue(result.containsAll(asList("Mario", "Marilena")));
}
Aggregations