use of org.drools.model.Prototype in project drools by kiegroup.
the class FactTemplateTest method testIndexedAlpha.
@Test
public void testIndexedAlpha() {
Prototype personFact = prototype("org.drools.Person", "name", "age");
PrototypeVariable personV = variable(personFact);
Rule rule1 = rule("alpha1").build(protoPattern(personV).expr("name", Index.ConstraintType.EQUAL, "Mark"), on(personV).execute((drools, p) -> drools.insert(new Result("Found a " + p.get("age") + " years old Mark"))));
Rule rule2 = rule("alpha2").build(protoPattern(personV).expr("name", Index.ConstraintType.EQUAL, "Mario"), on(personV).execute((drools, p) -> drools.insert(new Result("Found a " + p.get("age") + " years old Mario"))));
Rule rule3 = rule("alpha3").build(protoPattern(personV).expr("name", Index.ConstraintType.EQUAL, "Edson"), on(personV).execute((drools, p) -> drools.insert(new Result("Found a " + p.get("age") + " years old Edson"))));
Model model = new ModelImpl().addRule(rule1).addRule(rule2).addRule(rule3);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ObjectTypeNode otn = getFactTemplateObjectTypeNode(ksession, "Person");
assertEquals(3, ((CompositeObjectSinkAdapter) otn.getObjectSinkPropagator()).getHashedSinkMap().size());
Fact mark = createMapBasedFact(personFact);
mark.set("name", "Mark");
mark.set("age", 40);
ksession.insert(mark);
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(1, results.size());
assertThat(results, hasItem(new Result("Found a 40 years old Mark")));
}
use of org.drools.model.Prototype in project drools by kiegroup.
the class FactTemplateTest method testAlpha.
private void testAlpha(Prototype personFact, boolean hasFields) {
PrototypeVariable markV = variable(personFact);
Rule rule = rule("alpha").build(protoPattern(markV).expr("name", Index.ConstraintType.EQUAL, "Mark"), on(markV).execute((drools, p) -> drools.insert(new Result("Found a " + p.get("age") + " years old Mark"))));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
assertTrue(hasFactTemplateObjectType(ksession, "Person"));
Fact mark = createMapBasedFact(personFact);
mark.set("name", "Mark");
mark.set("age", 40);
FactHandle fh = ksession.insert(mark);
assertEquals(1, ksession.fireAllRules());
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertThat(results, hasItem(new Result("Found a 40 years old Mark")));
mark.set("age", 41);
ksession.update(fh, mark, "age");
// property reactivity should prevent this firing
assertEquals(hasFields ? 0 : 1, ksession.fireAllRules());
ksession.update(fh, mark, "age", "name");
// saying to the engine that also name is changed to check if this time the update is processed as it should
assertEquals(1, ksession.fireAllRules());
}
Aggregations