Search in sources :

Example 6 with Result

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

the class AccumulateTest method testAccumulateWithAnd.

@Test
public void testAccumulateWithAnd() {
    String str = "import " + Adult.class.getCanonicalName() + ";\n" + "import " + Child.class.getCanonicalName() + ";\n" + "import " + Result.class.getCanonicalName() + ";\n" + "rule R when\n" + "  accumulate( $c : Child( age < 10 ) and $a : Adult( name == $c.parent ), $parentAge : sum($a.getAge()) )\n" + "then\n" + "  insert(new Result($parentAge));\n" + "end";
    KieSession ksession = getKieSession(str);
    Adult a = new Adult("Mario", 43);
    Child c = new Child("Sofia", 6, "Mario");
    ksession.insert(a);
    ksession.insert(c);
    ksession.fireAllRules();
    Collection<Result> results = getObjectsIntoList(ksession, Result.class);
    assertThat(results, hasItem(new Result(43)));
}
Also used : Adult(org.drools.modelcompiler.domain.Adult) KieSession(org.kie.api.runtime.KieSession) Child(org.drools.modelcompiler.domain.Child) Result(org.drools.modelcompiler.domain.Result) Test(org.junit.Test)

Example 7 with Result

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

the class AccumulateTest method testAccumulate2.

@Test
public void testAccumulate2() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule X when\n" + "  accumulate ( $p: Person ( getName().startsWith(\"M\")); \n" + "                $sum : sum($p.getAge()),  \n" + "                $average : average($p.getAge())  \n" + "              )                          \n" + "then\n" + "  insert(new Result($sum));\n" + "  insert(new Result($average));\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);
    assertThat(results, hasItem(new Result(38.5)));
    assertThat(results, hasItem(new Result(77)));
}
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 8 with Result

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

the class AccumulateTest method testAccumulateMultipleFunctionsConstrainingValues.

@Test
public void testAccumulateMultipleFunctionsConstrainingValues() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule X when\n" + "  accumulate ( Person ( $age : age > 36); \n" + "                $sum : sum($age),  \n" + "                $min : min($age)  \n" + "                ; $sum > 50, $min > 30\n" + "              )                          \n" + "then\n" + "  insert(new Result($sum));\n" + "  insert(new Result($min));\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);
    assertThat(results, hasItem(new Result(37)));
    assertThat(results, hasItem(new Result(77)));
}
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 9 with Result

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

the class AccumulateTest method testAccumulateMultipleFunctions.

@Test
public void testAccumulateMultipleFunctions() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule X when\n" + "  accumulate ( Person ( $age : age > 36); \n" + "                $sum : sum($age),  \n" + "                $average : average($age)  \n" + "              )                          \n" + "then\n" + "  insert(new Result($sum));\n" + "  insert(new Result($average));\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);
    assertThat(results, hasItem(new Result(38.5d)));
    assertThat(results, hasItem(new Result(77)));
}
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 10 with Result

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

the class CompilerTest method testEmptyPattern.

@Test
public void testEmptyPattern() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule R when\n" + "  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(1, results.size());
    assertEquals("ok", 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