Search in sources :

Example 41 with RuleUnitExecutor

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

the class RuleUnitTest method testRuleUnitWithAccumulate.

@Test
public void testRuleUnitWithAccumulate() throws Exception {
    // DROOLS-2209
    String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "global java.util.List list;\n" + "rule AccumulateAdults @Unit( AdultUnit.class ) when\n" + "   accumulate( $p: Person( $age: age >= 18 ) from persons, \n" + "               $sum : sum( $age ) )\n" + "then\n" + "   list.add($sum); \n" + "end\n";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    List<Integer> list = new ArrayList<>();
    executor.getKieSession().setGlobal("list", list);
    DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 42), new Person("Marilena", 44), new Person("Sofia", 4));
    assertEquals(1, executor.run(AdultUnit.class));
    assertEquals(1, list.size());
    assertEquals(86, (int) list.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)

Example 42 with RuleUnitExecutor

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

the class RuleUnitTest method testRunUnexistingUnit.

@Test
public void testRunUnexistingUnit() 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";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    try {
        executor.run(NotAdultUnit.class);
        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) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 43 with RuleUnitExecutor

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

the class RuleUnitTest method testWithOOPathOnList.

@Test
public void testWithOOPathOnList() throws Exception {
    // DROOLS-1646
    String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnitWithList.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult when\n" + "    $p : /persons[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);
    List<Person> persons = asList(new Person("Mario", 42), new Person("Marilena", 44), new Person("Sofia", 4));
    RuleUnit adultUnit = new AdultUnitWithList(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 44 with RuleUnitExecutor

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

the class RuleUnitTest method testReactiveDataSource.

@Test
public void testReactiveDataSource() throws Exception {
    String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + ReactiveAdultUnit.class.getCanonicalName() + "\n" + "import " + ReactiveNotAdultUnit.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\");\n" + "end\n" + "rule NotAdult @Unit( ReactiveNotAdultUnit.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));
    ReactiveAdultUnit adultUnit = new ReactiveAdultUnit(persons, null);
    assertEquals(1, executor.run(adultUnit));
    ReactiveNotAdultUnit notAdultUnit = new ReactiveNotAdultUnit(persons);
    assertEquals(0, executor.run(notAdultUnit));
    persons.insert(new Person("Sofia", 4));
    assertEquals(0, executor.run(adultUnit));
    assertEquals(1, executor.run(notAdultUnit));
    persons.insert(new Person("Marilena", 44));
    assertEquals(1, executor.run(adultUnit));
    assertEquals(0, executor.run(notAdultUnit));
}
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 45 with RuleUnitExecutor

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

the class RuleUnitTest method testModifyGlobalFactWithMvelDialect.

@Test
public void testModifyGlobalFactWithMvelDialect() throws Exception {
    String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnit.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult dialect \"mvel\" when\n" + "    $p : Person(age < 18, $name : name)\n" + "then\n" + "    System.out.println($name + \" is NOT adult\");\n" + "    modify($p) { setAge(18) }\n" + "end";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    executor.getKieSession().insert(new Person("Sofia", 4));
    assertEquals(1, executor.run(AdultUnit.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)

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