Search in sources :

Example 1 with Child

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

the class AccumulateTest method testAccumulateWithAnd3.

@Test
public void testAccumulateWithAnd3() {
    String str = "import " + Adult.class.getCanonicalName() + ";\n" + "import " + Child.class.getCanonicalName() + ";\n" + "import " + Result.class.getCanonicalName() + ";\n" + "rule R when\n" + "  accumulate( $x : Child( age < 10 ) and $y : Adult( name == $x.parent ), $parentAge : sum($x.getAge() + $y.getAge()) )\n" + "then\n" + "  insert(new Result($parentAge));\n" + "end";
    KieSession ksession = getKieSession(str);
    Adult a = new Adult("Mario", 43);
    Child c = new Child("Sofia", 6, "Mario");
    ksession.insert(a);
    ksession.insert(c);
    ksession.fireAllRules();
    Collection<Result> results = getObjectsIntoList(ksession, Result.class);
    // The original DSL test returns a double while the exec model returns an integer
    assertEquals(((Number) results.iterator().next().getValue()).intValue(), 49);
}
Also used : Adult(org.drools.modelcompiler.domain.Adult) KieSession(org.kie.api.runtime.KieSession) Child(org.drools.modelcompiler.domain.Child) Result(org.drools.modelcompiler.domain.Result) Test(org.junit.Test)

Example 2 with Child

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

the class AccumulateTest method testAccumulateWithAnd.

@Test
public void testAccumulateWithAnd() {
    String str = "import " + Adult.class.getCanonicalName() + ";\n" + "import " + Child.class.getCanonicalName() + ";\n" + "import " + Result.class.getCanonicalName() + ";\n" + "rule R when\n" + "  accumulate( $c : Child( age < 10 ) and $a : Adult( name == $c.parent ), $parentAge : sum($a.getAge()) )\n" + "then\n" + "  insert(new Result($parentAge));\n" + "end";
    KieSession ksession = getKieSession(str);
    Adult a = new Adult("Mario", 43);
    Child c = new Child("Sofia", 6, "Mario");
    ksession.insert(a);
    ksession.insert(c);
    ksession.fireAllRules();
    Collection<Result> results = getObjectsIntoList(ksession, Result.class);
    assertThat(results, hasItem(new Result(43)));
}
Also used : Adult(org.drools.modelcompiler.domain.Adult) KieSession(org.kie.api.runtime.KieSession) Child(org.drools.modelcompiler.domain.Child) Result(org.drools.modelcompiler.domain.Result) Test(org.junit.Test)

Example 3 with Child

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

the class CompilerTest method testFrom.

@Test
public void testFrom() {
    String str = "import " + Result.class.getCanonicalName() + ";\n" + "import " + Adult.class.getCanonicalName() + ";\n" + "import " + Child.class.getCanonicalName() + ";\n" + "rule R when\n" + "  $r : Result()\n" + "  $a : Adult()\n" + "  $c : Child( age > 8 ) from $a.children\n" + "then\n" + "  $r.setValue($c.getName());\n" + "end";
    KieSession ksession = getKieSession(str);
    Result result = new Result();
    ksession.insert(result);
    Adult dad = new Adult("dad", 40);
    dad.addChild(new Child("Alan", 10));
    dad.addChild(new Child("Betty", 7));
    ksession.insert(dad);
    ksession.fireAllRules();
    assertEquals("Alan", result.getValue());
}
Also used : Adult(org.drools.modelcompiler.domain.Adult) KieSession(org.kie.api.runtime.KieSession) Child(org.drools.modelcompiler.domain.Child) Result(org.drools.modelcompiler.domain.Result) Test(org.junit.Test)

Example 4 with Child

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

the class FlowTest method testAccumuluateWithAnd2.

@Test
public void testAccumuluateWithAnd2() {
    Variable<Object> var_$pattern_Object$1$ = declarationOf(Object.class, "$pattern_Object$1$");
    Variable<Child> var_$c = declarationOf(Child.class, "$c");
    Variable<Adult> var_$a = declarationOf(Adult.class, "$a");
    Variable<Integer> var_$parentAge = declarationOf(Integer.class, "$parentAge");
    Variable<Integer> var_$expr$5$ = declarationOf(Integer.class, "$expr$5$");
    org.drools.model.Rule rule = rule("R").build(bind(var_$expr$5$).as(var_$a, var_$c, ($a, $c) -> $a.getAge() + $c.getAge()), accumulate(and(expr("$expr$1$", var_$c, (_this) -> _this.getAge() < 10).indexedBy(int.class, org.drools.model.Index.ConstraintType.LESS_THAN, 0, _this -> _this.getAge(), 10).reactOn("age"), expr("$expr$2$", var_$a, var_$c, (_this, $c) -> _this.getName().equals($c.getParent())).reactOn("name")), accFunction(org.drools.core.base.accumulators.IntegerSumAccumulateFunction.class, var_$expr$5$).as(var_$parentAge)), on(var_$parentAge).execute((drools, $parentAge) -> {
        drools.insert(new Result($parentAge));
    }));
    Model model = new ModelImpl().addRule(rule);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    KieSession ksession = kieBase.newKieSession();
    Adult a = new Adult("Mario", 43);
    Child c = new Child("Sofia", 6, "Mario");
    ksession.insert(a);
    ksession.insert(c);
    ksession.fireAllRules();
    Collection<Result> results = getObjectsIntoList(ksession, Result.class);
    assertThat(results, hasItem(new Result(49)));
}
Also used : Arrays(java.util.Arrays) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) FlowDSL.eval(org.drools.model.FlowDSL.eval) ObjectOutput(java.io.ObjectOutput) Man(org.drools.modelcompiler.domain.Man) Global(org.drools.model.Global) FlowDSL.not(org.drools.model.FlowDSL.not) Toy(org.drools.modelcompiler.domain.Toy) FlowDSL.and(org.drools.model.FlowDSL.and) FlowDSL.accFunction(org.drools.model.FlowDSL.accFunction) Relationship(org.drools.modelcompiler.domain.Relationship) BaseModelTest.getObjectsIntoList(org.drools.modelcompiler.BaseModelTest.getObjectsIntoList) Assert.assertThat(org.junit.Assert.assertThat) Query2Def(org.drools.model.Query2Def) Child(org.drools.modelcompiler.domain.Child) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) FlowDSL.from(org.drools.model.FlowDSL.from) QueryResults(org.kie.api.runtime.rule.QueryResults) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) FlowDSL.reactiveFrom(org.drools.model.FlowDSL.reactiveFrom) Assertions(org.assertj.core.api.Assertions) KieSession(org.kie.api.runtime.KieSession) FlowDSL.declarationOf(org.drools.model.FlowDSL.declarationOf) TargetPolicy(org.drools.modelcompiler.domain.TargetPolicy) FlowDSL.or(org.drools.model.FlowDSL.or) EventProcessingOption(org.kie.api.conf.EventProcessingOption) Collection(java.util.Collection) FlowDSL.on(org.drools.model.FlowDSL.on) Customer(org.drools.modelcompiler.domain.Customer) Serializable(java.io.Serializable) ConstraintType(org.drools.model.Index.ConstraintType) List(java.util.List) Query(org.drools.model.Query) FlowDSL.executeScript(org.drools.model.FlowDSL.executeScript) InternationalAddress(org.drools.modelcompiler.oopathdtables.InternationalAddress) Assert.assertFalse(org.junit.Assert.assertFalse) KnowledgeBaseFactory(org.drools.core.impl.KnowledgeBaseFactory) FlowDSL.bind(org.drools.model.FlowDSL.bind) FlowDSL.valueOf(org.drools.model.FlowDSL.valueOf) Person(org.drools.modelcompiler.domain.Person) ObjectInput(java.io.ObjectInput) InOperator(org.drools.model.operators.InOperator) FlowDSL.when(org.drools.model.FlowDSL.when) ModelImpl(org.drools.model.impl.ModelImpl) FlowDSL.globalOf(org.drools.model.FlowDSL.globalOf) StockTick(org.drools.modelcompiler.domain.StockTick) Employee.createEmployee(org.drools.modelcompiler.domain.Employee.createEmployee) ClockType(org.drools.core.ClockType) FlowDSL.accumulate(org.drools.model.FlowDSL.accumulate) ArrayList(java.util.ArrayList) Result(org.drools.modelcompiler.domain.Result) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Adult(org.drools.modelcompiler.domain.Adult) Employee(org.drools.modelcompiler.domain.Employee) FlowDSL.execute(org.drools.model.FlowDSL.execute) Woman(org.drools.modelcompiler.domain.Woman) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) 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) Address(org.drools.modelcompiler.domain.Address) FlowDSL.window(org.drools.model.FlowDSL.window) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) FlowDSL.input(org.drools.model.FlowDSL.input) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) FactHandle(org.kie.api.runtime.rule.FactHandle) TimeUnit(java.util.concurrent.TimeUnit) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) ClockTypeOption(org.kie.api.runtime.conf.ClockTypeOption) Assert.assertNull(org.junit.Assert.assertNull) FlowDSL.forall(org.drools.model.FlowDSL.forall) Rule(org.drools.model.Rule) Query1Def(org.drools.model.Query1Def) Assert.assertEquals(org.junit.Assert.assertEquals) Result(org.drools.modelcompiler.domain.Result) KieBase(org.kie.api.KieBase) Rule(org.drools.model.Rule) Model(org.drools.model.Model) Adult(org.drools.modelcompiler.domain.Adult) KieSession(org.kie.api.runtime.KieSession) ModelImpl(org.drools.model.impl.ModelImpl) Child(org.drools.modelcompiler.domain.Child) Test(org.junit.Test)

Example 5 with Child

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

Aggregations

Child (org.drools.modelcompiler.domain.Child)21 KieSession (org.kie.api.runtime.KieSession)21 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)14 Man (org.drools.modelcompiler.domain.Man)14 Woman (org.drools.modelcompiler.domain.Woman)14 Result (org.drools.modelcompiler.domain.Result)11 Toy (org.drools.modelcompiler.domain.Toy)11 Adult (org.drools.modelcompiler.domain.Adult)10 List (java.util.List)8 Assertions (org.assertj.core.api.Assertions)8 Global (org.drools.model.Global)8 Model (org.drools.model.Model)8 Rule (org.drools.model.Rule)8 Variable (org.drools.model.Variable)8 ModelImpl (org.drools.model.impl.ModelImpl)8 KieBaseBuilder (org.drools.modelcompiler.builder.KieBaseBuilder)8 Assert.assertEquals (org.junit.Assert.assertEquals)8 KieBase (org.kie.api.KieBase)8 QueryResults (org.kie.api.runtime.rule.QueryResults)8