Search in sources :

Example 31 with Result

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

the class EvalTest method testEvalWith2BindingsInvokingMethod.

@Test
public void testEvalWith2BindingsInvokingMethod() {
    String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + "  $p1 : Person( age == 40 )\n" + "  $p2 : Person( age == 38 )\n" + "  eval( $p1.getName().equals( $p2.getName() ) )\n" + "then\n" + "  insert(new Result($p1.getName()));\n" + "end";
    KieSession ksession = getKieSession(str);
    ksession.insert(new Person("Mario", 40));
    ksession.insert(new Person("Mario", 38));
    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)

Example 32 with Result

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

the class EvalTest method testEvalWithDeclaration.

@Test
public void testEvalWithDeclaration() {
    String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + "  $p1 : Person( $a1 : age )\n" + "  eval( $a1 > 39 )\n" + "then\n" + "  insert(new Result($p1.getName()));\n" + "end";
    KieSession ksession = getKieSession(str);
    ksession.insert(new Person("Mario", 40));
    ksession.insert(new Person("Mark", 38));
    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)

Example 33 with Result

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

the class EvalTest method testEval.

@Test
public void testEval() {
    String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + "  $p : Person()\n" + "  eval( $p.getAge() == 40 )\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)

Example 34 with Result

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

the class EvalTest method testEvalWith2Bindings.

@Test
public void testEvalWith2Bindings() {
    String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + "  $p1 : Person( name == \"Mario\" )\n" + "  $p2 : Person( name == \"Mark\" )\n" + "  eval( $p1.getAge() == $p2.getAge() + 2 )\n" + "then\n" + "  insert(new Result($p1.getName()));\n" + "end";
    KieSession ksession = getKieSession(str);
    ksession.insert(new Person("Mario", 40));
    ksession.insert(new Person("Mark", 38));
    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)

Example 35 with Result

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

the class EvalTest method testEvalWith2Declarations.

@Test
public void testEvalWith2Declarations() {
    String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + "  $p1 : Person( name == \"Mario\", $a1 : age > 0 )\n" + "  $p2 : Person( name == \"Mark\", $a2 : age > 0 )\n" + "  eval( $a1 == $a2 + 2 )\n" + "then\n" + "  insert(new Result($p1.getName()));\n" + "end";
    KieSession ksession = getKieSession(str);
    ksession.insert(new Person("Mario", 40));
    ksession.insert(new Person("Mark", 38));
    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