Search in sources :

Example 11 with Man

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

the class OOPathFlowTest method testReactiveOOPath.

@Test
public void testReactiveOOPath() {
    Global<List> listG = globalOf(List.class, "defaultpkg", "list");
    Variable<Man> manV = declarationOf(Man.class);
    Variable<Woman> wifeV = declarationOf(Woman.class, reactiveFrom(manV, Man::getWife));
    Variable<Child> childV = declarationOf(Child.class, reactiveFrom(wifeV, Woman::getChildren));
    Variable<Toy> toyV = declarationOf(Toy.class, reactiveFrom(childV, Child::getToys));
    Rule rule = rule("oopath").build(expr("exprA", childV, c -> c.getAge() > 10), on(toyV, 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 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");
    list.clear();
    debbie.setAge(11);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("doll");
}
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) 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) Woman(org.drools.modelcompiler.domain.Woman) ModelImpl(org.drools.model.impl.ModelImpl) Child(org.drools.modelcompiler.domain.Child) Toy(org.drools.modelcompiler.domain.Toy) Test(org.junit.Test)

Example 12 with Man

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

the class OOPathFlowTest method testBackReferenceConstraint.

@Test
public void testBackReferenceConstraint() {
    Global<List> listG = globalOf(List.class, "defaultpkg", "list");
    Variable<Man> manV = declarationOf(Man.class);
    Variable<Woman> wifeV = declarationOf(Woman.class, reactiveFrom(manV, Man::getWife));
    Variable<Child> childV = declarationOf(Child.class, reactiveFrom(wifeV, Woman::getChildren));
    Variable<Toy> toyV = declarationOf(Toy.class, reactiveFrom(childV, Child::getToys));
    Rule rule = rule("oopath").build(expr("exprA", toyV, childV, (t, c) -> t.getName().length() == c.getName().length()), on(toyV, 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 Woman alice = new Woman("Alice", 38);
    final Man bob = new Man("Bob", 40);
    bob.setWife(alice);
    final Child charlie = new Child("Carl", 12);
    final Child debbie = new Child("Debbie", 8);
    alice.addChild(charlie);
    alice.addChild(debbie);
    charlie.addToy(new Toy("car"));
    charlie.addToy(new Toy("ball"));
    debbie.addToy(new Toy("doll"));
    debbie.addToy(new Toy("guitar"));
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("ball", "guitar");
}
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) 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) Woman(org.drools.modelcompiler.domain.Woman) ModelImpl(org.drools.model.impl.ModelImpl) Child(org.drools.modelcompiler.domain.Child) Toy(org.drools.modelcompiler.domain.Toy) Test(org.junit.Test)

Example 13 with Man

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

the class OOPathTest method testReactiveOOPath.

@Test
public void testReactiveOOPath() {
    final String str = "import org.drools.modelcompiler.domain.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Man( $toy: /wife/children[age > 10]/toys )\n" + "then\n" + "  list.add( $toy.getName() );\n" + "end\n";
    KieSession ksession = getKieSession(str);
    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");
    list.clear();
    debbie.setAge(11);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("doll");
}
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) Toy(org.drools.modelcompiler.domain.Toy) Test(org.junit.Test)

Example 14 with Man

use of org.drools.modelcompiler.domain.Man 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)

Example 15 with Man

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

the class OOPathTest method testSimpleOOPathCast2.

@Test
public void testSimpleOOPathCast2() {
    final String str = "import org.drools.modelcompiler.domain.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Man( $name : name, $italy: /address#InternationalAddress[ state == \"Italy\" ] )\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)

Aggregations

ArrayList (java.util.ArrayList)15 Man (org.drools.modelcompiler.domain.Man)15 KieSession (org.kie.api.runtime.KieSession)15 Test (org.junit.Test)14 Child (org.drools.modelcompiler.domain.Child)12 Woman (org.drools.modelcompiler.domain.Woman)12 Toy (org.drools.modelcompiler.domain.Toy)9 InternationalAddress (org.drools.modelcompiler.domain.InternationalAddress)7 List (java.util.List)6 Assertions (org.assertj.core.api.Assertions)6 Global (org.drools.model.Global)6 Model (org.drools.model.Model)6 Rule (org.drools.model.Rule)6 Variable (org.drools.model.Variable)6 ModelImpl (org.drools.model.impl.ModelImpl)6 KieBaseBuilder (org.drools.modelcompiler.builder.KieBaseBuilder)6 Assert.assertEquals (org.junit.Assert.assertEquals)6 KieBase (org.kie.api.KieBase)6 QueryResults (org.kie.api.runtime.rule.QueryResults)6 FlowDSL.accFunction (org.drools.model.FlowDSL.accFunction)5