Search in sources :

Example 86 with Result

use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.

the class AccumulateTest method testAccumulateWithProperty.

@Test
public void testAccumulateWithProperty() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule X when\n" + "  accumulate ( $person: Person ( getName().startsWith(\"M\")); \n" + "                $sum : sum($person.getAge())  \n" + "              )                          \n" + "then\n" + "  insert(new Result($sum));\n" + "end";
    KieSession ksession = getKieSession(str);
    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());
}
Also used : KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Result(org.drools.modelcompiler.domain.Result) Test(org.junit.Test)

Example 87 with Result

use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.

the class CompilerTest method testRuleExtends.

@Test
public void testRuleExtends() {
    String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R1 when\n" + "  $r : Result()\n" + "then\n" + "end\n" + "rule R2 extends R1 when\n" + "  $p1 : Person(name == \"Mark\")\n" + "then\n" + "end\n" + "rule R3 extends R2 when\n" + "  $p2 : Person(name != \"Mark\", age > $p1.age)\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);
    assertEquals(1, ksession.fireAllRules());
    ksession.insert(new Person("Edson", 35));
    ksession.insert(new Person("Mario", 40));
    assertEquals(0, ksession.fireAllRules());
    ksession.insert(new Person("Mark", 37));
    assertEquals(2, ksession.fireAllRules());
    assertEquals("Mario is older than Mark", result.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)

Example 88 with Result

use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.

the class CompilerTest method testSimpleDelete.

@Test
public void testSimpleDelete() {
    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" + "  delete($p);\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());
    assertEquals(1, getObjectsIntoList(ksession, Person.class).size());
}
Also used : KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Result(org.drools.modelcompiler.domain.Result) Test(org.junit.Test)

Example 89 with Result

use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.

the class CompilerTest method testNonBreakingNamedConsequence.

@Test
public void testNonBreakingNamedConsequence() {
    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 do[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(2, results.size());
    assertTrue(results.containsAll(asList("Found Mark", "Mario is older than Mark")));
}
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 90 with Result

use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.

the class CompilerTest method testEqualityCheckOnNull.

@Test
public void testEqualityCheckOnNull() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule R when\n" + "  Person($name : name == \"Mario\")\n" + "then\n" + "  insert(new Result($name));\n" + "end";
    KieSession ksession = getKieSession(str);
    final Person mario = new Person("Mario", 40);
    final Person luca = new Person(null, 33);
    ksession.insert(mario);
    ksession.insert(luca);
    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