Exemple #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);
  }
Exemple #2
0
 protected List<Rule> createRuleList(PMML pmmlDocument) {
   List<Rule> ruleList = new ArrayList<Rule>();
   for (Object obj : pmmlDocument.getAssociationModelsAndBaselineModelsAndClusteringModels()) {
     if (obj instanceof Scorecard) {
       Scorecard scorecard = (Scorecard) obj;
       Characteristics characteristics = getCharacteristicsFromScorecard(scorecard);
       createInitialRule(ruleList, scorecard);
       for (org.dmg.pmml.pmml_4_1.descr.Characteristic c : characteristics.getCharacteristics()) {
         int attributePosition = 0;
         for (org.dmg.pmml.pmml_4_1.descr.Attribute scoreAttribute : c.getAttributes()) {
           String name =
               formRuleName(
                   pmmlDocument, scorecard.getModelName().replaceAll(" ", ""), c, scoreAttribute);
           Rule rule = new Rule(name, 99, 1);
           String desc =
               ScorecardPMMLUtils.getExtensionValue(scoreAttribute.getExtensions(), "description");
           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;
 }
Exemple #3
0
 protected Characteristics getCharacteristicsFromScorecard(Scorecard scorecard) {
   for (Object obj : scorecard.getExtensionsAndCharacteristicsAndMiningSchemas()) {
     if (obj instanceof Characteristics) {
       return (Characteristics) obj;
     }
   }
   return null;
 }
Exemple #4
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);
  }
Exemple #5
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());
  }
Exemple #6
0
  protected void createInitialRule(List<Rule> 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");

      Condition condition = createInitialRuleCondition(scorecard, objectClass);
      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())) {
            // TODO: ReasonCode Algorithm
            consequence.setSnippet(
                "//$sc.setReasonCodeAlgorithm(DroolsScorecard.REASON_CODE_ALGORITHM_POINTSABOVE);");
          } else if ("pointsBelow".equalsIgnoreCase(scorecard.getReasonCodeAlgorithm())) {
            consequence.setSnippet(
                "//$sc.setReasonCodeAlgorithm(DroolsScorecard.REASON_CODE_ALGORITHM_POINTSBELOW);");
          }
          rule.addConsequence(consequence);
        }
      }
      ruleList.add(rule);
    }
  }