Ejemplo n.º 1
0
  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);
  }
Ejemplo n.º 2
0
  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());
  }
Ejemplo n.º 3
0
  protected void createSummationRules(List<Rule> 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);
  }
Ejemplo n.º 4
0
 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();
 }
Ejemplo n.º 5
0
  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(")");
  }