Search in sources :

Example 11 with Result

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

the class CompilerTest method testSimpleInsertDeleteExplicitScope.

@Test
public void testSimpleInsertDeleteExplicitScope() {
    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());" + "  drools.insert(r);\n" + "  drools.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 12 with Result

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

the class CompilerTest method testEmptyPatternWithBinding.

@Test
public void testEmptyPatternWithBinding() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule R when\n" + "  $p : Person() \n" + "then\n" + "  insert(new Result($p.getName()));\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("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 13 with Result

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

the class CompilerTest method testSimpleInsertWithProperties.

@Test
public void testSimpleInsertWithProperties() {
    String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + "  $p : Person( address.city.startsWith(\"M\"))\n" + "then\n" + "  Result r = new Result($p.getName());" + "  insert(r);\n" + "end";
    KieSession ksession = getKieSession(str);
    ksession.insert(new Person("Mark", 37, new Address("London")));
    ksession.insert(new Person("Luca", 32, new Address("Milan")));
    ksession.fireAllRules();
    Collection<Result> results = getObjectsIntoList(ksession, Result.class);
    assertEquals(1, results.size());
    assertEquals("Luca", results.iterator().next().getValue());
}
Also used : Address(org.drools.modelcompiler.domain.Address) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Result(org.drools.modelcompiler.domain.Result) Test(org.junit.Test)

Example 14 with Result

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

the class CompilerTest method testOrWithFixedLeftOperand.

@Test
public void testOrWithFixedLeftOperand() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule R when\n" + "  $p : Person(name == \"Mario\" || == \"Luca\" || == \"Edoardo\")\n" + "then\n" + "  insert(new Result($p));\n" + "end";
    KieSession ksession = getKieSession(str);
    final Person mario = new Person("Mario", 40);
    final Person luca = new Person("Luca", 33);
    final Person edoardo = new Person("Edoardo", 31);
    final Person matteo = new Person("Matteo", 36);
    ksession.insert(mario);
    ksession.insert(luca);
    ksession.insert(edoardo);
    ksession.insert(matteo);
    ksession.fireAllRules();
    Collection<Result> results = getObjectsIntoList(ksession, Result.class);
    assertEquals(3, results.size());
    Assertions.assertThat(results.stream().map(r -> r.getValue())).containsExactlyInAnyOrder(mario, luca, edoardo);
}
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 15 with Result

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

the class CompilerTest method testNullSafeDereferncing2.

@Test
@Ignore("the codegen does not support it yet")
public void testNullSafeDereferncing2() {
    String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + "  $p : Person( address!.city.startsWith(\"M\") )\n" + "then\n" + "  Result r = new Result($p.getName());" + "  insert(r);\n" + "end";
    KieSession ksession = getKieSession(str);
    ksession.insert(new Person("John1", 41, null));
    ksession.insert(new Person("John2", 42, new Address("Milan")));
    ksession.fireAllRules();
    List<Result> results = getObjectsIntoList(ksession, Result.class);
    assertEquals(1, results.size());
    assertEquals("John2", results.get(0).getValue());
}
Also used : Address(org.drools.modelcompiler.domain.Address) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Result(org.drools.modelcompiler.domain.Result) Ignore(org.junit.Ignore) 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