Search in sources :

Example 1 with Attribute

use of org.dmg.pmml.pmml_4_2.descr.Attribute in project drools by kiegroup.

the class GuidedScoreCardDRLPersistence method createPMMLDocument.

private static PMML createPMMLDocument(final ScoreCardModel model) {
    final Scorecard pmmlScorecard = ScorecardPMMLUtils.createScorecard();
    final Output output = new Output();
    final Characteristics characteristics = new Characteristics();
    final MiningSchema miningSchema = new MiningSchema();
    Extension extension = new Extension();
    extension.setName(PMMLExtensionNames.EXTERNAL_CLASS);
    extension.setValue(model.getFactName());
    pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(extension);
    String agendaGroup = model.getAgendaGroup();
    if (!StringUtils.isEmpty(agendaGroup)) {
        extension = new Extension();
        extension.setName(PMMLExtensionNames.AGENDA_GROUP);
        extension.setValue(agendaGroup);
        pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(extension);
    }
    String ruleFlowGroup = model.getRuleFlowGroup();
    if (!StringUtils.isEmpty(ruleFlowGroup)) {
        extension = new Extension();
        extension.setName(PMMLExtensionNames.RULEFLOW_GROUP);
        extension.setValue(agendaGroup);
        pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(extension);
    }
    extension = new Extension();
    extension.setName(PMMLExtensionNames.MODEL_IMPORTS);
    pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(extension);
    List<String> imports = new ArrayList<String>();
    StringBuilder importBuilder = new StringBuilder();
    for (Import imp : model.getImports().getImports()) {
        if (!imports.contains(imp.getType())) {
            imports.add(imp.getType());
            importBuilder.append(imp.getType()).append(",");
        }
    }
    extension.setValue(importBuilder.toString());
    extension = new Extension();
    extension.setName(ScorecardPMMLExtensionNames.SCORECARD_RESULTANT_SCORE_FIELD);
    extension.setValue(model.getFieldName());
    pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(extension);
    extension = new Extension();
    extension.setName(PMMLExtensionNames.MODEL_PACKAGE);
    String pkgName = model.getPackageName();
    extension.setValue(!(pkgName == null || pkgName.isEmpty()) ? pkgName : null);
    pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(extension);
    final String modelName = convertToJavaIdentifier(model.getName());
    pmmlScorecard.setModelName(modelName);
    pmmlScorecard.setInitialScore(model.getInitialScore());
    pmmlScorecard.setUseReasonCodes(model.isUseReasonCodes());
    if (model.isUseReasonCodes()) {
        pmmlScorecard.setBaselineScore(model.getBaselineScore());
        pmmlScorecard.setReasonCodeAlgorithm(model.getReasonCodesAlgorithm());
    }
    for (final org.drools.workbench.models.guided.scorecard.shared.Characteristic characteristic : model.getCharacteristics()) {
        final Characteristic _characteristic = new Characteristic();
        characteristics.getCharacteristics().add(_characteristic);
        extension = new Extension();
        extension.setName(PMMLExtensionNames.EXTERNAL_CLASS);
        extension.setValue(characteristic.getFact());
        _characteristic.getExtensions().add(extension);
        extension = new Extension();
        extension.setName(ScorecardPMMLExtensionNames.CHARACTERTISTIC_DATATYPE);
        if ("string".equalsIgnoreCase(characteristic.getDataType())) {
            extension.setValue(XLSKeywords.DATATYPE_TEXT);
        } else if ("int".equalsIgnoreCase(characteristic.getDataType()) || "double".equalsIgnoreCase(characteristic.getDataType())) {
            extension.setValue(XLSKeywords.DATATYPE_NUMBER);
        } else if ("boolean".equalsIgnoreCase(characteristic.getDataType())) {
            extension.setValue(XLSKeywords.DATATYPE_BOOLEAN);
        } else {
            System.out.println(">>>> Found unknown data type :: " + characteristic.getDataType());
        }
        _characteristic.getExtensions().add(extension);
        _characteristic.setBaselineScore(characteristic.getBaselineScore());
        if (model.isUseReasonCodes()) {
            _characteristic.setReasonCode(characteristic.getReasonCode());
        }
        _characteristic.setName(characteristic.getName());
        final MiningField miningField = new MiningField();
        miningField.setName(characteristic.getField());
        miningField.setUsageType(FIELDUSAGETYPE.ACTIVE);
        miningField.setInvalidValueTreatment(INVALIDVALUETREATMENTMETHOD.RETURN_INVALID);
        miningSchema.getMiningFields().add(miningField);
        extension = new Extension();
        extension.setName(PMMLExtensionNames.EXTERNAL_CLASS);
        extension.setValue(characteristic.getFact());
        miningField.getExtensions().add(extension);
        for (final org.drools.workbench.models.guided.scorecard.shared.Attribute attribute : characteristic.getAttributes()) {
            final Attribute _attribute = new Attribute();
            _characteristic.getAttributes().add(_attribute);
            extension = new Extension();
            extension.setName(ScorecardPMMLExtensionNames.CHARACTERTISTIC_FIELD);
            extension.setValue(characteristic.getField());
            _attribute.getExtensions().add(extension);
            if (model.isUseReasonCodes()) {
                _attribute.setReasonCode(attribute.getReasonCode());
            }
            _attribute.setPartialScore(attribute.getPartialScore());
            final String operator = attribute.getOperator();
            final String dataType = characteristic.getDataType();
            String predicateResolver;
            if ("boolean".equalsIgnoreCase(dataType)) {
                predicateResolver = operator.toUpperCase();
            } else if ("String".equalsIgnoreCase(dataType)) {
                if (operator.contains("=")) {
                    predicateResolver = operator + attribute.getValue();
                } else {
                    predicateResolver = attribute.getValue() + ",";
                }
            } else {
                if (NUMERIC_OPERATORS.contains(operator)) {
                    predicateResolver = operator + " " + attribute.getValue();
                } else {
                    predicateResolver = attribute.getValue().replace(",", "-");
                }
            }
            extension = new Extension();
            extension.setName("predicateResolver");
            extension.setValue(predicateResolver);
            _attribute.getExtensions().add(extension);
        }
    }
    pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(miningSchema);
    pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(output);
    pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(characteristics);
    return new ScorecardPMMLGenerator().generateDocument(pmmlScorecard);
}
Also used : MiningField(org.dmg.pmml.pmml_4_2.descr.MiningField) Import(org.kie.soup.project.datamodel.imports.Import) Attribute(org.dmg.pmml.pmml_4_2.descr.Attribute) Characteristic(org.dmg.pmml.pmml_4_2.descr.Characteristic) ArrayList(java.util.ArrayList) Extension(org.dmg.pmml.pmml_4_2.descr.Extension) MiningSchema(org.dmg.pmml.pmml_4_2.descr.MiningSchema) Characteristics(org.dmg.pmml.pmml_4_2.descr.Characteristics) Output(org.dmg.pmml.pmml_4_2.descr.Output) ScorecardPMMLGenerator(org.drools.scorecards.pmml.ScorecardPMMLGenerator) Scorecard(org.dmg.pmml.pmml_4_2.descr.Scorecard)

Example 2 with Attribute

use of org.dmg.pmml.pmml_4_2.descr.Attribute in project drools by kiegroup.

the class ExcelScorecardValidator method validateReasonCodes.

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"));
                        }
                    }
                }
            }
        }
    }
}
Also used : Characteristics(org.dmg.pmml.pmml_4_2.descr.Characteristics) Attribute(org.dmg.pmml.pmml_4_2.descr.Attribute) Characteristic(org.dmg.pmml.pmml_4_2.descr.Characteristic) ScorecardError(org.drools.scorecards.ScorecardError)

Example 3 with Attribute

use of org.dmg.pmml.pmml_4_2.descr.Attribute in project drools by kiegroup.

the class ExcelScorecardValidator method checkForInvalidDataTypes.

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(), ScorecardPMMLExtensionNames.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"));
                        }
                    }
                }
            }
        }
    }
}
Also used : Characteristics(org.dmg.pmml.pmml_4_2.descr.Characteristics) Attribute(org.dmg.pmml.pmml_4_2.descr.Attribute) Characteristic(org.dmg.pmml.pmml_4_2.descr.Characteristic) ScorecardError(org.drools.scorecards.ScorecardError)

Example 4 with Attribute

use of org.dmg.pmml.pmml_4_2.descr.Attribute in project drools by kiegroup.

the class ExcelScorecardValidator method validateWeights.

private void validateWeights() {
    for (Object obj : scorecard.getExtensionsAndCharacteristicsAndMiningSchemas()) {
        if (obj instanceof Characteristics) {
            Characteristics characteristics = (Characteristics) obj;
            for (Characteristic characteristic : characteristics.getCharacteristics()) {
                for (Attribute attribute : characteristic.getAttributes()) {
                    String newCellRef = createDataTypeCellRef(ScorecardPMMLUtils.getExtensionValue(attribute.getExtensions(), "cellRef"), 2);
                    String weight = ScorecardPMMLUtils.getExtensionValue(attribute.getExtensions(), ScorecardPMMLExtensionNames.CHARACTERTISTIC_WEIGHT);
                    if (StringUtils.isEmpty(weight) || !isDouble(weight)) {
                        parseErrors.add(new ScorecardError(newCellRef, "Attribute is missing weight or specified weight is not a double."));
                    }
                }
            }
        }
    }
}
Also used : Characteristics(org.dmg.pmml.pmml_4_2.descr.Characteristics) Attribute(org.dmg.pmml.pmml_4_2.descr.Attribute) Characteristic(org.dmg.pmml.pmml_4_2.descr.Characteristic) ScorecardError(org.drools.scorecards.ScorecardError)

Example 5 with Attribute

use of org.dmg.pmml.pmml_4_2.descr.Attribute in project drools by kiegroup.

the class ScorecardReasonCodeTest method testReasonCodes.

@Test
public void testReasonCodes() throws Exception {
    final ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES);
    boolean compileResult = scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_reasoncodes.xls"));
    if (!compileResult) {
        assertErrors(scorecardCompiler);
    }
    final PMML pmmlDocument = scorecardCompiler.getPMMLDocument();
    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();
}
Also used : Characteristics(org.dmg.pmml.pmml_4_2.descr.Characteristics) Attribute(org.dmg.pmml.pmml_4_2.descr.Attribute) Characteristic(org.dmg.pmml.pmml_4_2.descr.Characteristic) PMML(org.dmg.pmml.pmml_4_2.descr.PMML) Scorecard(org.dmg.pmml.pmml_4_2.descr.Scorecard) Test(org.junit.Test)

Aggregations

Attribute (org.dmg.pmml.pmml_4_2.descr.Attribute)5 Characteristic (org.dmg.pmml.pmml_4_2.descr.Characteristic)5 Characteristics (org.dmg.pmml.pmml_4_2.descr.Characteristics)5 ScorecardError (org.drools.scorecards.ScorecardError)3 Scorecard (org.dmg.pmml.pmml_4_2.descr.Scorecard)2 ArrayList (java.util.ArrayList)1 Extension (org.dmg.pmml.pmml_4_2.descr.Extension)1 MiningField (org.dmg.pmml.pmml_4_2.descr.MiningField)1 MiningSchema (org.dmg.pmml.pmml_4_2.descr.MiningSchema)1 Output (org.dmg.pmml.pmml_4_2.descr.Output)1 PMML (org.dmg.pmml.pmml_4_2.descr.PMML)1 ScorecardPMMLGenerator (org.drools.scorecards.pmml.ScorecardPMMLGenerator)1 Test (org.junit.Test)1 Import (org.kie.soup.project.datamodel.imports.Import)1