use of org.kie.api.builder.Results in project drools by kiegroup.
the class ScorecardProviderPMMLTest method testKnowledgeBaseWithExecution.
@Test
public void testKnowledgeBaseWithExecution() throws Exception {
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write(ks.getResources().newUrlResource(ScorecardProviderPMMLTest.class.getResource("/SimpleScorecard.pmml")).setSourcePath("SimpleScorecard.pmml").setResourceType(ResourceType.PMML));
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");
assertNotNull(scorecardType);
Object scorecard = scorecardType.newInstance();
assertNotNull(scorecard);
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"));
}
use of org.kie.api.builder.Results in project drools by kiegroup.
the class ScorecardReasonCodeTest method testPointsBelow.
@Test
public void testPointsBelow() throws Exception {
ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES);
scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_reasoncodes.xls"), "scorecards_pointsBelow");
assertEquals(0, scorecardCompiler.getScorecardParseErrors().size());
String drl = scorecardCompiler.getDRL();
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write(ks.getResources().newByteArrayResource(drl.getBytes()).setSourcePath("scoremodel_pointsAbove.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");
FactType scorecardInternalsType = kbase.getFactType(PMML4Helper.pmmlDefaultPackageName(), "ScoreCard");
FactType scorecardOutputType = kbase.getFactType("org.drools.scorecards.example", "SampleScoreOutput");
Object scorecard = scorecardType.newInstance();
scorecardType.set(scorecard, "age", 10);
session.insert(scorecard);
session.fireAllRules();
assertEquals(29.0, scorecardType.get(scorecard, "scorecard__calculatedScore"));
Object scorecardInternals = session.getObjects(new ClassObjectFilter(scorecardInternalsType.getFactClass())).iterator().next();
assertEquals(29.0, scorecardInternalsType.get(scorecardInternals, "score"));
Map reasonCodesMap = (Map) scorecardInternalsType.get(scorecardInternals, "ranking");
assertNotNull(reasonCodesMap);
assertEquals(2, reasonCodesMap.size());
assertEquals(16.0, reasonCodesMap.get("VL002"));
assertEquals(-20.0, reasonCodesMap.get("AGE02"));
Object scorecardOutput = session.getObjects(new ClassObjectFilter(scorecardOutputType.getFactClass())).iterator().next();
assertEquals(29.0, scorecardOutputType.get(scorecardOutput, "calculatedScore"));
assertEquals("VL002", scorecardOutputType.get(scorecardOutput, "reasonCode"));
session.dispose();
session = kbase.newKieSession();
scorecard = scorecardType.newInstance();
scorecardType.set(scorecard, "age", 0);
scorecardType.set(scorecard, "occupation", "SKYDIVER");
session.insert(scorecard);
session.fireAllRules();
assertEquals(-1.0, scorecardType.get(scorecard, "scorecard__calculatedScore"));
scorecardInternals = session.getObjects(new ClassObjectFilter(scorecardInternalsType.getFactClass())).iterator().next();
System.out.println(scorecardInternals);
assertEquals(-1.0, scorecardInternalsType.get(scorecardInternals, "score"));
reasonCodesMap = (Map) scorecardInternalsType.get(scorecardInternals, "ranking");
assertNotNull(reasonCodesMap);
assertEquals(3, reasonCodesMap.size());
assertEquals(109.0, reasonCodesMap.get("OCC01"));
assertEquals(16.0, reasonCodesMap.get("VL002"));
assertEquals(0.0, reasonCodesMap.get("AGE01"));
scorecardOutput = session.getObjects(new ClassObjectFilter(scorecardOutputType.getFactClass())).iterator().next();
assertEquals(-1.0, scorecardOutputType.get(scorecardOutput, "calculatedScore"));
assertEquals("OCC01", scorecardOutputType.get(scorecardOutput, "reasonCode"));
session.dispose();
session = kbase.newKieSession();
scorecard = scorecardType.newInstance();
scorecardType.set(scorecard, "age", 20);
scorecardType.set(scorecard, "occupation", "TEACHER");
scorecardType.set(scorecard, "residenceState", "AP");
scorecardType.set(scorecard, "validLicense", true);
session.insert(scorecard);
session.fireAllRules();
assertEquals(41.0, scorecardType.get(scorecard, "scorecard__calculatedScore"));
scorecardInternals = session.getObjects(new ClassObjectFilter(scorecardInternalsType.getFactClass())).iterator().next();
System.out.println(scorecardInternals);
assertEquals(41.0, scorecardInternalsType.get(scorecardInternals, "score"));
reasonCodesMap = (Map) scorecardInternalsType.get(scorecardInternals, "ranking");
assertNotNull(reasonCodesMap);
assertEquals(4, reasonCodesMap.size());
assertEquals(89.0, reasonCodesMap.get("OCC02"));
assertEquals(22.0, reasonCodesMap.get("RS001"));
assertEquals(14.0, reasonCodesMap.get("VL001"));
assertEquals(-30.0, reasonCodesMap.get("AGE03"));
scorecardOutput = session.getObjects(new ClassObjectFilter(scorecardOutputType.getFactClass())).iterator().next();
assertEquals(41.0, scorecardOutputType.get(scorecardOutput, "calculatedScore"));
assertEquals("OCC02", scorecardOutputType.get(scorecardOutput, "reasonCode"));
session.dispose();
}
use of org.kie.api.builder.Results in project drools by kiegroup.
the class ScorecardReasonCodeTest method testPointsAbove.
@Test
public void testPointsAbove() throws Exception {
ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES);
scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_reasoncodes.xls"), "scorecards_pointsAbove");
assertEquals(0, scorecardCompiler.getScorecardParseErrors().size());
String drl = scorecardCompiler.getDRL();
assertNotNull(drl);
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write(ks.getResources().newByteArrayResource(drl.getBytes()).setSourcePath("scoremodel_pointsAbove.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");
FactType scorecardInternalsType = kbase.getFactType(PMML4Helper.pmmlDefaultPackageName(), "ScoreCard");
FactType scorecardOutputType = kbase.getFactType("org.drools.scorecards.example", "SampleScoreOutput");
Object scorecard = scorecardType.newInstance();
scorecardType.set(scorecard, "age", 10);
session.insert(scorecard);
session.fireAllRules();
assertEquals(29.0, scorecardType.get(scorecard, "scorecard__calculatedScore"));
Object scorecardInternals = session.getObjects(new ClassObjectFilter(scorecardInternalsType.getFactClass())).iterator().next();
assertEquals(29.0, scorecardInternalsType.get(scorecardInternals, "score"));
Map reasonCodesMap = (Map) scorecardInternalsType.get(scorecardInternals, "ranking");
assertNotNull(reasonCodesMap);
assertEquals(2, reasonCodesMap.size());
assertEquals(-16.0, reasonCodesMap.get("VL002"));
assertEquals(20.0, reasonCodesMap.get("AGE02"));
Object scorecardOutput = session.getObjects(new ClassObjectFilter(scorecardOutputType.getFactClass())).iterator().next();
assertEquals(29.0, scorecardOutputType.get(scorecardOutput, "calculatedScore"));
assertEquals("AGE02", scorecardOutputType.get(scorecardOutput, "reasonCode"));
session.dispose();
session = kbase.newKieSession();
scorecard = scorecardType.newInstance();
scorecardType.set(scorecard, "age", 0);
scorecardType.set(scorecard, "occupation", "SKYDIVER");
session.insert(scorecard);
session.fireAllRules();
assertEquals(-1.0, scorecardType.get(scorecard, "scorecard__calculatedScore"));
scorecardInternals = session.getObjects(new ClassObjectFilter(scorecardInternalsType.getFactClass())).iterator().next();
System.out.println(scorecardInternals);
assertEquals(-1.0, scorecardInternalsType.get(scorecardInternals, "score"));
reasonCodesMap = (Map) scorecardInternalsType.get(scorecardInternals, "ranking");
assertNotNull(reasonCodesMap);
assertEquals(3, reasonCodesMap.size());
assertEquals(-109.0, reasonCodesMap.get("OCC01"));
assertEquals(-16.0, reasonCodesMap.get("VL002"));
assertEquals(0.0, reasonCodesMap.get("AGE01"));
assertEquals(Arrays.asList("AGE01", "VL002", "OCC01"), new ArrayList(reasonCodesMap.keySet()));
scorecardOutput = session.getObjects(new ClassObjectFilter(scorecardOutputType.getFactClass())).iterator().next();
assertEquals(-1.0, scorecardOutputType.get(scorecardOutput, "calculatedScore"));
assertEquals("AGE01", scorecardOutputType.get(scorecardOutput, "reasonCode"));
session.dispose();
session = kbase.newKieSession();
scorecard = scorecardType.newInstance();
scorecardType.set(scorecard, "age", 20);
scorecardType.set(scorecard, "occupation", "TEACHER");
scorecardType.set(scorecard, "residenceState", "AP");
scorecardType.set(scorecard, "validLicense", true);
session.insert(scorecard);
session.fireAllRules();
assertEquals(41.0, scorecardType.get(scorecard, "scorecard__calculatedScore"));
scorecardInternals = session.getObjects(new ClassObjectFilter(scorecardInternalsType.getFactClass())).iterator().next();
System.out.println(scorecardInternals);
assertEquals(41.0, scorecardInternalsType.get(scorecardInternals, "score"));
reasonCodesMap = (Map) scorecardInternalsType.get(scorecardInternals, "ranking");
assertNotNull(reasonCodesMap);
assertEquals(4, reasonCodesMap.size());
assertEquals(-89.0, reasonCodesMap.get("OCC02"));
assertEquals(-22.0, reasonCodesMap.get("RS001"));
assertEquals(-14.0, reasonCodesMap.get("VL001"));
assertEquals(30.0, reasonCodesMap.get("AGE03"));
assertEquals(Arrays.asList("AGE03", "VL001", "RS001", "OCC02"), new ArrayList(reasonCodesMap.keySet()));
scorecardOutput = session.getObjects(new ClassObjectFilter(scorecardOutputType.getFactClass())).iterator().next();
assertEquals(41.0, scorecardOutputType.get(scorecardOutput, "calculatedScore"));
assertEquals("AGE03", scorecardOutputType.get(scorecardOutput, "reasonCode"));
session.dispose();
}
use of org.kie.api.builder.Results in project drools by kiegroup.
the class ScorecardReasonCodeTest method testReasonCodesCombinations.
@Test
public void testReasonCodesCombinations() throws Exception {
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write(ks.getResources().newClassPathResource("scoremodel_reasoncodes.xls").setSourcePath("scoremodel_reasoncodes.xls").setResourceType(ResourceType.SCARD));
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");
FactType scorecardInternalsType = kbase.getFactType(PMML4Helper.pmmlDefaultPackageName(), "ScoreCard");
FactType scorecardOutputType = kbase.getFactType("org.drools.scorecards.example", "SampleScoreOutput");
Object scorecard = scorecardType.newInstance();
scorecardType.set(scorecard, "age", 10);
session.insert(scorecard);
session.fireAllRules();
assertEquals(129.0, scorecardType.get(scorecard, "scorecard__calculatedScore"));
Object scorecardInternals = session.getObjects(new ClassObjectFilter(scorecardInternalsType.getFactClass())).iterator().next();
assertEquals(129.0, scorecardInternalsType.get(scorecardInternals, "score"));
Map reasonCodesMap = (Map) scorecardInternalsType.get(scorecardInternals, "ranking");
assertNotNull(reasonCodesMap);
assertEquals(2, reasonCodesMap.size());
assertEquals(16.0, reasonCodesMap.get("VL002"));
assertEquals(-20.0, reasonCodesMap.get("AGE02"));
Object scorecardOutput = session.getObjects(new ClassObjectFilter(scorecardOutputType.getFactClass())).iterator().next();
assertEquals(129.0, scorecardOutputType.get(scorecardOutput, "calculatedScore"));
assertEquals("VL002", scorecardOutputType.get(scorecardOutput, "reasonCode"));
session.dispose();
session = kbase.newKieSession();
scorecard = scorecardType.newInstance();
scorecardType.set(scorecard, "age", 0);
scorecardType.set(scorecard, "occupation", "SKYDIVER");
session.insert(scorecard);
session.fireAllRules();
assertEquals(99.0, scorecardType.get(scorecard, "scorecard__calculatedScore"));
scorecardInternals = session.getObjects(new ClassObjectFilter(scorecardInternalsType.getFactClass())).iterator().next();
System.out.println(scorecardInternals);
assertEquals(99.0, scorecardInternalsType.get(scorecardInternals, "score"));
reasonCodesMap = (Map) scorecardInternalsType.get(scorecardInternals, "ranking");
assertNotNull(reasonCodesMap);
assertEquals(3, reasonCodesMap.size());
assertEquals(109.0, reasonCodesMap.get("OCC01"));
assertEquals(16.0, reasonCodesMap.get("VL002"));
assertEquals(0.0, reasonCodesMap.get("AGE01"));
scorecardOutput = session.getObjects(new ClassObjectFilter(scorecardOutputType.getFactClass())).iterator().next();
assertEquals(99.0, scorecardOutputType.get(scorecardOutput, "calculatedScore"));
assertEquals("OCC01", scorecardOutputType.get(scorecardOutput, "reasonCode"));
session.dispose();
session = kbase.newKieSession();
scorecard = scorecardType.newInstance();
scorecardType.set(scorecard, "age", 20);
scorecardType.set(scorecard, "occupation", "TEACHER");
scorecardType.set(scorecard, "residenceState", "AP");
scorecardType.set(scorecard, "validLicense", true);
session.insert(scorecard);
session.fireAllRules();
assertEquals(141.0, scorecardType.get(scorecard, "scorecard__calculatedScore"));
scorecardInternals = session.getObjects(new ClassObjectFilter(scorecardInternalsType.getFactClass())).iterator().next();
System.out.println(scorecardInternals);
assertEquals(141.0, scorecardInternalsType.get(scorecardInternals, "score"));
reasonCodesMap = (Map) scorecardInternalsType.get(scorecardInternals, "ranking");
assertNotNull(reasonCodesMap);
assertEquals(4, reasonCodesMap.size());
assertEquals(89.0, reasonCodesMap.get("OCC02"));
assertEquals(22.0, reasonCodesMap.get("RS001"));
assertEquals(14.0, reasonCodesMap.get("VL001"));
assertEquals(-30.0, reasonCodesMap.get("AGE03"));
scorecardOutput = session.getObjects(new ClassObjectFilter(scorecardOutputType.getFactClass())).iterator().next();
assertEquals(141.0, scorecardOutputType.get(scorecardOutput, "calculatedScore"));
assertEquals("OCC02", scorecardOutputType.get(scorecardOutput, "reasonCode"));
session.dispose();
}
use of org.kie.api.builder.Results in project drools by kiegroup.
the class IncrementalCompilationTest method testUpdateWithDeclarationPresent.
@Test
public void testUpdateWithDeclarationPresent() throws Exception {
// DROOLS-560
String header = "package org.drools.compiler\n" + "import org.drools.compiler.integrationtests.incrementalcompilation.IncrementalCompilationTest.FooEvent\n";
String declaration = "declare FooEvent\n" + " @timestamp( mytime )\n" + " @role( event )\n" + "end\n";
String rule1 = "rule R1 when\n" + " $e : FooEvent( )\n" + "then\n" + " insert(new Message(\"Hello R1\"));\n" + "end\n";
String rule2 = "rule R1 when\n" + " $e : FooEvent( )\n" + "then\n" + " insert(new Message(\"Hello R2\"));\n" + "end\n";
String file1 = header + declaration + rule1;
String file2 = header + declaration + rule2;
KieServices ks = KieServices.Factory.get();
// Create an in-memory jar for version 1.0.0
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
KieModule km = createAndDeployJar(ks, releaseId1, file1);
// Create a session and fire rules
KieContainer kc = ks.newKieContainer(km.getReleaseId());
KieSession ksession = kc.newKieSession();
ksession.insert(new FooEvent(0));
assertEquals(1, ksession.fireAllRules());
// Create a new jar for version 1.1.0
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
km = createAndDeployJar(ks, releaseId2, file2);
// try to update the container to version 1.1.0
Results results = kc.updateToVersion(releaseId2);
assertFalse("Errors detected on updateToVersion: " + results.getMessages(org.kie.api.builder.Message.Level.ERROR), results.hasMessages(org.kie.api.builder.Message.Level.ERROR));
// continue working with the session
ksession.insert(new FooEvent(1));
assertEquals(2, ksession.fireAllRules());
}
Aggregations