Search in sources :

Example 61 with Result

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());
}
Also used : Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Result(org.drools.modelcompiler.domain.Result) Test(org.junit.Test)

Example 62 with Result

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());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Result(org.drools.modelcompiler.domain.Result) Test(org.junit.Test)

Example 63 with Result

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());
}
Also used : Constructor(java.lang.reflect.Constructor) KieSession(org.kie.api.runtime.KieSession) Method(java.lang.reflect.Method) Person(org.drools.modelcompiler.domain.Person) Result(org.drools.modelcompiler.domain.Result) Test(org.junit.Test)

Example 64 with Result

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());
}
Also used : Constructor(java.lang.reflect.Constructor) KieSession(org.kie.api.runtime.KieSession) Method(java.lang.reflect.Method) Person(org.drools.modelcompiler.domain.Person) Result(org.drools.modelcompiler.domain.Result) Test(org.junit.Test)

Example 65 with Result

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());
}
Also used : KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Result(org.drools.modelcompiler.domain.Result) Test(org.junit.Test)

Aggregations

Result (org.drools.modelcompiler.domain.Result)91 KieSession (org.kie.api.runtime.KieSession)91 Test (org.junit.Test)90 Person (org.drools.modelcompiler.domain.Person)84 FactHandle (org.kie.api.runtime.rule.FactHandle)36 Collection (java.util.Collection)35 Adult (org.drools.modelcompiler.domain.Adult)35 Child (org.drools.modelcompiler.domain.Child)35 Model (org.drools.model.Model)32 Rule (org.drools.model.Rule)32 Variable (org.drools.model.Variable)32 ModelImpl (org.drools.model.impl.ModelImpl)32 BaseModelTest.getObjectsIntoList (org.drools.modelcompiler.BaseModelTest.getObjectsIntoList)32 KieBaseBuilder (org.drools.modelcompiler.builder.KieBaseBuilder)32 CoreMatchers.hasItem (org.hamcrest.CoreMatchers.hasItem)32 Assert.assertEquals (org.junit.Assert.assertEquals)32 Assert.assertNull (org.junit.Assert.assertNull)32 Assert.assertThat (org.junit.Assert.assertThat)32 KieBase (org.kie.api.KieBase)32 ArrayList (java.util.ArrayList)30