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));
}
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;
}
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;
}
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);
}
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);
}
Aggregations