Search in sources :

Example 16 with RuleUnitExecutor

use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.

the class RuleUnitTest method testRuleUnitInvocationFromConsequence.

@Test
public void testRuleUnitInvocationFromConsequence() throws Exception {
    String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "import " + NotAdultUnit.class.getCanonicalName() + "\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" + "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";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 42), new Person("Marilena", 44), new Person("Sofia", 4));
    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 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 17 with RuleUnitExecutor

use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.

the class RuleUnitTest method testBindDataSource.

@Test
public void testBindDataSource() throws Exception {
    String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "import " + NotAdultUnit.class.getCanonicalName() + "\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" + "rule NotAdult @Unit( NotAdultUnit.class ) when\n" + "    Person(age < 18, $name : name) from persons\n" + "then\n" + "    System.out.println($name + \" is NOT adult\");\n" + "end";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    DataSource<Person> persons = DataSource.create(new Person("Mario", 42), new Person("Marilena", 44), new Person("Sofia", 4));
    executor.bindVariable("persons", persons);
    // explicitly create unit
    assertEquals(2, executor.run(AdultUnit.class));
    // let RuleUnitExecutor internally create and wire the unit instance
    assertEquals(1, executor.run(NotAdultUnit.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 18 with RuleUnitExecutor

use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.

the class RuleUnitTest method testReactiveOnUnitCreatingDataSource.

@Test(timeout = 10000L)
public void testReactiveOnUnitCreatingDataSource() throws Exception {
    // DROOLS-1647
    String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnitCreatingDataSource.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult when\n" + "    Person(age >= 18, $name : name) from persons\n" + "then\n" + "    System.out.println($name + \" is adult\");" + "    list.add($name);\n" + "end";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    DebugList<String> list = new DebugList<>();
    executor.bindVariable("list", list);
    AdultUnitCreatingDataSource adultUnit = new AdultUnitCreatingDataSource(list);
    adultUnit.insertPerson(new Person("Mario", 42));
    Semaphore ready = new Semaphore(0, true);
    list.onItemAdded = (l -> ready.release());
    new Thread(() -> executor.runUntilHalt(adultUnit)).start();
    ready.acquire();
    assertEquals(1, list.size());
    assertEquals("Mario", list.get(0));
    list.clear();
    list.onItemAdded = (l -> ready.release());
    adultUnit.insertPerson(new Person("Sofia", 4));
    adultUnit.insertPerson(new Person("Marilena", 44));
    ready.acquire();
    assertEquals(1, list.size());
    assertEquals("Marilena", list.get(0));
    executor.halt();
}
Also used : DataSource(org.kie.api.runtime.rule.DataSource) UnitVar(org.kie.api.definition.rule.UnitVar) ResourceType(org.kie.api.io.ResourceType) ArrayList(java.util.ArrayList) Person(org.drools.compiler.Person) Arrays.asList(java.util.Arrays.asList) LongAddress(org.drools.compiler.LongAddress) KieServices(org.kie.api.KieServices) RuleUnitFactory(org.drools.core.ruleunit.RuleUnitFactory) Assert.fail(org.junit.Assert.fail) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieFileSystem(org.kie.api.builder.KieFileSystem) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) Semaphore(java.util.concurrent.Semaphore) Results(org.kie.api.builder.Results) KieContainer(org.kie.api.runtime.KieContainer) Assert.assertTrue(org.junit.Assert.assertTrue) DebugList(org.drools.compiler.util.debug.DebugList) Test(org.junit.Test) RuleUnitUtil.getUnitName(org.drools.core.ruleunit.RuleUnitUtil.getUnitName) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) FactHandle(org.kie.api.runtime.rule.FactHandle) RuleUnit(org.kie.api.runtime.rule.RuleUnit) List(java.util.List) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) Assert.assertFalse(org.junit.Assert.assertFalse) PropertySpecificOption(org.kie.internal.builder.conf.PropertySpecificOption) ClassUtils.getCanonicalSimpleName(org.drools.core.util.ClassUtils.getCanonicalSimpleName) Assert.assertEquals(org.junit.Assert.assertEquals) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) Semaphore(java.util.concurrent.Semaphore) DebugList(org.drools.compiler.util.debug.DebugList) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 19 with RuleUnitExecutor

use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.

the class RuleUnitTest method testWithOOPath.

@Test
public void testWithOOPath() throws Exception {
    String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "rule Adult @Unit( AdultUnit.class ) when\n" + "    $p : /persons[age >= 18]\n" + "then\n" + "    System.out.println($p.getName() + \" is adult\");\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", 42), new Person("Marilena", 44), new Person("Sofia", 4));
    RuleUnit adultUnit = new AdultUnit(persons);
    assertEquals(2, 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)

Example 20 with RuleUnitExecutor

use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.

the class RuleUnitTest method testRuleUnitIdentity.

@Test
public void testRuleUnitIdentity() throws Exception {
    String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(Unit0.class) + "\n" + "import " + AgeCheckUnit.class.getCanonicalName() + "\n" + "\n" + "rule R1 when\n" + "    $i: /ds#Integer\n" + "then\n" + "    drools.guard( new AgeCheckUnit($i) );" + "end\n" + "rule RegisterAdultTicket when\n" + "    $s: /ds#String\n" + "then\n" + "    drools.guard( new AgeCheckUnit($s.length()) );" + "end";
    String drl2 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AgeCheckUnit.class) + ";\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule CheckAge when\n" + "    $p : /persons[ age > minAge ]\n" + "then\n" + "    list.add($p.getName() + \">\" + minAge);\n" + "end";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).addContent(drl2, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    DataSource<Object> ds = executor.newDataSource("ds");
    DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 42), new Person("Sofia", 4));
    List<String> list = new ArrayList<>();
    executor.bindVariable("list", list);
    ds.insert("test");
    ds.insert(3);
    ds.insert(4);
    executor.run(Unit0.class);
    System.out.println(list);
    assertEquals(3, list.size());
    assertTrue(list.containsAll(asList("Mario>4", "Mario>3", "Sofia>3")));
    list.clear();
    ds.insert("xxx");
    ds.insert("yyyy");
    executor.run(Unit0.class);
    assertEquals(0, list.size());
}
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)

Aggregations

RuleUnitExecutor (org.kie.api.runtime.rule.RuleUnitExecutor)64 Test (org.junit.Test)61 InternalRuleUnitExecutor (org.drools.core.impl.InternalRuleUnitExecutor)53 KieBase (org.kie.api.KieBase)39 KieHelper (org.kie.internal.utils.KieHelper)38 Person (org.drools.compiler.Person)33 PMML4Result (org.kie.api.pmml.PMML4Result)17 PMMLRequestData (org.kie.api.pmml.PMMLRequestData)17 RuleUnit (org.kie.api.runtime.rule.RuleUnit)17 DroolsAbstractPMMLTest (org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)16 ArrayList (java.util.ArrayList)14 KieContainer (org.kie.api.runtime.KieContainer)9 KieServices (org.kie.api.KieServices)7 FactHandle (org.kie.api.runtime.rule.FactHandle)6 SegmentExecution (org.kie.pmml.pmml_4_2.model.mining.SegmentExecution)6 DataSource (org.kie.api.runtime.rule.DataSource)5 Arrays.asList (java.util.Arrays.asList)4 LinkedHashMap (java.util.LinkedHashMap)4 List (java.util.List)4 LongAddress (org.drools.compiler.LongAddress)4