Search in sources :

Example 6 with InternalRuleUnitExecutor

use of org.drools.core.impl.InternalRuleUnitExecutor in project drools by kiegroup.

the class RuleUnitTest method testNamingConventionOnDrlFile.

@Test
public void testNamingConventionOnDrlFile() throws Exception {
    String drl1 = "package org.kie.test;\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult when\n" + "    $p : /persons[age >= 18]\n" + "then\n" + "    System.out.println($p.getName() + \" is adult\");\n" + "end";
    String javaRuleUnit = "package org.kie.test;\n" + "\n" + "import " + Person.class.getCanonicalName() + ";\n" + "import " + RuleUnit.class.getCanonicalName() + ";\n" + "import " + DataSource.class.getCanonicalName() + ";\n" + "\n" + "public class MyRuleUnit implements RuleUnit {\n" + "    private DataSource<Person> persons;\n" + "\n" + "    public DataSource<Person> getPersons() {\n" + "        return persons;\n" + "    }\n" + "}\n";
    String path = "org/kie/test/MyRuleUnit";
    KieServices ks = KieServices.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.writeKModuleXML(ks.newKieModuleModel().toXML()).write("src/main/resources/" + path + ".drl", drl1).write("src/main/java/" + path + ".java", javaRuleUnit);
    ks.newKieBuilder(kfs).buildAll();
    KieContainer kcontainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
    KieBase kbase = kcontainer.getKieBase();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 42));
    RuleUnit ruleUnit = new RuleUnitFactory().bindVariable("persons", persons).getOrCreateRuleUnit(((InternalRuleUnitExecutor) executor), "org.kie.test.MyRuleUnit", kcontainer.getClassLoader());
    assertEquals(1, executor.run(ruleUnit));
    persons.insert(new Person("Sofia", 4));
    assertEquals(0, executor.run(ruleUnit));
    persons.insert(new Person("Marilena", 44));
    assertEquals(1, executor.run(ruleUnit));
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) KieFileSystem(org.kie.api.builder.KieFileSystem) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) RuleUnitFactory(org.drools.core.ruleunit.RuleUnitFactory) KieServices(org.kie.api.KieServices) RuleUnit(org.kie.api.runtime.rule.RuleUnit) Person(org.drools.compiler.Person) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 7 with InternalRuleUnitExecutor

use of org.drools.core.impl.InternalRuleUnitExecutor in project drools by kiegroup.

the class DroolsAbstractPMMLTest method createExecutor.

protected RuleUnitExecutor createExecutor(String sourceName) {
    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();
    ReleaseId releaseId = new ReleaseIdImpl("org.kie:pmmlTest:1.0-SNAPSHOT");
    ((KieRepositoryImpl) kr).setDefaultGAV(releaseId);
    Resource res = ResourceFactory.newClassPathResource(sourceName);
    kbase = new KieHelper().addResource(res, ResourceType.PMML).build();
    assertNotNull(kbase);
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    KieContainer kc = ((KnowledgeBaseImpl) ((InternalRuleUnitExecutor) executor).getKieSession().getKieBase()).getKieContainer();
    InternalKieModule ikm = (InternalKieModule) kr.getKieModule(releaseId);
    try (FileOutputStream fos = new FileOutputStream("/tmp/outputModule.jar")) {
        fos.write(ikm.getBytes());
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    kc.getKieBaseNames().forEach(n -> {
        System.out.println(n);
    });
    data = executor.newDataSource("request");
    resultData = executor.newDataSource("results");
    pmmlData = executor.newDataSource("pmmlData");
    return executor;
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) ReleaseIdImpl(org.drools.compiler.kproject.ReleaseIdImpl) KieRepository(org.kie.api.builder.KieRepository) Resource(org.kie.api.io.Resource) FileNotFoundException(java.io.FileNotFoundException) KieHelper(org.kie.internal.utils.KieHelper) KieServices(org.kie.api.KieServices) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) ReleaseId(org.kie.api.builder.ReleaseId) IOException(java.io.IOException) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieRepositoryImpl(org.drools.compiler.kie.builder.impl.KieRepositoryImpl) FileOutputStream(java.io.FileOutputStream) KieContainer(org.kie.api.runtime.KieContainer) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 8 with InternalRuleUnitExecutor

use of org.drools.core.impl.InternalRuleUnitExecutor in project drools by kiegroup.

the class PMMLExecutor method run.

private PMML4Result run(PMMLRequestData requestData, Optional<PMML4Data> data) {
    KieRuntimeLogger logger = null;
    RuleUnitExecutor ruleUnitExecutor = RuleUnitExecutor.create().bind(kieBase);
    if (runWithLogging) {
        if (loggingFileName != null) {
            logger = ((InternalRuleUnitExecutor) ruleUnitExecutor).addFileLogger(loggingFileName);
        } else {
            logger = ((InternalRuleUnitExecutor) ruleUnitExecutor).addConsoleLogger();
        }
    }
    DataSource<PMMLRequestData> requestDataSource = ruleUnitExecutor.newDataSource("request");
    ;
    DataSource<PMML4Result> resultDataSource = ruleUnitExecutor.newDataSource("results");
    DataSource<PMML4Data> pmmlDataSource = ruleUnitExecutor.newDataSource("pmmlData");
    requestDataSource.insert(requestData);
    PMML4Result resultHandler = new PMML4Result();
    resultDataSource.insert(resultHandler);
    if (data.isPresent()) {
        pmmlDataSource.insert(data.get());
    }
    try {
        ruleUnitExecutor.run(startingRuleUnit("RuleUnitIndicator", requestData.getModelName(), "org.drools.scorecards.example"));
    } finally {
        if (logger != null) {
            logger.close();
        }
    }
    return resultHandler;
}
Also used : PMMLRequestData(org.kie.api.pmml.PMMLRequestData) PMML4Result(org.kie.api.pmml.PMML4Result) KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) PMML4Data(org.kie.api.pmml.PMML4Data)

Example 9 with InternalRuleUnitExecutor

use of org.drools.core.impl.InternalRuleUnitExecutor in project drools by kiegroup.

the class DecisionTreeTest method testMissingTreeWeighted1.

@Test
public void testMissingTreeWeighted1() 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, "optA"));
    PMML4Result resultHolder = new PMML4Result();
    // , requestData);
    DataSource<PMMLRequestData> data = executor.newDataSource("request");
    // , resultHolder);
    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);
    int y = 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 missingTreeToken = (AbstractTreeToken) resultHolder.getResultValue("MissingTreeToken", null);
    assertNotNull(missingTreeToken);
    assertEquals(0.8, missingTreeToken.getConfidence(), 0.0);
    assertEquals("null", missingTreeToken.getCurrent());
    assertEquals(50.0, missingTreeToken.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)

Example 10 with InternalRuleUnitExecutor

use of org.drools.core.impl.InternalRuleUnitExecutor in project drools by kiegroup.

the class MiningmodelTest method testWithRegression.

@Test
public void testWithRegression() {
    RuleUnitExecutor executor = createExecutor(source2);
    // KieRuntimeLogger console = ((InternalRuleUnitExecutor)executor).addConsoleLogger();
    PMMLRequestData request = new PMMLRequestData("123", "SampleScorecardMine");
    request.addRequestParam("fld1r", 1.0);
    request.addRequestParam("fld2r", 1.0);
    request.addRequestParam("fld3r", "x");
    PMML4Result resultHolder = new PMML4Result();
    resultHolder.setCorrelationId(request.getCorrelationId());
    DataSource<PMMLRequestData> childModelRequest = executor.newDataSource("childModelRequest");
    DataSource<PMML4Result> childModelResults = executor.newDataSource("childModelResults");
    DataSource<SegmentExecution> childModelSegments = executor.newDataSource("childModelSegments");
    DataSource<? extends AbstractPMMLData> miningModelPojo = executor.newDataSource("miningModelPojo");
    List<String> possiblePackages = this.calculatePossiblePackageNames("SampleScorecardMine");
    Class<? extends RuleUnit> ruleUnitClass = this.getStartingRuleUnit("Start Mining - SampleScorecardMine", (InternalKnowledgeBase) kbase, possiblePackages);
    assertNotNull(ruleUnitClass);
    data.insert(request);
    resultData.insert(resultHolder);
    executor.run(ruleUnitClass);
    // console.close();
    resultData.forEach(rd -> {
        assertEquals(request.getCorrelationId(), rd.getCorrelationId());
        assertEquals("OK", rd.getResultCode());
        if (rd.getSegmentationId() == null) {
            System.out.println(rd);
            assertNotNull(rd.getResultValue("RegOut", null));
            String regOutValue = rd.getResultValue("RegOut", "value", String.class).orElse(null);
            assertEquals("catC", regOutValue);
            assertNotNull(rd.getResultValue("RegProb", null));
            Double regProbValue = rd.getResultValue("RegProb", "value", Double.class).orElse(null);
            assertEquals(0.709228, regProbValue, 1e-6);
            assertNotNull(rd.getResultValue("RegProbA", null));
            Double regProbValueA = rd.getResultValue("RegProbA", "value", Double.class).orElse(null);
            assertEquals(0.010635, regProbValueA, 1e-6);
        }
    });
    int segmentsExecuted = 0;
    for (Iterator<SegmentExecution> iter = childModelSegments.iterator(); iter.hasNext(); ) {
        SegmentExecution cms = iter.next();
        assertEquals(request.getCorrelationId(), cms.getCorrelationId());
        if (cms.getState() == SegmentExecutionState.COMPLETE)
            segmentsExecuted++;
    }
    assertEquals(1, segmentsExecuted);
}
Also used : PMMLRequestData(org.kie.api.pmml.PMMLRequestData) PMML4Result(org.kie.api.pmml.PMML4Result) SegmentExecution(org.kie.pmml.pmml_4_2.model.mining.SegmentExecution) 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)12 RuleUnitExecutor (org.kie.api.runtime.rule.RuleUnitExecutor)12 Test (org.junit.Test)10 PMML4Result (org.kie.api.pmml.PMML4Result)10 PMMLRequestData (org.kie.api.pmml.PMMLRequestData)10 DroolsAbstractPMMLTest (org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)9 SegmentExecution (org.kie.pmml.pmml_4_2.model.mining.SegmentExecution)6 Iterator (java.util.Iterator)3 LinkedHashMap (java.util.LinkedHashMap)3 KieBase (org.kie.api.KieBase)3 Resource (org.kie.api.io.Resource)3 KieRuntimeLogger (org.kie.api.logger.KieRuntimeLogger)3 PMML4Data (org.kie.api.pmml.PMML4Data)3 KieHelper (org.kie.internal.utils.KieHelper)3 AbstractTreeToken (org.kie.pmml.pmml_4_2.model.tree.AbstractTreeToken)3 Map (java.util.Map)2 KieServices (org.kie.api.KieServices)2 KieContainer (org.kie.api.runtime.KieContainer)2 ScoreCard (org.kie.pmml.pmml_4_2.model.ScoreCard)2 FileNotFoundException (java.io.FileNotFoundException)1