use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class EvalTest method testFunction2.
@Test
public void testFunction2() {
String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "function Boolean isFortyYearsOld(Person p, Boolean booleanParameter) {\n" + " return p.getAge() == 40; \n" + "}" + "rule R when\n" + " $p : Person()\n" + " eval( isFortyYearsOld($p, true) )\n" + "then\n" + " insert(new Result($p.getName()));\n" + "end";
KieSession ksession = getKieSession(str);
ksession.insert(new Person("Mario", 40));
ksession.insert(new Person("Mark", 37));
ksession.insert(new Person("Edson", 35));
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(1, results.size());
assertEquals("Mario", results.iterator().next().getValue());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class ExisistentialTest method testExists.
@Test
public void testExists() {
String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule R when\n" + " exists Person( name.length == 5 )\n" + "then\n" + " insert(new Result(\"ok\"));\n" + "end";
KieSession ksession = getKieSession(str);
Person mario = new Person("Mario", 40);
ksession.insert(mario);
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(1, results.size());
assertEquals("ok", results.iterator().next().getValue());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class ExisistentialTest method testExistsEmptyPredicate.
@Test
public void testExistsEmptyPredicate() {
String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule R when\n" + " exists( Person() )\n" + "then\n" + " insert(new Result(\"ok\"));\n" + "end";
KieSession ksession = getKieSession(str);
Person mark = new Person("Mark", 37);
Person mario = new Person("Mario", 40);
ksession.insert(mark);
ksession.insert(mario);
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(1, results.size());
assertEquals("ok", results.iterator().next().getValue());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class ExisistentialTest method testForall.
@Test
public void testForall() {
String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule R when\n" + " forall( $p : Person( name.length == 5 ) " + " Person( this == $p, age > 40 ) )\n" + "then\n" + " insert(new Result(\"ok\"));\n" + "end";
KieSession ksession = getKieSession(str);
ksession.insert(new Person("Mario", 41));
ksession.insert(new Person("Mark", 39));
ksession.insert(new Person("Edson", 42));
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(1, results.size());
assertEquals("ok", results.iterator().next().getValue());
}
use of org.drools.modelcompiler.domain.Result 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());
}
Aggregations