Search in sources :

Example 1 with DataSource

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();
}
Also used : DataSource(org.kie.api.runtime.rule.DataSource) UnitVar(org.kie.api.definition.rule.UnitVar) ResourceType(org.kie.api.io.ResourceType) ArrayList(java.util.ArrayList) Person(org.drools.compiler.Person) Arrays.asList(java.util.Arrays.asList) LongAddress(org.drools.compiler.LongAddress) KieServices(org.kie.api.KieServices) RuleUnitFactory(org.drools.core.ruleunit.RuleUnitFactory) Assert.fail(org.junit.Assert.fail) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieFileSystem(org.kie.api.builder.KieFileSystem) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) Semaphore(java.util.concurrent.Semaphore) Results(org.kie.api.builder.Results) KieContainer(org.kie.api.runtime.KieContainer) Assert.assertTrue(org.junit.Assert.assertTrue) DebugList(org.drools.compiler.util.debug.DebugList) Test(org.junit.Test) RuleUnitUtil.getUnitName(org.drools.core.ruleunit.RuleUnitUtil.getUnitName) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) FactHandle(org.kie.api.runtime.rule.FactHandle) RuleUnit(org.kie.api.runtime.rule.RuleUnit) List(java.util.List) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) Assert.assertFalse(org.junit.Assert.assertFalse) PropertySpecificOption(org.kie.internal.builder.conf.PropertySpecificOption) ClassUtils.getCanonicalSimpleName(org.drools.core.util.ClassUtils.getCanonicalSimpleName) Assert.assertEquals(org.junit.Assert.assertEquals) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) Semaphore(java.util.concurrent.Semaphore) DebugList(org.drools.compiler.util.debug.DebugList) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 2 with DataSource

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]);
    }
}
Also used : PMMLRequestData(org.kie.api.pmml.PMMLRequestData) PMML4Result(org.kie.api.pmml.PMML4Result) Resource(org.kie.api.io.Resource) KieHelper(org.kie.internal.utils.KieHelper) EntryPoint(org.kie.api.runtime.rule.EntryPoint) DataSource(org.kie.api.runtime.rule.DataSource) LinkedHashMap(java.util.LinkedHashMap) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) Test(org.junit.Test) DroolsAbstractPMMLTest(org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)

Aggregations

InternalRuleUnitExecutor (org.drools.core.impl.InternalRuleUnitExecutor)2 Test (org.junit.Test)2 DataSource (org.kie.api.runtime.rule.DataSource)2 RuleUnitExecutor (org.kie.api.runtime.rule.RuleUnitExecutor)2 KieHelper (org.kie.internal.utils.KieHelper)2 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Semaphore (java.util.concurrent.Semaphore)1 LongAddress (org.drools.compiler.LongAddress)1 Person (org.drools.compiler.Person)1 DebugList (org.drools.compiler.util.debug.DebugList)1 RuleUnitFactory (org.drools.core.ruleunit.RuleUnitFactory)1 RuleUnitUtil.getUnitName (org.drools.core.ruleunit.RuleUnitUtil.getUnitName)1 ClassUtils.getCanonicalSimpleName (org.drools.core.util.ClassUtils.getCanonicalSimpleName)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Assert.fail (org.junit.Assert.fail)1