Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Scorecards: Changes related to setting of Initial Score for external …

…object model scorecards...
  • Loading branch information...
commit 9415e9219b673dcfc82ead743b222bd3920ae170 1 parent dda16a1
@vinodkiran vinodkiran authored
View
23 drools-scorecards/src/main/java/org/drools/scorecards/drl/AbstractDRLEmitter.java
@@ -15,18 +15,7 @@
*/
package org.drools.scorecards.drl;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.dmg.pmml_4_1.Array;
-import org.dmg.pmml_4_1.Attribute;
-import org.dmg.pmml_4_1.Characteristic;
-import org.dmg.pmml_4_1.Characteristics;
-import org.dmg.pmml_4_1.CompoundPredicate;
-import org.dmg.pmml_4_1.PMML;
-import org.dmg.pmml_4_1.Scorecard;
-import org.dmg.pmml_4_1.SimplePredicate;
-import org.dmg.pmml_4_1.SimpleSetPredicate;
+import org.dmg.pmml_4_1.*;
import org.drools.core.util.StringUtils;
import org.drools.scorecards.parser.xls.XLSKeywords;
import org.drools.scorecards.pmml.PMMLExtensionNames;
@@ -35,6 +24,9 @@
import org.drools.template.model.*;
import org.drools.template.model.Package;
+import java.util.ArrayList;
+import java.util.List;
+
public abstract class AbstractDRLEmitter {
protected String formRuleName(PMML pmmlDocument, String modelName, Characteristic c, Attribute scoreAttribute) {
@@ -173,12 +165,8 @@ protected void createInitialRule(List<Rule> ruleList, Scorecard scorecard) {
String ruleName = objectClass+"_init";
Rule rule = new Rule(ruleName, 999, 1);
rule.setDescription("set the initial score");
- StringBuilder stringBuilder = new StringBuilder();
- String var = "$sc";
- stringBuilder.append(var).append(" : ").append(objectClass).append("()");
- Condition condition = new Condition();
- condition.setSnippet(stringBuilder.toString());
+ Condition condition = createInitialRuleCondition(scorecard, objectClass);
rule.addCondition(condition);
if (scorecard.getInitialScore() > 0 ) {
Consequence consequence = new Consequence();
@@ -377,4 +365,5 @@ protected void createSummationRules(List<Rule> ruleList, Scorecard scorecard) {
protected abstract void addAdditionalReasonCodeCondition(Rule rule, Scorecard scorecard);
protected abstract void addAdditionalSummationConsequence(Rule rule, Scorecard scorecard);
protected abstract void addAdditionalSummationCondition(Rule rule, Scorecard scorecard);
+ protected abstract Condition createInitialRuleCondition(Scorecard scorecard, String objectClass);
}
View
20 drools-scorecards/src/main/java/org/drools/scorecards/drl/DeclaredTypesDRLEmitter.java
@@ -16,13 +16,7 @@
package org.drools.scorecards.drl;
-import java.util.List;
-
-import org.dmg.pmml_4_1.Attribute;
-import org.dmg.pmml_4_1.Characteristic;
-import org.dmg.pmml_4_1.Characteristics;
-import org.dmg.pmml_4_1.PMML;
-import org.dmg.pmml_4_1.Scorecard;
+import org.dmg.pmml_4_1.*;
import org.drools.scorecards.parser.xls.XLSKeywords;
import org.drools.scorecards.pmml.ScorecardPMMLUtils;
import org.drools.template.model.Condition;
@@ -30,6 +24,8 @@
import org.drools.template.model.Package;
import org.drools.template.model.Rule;
+import java.util.List;
+
public class DeclaredTypesDRLEmitter extends AbstractDRLEmitter{
protected void addDeclaredTypeContents(PMML pmmlDocument, StringBuilder stringBuilder, Scorecard scorecard) {
@@ -113,4 +109,14 @@ protected void createEmptyScorecardCondition(Rule rule, Scorecard scorecard) {
condition.setSnippet(stringBuilder.toString());
rule.addCondition(condition);
}
+
+ protected Condition createInitialRuleCondition(Scorecard scorecard, String objectClass) {
+ String var = "$sc";
+ StringBuilder stringBuilder = new StringBuilder();
+
+ stringBuilder.append(var).append(" : ").append(objectClass).append("()");
+ Condition condition = new Condition();
+ condition.setSnippet(stringBuilder.toString());
+ return condition;
+ }
}
View
38 drools-scorecards/src/main/java/org/drools/scorecards/drl/ExternalModelDRLEmitter.java
@@ -16,17 +16,7 @@
package org.drools.scorecards.drl;
-import java.util.List;
-
-import org.dmg.pmml_4_1.Attribute;
-import org.dmg.pmml_4_1.Characteristic;
-import org.dmg.pmml_4_1.Extension;
-import org.dmg.pmml_4_1.MiningField;
-import org.dmg.pmml_4_1.MiningSchema;
-import org.dmg.pmml_4_1.Output;
-import org.dmg.pmml_4_1.OutputField;
-import org.dmg.pmml_4_1.PMML;
-import org.dmg.pmml_4_1.Scorecard;
+import org.dmg.pmml_4_1.*;
import org.drools.scorecards.pmml.PMMLExtensionNames;
import org.drools.scorecards.pmml.ScorecardPMMLUtils;
import org.drools.template.model.Condition;
@@ -34,6 +24,8 @@
import org.drools.template.model.Package;
import org.drools.template.model.Rule;
+import java.util.List;
+
public class ExternalModelDRLEmitter extends AbstractDRLEmitter {
@Override
@@ -101,7 +93,11 @@ protected void addAdditionalSummationConsequence(Rule calcTotalRule, Scorecard s
Consequence consequence = new Consequence();
StringBuilder stringBuilder = new StringBuilder("$");
stringBuilder.append(fieldName).append("Var").append(".set").append(Character.toUpperCase(fieldName.charAt(0))).append(fieldName.substring(1));
- stringBuilder.append("($calculatedScore);");
+ if (scorecard.getInitialScore() > 0 ) {
+ stringBuilder.append("($calculatedScore+$initialScore);");
+ } else {
+ stringBuilder.append("($calculatedScore);");
+ }
consequence.setSnippet(stringBuilder.toString());
calcTotalRule.addConsequence(consequence);
}
@@ -129,6 +125,24 @@ protected void addAdditionalSummationCondition(Rule calcTotalRule, Scorecard sco
condition.setSnippet(stringBuilder.toString());
calcTotalRule.addCondition(condition);
}
+ }
+ protected Condition createInitialRuleCondition(Scorecard scorecard, String objectClass) {
+ String externalClassName = null;
+ for (Object obj :scorecard.getExtensionsAndCharacteristicsAndMiningSchemas()){
+ if ( obj instanceof Output) {
+ Output output = (Output)obj;
+ final List<OutputField> outputFields = output.getOutputFields();
+ final OutputField outputField = outputFields.get(0);
+ externalClassName = ScorecardPMMLUtils.getExtension(outputField.getExtensions(), PMMLExtensionNames.SCORECARD_RESULTANT_SCORE_CLASS).getValue();
+ break;
+ }
+ }
+ if ( externalClassName != null) {
+ Condition condition = new Condition();
+ condition.setSnippet(externalClassName+"()");
+ return condition;
+ }
+ return null;
}
}
View
34 drools-scorecards/src/main/java/org/drools/scorecards/pmml/PMMLGenerator.java
@@ -16,37 +16,17 @@
package org.drools.scorecards.pmml;
+import org.dmg.pmml_4_1.*;
+import org.drools.core.util.StringUtils;
+import org.drools.scorecards.StringUtil;
+import org.drools.scorecards.parser.xls.XLSKeywords;
+
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
-import org.dmg.pmml_4_1.Array;
-import org.dmg.pmml_4_1.Attribute;
-import org.dmg.pmml_4_1.Characteristic;
-import org.dmg.pmml_4_1.Characteristics;
-import org.dmg.pmml_4_1.CompoundPredicate;
-import org.dmg.pmml_4_1.DATATYPE;
-import org.dmg.pmml_4_1.DataDictionary;
-import org.dmg.pmml_4_1.DataField;
-import org.dmg.pmml_4_1.Extension;
-import org.dmg.pmml_4_1.Header;
-import org.dmg.pmml_4_1.MiningField;
-import org.dmg.pmml_4_1.MiningSchema;
-import org.dmg.pmml_4_1.OPTYPE;
-import org.dmg.pmml_4_1.Output;
-import org.dmg.pmml_4_1.OutputField;
-import org.dmg.pmml_4_1.PMML;
-import org.dmg.pmml_4_1.RESULTFEATURE;
-import org.dmg.pmml_4_1.Scorecard;
-import org.dmg.pmml_4_1.SimplePredicate;
-import org.dmg.pmml_4_1.SimpleSetPredicate;
-import org.dmg.pmml_4_1.Timestamp;
-import org.drools.core.util.StringUtils;
-import org.drools.scorecards.StringUtil;
-import org.drools.scorecards.parser.xls.XLSKeywords;
-
public class PMMLGenerator {
public PMML generateDocument(Scorecard pmmlScorecard) {
@@ -271,6 +251,10 @@ private void setTextPredicate(Attribute pmmlAttribute, String field, String pred
simpleSetPredicate.setBooleanOperator(PMMLOperators.IS_IN);
}
simpleSetPredicate.setField(field);
+ predicateAsString = predicateAsString.trim();
+ if (predicateAsString.endsWith(",")) {
+ predicateAsString = predicateAsString.substring(0, predicateAsString.length()-1);
+ }
Array array = new Array();
array.setContent(predicateAsString.replace(",", " "));
array.setType("string");
View
75 drools-scorecards/src/test/java/org/drools/scorecards/ExternalObjectModelTest.java
@@ -1,12 +1,6 @@
package org.drools.scorecards;
-import java.util.List;
-
-import org.dmg.pmml_4_1.Extension;
-import org.dmg.pmml_4_1.Output;
-import org.dmg.pmml_4_1.OutputField;
-import org.dmg.pmml_4_1.PMML;
-import org.dmg.pmml_4_1.Scorecard;
+import org.dmg.pmml_4_1.*;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
@@ -21,8 +15,10 @@
import org.junit.Before;
import org.junit.Test;
+import java.util.List;
+
import static junit.framework.Assert.*;
-import static org.drools.scorecards.ScorecardCompiler.DrlType.*;
+import static org.drools.scorecards.ScorecardCompiler.DrlType.EXTERNAL_OBJECT_MODEL;
public class ExternalObjectModelTest {
private static String drl;
@@ -133,4 +129,67 @@ public void testDRLExecution() throws Exception {
//occupation = +10, age = +40, state = -10, validLicense = 1
assertEquals(41.0,applicant.getTotalScore());
}
+
+ @Test
+ public void testWithInitialScore() 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_initialscore") ) {
+ pmmlDocument2 = scorecardCompiler2.getPMMLDocument();
+ assertNotNull(pmmlDocument2);
+ drl2 = scorecardCompiler2.getDRL();
+ //System.out.println(drl2);
+ } else {
+ fail("failed to parse scoremodel Excel.");
+ }
+ testDRLExecutionWithInitialScore(drl2);
+ }
+
+ public void testDRLExecutionWithInitialScore(String drl2) throws Exception {
+ KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+
+ kbuilder.add( ResourceFactory.newByteArrayResource(drl2.getBytes()), ResourceType.DRL);
+ for (KnowledgeBuilderError error : kbuilder.getErrors()){
+ System.out.println(error.getMessage());
+ }
+ assertFalse( kbuilder.hasErrors() );
+
+ //BUILD RULEBASE
+ KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
+ kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
+
+ //NEW WORKING MEMORY
+ StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession();
+ Applicant applicant = new Applicant();
+ applicant.setAge(10);
+ session.insert(applicant);
+ //session.addEventListener(new DebugWorkingMemoryEventListener());
+ session.fireAllRules();
+ session.dispose();
+ //occupation = 0, age = 30, validLicence -1, initialScore=100
+ assertEquals(129.0,applicant.getTotalScore());
+
+ session = kbase.newStatefulKnowledgeSession();
+ 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());
+
+ session = kbase.newStatefulKnowledgeSession();
+ 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());
+ }
}
View
BIN  drools-scorecards/src/test/resources/scoremodel_externalmodel.xls
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.