use of org.drools.model.PrototypeVariable in project drools by kiegroup.
the class FactTemplateTest method testBeta.
@Test
public void testBeta() {
Result result = new Result();
Prototype personFact = prototype("org.drools", "Person", field("name", Integer.class), field("age", String.class));
PrototypeVariable markV = declarationOf(personFact);
PrototypeVariable olderV = declarationOf(personFact);
Rule rule = rule("beta").build(pattern(markV).expr("exprA", p -> p.get("name").equals("Mark"), alphaIndexedBy(String.class, Index.ConstraintType.EQUAL, 1, p -> (String) p.get("name"), "Mark"), reactOn("name", "age")), pattern(olderV).expr("exprB", p -> !p.get("name").equals("Mark"), alphaIndexedBy(String.class, Index.ConstraintType.NOT_EQUAL, 1, p -> (String) p.get("name"), "Mark"), reactOn("name")).expr("exprC", markV, (p1, p2) -> (int) p1.get("age") > (int) p2.get("age"), betaIndexedBy(int.class, Index.ConstraintType.GREATER_THAN, 0, p -> (int) p.get("age"), p -> (int) p.get("age")), reactOn("age")), on(olderV, markV).execute((p1, p2) -> result.setValue(p1.get("name") + " is older than " + p2.get("name"))));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
assertTrue(hasFactTemplateObjectType(ksession, "Person"));
Fact mark = createMapBasedFact(personFact);
mark.setFieldValue("name", "Mark");
mark.setFieldValue("age", 37);
Fact edson = createMapBasedFact(personFact);
edson.setFieldValue("name", "Edson");
edson.setFieldValue("age", 35);
Fact mario = createMapBasedFact(personFact);
mario.setFieldValue("name", "Mario");
mario.setFieldValue("age", 40);
FactHandle markFH = ksession.insert(mark);
FactHandle edsonFH = ksession.insert(edson);
FactHandle marioFH = ksession.insert(mario);
ksession.fireAllRules();
assertEquals("Mario is older than Mark", result.getValue());
result.setValue(null);
ksession.delete(marioFH);
ksession.fireAllRules();
assertNull(result.getValue());
mark.setFieldValue("age", 34);
ksession.update(markFH, mark);
ksession.fireAllRules();
assertEquals("Edson is older than Mark", result.getValue());
}
use of org.drools.model.PrototypeVariable in project drools by kiegroup.
the class FactTemplateTest method testAlpha.
@Test
public void testAlpha() {
Prototype personFact = prototype("org.drools", "Person", field("name", Integer.class), field("age", String.class));
PrototypeVariable markV = declarationOf(personFact);
Rule rule = rule("alpha").build(pattern(markV).expr("exprA", p -> p.get("name").equals("Mark"), alphaIndexedBy(String.class, Index.ConstraintType.EQUAL, 1, p -> (String) p.get("name"), "Mark"), reactOn("name", "age")), 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.setFieldValue("name", "Mark");
mark.setFieldValue("age", 40);
ksession.insert(mark);
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertThat(results, hasItem(new Result("Found a 40 years old Mark")));
}
use of org.drools.model.PrototypeVariable in project drools by kiegroup.
the class FactTemplateTest method testBetaMixingClassAndFact.
@Test
public void testBetaMixingClassAndFact() {
Prototype personFact = prototype("org.drools", "FactPerson", field("name", Integer.class), field("age", String.class));
PrototypeVariable markV = declarationOf(personFact);
Variable<Person> olderV = declarationOf(Person.class);
Rule rule = rule("beta").build(pattern(markV).expr("exprA", p -> p.get("name").equals("Mark"), alphaIndexedBy(String.class, Index.ConstraintType.EQUAL, 1, p -> (String) p.get("name"), "Mark"), reactOn("name", "age")), pattern(olderV).expr("exprB", p -> !p.getName().equals("Mark"), alphaIndexedBy(String.class, Index.ConstraintType.NOT_EQUAL, 1, p -> p.getName(), "Mark"), reactOn("name")).expr("exprC", markV, (p1, p2) -> p1.getAge() > (int) p2.get("age"), betaIndexedBy(int.class, Index.ConstraintType.GREATER_THAN, 0, p -> p.getAge(), p -> (int) p.get("age")), reactOn("age")), on(olderV, markV).execute((drools, p1, p2) -> drools.insert(new Result(p1.getName() + " is older than " + p2.get("name")))));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
assertTrue(hasFactTemplateObjectType(ksession, "FactPerson"));
Fact mark = createMapBasedFact(personFact);
mark.setFieldValue("name", "Mark");
mark.setFieldValue("age", 37);
ksession.insert(mark);
FactHandle edsonFH = ksession.insert(new Person("Edson", 35));
FactHandle marioFH = ksession.insert(new Person("Mario", 40));
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertThat(results, hasItem(new Result("Mario is older than Mark")));
}
Aggregations