Search in sources :

Example 56 with Result

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

the class QueryTest method testQueryInvokedWithGlobal.

@Test
public void testQueryInvokedWithGlobal() {
    String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "global Integer ageG;" + "query olderThan( Person $p, int $age )\n" + "    $p := Person(age > $age)\n" + "end\n" + "rule R when\n" + "    $p : Person( name.startsWith(\"M\") )\n" + "    olderThan( $p, ageG; )\n" + "then\n" + "    insert(new Result($p.getName()));\n" + "end";
    KieSession ksession = getKieSession(str);
    ksession.setGlobal("ageG", 40);
    ksession.insert(new Person("Mark", 39));
    ksession.insert(new Person("Mario", 41));
    ksession.insert(new Person("Edson", 41));
    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 57 with Result

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

the class QueryTest method testQueryInRule.

@Test
public void testQueryInRule() {
    String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "query olderThan( Person $p, int $age )\n" + "    $p := Person(age > $age)\n" + "end\n" + "rule R when\n" + "    olderThan( $p, 40; )\n" + "then\n" + "    insert(new Result($p.getName()));\n" + "end";
    KieSession ksession = getKieSession(str);
    ksession.insert(new Person("Mark", 39));
    ksession.insert(new Person("Mario", 41));
    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 58 with Result

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

the class InlineCastTest method testInlineCastThis.

@Test
public void testInlineCastThis() {
    String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + "  $r : Result()\n" + "  $o : Object( this#Person.name == \"Mark\" )\n" + "then\n" + "  $r.setValue(\"Found: \" + $o);\n" + "end";
    KieSession ksession = getKieSession(str);
    Result result = new Result();
    ksession.insert(result);
    ksession.insert("Mark");
    ksession.insert(new Person("Mark", 37));
    ksession.insert(new Person("Mario", 40));
    ksession.fireAllRules();
    assertEquals("Found: Mark", result.getValue());
}
Also used : KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Result(org.drools.modelcompiler.domain.Result) BaseModelTest(org.drools.modelcompiler.BaseModelTest) Test(org.junit.Test)

Example 59 with Result

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

the class CompilerTest method testAgeWithSum.

@Test
public void testAgeWithSum() {
    String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + "  $p : Person( personAge : age )\n" + "  $plusTwo : Person(age == personAge + 2 )\n" + "then\n" + "  insert(new Result($plusTwo.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("Mark", 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 60 with Result

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

the class CompilerTest method checkConcatenatedFrom.

private void checkConcatenatedFrom(boolean withCondition) {
    String str = "import " + Result.class.getCanonicalName() + ";\n" + "import " + Man.class.getCanonicalName() + ";\n" + "import " + Woman.class.getCanonicalName() + ";\n" + "import " + Child.class.getCanonicalName() + ";\n" + "import " + Toy.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R when\n" + "  $m : Man(" + (withCondition ? "age > 0" : "") + ")\n" + "  $w : Woman() from $m.wife\n" + "  $c : Child( age > 10 ) from $w.children\n" + "  $t : Toy() from $c.toys\n" + "then\n" + "  list.add($t.getName());\n" + "end";
    KieSession ksession = getKieSession(str);
    Result result = new Result();
    ksession.insert(result);
    final List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    final Woman alice = new Woman("Alice", 38);
    final Man bob = new Man("Bob", 40);
    bob.setWife(alice);
    final Child charlie = new Child("Charles", 12);
    final Child debbie = new Child("Debbie", 10);
    alice.addChild(charlie);
    alice.addChild(debbie);
    charlie.addToy(new Toy("car"));
    charlie.addToy(new Toy("ball"));
    debbie.addToy(new Toy("doll"));
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("car", "ball");
}
Also used : ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.modelcompiler.domain.Man) Woman(org.drools.modelcompiler.domain.Woman) Child(org.drools.modelcompiler.domain.Child) Result(org.drools.modelcompiler.domain.Result) Toy(org.drools.modelcompiler.domain.Toy)

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