Search in sources :

Example 46 with RuleUnitExecutor

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

the class RuleUnitTest method testNamingConventionOnDrlFile.

@Test
public void testNamingConventionOnDrlFile() throws Exception {
    String drl1 = "package org.kie.test;\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult when\n" + "    $p : /persons[age >= 18]\n" + "then\n" + "    System.out.println($p.getName() + \" is adult\");\n" + "end";
    String javaRuleUnit = "package org.kie.test;\n" + "\n" + "import " + Person.class.getCanonicalName() + ";\n" + "import " + RuleUnit.class.getCanonicalName() + ";\n" + "import " + DataSource.class.getCanonicalName() + ";\n" + "\n" + "public class MyRuleUnit implements RuleUnit {\n" + "    private DataSource<Person> persons;\n" + "\n" + "    public DataSource<Person> getPersons() {\n" + "        return persons;\n" + "    }\n" + "}\n";
    String path = "org/kie/test/MyRuleUnit";
    KieServices ks = KieServices.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.writeKModuleXML(ks.newKieModuleModel().toXML()).write("src/main/resources/" + path + ".drl", drl1).write("src/main/java/" + path + ".java", javaRuleUnit);
    ks.newKieBuilder(kfs).buildAll();
    KieContainer kcontainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
    KieBase kbase = kcontainer.getKieBase();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 42));
    RuleUnit ruleUnit = new RuleUnitFactory().bindVariable("persons", persons).getOrCreateRuleUnit(((InternalRuleUnitExecutor) executor), "org.kie.test.MyRuleUnit", kcontainer.getClassLoader());
    assertEquals(1, executor.run(ruleUnit));
    persons.insert(new Person("Sofia", 4));
    assertEquals(0, executor.run(ruleUnit));
    persons.insert(new Person("Marilena", 44));
    assertEquals(1, executor.run(ruleUnit));
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) KieFileSystem(org.kie.api.builder.KieFileSystem) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) RuleUnitFactory(org.drools.core.ruleunit.RuleUnitFactory) KieServices(org.kie.api.KieServices) RuleUnit(org.kie.api.runtime.rule.RuleUnit) Person(org.drools.compiler.Person) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 47 with RuleUnitExecutor

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

the class RuleUnitTest method testMixingGlobalDataAndDataSource.

@Test
public void testMixingGlobalDataAndDataSource() throws Exception {
    String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + FlowUnit.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "import " + NotAdultUnit.class.getCanonicalName() + "\n" + "rule Flow @Unit( FlowUnit.class ) when\n" + "then\n" + "    insert(18);\n" + "    drools.run( NotAdultUnit.class );\n" + "    drools.run( AdultUnit.class );\n" + "end\n" + "rule Adult @Unit( AdultUnit.class ) when\n" + "    $i : Integer()\n" + "    Person(age >= $i, $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 : age < 18, $name : name) from persons\n" + "    $i : Integer(this >= $age)\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));
    assertEquals(4, executor.run(FlowUnit.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 48 with RuleUnitExecutor

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

the class RuleUnitTest method testPropertyReactiveModify.

@Test(timeout = 10000L)
public void testPropertyReactiveModify() 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: /persons[ age < 18 ]\n" + "then\n" + "    System.out.println($p.getName() + \" is NOT adult\");\n" + "    modify($p) { setHappy(true); }\n" + "end";
    KieBase kbase = new KieHelper(PropertySpecificOption.ALWAYS).addContent(drl1, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    Person mario = new Person("Mario", 42);
    Person sofia = new Person("Sofia", 4);
    DataSource<Person> persons = executor.newDataSource("persons");
    FactHandle marioFh = persons.insert(mario);
    FactHandle sofiaFh = persons.insert(sofia);
    executor.run(AdultUnit.class);
    assertTrue(sofia.isHappy());
    assertFalse(mario.isHappy());
    sofia.setAge(5);
    persons.update(sofiaFh, sofia, "age");
    assertEquals(1, executor.run(AdultUnit.class));
    sofia.setSex('F');
    persons.update(sofiaFh, sofia, "sex");
    assertEquals(0, executor.run(AdultUnit.class));
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 49 with RuleUnitExecutor

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

the class RuleUnitTest method testBindingWithNamedVars.

@Test
public void testBindingWithNamedVars() throws Exception {
    String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + NamedVarsUnit.class.getCanonicalName() + "\n" + "rule Adult @Unit( NamedVarsUnit.class ) when\n" + "    $p : /persons[age >= adultAge]\n" + "then\n" + "    System.out.println($p.getName() + \" is adult and greater than \" + adultAge);\n" + "end";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    DataSource<Person> persons = executor.newDataSource("data", new Person("Mario", 42), new Person("Marilena", 44), new Person("Sofia", 4));
    executor.bindVariable("minAge", 18);
    assertEquals(2, executor.run(NamedVarsUnit.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 50 with RuleUnitExecutor

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

the class RuleUnitTest method testWithOOPathOnSingleItem.

@Test
public void testWithOOPathOnSingleItem() throws Exception {
    // DROOLS-1646
    String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnitWithSingleItem.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult when\n" + "    $p : /person[age >= adultAge]\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);
    RuleUnit adultUnit = new AdultUnitWithSingleItem(new Person("Mario", 42));
    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

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