Search in sources :

Example 6 with RuleUnitExecutor

use of org.kie.api.runtime.rule.RuleUnitExecutor 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)

Example 7 with RuleUnitExecutor

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

the class RuleUnitTest method testRuleWithoutUnitsIsNotExecutor.

@Test
public void testRuleWithoutUnitsIsNotExecutor() throws Exception {
    String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "import " + NotAdultUnit.class.getCanonicalName() + "\n" + "rule Adult when\n" + "    Person(age >= 18, $name : name)\n" + "then\n" + "    System.out.println($name + \" is adult\");\n" + "end\n" + "rule NotAdult when\n" + "    Person(age < 18, $name : name)\n" + "then\n" + "    System.out.println($name + \" is NOT adult\");\n" + "end";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
    try {
        RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
        fail("It should throw IllegalStateException");
    } catch (IllegalStateException e) {
    // expected
    }
}
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) Test(org.junit.Test)

Example 8 with RuleUnitExecutor

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

the class RuleUnitTest method testMultiLevelGuards.

@Test
public void testMultiLevelGuards() throws Exception {
    String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(Unit0.class) + "\n" + "import " + UnitA.class.getCanonicalName() + "\n" + "rule X when\n" + "    $b: /ds#Boolean\n" + "then\n" + "    Boolean b = $b;\n" + "    drools.guard( UnitA.class );\n" + "end";
    String drl2 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(UnitA.class) + "\n" + "import " + UnitB.class.getCanonicalName() + "\n" + "rule A when\n" + "    $s: /ds#String\n" + "then\n" + "    drools.guard( UnitB.class );" + "end";
    String drl3 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(UnitB.class) + "\n" + "import " + UnitB.class.getCanonicalName() + "\n" + "rule B when\n" + "    $i: /ds#Integer\n" + "then\n" + "    list.add($i);" + "end";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).addContent(drl2, ResourceType.DRL).addContent(drl3, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    DataSource<Object> ds = executor.newDataSource("ds");
    List<Integer> list = new ArrayList<>();
    executor.bindVariable("list", list);
    ds.insert(1);
    executor.run(Unit0.class);
    // all units are inactive
    assertEquals(0, list.size());
    FactHandle guardA = ds.insert(true);
    executor.run(Unit0.class);
    // UnitB still inactive
    assertEquals(0, list.size());
    FactHandle guardB = ds.insert("test");
    executor.run(Unit0.class);
    // all units are active
    assertEquals(1, list.size());
    // all units are active
    assertEquals(1, (int) list.get(0));
    list.clear();
    ds.insert(2);
    executor.run(Unit0.class);
    // all units are inactive
    assertEquals(1, list.size());
    // all units are active
    assertEquals(2, (int) list.get(0));
    list.clear();
    // retracting guard A deactivate unitA and in cascade unit B
    ds.delete(guardA);
    ds.insert(3);
    executor.run(Unit0.class);
    // all units are inactive
    assertEquals(0, list.size());
    // activating guard A reactivate unitA and in cascade unit B
    guardA = ds.insert(true);
    executor.run(Unit0.class);
    // all units are active
    assertEquals(1, list.size());
    list.clear();
}
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) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) Test(org.junit.Test)

Example 9 with RuleUnitExecutor

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

the class RuleUnitTest method testTwoPartsOOPath.

@Test
public void testTwoPartsOOPath() throws Exception {
    // DROOLS-1539
    String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnit.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + LongAddress.class.getCanonicalName() + "\n" + "rule Adult when\n" + "    $a: /persons[ age > 18 ]/addresses#LongAddress[ country == \"it\" ]\n" + "then\n" + "    System.out.println($a.getCountry());\n" + "end";
    KieBase kbase = new KieHelper(PropertySpecificOption.ALWAYS).addContent(drl1, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    Person mario = new Person("Mario", 43);
    mario.setAddresses(asList(new LongAddress("street", "suburb", "zipCode", "it")));
    Person mark = new Person("Mark", 40);
    mark.setAddresses(asList(new LongAddress("street", "suburb", "zipCode", "uk")));
    DataSource<Person> persons = executor.newDataSource("persons", mario, mark);
    assertEquals(1, executor.run(AdultUnit.class));
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) LongAddress(org.drools.compiler.LongAddress) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 10 with RuleUnitExecutor

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

the class RuleUnitTest method testDeleteFromDataSource.

@Test
public void testDeleteFromDataSource() throws Exception {
    String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + FlowUnit.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "import " + FilterUnit.class.getCanonicalName() + "\n" + "rule Flow @Unit( FlowUnit.class ) when\n" + "then\n" + "    drools.run( FilterUnit.class );\n" + "    drools.run( AdultUnit.class );\n" + "end\n" + "rule filter @Unit( FilterUnit.class ) when\n" + "    $p:Person(name str[startsWith] \"D\") from persons\n" + "then\n" + "    System.out.println(\"Deleting person: \" + $p.getName() + \". Sorry man, your name starts with a 'D' ....\");\n" + "    persons.delete( $p );\n" + "end\n" + "rule AdultUnit @Unit( AdultUnit.class ) when\n" + "    Person($age : age > 18, $name : name) from persons\n" + "then\n" + "    results.add($name);\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", 43), new Person("Duncan", 34), new Person("Sofia", 6));
    List<String> results = new ArrayList<>();
    executor.bindVariable("results", results);
    assertEquals(3, executor.run(FlowUnit.class));
    assertEquals(1, results.size());
    assertEquals("Mario", results.get(0));
}
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