Search in sources :

Example 36 with RuleUnitExecutor

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

the class RuleUnitTest method testRuleUnit.

@Test
public void testRuleUnit() {
    List<String> result = new ArrayList<>();
    Variable<Person> adult = declarationOf(Person.class, unitData("persons"));
    Rule rule = rule("org.drools.retebuilder", "Adult").unit(AdultUnit.class).build(expr("$expr$1$", adult, p -> p.getAge() > 18), on(adult).execute(p -> {
        System.out.println(p.getName());
        result.add(p.getName());
    }));
    Model model = new ModelImpl().addRule(rule);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kieBase);
    executor.newDataSource("persons", new Person("Mario", 43), new Person("Marilena", 44), new Person("Sofia", 5));
    executor.run(AdultUnit.class);
    assertTrue(result.containsAll(asList("Mario", "Marilena")));
}
Also used : DataSource(org.kie.api.runtime.rule.DataSource) FlowDSL.rule(org.drools.model.FlowDSL.rule) FlowDSL.expr(org.drools.model.FlowDSL.expr) FlowDSL.unitData(org.drools.model.FlowDSL.unitData) Variable(org.drools.model.Variable) FlowDSL.on(org.drools.model.FlowDSL.on) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) ArrayList(java.util.ArrayList) RuleUnit(org.kie.api.runtime.rule.RuleUnit) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) List(java.util.List) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) Arrays.asList(java.util.Arrays.asList) Rule(org.drools.model.Rule) Person(org.drools.modelcompiler.domain.Person) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) ModelImpl(org.drools.model.impl.ModelImpl) FlowDSL.declarationOf(org.drools.model.FlowDSL.declarationOf) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) Model(org.drools.model.Model) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Example 37 with RuleUnitExecutor

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

the class UnitCompilationTest method testOr.

@Test
public void testOr() throws Exception {
    CompiledUnit unit = DrlxCompiler.compileFolders("src/test/resources/model", "src/test/resources/unit4");
    RuleUnitExecutor executor = unit.createExecutor();
    Constructor<?> personConstructor = unit.getConstructorFor("org.model.Person", String.class, int.class);
    Constructor<?> childConstructor = unit.getConstructorFor("org.model.Child", String.class, int.class, int.class);
    DataSource<?> persons = executor.newDataSource("persons", personConstructor.newInstance("Mario", 43), personConstructor.newInstance("Marilena", 44), childConstructor.newInstance("Sofia", 5, 10));
    RuleUnit ruleUnit = unit.getOrCreateRuleUnit();
    assertEquals(1, executor.run(ruleUnit));
}
Also used : RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) RuleUnit(org.kie.api.runtime.rule.RuleUnit) Test(org.junit.Test)

Example 38 with RuleUnitExecutor

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

the class RuleUnitTest method testWithOOPathAndNotNoMatch.

@Test
public void testWithOOPathAndNotNoMatch() 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", 44), new Person("Marilena", 170), new Person("Sofia", 18));
    RuleUnit adultUnit = new AdultUnit(persons);
    assertEquals(0, 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 39 with RuleUnitExecutor

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

the class RuleUnitTest method testWithDataSource.

@Test
public void testWithDataSource() 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 = executor.newDataSource("persons", new Person("Mario", 42), new Person("Marilena", 44), new Person("Sofia", 4));
    // explicitly create unit
    assertEquals(2, executor.run(new AdultUnit(persons)));
    // 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 40 with RuleUnitExecutor

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

the class RuleUnitTest method testReactiveDataSourceWithRunUntilHalt.

@Test(timeout = 10000L)
public void testReactiveDataSourceWithRunUntilHalt() throws Exception {
    String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + ReactiveAdultUnit.class.getCanonicalName() + "\n" + "rule Adult @Unit( ReactiveAdultUnit.class ) 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);
    DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 42));
    ReactiveAdultUnit adultUnit = new ReactiveAdultUnit(persons, list);
    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());
    persons.insert(new Person("Sofia", 4));
    persons.insert(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)

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