use of org.drools.model.Variable in project drools by kiegroup.
the class FlowTest method testQueryOOPathAccumulateTransformed.
@Test
public void testQueryOOPathAccumulateTransformed() {
final org.drools.model.QueryDef queryDef_listSafeCities = query("listSafeCities");
final Variable<org.drools.modelcompiler.oopathdtables.Person> var_$p = declarationOf(org.drools.modelcompiler.oopathdtables.Person.class, "$p");
final Variable<org.drools.modelcompiler.oopathdtables.InternationalAddress> var_$a = declarationOf(org.drools.modelcompiler.oopathdtables.InternationalAddress.class, "$a", from(var_$p, (_this) -> _this.getAddress()));
final Variable<java.util.List> var_$cities = declarationOf(java.util.List.class, "$cities");
final Variable<String> var_$city = declarationOf(String.class, "$city", from(var_$a, (_this) -> _this.getCity()));
org.drools.model.Query listSafeCities_build = queryDef_listSafeCities.build(input(var_$p), expr("$expr$2$", var_$a, (_this) -> _this.getState().equals("Safecountry")).indexedBy(String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getState(), "Safecountry").reactOn("state"), accumulate(expr(var_$city, s -> true), accFunction(org.drools.core.base.accumulators.CollectListAccumulateFunction.class, var_$city).as(var_$cities)));
Model model = new ModelImpl().addQuery(listSafeCities_build);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
org.drools.modelcompiler.oopathdtables.Person person = new org.drools.modelcompiler.oopathdtables.Person();
person.setAddress(new InternationalAddress("", 1, "Milan", "Safecountry"));
ksession.insert(person);
org.drools.modelcompiler.oopathdtables.Person person2 = new org.drools.modelcompiler.oopathdtables.Person();
person2.setAddress(new InternationalAddress("", 1, "Rome", "Unsafecountry"));
ksession.insert(person2);
QueryResults results = ksession.getQueryResults("listSafeCities");
assertEquals("Milan", ((List) results.iterator().next().get("$cities")).iterator().next());
}
use of org.drools.model.Variable in project drools by kiegroup.
the class FlowTest method testAccumulateAnd.
@Test
public void testAccumulateAnd() {
Variable<Number> var_$sum = declarationOf(Number.class, "$sum");
Variable<Person> var_$p = declarationOf(Person.class, "$p");
Variable<Integer> var_$expr$3$ = declarationOf(Integer.class, "$expr$3$");
org.drools.model.Rule rule = rule("X").build(bind(var_$expr$3$).as(var_$p, (_this) -> _this.getAge()), accumulate(and(expr("$expr$1$", var_$p, (_this) -> _this.getAge() > 10).indexedBy(int.class, org.drools.model.Index.ConstraintType.GREATER_THAN, 0, _this -> _this.getAge(), 10).reactOn("age"), expr("$expr$2$", var_$p, (_this) -> _this.getName().startsWith("M"))), accFunction(org.drools.core.base.accumulators.IntegerSumAccumulateFunction.class, var_$expr$3$).as(var_$sum)), on(var_$sum).execute((drools, $sum) -> {
drools.insert(new Result($sum));
}));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Mark", 37));
ksession.insert(new Person("Edson", 35));
ksession.insert(new Person("Mario", 40));
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(1, results.size());
assertEquals(77, results.iterator().next().getValue());
}
use of org.drools.model.Variable in project drools by kiegroup.
the class OOPathFlowTest 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(expr("exprA", childV, c -> c.getAge() > 10), 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 OOPathFlowTest method testBackReferenceConstraint.
@Test
public void testBackReferenceConstraint() {
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(expr("exprA", toyV, childV, (t, c) -> t.getName().length() == c.getName().length()), 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("Carl", 12);
final Child debbie = new Child("Debbie", 8);
alice.addChild(charlie);
alice.addChild(debbie);
charlie.addToy(new Toy("car"));
charlie.addToy(new Toy("ball"));
debbie.addToy(new Toy("doll"));
debbie.addToy(new Toy("guitar"));
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("ball", "guitar");
}
use of org.drools.model.Variable in project drools by kiegroup.
the class OOPathFlowTest method testQueryOOPathAccumulate.
@Test
public void testQueryOOPathAccumulate() {
QueryDef queryDef_listSafeCities = query("listSafeCities");
Variable<java.util.List> var_$cities = declarationOf(java.util.List.class, "$cities");
Variable<org.drools.modelcompiler.oopathdtables.Person> var_$p = declarationOf(org.drools.modelcompiler.oopathdtables.Person.class, "$p");
Variable<org.drools.modelcompiler.oopathdtables.InternationalAddress> var_$a = declarationOf(org.drools.modelcompiler.oopathdtables.InternationalAddress.class, "$a", from(var_$p, (_this) -> _this.getAddress()));
Variable<String> var_$city = declarationOf(String.class, "$city", from(var_$a, (_this) -> _this.getCity()));
org.drools.model.Query listSafeCities_build = queryDef_listSafeCities.build(accumulate(and(input(var_$p), expr("$expr$2$", var_$a, (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getState(), "Safecountry")).indexedBy(java.lang.String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getState(), "Safecountry").reactOn("state"), expr(var_$city)), accFunction(org.drools.core.base.accumulators.CollectListAccumulateFunction.class, var_$city).as(var_$cities)));
Model model = new ModelImpl().addQuery(listSafeCities_build);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
org.drools.modelcompiler.oopathdtables.Person person = new org.drools.modelcompiler.oopathdtables.Person();
person.setAddress(new org.drools.modelcompiler.oopathdtables.InternationalAddress("", 1, "Milan", "Safecountry"));
ksession.insert(person);
org.drools.modelcompiler.oopathdtables.Person person2 = new org.drools.modelcompiler.oopathdtables.Person();
person2.setAddress(new org.drools.modelcompiler.oopathdtables.InternationalAddress("", 1, "Rome", "Unsafecountry"));
ksession.insert(person2);
QueryResults results = ksession.getQueryResults("listSafeCities");
List cities = (List) results.iterator().next().get("$cities");
assertEquals(1, cities.size());
assertEquals("Milan", cities.get(0));
}
Aggregations