Search in sources :

Example 1 with InternationalAddress

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

the class InlineCastTest method testInlineCastForAFieldAndMixMethodCall.

@Test
public void testInlineCastForAFieldAndMixMethodCall() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "import " + InternationalAddress.class.getCanonicalName() + ";" + "rule R when\n" + "  $p : Person( address#InternationalAddress.getState().length == 5 )\n" + "then\n" + "  insert(\"matched\");\n" + "end";
    KieSession ksession = getKieSession(str);
    Person john = new Person("John", 47);
    InternationalAddress a = new InternationalAddress("address", "Italy");
    john.setAddress(a);
    ksession.insert(john);
    ksession.fireAllRules();
    Collection<String> results = getObjectsIntoList(ksession, String.class);
    assertEquals(1, results.size());
}
Also used : InternationalAddress(org.drools.modelcompiler.domain.InternationalAddress) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) BaseModelTest(org.drools.modelcompiler.BaseModelTest) Test(org.junit.Test)

Example 2 with InternationalAddress

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

the class InlineCastTest method testInlineCastForAField.

@Test
public void testInlineCastForAField() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "import " + InternationalAddress.class.getCanonicalName() + ";" + "rule R when\n" + "  $p : Person( address#InternationalAddress.state.length == 5 )\n" + "then\n" + "  insert(\"matched\");\n" + "end";
    KieSession ksession = getKieSession(str);
    Person john = new Person("John", 47);
    InternationalAddress a = new InternationalAddress("address", "Italy");
    john.setAddress(a);
    ksession.insert(john);
    ksession.fireAllRules();
    Collection<String> results = getObjectsIntoList(ksession, String.class);
    assertEquals(1, results.size());
}
Also used : InternationalAddress(org.drools.modelcompiler.domain.InternationalAddress) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) BaseModelTest(org.drools.modelcompiler.BaseModelTest) Test(org.junit.Test)

Example 3 with InternationalAddress

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

the class OOPathFlowTest method testOOPathCast.

@Test
public void testOOPathCast() {
    Global<List> listG = globalOf(List.class, "defaultpkg", "list");
    Variable<Man> manV = declarationOf(Man.class, "$man");
    // Drools doc: In this way subsequent constraints can also safely access to properties declared only in that subclass
    Variable<InternationalAddress> addressV = declarationOf(InternationalAddress.class, "$italy", reactiveFrom(manV, Man::getAddress));
    Rule rule = rule("oopath").build(expr("exprA", addressV, c -> c.getState().equals("Italy")), on(manV, listG).execute((t, l) -> l.add(t.getName())));
    Model model = new ModelImpl().addGlobal(listG).addRule(rule);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    KieSession ksession = kieBase.newKieSession();
    final List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    final Man bob = new Man("Bob", 40);
    bob.setAddress(new InternationalAddress("Via Verdi", "Italy"));
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("Bob");
}
Also used : FlowDSL.globalOf(org.drools.model.FlowDSL.globalOf) Man(org.drools.modelcompiler.domain.Man) Global(org.drools.model.Global) Toy(org.drools.modelcompiler.domain.Toy) FlowDSL.and(org.drools.model.FlowDSL.and) FlowDSL.accFunction(org.drools.model.FlowDSL.accFunction) FlowDSL.accumulate(org.drools.model.FlowDSL.accumulate) ArrayList(java.util.ArrayList) QueryDef(org.drools.model.QueryDef) Child(org.drools.modelcompiler.domain.Child) FlowDSL.from(org.drools.model.FlowDSL.from) QueryResults(org.kie.api.runtime.rule.QueryResults) FlowDSL.reactiveFrom(org.drools.model.FlowDSL.reactiveFrom) Assertions(org.assertj.core.api.Assertions) Woman(org.drools.modelcompiler.domain.Woman) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Model(org.drools.model.Model) FlowDSL.declarationOf(org.drools.model.FlowDSL.declarationOf) FlowDSL.rule(org.drools.model.FlowDSL.rule) FlowDSL.expr(org.drools.model.FlowDSL.expr) Variable(org.drools.model.Variable) FlowDSL.query(org.drools.model.FlowDSL.query) FlowDSL.on(org.drools.model.FlowDSL.on) Test(org.junit.Test) FlowDSL.input(org.drools.model.FlowDSL.input) InternationalAddress(org.drools.modelcompiler.domain.InternationalAddress) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) List(java.util.List) Rule(org.drools.model.Rule) Assert.assertEquals(org.junit.Assert.assertEquals) ModelImpl(org.drools.model.impl.ModelImpl) ArrayList(java.util.ArrayList) InternationalAddress(org.drools.modelcompiler.domain.InternationalAddress) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) ArrayList(java.util.ArrayList) List(java.util.List) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.modelcompiler.domain.Man) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) Test(org.junit.Test)

Example 4 with InternationalAddress

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

the class OOPathTest method testSimpleOOPathCast3.

@Test
public void testSimpleOOPathCast3() {
    final String str = "import org.drools.modelcompiler.domain.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Man( $italy: /address#InternationalAddress[ state == \"Italy\" ], $name : name != null )\n" + "then\n" + "  list.add( $name );\n" + "end\n";
    KieSession ksession = getKieSession(str);
    final List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    final Man bob = new Man("Bob", 40);
    bob.setAddress(new InternationalAddress("Via Verdi", "Italy"));
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("Bob");
}
Also used : InternationalAddress(org.drools.modelcompiler.domain.InternationalAddress) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.modelcompiler.domain.Man) Test(org.junit.Test)

Example 5 with InternationalAddress

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

the class OOPathTest method testSimpleOOPathCast1.

@Test
public void testSimpleOOPathCast1() {
    final String str = "import org.drools.modelcompiler.domain.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  $man : Man( $italy: /address#InternationalAddress[ state == \"Italy\" ] )\n" + "then\n" + "  list.add( $man.getName() );\n" + "end\n";
    KieSession ksession = getKieSession(str);
    final List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    final Man bob = new Man("Bob", 40);
    bob.setAddress(new InternationalAddress("Via Verdi", "Italy"));
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("Bob");
}
Also used : InternationalAddress(org.drools.modelcompiler.domain.InternationalAddress) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.modelcompiler.domain.Man) Test(org.junit.Test)

Aggregations

InternationalAddress (org.drools.modelcompiler.domain.InternationalAddress)6 Test (org.junit.Test)6 KieSession (org.kie.api.runtime.KieSession)6 ArrayList (java.util.ArrayList)4 Man (org.drools.modelcompiler.domain.Man)4 BaseModelTest (org.drools.modelcompiler.BaseModelTest)2 Person (org.drools.modelcompiler.domain.Person)2 List (java.util.List)1 Assertions (org.assertj.core.api.Assertions)1 FlowDSL.accFunction (org.drools.model.FlowDSL.accFunction)1 FlowDSL.accumulate (org.drools.model.FlowDSL.accumulate)1 FlowDSL.and (org.drools.model.FlowDSL.and)1 FlowDSL.declarationOf (org.drools.model.FlowDSL.declarationOf)1 FlowDSL.expr (org.drools.model.FlowDSL.expr)1 FlowDSL.from (org.drools.model.FlowDSL.from)1 FlowDSL.globalOf (org.drools.model.FlowDSL.globalOf)1 FlowDSL.input (org.drools.model.FlowDSL.input)1 FlowDSL.on (org.drools.model.FlowDSL.on)1 FlowDSL.query (org.drools.model.FlowDSL.query)1 FlowDSL.reactiveFrom (org.drools.model.FlowDSL.reactiveFrom)1