Search in sources :

Example 1 with Woman

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

the class OOPathFlowTest method testOOPath.

@Test
public void testOOPath() {
    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));
    Rule rule = rule("oopath").build(expr("exprA", childV, c -> c.getAge() > 10), 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 Woman alice = new Woman("Alice", 38);
    final Man bob = new Man("Bob", 40);
    final Man carl = new Man("Bob", 42);
    bob.setWife(alice);
    final Child charlie = new Child("Charles", 12);
    final Child debbie = new Child("Debbie", 10);
    alice.addChild(charlie);
    alice.addChild(debbie);
    ksession.insert(bob);
    ksession.insert(carl);
    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) 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) Test(org.junit.Test)

Example 2 with Woman

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

the class ExternalisedLambdaTest method testFromExpression.

@Test
public void testFromExpression() {
    final String str = "import org.drools.modelcompiler.domain.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Man( $wife : wife )\n" + " $child: Child( age > 10 ) from $wife.children\n" + "then\n" + "  list.add( $child.getName() );\n" + "end\n";
    KieSession ksession = null;
    try {
        ksession = getKieSession(str);
    } catch (NonExternalisedLambdaFoundException e) {
        fail(e.getMessage());
    }
    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);
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("Charles");
}
Also used : NonExternalisedLambdaFoundException(org.drools.modelcompiler.util.lambdareplace.NonExternalisedLambdaFoundException) 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) Test(org.junit.Test)

Example 3 with Woman

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

the class FromTest method testFromVariable.

@Test
public void testFromVariable() {
    final String str = "import org.drools.modelcompiler.domain.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Man( $children : wife.children )\n" + " $child: Child( age > 10 ) from $children\n" + "then\n" + "  list.add( $child.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);
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("Charles");
}
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) Test(org.junit.Test)

Example 4 with Woman

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

the class OOPathTest method testBackReferenceConstraint.

@Test
public void testBackReferenceConstraint() {
    final String str = "import org.drools.modelcompiler.domain.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Man( $toy: /wife/children/toys[ name.length == ../name.length ] )\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("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 : 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 5 with Woman

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

the class OOPathTest method testOOPath.

@Test
public void testOOPath() {
    final String str = "import org.drools.modelcompiler.domain.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " $man: Man( /wife/children[age > 10] )\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 Woman alice = new Woman("Alice", 38);
    final Man bob = new Man("Bob", 40);
    final Man carl = new Man("Carl", 40);
    bob.setWife(alice);
    final Child charlie = new Child("Charles", 12);
    final Child debbie = new Child("Debbie", 10);
    alice.addChild(charlie);
    alice.addChild(debbie);
    ksession.insert(bob);
    ksession.insert(carl);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("Bob");
}
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) Test(org.junit.Test)

Aggregations

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