use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class CompilerTest method testSimpleInsert.
@Test
public void testSimpleInsert() {
String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + " $p : Person( name.length == 4 )\n" + "then\n" + " Result r = new Result($p.getName());" + " insert(r);\n" + "end";
KieSession ksession = getKieSession(str);
ksession.insert(new Person("Mark", 37));
ksession.insert(new Person("Mario", 40));
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(1, results.size());
assertEquals("Mark", results.iterator().next().getValue());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class CompilerTest method testBindWith2Arguments.
@Test
public void testBindWith2Arguments() {
String str = "import " + Adult.class.getCanonicalName() + ";\n" + "import " + Child.class.getCanonicalName() + ";\n" + "import " + Result.class.getCanonicalName() + ";\n" + "rule R when\n" + " $y : Adult( $sum : (name.length + age) )\n" + "then\n" + " insert(new Result($sum));\n" + "end";
KieSession ksession = getKieSession(str);
Adult a = new Adult("Mario", 43);
ksession.insert(a);
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(((Number) results.iterator().next().getValue()).intValue(), 48);
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class CompilerTest method testAgeWithSumUsing2DeclarationInBeta.
@Test
public void testAgeWithSumUsing2DeclarationInBeta() {
String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + " $p : Person( personAge : age )\n" + " $plusTwo : Person(age == personAge + 2 + $p.age - $p.age )\n" + "then\n" + " insert(new Result($plusTwo.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);
System.out.println(results);
assertEquals(1, results.size());
assertEquals("Mark", results.iterator().next().getValue());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class CompilerTest method testBetaWithDeclaration.
@Test
public void testBetaWithDeclaration() {
String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + " $r : Result()\n" + " $p1 : Person(name == \"Mark\", $markAge : age)\n" + " $p2 : Person(name != \"Mark\", age > $markAge)\n" + "then\n" + " $r.setValue($p2.getName() + \" is older than \" + $p1.getName());\n" + "end";
KieSession ksession = getKieSession(str);
Result result = new Result();
ksession.insert(result);
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();
assertEquals("Mario is older than Mark", result.getValue());
result.setValue(null);
ksession.delete(marioFH);
ksession.fireAllRules();
assertNull(result.getValue());
mark.setAge(34);
ksession.update(markFH, mark, "age");
ksession.fireAllRules();
assertEquals("Edson is older than Mark", result.getValue());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class PatternDSLTest method testQueryInRule.
@Test
public void testQueryInRule() {
Variable<Person> personV = DSL.declarationOf(Person.class);
Query2Def<Person, Integer> qdef = query("olderThan", Person.class, Integer.class);
Query query = qdef.build(pattern(qdef.getArg1()).expr("exprA", qdef.getArg2(), (p, a) -> p.getAge() > a));
Variable<Person> personVRule = DSL.declarationOf(Person.class);
Rule rule = rule("R").build(qdef.call(personVRule, valueOf(40)), on(personVRule).execute((drools, p) -> drools.insert(new Result(p.getName()))));
Model model = new ModelImpl().addQuery(query).addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Mark", 39));
ksession.insert(new Person("Mario", 41));
ksession.fireAllRules();
Collection<Result> results = (Collection<Result>) ksession.getObjects(new ClassObjectFilter(Result.class));
assertEquals(1, results.size());
assertEquals("Mario", results.iterator().next().getValue());
}
Aggregations