Search in sources :

Example 1 with Package

use of org.drools.template.model.Package in project drools by kiegroup.

the class DefaultRuleSheetListener method buildRuleSet.

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;
}
Also used : Import(org.drools.template.model.Import) DecisionTableParseException(org.drools.template.parser.DecisionTableParseException) Code(org.drools.decisiontable.parser.ActionType.Code) Global(org.drools.template.model.Global) Package(org.drools.template.model.Package) Rule(org.drools.template.model.Rule)

Example 2 with Package

use of org.drools.template.model.Package in project drools by kiegroup.

the class RuleWorksheetParseTest method testQuoteEscapingDisabled.

@Test
public void testQuoteEscapingDisabled() throws Exception {
    final InputStream stream = RuleWorksheetParseTest.class.getResourceAsStream("/data/QuoteEscapeDisabledWorkbook.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());
    rule = ruleset.getRules().get(1);
    cond = rule.getConditions().get(0);
    assertEquals("Foo(myObject.getColour().equals(blue), myObject.size () > 12\")", cond.getSnippet());
}
Also used : Condition(org.drools.template.model.Condition) DRLOutput(org.drools.template.model.DRLOutput) InputStream(java.io.InputStream) Package(org.drools.template.model.Package) Rule(org.drools.template.model.Rule) Test(org.junit.Test)

Example 3 with Package

use of org.drools.template.model.Package in project drools by kiegroup.

the class RuleWorksheetParseTest method testSalienceRange.

@Test
public void testSalienceRange() throws Exception {
    // DROOLS-1225
    final InputStream stream = RuleWorksheetParseTest.class.getResourceAsStream("/data/SalienceRangeWorkbook.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
    List<Rule> rules = ruleset.getRules();
    assertEquals("10000", rules.get(0).getSalience());
    assertEquals("9999", rules.get(1).getSalience());
}
Also used : DRLOutput(org.drools.template.model.DRLOutput) InputStream(java.io.InputStream) Package(org.drools.template.model.Package) Rule(org.drools.template.model.Rule) Test(org.junit.Test)

Example 4 with Package

use of org.drools.template.model.Package in project drools by kiegroup.

the class RuleWorksheetParseTest method testQuoteEscapingEnabled.

@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 = ruleset.getRules().get(0);
    Condition cond = rule.getConditions().get(0);
    assertEquals("Foo(myObject.getColour().equals(red), myObject.size () > 12\\\")", cond.getSnippet());
}
Also used : Condition(org.drools.template.model.Condition) DRLOutput(org.drools.template.model.DRLOutput) InputStream(java.io.InputStream) Package(org.drools.template.model.Package) Rule(org.drools.template.model.Rule) Test(org.junit.Test)

Example 5 with Package

use of org.drools.template.model.Package in project drools by kiegroup.

the class RuleWorksheetParseTest method testRuleAttributeRendering.

/**
 * 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\""));
}
Also used : DRLOutput(org.drools.template.model.DRLOutput) Package(org.drools.template.model.Package) Test(org.junit.Test)

Aggregations

Package (org.drools.template.model.Package)14 Test (org.junit.Test)11 DRLOutput (org.drools.template.model.DRLOutput)10 InputStream (java.io.InputStream)7 Rule (org.drools.template.model.Rule)7 Condition (org.drools.template.model.Condition)5 DecisionTableParseException (org.drools.template.parser.DecisionTableParseException)3 Consequence (org.drools.template.model.Consequence)2 Import (org.drools.template.model.Import)2 Code (org.drools.decisiontable.parser.ActionType.Code)1 Global (org.drools.template.model.Global)1