Search in sources :

Example 11 with RuleUnitExecutor

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

the class RuleUnitTest method testUnitDeclaration.

@Test
public void testUnitDeclaration() throws Exception {
    String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnit.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult when\n" + "    Person(age >= adultAge, $name : name) from persons\n" + "then\n" + "    System.out.println($name + \" is adult\");\n" + "end";
    String drl2 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(NotAdultUnit.class) + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule NotAdult when\n" + "    $p : Person(age < 18, $name : name) from persons\n" + "then\n" + "    System.out.println($name + \" is NOT adult\");\n" + "    modify($p) { setAge(18); }\n" + "    drools.run( AdultUnit.class );\n" + "end";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).addContent(drl2, 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));
    List<String> log = new ArrayList<>();
    executor.bindVariable("log", log).bindVariable("adultAge", 18);
    assertEquals(4, executor.run(NotAdultUnit.class));
    List<String> expectedLogs = asList("org.drools.compiler.integrationtests.RuleUnitTest$NotAdultUnit started.", "org.drools.compiler.integrationtests.RuleUnitTest$NotAdultUnit yielded to org.drools.compiler.integrationtests.RuleUnitTest$AdultUnit", "org.drools.compiler.integrationtests.RuleUnitTest$AdultUnit started.", "org.drools.compiler.integrationtests.RuleUnitTest$AdultUnit ended.", "org.drools.compiler.integrationtests.RuleUnitTest$NotAdultUnit started.", "org.drools.compiler.integrationtests.RuleUnitTest$NotAdultUnit ended.");
    assertEquals(expectedLogs, log);
}
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 12 with RuleUnitExecutor

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

the class RuleUnitTest method testNestedOOPath.

@Test
public void testNestedOOPath() 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" + "    $p: /persons[ age > 18, $a: /addresses#LongAddress[ country == \"it\" ] ]\n" + "then\n" + "    System.out.println($p.getName() + \" is in \" + $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 13 with RuleUnitExecutor

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

the class RuleUnitTest method testUnboundDataSource.

@Test
public void testUnboundDataSource() throws Exception {
    // DROOLS-1533
    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 = DataSource.create(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(new NotAdultUnit(persons)));
}
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 14 with RuleUnitExecutor

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

the class RuleUnitTest method testComplexData.

@Test
public void testComplexData() {
    String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(ComplexDataUnit.class) + "\n" + "import " + RequestData.class.getCanonicalName() + "\n" + "import " + ParameterHolder.class.getCanonicalName() + "\n" + "import " + InternalData.class.getCanonicalName() + "\n" + "import " + InternalDataFactory.class.getCanonicalName() + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "\n" + "\n" + "rule 'Extract Named Parameter out of Request - Name'\n" + "when\n" + "   $request: RequestData( $id: requestId != null, modelId != null, $parameters: parameters != null ) from requestData\n" + "   $param: ParameterHolder( capitalizedName == \"Name\" ) from $parameters\n" + "then\n" + "   System.out.printf(\"Parameter found: %s with value of %s%n\",$param.getCapitalizedName(),$param.getValue());\n" + "   InternalData d = InternalDataFactory.get().createInternalData($param);\n" + "   insert(d); \n" + "end\n" + "\n" + "\n" + "rule 'Extract Named Parameter out of Request - LikesBeets'\n" + "when\n" + "   $request: RequestData( $id: requestId != null, modelId != null, $parameters: parameters != null ) from requestData\n" + "   $param: ParameterHolder( capitalizedName == \"LikesBeets\" ) from $parameters\n" + "then\n" + "   System.out.printf(\"Parameter found: %s with value of %s%n\",$param.getCapitalizedName(),$param.getValue());\n" + "   InternalData d = InternalDataFactory.get().createInternalData($param);\n" + "   insert(d); \n" + "end\n" + "\n" + "\n" + "rule 'Extract Named Parameter out of Request - HairColor'\n" + "when\n" + "   $request: RequestData( $id: requestId != null, modelId != null, $parameters: parameters != null ) from requestData\n" + "   $param: ParameterHolder( capitalizedName == \"HairColor\" ) from $parameters\n" + "then\n" + "   System.out.printf(\"Parameter found: %s with value of %s%n\",$param.getCapitalizedName(),$param.getValue());\n" + "   InternalData d = InternalDataFactory.get().createInternalData($param);\n" + "   insert(d); \n" + "end\n" + "\n" + "rule 'Extract Named Parameter out of Request - Age'\n" + "when\n" + "   $request: RequestData( $id: requestId != null, modelId != null, $parameters: parameters != null ) from requestData\n" + "   $param: ParameterHolder( capitalizedName == \"Age\" ) from $parameters\n" + "then\n" + "   System.out.printf(\"Parameter found: %s with value of %f%n\",$param.getCapitalizedName(),$param.getValue());\n" + "   InternalData d = InternalDataFactory.get().createInternalData($param);\n" + "   insert(d); \n" + "end\n" + "\n" + "rule 'Check InternalData is inserted - Age' \n" + "when\n" + "   $i: InternalData( capitalizedName == \"Age\" )\n" + "then\n" + "   System.out.printf(\"Actual Age: %f   Factored Age: %f%n\",$i.getValue(),$i.getTransformed()); \n" + "end\n" + "";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    RequestData request = new RequestData("123", "simple");
    request.addParameter("name", "Lance", String.class);
    request.addParameter("age", 54.5, Double.class);
    request.addParameter("hairColor", "bald", String.class);
    request.addParameter("likesBeets", false, Boolean.class);
    DataSource<RequestData> requestData = executor.newDataSource("requestData", request);
    assertEquals(5, executor.run(ComplexDataUnit.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 15 with RuleUnitExecutor

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

the class RuleUnitTest method testModifyOnDataSource.

@Test
public void testModifyOnDataSource() throws Exception {
    String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.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" + "    results.add($name);\n" + "end\n" + "rule NotAdult @Unit( AdultUnit.class ) when\n" + "    $p : Person(age < 18, $name : name) from persons\n" + "then\n" + "    System.out.println($name + \" is NOT adult\");\n" + "    modify($p) { setAge(18); }\n" + "    drools.run( AdultUnit.class );" + "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));
    List<String> results = new ArrayList<>();
    executor.bindVariable("results", results);
    assertEquals(4, executor.run(AdultUnit.class));
    assertEquals(3, results.size());
    assertTrue(results.containsAll(asList("Mario", "Marilena", "Sofia")));
}
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