use of org.drools.model.impl.ModelImpl in project drools by kiegroup.
the class FlowTest method testFrom.
@Test
public void testFrom() throws Exception {
Variable<Result> resultV = declarationOf(Result.class);
Variable<Adult> dadV = declarationOf(Adult.class);
Variable<Child> childV = declarationOf(Child.class, from(dadV, adult -> adult.getChildren()));
Rule rule = rule("from").build(expr("exprA", childV, c -> c.getAge() > 8), on(childV, resultV).execute((c, r) -> r.setValue(c.getName())));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Result result = new Result();
ksession.insert(result);
Adult dad = new Adult("dad", 40);
dad.addChild(new Child("Alan", 10));
dad.addChild(new Child("Betty", 7));
ksession.insert(dad);
ksession.fireAllRules();
assertEquals("Alan", result.getValue());
}
use of org.drools.model.impl.ModelImpl in project drools by kiegroup.
the class FlowTest method testQueryInvokingQuery.
@Test
public void testQueryInvokingQuery() {
Variable<Relationship> relV = declarationOf(Relationship.class);
Query2Def<String, String> query1Def = query("isRelatedTo1", String.class, String.class);
Query2Def<String, String> query2Def = query("isRelatedTo2", String.class, String.class);
Query query2 = query2Def.build(expr("exprA", relV, query2Def.getArg1(), (r, s) -> r.getStart().equals(s)), expr("exprB", relV, query2Def.getArg2(), (r, e) -> r.getEnd().equals(e)));
Query query1 = query1Def.build(query2Def.call(query1Def.getArg1(), query1Def.getArg2()));
Model model = new ModelImpl().addQuery(query2).addQuery(query1);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Relationship("A", "B"));
ksession.insert(new Relationship("B", "C"));
QueryResults results = ksession.getQueryResults("isRelatedTo1", "A", "B");
assertEquals(1, results.size());
assertEquals("B", results.iterator().next().get(query1Def.getArg2().getName()));
}
use of org.drools.model.impl.ModelImpl in project drools by kiegroup.
the class FlowTest method testQueryInRuleWithDeclaration.
@Test
public void testQueryInRuleWithDeclaration() {
Variable<Person> personV = declarationOf(Person.class);
Query2Def<Person, Integer> qdef = query("olderThan", Person.class, Integer.class);
Query query = qdef.build(expr("exprA", qdef.getArg1(), qdef.getArg2(), (p, a) -> p.getAge() > a));
Rule rule = rule("R").build(expr("exprB", personV, p -> p.getName().startsWith("M")), qdef.call(personV, valueOf(40)), on(personV).execute((drools, p) -> drools.insert(new Result(p.getName()))));
Model model = new ModelImpl().addQuery(query).addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Mark", 39));
ksession.insert(new Person("Mario", 41));
ksession.insert(new Person("Edson", 41));
ksession.fireAllRules();
Collection<Result> results = (Collection<Result>) ksession.getObjects(new ClassObjectFilter(Result.class));
assertEquals(1, results.size());
assertEquals("Mario", results.iterator().next().getValue());
}
use of org.drools.model.impl.ModelImpl in project drools by kiegroup.
the class FlowTest method testCustomAccumulate.
@Test
public void testCustomAccumulate() {
final Variable<Customer> var_$customer = declarationOf(Customer.class, "$customer");
final Variable<String> var_$code = declarationOf(String.class, "$code");
final Variable<TargetPolicy> var_$target = declarationOf(TargetPolicy.class, "$target");
final Variable<java.util.List> var_$pattern_List$1$ = declarationOf(java.util.List.class, "$pattern_List$1$");
final Variable<TargetPolicy> var_$tp = declarationOf(TargetPolicy.class, "$tp");
final org.drools.model.BitMask mask_$target = org.drools.model.BitMask.getPatternMask(TargetPolicy.class, "coefficient");
org.drools.model.Rule rule = rule("Customer can only have one Target Policy for Product p1 with coefficient 1").build(bind(var_$code).as(var_$customer, (_this) -> _this.getCode()).reactOn("code"), expr("$expr$2$", var_$target, var_$code, (_this, $code) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCustomerCode(), $code)).indexedBy(String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getCustomerCode(), $code -> $code).reactOn("customerCode"), expr("$expr$3$", var_$target, (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getProductCode(), "p1")).indexedBy(String.class, org.drools.model.Index.ConstraintType.EQUAL, 1, _this -> _this.getProductCode(), "p1").reactOn("productCode"), expr("$expr$4$", var_$target, (_this) -> _this.getCoefficient() == 1).indexedBy(int.class, org.drools.model.Index.ConstraintType.EQUAL, 2, _this -> _this.getCoefficient(), 1).reactOn("coefficient"), expr("$expr$5$", var_$pattern_List$1$, (_this) -> _this.size() > 1).indexedBy(int.class, org.drools.model.Index.ConstraintType.GREATER_THAN, 0, _this -> _this.size(), 1).reactOn("size"), accumulate(and(expr("$expr$2$", var_$tp, var_$code, (_this, $code) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCustomerCode(), $code)).indexedBy(String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getCustomerCode(), $code -> $code).reactOn("customerCode"), expr("$expr$3$", var_$tp, (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getProductCode(), "p1")).indexedBy(String.class, org.drools.model.Index.ConstraintType.EQUAL, 1, _this -> _this.getProductCode(), "p1").reactOn("productCode"), expr("$expr$4$", var_$tp, (_this) -> _this.getCoefficient() == 1).indexedBy(int.class, org.drools.model.Index.ConstraintType.EQUAL, 2, _this -> _this.getCoefficient(), 1).reactOn("coefficient")), accFunction(MyAccumulateFunction.class, var_$tp).as(var_$pattern_List$1$)), on(var_$target).execute((drools, $target) -> {
$target.setCoefficient(0);
drools.update($target, mask_$target);
}));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Customer customer = new Customer();
customer.setCode("code1");
TargetPolicy target1 = new TargetPolicy();
target1.setCustomerCode("code1");
target1.setProductCode("p1");
target1.setCoefficient(1);
TargetPolicy target2 = new TargetPolicy();
target2.setCustomerCode("code1");
target2.setProductCode("p1");
target2.setCoefficient(1);
TargetPolicy target3 = new TargetPolicy();
target3.setCustomerCode("code1");
target3.setProductCode("p1");
target3.setCoefficient(1);
ksession.insert(customer);
ksession.insert(target1);
ksession.insert(target2);
ksession.insert(target3);
ksession.fireAllRules();
List<TargetPolicy> targetPolicyList = Arrays.asList(target1, target2, target3);
long filtered = targetPolicyList.stream().filter(c -> c.getCoefficient() == 1).count();
assertEquals(1, filtered);
}
use of org.drools.model.impl.ModelImpl in project drools by kiegroup.
the class FlowTest method testConcatenatedFrom.
@Test
public void testConcatenatedFrom() {
Global<List> listG = globalOf(List.class, "defaultpkg", "list");
Variable<Man> manV = declarationOf(Man.class);
Variable<Woman> wifeV = declarationOf(Woman.class, from(manV, Man::getWife));
Variable<Child> childV = declarationOf(Child.class, from(wifeV, Woman::getChildren));
Variable<Toy> toyV = declarationOf(Toy.class, from(childV, Child::getToys));
Rule rule = rule("froms").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");
}
Aggregations