Search in sources :

Example 21 with RuleUnitExecutor

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

the class RuleUnitExample method go.

public void go() {
    KieServices ks = KieServices.Factory.get();
    KieContainer kcontainer = ks.getKieClasspathContainer();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kcontainer.getKieBase());
    DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 42));
    executor.run(PersonRuleUnit.class);
    persons.insert(new Person("Sofia", 4));
    executor.run(PersonRuleUnit.class);
    persons.insert(new Person("Marilena", 44));
    executor.run(PersonRuleUnit.class);
}
Also used : RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieServices(org.kie.api.KieServices) KieContainer(org.kie.api.runtime.KieContainer)

Example 22 with RuleUnitExecutor

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

the class RuleUnitExampleTest method testGo.

@Test
public void testGo() {
    KieServices ks = KieServices.Factory.get();
    KieContainer kcontainer = ks.getKieClasspathContainer();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kcontainer.getKieBase());
    DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 42));
    assertEquals(1, executor.run(PersonRuleUnit.class));
    persons.insert(new Person("Sofia", 4));
    assertEquals(0, executor.run(PersonRuleUnit.class));
    persons.insert(new Person("Marilena", 44));
    assertEquals(1, executor.run(PersonRuleUnit.class));
}
Also used : PersonRuleUnit(org.drools.example.api.ruleunit.PersonRuleUnit) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieServices(org.kie.api.KieServices) Person(org.drools.example.api.ruleunit.Person) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 23 with RuleUnitExecutor

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

the class RuleUnitCompilerTest method testRuleUnit.

@Test
public void testRuleUnit() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "rule Adult @Unit( AdultUnit.class ) when\n" + "    Person(age >= 18, $name : name) from persons\n" + "then\n" + "    results.add($name);\n" + "end\n";
    KieContainer kieContainer = getKieContainer(null, str);
    RuleUnitExecutor executor = kieContainer.newRuleUnitExecutor();
    DataSource<Person> persons = DataSource.create(new Person("Mario", 42), new Person("Marilena", 44), new Person("Sofia", 4));
    AdultUnit unit = new AdultUnit(persons);
    assertEquals(2, executor.run(unit));
    assertTrue(unit.getResults().containsAll(asList("Mario", "Marilena")));
}
Also used : RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) Person(org.drools.modelcompiler.domain.Person) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 24 with RuleUnitExecutor

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

the class RuleUnitCompilerTest method testWith2Rules.

@Test
public void testWith2Rules() {
    String str = "package " + this.getClass().getPackage().getName() + ";\n" + "unit " + getCanonicalSimpleName(PositiveNegativeDTUnit.class) + ";\n" + "import " + BigDecimal.class.getCanonicalName() + ";\n" + "rule R1 \n" + "when\n" + "  BigDecimal( intValue() >= 0 ) from input1\n" + "then\n" + "  output1.insert(\"positive\");\n" + "end\n" + "rule R2 \n" + "when\n" + "  BigDecimal( intValue() < 0 ) from input1\n" + "then\n" + "  output1.insert(\"negative\");\n" + "end\n";
    KieContainer kieContainer = getKieContainer(null, str);
    RuleUnitExecutor executor = kieContainer.newRuleUnitExecutor();
    PositiveNegativeDTUnit ruleUnit = new PositiveNegativeDTUnit(47);
    executor.run(ruleUnit);
    assertEquals("positive", ruleUnit.getPositive_or_negative());
    ruleUnit = new PositiveNegativeDTUnit(-999);
    executor.run(ruleUnit);
    assertEquals("negative", ruleUnit.getPositive_or_negative());
}
Also used : RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 25 with RuleUnitExecutor

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

the class RuleUnitTest method testRuleUnitWithVarBinding.

@Test
public void testRuleUnitWithVarBinding() {
    Variable<AdultUnit> unit = declarationOf(AdultUnit.class);
    Variable<Person> adult = declarationOf(Person.class, unitData("persons"));
    Rule rule = rule("org.drools.retebuilder", "Adult").unit(RuleUnitTest.AdultUnit.class).build(expr("$expr$1$", adult, unit, (p, u) -> p.getAge() > u.getAdultAge()), on(adult, unitData(List.class, "results")).execute((p, r) -> {
        System.out.println(p.getName());
        r.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));
    AdultUnit ruleUnit = new AdultUnit();
    executor.run(ruleUnit);
    assertTrue(ruleUnit.getResults().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) Model(org.drools.model.Model) ArrayList(java.util.ArrayList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) Person(org.drools.modelcompiler.domain.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