use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class CompilerTest method testBreakingNamedConsequence.
@Test
public void testBreakingNamedConsequence() {
String str = "import " + Result.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + "rule R when\n" + " $r : Result()\n" + " $p1 : Person(name == \"Mark\")\n" + " if ( age < 30 ) break[FoundYoungMark]" + " else if ( age > 50) break[FoundOldMark]\n" + " else break[FoundMark]\n" + " $p2 : Person(name != \"Mark\", age > $p1.age)\n" + "then\n" + " $r.addValue($p2.getName() + \" is older than \" + $p1.getName());\n" + "then[FoundYoungMark]\n" + " $r.addValue(\"Found young \" + $p1.getName());\n" + "then[FoundOldMark]\n" + " $r.addValue(\"Found old \" + $p1.getName());\n" + "then[FoundMark]\n" + " $r.addValue(\"Found \" + $p1.getName());\n" + "end";
KieSession ksession = getKieSession(str);
Result result = new Result();
ksession.insert(result);
ksession.insert(new Person("Mark", 37));
ksession.insert(new Person("Edson", 35));
ksession.insert(new Person("Mario", 40));
ksession.fireAllRules();
Collection results = (Collection) result.getValue();
assertEquals(1, results.size());
assertEquals("Found Mark", results.iterator().next());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class CompilerTest method testBeta.
@Test
public void testBeta() {
String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + " $r : Result()\n" + " $markV : Person(name == \"Mark\")\n" + " $olderV : Person(name != \"Mark\", age > $markV.age)\n" + "then\n" + " $r.setValue($olderV.getName() + \" is older than \" + $markV.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 DeclaredTypesTest method testPojoInDifferentPackages.
@Test
public void testPojoInDifferentPackages() throws Exception {
String ruleWithPojo = "package org.drools.pojo.model;" + "\n" + "declare POJOPerson\n" + " name : String\n" + " surname : String\n" + " age : int\n" + "end\n";
String rule = "package org.drools.pojo;\n" + "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "import org.drools.pojo.model.*;" + "\n" + "rule R when\n" + " $p : Person( name.length == 4 )\n" + "then\n" + " POJOPerson p = new POJOPerson();\n" + " p.setName($p.getName());\n" + " insert(p);\n" + "end\n" + "rule R2 when\n" + " $p : POJOPerson( name.length == 4 )\n" + "then\n" + " insert(new Result($p));\n" + "end\n";
KieSession ksession = getKieSession(rule, ruleWithPojo);
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());
Result r = results.iterator().next();
Object result = r.getValue();
Class<?> resultClass = result.getClass();
Method name = resultClass.getMethod("getName");
assertEquals("org.drools.pojo.model.POJOPerson", resultClass.getName());
assertEquals("Mark", name.invoke(result));
Constructor<?>[] constructors = resultClass.getConstructors();
assertEquals(2, constructors.length);
Object instance1 = resultClass.newInstance();
Constructor<?> ctor = resultClass.getConstructor(String.class, String.class, int.class);
Object luca = ctor.newInstance("Luca", null, 32);
Method getName = resultClass.getMethod("getName");
Method getAge = resultClass.getMethod("getAge");
assertEquals("Luca", getName.invoke(luca));
assertEquals(32, getAge.invoke(luca));
assertEquals("POJOPerson( name=Luca, surname=null, age=32 )", luca.toString());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class DeclaredTypesTest method testDeclaredTypeInLhs.
@Test
public void testDeclaredTypeInLhs() throws Exception {
String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "\n" + "declare POJOPerson\n" + " name : String\n" + " surname : String\n" + " age : int\n" + "end\n" + "rule R1 when\n" + " $p : Person( name.length == 4 )\n" + "then\n" + " POJOPerson p = new POJOPerson();\n" + " p.setName($p.getName());\n" + " insert(p);\n" + "end\n" + "rule R2 when\n" + " $p : POJOPerson( name.length == 4 )\n" + "then\n" + " insert(new Result($p));\n" + "end\n";
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());
Result r = results.iterator().next();
Object result = r.getValue();
Class<?> resultClass = result.getClass();
Method name = resultClass.getMethod("getName");
assertEquals("defaultpkg.POJOPerson", resultClass.getName());
assertEquals("Mark", name.invoke(result));
Constructor<?>[] constructors = resultClass.getConstructors();
assertEquals(2, constructors.length);
Object instance1 = resultClass.newInstance();
Constructor<?> ctor = resultClass.getConstructor(String.class, String.class, int.class);
Object luca = ctor.newInstance("Luca", null, 32);
Method getName = resultClass.getMethod("getName");
Method getAge = resultClass.getMethod("getAge");
assertEquals("Luca", getName.invoke(luca));
assertEquals(32, getAge.invoke(luca));
assertEquals("POJOPerson( name=Luca, surname=null, age=32 )", luca.toString());
}
use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.
the class EvalTest method testEvalWithBinary.
@Test
public void testEvalWithBinary() {
String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + " $p : Person()\n" + " eval( $p.getAge() + 27 == 67 )\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());
}
Aggregations