예제 #1
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;
 }
예제 #2
0
  private Rule createNewRuleForRow(final int row, final String headCell, final String ruleCell) {
    Integer salience = null;
    if (this._currentSequentialFlag) {
      salience = new Integer(Rule.calcSalience(row));
    }
    final int spreadsheetRow = row + 1;
    final String name = this._currentRulePrefix + "_" + spreadsheetRow;
    final Rule rule = new Rule(name, salience, spreadsheetRow);
    rule.setComment(" rule values at " + ruleCell + ", header at " + headCell);

    return rule;
  }
예제 #3
0
  private void doComplexWorksheetMissingConditions() throws IOException {
    final InputStream stream =
        RuleWorksheetParseTest.class.getResourceAsStream("/data/ComplexWorkbook.xls");
    final RuleSheetListener listener = getRuleSheetListener(stream);

    final Package ruleset = listener.getRuleSet();
    assertEquals(6, ruleset.getRules().size());
    assertEquals(0, ruleset.getImports().size());

    Rule rule = (Rule) ruleset.getRules().get(0);
    assertEquals(3, rule.getConditions().size());
    assertEquals(2, rule.getConsequences().size());
    final Consequence cons = (Consequence) rule.getConsequences().get(1);
    assertEquals("myObject.setIsValid(1, 2)", cons.getSnippet());
    final Condition con = (Condition) rule.getConditions().get(2);
    assertEquals("myObject.size() < $3.00", con.getSnippet());

    rule = (Rule) ruleset.getRules().get(4);

    // this should have less conditions
    assertEquals(1, rule.getConditions().size());

    rule = (Rule) ruleset.getRules().get(5);
    assertEquals(2, rule.getConditions().size());
    assertEquals(1, rule.getConsequences().size());
  }
예제 #4
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);
  }
예제 #5
0
  @Test
  public void testNumericDisabled() throws Exception {
    // DROOLS-1378
    final InputStream stream =
        RuleWorksheetParseTest.class.getResourceAsStream("/data/NumericDisabled.xls");
    final RuleSheetListener listener = getRuleSheetListener(stream);

    final Package ruleset = listener.getRuleSet();
    assertNotNull(ruleset);
    DRLOutput dout = new DRLOutput();
    ruleset.renderDRL(dout);
    String drl = dout.getDRL();
    System.out.println(drl);

    // check rules
    Rule rule = (Rule) ruleset.getRules().get(0);
    Condition cond = (Condition) rule.getConditions().get(0);
    assertEquals("Cheese(price == 6600)", cond.getSnippet());
  }
예제 #6
0
  @Test
  public void testQuoteEscapingEnabled() throws Exception {
    final InputStream stream =
        RuleWorksheetParseTest.class.getResourceAsStream("/data/QuoteEscapeEnabledWorkbook.xls");
    final RuleSheetListener listener = getRuleSheetListener(stream);

    final Package ruleset = listener.getRuleSet();
    assertNotNull(ruleset);
    DRLOutput dout = new DRLOutput();
    ruleset.renderDRL(dout);
    String drl = dout.getDRL();
    System.out.println(drl);

    // check rules
    Rule rule = (Rule) ruleset.getRules().get(0);
    Condition cond = (Condition) rule.getConditions().get(0);
    assertEquals(
        "Foo(myObject.getColour().equals(red), myObject.size () > 12\\\")", cond.getSnippet());
  }
예제 #7
0
 /**
  * This makes sure that the rules have all their components added. As when there are
  * merged/spanned cells, they may be left out.
  */
 private void flushRule() {
   for (Iterator<SourceBuilder> iter = sourceBuilders.iterator(); iter.hasNext(); ) {
     SourceBuilder src = iter.next();
     if (src.hasValues()) {
       switch (src.getActionTypeCode()) {
         case CONDITION:
           Condition cond = new Condition();
           cond.setSnippet(src.getResult());
           _currentRule.addCondition(cond);
           break;
         case ACTION:
           Consequence cons = new Consequence();
           cons.setSnippet(src.getResult());
           _currentRule.addConsequence(cons);
           break;
         case METADATA:
           _currentRule.addMetadata(src.getResult());
           break;
       }
       src.clearValues();
     }
   }
 }
예제 #8
0
  @Test
  public void testWorkbookParse() throws Exception {
    final InputStream stream =
        RuleWorksheetParseTest.class.getResourceAsStream("/data/BasicWorkbook.xls");
    final RuleSheetListener listener = getRuleSheetListener(stream);

    final Package ruleset = listener.getRuleSet();
    assertNotNull(ruleset);

    final Rule firstRule = (Rule) ruleset.getRules().get(0);
    assertNotNull(firstRule.getSalience());
    assertTrue(Integer.parseInt(firstRule.getSalience()) > 0);

    // System.out.println(ruleset.toXML());

    assertEquals("myruleset", ruleset.getName());
    assertEquals(3, ruleset.getImports().size());
    assertEquals(6, ruleset.getRules().size());

    // check imports
    Import imp = (Import) ruleset.getImports().get(0);
    assertEquals("blah.class1", imp.getClassName());
    imp = (Import) ruleset.getImports().get(1);
    assertEquals("blah.class2", imp.getClassName());
    imp = (Import) ruleset.getImports().get(2);
    assertEquals("lah.di.dah", imp.getClassName());

    // check rules
    Rule rule = (Rule) ruleset.getRules().get(0);
    Condition cond = (Condition) rule.getConditions().get(0);
    assertEquals(
        "Foo(myObject.getColour().equals(red), myObject.size () > 12\\\")", cond.getSnippet());

    Consequence cons = (Consequence) rule.getConsequences().get(0);
    assertNotNull(cons);
    assertEquals("myObject.setIsValid(Y);", cons.getSnippet());

    rule = (Rule) ruleset.getRules().get(5);
    cond = (Condition) rule.getConditions().get(1);
    assertEquals("myObject.size () > 7", cond.getSnippet());
    cons = (Consequence) rule.getConsequences().get(0);
    assertEquals("myObject.setIsValid(10-Jul-1974)", cons.getSnippet());
  }
예제 #9
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);
  }
예제 #10
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);
    }
  }