/** Check correct rendering of string-valued attribute */
 @Test
 public void testRuleAttributeRendering() {
   makeRuleSet();
   makeRuleTable();
   makeRow(11, "C", "A", "G");
   makeRow(12, "Foo", "Foo");
   makeRow(13, "attr == $param", "x");
   makeRow(15, "1", "show()", "foo bar");
   makeRow(16, "2", "list()", "\"10\" group\"");
   listener.finishSheet();
   Package p = listener.getRuleSet();
   DRLOutput dout = new DRLOutput();
   p.renderDRL(dout);
   String drl = dout.getDRL();
   // System.out.println( drl );
   assertTrue(drl.contains("agenda-group \"foo bar\""));
   assertTrue(drl.contains("agenda-group \"10\\\" group\""));
 }
  @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());
  }
  @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());
  }
  @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());
  }
 /** Check correct rendering of package level attributes */
 @Test
 public void testPackageAttributeRendering() {
   makeRuleSet();
   makeAttribute("NO-LOOP", "true");
   makeAttribute("agenda-group", "agroup");
   makeRuleTable();
   makeRow(11, "C", "A", "P");
   makeRow(12, "foo:Foo", "foo");
   makeRow(13, "attr == $param", "x($param)");
   makeRow(15, "1", "1", "100");
   listener.finishSheet();
   Package p = listener.getRuleSet();
   DRLOutput dout = new DRLOutput();
   p.renderDRL(dout);
   String drl = dout.getDRL();
   // System.out.println( drl );
   assertTrue(drl.contains("no-loop true"));
   assertTrue(drl.contains("agenda-group \"agroup\""));
 }
  @Test
  public void testMetadata() {
    makeRuleSet();
    makeRuleTable();
    makeRow(11, "C", "A", "@", "@");
    makeRow(12, "foo: Foo", "foo");
    makeRow(13, "attr == $param", "goaway($param)", "Author($param)", "Version($1-$2)");
    makeRow(15, "1", "1", "J.W.Goethe", "3,14");
    makeRow(16, "2", "2", "", "");
    listener.finishSheet();
    Package p = listener.getRuleSet();
    DRLOutput dout = new DRLOutput();
    p.renderDRL(dout);
    String drl = dout.getDRL();

    assertTrue(drl.contains("@Author(J.W.Goethe)"));
    assertTrue(drl.contains("@Version(3-14)"));
    assertFalse(drl.contains("@Author()"));
    assertFalse(drl.contains("@Version(-)"));
  }
  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());
  }
 /** Must have a code snippet in an action. */
 @Test
 public void testMissingCodeSnippetAction() {
   try {
     makeRuleSet();
     makeRuleTable();
     makeRow(11, "C", "A");
     makeRow(12, "foo: Foo", "Bar()");
     makeRow(13, "attr == $param");
     makeRow(15, "1", "1");
     makeRow(16, "2", "2");
     listener.finishSheet();
     Package p = listener.getRuleSet();
     DRLOutput dout = new DRLOutput();
     p.renderDRL(dout);
     String drl = dout.getDRL();
     System.out.println(drl);
     fail("should have failed");
   } catch (DecisionTableParseException e) {
     String badCell = RuleSheetParserUtil.rc2name(13, 2);
     System.err.println(e.getMessage());
     assertTrue(e.getMessage().contains(badCell));
   }
 }
 /** Duplicate package level attribute */
 @Test
 public void testDuplicatePackageAttribute() {
   try {
     makeRuleSet();
     makeAttribute("agenda-group", "agroup"); // B3, C3
     makeAttribute("agenda-group", "bgroup"); // B3. B4
     makeRuleTable();
     makeRow(11, "C", "A", "P");
     makeRow(12, "Foo", "Foo");
     makeRow(13, "attr == $param", "x");
     makeRow(15, "1", "show()", "10");
     listener.finishSheet();
     Package p = listener.getRuleSet();
     DRLOutput dout = new DRLOutput();
     p.renderDRL(dout);
     String drl = dout.getDRL();
     // System.out.println( drl );
     fail("should have failed");
   } catch (DecisionTableParseException e) {
     System.err.println(e.getMessage());
     assertTrue(e.getMessage().contains("C3, C4"));
   }
 }
Example #10
0
  public String emitDRL(PMML pmml) {
    List<Rule> 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;
  }
Example #11
0
 private void addImports(PMML pmml, Package aPackage) {
   String importsFromDelimitedString =
       ScorecardPMMLUtils.getExtensionValue(
           pmml.getHeader().getExtensions(), PMMLExtensionNames.SCORECARD_IMPORTS);
   if (!(importsFromDelimitedString == null || importsFromDelimitedString.isEmpty())) {
     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);
 }
Example #12
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());
  }
  private Package buildRuleSet() {
    final String defaultPackageName = "rule_table";
    final String rulesetName = getProperties().getSingleProperty(RULESET_TAG, defaultPackageName);

    final Package ruleset = new Package((showPackage) ? rulesetName : null);
    for (Rule rule : this._ruleList) {
      ruleset.addRule(rule);
    }

    final List<Import> importList =
        RuleSheetParserUtil.getImportList(getProperties().getProperty(IMPORT_TAG));
    for (Import import1 : importList) {
      ruleset.addImport(import1);
    }

    final List<Global> variableList =
        RuleSheetParserUtil.getVariableList(getProperties().getProperty(VARIABLES_TAG));
    for (Global global : variableList) {
      ruleset.addVariable(global);
    }

    final List<String> functions = getProperties().getProperty(FUNCTIONS_TAG);
    if (functions != null) {
      for (String function : functions) {
        ruleset.addFunctions(function);
      }
    }

    final List<String> queries = getProperties().getProperty(QUERIES_TAG);
    if (queries != null) {
      for (String query : queries) {
        ruleset.addQueries(query);
      }
    }

    final List<String> declarations = getProperties().getProperty(DECLARES_TAG);
    if (declarations != null) {
      for (String declaration : declarations) {
        ruleset.addDeclaredType(declaration);
      }
    }

    for (Code code : ActionType.ATTRIBUTE_CODE_SET) {
      List<String> values = getProperties().getProperty(code.getColHeader());
      if (values != null) {
        if (values.size() > 1) {
          List<String> cells = getProperties().getPropertyCells(code.getColHeader());
          throw new DecisionTableParseException(
              "Multiple values for " + code.getColHeader() + " in cells " + cells.toString());
        }
        String value = values.get(0);
        switch (code) {
          case SALIENCE:
            try {
              ruleset.setSalience(new Integer(value));
            } catch (NumberFormatException nfe) {
              throw new DecisionTableParseException(
                  "Priority is not an integer literal, in cell "
                      + getProperties().getSinglePropertyCell(code.getColHeader()));
            }
            break;
          case DURATION:
            try {
              ruleset.setDuration(new Long(value));
            } catch (NumberFormatException nfe) {
              throw new DecisionTableParseException(
                  "Duration is not an integer literal, in cell "
                      + getProperties().getSinglePropertyCell(code.getColHeader()));
            }
            break;
          case TIMER:
            ruleset.setTimer(value);
            break;
          case ENABLED:
            ruleset.setEnabled(RuleSheetParserUtil.isStringMeaningTrue(value));
            break;
          case CALENDARS:
            ruleset.setCalendars(value);
            break;
          case NOLOOP:
            ruleset.setNoLoop(RuleSheetParserUtil.isStringMeaningTrue(value));
            break;
          case LOCKONACTIVE:
            ruleset.setLockOnActive(RuleSheetParserUtil.isStringMeaningTrue(value));
            break;
          case AUTOFOCUS:
            ruleset.setAutoFocus(RuleSheetParserUtil.isStringMeaningTrue(value));
            break;
          case ACTIVATIONGROUP:
            ruleset.setActivationGroup(value);
            break;
          case AGENDAGROUP:
            ruleset.setAgendaGroup(value);
            break;
          case RULEFLOWGROUP:
            ruleset.setRuleFlowGroup(value);
            break;
          case DATEEFFECTIVE:
            ruleset.setDateEffective(value);
            break;
          case DATEEXPIRES:
            ruleset.setDateExpires(value);
            break;
        }
      }
    }

    return ruleset;
  }