Browse files

Scorecards: Changes to keep in sync with drools-pmml changes and othe…

…r code refactorings
  • Loading branch information...
1 parent 9415e92 commit 4130a34953a5b1a02a0c5da600caefa11fb1a45e @vinodkiran vinodkiran committed Oct 18, 2012
Showing with 271 additions and 94 deletions.
  1. +1 −1 drools-scorecards/pom.xml
  2. +7 −11 drools-scorecards/src/main/java/org/drools/scorecards/ScorecardCompiler.java
  3. +8 −7 drools-scorecards/src/main/java/org/drools/scorecards/drl/AbstractDRLEmitter.java
  4. +2 −2 drools-scorecards/src/main/java/org/drools/scorecards/drl/DeclaredTypesDRLEmitter.java
  5. +58 −1 drools-scorecards/src/main/java/org/drools/scorecards/drl/ExternalModelDRLEmitter.java
  6. +3 −3 drools-scorecards/src/main/java/org/drools/scorecards/parser/AbstractScorecardParser.java
  7. +6 −6 drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/ExcelScorecardValidator.java
  8. +12 −15 drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/XLSEventDataCollector.java
  9. +1 −0 drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/XLSKeywords.java
  10. +7 −7 drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/XLSScorecardParser.java
  11. +4 −0 drools-scorecards/src/main/java/org/drools/scorecards/pmml/PMMLExtensionNames.java
  12. +31 −8 drools-scorecards/src/main/java/org/drools/scorecards/pmml/PMMLGenerator.java
  13. +3 −12 drools-scorecards/src/main/java/org/drools/scorecards/pmml/ScorecardPMMLUtils.java
  14. +2 −2 drools-scorecards/src/test/java/org/drools/scorecards/DrlFromPMMLTest.java
  15. +68 −2 drools-scorecards/src/test/java/org/drools/scorecards/ExternalObjectModelTest.java
  16. +4 −11 drools-scorecards/src/test/java/org/drools/scorecards/PMMLDocumentTest.java
  17. +2 −6 drools-scorecards/src/test/java/org/drools/scorecards/ScorecardReasonCodeTest.java
  18. +12 −0 drools-scorecards/src/test/java/org/drools/scorecards/example/Applicant.java
  19. +40 −0 drools-scorecards/src/test/java/org/drools/scorecards/example/Customer.java
  20. BIN drools-scorecards/src/test/resources/scoremodel_c.xls
  21. BIN drools-scorecards/src/test/resources/scoremodel_externalmodel.xls
View
2 drools-scorecards/pom.xml
@@ -130,7 +130,7 @@
</executions>
<configuration>
<outputDirectory>${basedir}/target/generated-sources/java</outputDirectory>
- <packageName>org.dmg.pmml_4_1</packageName>
+ <packageName>org.dmg.pmml.pmml_4_1.descr</packageName>
<schemaDirectory>${basedir}/src/main/resources/org/dmg/pmml</schemaDirectory>
<bindingDirectory>${basedir}/src/main/resources/org/dmg/pmml</bindingDirectory>
<extension>true</extension>
View
18 drools-scorecards/src/main/java/org/drools/scorecards/ScorecardCompiler.java
@@ -16,17 +16,7 @@
package org.drools.scorecards;
-import java.io.BufferedInputStream;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.InputStream;
-import java.io.StringWriter;
-import java.util.List;
-import javax.xml.bind.JAXBContext;
-import javax.xml.bind.JAXBException;
-import javax.xml.bind.Marshaller;
-
-import org.dmg.pmml_4_1.PMML;
+import org.dmg.pmml.pmml_4_1.descr.PMML;
import org.drools.scorecards.drl.DeclaredTypesDRLEmitter;
import org.drools.scorecards.drl.ExternalModelDRLEmitter;
import org.drools.scorecards.parser.AbstractScorecardParser;
@@ -35,6 +25,12 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import java.io.*;
+import java.util.List;
+
public class ScorecardCompiler {
private PMML pmmlDocument = null;
View
15 drools-scorecards/src/main/java/org/drools/scorecards/drl/AbstractDRLEmitter.java
@@ -15,7 +15,7 @@
*/
package org.drools.scorecards.drl;
-import org.dmg.pmml_4_1.*;
+import org.dmg.pmml.pmml_4_1.descr.*;
import org.drools.core.util.StringUtils;
import org.drools.scorecards.parser.xls.XLSKeywords;
import org.drools.scorecards.pmml.PMMLExtensionNames;
@@ -138,9 +138,9 @@ protected void addDeclaredTypes(PMML pmml, Package aPackage) {
Scorecard scorecard = (Scorecard) obj;
Characteristics characteristics = getCharacteristicsFromScorecard(scorecard);
createInitialRule(ruleList, scorecard);
- for (org.dmg.pmml_4_1.Characteristic c : characteristics.getCharacteristics()) {
+ for (org.dmg.pmml.pmml_4_1.descr.Characteristic c : characteristics.getCharacteristics()) {
int attributePosition = 0;
- for (org.dmg.pmml_4_1.Attribute scoreAttribute : c.getAttributes()) {
+ for (org.dmg.pmml.pmml_4_1.descr.Attribute scoreAttribute : c.getAttributes()) {
String name = formRuleName(pmmlDocument, scorecard.getModelName().replaceAll(" ",""), c, scoreAttribute);
Rule rule = new Rule(name, 99, 1);
String desc = ScorecardPMMLUtils.getExtensionValue(scoreAttribute.getExtensions(), "description");
@@ -195,9 +195,10 @@ protected void createInitialRule(List<Rule> ruleList, Scorecard scorecard) {
if (scorecard.getReasonCodeAlgorithm() != null) {
Consequence consequence = new Consequence();
if ("pointsAbove".equalsIgnoreCase(scorecard.getReasonCodeAlgorithm())) {
- consequence.setSnippet("$sc.setReasonCodeAlgorithm(DroolsScorecard.REASON_CODE_ALGORITHM_POINTSABOVE);");
+ //TODO: ReasonCode Algorithm
+ consequence.setSnippet("//$sc.setReasonCodeAlgorithm(DroolsScorecard.REASON_CODE_ALGORITHM_POINTSABOVE);");
} else if ("pointsBelow".equalsIgnoreCase(scorecard.getReasonCodeAlgorithm())) {
- consequence.setSnippet("$sc.setReasonCodeAlgorithm(DroolsScorecard.REASON_CODE_ALGORITHM_POINTSBELOW);");
+ consequence.setSnippet("//$sc.setReasonCodeAlgorithm(DroolsScorecard.REASON_CODE_ALGORITHM_POINTSBELOW);");
}
rule.addConsequence(consequence);
}
@@ -260,7 +261,7 @@ protected void createFieldRestriction(PMML pmmlDocument, Characteristic c, Attri
} else if (PMMLOperators.NOT_EQUAL.equalsIgnoreCase(operator)) {
stringBuilder.append(" <> ");
} else if (PMMLOperators.EQUAL.equalsIgnoreCase(operator)) {
- stringBuilder.append(" = ");
+ stringBuilder.append(" == ");
} else if (PMMLOperators.GREATER_OR_EQUAL.equalsIgnoreCase(operator)) {
stringBuilder.append(" >= ");
} else if (PMMLOperators.LESS_OR_EQUAL.equalsIgnoreCase(operator)) {
@@ -287,7 +288,7 @@ protected void createFieldRestriction(PMML pmmlDocument, Characteristic c, Attri
} else if (PMMLOperators.NOT_EQUAL.equalsIgnoreCase(operator)) {
stringBuilder.append(" <> ");
} else if (PMMLOperators.EQUAL.equalsIgnoreCase(operator)) {
- stringBuilder.append(" = ");
+ stringBuilder.append(" == ");
} else if (PMMLOperators.GREATER_OR_EQUAL.equalsIgnoreCase(operator)) {
stringBuilder.append(" >= ");
} else if (PMMLOperators.LESS_OR_EQUAL.equalsIgnoreCase(operator)) {
View
4 drools-scorecards/src/main/java/org/drools/scorecards/drl/DeclaredTypesDRLEmitter.java
@@ -16,7 +16,7 @@
package org.drools.scorecards.drl;
-import org.dmg.pmml_4_1.*;
+import org.dmg.pmml.pmml_4_1.descr.*;
import org.drools.scorecards.parser.xls.XLSKeywords;
import org.drools.scorecards.pmml.ScorecardPMMLUtils;
import org.drools.template.model.Condition;
@@ -30,7 +30,7 @@
protected void addDeclaredTypeContents(PMML pmmlDocument, StringBuilder stringBuilder, Scorecard scorecard) {
Characteristics characteristics = getCharacteristicsFromScorecard(scorecard);
- for (org.dmg.pmml_4_1.Characteristic c : characteristics.getCharacteristics()) {
+ for (org.dmg.pmml.pmml_4_1.descr.Characteristic c : characteristics.getCharacteristics()) {
String field = ScorecardPMMLUtils.extractFieldNameFromCharacteristic(c);
String dataType = ScorecardPMMLUtils.getDataType(pmmlDocument, field);
//String dataType = ScorecardPMMLUtils.getExtensionValue(c.getExtensions(), PMMLExtensionNames.CHARACTERTISTIC_DATATYPE);
View
59 drools-scorecards/src/main/java/org/drools/scorecards/drl/ExternalModelDRLEmitter.java
@@ -16,7 +16,7 @@
package org.drools.scorecards.drl;
-import org.dmg.pmml_4_1.*;
+import org.dmg.pmml.pmml_4_1.descr.*;
import org.drools.scorecards.pmml.PMMLExtensionNames;
import org.drools.scorecards.pmml.ScorecardPMMLUtils;
import org.drools.template.model.Condition;
@@ -67,11 +67,68 @@ protected void addLHSConditions(Rule rule, PMML pmmlDocument, Scorecard scorecar
@Override
protected void addAdditionalReasonCodeConsequence(Rule rule, Scorecard scorecard) {
+ if (!scorecard.isUseReasonCodes()) {
+ return;
+ }
+ String externalClassName = null;
+ String reasonCodesField = null;
+ String fieldName = 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();
+ fieldName = outputField.getName();
+ Extension e = ScorecardPMMLUtils.getExtension(outputField.getExtensions(), PMMLExtensionNames.SCORECARD_RESULTANT_REASONCODES_FIELD);
+ if (e != null) {
+ reasonCodesField = e.getValue();
+ }
+ break;
+ }
+ }
+ if ( reasonCodesField != null && externalClassName != null && fieldName != null) {
+ Consequence consequence = new Consequence();
+ StringBuilder stringBuilder = new StringBuilder("$");
+ stringBuilder.append(fieldName).append("Var").append(".set").append(Character.toUpperCase(reasonCodesField.charAt(0))).append(reasonCodesField.substring(1));
+ stringBuilder.append("($reasons);");
+ consequence.setSnippet(stringBuilder.toString());
+ rule.addConsequence(consequence);
+ }
}
@Override
protected void addAdditionalReasonCodeCondition(Rule rule, Scorecard scorecard) {
+ if (!scorecard.isUseReasonCodes()) {
+ return;
+ }
+ String externalClassName = null;
+ String reasonCodesField = null;
+ String fieldName = 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();
+ fieldName = outputField.getName();
+ Extension e = ScorecardPMMLUtils.getExtension(outputField.getExtensions(), PMMLExtensionNames.SCORECARD_RESULTANT_REASONCODES_FIELD);
+ if (e != null) {
+ reasonCodesField = e.getValue();
+ }
+ break;
+ }
+ }
+ if ( reasonCodesField != null && externalClassName != null && fieldName != null) {
+ Condition condition = new Condition();
+ StringBuilder stringBuilder = new StringBuilder("$");
+ stringBuilder.append(fieldName).append("Var : ").append(externalClassName).append("()");
+ condition.setSnippet(stringBuilder.toString());
+ rule.addCondition(condition);
+ }
}
View
6 drools-scorecards/src/main/java/org/drools/scorecards/parser/AbstractScorecardParser.java
@@ -16,12 +16,12 @@
package org.drools.scorecards.parser;
+import org.dmg.pmml.pmml_4_1.descr.PMML;
+import org.drools.scorecards.ScorecardError;
+
import java.io.InputStream;
import java.util.List;
-import org.dmg.pmml_4_1.PMML;
-import org.drools.scorecards.ScorecardError;
-
public abstract class AbstractScorecardParser {
public abstract List<ScorecardError> parseFile(InputStream inStream, String worksheetName) throws ScorecardParseException;
View
12 ...ls-scorecards/src/main/java/org/drools/scorecards/parser/xls/ExcelScorecardValidator.java
@@ -16,18 +16,18 @@
package org.drools.scorecards.parser.xls;
-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.Scorecard;
+import org.dmg.pmml.pmml_4_1.descr.Attribute;
+import org.dmg.pmml.pmml_4_1.descr.Characteristic;
+import org.dmg.pmml.pmml_4_1.descr.Characteristics;
+import org.dmg.pmml.pmml_4_1.descr.Scorecard;
import org.drools.core.util.StringUtils;
import org.drools.scorecards.ScorecardError;
import org.drools.scorecards.StringUtil;
import org.drools.scorecards.pmml.PMMLExtensionNames;
import org.drools.scorecards.pmml.ScorecardPMMLUtils;
+import java.util.List;
+
class ExcelScorecardValidator {
private Scorecard scorecard;
View
27 drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/XLSEventDataCollector.java
@@ -16,28 +16,19 @@
package org.drools.scorecards.parser.xls;
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-
import org.apache.poi.hssf.util.CellReference;
-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.Extension;
-import org.dmg.pmml_4_1.FIELDUSAGETYPE;
-import org.dmg.pmml_4_1.INVALIDVALUETREATMENTMETHOD;
-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.Scorecard;
+import org.dmg.pmml.pmml_4_1.descr.*;
import org.drools.core.util.StringUtils;
import org.drools.scorecards.ScorecardError;
import org.drools.scorecards.parser.ScorecardParseException;
import org.drools.scorecards.pmml.PMMLExtensionNames;
import org.drools.scorecards.pmml.ScorecardPMMLUtils;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
class XLSEventDataCollector {
private List<DataExpectation> expectations = new ArrayList<DataExpectation>();
@@ -151,6 +142,12 @@ private void setAdditionalExpectation(int currentRowCtr, int currentColCtr, Stri
scorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(extension);
addExpectation(currentRowCtr, currentColCtr + 1, "value", extension, null);
+ } else if (XLSKeywords.SCORECARD_RESULTANT_REASONCODES_FIELD.equalsIgnoreCase(stringCellValue)) {
+ Extension extension = new Extension();
+ extension.setName(PMMLExtensionNames.SCORECARD_RESULTANT_REASONCODES_FIELD);
+ scorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(extension);
+ addExpectation(currentRowCtr, currentColCtr + 1, "value", extension, null);
+
} else if (XLSKeywords.SCORECARD_BASE_SCORE.equalsIgnoreCase(stringCellValue)) {
addExpectation(currentRowCtr, currentColCtr + 1, "initialScore", scorecard, null);
View
1 drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/XLSKeywords.java
@@ -21,6 +21,7 @@
public static final String SCORECARD_NAME = "Scorecard Name";
public static final String SCORECARD_RESULTANT_SCORE_CLASS = "Resultant Score Class";
public static final String SCORECARD_RESULTANT_SCORE_FIELD = "Resultant Score Field";
+ public static final String SCORECARD_RESULTANT_REASONCODES_FIELD = "Resultant Reasoncodes Field";
public static final String SCORECARD_CHARACTERISTIC_EXTERNAL_CLASS = "Full Class Name";
public static final String SCORECARD_BASE_SCORE = "Initial Score";
View
14 drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/XLSScorecardParser.java
@@ -15,25 +15,25 @@
*/
package org.drools.scorecards.parser.xls;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.List;
-
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
-import org.dmg.pmml_4_1.PMML;
-import org.dmg.pmml_4_1.Scorecard;
+import org.dmg.pmml.pmml_4_1.descr.PMML;
+import org.dmg.pmml.pmml_4_1.descr.Scorecard;
import org.drools.scorecards.ScorecardError;
import org.drools.scorecards.parser.AbstractScorecardParser;
import org.drools.scorecards.parser.ScorecardParseException;
import org.drools.scorecards.pmml.PMMLGenerator;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
public class XLSScorecardParser extends AbstractScorecardParser {
protected XLSEventDataCollector excelDataCollector;
View
4 drools-scorecards/src/main/java/org/drools/scorecards/pmml/PMMLExtensionNames.java
@@ -22,11 +22,15 @@
public static final String SCORECARD_CELL_REF = "cellRef";
public static final String SCORECARD_RESULTANT_SCORE_FIELD = "final";
+ public static final String SCORECARD_RESULTANT_REASONCODES_FIELD = "reasonCodeField";
public static final String SCORECARD_RESULTANT_SCORE_CLASS = "externalClass";
public static final String SCORECARD_IMPORTS = "importsFromDelimitedString";
public static final String CHARACTERTISTIC_EXTERNAL_CLASS = "externalClass";
public static final String CHARACTERTISTIC_FACTTYPE = "factType";
public static final String CHARACTERTISTIC_FIELD = "field";
public static final String CHARACTERTISTIC_DATATYPE = "dataType";
+
+ public static final String DEFAULT_PREDICTED_FIELD = "scorecard__calculatedScore";
+
}
View
39 drools-scorecards/src/main/java/org/drools/scorecards/pmml/PMMLGenerator.java
@@ -16,7 +16,7 @@
package org.drools.scorecards.pmml;
-import org.dmg.pmml_4_1.*;
+import org.dmg.pmml.pmml_4_1.descr.*;
import org.drools.core.util.StringUtils;
import org.drools.scorecards.StringUtil;
import org.drools.scorecards.parser.xls.XLSKeywords;
@@ -88,6 +88,18 @@ private void repositionExternalClassExtensions(Scorecard pmmlScorecard) {
}
}
}
+ MiningField targetField = new MiningField();
+ targetField.setName( PMMLExtensionNames.DEFAULT_PREDICTED_FIELD );
+ targetField.setUsageType( FIELDUSAGETYPE.PREDICTED );
+ schema.getMiningFields().add( targetField );
+ } else if ( obj instanceof Output ) {
+ for ( OutputField of : ((Output) obj).getOutputFields() ) {
+ //TODO FIXME : is "calculatedScore" a constant name?
+ // or is there always one outputfield?
+ if ( "calculatedScore".equals( of.getName() ) ) {
+ of.setTargetField( PMMLExtensionNames.DEFAULT_PREDICTED_FIELD );
+ }
+ }
}
}
}
@@ -96,7 +108,7 @@ private void removeAttributeFieldExtension(Scorecard pmmlScorecard) {
for (Object obj : pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas()) {
if (obj instanceof Characteristics) {
Characteristics characteristics = (Characteristics) obj;
- for (org.dmg.pmml_4_1.Characteristic characteristic : characteristics.getCharacteristics()) {
+ for (org.dmg.pmml.pmml_4_1.descr.Characteristic characteristic : characteristics.getCharacteristics()) {
for (Attribute attribute : characteristic.getAttributes()) {
Extension fieldExtension = ScorecardPMMLUtils.getExtension(attribute.getExtensions(), PMMLExtensionNames.CHARACTERTISTIC_FIELD);
if ( fieldExtension != null ) {
@@ -117,7 +129,7 @@ private void createAndSetDataDictionary(PMML pmml, Scorecard pmmlScorecard) {
for (Object obj : pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas()) {
if (obj instanceof Characteristics) {
Characteristics characteristics = (Characteristics) obj;
- for (org.dmg.pmml_4_1.Characteristic characteristic : characteristics.getCharacteristics()) {
+ for (org.dmg.pmml.pmml_4_1.descr.Characteristic characteristic : characteristics.getCharacteristics()) {
DataField dataField = new DataField();
Extension dataTypeExtension = ScorecardPMMLUtils.getExtension(characteristic.getExtensions(), PMMLExtensionNames.CHARACTERTISTIC_DATATYPE);
@@ -157,12 +169,18 @@ private void createAndSetDataDictionary(PMML pmml, Scorecard pmmlScorecard) {
}
}
}
- dataDictionary.setNumberOfFields(BigInteger.valueOf(ctr));
+ DataField targetField = new DataField();
+ targetField.setName( PMMLExtensionNames.DEFAULT_PREDICTED_FIELD );
+ targetField.setDataType( DATATYPE.DOUBLE );
+ targetField.setOptype( OPTYPE.CONTINUOUS );
+ dataDictionary.getDataFields().add( targetField );
+ dataDictionary.setNumberOfFields(BigInteger.valueOf(ctr + 1));
}
private void createAndSetOutput(Scorecard pmmlScorecard) {
Extension classExtension = ScorecardPMMLUtils.getExtension(pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas(), PMMLExtensionNames.SCORECARD_RESULTANT_SCORE_CLASS);
Extension fieldExtension = ScorecardPMMLUtils.getExtension(pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas(), PMMLExtensionNames.SCORECARD_RESULTANT_SCORE_FIELD);
+ Extension reasonCodeExtension = ScorecardPMMLUtils.getExtension(pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas(), PMMLExtensionNames.SCORECARD_RESULTANT_REASONCODES_FIELD);
for (Object obj : pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas()) {
if (obj instanceof Output) {
Output output = (Output)obj;
@@ -181,6 +199,11 @@ private void createAndSetOutput(Scorecard pmmlScorecard) {
}
output.getOutputFields().add(outputField);
outputField.setFeature(RESULTFEATURE.PREDICTED_VALUE);
+ if ( reasonCodeExtension != null ) {
+ pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().remove(reasonCodeExtension);
+ //TODO: Add output field for reason codes.
+ outputField.getExtensions().add(reasonCodeExtension);
+ }
break;
}
}
@@ -190,7 +213,7 @@ private void createAndSetPredicates(Scorecard pmmlScorecard) {
for (Object obj : pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas()) {
if (obj instanceof Characteristics) {
Characteristics characteristics = (Characteristics) obj;
- for (org.dmg.pmml_4_1.Characteristic characteristic : characteristics.getCharacteristics()) {
+ for (org.dmg.pmml.pmml_4_1.descr.Characteristic characteristic : characteristics.getCharacteristics()) {
String dataType = ScorecardPMMLUtils.getExtensionValue(characteristic.getExtensions(), PMMLExtensionNames.CHARACTERTISTIC_DATATYPE);
Extension predicateExtension = null;
for (Attribute attribute : characteristic.getAttributes()) {
@@ -308,10 +331,10 @@ private void setNumericPredicate(Attribute pmmlAttribute, String field, String p
simplePredicate.setValue(predicateAsString.substring(3).trim());
} else if (predicateAsString.startsWith("<")) {
simplePredicate.setOperator(PMMLOperators.LESS_THAN);
- simplePredicate.setValue(predicateAsString.substring(3).trim());
+ simplePredicate.setValue(predicateAsString.substring(2).trim());
} else if (predicateAsString.startsWith(">")) {
simplePredicate.setOperator(PMMLOperators.GREATER_THAN);
- simplePredicate.setValue(predicateAsString.substring(3).trim());
+ simplePredicate.setValue(predicateAsString.substring(2).trim());
}
pmmlAttribute.setSimplePredicate(simplePredicate);
}
@@ -321,7 +344,7 @@ private void removeEmptyExtensions(Scorecard pmmlScorecard) {
for (Object obj : pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas()) {
if (obj instanceof Characteristics) {
Characteristics characteristics = (Characteristics) obj;
- for (org.dmg.pmml_4_1.Characteristic characteristic : characteristics.getCharacteristics()) {
+ for (org.dmg.pmml.pmml_4_1.descr.Characteristic characteristic : characteristics.getCharacteristics()) {
List<Extension> toRemoveExtensionsList = new ArrayList<Extension>();
for (Extension extension : characteristic.getExtensions()) {
if (StringUtils.isEmpty(extension.getValue())) {
View
15 drools-scorecards/src/main/java/org/drools/scorecards/pmml/ScorecardPMMLUtils.java
@@ -16,20 +16,11 @@
package org.drools.scorecards.pmml;
-import java.util.List;
-
-import org.dmg.pmml_4_1.Attribute;
-import org.dmg.pmml_4_1.Characteristic;
-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.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.pmml_4_1.descr.*;
import org.drools.scorecards.parser.xls.XLSKeywords;
+import java.util.List;
+
public class ScorecardPMMLUtils {
// public static String getDataType(org.dmg.pmml_4_1.Characteristic c) {
View
4 drools-scorecards/src/test/java/org/drools/scorecards/DrlFromPMMLTest.java
@@ -1,6 +1,6 @@
package org.drools.scorecards;
-import org.dmg.pmml_4_1.PMML;
+import org.dmg.pmml.pmml_4_1.descr.PMML;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
@@ -36,7 +36,7 @@ public void setUp() throws Exception {
public void testDrlNoNull() throws Exception {
assertNotNull(drl);
assertTrue(drl.length() > 0);
- //System.out.println(drl);
+ System.out.println(drl);
}
@Test
View
70 drools-scorecards/src/test/java/org/drools/scorecards/ExternalObjectModelTest.java
@@ -1,6 +1,6 @@
package org.drools.scorecards;
-import org.dmg.pmml_4_1.*;
+import org.dmg.pmml.pmml_4_1.descr.*;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
@@ -86,7 +86,6 @@ public void testDrlNoNull() throws Exception {
@Test
public void testDRLExecution() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
-
kbuilder.add( ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
for (KnowledgeBuilderError error : kbuilder.getErrors()){
System.out.println(error.getMessage());
@@ -192,4 +191,71 @@ public void testDRLExecutionWithInitialScore(String drl2) throws Exception {
//occupation = +10, age = +40, state = -10, validLicense = 1, initialScore=100
assertEquals(141.0,applicant.getTotalScore());
}
+
+ @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();
+ 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).");
+ }
+ testDRLExecutionWithReasonCodes(drl2);
+ }
+
+ public void testDRLExecutionWithReasonCodes(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());
+ assertTrue(applicant.getReasonCodes().size() > 0);
+
+ 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
15 drools-scorecards/src/test/java/org/drools/scorecards/PMMLDocumentTest.java
@@ -1,21 +1,14 @@
package org.drools.scorecards;
import junit.framework.Assert;
-import org.dmg.pmml_4_1.Attribute;
-import org.dmg.pmml_4_1.Characteristics;
-import org.dmg.pmml_4_1.DataDictionary;
-import org.dmg.pmml_4_1.Header;
-import org.dmg.pmml_4_1.MiningSchema;
-import org.dmg.pmml_4_1.Output;
-import org.dmg.pmml_4_1.PMML;
-import org.dmg.pmml_4_1.Scorecard;
+import org.dmg.pmml.pmml_4_1.descr.*;
import org.drools.scorecards.pmml.PMMLExtensionNames;
import org.drools.scorecards.pmml.ScorecardPMMLUtils;
import org.junit.Before;
import org.junit.Test;
import static junit.framework.Assert.*;
-import static org.drools.scorecards.ScorecardCompiler.DrlType.*;
+import static org.drools.scorecards.ScorecardCompiler.DrlType.INTERNAL_DECLARED_TYPES;
public class PMMLDocumentTest {
@@ -49,7 +42,7 @@ public void testHeader() throws Exception {
public void testDataDictionary() throws Exception {
DataDictionary dataDictionary = pmmlDocument.getDataDictionary();
assertNotNull(dataDictionary);
- assertEquals(4, dataDictionary.getNumberOfFields().intValue());
+ assertEquals(5, dataDictionary.getNumberOfFields().intValue());
assertEquals("age", dataDictionary.getDataFields().get(0).getName());
assertEquals("occupation",dataDictionary.getDataFields().get(1).getName());
assertEquals("residenceState", dataDictionary.getDataFields().get(2).getName());
@@ -63,7 +56,7 @@ public void testMiningSchema() throws Exception {
for (Object obj :((Scorecard)serializable) .getExtensionsAndCharacteristicsAndMiningSchemas()){
if (obj instanceof MiningSchema){
MiningSchema miningSchema = ((MiningSchema)obj);
- assertEquals(4, miningSchema.getMiningFields().size());
+ assertEquals(5, miningSchema.getMiningFields().size());
assertEquals("age", miningSchema.getMiningFields().get(0).getName());
assertEquals("occupation",miningSchema.getMiningFields().get(1).getName());
assertEquals("residenceState", miningSchema.getMiningFields().get(2).getName());
View
8 drools-scorecards/src/test/java/org/drools/scorecards/ScorecardReasonCodeTest.java
@@ -1,11 +1,7 @@
package org.drools.scorecards;
import junit.framework.Assert;
-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.pmml_4_1.descr.*;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
@@ -19,7 +15,7 @@
import org.junit.Test;
import static junit.framework.Assert.*;
-import static org.drools.scorecards.ScorecardCompiler.DrlType.*;
+import static org.drools.scorecards.ScorecardCompiler.DrlType.INTERNAL_DECLARED_TYPES;
public class ScorecardReasonCodeTest {
private static PMML pmmlDocument;
View
12 drools-scorecards/src/test/java/org/drools/scorecards/example/Applicant.java
@@ -1,5 +1,7 @@
package org.drools.scorecards.example;
+import java.util.List;
+
/**
* Created with IntelliJ IDEA.
* User: vinod
@@ -14,6 +16,16 @@
double totalScore;
boolean validLicense;
+ public List<String> getReasonCodes() {
+ return reasonCodes;
+ }
+
+ public void setReasonCodes(List<String> reasonCodes) {
+ this.reasonCodes = reasonCodes;
+ }
+
+ List<String> reasonCodes;
+
public boolean isValidLicense() {
return validLicense;
}
View
40 drools-scorecards/src/test/java/org/drools/scorecards/example/Customer.java
@@ -0,0 +1,40 @@
+package org.drools.scorecards.example;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: vinod kiran
+ * Date: 14/10/12
+ * Time: 11:00 AM
+ */
+public class Customer {
+ double customerScore;
+ int customerAge;
+ String placeOfResidence;
+
+ public Customer() {
+ }
+
+ public double getCustomerScore() {
+ return customerScore;
+ }
+
+ public void setCustomerScore(double customerScore) {
+ this.customerScore = customerScore;
+ }
+
+ public int getCustomerAge() {
+ return customerAge;
+ }
+
+ public void setCustomerAge(int customerAge) {
+ this.customerAge = customerAge;
+ }
+
+ public String getPlaceOfResidence() {
+ return placeOfResidence;
+ }
+
+ public void setPlaceOfResidence(String placeOfResidence) {
+ this.placeOfResidence = placeOfResidence;
+ }
+}
View
BIN drools-scorecards/src/test/resources/scoremodel_c.xls
Binary file not shown.
View
BIN drools-scorecards/src/test/resources/scoremodel_externalmodel.xls
Binary file not shown.

0 comments on commit 4130a34

Please sign in to comment.