use of org.kie.api.runtime.rule.DataSource in project drools by kiegroup.
the class RuleUnitTest method testReactiveDataSourceWithRunUntilHalt.
@Test(timeout = 10000L)
public void testReactiveDataSourceWithRunUntilHalt() throws Exception {
String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + ReactiveAdultUnit.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\");" + " list.add($name);\n" + "end";
KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
DebugList<String> list = new DebugList<>();
executor.bindVariable("list", list);
DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 42));
ReactiveAdultUnit adultUnit = new ReactiveAdultUnit(persons, list);
Semaphore ready = new Semaphore(0, true);
list.onItemAdded = (l -> ready.release());
new Thread(() -> executor.runUntilHalt(adultUnit)).start();
ready.acquire();
assertEquals(1, list.size());
assertEquals("Mario", list.get(0));
list.clear();
list.onItemAdded = (l -> ready.release());
persons.insert(new Person("Sofia", 4));
persons.insert(new Person("Marilena", 44));
ready.acquire();
assertEquals(1, list.size());
assertEquals("Marilena", list.get(0));
executor.halt();
}
use of org.kie.api.runtime.rule.DataSource in project drools by kiegroup.
the class ScorecardTest method testMultipleInputData.
@Test
public void testMultipleInputData() throws Exception {
RuleUnitExecutor[] executor = new RuleUnitExecutor[3];
PMMLRequestData[] requestData = new PMMLRequestData[3];
PMML4Result[] resultHolder = new PMML4Result[3];
Resource res = ResourceFactory.newClassPathResource(source1);
kbase = new KieHelper().addResource(res, ResourceType.PMML).build();
executor[0] = RuleUnitExecutor.create().bind(kbase);
executor[1] = RuleUnitExecutor.create().bind(kbase);
executor[2] = RuleUnitExecutor.create().bind(kbase);
DataSource<PMMLRequestData>[] requests = new DataSource[3];
DataSource<PMML4Result>[] results = new DataSource[3];
DataSource<PMML4Data>[] pmmlDatas = new DataSource[3];
Double[] expectedScores = new Double[3];
expectedScores[0] = 41.345;
expectedScores[1] = 26.345;
expectedScores[2] = 39.345;
LinkedHashMap<String, Double>[] expectedResults = new LinkedHashMap[3];
expectedResults[0] = new LinkedHashMap<>();
expectedResults[0].put("LX00", -1.0);
expectedResults[0].put("RES", -10.0);
expectedResults[0].put("CX2", -30.0);
expectedResults[1] = new LinkedHashMap<>();
expectedResults[1].put("RES", 10.0);
expectedResults[1].put("LX00", -1.0);
expectedResults[1].put("OCC", -10.0);
expectedResults[1].put("ABZ", -25.0);
expectedResults[2] = new LinkedHashMap<>();
expectedResults[2].put("LX00", 1.0);
expectedResults[2].put("OCC", -5.0);
expectedResults[2].put("RES", -5.0);
expectedResults[2].put("CX1", -30.0);
requestData[0] = createRequest("123", "Sample Score", 33.0, "SKYDIVER", "KN", true);
requestData[1] = createRequest("124", "Sample Score", 50.0, "TEACHER", "AP", true);
requestData[2] = createRequest("125", "Sample Score", 10.0, "STUDENT", "TN", false);
for (int x = 0; x < 3; x++) {
requests[x] = executor[x].newDataSource("request");
results[x] = executor[x].newDataSource("results");
pmmlDatas[x] = executor[x].newDataSource("pmmlData");
resultHolder[x] = new PMML4Result(requestData[x].getCorrelationId());
}
List<String> possiblePackages = calculatePossiblePackageNames("Sample Score", "org.drools.scorecards.example");
Class<? extends RuleUnit> unitClass = getStartingRuleUnit("RuleUnitIndicator", (InternalKnowledgeBase) kbase, possiblePackages);
assertNotNull(unitClass);
for (int x = 0; x < 3; x++) {
executor[x].run(unitClass);
}
for (int y = 0; y < 3; y++) {
requests[y].insert(requestData[y]);
results[y].insert(resultHolder[y]);
}
for (int z = 0; z < 3; z++) {
executor[z].run(unitClass);
}
for (int p = 0; p < 3; p++) {
checkResult(resultHolder[p], expectedScores[p], expectedResults[p]);
}
}
Aggregations