use of org.drools.model.impl.ModelImpl in project drools by kiegroup.
the class FlowTest method testBreakingNamedConsequence.
@Test
public void testBreakingNamedConsequence() {
Variable<Result> resultV = declarationOf(Result.class);
Variable<Person> markV = declarationOf(Person.class);
Variable<Person> olderV = declarationOf(Person.class);
Rule rule = rule("beta").build(expr("exprA", markV, p -> p.getName().equals("Mark")).indexedBy(String.class, ConstraintType.EQUAL, 1, Person::getName, "Mark").reactOn("name", "age"), when("cond1", markV, p -> p.getAge() < 30).then(on(markV, resultV).breaking().execute((p, r) -> r.addValue("Found young " + p.getName()))).elseWhen("cond2", markV, p -> p.getAge() > 50).then(on(markV, resultV).breaking().execute((p, r) -> r.addValue("Found old " + p.getName()))).elseWhen().then(on(markV, resultV).breaking().execute((p, r) -> r.addValue("Found " + p.getName()))), expr("exprB", olderV, p -> !p.getName().equals("Mark")).indexedBy(String.class, ConstraintType.NOT_EQUAL, 1, Person::getName, "Mark").reactOn("name"), expr("exprC", olderV, markV, (p1, p2) -> p1.getAge() > p2.getAge()).indexedBy(int.class, ConstraintType.GREATER_THAN, 0, Person::getAge, Person::getAge).reactOn("age"), on(olderV, markV, resultV).execute((p1, p2, r) -> r.addValue(p1.getName() + " is older than " + p2.getName())));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Result result = new Result();
ksession.insert(result);
ksession.insert(new Person("Mark", 37));
ksession.insert(new Person("Edson", 35));
ksession.insert(new Person("Mario", 40));
ksession.fireAllRules();
Collection<String> results = (Collection<String>) result.getValue();
assertEquals(1, results.size());
assertEquals("Found Mark", results.iterator().next());
}
use of org.drools.model.impl.ModelImpl 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.impl.ModelImpl 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.impl.ModelImpl 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.impl.ModelImpl 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");
}
Aggregations