use of org.kie.api.runtime.ClassObjectFilter 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);
}
use of org.kie.api.runtime.ClassObjectFilter 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.runtime.ClassObjectFilter 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.runtime.ClassObjectFilter 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.runtime.ClassObjectFilter in project drools by kiegroup.
the class LogicalTraitTest method testShadowAliasTraitOnClassLogicalRetract.
@Test
public void testShadowAliasTraitOnClassLogicalRetract() {
String drl = "package org.drools.test; \n" + "import org.drools.core.factmodel.traits.*; \n" + "import org.drools.core.factmodel.traits.Trait; \n" + "" + "global java.util.List list; \n" + "" + "declare trait X \n" + " fld : T \n" + " fld2 : Q \n" + "end \n" + "" + "declare trait W \n" + " fld : T \n" + "end \n" + "declare trait V \n" + " fld2 : Q \n" + "end \n" + "" + "declare Y \n" + "@Traitable( logical = true ) \n" + " fld : K \n" + " fld2 : Object \n" + "end \n" + "" + "declare trait T @Trait( logical=true ) end \n" + "declare trait Q @Trait( logical=true ) end \n" + "declare K @Traitable() end \n" + "" + "rule Don \n" + "when \n" + " $s : String( this == \"go\" ) \n" + "then \n" + " K k = new K(); \n" + " Y y = new Y( k, null ); \n" + " insert( y ); \n" + " insert( k ); \n" + "" + " don( k, Q.class ); \n" + "" + " don( y, X.class, true ); \n" + " don( y, W.class ); \n" + " don( y, V.class ); \n" + "end \n" + "" + "rule Check \n" + "when \n" + " $x : X( this isA V, fld isA T ) \n" + "then \n" + " shed( $x, V.class ); \n" + " list.add( \"ok\" );" + "end \n" + "" + "rule Check2 \n" + "salience 10 \n" + "when \n" + " String( this == \"go2\" ) \n" + " Q() \n" + " not X() not V() " + "then \n" + " list.add( \"ok2\" );" + "end \n" + "" + "rule Check3 \n" + "salience 5 \n" + "when \n" + " String( this == \"go2\" ) \n" + " K( this isA Q ) \n" + " not X() not V() " + "then \n" + " list.add( \"ok3\" );" + "end \n" + "" + "";
KnowledgeBuilder kbuilderImpl = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilderImpl.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kbuilderImpl.hasErrors()) {
fail(kbuilderImpl.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilderImpl.getKnowledgePackages());
TraitFactory.setMode(mode, kbase);
KieSession ks = kbase.newKieSession();
ArrayList list = new ArrayList();
ks.setGlobal("list", list);
FactHandle handle = ks.insert("go");
ks.fireAllRules();
assertEquals(Arrays.asList("ok"), list);
for (Object o : ks.getObjects()) {
System.out.println(o + " >> " + ((InternalFactHandle) ks.getFactHandle(o)).getEqualityKey());
}
ks.retract(handle);
ks.fireAllRules();
for (Object o : ks.getObjects(new ClassObjectFilter(ks.getKieBase().getFactType("org.drools.test", "Y").getFactClass()))) {
assertTrue(o instanceof TraitableBean);
TraitableBean tb = (TraitableBean) o;
TraitField fld = tb._getFieldTMS().getRegisteredTraitField("fld");
Set<Class<?>> types = fld.getRangeTypes();
assertEquals(2, types.size());
TraitField fld2 = tb._getFieldTMS().getRegisteredTraitField("fld2");
Set<Class<?>> types2 = fld2.getRangeTypes();
assertEquals(1, types2.size());
}
try {
ks = SerializationHelper.getSerialisedStatefulKnowledgeSession(ks, true);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
ks.setGlobal("list", list);
ks.insert("go2");
ks.fireAllRules();
System.out.println(list);
assertEquals(Arrays.asList("ok", "ok2", "ok3"), list);
}
Aggregations