use of org.drools.model.Variable in project drools by kiegroup.
the class FlowTest method testBetaWithDeclaration.
@Test
public void testBetaWithDeclaration() {
Variable<Person> markV = declarationOf(Person.class);
Variable<Integer> markAge = declarationOf(Integer.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(// also react on age, see RuleDescr.lookAheadFieldsOfIdentifier
"name"), bind(markAge).as(markV, Person::getAge).reactOn("age"), expr("exprB", olderV, p -> !p.getName().equals("Mark")).indexedBy(String.class, ConstraintType.NOT_EQUAL, 1, Person::getName, "Mark").reactOn("name"), expr("exprC", olderV, markAge, (p1, age) -> p1.getAge() > age).indexedBy(int.class, ConstraintType.GREATER_THAN, 0, Person::getAge, int.class::cast).reactOn("age"), on(olderV, markV).execute((drools, p1, p2) -> drools.insert(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();
Collection<String> results = getObjectsIntoList(ksession, String.class);
Assertions.assertThat(results).containsExactlyInAnyOrder("Mario is older than Mark");
ksession.delete(marioFH);
ksession.fireAllRules();
mark.setAge(34);
ksession.update(markFH, mark, "age");
ksession.fireAllRules();
results = getObjectsIntoList(ksession, String.class);
Assertions.assertThat(results).containsExactlyInAnyOrder("Mario is older than Mark", "Edson is older than Mark");
}
use of org.drools.model.Variable in project drools by kiegroup.
the class FlowTest method testQuery.
@Test
public void testQuery() {
Variable<Person> personV = declarationOf(Person.class, "$p");
Query1Def<Integer> qdef = query("olderThan", Integer.class);
Query query = qdef.build(expr("exprA", personV, qdef.getArg1(), (p, a) -> p.getAge() > a));
Model model = new ModelImpl().addQuery(query);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Mark", 39));
ksession.insert(new Person("Mario", 41));
QueryResults results = ksession.getQueryResults("olderThan", 40);
assertEquals(1, results.size());
Person p = (Person) results.iterator().next().get("$p");
assertEquals("Mario", p.getName());
}
use of org.drools.model.Variable in project drools by kiegroup.
the class OOPathFlowTest method testOOPathCast.
@Test
public void testOOPathCast() {
Global<List> listG = globalOf(List.class, "defaultpkg", "list");
Variable<Man> manV = declarationOf(Man.class, "$man");
// Drools doc: In this way subsequent constraints can also safely access to properties declared only in that subclass
Variable<InternationalAddress> addressV = declarationOf(InternationalAddress.class, "$italy", reactiveFrom(manV, Man::getAddress));
Rule rule = rule("oopath").build(expr("exprA", addressV, c -> c.getState().equals("Italy")), on(manV, 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 Man bob = new Man("Bob", 40);
bob.setAddress(new InternationalAddress("Via Verdi", "Italy"));
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("Bob");
}
use of org.drools.model.Variable in project drools by kiegroup.
the class OOPathFlowTest method testOOPath.
@Test
public void testOOPath() {
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));
Rule rule = rule("oopath").build(expr("exprA", childV, c -> c.getAge() > 10), on(manV, 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);
final Man carl = new Man("Bob", 42);
bob.setWife(alice);
final Child charlie = new Child("Charles", 12);
final Child debbie = new Child("Debbie", 10);
alice.addChild(charlie);
alice.addChild(debbie);
ksession.insert(bob);
ksession.insert(carl);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("Bob");
}
use of org.drools.model.Variable in project drools by kiegroup.
the class ViewFlowBuilder method ensureVariablesDeclarationInView.
private static CompositePatterns ensureVariablesDeclarationInView(CompositePatterns view, BuildContext ctx) {
ctx.inputs.keySet().removeAll(ctx.usedVars);
int i = 0;
for (Map.Entry<Variable<?>, InputViewItemImpl<?>> entry : ctx.inputs.entrySet()) {
view.addCondition(i++, new PatternImpl(entry.getKey()));
ctx.usedVars.add(entry.getKey());
}
view.ensureVariablesDeclarationInView();
view.getSubConditions().sort(ConditionComparator.INSTANCE);
return view;
}
Aggregations