Search in sources :

Example 16 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class RuleUnitCoordinationTest method testCoordination.

@Test
public void testCoordination() throws Exception {
    String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(MasterModelUnit.class) + ";\n" + "import " + MasterModel.class.getCanonicalName() + "\n" + "import " + ApplicableModel.class.getCanonicalName() + "\n" + "import " + ApplyMathModel.class.getCanonicalName() + "\n" + "import " + ApplyStringModel.class.getCanonicalName() + "\n" + "import " + ScheduledModelApplicationUnit.class.getCanonicalName() + "\n" + "\n" + "rule FindModelToApply \n" + "when\n" + "   $mm: MasterModel( subModels != null ) from models\n" + "   $am: ApplicableModel( applied == false, $idx: index ) from $mm.subModels\n" + // "   not ApplicableModel( applied == false, index < $idx ) from $mm.subModels\n" +
    "then\n" + "   applicableModels.insert($am);\n" + "   drools.run(new ScheduledModelApplicationUnit(models,applicableModels));\n" + "end\n" + "";
    String drl2 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(ScheduledModelApplicationUnit.class) + ";\n" + "import " + MasterModel.class.getCanonicalName() + "\n" + "import " + ApplicableModel.class.getCanonicalName() + "\n" + "import " + ApplyMathModel.class.getCanonicalName() + "\n" + "import " + ApplyStringModel.class.getCanonicalName() + "\n" + "\n" + "rule Apply_ApplyMathModel_Addition \n" + "when\n" + "    $amm: ApplyMathModel( applied == false, inputValue1 != null, " + "                          inputValue2 != null, operation == \"add\" ) from applicableModels\n" + "    $v1: Integer() from $amm.inputValue1 \n" + "    $v2: Integer() from $amm.inputValue2 \n" + "then\n" + "    modify($amm) { \n" + "       setResult($v1.intValue() + $v2.intValue()), \n" + "       setApplied(true) \n" + "    };\n" + "    System.out.println(\"Result = \"+$amm.getResult());\n" + "end\n" + "\n" + "rule Apply_ApplyStringModel_Concat \n" + "when\n" + "    $asm: ApplyStringModel( applied == false, inputString1 != null, " + "                            inputString2 != null, operation == \"concat\" ) from applicableModels \n" + "    $v1: String() from $asm.inputString1 \n" + "    $v2: String() from $asm.inputString2 \n" + "then\n" + "    String result = $v1+\" \"+$v2; \n" + "    modify($asm) {\n" + "       setResult(result),\n" + "       setApplied(true)\n" + "    };\n" + "    System.out.println(\"Result = \"+$asm.getResult());\n" + "end\n" + "";
    MasterModel master = new MasterModel("TestMaster");
    ApplyMathModel mathModel = new ApplyMathModel(1, "Math1", "add", 10, 10);
    ApplyStringModel stringModel = new ApplyStringModel(2, "String1", "concat", "hello", "world");
    master.addSubModel(mathModel);
    master.addSubModel(stringModel);
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).addContent(drl2, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    DataSource<MasterModel> masterModels = executor.newDataSource("models");
    DataSource<ApplicableModel> applicableModels = executor.newDataSource("applicableModel");
    FactHandle masterFH = masterModels.insert(master);
    RuleUnit unit = new MasterModelUnit(masterModels, applicableModels);
    // int x = 1;
    // while (x > 0) {
    // x = executor.run( unit );
    // System.out.println( x );
    // // I'm reinserting the master model to reinforce its evaluation, as I said in our call this is necessary because
    // // the second level froms are made on plain lists which are not normally re-evaluated by from nodes (regardless of rule units).
    // // In theory also the update should be more correct and enough to reinforce this re-evaluation, but for some reason
    // // in this case is not working. I suspect we have a bug in our NotNode related to this specific case, but I'm still
    // // evaluating it. For now the insert should be good enough to allow you to make some progress on this.
    // // masterModels.update( masterFH, master, "subModels" );
    // masterModels.insert( master );
    // }
    executor.run(unit);
    assertEquals(20, (int) mathModel.getResult());
    assertEquals("hello world", stringModel.getResult());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieHelper(org.kie.internal.utils.KieHelper) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) RuleUnit(org.kie.api.runtime.rule.RuleUnit) Test(org.junit.Test)

Example 17 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class RuleUnitTest method testStackedRuleUnitInvocationFromConsequence.

@Test
public void testStackedRuleUnitInvocationFromConsequence() throws Exception {
    String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "import " + NotAdultUnit.class.getCanonicalName() + "\n" + "rule NotAdult @Unit( NotAdultUnit.class ) when\n" + "    $p : Person(age < 18, $name : name) from persons\n" + "then\n" + "    System.out.println($name + \" is NOT adult\");\n" + "    modify($p) { setAge(18); }\n" + "    drools.run( AdultUnit.class );" + "end\n" + "rule Adult @Unit( AdultUnit.class ) when\n" + "    Person(age >= 18, $name : name) from persons\n" + "then\n" + "    System.out.println($name + \" is adult\");\n" + "end\n";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 2), new Person("Sofia", 6));
    List<String> log = new ArrayList<>();
    executor.bindVariable("log", log);
    assertEquals(4, executor.run(NotAdultUnit.class));
    List<String> expectedLogs = asList("org.drools.compiler.integrationtests.RuleUnitTest$NotAdultUnit started.", "org.drools.compiler.integrationtests.RuleUnitTest$NotAdultUnit yielded to org.drools.compiler.integrationtests.RuleUnitTest$AdultUnit", "org.drools.compiler.integrationtests.RuleUnitTest$AdultUnit started.", "org.drools.compiler.integrationtests.RuleUnitTest$AdultUnit ended.", "org.drools.compiler.integrationtests.RuleUnitTest$NotAdultUnit started.", "org.drools.compiler.integrationtests.RuleUnitTest$NotAdultUnit yielded to org.drools.compiler.integrationtests.RuleUnitTest$AdultUnit", "org.drools.compiler.integrationtests.RuleUnitTest$AdultUnit started.", "org.drools.compiler.integrationtests.RuleUnitTest$AdultUnit ended.", "org.drools.compiler.integrationtests.RuleUnitTest$NotAdultUnit started.", "org.drools.compiler.integrationtests.RuleUnitTest$NotAdultUnit ended.");
    assertEquals(expectedLogs, log);
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 18 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class RuleUnitTest method testRunOrder.

@Test
public void testRunOrder() throws Exception {
    // DROOLS-2199
    String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + FlowUnit.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "import " + NotAdultUnit.class.getCanonicalName() + "\n" + "global java.util.List list;\n" + "rule Flow @Unit( FlowUnit.class ) when\n" + "then\n" + "    drools.run( NotAdultUnit.class );\n" + "    drools.run( AdultUnit.class );\n" + "end\n" + "rule Adult @Unit( AdultUnit.class ) when\n" + "    Person(age >= 18, $name : name) from persons\n" + "then\n" + "    list.add($name + \" is adult\");\n" + "end\n" + "rule NotAdult @Unit( NotAdultUnit.class ) when\n" + "    Person(age < 18, $name : name) from persons\n" + "then\n" + "    list.add($name + \" is NOT adult\");\n" + "end";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    List<String> list = new ArrayList<>();
    executor.getKieSession().setGlobal("list", list);
    DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 42), new Person("Sofia", 4));
    assertEquals(3, executor.run(FlowUnit.class));
    assertEquals(list, asList("Sofia is NOT adult", "Mario is adult"));
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 19 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class RuleUnitTest method testModifyGlobalFact.

@Test
public void testModifyGlobalFact() throws Exception {
    String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnit.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult when\n" + "    $p : Person(age < 18, $name : name)\n" + "then\n" + "    System.out.println($name + \" is NOT adult\");\n" + "    modify($p) { setAge(18) }\n" + "end";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    executor.getKieSession().insert(new Person("Sofia", 4));
    assertEquals(1, executor.run(AdultUnit.class));
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 20 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class RuleUnitTest method testWithOOPathAndNot.

@Test
public void testWithOOPathAndNot() throws Exception {
    String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "rule Adult @Unit( AdultUnit.class ) when\n" + "    not /persons[age >= 18]\n" + "then\n" + "    System.out.println(\"No adults\");\n" + "end";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 4), new Person("Marilena", 17), new Person("Sofia", 4));
    RuleUnit adultUnit = new AdultUnit(persons);
    assertEquals(1, executor.run(adultUnit));
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) RuleUnit(org.kie.api.runtime.rule.RuleUnit) Person(org.drools.compiler.Person) Test(org.junit.Test)

Aggregations

KieHelper (org.kie.internal.utils.KieHelper)467 Test (org.junit.Test)427 KieSession (org.kie.api.runtime.KieSession)355 ArrayList (java.util.ArrayList)194 KieBase (org.kie.api.KieBase)152 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)77 Person (org.drools.compiler.Person)61 FactHandle (org.kie.api.runtime.rule.FactHandle)55 List (java.util.List)44 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)41 RuleUnitExecutor (org.kie.api.runtime.rule.RuleUnitExecutor)38 InternalRuleUnitExecutor (org.drools.core.impl.InternalRuleUnitExecutor)37 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)34 Man (org.drools.compiler.oopath.model.Man)29 Child (org.drools.compiler.oopath.model.Child)24 Woman (org.drools.compiler.oopath.model.Woman)23 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)23 InternalFactHandle (org.drools.core.common.InternalFactHandle)18 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)18 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)18