diff --git a/drools-scorecards/pom.xml b/drools-scorecards/pom.xml deleted file mode 100644 index ad19b005..00000000 --- a/drools-scorecards/pom.xml +++ /dev/null @@ -1,143 +0,0 @@ - - - - - 4.0.0 - - drools-chance - org.drools - 5.5.0-SNAPSHOT - - - org.drools - drools-scorecards - - drools-scorecards - Add support for PMML scorecards - - - UTF-8 - - - - - junit - junit - - - - org.drools - drools-core - - - org.drools - drools-compiler - - - org.drools - drools-templates - - - - - - - org.apache.poi - poi - 3.8 - - - commons-io - commons-io - test - - - javax.xml - jaxb-xjc - 2.0EA3 - - - javax.xml.bind - jaxb-api - 2.1 - - - - com.sun.xml.bind - jaxb-impl - 2.1.13 - - - - - - Lab4Inf - http://www.lab4inf.fh-muenster.de/lab4inf/maven-repository - - - - - - - org.codehaus.mojo - build-helper-maven-plugin - 1.7 - - - include-generated-sources - - add-source - - - - ${basedir}/target/generated-sources - - - - - - - - org.codehaus.mojo - jaxb2-maven-plugin - 1.3 - - - - xjc - - - - - ${basedir}/target/generated-sources/java - org.dmg.pmml_4_1 - ${basedir}/src/main/resources/org/dmg/pmml - ${basedir}/src/main/resources/org/dmg/pmml - true - false - -no-header - - - - - diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/BaselineScore.java b/drools-scorecards/src/main/java/org/drools/scorecards/BaselineScore.java deleted file mode 100644 index 6e485b87..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/BaselineScore.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2012 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.drools.scorecards; - -import java.io.Serializable; - -public class BaselineScore extends InitialScore implements Serializable { - protected String characteristic; - - public BaselineScore(String scorecardName, String characteristic, double score) { - super(scorecardName, score); - this.characteristic = characteristic; - } - - public String getCharacteristic() { - return characteristic; - } - - public void setCharacteristic(String characteristic) { - this.characteristic = characteristic; - } -} diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/DroolsScorecard.java b/drools-scorecards/src/main/java/org/drools/scorecards/DroolsScorecard.java deleted file mode 100644 index 9ea309cd..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/DroolsScorecard.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright 2012 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.drools.scorecards; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; - -public class DroolsScorecard implements Serializable { - double calculatedScore; - List reasonCodes = new ArrayList(); - Map baselineScoreMap = new HashMap(); - private int reasonCodeAlgorithm; - public static int REASON_CODE_ALGORITHM_POINTSABOVE = 1; - public static int REASON_CODE_ALGORITHM_POINTSBELOW = -1; - - public int getReasonCodeAlgorithm() { - return reasonCodeAlgorithm; - } - - public void setReasonCodeAlgorithm(int reasonCodeAlgorithm) { - this.reasonCodeAlgorithm = reasonCodeAlgorithm; - } - - public void setBaselineScore(String characteristic, int baselineScore){ - baselineScoreMap.put(characteristic, (double)baselineScore); - } - - public void setBaselineScore(String characteristic, double baselineScore){ - baselineScoreMap.put(characteristic, baselineScore); - } - - public double getCalculatedScore() { - return calculatedScore; - } - - public void setCalculatedScore(double calculatedScore) { - this.calculatedScore = calculatedScore; - } - - public void sortReasonCodes() { - - } - -// public void addPartialScore(int partialScore) { -// this.calculatedScore += partialScore; -// } -// -// public void setInitialScore(int initialScore) { -// this.calculatedScore = initialScore; -// } - - public void setInitialScore(double initialScore) { - this.calculatedScore = initialScore; - } - -// public void addPartialScore(double partialScore) { -// this.calculatedScore += partialScore; -// } -// -// public void addPartialScore(String field, double partialScore, String reasonCode) { -// this.calculatedScore += partialScore; -// reasonCodes.add(reasonCode); -// } - -// public void addReasonCode(String reasonCode){ -// reasonCodes.add(reasonCode); -// } -// - public List getReasonCodes() { - return Collections.unmodifiableList(reasonCodes); - } - - public void setReasonCodes(List reasonCodes) { - this.reasonCodes = reasonCodes; - } - - public void sortReasonCodes(List partialScores) { - TreeMap distanceMap = new TreeMap(); - for (PartialScore partialScore : partialScores ){ - if (baselineScoreMap.get(partialScore.getCharacteristic()) != null ) { - double baseline = baselineScoreMap.get(partialScore.getCharacteristic()); - double distance = 0; - if (getReasonCodeAlgorithm() == REASON_CODE_ALGORITHM_POINTSABOVE) { - distance = (baseline - partialScore.getScore())+partialScore.getPosition(); - distanceMap.put(distance, partialScore.getReasoncode()); - } else if (getReasonCodeAlgorithm() == REASON_CODE_ALGORITHM_POINTSBELOW){ - distance = (partialScore.getScore()-baseline)+partialScore.getPosition(); - distanceMap.put(distance, partialScore.getReasoncode()); - } - } - } - - for ( Double distance : distanceMap.descendingKeySet()) { - System.out.println(distance+" "+distanceMap.get(distance)); - } - } - public DroolsScorecard() { - } -} diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/InitialScore.java b/drools-scorecards/src/main/java/org/drools/scorecards/InitialScore.java deleted file mode 100644 index c8ad247b..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/InitialScore.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2012 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.drools.scorecards; - -import java.io.Serializable; - -public class InitialScore implements Serializable { - protected String scorecardName; - protected double score; - - public String getScorecardName() { - return scorecardName; - } - - public void setScorecardName(String scorecardName) { - this.scorecardName = scorecardName; - } - - public double getScore() { - return score; - } - - public void setScore(double score) { - this.score = score; - } - - public InitialScore(String scorecardName, double score) { - - this.scorecardName = scorecardName; - this.score = score; - } - - public InitialScore() { - - } -} diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/PartialScore.java b/drools-scorecards/src/main/java/org/drools/scorecards/PartialScore.java deleted file mode 100644 index 94effc3c..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/PartialScore.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2012 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.drools.scorecards; - -import java.io.Serializable; - -public class PartialScore extends BaselineScore implements Serializable { - protected String reasoncode; - protected int position; - - public PartialScore(String scorecardName, String characteristic, double score, String reasoncode, int position) { - super(scorecardName, characteristic, score); - this.reasoncode = reasoncode; - this.position = position; - } - - public PartialScore(String scorecardName, String characteristic, double score) { - super(scorecardName, characteristic, score); - this.scorecardName = scorecardName; - this.characteristic = characteristic; - this.score = score; - } - - public int getPosition() { - return position; - } - - public String getReasoncode() { - return reasoncode; - } - - public void setReasoncode(String reasoncode) { - this.reasoncode = reasoncode; - } -} diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/ScorecardCompiler.java b/drools-scorecards/src/main/java/org/drools/scorecards/ScorecardCompiler.java deleted file mode 100644 index 44cca4ae..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/ScorecardCompiler.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Copyright 2012 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -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.drools.scorecards.drl.DeclaredTypesDRLEmitter; -import org.drools.scorecards.drl.ExternalModelDRLEmitter; -import org.drools.scorecards.parser.AbstractScorecardParser; -import org.drools.scorecards.parser.ScorecardParseException; -import org.drools.scorecards.parser.xls.XLSScorecardParser; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class ScorecardCompiler { - - private PMML pmmlDocument = null; - public static final String DEFAULT_SHEET_NAME = "scorecards"; - private List scorecardErrors; - private DrlType drlType; - private final static Logger logger = LoggerFactory.getLogger(ScorecardCompiler.class); - - public ScorecardCompiler(DrlType drlType) { - this.drlType = drlType; - } - - public ScorecardCompiler() { - this(DrlType.INTERNAL_DECLARED_TYPES); - } - - /* method for use from Guvnor */ - protected void setPMMLDocument(PMML pmmlDocument){ - this.pmmlDocument = pmmlDocument; - } - - public boolean compileFromExcel(final String pathToFile) { - return compileFromExcel(pathToFile, DEFAULT_SHEET_NAME); - } - - public boolean compileFromExcel(final String pathToFile, final String worksheetName) { - FileInputStream inputStream = null; - BufferedInputStream bufferedInputStream = null; - try { - inputStream = new FileInputStream(pathToFile); - bufferedInputStream = new BufferedInputStream(inputStream); - return compileFromExcel(bufferedInputStream, worksheetName); - } catch (FileNotFoundException e) { - logger.error(e.getMessage(), e); - } finally { - closeStream(bufferedInputStream); - closeStream(inputStream); - } - return false; - } - - public boolean compileFromExcel(final InputStream stream) { - return compileFromExcel(stream, DEFAULT_SHEET_NAME); - } - - public boolean compileFromExcel(final InputStream stream, final String worksheetName) { - try { - AbstractScorecardParser parser = new XLSScorecardParser(); - scorecardErrors = parser.parseFile(stream, worksheetName); - if ( scorecardErrors.isEmpty() ) { - pmmlDocument = parser.getPMMLDocument(); - return true; - } - } catch (ScorecardParseException e) { - logger.error(e.getMessage(), e); - } finally { - closeStream(stream); - } - return false; - } - - public PMML getPMMLDocument() { - return pmmlDocument; - } - - public String getPMML(){ - if (pmmlDocument == null ) { - return null; - } - // create a JAXBContext for the PMML class - JAXBContext ctx = null; - try { - ctx = JAXBContext.newInstance(PMML.class); - Marshaller marshaller = ctx.createMarshaller(); - // the property JAXB_FORMATTED_OUTPUT specifies whether or not the - // marshalled XML data is formatted with linefeeds and indentation - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); - // marshal the data in the Java content tree - StringWriter stringWriter = new StringWriter(); - marshaller.marshal(pmmlDocument, stringWriter); - return stringWriter.toString(); - } catch (JAXBException e) { - logger.error(e.getMessage(), e); - } - return null; - } - - public String getDRL(){ - if (pmmlDocument != null) { - if (drlType == DrlType.INTERNAL_DECLARED_TYPES) { - return new DeclaredTypesDRLEmitter().emitDRL(pmmlDocument); - } else if (drlType == DrlType.EXTERNAL_OBJECT_MODEL) { - return new ExternalModelDRLEmitter().emitDRL(pmmlDocument); - } - } - return null; - } - - /* convienence method for use from Guvnor*/ - public static String convertToDRL(PMML pmml, DrlType drlType) { - if (pmml != null) { - ScorecardCompiler scorecardCompiler = new ScorecardCompiler(drlType); - scorecardCompiler.setPMMLDocument(pmml); - return scorecardCompiler.getDRL(); - } - return null; - } - - public List getScorecardParseErrors() { - return scorecardErrors; - } - - private void closeStream(final InputStream stream) { - try { - if ( stream != null ) { - stream.close(); - } - } catch (final Exception e) { - logger.error(e.getMessage(), e); - } - } - - public static enum DrlType { - INTERNAL_DECLARED_TYPES, EXTERNAL_OBJECT_MODEL - } -} diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/ScorecardError.java b/drools-scorecards/src/main/java/org/drools/scorecards/ScorecardError.java deleted file mode 100644 index 803cee0e..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/ScorecardError.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.drools.scorecards; - -import java.io.Serializable; - - -public class ScorecardError implements Serializable { - private String errorLocation; - private String errorMessage; - - public ScorecardError(String errorLocation, String errorMessage) { - this.errorLocation = errorLocation; - this.errorMessage = errorMessage; - } - - public ScorecardError() { - } - - public String getErrorLocation() { - return errorLocation; - } - - public void setErrorLocation(String errorLocation) { - this.errorLocation = errorLocation; - } - - public String getErrorMessage() { - return errorMessage; - } - - public void setErrorMessage(String errorMessage) { - this.errorMessage = errorMessage; - } -} diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/ScorecardFormat.java b/drools-scorecards/src/main/java/org/drools/scorecards/ScorecardFormat.java deleted file mode 100644 index 76aef821..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/ScorecardFormat.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright 2012 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.drools.scorecards; - -public enum ScorecardFormat { - XLS, CSV -} diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/StringUtil.java b/drools-scorecards/src/main/java/org/drools/scorecards/StringUtil.java deleted file mode 100644 index bedf3aed..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/StringUtil.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright 2012 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.drools.scorecards; - -import java.util.HashMap; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.drools.core.util.StringUtils; - -public class StringUtil { - - - public static boolean isNumericWithOperators(CharSequence cs) { - if (cs == null) { - return false; - } - String allowedCharsInNumeric="<>=- "; - int sz = cs.length(); - for (int i = 0; i < sz; i++) { - char ch = cs.charAt(i); - if (!(Character.isDigit(ch) || allowedCharsInNumeric.indexOf(ch) > -1)) { - return false; - } - } - return true; - } - - public static String unescapeXML(final String xml) { - Pattern xmlEntityRegex = Pattern.compile("&(#?)([^;]+);"); - //Unfortunately, Matcher requires a StringBuffer instead of a StringBuilder - StringBuffer unescapedOutput = new StringBuffer(xml.length()); - - Matcher m = xmlEntityRegex.matcher(xml); - Map builtinEntities = null; - String entity; - String hashmark; - String ent; - int code; - while (m.find()) { - ent = m.group(2); - hashmark = m.group(1); - if ((hashmark != null) && (hashmark.length() > 0)) { - code = Integer.parseInt(ent); - entity = Character.toString((char) code); - } else { - //must be a non-numerical entity - if (builtinEntities == null) { - builtinEntities = buildBuiltinXMLEntityMap(); - } - entity = builtinEntities.get(ent); - if (entity == null) { - //not a known entity - ignore it - entity = "&" + ent + ';'; - } - } - m.appendReplacement(unescapedOutput, entity); - } - m.appendTail(unescapedOutput); - - return unescapedOutput.toString(); - } - - private static Map buildBuiltinXMLEntityMap() { - Map entities = new HashMap(10); - entities.put("lt", "<"); - entities.put("gt", ">"); - entities.put("amp", "&"); - entities.put("apos", "'"); - entities.put("quot", "\""); - return entities; - } - - public static int countMatches(String str, String sub) { - if (StringUtils.isEmpty(str)) { - return 0; - } - int count = 0; - int idx = 0; - while ((idx = str.indexOf(sub, idx)) != -1) { - count++; - idx += sub.length(); - } - return count; - } -} diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/drl/AbstractDRLEmitter.java b/drools-scorecards/src/main/java/org/drools/scorecards/drl/AbstractDRLEmitter.java deleted file mode 100644 index f3af6a4b..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/drl/AbstractDRLEmitter.java +++ /dev/null @@ -1,380 +0,0 @@ -/* - * Copyright 2012 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -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.drools.core.util.StringUtils; -import org.drools.scorecards.parser.xls.XLSKeywords; -import org.drools.scorecards.pmml.PMMLExtensionNames; -import org.drools.scorecards.pmml.PMMLOperators; -import org.drools.scorecards.pmml.ScorecardPMMLUtils; -import org.drools.template.model.*; -import org.drools.template.model.Package; - -public abstract class AbstractDRLEmitter { - - protected String formRuleName(PMML pmmlDocument, String modelName, Characteristic c, Attribute scoreAttribute) { - StringBuilder sb = new StringBuilder(); - sb.append(modelName).append("_").append(c.getName()).append("_"); - String dataType = ScorecardPMMLUtils.getDataType(pmmlDocument, ScorecardPMMLUtils.extractFieldNameFromCharacteristic(c)); - if (XLSKeywords.DATATYPE_NUMBER.equalsIgnoreCase(dataType)) { - if (scoreAttribute.getSimplePredicate() != null) { - sb.append(scoreAttribute.getSimplePredicate().getOperator()).append("_").append(scoreAttribute.getSimplePredicate().getValue()); - } else if (scoreAttribute.getCompoundPredicate() != null) { - sb.append("between"); - for (Object obj : scoreAttribute.getCompoundPredicate().getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates()) { - if (obj instanceof SimplePredicate) { - sb.append("_").append(((SimplePredicate) obj).getValue()); - } - } - } - } else if (XLSKeywords.DATATYPE_TEXT.equalsIgnoreCase(dataType) || XLSKeywords.DATATYPE_BOOLEAN.equalsIgnoreCase(dataType)) { - if (scoreAttribute.getSimplePredicate() != null) { - sb.append(scoreAttribute.getSimplePredicate().getOperator()).append("_").append(scoreAttribute.getSimplePredicate().getValue()); - } else if (scoreAttribute.getSimpleSetPredicate() != null) { - SimpleSetPredicate predicate = scoreAttribute.getSimpleSetPredicate(); - Array array = predicate.getArray(); - String text = array.getContent().replace(" ", "_"); - sb.append(predicate.getBooleanOperator()).append("_").append(text); - } - } - return sb.toString(); - } - - protected Characteristics getCharacteristicsFromScorecard(Scorecard scorecard) { - for (Object obj : scorecard.getExtensionsAndCharacteristicsAndMiningSchemas()) { - if (obj instanceof Characteristics) { - return (Characteristics) obj; - } - } - return null; - } - - protected void addGlobals(PMML pmml, org.drools.template.model.Package aPackage) { - - } - - public String emitDRL(PMML pmml) { - List ruleList = createRuleList(pmml); - String pkgName = ScorecardPMMLUtils.getExtensionValue(pmml.getHeader().getExtensions(), PMMLExtensionNames.SCORECARD_PACKAGE); - org.drools.template.model.Package aPackage = new org.drools.template.model.Package(pkgName); - - DRLOutput drlOutput = new DRLOutput(); - for (Rule rule : ruleList) { - aPackage.addRule(rule); - } - - addDeclaredTypes(pmml, aPackage); - - addImports(pmml, aPackage); - - addGlobals(pmml, aPackage); - - internalEmitDRL(pmml, ruleList, aPackage); - - aPackage.renderDRL(drlOutput); - String drl = drlOutput.getDRL(); - return drl; - } - - private void addImports(PMML pmml, Package aPackage) { - String importsFromDelimitedString = ScorecardPMMLUtils.getExtensionValue(pmml.getHeader().getExtensions(), PMMLExtensionNames.SCORECARD_IMPORTS); - for (String importStatement : importsFromDelimitedString.split(",")) { - Import imp = new Import(); - imp.setClassName(importStatement); - aPackage.addImport(imp); - } - Import defaultScorecardImport = new Import(); - defaultScorecardImport.setClassName("org.drools.scorecards.DroolsScorecard"); - aPackage.addImport(defaultScorecardImport); - defaultScorecardImport = new Import(); - defaultScorecardImport.setClassName("org.drools.scorecards.PartialScore"); - aPackage.addImport(defaultScorecardImport); - defaultScorecardImport = new Import(); - defaultScorecardImport.setClassName("org.drools.scorecards.InitialScore"); - aPackage.addImport(defaultScorecardImport); - defaultScorecardImport = new Import(); - defaultScorecardImport.setClassName("org.drools.scorecards.BaselineScore"); - aPackage.addImport(defaultScorecardImport); - } - - protected void addDeclaredTypes(PMML pmml, Package aPackage) { - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append("\ndeclare DroolsScorecard\nend\n\n"); - - for (Object obj : pmml.getAssociationModelsAndBaselineModelsAndClusteringModels()) { - if (obj instanceof Scorecard) { - Scorecard scorecard = (Scorecard) obj; - stringBuilder.append("declare ").append(scorecard.getModelName().replaceAll(" ","")).append(" extends DroolsScorecard\n"); - - addDeclaredTypeContents(pmml, stringBuilder, scorecard); - - stringBuilder.append("end\n"); - } - } - aPackage.addDeclaredType(stringBuilder.toString()); - } - - protected List createRuleList(PMML pmmlDocument) { - List ruleList = new ArrayList(); - for (Object obj : pmmlDocument.getAssociationModelsAndBaselineModelsAndClusteringModels()) { - if (obj instanceof Scorecard) { - Scorecard scorecard = (Scorecard) obj; - Characteristics characteristics = getCharacteristicsFromScorecard(scorecard); - createInitialRule(ruleList, scorecard); - for (org.dmg.pmml_4_1.Characteristic c : characteristics.getCharacteristics()) { - int attributePosition = 0; - for (org.dmg.pmml_4_1.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"); - if (desc != null) { - rule.setDescription(desc); - } - attributePosition++; - populateLHS(rule, pmmlDocument, scorecard, c, scoreAttribute); - populateRHS(rule, pmmlDocument, scorecard, c, scoreAttribute, attributePosition); - ruleList.add(rule); - } - } - createSummationRules(ruleList, scorecard); - } - } - return ruleList; - } - - protected void createInitialRule(List ruleList, Scorecard scorecard) { - if (scorecard.getInitialScore() > 0 || scorecard.isUseReasonCodes()){ - String objectClass = scorecard.getModelName().replaceAll(" ", ""); - 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()); - rule.addCondition(condition); - if (scorecard.getInitialScore() > 0 ) { - Consequence consequence = new Consequence(); - //consequence.setSnippet("$sc.setInitialScore(" + scorecard.getInitialScore() + ");"); - consequence.setSnippet("insertLogical(new InitialScore(\"" + objectClass+"\","+scorecard.getInitialScore() +"));"); - rule.addConsequence(consequence); - } - if (scorecard.isUseReasonCodes() ) { - for (Object obj :scorecard.getExtensionsAndCharacteristicsAndMiningSchemas()){ - if (obj instanceof Characteristics){ - Characteristics characteristics = (Characteristics)obj; - for (Characteristic characteristic : characteristics.getCharacteristics()){ - String field = ScorecardPMMLUtils.extractFieldNameFromCharacteristic(characteristic); - Consequence consequence = new Consequence(); - if (characteristic.getBaselineScore() == null || characteristic.getBaselineScore() == 0 ) { - consequence.setSnippet("insertLogical(new BaselineScore(\"" + objectClass+"\",\""+field + "\","+scorecard.getBaselineScore()+"));"); - //consequence.setSnippet("$sc.setBaselineScore(\"" + field + "\","+scorecard.getBaselineScore()+");"); - } else { - consequence.setSnippet("insertLogical(new BaselineScore(\"" + objectClass+"\",\""+field + "\","+characteristic.getBaselineScore()+"));"); - //consequence.setSnippet("$sc.setBaselineScore(\"" + field + "\","+characteristic.getBaselineScore()+");"); - } - rule.addConsequence(consequence); - } - } - } - if (scorecard.getReasonCodeAlgorithm() != null) { - Consequence consequence = new Consequence(); - if ("pointsAbove".equalsIgnoreCase(scorecard.getReasonCodeAlgorithm())) { - consequence.setSnippet("$sc.setReasonCodeAlgorithm(DroolsScorecard.REASON_CODE_ALGORITHM_POINTSABOVE);"); - } else if ("pointsBelow".equalsIgnoreCase(scorecard.getReasonCodeAlgorithm())) { - consequence.setSnippet("$sc.setReasonCodeAlgorithm(DroolsScorecard.REASON_CODE_ALGORITHM_POINTSBELOW);"); - } - rule.addConsequence(consequence); - } - } - ruleList.add(rule); - } - } - - protected void populateLHS(Rule rule, PMML pmmlDocument, Scorecard scorecard, Characteristic c, Attribute scoreAttribute) { - addLHSConditions(rule, pmmlDocument, scorecard, c, scoreAttribute); - } - - protected void createFieldRestriction(PMML pmmlDocument, Characteristic c, Attribute scoreAttribute, StringBuilder stringBuilder) { - stringBuilder.append("("); - //String dataType = ScorecardPMMLUtils.getExtensionValue(c.getExtensions(), PMMLExtensionNames.CHARACTERTISTIC_DATATYPE); - String dataType = ScorecardPMMLUtils.getDataType(pmmlDocument, ScorecardPMMLUtils.extractFieldNameFromCharacteristic(c)); - if (XLSKeywords.DATATYPE_TEXT.equalsIgnoreCase(dataType)) { - if (scoreAttribute.getSimplePredicate() != null) { - SimplePredicate predicate = scoreAttribute.getSimplePredicate(); - String operator = predicate.getOperator(); - if (PMMLOperators.EQUAL.equalsIgnoreCase(operator)) { - stringBuilder.append(predicate.getField()); - stringBuilder.append(" == "); - stringBuilder.append("\"").append(predicate.getValue()).append("\""); - } else if (PMMLOperators.NOT_EQUAL.equalsIgnoreCase(operator)) { - stringBuilder.append(predicate.getField()); - stringBuilder.append(" != "); - stringBuilder.append("\"").append(predicate.getValue()).append("\""); - } - } else if (scoreAttribute.getSimpleSetPredicate() != null) { - SimpleSetPredicate simpleSetPredicate = scoreAttribute.getSimpleSetPredicate(); - String content = simpleSetPredicate.getArray().getContent(); - content = content.replaceAll(" ", "\",\""); - - stringBuilder.append(simpleSetPredicate.getField()).append(" in ( \"").append(content).append("\" )"); - } - } else if (XLSKeywords.DATATYPE_BOOLEAN.equalsIgnoreCase(dataType)) { - if (scoreAttribute.getSimplePredicate() != null) { - SimplePredicate predicate = scoreAttribute.getSimplePredicate(); - String operator = predicate.getOperator(); - if (PMMLOperators.EQUAL.equalsIgnoreCase(operator)) { - stringBuilder.append(predicate.getField()); - stringBuilder.append(" == "); - stringBuilder.append(predicate.getValue().toLowerCase()); - } else if (PMMLOperators.NOT_EQUAL.equalsIgnoreCase(operator)) { - stringBuilder.append(predicate.getField()); - stringBuilder.append(" != "); - stringBuilder.append(predicate.getValue().toLowerCase()); - } - } - } else if (XLSKeywords.DATATYPE_NUMBER.equalsIgnoreCase(dataType)) { - if (scoreAttribute.getSimplePredicate() != null) { - SimplePredicate predicate = scoreAttribute.getSimplePredicate(); - String operator = predicate.getOperator(); - stringBuilder.append(predicate.getField()); - if (PMMLOperators.LESS_THAN.equalsIgnoreCase(operator)) { - stringBuilder.append(" < "); - } else if (PMMLOperators.GREATER_THAN.equalsIgnoreCase(operator)) { - stringBuilder.append(" > "); - } else if (PMMLOperators.NOT_EQUAL.equalsIgnoreCase(operator)) { - stringBuilder.append(" <> "); - } else if (PMMLOperators.EQUAL.equalsIgnoreCase(operator)) { - stringBuilder.append(" = "); - } else if (PMMLOperators.GREATER_OR_EQUAL.equalsIgnoreCase(operator)) { - stringBuilder.append(" >= "); - } else if (PMMLOperators.LESS_OR_EQUAL.equalsIgnoreCase(operator)) { - stringBuilder.append(" <= "); - } - stringBuilder.append(predicate.getValue()); - } else if (scoreAttribute.getCompoundPredicate() != null) { - CompoundPredicate predicate = scoreAttribute.getCompoundPredicate(); - String field = null; - for (Object obj : predicate.getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates()) { - if (obj instanceof SimplePredicate) { - SimplePredicate simplePredicate = (SimplePredicate) obj; - String operator = simplePredicate.getOperator(); - if (field == null) { - stringBuilder.append(simplePredicate.getField()); - field = simplePredicate.getField(); - } else { - stringBuilder.append(" && "); - } - if (PMMLOperators.LESS_THAN.equalsIgnoreCase(operator)) { - stringBuilder.append(" < "); - } else if (PMMLOperators.GREATER_THAN.equalsIgnoreCase(operator)) { - stringBuilder.append(" > "); - } else if (PMMLOperators.NOT_EQUAL.equalsIgnoreCase(operator)) { - stringBuilder.append(" <> "); - } else if (PMMLOperators.EQUAL.equalsIgnoreCase(operator)) { - stringBuilder.append(" = "); - } else if (PMMLOperators.GREATER_OR_EQUAL.equalsIgnoreCase(operator)) { - stringBuilder.append(" >= "); - } else if (PMMLOperators.LESS_OR_EQUAL.equalsIgnoreCase(operator)) { - stringBuilder.append(" <= "); - } - stringBuilder.append(simplePredicate.getValue()); - } - } - } - } - stringBuilder.append(")"); - } - - protected void populateRHS(Rule rule, PMML pmmlDocument, Scorecard scorecard, Characteristic c, Attribute scoreAttribute, int position) { - Consequence consequence = new Consequence(); - StringBuilder stringBuilder = new StringBuilder(); - String objectClass = scorecard.getModelName().replaceAll(" ", ""); - - String setter = "insertLogical(new PartialScore(\""; - String field = ScorecardPMMLUtils.extractFieldNameFromCharacteristic(c); - - stringBuilder.append(setter).append(objectClass).append("\",\"").append(field).append("\",").append(scoreAttribute.getPartialScore()); - if (scorecard.isUseReasonCodes()){ - String reasonCode = scoreAttribute.getReasonCode(); - if (reasonCode == null || StringUtils.isEmpty(reasonCode)) { - reasonCode = c.getReasonCode(); - } - stringBuilder.append(",\"").append(reasonCode).append("\", ").append(position); - } - stringBuilder.append("));"); - consequence.setSnippet(stringBuilder.toString()); - rule.addConsequence(consequence); - } - - protected void createSummationRules(List ruleList, Scorecard scorecard) { - String objectClass = scorecard.getModelName().replaceAll(" ", ""); - Rule calcTotalRule = new Rule(objectClass+"_calculateTotalScore",1,1); - StringBuilder stringBuilder = new StringBuilder(); - Condition condition = new Condition(); - stringBuilder.append("$calculatedScore : Double() from accumulate (PartialScore(scorecardName ==\"").append(objectClass).append("\", $partialScore:score), sum($partialScore))"); - condition.setSnippet(stringBuilder.toString()); - calcTotalRule.addCondition(condition); - if (scorecard.getInitialScore() > 0) { - condition = new Condition(); - stringBuilder = new StringBuilder(); - stringBuilder.append("InitialScore(scorecardName == \"").append(objectClass).append("\", $initialScore:score)"); - condition.setSnippet(stringBuilder.toString()); - calcTotalRule.addCondition(condition); - } - ruleList.add(calcTotalRule); - - if (scorecard.isUseReasonCodes()) { - String ruleName = objectClass+"_collectReasonCodes"; - Rule rule = new Rule(ruleName, 1, 1); - rule.setDescription("collect and sort the reason codes as per the specified algorithm"); - condition = new Condition(); - stringBuilder = new StringBuilder(); - stringBuilder.append("$reasons : List() from accumulate ( PartialScore(scorecardName == \"").append(objectClass).append("\", $reasonCode : reasoncode ); collectList($reasonCode) )"); - condition.setSnippet(stringBuilder.toString()); - rule.addCondition(condition); - ruleList.add(rule); - - addAdditionalReasonCodeCondition(rule, scorecard); - addAdditionalReasonCodeConsequence(rule, scorecard); - } - - addAdditionalSummationCondition(calcTotalRule, scorecard); - addAdditionalSummationConsequence(calcTotalRule, scorecard); - } - - protected abstract void addDeclaredTypeContents(PMML pmmlDocument, StringBuilder stringBuilder, Scorecard scorecard); - protected abstract void internalEmitDRL(PMML pmml, List ruleList, Package aPackage); - protected abstract void addLHSConditions(Rule rule, PMML pmmlDocument, Scorecard scorecard, Characteristic c, Attribute scoreAttribute); - protected abstract void addAdditionalReasonCodeConsequence(Rule rule, 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); -} diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/drl/DeclaredTypesDRLEmitter.java b/drools-scorecards/src/main/java/org/drools/scorecards/drl/DeclaredTypesDRLEmitter.java deleted file mode 100644 index fc76dafd..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/drl/DeclaredTypesDRLEmitter.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright 2012 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -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.drools.scorecards.parser.xls.XLSKeywords; -import org.drools.scorecards.pmml.ScorecardPMMLUtils; -import org.drools.template.model.Condition; -import org.drools.template.model.Consequence; -import org.drools.template.model.Package; -import org.drools.template.model.Rule; - -public class DeclaredTypesDRLEmitter extends AbstractDRLEmitter{ - - protected void addDeclaredTypeContents(PMML pmmlDocument, StringBuilder stringBuilder, Scorecard scorecard) { - Characteristics characteristics = getCharacteristicsFromScorecard(scorecard); - for (org.dmg.pmml_4_1.Characteristic c : characteristics.getCharacteristics()) { - String field = ScorecardPMMLUtils.extractFieldNameFromCharacteristic(c); - String dataType = ScorecardPMMLUtils.getDataType(pmmlDocument, field); - //String dataType = ScorecardPMMLUtils.getExtensionValue(c.getExtensions(), PMMLExtensionNames.CHARACTERTISTIC_DATATYPE); - if (XLSKeywords.DATATYPE_TEXT.equalsIgnoreCase(dataType)) { - dataType = "String"; - } else if (XLSKeywords.DATATYPE_NUMBER.equalsIgnoreCase(dataType)) { - dataType = "int"; - } else if (XLSKeywords.DATATYPE_BOOLEAN.equalsIgnoreCase(dataType)) { - dataType = "boolean"; - } - stringBuilder.append("\t").append(field).append(" : ").append(dataType).append("\n"); - } - } - - @Override - protected void internalEmitDRL(PMML pmml, List ruleList, Package aPackage) { - //ignore - } - - @Override - protected void addLHSConditions(Rule rule, PMML pmmlDocument, Scorecard scorecard, Characteristic c, Attribute scoreAttribute) { - Condition condition = new Condition(); - StringBuilder stringBuilder = new StringBuilder(); - String var = "$sc"; - - String objectClass = scorecard.getModelName().replaceAll(" ", ""); - stringBuilder.append(var).append(" : ").append(objectClass); - - createFieldRestriction(pmmlDocument, c, scoreAttribute, stringBuilder); - - condition.setSnippet(stringBuilder.toString()); - rule.addCondition(condition); - } - - @Override - protected void addAdditionalReasonCodeConsequence(Rule rule, Scorecard scorecard) { - Consequence consequence = new Consequence(); - consequence.setSnippet("$sc.setReasonCodes($reasons);"); - rule.addConsequence(consequence); - consequence = new Consequence(); - consequence.setSnippet("$sc.sortReasonCodes();"); - rule.addConsequence(consequence); - } - - @Override - protected void addAdditionalReasonCodeCondition(Rule rule, Scorecard scorecard) { - createEmptyScorecardCondition(rule, scorecard); - } - - @Override - protected void addAdditionalSummationCondition(Rule calcTotalRule, Scorecard scorecard) { - createEmptyScorecardCondition(calcTotalRule, scorecard); - } - - @Override - protected void addAdditionalSummationConsequence(Rule calcTotalRule, Scorecard scorecard) { - - Consequence consequence = new Consequence(); - if (scorecard.getInitialScore() > 0) { - consequence.setSnippet("$sc.setCalculatedScore(($calculatedScore+$initialScore));"); - } else { - consequence.setSnippet("$sc.setCalculatedScore($calculatedScore);"); - } - calcTotalRule.addConsequence(consequence); - - } - - protected void createEmptyScorecardCondition(Rule rule, Scorecard scorecard) { - String objectClass = scorecard.getModelName().replaceAll(" ", ""); - StringBuilder stringBuilder = new StringBuilder(); - String var = "$sc"; - - stringBuilder.append(var).append(" : ").append(objectClass).append("()"); - - Condition condition = new Condition(); - condition.setSnippet(stringBuilder.toString()); - rule.addCondition(condition); - } -} diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/drl/ExternalModelDRLEmitter.java b/drools-scorecards/src/main/java/org/drools/scorecards/drl/ExternalModelDRLEmitter.java deleted file mode 100644 index 1b9e8b63..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/drl/ExternalModelDRLEmitter.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Copyright 2012 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -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.drools.scorecards.pmml.PMMLExtensionNames; -import org.drools.scorecards.pmml.ScorecardPMMLUtils; -import org.drools.template.model.Condition; -import org.drools.template.model.Consequence; -import org.drools.template.model.Package; -import org.drools.template.model.Rule; - -public class ExternalModelDRLEmitter extends AbstractDRLEmitter { - - @Override - protected void addDeclaredTypeContents(PMML pmmlDocument, StringBuilder stringBuilder, Scorecard scorecard) { - //empty by design - } - - @Override - protected void internalEmitDRL(PMML pmml, List ruleList, Package aPackage) { - //do nothing for now. - } - - @Override - protected void addLHSConditions(Rule rule, PMML pmmlDocument, Scorecard scorecard, Characteristic c, Attribute scoreAttribute) { - Extension extension = null; - for (Object obj : scorecard.getExtensionsAndCharacteristicsAndMiningSchemas()){ - if ( obj instanceof MiningSchema ) { - MiningSchema miningSchema = (MiningSchema)obj; - String fieldName = ScorecardPMMLUtils.extractFieldNameFromCharacteristic(c); - for (MiningField miningField : miningSchema.getMiningFields() ){ - if ( miningField.getName().equalsIgnoreCase(fieldName)) { - if (miningField.getExtensions().size() > 0 ) { - extension = miningField.getExtensions().get(0); - } - } - } - } - } - //Extension extension = ScorecardPMMLUtils.getExtension(c.getExtensions(), PMMLExtensionNames.CHARACTERTISTIC_EXTERNAL_CLASS); - if ( extension != null ) { - Condition condition = new Condition(); - StringBuilder stringBuilder = new StringBuilder("$"); - stringBuilder.append(c.getName()).append(" : ").append(extension.getValue()); - createFieldRestriction(pmmlDocument, c, scoreAttribute, stringBuilder); - condition.setSnippet(stringBuilder.toString()); - rule.addCondition(condition); - } - } - - @Override - protected void addAdditionalReasonCodeConsequence(Rule rule, Scorecard scorecard) { - - } - - @Override - protected void addAdditionalReasonCodeCondition(Rule rule, Scorecard scorecard) { - - } - - @Override - protected void addAdditionalSummationConsequence(Rule calcTotalRule, Scorecard scorecard) { - String externalClassName = null; - String fieldName = null; - for (Object obj :scorecard.getExtensionsAndCharacteristicsAndMiningSchemas()){ - if ( obj instanceof Output) { - Output output = (Output)obj; - final List outputFields = output.getOutputFields(); - final OutputField outputField = outputFields.get(0); - fieldName = outputField.getName(); - externalClassName = ScorecardPMMLUtils.getExtension(outputField.getExtensions(), PMMLExtensionNames.SCORECARD_RESULTANT_SCORE_CLASS).getValue(); - break; - } - } - if ( fieldName != null && externalClassName != null) { - 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);"); - consequence.setSnippet(stringBuilder.toString()); - calcTotalRule.addConsequence(consequence); - } - - } - - @Override - protected void addAdditionalSummationCondition(Rule calcTotalRule, Scorecard scorecard) { - String externalClassName = null; - String fieldName = null; - for (Object obj :scorecard.getExtensionsAndCharacteristicsAndMiningSchemas()){ - if ( obj instanceof Output) { - Output output = (Output)obj; - final List outputFields = output.getOutputFields(); - final OutputField outputField = outputFields.get(0); - fieldName = outputField.getName(); - externalClassName = ScorecardPMMLUtils.getExtension(outputField.getExtensions(), PMMLExtensionNames.SCORECARD_RESULTANT_SCORE_CLASS).getValue(); - break; - } - } - if ( fieldName != null && externalClassName != null) { - Condition condition = new Condition(); - StringBuilder stringBuilder = new StringBuilder("$"); - stringBuilder.append(fieldName).append("Var : ").append(externalClassName).append("()"); - condition.setSnippet(stringBuilder.toString()); - calcTotalRule.addCondition(condition); - } - - } -} diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/parser/AbstractScorecardParser.java b/drools-scorecards/src/main/java/org/drools/scorecards/parser/AbstractScorecardParser.java deleted file mode 100644 index ae2857c4..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/parser/AbstractScorecardParser.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2012 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.drools.scorecards.parser; - -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 parseFile(InputStream inStream, String worksheetName) throws ScorecardParseException; - - public abstract PMML getPMMLDocument(); -} diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/parser/ScorecardParseException.java b/drools-scorecards/src/main/java/org/drools/scorecards/parser/ScorecardParseException.java deleted file mode 100644 index 8b46929c..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/parser/ScorecardParseException.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2012 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.drools.scorecards.parser; - -public class ScorecardParseException extends Exception { - - public ScorecardParseException() { - } - - public ScorecardParseException(String message) { - super(message); - } - - public ScorecardParseException(String message, Throwable cause) { - super(message, cause); - } - - public ScorecardParseException(Throwable cause) { - super(cause); - } -} diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/ExcelScorecardValidator.java b/drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/ExcelScorecardValidator.java deleted file mode 100644 index e2676126..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/ExcelScorecardValidator.java +++ /dev/null @@ -1,141 +0,0 @@ -/* -* Copyright 2012 JBoss Inc -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -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.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; - -class ExcelScorecardValidator { - - private Scorecard scorecard; - private List parseErrors; - - private ExcelScorecardValidator(Scorecard scorecard, List parseErrors) { - //to ensure this used as a pure Util class only. - this.scorecard = scorecard; - this.parseErrors = parseErrors; - } - - public static void runAdditionalValidations(Scorecard scorecard, List parseErrors) { - ExcelScorecardValidator validator = new ExcelScorecardValidator(scorecard, parseErrors); - validator.checkForInvalidDataTypes(); - validator.checkForMissingAttributes(); - if (scorecard.isUseReasonCodes()){ - validator.validateReasonCodes(); - validator.validateBaselineScores(); - } - } - - private void validateReasonCodes() { - for (Object obj :scorecard.getExtensionsAndCharacteristicsAndMiningSchemas()){ - if (obj instanceof Characteristics){ - Characteristics characteristics = (Characteristics)obj; - for (Characteristic characteristic : characteristics.getCharacteristics()){ - String charReasonCode = characteristic.getReasonCode(); - if (charReasonCode == null || StringUtils.isEmpty(charReasonCode)){ - for (Attribute attribute : characteristic.getAttributes()){ - String newCellRef = createDataTypeCellRef(ScorecardPMMLUtils.getExtensionValue(attribute.getExtensions(), "cellRef"),3); - String attrReasonCode = attribute.getReasonCode(); - if ( attrReasonCode == null || StringUtils.isEmpty(attrReasonCode)){ - parseErrors.add(new ScorecardError(newCellRef, "Attribute is missing Reason Code")); - } - } - } - } - } - } - } - - private void validateBaselineScores() { - for (Object obj :scorecard.getExtensionsAndCharacteristicsAndMiningSchemas()){ - Double scorecardBaseline = scorecard.getBaselineScore(); - if (obj instanceof Characteristics){ - Characteristics characteristics = (Characteristics)obj; - for (Characteristic characteristic : characteristics.getCharacteristics()){ - Double charBaseline = characteristic.getBaselineScore(); - if ( (charBaseline == null || charBaseline.doubleValue() == 0) - && ((scorecardBaseline == null || scorecardBaseline.doubleValue() == 0)) ){ - String newCellRef = createDataTypeCellRef(ScorecardPMMLUtils.getExtensionValue(characteristic.getExtensions(), "cellRef"),2); - parseErrors.add(new ScorecardError(newCellRef, "Characteristic is missing Baseline Score")); - } - } - } - } - } - - private void checkForInvalidDataTypes() { - for (Object obj :scorecard.getExtensionsAndCharacteristicsAndMiningSchemas()){ - if (obj instanceof Characteristics){ - Characteristics characteristics = (Characteristics)obj; - for (Characteristic characteristic : characteristics.getCharacteristics()){ - String dataType = ScorecardPMMLUtils.getExtensionValue(characteristic.getExtensions(), PMMLExtensionNames.CHARACTERTISTIC_DATATYPE); - String newCellRef = createDataTypeCellRef(ScorecardPMMLUtils.getExtensionValue(characteristic.getExtensions(), "cellRef"),1); - if ( dataType == null || StringUtils.isEmpty(dataType)) { - parseErrors.add(new ScorecardError(newCellRef, "Missing Data Type!")); - } else if ( !XLSKeywords.DATATYPE_TEXT.equalsIgnoreCase(dataType) && !XLSKeywords.DATATYPE_NUMBER.equalsIgnoreCase(dataType) && !XLSKeywords.DATATYPE_BOOLEAN.equalsIgnoreCase(dataType)){ - parseErrors.add(new ScorecardError(newCellRef, "Invalid Data Type!")); - } - - if (XLSKeywords.DATATYPE_BOOLEAN.equalsIgnoreCase(dataType)){ - for (Attribute attribute : characteristic.getAttributes()){ - String value = ScorecardPMMLUtils.getExtensionValue(attribute.getExtensions(), "predicateResolver"); - if (!"TRUE".equalsIgnoreCase(value) && !"FALSE".equalsIgnoreCase(value)){ - parseErrors.add(new ScorecardError(newCellRef, "Characteristic '"+characteristic.getName()+"' is Boolean and can support TRUE|FALSE only")); - break; - } - } - } else if (XLSKeywords.DATATYPE_NUMBER.equalsIgnoreCase(dataType)){ - for (Attribute attribute : characteristic.getAttributes()){ - String value = ScorecardPMMLUtils.getExtensionValue(attribute.getExtensions(), "predicateResolver"); - if (!StringUtil.isNumericWithOperators(value)){ - parseErrors.add(new ScorecardError(newCellRef, "Characteristic '"+characteristic.getName()+"' is Number and can support numerics only")); - } - } - } - } - } - } - } - - private void checkForMissingAttributes() { - for (Object obj :scorecard.getExtensionsAndCharacteristicsAndMiningSchemas()){ - if (obj instanceof Characteristics){ - Characteristics characteristics = (Characteristics)obj; - for (Characteristic characteristic : characteristics.getCharacteristics()){ - String newCellRef = ScorecardPMMLUtils.getExtensionValue(characteristic.getExtensions(), "cellRef"); - if ( characteristic.getAttributes().size() == 0 ) { - parseErrors.add(new ScorecardError(newCellRef, "Missing Attribute Bins for Characteristic '"+characteristic.getName()+"'.")); - } - } - } - } - } - - private String createDataTypeCellRef(String cellRef, int n) { - int col = ((int)(cellRef.charAt(1)))+n; - return "$"+((char)col)+cellRef.substring(cellRef.indexOf('$',1)); - } -} diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/MergedCellRange.java b/drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/MergedCellRange.java deleted file mode 100644 index 82e9711b..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/MergedCellRange.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2012 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.drools.scorecards.parser.xls; - -class MergedCellRange { - - private int firstRow; - private int firstCol; - private int lastRow; - private int lastCol; - - protected MergedCellRange(int firstRow, int firstCol, int lastRow, int lastCol) { - this.firstRow = firstRow; - this.lastRow = lastRow; - this.firstCol = firstCol; - this.lastCol = lastCol; - } - - public int getFirstRow() { - return firstRow; - } - - public void setFirstRow(int firstRow) { - this.firstRow = firstRow; - } - - public int getFirstCol() { - return firstCol; - } - - public void setFirstCol(int firstCol) { - this.firstCol = firstCol; - } - - public int getLastRow() { - return lastRow; - } - - public void setLastRow(int lastRow) { - this.lastRow = lastRow; - } - - public int getLastCol() { - return lastCol; - } - - public void setLastCol(int lastCol) { - this.lastCol = lastCol; - } -} diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/XLSEventDataCollector.java b/drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/XLSEventDataCollector.java deleted file mode 100644 index e15f82a7..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/XLSEventDataCollector.java +++ /dev/null @@ -1,352 +0,0 @@ -/* - * Copyright 2012 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -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.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; - -class XLSEventDataCollector { - - private List expectations = new ArrayList(); - private List cellRangeList; - private Scorecard scorecard; - private Characteristics characteristics; - private Characteristic _characteristic; //stateMachine variables - private Output output; - private List parseErrors; - private MiningSchema miningSchema; - private XLSScorecardParser xlsScorecardParser; - - public XLSEventDataCollector() { - parseErrors = new ArrayList(); - } - - public Scorecard getScorecard() { - return scorecard; - } - - private void fulfillExpectation(int currentRowCtr, int currentColCtr, Object cellValue, Class expectedClass) throws ScorecardParseException { - List dataExpectations = resolveExpectations(currentRowCtr, currentColCtr); - CellReference cellRef = new CellReference(currentRowCtr, currentColCtr); - for (DataExpectation dataExpectation : dataExpectations) { - try { - if (dataExpectation != null && dataExpectation.object != null) { - if ( cellValue == null || StringUtils.isEmpty(cellValue.toString())){ - if ( dataExpectation.errorMessage != null && !StringUtils.isEmpty(dataExpectation.errorMessage)) { - parseErrors.add(new ScorecardError(cellRef.formatAsString(), dataExpectation.errorMessage)); - return; - } - } - String setter = "set" + Character.toUpperCase(dataExpectation.property.charAt(0)) + dataExpectation.property.substring(1); - Method method = getSuitableMethod(cellValue, expectedClass, dataExpectation, setter); - if ( method == null ) { - if (cellValue != null && !StringUtils.isEmpty(cellValue.toString())) { - parseErrors.add(new ScorecardError(cellRef.formatAsString(), "Unexpected Value! Wrong Datatype?")); - } - return; - } - if (method.getParameterTypes()[0] == Double.class) { - cellValue = new Double(Double.parseDouble(cellValue.toString())); - } - if (method.getParameterTypes()[0] == Boolean.class) { - cellValue = Boolean.valueOf(cellValue.toString()); - } - method.invoke(dataExpectation.object, cellValue); - if (dataExpectation.object instanceof Extension && ("cellRef".equals(((Extension) dataExpectation.object).getName()))) { - ((Extension) dataExpectation.object).setValue(cellRef.formatAsString()); - } - //dataExpectations.remove(dataExpectation); - } - } catch (Exception e) { - throw new ScorecardParseException(e); - } - } - } - - private Method getSuitableMethod(Object cellValue, Class expectedClass, DataExpectation dataExpectation, String setter) { - Method method; - try { - method = dataExpectation.object.getClass().getMethod(setter, expectedClass); - return method; - } catch (NoSuchMethodException e) { - if ( expectedClass == int.class) { - try{ - method = dataExpectation.object.getClass().getMethod(setter, Double.class); - return method; - }catch (NoSuchMethodException e1) { - //stay silent - } - } - if ( expectedClass != String.class) { - try { - method = dataExpectation.object.getClass().getMethod(setter, String.class); - return method; - } catch (NoSuchMethodException e1) { - return null; - } - } - if ("TRUE".equalsIgnoreCase(cellValue.toString()) || "FALSE".equalsIgnoreCase(cellValue.toString())){ - try { - method = dataExpectation.object.getClass().getMethod(setter, Boolean.class); - return method; - } catch (NoSuchMethodException e1) { - return null; - } - } - } - return null; - } - - private void setAdditionalExpectation(int currentRowCtr, int currentColCtr, String stringCellValue) { - if (XLSKeywords.SCORECARD_NAME.equalsIgnoreCase(stringCellValue)) { - addExpectation(currentRowCtr, currentColCtr + 1, "modelName", scorecard, "Model Name is missing!"); - - } else if (XLSKeywords.SCORECARD_REASONCODE_ALGORITHM.equalsIgnoreCase(stringCellValue)) { - addExpectation(currentRowCtr, currentColCtr + 1, "reasonCodeAlgorithm", scorecard, null); - } else if (XLSKeywords.SCORECARD_USE_REASONCODES.equalsIgnoreCase(stringCellValue)) { - addExpectation(currentRowCtr, currentColCtr + 1, "useReasonCodes", scorecard, null); - - } else if (XLSKeywords.SCORECARD_RESULTANT_SCORE_CLASS.equalsIgnoreCase(stringCellValue)) { - Extension extension = new Extension(); - extension.setName(PMMLExtensionNames.SCORECARD_RESULTANT_SCORE_CLASS); - scorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(extension); - addExpectation(currentRowCtr, currentColCtr + 1, "value", extension, null); - - } else if (XLSKeywords.SCORECARD_RESULTANT_SCORE_FIELD.equalsIgnoreCase(stringCellValue)) { - Extension extension = new Extension(); - extension.setName(PMMLExtensionNames.SCORECARD_RESULTANT_SCORE_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); - -// } else if (XLSKeywords.SCORECARD_SCORE_VAR.equalsIgnoreCase(stringCellValue)) { -// OutputField outputField = new OutputField(); -// outputField.setDataType(DATATYPE.DOUBLE); -// outputField.setDisplayName("Final Score"); -// output.getOutputFields().add(outputField); -// outputField.setFeature(RESULTFEATURE.PREDICTED_VALUE); -// addExpectation(currentRowCtr, currentColCtr + 1, "name", outputField, "Final Score Variable is missing!"); - - } else if (XLSKeywords.SCORECARD_IMPORTS.equalsIgnoreCase(stringCellValue)) { - Extension extension = new Extension(); - extension.setName(PMMLExtensionNames.SCORECARD_IMPORTS); - scorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(extension); - addExpectation(currentRowCtr, currentColCtr + 1, "value", extension, null); - - } else if (XLSKeywords.SCORECARD_PACKAGE.equalsIgnoreCase(stringCellValue)) { - Extension extension = new Extension(); - extension.setName(PMMLExtensionNames.SCORECARD_PACKAGE); - scorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(extension); - addExpectation(currentRowCtr, currentColCtr + 1, "value", extension, "Scorecard Package is missing"); - - } else if (XLSKeywords.SCORECARD_CHARACTERISTIC_NAME.equalsIgnoreCase(stringCellValue)) { - _characteristic = new Characteristic(); - characteristics.getCharacteristics().add(_characteristic); - addExpectation(currentRowCtr + 1, currentColCtr, "name", _characteristic, "Characteristic (Property) Display Name is missing."); - - Extension extension = new Extension(); - extension.setName(PMMLExtensionNames.SCORECARD_CELL_REF); - addExpectation(currentRowCtr + 1, currentColCtr, "value", extension, null); - _characteristic.getExtensions().add(extension); - - } else if (XLSKeywords.SCORECARD_CHARACTERISTIC_EXTERNAL_CLASS.equalsIgnoreCase(stringCellValue)) { - Extension extension = new Extension(); - extension.setName(PMMLExtensionNames.CHARACTERTISTIC_EXTERNAL_CLASS); - addExpectation(currentRowCtr + 1, currentColCtr, "value", extension, null); - _characteristic.getExtensions().add(extension); - - } else if (XLSKeywords.SCORECARD_CHARACTERISTIC_DATATYPE.equalsIgnoreCase(stringCellValue)) { - Extension extension = new Extension(); - extension.setName(PMMLExtensionNames.CHARACTERTISTIC_DATATYPE); - _characteristic.getExtensions().add(extension); - addExpectation(currentRowCtr + 1, currentColCtr, "value", extension, "Characteristic (Property) Data Type is missing."); - - } else if (XLSKeywords.SCORECARD_CHARACTERISTIC_BASELINE_SCORE.equalsIgnoreCase(stringCellValue)) { - String value = xlsScorecardParser.peekValueAt(currentRowCtr, currentColCtr-2); - if ("Name".equalsIgnoreCase(value)){ - addExpectation(currentRowCtr + 1, currentColCtr, "baselineScore", _characteristic, null); - } else { - addExpectation(currentRowCtr, currentColCtr+1, "baselineScore", scorecard, null); - } - } else if (XLSKeywords.SCORECARD_REASONCODE.equalsIgnoreCase(stringCellValue)) { - String value = xlsScorecardParser.peekValueAt(currentRowCtr, currentColCtr-4); - if ("Name".equalsIgnoreCase(value)){ - //only for characteristics... - addExpectation(currentRowCtr + 1, currentColCtr, "reasonCode", _characteristic, null); - } - - } else if (XLSKeywords.SCORECARD_CHARACTERISTIC_BIN_ATTRIBUTE.equalsIgnoreCase(stringCellValue)) { - MergedCellRange cellRange = getMergedRegionForCell(currentRowCtr + 1, currentColCtr); - if (cellRange != null) { - for (int r = cellRange.getFirstRow(); r <= cellRange.getLastRow(); r++) { - Attribute attribute = new Attribute(); - _characteristic.getAttributes().add(attribute); - addExpectation(r, currentColCtr + 2, "partialScore", attribute, "Characteristic (Property) Partial Score is missing."); - - Extension extension = new Extension(); - extension.setName("description"); - attribute.getExtensions().add(extension); - addExpectation(r, currentColCtr + 3, "value", extension, null); - - extension = new Extension(); - extension.setName(PMMLExtensionNames.CHARACTERTISTIC_FIELD); - attribute.getExtensions().add(extension); - addExpectation(currentRowCtr + 1, currentColCtr, "value", extension, "Characteristic (Property) Name is missing."); - - extension = new Extension(); - extension.setName("predicateResolver"); - attribute.getExtensions().add(extension); - addExpectation(r, currentColCtr + 1, "value", extension, "Characteristic (Property) Value is missing."); - - extension = new Extension(); - extension.setName("cellRef"); - addExpectation(r, currentColCtr + 1, "value", extension, null); - attribute.getExtensions().add(extension); - addExpectation(r, currentColCtr+4, "reasonCode", attribute,null); - } - MiningField miningField = new MiningField(); - miningField.setInvalidValueTreatment(INVALIDVALUETREATMENTMETHOD.AS_MISSING); - miningField.setUsageType(FIELDUSAGETYPE.ACTIVE); - miningSchema.getMiningFields().add(miningField); - addExpectation(currentRowCtr + 1, currentColCtr, "name", miningField, null); - } - - } else if (XLSKeywords.SCORECARD_CHARACTERISTIC_BIN_INITIALSCORE.equalsIgnoreCase(stringCellValue)) { - } else if (XLSKeywords.SCORECARD_CHARACTERISTIC_BIN_LABEL.equalsIgnoreCase(stringCellValue)) { - } else if (XLSKeywords.SCORECARD_CHARACTERISTIC_BIN_DESC.equalsIgnoreCase(stringCellValue)) { - } - } - - private void addExpectation(int row, int column, String property, Object ref, String errorMessage) { - expectations.add(new DataExpectation(row, column, ref, property, errorMessage)); - } - - private List resolveExpectations(int row, int col) { - List dataExpectations = new ArrayList(); - for (DataExpectation dataExpectation : expectations) { - if (dataExpectation.row == row && dataExpectation.col == col) { - dataExpectations.add(dataExpectation); - } - } - return dataExpectations; - } - - public void newCell(int currentRowCtr, int currentColCtr, String stringCellValue) throws ScorecardParseException { - setAdditionalExpectation(currentRowCtr, currentColCtr, stringCellValue); - fulfillExpectation(currentRowCtr, currentColCtr, stringCellValue, String.class); - } - - public void newCell(int currentRowCtr, int currentColCtr, double numericCellValue) throws ScorecardParseException { - fulfillExpectation(currentRowCtr, currentColCtr, numericCellValue, Double.class); - } - - public void newCell(int currentRowCtr, int currentColCtr, boolean booleanCellValue) throws ScorecardParseException { - fulfillExpectation(currentRowCtr, currentColCtr, booleanCellValue, boolean.class); - } - - public void newCell(int currentRowCtr, int currentColCtr, Date dateCellValue) throws ScorecardParseException { - fulfillExpectation(currentRowCtr, currentColCtr, dateCellValue, Date.class); - } - - public void sheetComplete() { - //verify the data - ExcelScorecardValidator.runAdditionalValidations(scorecard, parseErrors); - } - - @SuppressWarnings("unused") - public void newRow(int rowNum) { - - } - - public void sheetStart(String worksheetName) { - //this.worksheetName = worksheetName; - expectations.clear(); - cellRangeList = null; - _characteristic = null; - - scorecard = ScorecardPMMLUtils.createScorecard(); - - output = new Output(); - characteristics = new Characteristics(); - miningSchema = new MiningSchema(); - - scorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(miningSchema); - scorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(output); - scorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(characteristics); - - } - - public void setMergedRegionsInSheet(List cellRangeList) { - this.cellRangeList = cellRangeList; - } - - private MergedCellRange getMergedRegionForCell(int rowInd, int colInd) { - for (MergedCellRange cellRange : cellRangeList) { - if ((cellRange.getFirstRow() <= rowInd && rowInd <= cellRange.getLastRow() && - cellRange.getFirstCol() <= colInd && colInd <= cellRange.getLastCol())) { - return cellRange; - } - } - return null; - } - - public List getParseErrors() { - return parseErrors; - } - - public void setParser(XLSScorecardParser xlsScorecardParser) { - this.xlsScorecardParser = xlsScorecardParser; - } - - class DataExpectation { - - int row; - int col; - Object object; - String property; - String errorMessage; - - DataExpectation(int row, int col, Object object, String property, String errorMessage) { - this.row = row; - this.col = col; - this.object = object; - this.property = property; - this.errorMessage = errorMessage; - } - } -} diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/XLSKeywords.java b/drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/XLSKeywords.java deleted file mode 100644 index fc0709e8..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/XLSKeywords.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2012 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.drools.scorecards.parser.xls; - -public interface XLSKeywords { - - 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_CHARACTERISTIC_EXTERNAL_CLASS = "Full Class Name"; - - public static final String SCORECARD_BASE_SCORE = "Initial Score"; - public static final String SCORECARD_IMPORTS = "imports"; - public static final String SCORECARD_PACKAGE = "package"; - public static final String SCORECARD_USE_REASONCODES = "Use Reason Codes"; - public static final String SCORECARD_REASONCODE = "Reason Code"; - public static final String SCORECARD_REASONCODE_ALGORITHM = "Reason Code Algorithm"; - - public static final String SCORECARD_CHARACTERISTIC_NAME = "Name"; - public static final String SCORECARD_CHARACTERISTIC_DATATYPE = "Data Type"; - public static final String SCORECARD_CHARACTERISTIC_BASELINE_SCORE = "Baseline Score"; - - public static final String SCORECARD_CHARACTERISTIC_BIN_ATTRIBUTE = "Characteristic"; - public static final String SCORECARD_CHARACTERISTIC_BIN_OPERATOR = "Operator"; - public static final String SCORECARD_CHARACTERISTIC_BIN_LABEL = "Value"; - public static final String SCORECARD_CHARACTERISTIC_BIN_INITIALSCORE = "Partial Score"; - public static final String SCORECARD_CHARACTERISTIC_BIN_DESC = "Description"; - public static final String DATATYPE_NUMBER = "Number"; - public static final String DATATYPE_TEXT = "Text"; - public static final String DATATYPE_BOOLEAN = "Boolean"; -} diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/XLSScorecardParser.java b/drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/XLSScorecardParser.java deleted file mode 100644 index 8e022189..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/XLSScorecardParser.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright 2012 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -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.drools.scorecards.ScorecardError; -import org.drools.scorecards.parser.AbstractScorecardParser; -import org.drools.scorecards.parser.ScorecardParseException; -import org.drools.scorecards.pmml.PMMLGenerator; - -public class XLSScorecardParser extends AbstractScorecardParser { - - protected XLSEventDataCollector excelDataCollector; - private Scorecard scorecard; - private PMML pmmlDocument = null; - List parseErrors = new ArrayList(); - private HSSFSheet currentWorksheet; - - @Override - public List parseFile(InputStream inStream, String worksheetName) throws ScorecardParseException { - try { - excelDataCollector = new XLSEventDataCollector(); - excelDataCollector.setParser(this); - HSSFWorkbook workbook = new HSSFWorkbook(inStream); - HSSFSheet worksheet = workbook.getSheet(worksheetName); - if (worksheet != null) { - currentWorksheet = worksheet; - excelDataCollector.sheetStart(worksheetName); - excelDataCollector.setMergedRegionsInSheet(getMergedCellRangeList(worksheet)); - processSheet(worksheet); - excelDataCollector.sheetComplete(); - parseErrors = excelDataCollector.getParseErrors(); - scorecard = excelDataCollector.getScorecard(); - } else { - throw new ScorecardParseException("No worksheet found with name '" + worksheetName + "'."); - } - } catch (IOException e) { - throw new ScorecardParseException(e); - } - return parseErrors; - } - - @Override - public PMML getPMMLDocument() { - if (pmmlDocument == null) { - pmmlDocument = new PMMLGenerator().generateDocument(scorecard); - } - return pmmlDocument; - } - - private void processSheet(HSSFSheet worksheet) throws ScorecardParseException { - for (Row row : worksheet) { - int currentRowCtr = row.getRowNum(); - excelDataCollector.newRow(currentRowCtr); - for (Cell cell : row) { - int currentColCtr = cell.getColumnIndex(); - switch (cell.getCellType()) { - case Cell.CELL_TYPE_STRING: - excelDataCollector.newCell(currentRowCtr, currentColCtr, cell.getStringCellValue()); - break; - case Cell.CELL_TYPE_NUMERIC: - if (DateUtil.isCellDateFormatted(cell)) { - excelDataCollector.newCell(currentRowCtr, currentColCtr, cell.getDateCellValue()); - } else { - excelDataCollector.newCell(currentRowCtr, currentColCtr, Double.valueOf(cell.getNumericCellValue())); - } - break; - case Cell.CELL_TYPE_BOOLEAN: - excelDataCollector.newCell(currentRowCtr, currentColCtr, Boolean.valueOf(cell.getBooleanCellValue()).toString()); - break; - case Cell.CELL_TYPE_FORMULA: - break; - case Cell.CELL_TYPE_BLANK: - excelDataCollector.newCell(currentRowCtr, currentColCtr, ""); - break; - } - } - } - } - - public String peekValueAt(int row, int col) { - if (currentWorksheet != null){ - if ( row >= 0 && row < currentWorksheet.getLastRowNum() ) { - HSSFRow hssfRow = currentWorksheet.getRow(row); - if (hssfRow != null && col >= 0 && col < hssfRow.getLastCellNum()){ - return hssfRow.getCell(col).getStringCellValue(); - } - } - } - return null; - } - - private List getMergedCellRangeList(HSSFSheet worksheet) { - List mergedCellRanges = new ArrayList(); - int mergedRegionsCount = worksheet.getNumMergedRegions(); - for (int ctr = 0; ctr < mergedRegionsCount; ctr++) { - CellRangeAddress rangeAddress = worksheet.getMergedRegion(ctr); - mergedCellRanges.add(new MergedCellRange(rangeAddress.getFirstRow(), rangeAddress.getFirstColumn(), rangeAddress.getLastRow(), rangeAddress.getLastColumn())); - } - return mergedCellRanges; - } -} diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/pmml/PMMLExtensionNames.java b/drools-scorecards/src/main/java/org/drools/scorecards/pmml/PMMLExtensionNames.java deleted file mode 100644 index f704f388..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/pmml/PMMLExtensionNames.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2012 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.drools.scorecards.pmml; - -public class PMMLExtensionNames { - - public static final String SCORECARD_PACKAGE = "scorecardPackage"; - - public static final String SCORECARD_CELL_REF = "cellRef"; - public static final String SCORECARD_RESULTANT_SCORE_FIELD = "final"; - 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"; -} diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/pmml/PMMLGenerator.java b/drools-scorecards/src/main/java/org/drools/scorecards/pmml/PMMLGenerator.java deleted file mode 100644 index a97fd944..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/pmml/PMMLGenerator.java +++ /dev/null @@ -1,367 +0,0 @@ -/* - * Copyright 2012 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.drools.scorecards.pmml; - -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) { - //first clean up the scorecard - removeEmptyExtensions(pmmlScorecard); - createAndSetPredicates(pmmlScorecard); - - //second add additional elements to scorecard - createAndSetOutput(pmmlScorecard); - repositionExternalClassExtensions(pmmlScorecard); - - Extension scorecardPackage = ScorecardPMMLUtils.getExtension(pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas(), PMMLExtensionNames.SCORECARD_PACKAGE); - if ( scorecardPackage != null) { - pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().remove(scorecardPackage); - } - Extension importsExt = ScorecardPMMLUtils.getExtension(pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas(), PMMLExtensionNames.SCORECARD_IMPORTS); - if ( importsExt != null) { - pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().remove(importsExt); - } - - //now create the PMML document - PMML pmml = new PMML(); - Header header = new Header(); - Timestamp timestamp = new Timestamp(); - timestamp.getContent().add(new SimpleDateFormat("yyyy.MM.dd 'at' HH:mm:ss z").format(new Date())); - header.setTimestamp(timestamp); - header.setDescription("generated by the drools-scorecards module"); - header.getExtensions().add(scorecardPackage); - header.getExtensions().add(importsExt); - pmml.setHeader(header); - - createAndSetDataDictionary(pmml, pmmlScorecard); - pmml.getAssociationModelsAndBaselineModelsAndClusteringModels().add(pmmlScorecard); - removeAttributeFieldExtension(pmmlScorecard); - return pmml; - } - - private void repositionExternalClassExtensions(Scorecard pmmlScorecard) { - Characteristics characteristics = null; - for (Object obj : pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas()) { - if ( obj instanceof Characteristics ) { - characteristics = (Characteristics) obj; - break; - } - } - for (Object obj : pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas()) { - if ( obj instanceof MiningSchema ) { - MiningSchema schema = (MiningSchema)obj; - for (MiningField miningField : schema.getMiningFields()) { - String fieldName = miningField.getName(); - for (Characteristic characteristic : characteristics.getCharacteristics()){ - String characteristicName = ScorecardPMMLUtils.extractFieldNameFromCharacteristic(characteristic); - if (fieldName.equalsIgnoreCase(characteristicName)){ - Extension extension = ScorecardPMMLUtils.getExtension(characteristic.getExtensions(), PMMLExtensionNames.CHARACTERTISTIC_EXTERNAL_CLASS); - if ( extension != null ) { - characteristic.getExtensions().remove(extension); - miningField.getExtensions().add(extension); - } - } - } - } - } - } - } - - 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 (Attribute attribute : characteristic.getAttributes()) { - Extension fieldExtension = ScorecardPMMLUtils.getExtension(attribute.getExtensions(), PMMLExtensionNames.CHARACTERTISTIC_FIELD); - if ( fieldExtension != null ) { - attribute.getExtensions().remove(fieldExtension); - //break; - } - } - } - } - } - } - - private void createAndSetDataDictionary(PMML pmml, Scorecard pmmlScorecard) { - - DataDictionary dataDictionary = new DataDictionary(); - pmml.setDataDictionary(dataDictionary); - int ctr = 0; - for (Object obj : pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas()) { - if (obj instanceof Characteristics) { - Characteristics characteristics = (Characteristics) obj; - for (org.dmg.pmml_4_1.Characteristic characteristic : characteristics.getCharacteristics()) { - - DataField dataField = new DataField(); - Extension dataTypeExtension = ScorecardPMMLUtils.getExtension(characteristic.getExtensions(), PMMLExtensionNames.CHARACTERTISTIC_DATATYPE); - String dataType = dataTypeExtension.getValue(); - String factType = ScorecardPMMLUtils.getExtensionValue(characteristic.getExtensions(), PMMLExtensionNames.CHARACTERTISTIC_FACTTYPE); - - if ( factType != null ){ - Extension extension = new Extension(); - extension.setName("FactType"); - extension.setValue(factType); - dataField.getExtensions().add(extension); - } - - if (XLSKeywords.DATATYPE_NUMBER.equalsIgnoreCase(dataType)) { - dataField.setDataType(DATATYPE.DOUBLE); - dataField.setOptype(OPTYPE.CONTINUOUS); - } else if (XLSKeywords.DATATYPE_TEXT.equalsIgnoreCase(dataType)) { - dataField.setDataType(DATATYPE.STRING); - dataField.setOptype(OPTYPE.CATEGORICAL); - } else if (XLSKeywords.DATATYPE_BOOLEAN.equalsIgnoreCase(dataType)) { - dataField.setDataType(DATATYPE.BOOLEAN); - dataField.setOptype(OPTYPE.CATEGORICAL); - } - String field = ""; - for (Attribute attribute : characteristic.getAttributes()) { - for (Extension extension : attribute.getExtensions()) { - if (PMMLExtensionNames.CHARACTERTISTIC_FIELD.equalsIgnoreCase(extension.getName())) { - field = extension.getValue(); - break; - }// - } - } - dataField.setName(field); - dataDictionary.getDataFields().add(dataField); - characteristic.getExtensions().remove(dataTypeExtension); - ctr++; - } - } - } - dataDictionary.setNumberOfFields(BigInteger.valueOf(ctr)); - } - - 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); - for (Object obj : pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas()) { - if (obj instanceof Output) { - Output output = (Output)obj; - OutputField outputField = new OutputField(); - outputField.setDataType(DATATYPE.DOUBLE); - outputField.setDisplayName("Final Score"); - if ( fieldExtension != null ) { - pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().remove(fieldExtension); - outputField.setName(fieldExtension.getValue()); - } else { - outputField.setName("calculatedScore"); - } - if ( classExtension != null ) { - pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().remove(classExtension); - outputField.getExtensions().add(classExtension); - } - output.getOutputFields().add(outputField); - outputField.setFeature(RESULTFEATURE.PREDICTED_VALUE); - break; - } - } - } - - 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()) { - String dataType = ScorecardPMMLUtils.getExtensionValue(characteristic.getExtensions(), PMMLExtensionNames.CHARACTERTISTIC_DATATYPE); - Extension predicateExtension = null; - for (Attribute attribute : characteristic.getAttributes()) { - String predicateAsString = ""; - String field = ScorecardPMMLUtils.getExtensionValue(attribute.getExtensions(), PMMLExtensionNames.CHARACTERTISTIC_FIELD); - for (Extension extension : attribute.getExtensions()) { - if ("predicateResolver".equalsIgnoreCase(extension.getName())) { - predicateAsString = extension.getValue(); - predicateExtension = extension; - break; - } - } - setPredicatesForAttribute(attribute, dataType, field, predicateAsString); - attribute.getExtensions().remove(predicateExtension); - } - } - } - } - } - - private void setPredicatesForAttribute(Attribute pmmlAttribute, String dataType, String field, String predicateAsString) { - predicateAsString = StringUtil.unescapeXML(predicateAsString); - if (XLSKeywords.DATATYPE_NUMBER.equalsIgnoreCase(dataType)) { - setNumericPredicate(pmmlAttribute, field, predicateAsString); - } else if (XLSKeywords.DATATYPE_TEXT.equalsIgnoreCase(dataType)) { - setTextPredicate(pmmlAttribute, field, predicateAsString); - } else if (XLSKeywords.DATATYPE_BOOLEAN.equalsIgnoreCase(dataType)) { - setBooleanPredicate(pmmlAttribute, field, predicateAsString); - } - } - - private void setBooleanPredicate(Attribute pmmlAttribute, String field, String predicateAsString) { - SimplePredicate simplePredicate = new SimplePredicate(); - simplePredicate.setField(field); - simplePredicate.setOperator(PMMLOperators.EQUAL); - if ("TRUE".equalsIgnoreCase(predicateAsString)){ - simplePredicate.setValue("TRUE"); - } else if ("FALSE".equalsIgnoreCase(predicateAsString)){ - simplePredicate.setValue("FALSE"); - } - pmmlAttribute.setSimplePredicate(simplePredicate); - } - - private void setTextPredicate(Attribute pmmlAttribute, String field, String predicateAsString) { - String operator = ""; - if (predicateAsString.startsWith("=")) { - operator = "="; - predicateAsString = predicateAsString.substring(1); - } else if (predicateAsString.startsWith("!=")) { - operator = "!="; - predicateAsString = predicateAsString.substring(2); - } - if (predicateAsString.contains(",")) { - SimpleSetPredicate simpleSetPredicate = new SimpleSetPredicate(); - if ("!=".equalsIgnoreCase(operator)) { - simpleSetPredicate.setBooleanOperator(PMMLOperators.IS_NOT_IN); - } else { - simpleSetPredicate.setBooleanOperator(PMMLOperators.IS_IN); - } - simpleSetPredicate.setField(field); - Array array = new Array(); - array.setContent(predicateAsString.replace(",", " ")); - array.setType("string"); - array.setN(BigInteger.valueOf(predicateAsString.split(",").length)); - simpleSetPredicate.setArray(array); - pmmlAttribute.setSimpleSetPredicate(simpleSetPredicate); - } else { - SimplePredicate simplePredicate = new SimplePredicate(); - simplePredicate.setField(field); - if ("!=".equalsIgnoreCase(operator)) { - simplePredicate.setOperator(PMMLOperators.NOT_EQUAL); - } else { - simplePredicate.setOperator(PMMLOperators.EQUAL); - } - simplePredicate.setValue(predicateAsString); - pmmlAttribute.setSimplePredicate(simplePredicate); - } - } - - private void setNumericPredicate(Attribute pmmlAttribute, String field, String predicateAsString) { - if (predicateAsString.indexOf("-") > 0) { - CompoundPredicate compoundPredicate = new CompoundPredicate(); - compoundPredicate.setBooleanOperator("and"); - String left = predicateAsString.substring(0, predicateAsString.indexOf("-")).trim(); - String right = predicateAsString.substring(predicateAsString.indexOf("-") + 1).trim(); - SimplePredicate simplePredicate = new SimplePredicate(); - simplePredicate.setField(field); - simplePredicate.setOperator(PMMLOperators.GREATER_OR_EQUAL); - simplePredicate.setValue(left); - compoundPredicate.getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates().add(simplePredicate); - simplePredicate = new SimplePredicate(); - simplePredicate.setField(field); - simplePredicate.setOperator(PMMLOperators.LESS_THAN); - simplePredicate.setValue(right); - compoundPredicate.getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates().add(simplePredicate); - pmmlAttribute.setCompoundPredicate(compoundPredicate); - } else { - SimplePredicate simplePredicate = new SimplePredicate(); - simplePredicate.setField(field); - if (predicateAsString.startsWith("<=")) { - simplePredicate.setOperator(PMMLOperators.LESS_OR_EQUAL); - simplePredicate.setValue(predicateAsString.substring(3).trim()); - } else if (predicateAsString.startsWith(">=")) { - simplePredicate.setOperator(PMMLOperators.GREATER_OR_EQUAL); - simplePredicate.setValue(predicateAsString.substring(3).trim()); - } else if (predicateAsString.startsWith("=")) { - simplePredicate.setOperator(PMMLOperators.EQUAL); - simplePredicate.setValue(predicateAsString.substring(2).trim()); - } else if (predicateAsString.startsWith("!=")) { - simplePredicate.setOperator(PMMLOperators.NOT_EQUAL); - simplePredicate.setValue(predicateAsString.substring(3).trim()); - } else if (predicateAsString.startsWith("<")) { - simplePredicate.setOperator(PMMLOperators.LESS_THAN); - simplePredicate.setValue(predicateAsString.substring(3).trim()); - } else if (predicateAsString.startsWith(">")) { - simplePredicate.setOperator(PMMLOperators.GREATER_THAN); - simplePredicate.setValue(predicateAsString.substring(3).trim()); - } - pmmlAttribute.setSimplePredicate(simplePredicate); - } - } - - 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()) { - List toRemoveExtensionsList = new ArrayList(); - for (Extension extension : characteristic.getExtensions()) { - if (StringUtils.isEmpty(extension.getValue())) { - toRemoveExtensionsList.add(extension); - } - } - for (Extension extension : toRemoveExtensionsList) { - characteristic.getExtensions().remove(extension); - } - - for (Attribute attribute : characteristic.getAttributes()) { - List toRemoveExtensionsList2 = new ArrayList(); - for (Extension extension : attribute.getExtensions()) { - if (StringUtils.isEmpty(extension.getValue())) { - toRemoveExtensionsList2.add(extension); - } - } - for (Extension extension : toRemoveExtensionsList2) { - attribute.getExtensions().remove(extension); - } - } - } - } - } - } - -} diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/pmml/PMMLOperators.java b/drools-scorecards/src/main/java/org/drools/scorecards/pmml/PMMLOperators.java deleted file mode 100644 index 99cf86d6..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/pmml/PMMLOperators.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2012 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.drools.scorecards.pmml; - -public class PMMLOperators { - - public static final String LESS_THAN = "lessThan"; - public static final String GREATER_THAN = "greaterThan"; - public static final String NOT_EQUAL = "notEqual"; - public static final String EQUAL = "equal"; - public static final String GREATER_OR_EQUAL = "greaterOrEqual"; - public static final String LESS_OR_EQUAL = "lessOrEqual"; - public static final String IS_NOT_IN = "isNotIn"; - public static final String IS_IN = "isIn"; -} diff --git a/drools-scorecards/src/main/java/org/drools/scorecards/pmml/ScorecardPMMLUtils.java b/drools-scorecards/src/main/java/org/drools/scorecards/pmml/ScorecardPMMLUtils.java deleted file mode 100644 index d17a79d2..00000000 --- a/drools-scorecards/src/main/java/org/drools/scorecards/pmml/ScorecardPMMLUtils.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright 2012 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -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.drools.scorecards.parser.xls.XLSKeywords; - -public class ScorecardPMMLUtils { - -// public static String getDataType(org.dmg.pmml_4_1.Characteristic c) { -// for (Extension extension : c.getExtensions()) { -// if (PMMLExtensionNames.CHARACTERTISTIC_DATATYPE.equalsIgnoreCase(extension.getName())) { -// return extension.getValue(); -// } -// } -// return null; -// } - - public static String getExtensionValue(List extensions, String extensionName) { - for (Object obj : extensions) { - if (obj instanceof Extension) { - Extension extension = (Extension) obj; - if (extensionName.equalsIgnoreCase(extension.getName())) { - return extension.getValue(); - } - } - } - return null; - } - - public static Extension getExtension(List extensions, String extensionName) { - for (Object obj : extensions) { - if (obj instanceof Extension) { - Extension extension = (Extension) obj; - if (extensionName.equalsIgnoreCase(extension.getName())) { - return extension; - } - } - } - return null; - } - - public static Scorecard createScorecard(){ - Scorecard scorecard = new Scorecard(); - //default false, until the spreadsheet enables explicitly. - scorecard.setUseReasonCodes(Boolean.FALSE); - scorecard.setIsScorable(Boolean.TRUE); - return scorecard; - } - - public static String getDataType(PMML pmmlDocument, String fieldName) { - DataDictionary dataDictionary = pmmlDocument.getDataDictionary(); - for (DataField dataField : dataDictionary.getDataFields()){ - if (dataField.getName().equalsIgnoreCase(fieldName)) { - DATATYPE datatype = dataField.getDataType(); - if (datatype == DATATYPE.DOUBLE) { - return XLSKeywords.DATATYPE_NUMBER; - } else if (datatype == DATATYPE.STRING) { - return XLSKeywords.DATATYPE_TEXT; - } else if (datatype == DATATYPE.BOOLEAN) { - return XLSKeywords.DATATYPE_BOOLEAN; - } - } - } - return null; - } - - public static String extractFieldNameFromCharacteristic(Characteristic c) { - String field = ""; - Attribute scoreAttribute = c.getAttributes().get(0); - if (scoreAttribute.getSimplePredicate() != null) { - field = scoreAttribute.getSimplePredicate().getField(); - } else if (scoreAttribute.getSimpleSetPredicate() != null) { - field = scoreAttribute.getSimpleSetPredicate().getField(); - } else if (scoreAttribute.getCompoundPredicate() != null) { - Object predicate = scoreAttribute.getCompoundPredicate().getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates().get(0); - if (predicate instanceof SimplePredicate){ - field = ((SimplePredicate)predicate).getField(); - } else if (predicate instanceof SimpleSetPredicate){ - field = ((SimpleSetPredicate)predicate).getField(); - } - } - return field; - } - -} diff --git a/drools-scorecards/src/main/resources/org/dmg/pmml/bindings.xjb b/drools-scorecards/src/main/resources/org/dmg/pmml/bindings.xjb deleted file mode 100644 index 81680a40..00000000 --- a/drools-scorecards/src/main/resources/org/dmg/pmml/bindings.xjb +++ /dev/null @@ -1,44 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/drools-scorecards/src/main/resources/org/dmg/pmml/pmml-4-1.xsd b/drools-scorecards/src/main/resources/org/dmg/pmml/pmml-4-1.xsd deleted file mode 100644 index 1a1d00bf..00000000 --- a/drools-scorecards/src/main/resources/org/dmg/pmml/pmml-4-1.xsd +++ /dev/null @@ -1,3370 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/drools-scorecards/src/main/resources/org/dmg/pmml/run_xjc.bat b/drools-scorecards/src/main/resources/org/dmg/pmml/run_xjc.bat deleted file mode 100644 index 0700d7a7..00000000 --- a/drools-scorecards/src/main/resources/org/dmg/pmml/run_xjc.bat +++ /dev/null @@ -1 +0,0 @@ -xjc -p org.dmg.pmml_4_1 -d E:\opensource\drools-scorecard\src\main\java pmml-4-1.xsd -b bindings.xjb -extension diff --git a/drools-scorecards/src/test/java/org/drools/scorecards/DrlFromPMMLTest.java b/drools-scorecards/src/test/java/org/drools/scorecards/DrlFromPMMLTest.java deleted file mode 100644 index 9e293e7f..00000000 --- a/drools-scorecards/src/test/java/org/drools/scorecards/DrlFromPMMLTest.java +++ /dev/null @@ -1,106 +0,0 @@ -package org.drools.scorecards; - -import org.dmg.pmml_4_1.PMML; -import org.drools.KnowledgeBase; -import org.drools.KnowledgeBaseFactory; -import org.drools.builder.KnowledgeBuilder; -import org.drools.builder.KnowledgeBuilderError; -import org.drools.builder.KnowledgeBuilderFactory; -import org.drools.builder.ResourceType; -import org.drools.definition.type.FactType; -import org.drools.io.ResourceFactory; -import org.drools.runtime.StatefulKnowledgeSession; -import org.junit.Before; -import org.junit.Test; - -import static junit.framework.Assert.*; -import static org.drools.scorecards.ScorecardCompiler.DrlType.INTERNAL_DECLARED_TYPES; - -public class DrlFromPMMLTest { - - private static String drl; - - @Before - public void setUp() throws Exception { - ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES); - if (scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_c.xls")) ) { - PMML pmmlDocument = scorecardCompiler.getPMMLDocument(); - assertNotNull(pmmlDocument); - drl = scorecardCompiler.getDRL(); - } else { - fail("failed to parse scoremodel Excel."); - } - } - - @Test - public void testDrlNoNull() throws Exception { - assertNotNull(drl); - assertTrue(drl.length() > 0); - //System.out.println(drl); - } - - @Test - public void testPackage() throws Exception { - assertTrue(drl.contains("package org.drools.scorecards.example")); - } - - @Test - public void testRuleCount() throws Exception { - assertEquals(13, StringUtil.countMatches(drl, "rule \"")); - } - - @Test - public void testImports() throws Exception { - assertEquals(5, StringUtil.countMatches(drl, "import ")); - } - - @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()); - } - assertFalse( kbuilder.hasErrors() ); - - //BUILD RULEBASE - KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); - kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() ); - - //NEW WORKING MEMORY - StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession(); - FactType scorecardType = kbase.getFactType( "org.drools.scorecards.example","SampleScore" ); - DroolsScorecard scorecard = (DroolsScorecard) scorecardType.newInstance(); - scorecardType.set(scorecard, "age", 10); - session.insert( scorecard ); - session.fireAllRules(); - session.dispose(); - //occupation = 5, age = 25, validLicence -1 - assertEquals(29.0,scorecard.getCalculatedScore()); - - session = kbase.newStatefulKnowledgeSession(); - scorecard = (DroolsScorecard) 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; - assertTrue(-1 == scorecard.getCalculatedScore()); - - session = kbase.newStatefulKnowledgeSession(); - scorecard = (DroolsScorecard) 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,scorecard.getCalculatedScore()); - } - - -} diff --git a/drools-scorecards/src/test/java/org/drools/scorecards/ExternalObjectModelTest.java b/drools-scorecards/src/test/java/org/drools/scorecards/ExternalObjectModelTest.java deleted file mode 100644 index 34b2ded1..00000000 --- a/drools-scorecards/src/test/java/org/drools/scorecards/ExternalObjectModelTest.java +++ /dev/null @@ -1,136 +0,0 @@ -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.drools.KnowledgeBase; -import org.drools.KnowledgeBaseFactory; -import org.drools.builder.KnowledgeBuilder; -import org.drools.builder.KnowledgeBuilderError; -import org.drools.builder.KnowledgeBuilderFactory; -import org.drools.builder.ResourceType; -import org.drools.io.ResourceFactory; -import org.drools.runtime.StatefulKnowledgeSession; -import org.drools.scorecards.example.Applicant; -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.*; - -public class ExternalObjectModelTest { - private static String drl; - private PMML pmmlDocument; - private static ScorecardCompiler scorecardCompiler; - @Before - public void setUp() throws Exception { - scorecardCompiler = new ScorecardCompiler(EXTERNAL_OBJECT_MODEL); - if (scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_externalmodel.xls")) ) { - pmmlDocument = scorecardCompiler.getPMMLDocument(); - assertNotNull(pmmlDocument); - drl = scorecardCompiler.getDRL(); - //System.out.println(drl); - } else { - fail("failed to parse scoremodel Excel."); - } - } - - @Test - public void testPMMLNotNull() throws Exception { - assertNotNull(pmmlDocument); - } - - @Test - public void testPMMLToString() throws Exception { - String pmml = scorecardCompiler.getPMML(); - assertNotNull(pmml); - assertTrue(pmml.length() > 0); - } - - @Test - public void testPMMLCustomOutput() throws Exception { - for (Object serializable : pmmlDocument.getAssociationModelsAndBaselineModelsAndClusteringModels()){ - if (serializable instanceof Scorecard){ - Scorecard scorecard = (Scorecard)serializable; - for (Object obj :scorecard.getExtensionsAndCharacteristicsAndMiningSchemas()){ - if ( obj instanceof Output) { - Output output = (Output)obj; - final List outputFields = output.getOutputFields(); - assertEquals(1, outputFields.size()); - final OutputField outputField = outputFields.get(0); - assertNotNull(outputField); - assertEquals("totalScore", outputField.getName()); - assertEquals("Final Score", outputField.getDisplayName()); - assertEquals("double", outputField.getDataType().value()); - assertEquals("predictedValue", outputField.getFeature().value()); - final Extension extension = ScorecardPMMLUtils.getExtension(outputField.getExtensions(), PMMLExtensionNames.SCORECARD_RESULTANT_SCORE_CLASS); - assertNotNull(extension); - assertEquals("org.drools.scorecards.example.Applicant",extension.getValue()); - return; - } - } - } - } - fail(); - } - - @Test - public void testDrlNoNull() throws Exception { - assertNotNull(drl); - assertTrue(drl.length() > 0); - //System.out.println(drl); - } - - @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()); - } - 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.fireAllRules(); - session.dispose(); - //occupation = 0, age = 30, validLicence -1 - assertEquals(29.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; - assertEquals(-1.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 - assertEquals(41.0,applicant.getTotalScore()); - } -} diff --git a/drools-scorecards/src/test/java/org/drools/scorecards/PMMLDocumentTest.java b/drools-scorecards/src/test/java/org/drools/scorecards/PMMLDocumentTest.java deleted file mode 100644 index 086f90da..00000000 --- a/drools-scorecards/src/test/java/org/drools/scorecards/PMMLDocumentTest.java +++ /dev/null @@ -1,268 +0,0 @@ -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.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.*; - -public class PMMLDocumentTest { - - private static PMML pmmlDocument; - private static ScorecardCompiler scorecardCompiler; - - @Before - public void setUp() throws Exception { - scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES); - scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_c.xls")); - pmmlDocument = scorecardCompiler.getPMMLDocument(); - } - - @Test - public void testPMMLDocument() throws Exception { - Assert.assertNotNull(pmmlDocument); - String pmml = scorecardCompiler.getPMML(); - Assert.assertNotNull(pmml); - Assert.assertTrue(pmml.length() > 0); - } - - @Test - public void testHeader() throws Exception { - Header header = pmmlDocument.getHeader(); - assertNotNull(header); - assertNotNull(ScorecardPMMLUtils.getExtensionValue(header.getExtensions(), PMMLExtensionNames.SCORECARD_PACKAGE)); - assertNotNull(ScorecardPMMLUtils.getExtensionValue(header.getExtensions(), PMMLExtensionNames.SCORECARD_IMPORTS)); - } - - @Test - public void testDataDictionary() throws Exception { - DataDictionary dataDictionary = pmmlDocument.getDataDictionary(); - assertNotNull(dataDictionary); - assertEquals(4, dataDictionary.getNumberOfFields().intValue()); - assertEquals("age", dataDictionary.getDataFields().get(0).getName()); - assertEquals("occupation",dataDictionary.getDataFields().get(1).getName()); - assertEquals("residenceState", dataDictionary.getDataFields().get(2).getName()); - assertEquals("validLicense", dataDictionary.getDataFields().get(3).getName()); - } - - @Test - public void testMiningSchema() throws Exception { - for (Object serializable : pmmlDocument.getAssociationModelsAndBaselineModelsAndClusteringModels()){ - if (serializable instanceof Scorecard){ - for (Object obj :((Scorecard)serializable) .getExtensionsAndCharacteristicsAndMiningSchemas()){ - if (obj instanceof MiningSchema){ - MiningSchema miningSchema = ((MiningSchema)obj); - assertEquals(4, miningSchema.getMiningFields().size()); - assertEquals("age", miningSchema.getMiningFields().get(0).getName()); - assertEquals("occupation",miningSchema.getMiningFields().get(1).getName()); - assertEquals("residenceState", miningSchema.getMiningFields().get(2).getName()); - assertEquals("validLicense", miningSchema.getMiningFields().get(3).getName()); - return; - } - } - } - } - fail(); - } - - @Test - public void testCharacteristicsAndAttributes() throws Exception { - for (Object serializable : pmmlDocument.getAssociationModelsAndBaselineModelsAndClusteringModels()){ - if (serializable instanceof Scorecard){ - for (Object obj :((Scorecard)serializable) .getExtensionsAndCharacteristicsAndMiningSchemas()){ - if (obj instanceof Characteristics){ - Characteristics characteristics = (Characteristics)obj; - assertEquals(4, characteristics.getCharacteristics().size()); - assertEquals("AgeScore", characteristics.getCharacteristics().get(0).getName()); - assertEquals("$B$8", ScorecardPMMLUtils.getExtensionValue(characteristics.getCharacteristics().get(0).getExtensions(), "cellRef")); - - assertEquals("OccupationScore",characteristics.getCharacteristics().get(1).getName()); - assertEquals("$B$16", ScorecardPMMLUtils.getExtensionValue(characteristics.getCharacteristics().get(1).getExtensions(), "cellRef")); - - assertEquals("ResidenceStateScore",characteristics.getCharacteristics().get(2).getName()); - assertEquals("$B$22", ScorecardPMMLUtils.getExtensionValue(characteristics.getCharacteristics().get(2).getExtensions(), "cellRef")); - - assertEquals("ValidLicenseScore",characteristics.getCharacteristics().get(3).getName()); - assertEquals("$B$28", ScorecardPMMLUtils.getExtensionValue(characteristics.getCharacteristics().get(3).getExtensions(), "cellRef")); - return; - } - } - } - } - fail(); - } - - @Test - public void testAgeScoreCharacteristic() throws Exception { - for (Object serializable : pmmlDocument.getAssociationModelsAndBaselineModelsAndClusteringModels()){ - if (serializable instanceof Scorecard){ - for (Object obj :((Scorecard)serializable) .getExtensionsAndCharacteristicsAndMiningSchemas()){ - if (obj instanceof Characteristics){ - Characteristics characteristics = (Characteristics)obj; - assertEquals(4, characteristics.getCharacteristics().size()); - assertEquals("AgeScore", characteristics.getCharacteristics().get(0).getName()); - assertEquals("$B$8", ScorecardPMMLUtils.getExtensionValue(characteristics.getCharacteristics().get(0).getExtensions(), "cellRef")); - - assertNotNull(characteristics.getCharacteristics().get(0).getAttributes()); - assertEquals(4, characteristics.getCharacteristics().get(0).getAttributes().size()); - - Attribute attribute = characteristics.getCharacteristics().get(0).getAttributes().get(0); - assertEquals("$C$10", ScorecardPMMLUtils.getExtensionValue(attribute.getExtensions(), "cellRef")); - assertNotNull(attribute.getSimplePredicate()); - - attribute = characteristics.getCharacteristics().get(0).getAttributes().get(1); - assertEquals("$C$11", ScorecardPMMLUtils.getExtensionValue(attribute.getExtensions(), "cellRef")); - assertNotNull(attribute.getCompoundPredicate()); - - attribute = characteristics.getCharacteristics().get(0).getAttributes().get(2); - assertEquals("$C$12", ScorecardPMMLUtils.getExtensionValue(attribute.getExtensions(), "cellRef")); - assertNotNull(attribute.getCompoundPredicate()); - - attribute = characteristics.getCharacteristics().get(0).getAttributes().get(3); - assertEquals("$C$13", ScorecardPMMLUtils.getExtensionValue(attribute.getExtensions(), "cellRef")); - assertNotNull(attribute.getSimplePredicate()); - return; - } - } - } - } - fail(); - } - - @Test - public void testOccupationScoreCharacteristic() throws Exception { - for (Object serializable : pmmlDocument.getAssociationModelsAndBaselineModelsAndClusteringModels()){ - if (serializable instanceof Scorecard){ - for (Object obj :((Scorecard)serializable) .getExtensionsAndCharacteristicsAndMiningSchemas()){ - if (obj instanceof Characteristics){ - Characteristics characteristics = (Characteristics)obj; - assertEquals(4, characteristics.getCharacteristics().size()); - - assertNotNull(characteristics.getCharacteristics().get(1).getAttributes()); - assertEquals(3, characteristics.getCharacteristics().get(1).getAttributes().size()); - - Attribute attribute = characteristics.getCharacteristics().get(1).getAttributes().get(0); - assertEquals("$C$18", ScorecardPMMLUtils.getExtensionValue(attribute.getExtensions(), "cellRef")); - assertNotNull(ScorecardPMMLUtils.getExtensionValue(attribute.getExtensions(), "description")); - assertEquals("skydiving is a risky occupation", ScorecardPMMLUtils.getExtensionValue(attribute.getExtensions(), "description")); - assertNotNull(attribute.getSimplePredicate()); - - attribute = characteristics.getCharacteristics().get(1).getAttributes().get(1); - assertEquals("$C$19", ScorecardPMMLUtils.getExtensionValue(attribute.getExtensions(), "cellRef")); - assertNotNull(attribute.getSimpleSetPredicate()); - - attribute = characteristics.getCharacteristics().get(1).getAttributes().get(2); - assertEquals("$C$20", ScorecardPMMLUtils.getExtensionValue(attribute.getExtensions(), "cellRef")); - assertNotNull(attribute.getSimplePredicate()); - return; - } - } - } - } - fail(); - } - - @Test - public void testResidenceStateScoreCharacteristic() throws Exception { - for (Object serializable : pmmlDocument.getAssociationModelsAndBaselineModelsAndClusteringModels()){ - if (serializable instanceof Scorecard){ - for (Object obj :((Scorecard)serializable) .getExtensionsAndCharacteristicsAndMiningSchemas()){ - if (obj instanceof Characteristics){ - Characteristics characteristics = (Characteristics)obj; - assertEquals(4, characteristics.getCharacteristics().size()); - - assertNotNull(characteristics.getCharacteristics().get(2).getAttributes()); - assertEquals(3, characteristics.getCharacteristics().get(2).getAttributes().size()); - - Attribute attribute = characteristics.getCharacteristics().get(2).getAttributes().get(0); - assertEquals("$C$24", ScorecardPMMLUtils.getExtensionValue(attribute.getExtensions(), "cellRef")); - assertNotNull(attribute.getSimplePredicate()); - - attribute = characteristics.getCharacteristics().get(2).getAttributes().get(1); - assertEquals("$C$25", ScorecardPMMLUtils.getExtensionValue(attribute.getExtensions(), "cellRef")); - assertNotNull(attribute.getSimplePredicate()); - - attribute = characteristics.getCharacteristics().get(2).getAttributes().get(2); - assertEquals("$C$26", ScorecardPMMLUtils.getExtensionValue(attribute.getExtensions(), "cellRef")); - assertNotNull(attribute.getSimplePredicate()); - return; - } - } - } - } - fail(); - } - - @Test - public void testValidLicenseScoreCharacteristic() throws Exception { - for (Object serializable : pmmlDocument.getAssociationModelsAndBaselineModelsAndClusteringModels()){ - if (serializable instanceof Scorecard){ - for (Object obj :((Scorecard)serializable) .getExtensionsAndCharacteristicsAndMiningSchemas()){ - if (obj instanceof Characteristics){ - Characteristics characteristics = (Characteristics)obj; - assertEquals(4, characteristics.getCharacteristics().size()); - - assertNotNull(characteristics.getCharacteristics().get(3).getAttributes()); - assertEquals(2, characteristics.getCharacteristics().get(3).getAttributes().size()); - - Attribute attribute = characteristics.getCharacteristics().get(3).getAttributes().get(0); - assertEquals("$C$30", ScorecardPMMLUtils.getExtensionValue(attribute.getExtensions(), "cellRef")); - assertNotNull(attribute.getSimplePredicate()); - - attribute = characteristics.getCharacteristics().get(3).getAttributes().get(1); - assertEquals("$C$31", ScorecardPMMLUtils.getExtensionValue(attribute.getExtensions(), "cellRef")); - assertNotNull(attribute.getSimplePredicate()); - return; - } - } - } - } - fail(); - } - @Test - public void testScorecardWithExtensions() throws Exception { - for (Object serializable : pmmlDocument.getAssociationModelsAndBaselineModelsAndClusteringModels()){ - if (serializable instanceof Scorecard){ - Scorecard scorecard = (Scorecard)serializable; - assertEquals("Sample Score",scorecard.getModelName()); -// assertNotNull(ScorecardPMMLUtils.getExtension(scorecard.getExtensionsAndCharacteristicsAndMiningSchemas(), PMMLExtensionNames.SCORECARD_OBJECT_CLASS)); -// assertNotNull(ScorecardPMMLUtils.getExtension(scorecard.getExtensionsAndCharacteristicsAndMiningSchemas(), PMMLExtensionNames.SCORECARD_BOUND_VAR_NAME)); - return; - } - } - fail(); - } - - @Test - public void testOutput() throws Exception { - for (Object serializable : pmmlDocument.getAssociationModelsAndBaselineModelsAndClusteringModels()){ - if (serializable instanceof Scorecard){ - Scorecard scorecard = (Scorecard)serializable; - for (Object obj :scorecard.getExtensionsAndCharacteristicsAndMiningSchemas()){ - if ( obj instanceof Output) { - Output output = (Output)obj; - assertEquals(1, output.getOutputFields().size()); - assertNotNull(output.getOutputFields().get(0)); - assertEquals("calculatedScore", output.getOutputFields().get(0).getName()); - assertEquals("Final Score", output.getOutputFields().get(0).getDisplayName()); - assertEquals("double", output.getOutputFields().get(0).getDataType().value()); - assertEquals("predictedValue", output.getOutputFields().get(0).getFeature().value()); - return; - } - } - } - } - fail(); - } -} diff --git a/drools-scorecards/src/test/java/org/drools/scorecards/ScorecardParseErrorsTest.java b/drools-scorecards/src/test/java/org/drools/scorecards/ScorecardParseErrorsTest.java deleted file mode 100644 index 5e7a2c58..00000000 --- a/drools-scorecards/src/test/java/org/drools/scorecards/ScorecardParseErrorsTest.java +++ /dev/null @@ -1,65 +0,0 @@ -package org.drools.scorecards; - -import junit.framework.Assert; -import org.junit.Test; - -import static org.drools.scorecards.ScorecardCompiler.DrlType.INTERNAL_DECLARED_TYPES; - -public class ScorecardParseErrorsTest { - - @Test - public void testErrorCount() throws Exception { - ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES); - boolean compileResult = scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_errors.xls")); - Assert.assertFalse(compileResult); - Assert.assertEquals(4, scorecardCompiler.getScorecardParseErrors().size()); - Assert.assertEquals("$C$4", scorecardCompiler.getScorecardParseErrors().get(0).getErrorLocation()); - Assert.assertEquals("Scorecard Package is missing", scorecardCompiler.getScorecardParseErrors().get(0).getErrorMessage()); -// for(ScorecardError error : scorecardCompiler.getScorecardParseErrors()){ -// System.out.println("testErrorCount :"+error.getErrorLocation()+"->"+error.getErrorMessage()); -// } - } - - @Test - public void testWrongData() throws Exception { - ScorecardCompiler scorecardCompiler = new ScorecardCompiler(); - boolean compileResult = scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_errors.xls"), "scorecards_wrongData"); -// for(ScorecardError error : scorecardCompiler.getScorecardParseErrors()){ -// System.out.println("testWrongData :"+error.getErrorLocation()+"->"+error.getErrorMessage()); -// } - Assert.assertFalse(compileResult); - Assert.assertEquals(4, scorecardCompiler.getScorecardParseErrors().size()); - Assert.assertEquals("$D$10", scorecardCompiler.getScorecardParseErrors().get(0).getErrorLocation()); - Assert.assertEquals("$D$19", scorecardCompiler.getScorecardParseErrors().get(1).getErrorLocation()); - Assert.assertEquals("$C$8", scorecardCompiler.getScorecardParseErrors().get(2).getErrorLocation()); - Assert.assertEquals("$C$28", scorecardCompiler.getScorecardParseErrors().get(3).getErrorLocation()); - - } - - @Test - public void testMissingDataType() throws Exception { - ScorecardCompiler scorecardCompiler = new ScorecardCompiler(ScorecardCompiler.DrlType.INTERNAL_DECLARED_TYPES); - boolean compileResult = scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_errors.xls"), "missingDataType"); -// for(ScorecardError error : scorecardCompiler.getScorecardParseErrors()){ -// System.out.println("testMissingDataType :"+error.getErrorLocation()+"->"+error.getErrorMessage()); -// } - Assert.assertFalse(compileResult); - Assert.assertEquals(2, scorecardCompiler.getScorecardParseErrors().size()); - Assert.assertEquals("$C$8", scorecardCompiler.getScorecardParseErrors().get(0).getErrorLocation()); - Assert.assertEquals("$C$16", scorecardCompiler.getScorecardParseErrors().get(1).getErrorLocation()); - } - - @Test - public void testMissingAttributes() throws Exception { - ScorecardCompiler scorecardCompiler = new ScorecardCompiler(ScorecardCompiler.DrlType.INTERNAL_DECLARED_TYPES); - boolean compileResult = scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_errors.xls"), "incomplete_noAttr"); - Assert.assertFalse(compileResult); -// Assert.assertEquals(2, scorecardCompiler.getScorecardParseErrors().size()); -// Assert.assertEquals("$C$11", scorecardCompiler.getScorecardParseErrors().get(0).getErrorLocation()); -// Assert.assertEquals("$C$19", scorecardCompiler.getScorecardParseErrors().get(1).getErrorLocation()); -// for(ScorecardError error : scorecardCompiler.getScorecardParseErrors()){ -// System.out.println("testMissingAttributes :"+error.getErrorLocation()+"->"+error.getErrorMessage()); -// } - } -// -} diff --git a/drools-scorecards/src/test/java/org/drools/scorecards/ScorecardReasonCodeTest.java b/drools-scorecards/src/test/java/org/drools/scorecards/ScorecardReasonCodeTest.java deleted file mode 100644 index a11ebb87..00000000 --- a/drools-scorecards/src/test/java/org/drools/scorecards/ScorecardReasonCodeTest.java +++ /dev/null @@ -1,271 +0,0 @@ -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.drools.KnowledgeBase; -import org.drools.KnowledgeBaseFactory; -import org.drools.builder.KnowledgeBuilder; -import org.drools.builder.KnowledgeBuilderError; -import org.drools.builder.KnowledgeBuilderFactory; -import org.drools.builder.ResourceType; -import org.drools.definition.type.FactType; -import org.drools.io.ResourceFactory; -import org.drools.runtime.StatefulKnowledgeSession; -import org.junit.Before; -import org.junit.Test; - -import static junit.framework.Assert.*; -import static org.drools.scorecards.ScorecardCompiler.DrlType.*; - -public class ScorecardReasonCodeTest { - private static PMML pmmlDocument; - private static String drl; - private static ScorecardCompiler scorecardCompiler; - @Before - public void setUp() throws Exception { - scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES); - boolean compileResult = scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_reasoncodes.xls")); - if (!compileResult) { - for(ScorecardError error : scorecardCompiler.getScorecardParseErrors()){ - System.out.println("setup :"+error.getErrorLocation()+"->"+error.getErrorMessage()); - } - } - drl = scorecardCompiler.getDRL(); - Assert.assertNotNull(drl); - assertTrue(drl.length() > 0); - //System.out.println(drl); - } - - @Test - public void testPMMLDocument() throws Exception { - pmmlDocument = scorecardCompiler.getPMMLDocument(); - Assert.assertNotNull(pmmlDocument); - - String pmml = scorecardCompiler.getPMML(); - Assert.assertNotNull(pmml); - assertTrue(pmml.length() > 0); - //System.out.println(pmml); - } - - @Test - public void testAbsenceOfReasonCodes() throws Exception { - ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES); - scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_c.xls")); - PMML pmml = scorecardCompiler.getPMMLDocument(); - for (Object serializable : pmml.getAssociationModelsAndBaselineModelsAndClusteringModels()){ - if (serializable instanceof Scorecard){ - assertFalse(((Scorecard) serializable).isUseReasonCodes()); - } - } - } - - @Test - public void testUseReasonCodes() throws Exception { - for (Object serializable : pmmlDocument.getAssociationModelsAndBaselineModelsAndClusteringModels()){ - if (serializable instanceof Scorecard){ - assertTrue(((Scorecard)serializable).isUseReasonCodes()); - assertEquals(100.0, ((Scorecard)serializable).getInitialScore()); - assertEquals("pointsBelow",((Scorecard)serializable).getReasonCodeAlgorithm()); - } - } - } - - @Test - public void testReasonCodes() throws Exception { - for (Object serializable : pmmlDocument.getAssociationModelsAndBaselineModelsAndClusteringModels()){ - if (serializable instanceof Scorecard){ - for (Object obj :((Scorecard)serializable) .getExtensionsAndCharacteristicsAndMiningSchemas()){ - if (obj instanceof Characteristics){ - Characteristics characteristics = (Characteristics)obj; - assertEquals(4, characteristics.getCharacteristics().size()); - for (Characteristic characteristic : characteristics.getCharacteristics()){ - for (Attribute attribute : characteristic.getAttributes()){ - assertNotNull(attribute.getReasonCode()); - } - } - return; - } - } - } - } - fail(); - } - - @Test - public void testBaselineScores() throws Exception { - for (Object serializable : pmmlDocument.getAssociationModelsAndBaselineModelsAndClusteringModels()){ - if (serializable instanceof Scorecard){ - for (Object obj :((Scorecard)serializable) .getExtensionsAndCharacteristicsAndMiningSchemas()){ - if (obj instanceof Characteristics){ - Characteristics characteristics = (Characteristics)obj; - assertEquals(4, characteristics.getCharacteristics().size()); - assertEquals(10.0, characteristics.getCharacteristics().get(0).getBaselineScore()); - assertEquals(99.0, characteristics.getCharacteristics().get(1).getBaselineScore()); - assertEquals(12.0, characteristics.getCharacteristics().get(2).getBaselineScore()); - assertEquals(0.0, characteristics.getCharacteristics().get(3).getBaselineScore()); - assertEquals(25.0, ((Scorecard)serializable).getBaselineScore()); - return; - } - } - } - } - fail(); - } - - @Test - public void testMissingReasonCodes() throws Exception { - ScorecardCompiler scorecardCompiler = new ScorecardCompiler(); - scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_reasoncodes.xls"), "scorecards_reason_error"); - assertEquals(3, scorecardCompiler.getScorecardParseErrors().size()); - assertEquals("$F$13", scorecardCompiler.getScorecardParseErrors().get(0).getErrorLocation()); - assertEquals("$F$22", scorecardCompiler.getScorecardParseErrors().get(1).getErrorLocation()); - } - - @Test - public void testMissingBaselineScores() throws Exception { - ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES); - scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_reasoncodes.xls"), "scorecards_reason_error"); - assertEquals(3, scorecardCompiler.getScorecardParseErrors().size()); - assertEquals("$D$30", scorecardCompiler.getScorecardParseErrors().get(2).getErrorLocation()); - } - - @Test - public void testReasonCodesCombinations() throws Exception { - ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES); - scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_reasoncodes.xls"), "scorecards_char_reasoncode"); - assertEquals(0, scorecardCompiler.getScorecardParseErrors().size()); - String drl = scorecardCompiler.getDRL(); - assertNotNull(drl); - //System.out.println(drl); - KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - - kbuilder.add( ResourceFactory.newByteArrayResource(drl.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(); - FactType scorecardType = kbase.getFactType( "org.drools.scorecards.example","SampleScore" ); - - DroolsScorecard scorecard = (DroolsScorecard) scorecardType.newInstance(); - scorecardType.set(scorecard, "age", 10); - session.insert(scorecard); - session.fireAllRules(); - session.dispose(); - //age = 30, validLicence -1 - assertTrue(29 == scorecard.getCalculatedScore()); - //age-reasoncode=AGE02, license-reasoncode=VL002 - assertEquals(2, scorecard.getReasonCodes().size()); - assertTrue(scorecard.getReasonCodes().contains("AGE02")); - assertTrue(scorecard.getReasonCodes().contains("VL099")); - - session = kbase.newStatefulKnowledgeSession(); - scorecard = (DroolsScorecard) scorecardType.newInstance(); - scorecardType.set(scorecard, "age", 0); - scorecardType.set(scorecard, "occupation", "SKYDIVER"); - session.insert(scorecard); - session.fireAllRules(); - session.dispose(); - //occupation = -10, age = +10, validLicense = -1; - assertTrue(-1 == scorecard.getCalculatedScore()); - assertEquals(3, scorecard.getReasonCodes().size()); - //[AGE01, VL002, OCC01] - assertTrue(scorecard.getReasonCodes().contains("AGE01")); - assertTrue(scorecard.getReasonCodes().contains("VL099")); - assertTrue(scorecard.getReasonCodes().contains("OCC99")); - - session = kbase.newStatefulKnowledgeSession(); - scorecard = (DroolsScorecard) 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(); - session.dispose(); - //occupation = +10, age = +40, state = -10, validLicense = 1 - assertEquals(41.0,scorecard.getCalculatedScore()); - //[OCC02, AGE03, VL001, RS001] - assertEquals(4, scorecard.getReasonCodes().size()); - assertTrue(scorecard.getReasonCodes().contains("OCC99")); - assertTrue(scorecard.getReasonCodes().contains("AGE03")); - assertTrue(scorecard.getReasonCodes().contains("VL001")); - assertTrue(scorecard.getReasonCodes().contains("RS001")); - } - - @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()); - } - assertFalse(kbuilder.hasErrors()); - - //BUILD RULEBASE - KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); - kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() ); - - //NEW WORKING MEMORY - StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession(); - FactType scorecardType = kbase.getFactType( "org.drools.scorecards.example","SampleScore" ); - - DroolsScorecard scorecard = (DroolsScorecard) scorecardType.newInstance(); - scorecardType.set(scorecard, "age", 10); - session.insert(scorecard); - session.fireAllRules(); - session.dispose(); - //age = 30, validLicence -1, initialScore = 100; - assertTrue(129 == scorecard.getCalculatedScore()); - //age-reasoncode=AGE02, license-reasoncode=VL002 - assertEquals(2, scorecard.getReasonCodes().size()); - assertTrue(scorecard.getReasonCodes().contains("AGE02")); - assertTrue(scorecard.getReasonCodes().contains("VL002")); - - session = kbase.newStatefulKnowledgeSession(); - scorecard = (DroolsScorecard) scorecardType.newInstance(); - scorecardType.set(scorecard, "age", 0); - scorecardType.set(scorecard, "occupation", "SKYDIVER"); - session.insert(scorecard); - session.fireAllRules(); - session.dispose(); - //occupation = -10, age = +10, validLicense = -1, initialScore = 100; - assertEquals(99.0, scorecard.getCalculatedScore()); - - assertEquals(3, scorecard.getReasonCodes().size()); - //[AGE01, VL002, OCC01] - assertTrue(scorecard.getReasonCodes().contains("AGE01")); - assertTrue(scorecard.getReasonCodes().contains("VL002")); - assertTrue(scorecard.getReasonCodes().contains("OCC01")); - - session = kbase.newStatefulKnowledgeSession(); - scorecard = (DroolsScorecard) 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(); - session.dispose(); - //occupation = +10, age = +40, state = -10, validLicense = 1, initialScore = 100; - assertEquals(141.0,scorecard.getCalculatedScore()); - //[OCC02, AGE03, VL001, RS001] - assertEquals(4, scorecard.getReasonCodes().size()); - assertTrue(scorecard.getReasonCodes().contains("OCC02")); - assertTrue(scorecard.getReasonCodes().contains("AGE03")); - assertTrue(scorecard.getReasonCodes().contains("VL001")); - assertTrue(scorecard.getReasonCodes().contains("RS001")); - } - -} diff --git a/drools-scorecards/src/test/java/org/drools/scorecards/example/Applicant.java b/drools-scorecards/src/test/java/org/drools/scorecards/example/Applicant.java deleted file mode 100644 index 42838be2..00000000 --- a/drools-scorecards/src/test/java/org/drools/scorecards/example/Applicant.java +++ /dev/null @@ -1,59 +0,0 @@ -package org.drools.scorecards.example; - -/** - * Created with IntelliJ IDEA. - * User: vinod - * Date: 13/7/12 - * Time: 7:50 PM - * To change this template use File | Settings | File Templates. - */ -public class Applicant { - int age; - String occupation; - String residenceState; - double totalScore; - boolean validLicense; - - public boolean isValidLicense() { - return validLicense; - } - - public void setValidLicense(boolean validLicense) { - this.validLicense = validLicense; - } - - public Applicant() { - } - - public double getTotalScore() { - return totalScore; - } - - public void setTotalScore(double totalScore) { - this.totalScore = totalScore; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - public String getOccupation() { - return occupation; - } - - public void setOccupation(String occupation) { - this.occupation = occupation; - } - - public String getResidenceState() { - return residenceState; - } - - public void setResidenceState(String residenceState) { - this.residenceState = residenceState; - } -} diff --git a/drools-scorecards/src/test/resources/scoremodel_c.xls b/drools-scorecards/src/test/resources/scoremodel_c.xls deleted file mode 100644 index c6f00837..00000000 Binary files a/drools-scorecards/src/test/resources/scoremodel_c.xls and /dev/null differ diff --git a/drools-scorecards/src/test/resources/scoremodel_errors.xls b/drools-scorecards/src/test/resources/scoremodel_errors.xls deleted file mode 100644 index f9ec6442..00000000 Binary files a/drools-scorecards/src/test/resources/scoremodel_errors.xls and /dev/null differ diff --git a/drools-scorecards/src/test/resources/scoremodel_externalmodel.xls b/drools-scorecards/src/test/resources/scoremodel_externalmodel.xls deleted file mode 100644 index fad78ecd..00000000 Binary files a/drools-scorecards/src/test/resources/scoremodel_externalmodel.xls and /dev/null differ diff --git a/drools-scorecards/src/test/resources/scoremodel_reasoncodes.xls b/drools-scorecards/src/test/resources/scoremodel_reasoncodes.xls deleted file mode 100644 index c0b1d70b..00000000 Binary files a/drools-scorecards/src/test/resources/scoremodel_reasoncodes.xls and /dev/null differ