use of org.kie.api.runtime.KieSession 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.kie.api.runtime.KieSession 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.kie.api.runtime.KieSession in project drools by kiegroup.
the class KieBuilderTest method testDrlBuild.
@Test
public void testDrlBuild() throws Exception {
KieSession ksession = checkKieSession(DrlProject.class);
assertTrue(getAlphaConstraint(ksession) instanceof MvelConstraint);
}
use of org.kie.api.runtime.KieSession in project drools by kiegroup.
the class KieBuilderTest method testFlowModelBuild.
@Test
public void testFlowModelBuild() throws Exception {
KieSession ksession = checkKieSession(ExecutableModelFlowProject.class);
assertTrue(getAlphaConstraint(ksession) instanceof LambdaConstraint);
}
use of org.kie.api.runtime.KieSession 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");
}
Aggregations