Search in sources :

Example 26 with RuleUnitExecutor

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

the class UnitCompilationTest method testMultipleConsequences.

@Test
public void testMultipleConsequences() throws Exception {
    CompiledUnit unit = DrlxCompiler.compileFolders("src/test/resources/model", "src/test/resources/unit3");
    RuleUnitExecutor executor = unit.createExecutor();
    Constructor<?> personConstructor = unit.getConstructorFor("org.model.Person", String.class, int.class);
    Constructor<?> childConstructor = unit.getConstructorFor("org.model.Child", String.class, int.class, int.class);
    DataSource<?> persons = executor.newDataSource("persons", personConstructor.newInstance("Mario", 43), personConstructor.newInstance("Marilena", 44), childConstructor.newInstance("Sofia", 5, 10));
    RuleUnit ruleUnit = unit.getOrCreateRuleUnit();
    assertEquals(3, executor.run(ruleUnit));
}
Also used : RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) RuleUnit(org.kie.api.runtime.rule.RuleUnit) Test(org.junit.Test)

Example 27 with RuleUnitExecutor

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

the class UnitCompilationTest method testFolderUnit.

@Test
public void testFolderUnit() throws Exception {
    CompiledUnit unit = DrlxCompiler.compileFolders("src/test/resources/model", "src/test/resources/unit2");
    RuleUnitExecutor executor = unit.createExecutor();
    Constructor<?> personConstructor = unit.getConstructorFor("org.model.Person", String.class, int.class);
    Constructor<?> childConstructor = unit.getConstructorFor("org.model.Child", String.class, int.class, int.class);
    DataSource<?> persons = executor.newDataSource("persons", personConstructor.newInstance("Mario", 43), personConstructor.newInstance("Marilena", 44), childConstructor.newInstance("Sofia", 5, 10));
    RuleUnit ruleUnit = unit.getOrCreateRuleUnit();
    assertEquals(1, executor.run(ruleUnit));
}
Also used : RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) RuleUnit(org.kie.api.runtime.rule.RuleUnit) Test(org.junit.Test)

Example 28 with RuleUnitExecutor

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

the class UnitCompilationTest method testSingleFileUnit.

@Test
public void testSingleFileUnit() throws Exception {
    CompiledUnit unit = DrlxCompiler.compileSingleSource(getClass().getClassLoader().getResourceAsStream("unit1/AdultUnit.java"));
    RuleUnitExecutor executor = unit.createExecutor();
    Constructor<?> constructor = unit.getConstructorFor("org.unit1.AdultUnit$Person", String.class, int.class);
    DataSource<?> persons = executor.newDataSource("persons", constructor.newInstance("Mario", 43), constructor.newInstance("Marilena", 44), constructor.newInstance("Sofia", 5));
    RuleUnit ruleUnit = unit.getOrCreateRuleUnit();
    assertEquals(2, executor.run(ruleUnit));
}
Also used : RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) RuleUnit(org.kie.api.runtime.rule.RuleUnit) Test(org.junit.Test)

Example 29 with RuleUnitExecutor

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

the class DecisionTreeTest method testTreeFromMiningModel.

@Test
public void testTreeFromMiningModel() throws Exception {
    RuleUnitExecutor executor = createExecutor("org/kie/pmml/pmml_4_2/test_tree_from_mm.pmml");
    PMMLRequestData request = new PMMLRequestData("1234", "SampleMineTree1");
    request.addRequestParam("fld1", 30.0);
    request.addRequestParam("fld2", 60.0);
    request.addRequestParam("fld3", "false");
    request.addRequestParam("fld4", "optA");
    PMML4Result resultHolder = new PMML4Result();
    List<String> possiblePackages = calculatePossiblePackageNames("SampleMineTree1");
    Class<? extends RuleUnit> unitClass = getStartingRuleUnit("RuleUnitIndicator", (InternalKnowledgeBase) kbase, possiblePackages);
    assertNotNull(unitClass);
    int x = executor.run(unitClass);
    assertTrue(x > 0);
    data.insert(request);
    resultData.insert(resultHolder);
    executor.run(unitClass);
    assertEquals("OK", resultHolder.getResultCode());
    assertNotNull(resultHolder.getResultVariables());
    assertNotNull(resultHolder.getResultValue("Fld5", null));
    String value = resultHolder.getResultValue("Fld5", "value", String.class).orElse(null);
    assertEquals("tgtY", value);
    System.out.println(resultHolder);
}
Also used : PMMLRequestData(org.kie.api.pmml.PMMLRequestData) PMML4Result(org.kie.api.pmml.PMML4Result) 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)

Example 30 with RuleUnitExecutor

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

the class DecisionTreeTest method testMissingTreeWeighted2.

@Test
public void testMissingTreeWeighted2() throws Exception {
    Resource res = ResourceFactory.newClassPathResource(source2);
    KieBase kbase = new KieHelper().addResource(res, ResourceType.PMML).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    KieRuntimeLogger console = ((InternalRuleUnitExecutor) executor).addConsoleLogger();
    PMMLRequestData requestData = new PMMLRequestData("123", "Missing");
    requestData.addRequestParam(new ParameterInfo<>("123", "fld1", Double.class, -1.0));
    requestData.addRequestParam(new ParameterInfo<>("123", "fld2", Double.class, -1.0));
    requestData.addRequestParam(new ParameterInfo<>("123", "fld3", String.class, "miss"));
    PMML4Result resultHolder = new PMML4Result();
    DataSource<PMMLRequestData> data = executor.newDataSource("request");
    DataSource<PMML4Result> results = executor.newDataSource("results");
    DataSource<PMML4Data> pmmlData = executor.newDataSource("pmmlData");
    List<String> possiblePackages = calculatePossiblePackageNames("Missing");
    Class<? extends RuleUnit> unitClass = getStartingRuleUnit("RuleUnitIndicator", (InternalKnowledgeBase) kbase, possiblePackages);
    assertNotNull(unitClass);
    int x = executor.run(unitClass);
    data.insert(requestData);
    results.insert(resultHolder);
    executor.run(unitClass);
    console.close();
    System.out.println(resultHolder);
    Collection<?> objects = ((InternalRuleUnitExecutor) executor).getSessionObjects();
    objects.forEach(o -> {
        System.out.println(o);
    });
    pmmlData.forEach(pd -> {
        System.out.println(pd);
    });
    AbstractTreeToken token = (AbstractTreeToken) resultHolder.getResultValue("MissingTreeToken", null);
    assertNotNull(token);
    assertEquals(0.6, token.getConfidence(), 1e-6);
    assertEquals("null", token.getCurrent());
    assertEquals(100.0, token.getTotalCount(), 0.0);
    Object fld9 = resultHolder.getResultValue("Fld9", null);
    assertNotNull(fld9);
    String value = (String) resultHolder.getResultValue("Fld9", "value");
    assertNotNull(value);
    assertEquals("tgtX", value);
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) PMMLRequestData(org.kie.api.pmml.PMMLRequestData) PMML4Result(org.kie.api.pmml.PMML4Result) Resource(org.kie.api.io.Resource) PMML4Data(org.kie.api.pmml.PMML4Data) KieHelper(org.kie.internal.utils.KieHelper) KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) AbstractTreeToken(org.kie.pmml.pmml_4_2.model.tree.AbstractTreeToken) Test(org.junit.Test) DroolsAbstractPMMLTest(org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)

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