use of org.kie.api.builder.Results in project drools by kiegroup.
the class AgendaRuleFlowGroupsTest method testConflictingAgendaAndRuleflowGroups.
@Test
public void testConflictingAgendaAndRuleflowGroups() throws Exception {
String drl = "package org.drools.test; " + "" + "rule Test " + " agenda-group 'ag' " + " ruleflow-group 'rf' " + "when " + "then " + "end ";
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
Results res = helper.verify();
System.err.println(res.getMessages());
assertEquals(1, res.getMessages(Message.Level.WARNING).size());
assertEquals(0, res.getMessages(Message.Level.ERROR).size());
}
use of org.kie.api.builder.Results in project drools by kiegroup.
the class PMMLUsageDemoTest method invokePmmlWithTrait.
@Test
public void invokePmmlWithTrait() {
String extraDrl = "package org.drools.pmml.pmml_4_2.test;" + "" + "import org.drools.core.factmodel.traits.Entity;" + "" + "rule \"Init\" " + "when " + " $s : String( this == \"trigger\" ) " + "then " + " System.out.println( \"Trig\" ); " + " Entity o = new Entity(); " + " insert( o ); \n" + "" + // both soft and hard fields will be used to feed data into the model
"" + " MockColdTrait input = don( o, MockColdTrait.class ); " + " modify( input ) { " + " setTemp( 22.0 );" + " } " + "end " + "" + "" + "rule Log when $x : MockColdTrait() then System.out.println( \"IN \" + $x ); end " + "rule Log2 when $x : Cold() then System.out.println( \"OUT \" + $x ); end ";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write(ResourceFactory.newClassPathResource(pmmlSource).setResourceType(ResourceType.PMML));
kfs.write(ResourceFactory.newByteArrayResource(extraDrl.getBytes()).setSourcePath("rules.drl").setResourceType(ResourceType.DRL));
Results res = ks.newKieBuilder(kfs).buildAll().getResults();
if (res.hasMessages(Message.Level.ERROR)) {
System.out.println(res.getMessages(Message.Level.ERROR));
}
assertEquals(0, res.getMessages(Message.Level.ERROR).size());
KieSession kSession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
kSession.insert("trigger");
kSession.fireAllRules();
System.out.println(reportWMObjects(kSession));
QueryResults qrs = kSession.getQueryResults("Cold", "MockCold", Variable.v);
assertTrue(qrs.iterator().hasNext());
Object val = qrs.iterator().next().get("$result");
Assert.assertEquals(0.56, val);
}
use of org.kie.api.builder.Results in project drools by kiegroup.
the class DrlFromPMMLTest method testDRLExecution.
@Test
public void testDRLExecution() throws Exception {
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write(ks.getResources().newByteArrayResource(drl.getBytes()).setSourcePath("test_scorecard_rules.drl").setResourceType(ResourceType.DRL));
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
Results res = kieBuilder.buildAll().getResults();
KieContainer kieContainer = ks.newKieContainer(kieBuilder.getKieModule().getReleaseId());
KieBase kbase = kieContainer.getKieBase();
KieSession session = kbase.newKieSession();
FactType scorecardType = kbase.getFactType("org.drools.scorecards.example", "SampleScore");
Object scorecard = scorecardType.newInstance();
scorecardType.set(scorecard, "age", 10);
session.insert(scorecard);
session.fireAllRules();
session.dispose();
// occupation = 5, age = 25, validLicence -1
assertEquals(29.0, scorecardType.get(scorecard, "scorecard__calculatedScore"));
session = kbase.newKieSession();
scorecard = scorecardType.newInstance();
scorecardType.set(scorecard, "occupation", "SKYDIVER");
scorecardType.set(scorecard, "age", 0);
session.insert(scorecard);
session.fireAllRules();
session.dispose();
// occupation = -10, age = +10, validLicense = -1;
assertEquals(-1.0, scorecardType.get(scorecard, "scorecard__calculatedScore"));
session = kbase.newKieSession();
scorecard = scorecardType.newInstance();
scorecardType.set(scorecard, "residenceState", "AP");
scorecardType.set(scorecard, "occupation", "TEACHER");
scorecardType.set(scorecard, "age", 20);
scorecardType.set(scorecard, "validLicense", true);
session.insert(scorecard);
session.fireAllRules();
session.dispose();
// occupation = +10, age = +40, state = -10, validLicense = 1
assertEquals(41.0, scorecardType.get(scorecard, "scorecard__calculatedScore"));
}
use of org.kie.api.builder.Results in project drools by kiegroup.
the class ExternalObjectModelTest method testDRLExecution.
@Test
public void testDRLExecution() throws Exception {
PMML pmmlDocument = null;
String drl = null;
if (scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_externalmodel.xls"))) {
pmmlDocument = scorecardCompiler.getPMMLDocument();
assertNotNull(pmmlDocument);
PMML4Compiler.dumpModel(pmmlDocument, System.out);
drl = scorecardCompiler.getDRL();
assertTrue(drl != null && !drl.isEmpty());
// System.out.println(drl);
} else {
fail("failed to parse scoremodel Excel.");
}
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write(ks.getResources().newByteArrayResource(drl.getBytes()).setSourcePath("test_scorecard_rules.drl").setResourceType(ResourceType.DRL));
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
Results res = kieBuilder.buildAll().getResults();
System.err.print(res.getMessages());
KieContainer kieContainer = ks.newKieContainer(kieBuilder.getKieModule().getReleaseId());
KieBase kbase = kieContainer.getKieBase();
KieSession session = kbase.newKieSession();
Applicant applicant = new Applicant();
applicant.setAge(10);
session.insert(applicant);
session.fireAllRules();
session.dispose();
// occupation = 0, age = 30, validLicence -1
assertEquals(29.0, applicant.getTotalScore(), 0.0);
session = kbase.newKieSession();
applicant = new Applicant();
applicant.setOccupation("SKYDIVER");
applicant.setAge(0);
session.insert(applicant);
session.fireAllRules();
session.dispose();
// occupation = -10, age = +10, validLicense = -1;
assertEquals(-1.0, applicant.getTotalScore(), 0.0);
session = kbase.newKieSession();
applicant = new Applicant();
applicant.setResidenceState("AP");
applicant.setOccupation("TEACHER");
applicant.setAge(20);
applicant.setValidLicense(true);
session.insert(applicant);
session.fireAllRules();
session.dispose();
// occupation = +10, age = +40, state = -10, validLicense = 1
assertEquals(41.0, applicant.getTotalScore(), 0.0);
}
use of org.kie.api.builder.Results in project drools by kiegroup.
the class ExternalObjectModelTest method testWithReasonCodes.
@Test
public void testWithReasonCodes() throws Exception {
ScorecardCompiler scorecardCompiler2 = new ScorecardCompiler(EXTERNAL_OBJECT_MODEL);
PMML pmmlDocument2 = null;
String drl2 = null;
if (scorecardCompiler2.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_externalmodel.xls"), "scorecards_reasoncode")) {
pmmlDocument2 = scorecardCompiler2.getPMMLDocument();
PMML4Compiler.dumpModel(pmmlDocument2, System.out);
assertNotNull(pmmlDocument2);
drl2 = scorecardCompiler2.getDRL();
// System.out.println(drl2);
} else {
for (ScorecardError error : scorecardCompiler2.getScorecardParseErrors()) {
System.out.println(error.getErrorLocation() + ":" + error.getErrorMessage());
}
fail("failed to parse scoremodel Excel (scorecards_reasoncode).");
}
assertNotNull(pmmlDocument2);
assertTrue(drl2 != null && !drl2.isEmpty());
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write(ks.getResources().newByteArrayResource(drl2.getBytes()).setSourcePath("test_scorecard_rules.drl").setResourceType(ResourceType.DRL));
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
Results res = kieBuilder.buildAll().getResults();
KieContainer kieContainer = ks.newKieContainer(kieBuilder.getKieModule().getReleaseId());
KieBase kbase = kieContainer.getKieBase();
KieSession session = kbase.newKieSession();
FactType scorecardInternalsType = kbase.getFactType(PMML4Helper.pmmlDefaultPackageName(), "ScoreCard");
Applicant applicant = new Applicant();
applicant.setAge(10);
session.insert(applicant);
// session.addEventListener(new DebugWorkingMemoryEventListener());
session.fireAllRules();
// occupation = 0, age = 30, validLicence -1, initialScore=100
assertEquals(129.0, applicant.getTotalScore(), 0.0);
assertEquals("VL0099", applicant.getReasonCodes());
Object scorecardInternals = session.getObjects(new ClassObjectFilter(scorecardInternalsType.getFactClass())).iterator().next();
Assert.assertEquals(129.0, scorecardInternalsType.get(scorecardInternals, "score"));
Map reasonCodesMap = (Map) scorecardInternalsType.get(scorecardInternals, "ranking");
Assert.assertNotNull(reasonCodesMap);
Assert.assertEquals(Arrays.asList("VL0099", "AGE02"), new ArrayList(reasonCodesMap.keySet()));
session.dispose();
session = kbase.newKieSession();
applicant = new Applicant();
applicant.setOccupation("SKYDIVER");
applicant.setAge(0);
session.insert(applicant);
session.fireAllRules();
session.dispose();
// occupation = -10, age = +10, validLicense = -1, initialScore=100;
assertEquals(99.0, applicant.getTotalScore(), 0.0);
session = kbase.newKieSession();
applicant = new Applicant();
applicant.setResidenceState("AP");
applicant.setOccupation("TEACHER");
applicant.setAge(20);
applicant.setValidLicense(true);
session.insert(applicant);
session.fireAllRules();
session.dispose();
// occupation = +10, age = +40, state = -10, validLicense = 1, initialScore=100
assertEquals(141.0, applicant.getTotalScore(), 0.0);
}
Aggregations