use of org.kie.internal.utils.KieHelper 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));
}
use of org.kie.internal.utils.KieHelper 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)));
}
use of org.kie.internal.utils.KieHelper 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));
}
use of org.kie.internal.utils.KieHelper 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")));
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class RuleUnitTest method testRuleUnitInvocationFromConsequence.
@Test
public void testRuleUnitInvocationFromConsequence() 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\n" + "rule NotAdult @Unit( NotAdultUnit.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> log = new ArrayList<>();
executor.bindVariable("log", log);
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);
}
Aggregations