use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class ExisistentialTest method testNotEmptyPredicate.
@Test
public void testNotEmptyPredicate() {
String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule R when\n" + " not( Person( ) )\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(0, results.size());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class ExisistentialTest method testNot.
@Test
public void testNot() {
String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule R when\n" + " not( Person( name.length == 4 ) )\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 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.modelcompiler.domain.Result 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")));
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class FlowTest method testFromAccumulate.
@Test
public void testFromAccumulate() {
final Variable<Number> var_$sum = declarationOf(Number.class, "$sum");
final Variable<Person> var_$p = declarationOf(Person.class, "$p");
final Variable<Integer> var_$expr$3$ = declarationOf(Integer.class, "$expr$3$");
org.drools.model.Rule rule = rule("X").build(bind(var_$expr$3$).as(var_$p, (_this) -> _this.getAge()), expr("$expr$1$", var_$sum, (_this) -> _this.intValue() > 0).reactOn("intValue"), accumulate(expr("$expr$2$", var_$p, (_this) -> _this.getName().startsWith("M")), accFunction(org.drools.core.base.accumulators.IntegerSumAccumulateFunction.class, var_$expr$3$).as(var_$sum)), on(var_$sum).execute((drools, $sum) -> {
drools.insert(new Result($sum));
}));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Mark", 37));
ksession.insert(new Person("Edson", 35));
ksession.insert(new Person("Mario", 40));
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(1, results.size());
assertEquals(77, results.iterator().next().getValue());
}
Aggregations