Search in sources :

Example 1 with Result

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

the class AccumulateTest method testAccumulate1.

@Test
public void testAccumulate1() {
    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" + "              )                          \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 2 with Result

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

the class AccumulateTest method testAccumulateWithAnd3.

@Test
public void testAccumulateWithAnd3() {
    String str = "import " + Adult.class.getCanonicalName() + ";\n" + "import " + Child.class.getCanonicalName() + ";\n" + "import " + Result.class.getCanonicalName() + ";\n" + "rule R when\n" + "  accumulate( $x : Child( age < 10 ) and $y : Adult( name == $x.parent ), $parentAge : sum($x.getAge() + $y.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);
    // The original DSL test returns a double while the exec model returns an integer
    assertEquals(((Number) results.iterator().next().getValue()).intValue(), 49);
}
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 3 with Result

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

the class AccumulateTest method testAccumulateConstrainingValueInPattern.

@Test
public void testAccumulateConstrainingValueInPattern() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule X when\n" + "  $sum : Integer( this > 50 ) from accumulate ( $p: Person ( getName().startsWith(\"M\")); \n" + "                sum($p.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 4 with Result

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

the class AccumulateTest method testAccumulateConstrainingValue.

@Test
public void testAccumulateConstrainingValue() {
    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()); $sum > 50  \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 5 with Result

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

the class AccumulateTest method testAccumulateConstrainingValue2.

@Test
public void testAccumulateConstrainingValue2() {
    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()); $sum > 100  \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(0, results.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)

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