Search in sources :

Example 1 with DecisionTableParseException

use of org.drools.template.parser.DecisionTableParseException 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 DecisionTableParseException

use of org.drools.template.parser.DecisionTableParseException in project drools by kiegroup.

the class DefaultRuleSheetListener method createNewRuleForRow.

private Rule createNewRuleForRow(final int row, final String headCell, final String ruleCell) {
    Integer salience = null;
    if (this._currentSequentialFlag) {
        salience = _currentSalience--;
        if (salience < _minSalienceTag) {
            throw new DecisionTableParseException("Salience less than the minimum specified on row: " + 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;
}
Also used : DecisionTableParseException(org.drools.template.parser.DecisionTableParseException) Rule(org.drools.template.model.Rule)

Example 3 with DecisionTableParseException

use of org.drools.template.parser.DecisionTableParseException in project drools by kiegroup.

the class RuleSheetParserUtil method getVariableList.

/**
 * Create a list of Global model objects from cell contents.
 * @param variableCella The cells containing text for all the global variables to set.
 * @return A list of Variable classes, which can be added to the ruleset.
 */
public static List<Global> getVariableList(final List<String> variableCells) {
    final List<Global> variableList = new ArrayList<Global>();
    if (variableCells == null)
        return variableList;
    for (String variableCell : variableCells) {
        final StringTokenizer tokens = new StringTokenizer(variableCell, ",");
        while (tokens.hasMoreTokens()) {
            final String token = tokens.nextToken();
            final Global vars = new Global();
            final StringTokenizer paramTokens = new StringTokenizer(token, " ");
            vars.setClassName(paramTokens.nextToken());
            if (!paramTokens.hasMoreTokens()) {
                throw new DecisionTableParseException("The format for global variables is incorrect. " + "It should be: [Class name, Class otherName]. But it was: [" + variableCell + "]");
            }
            vars.setIdentifier(paramTokens.nextToken());
            variableList.add(vars);
        }
    }
    return variableList;
}
Also used : StringTokenizer(java.util.StringTokenizer) DecisionTableParseException(org.drools.template.parser.DecisionTableParseException) ArrayList(java.util.ArrayList) Global(org.drools.template.model.Global)

Example 4 with DecisionTableParseException

use of org.drools.template.parser.DecisionTableParseException in project drools by kiegroup.

the class ExcelParser method tryToReadCachedValue.

private String tryToReadCachedValue(Cell cell) {
    DataFormatter formatter = new DataFormatter(Locale.ENGLISH);
    String cachedValue;
    switch(cell.getCachedFormulaResultType()) {
        case Cell.CELL_TYPE_NUMERIC:
            double num = cell.getNumericCellValue();
            if (num - Math.round(num) != 0) {
                cachedValue = String.valueOf(num);
            } else {
                cachedValue = formatter.formatCellValue(cell);
            }
            break;
        case Cell.CELL_TYPE_STRING:
            cachedValue = cell.getStringCellValue();
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            cachedValue = String.valueOf(cell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_ERROR:
            cachedValue = String.valueOf(cell.getErrorCellValue());
            break;
        default:
            throw new DecisionTableParseException(format("Can't read cached value for cell[row=%d, col=%d, value=%s]!", cell.getRowIndex(), cell.getColumnIndex(), cell));
    }
    return cachedValue;
}
Also used : DecisionTableParseException(org.drools.template.parser.DecisionTableParseException) DataFormatter(org.apache.poi.ss.usermodel.DataFormatter)

Example 5 with DecisionTableParseException

use of org.drools.template.parser.DecisionTableParseException in project drools by kiegroup.

the class DefaultRuleSheetListener method nextDataCell.

private void nextDataCell(final int row, final int column, final String value) {
    final ActionType actionType = getActionForColumn(row, column);
    if (row - this._ruleRow > 1) {
        // Encountered a row gap from the last rule.
        // This is not part of the ruleset.
        finishRuleTable();
        processNonRuleCell(row, column, value);
        return;
    }
    if (row > this._ruleRow) {
        // In a new row/rule
        String headCell = RuleSheetParserUtil.rc2name(this._ruleStartRow, this._ruleStartColumn);
        String ruleCell = RuleSheetParserUtil.rc2name(row, this._ruleStartColumn);
        this._currentRule = createNewRuleForRow(row, headCell, ruleCell);
        this._ruleList.add(this._currentRule);
        this._ruleRow++;
    }
    switch(actionType.getCode()) {
        case CONDITION:
        case ACTION:
        case METADATA:
            if (actionType.getSourceBuilder() == null) {
                throw new DecisionTableParseException("Data cell " + RuleSheetParserUtil.rc2name(row, column) + " has an empty column header.");
            }
            actionType.addCellValue(row, column, value, _currentEscapeQuotesFlag);
            break;
        case SALIENCE:
            // Only if rule set is not sequential!
            if (!this._currentSequentialFlag) {
                if (value.startsWith("(") && value.endsWith(")")) {
                    this._currentRule.setSalience(value);
                } else {
                    try {
                        this._currentRule.setSalience(new Integer(value));
                    } catch (NumberFormatException nfe) {
                        throw new DecisionTableParseException("Priority is not an integer literal, in cell " + RuleSheetParserUtil.rc2name(row, column));
                    }
                }
            }
            break;
        case NAME:
            this._currentRule.setName(value);
            break;
        case DESCRIPTION:
            this._currentRule.setDescription(value);
            break;
        case ACTIVATIONGROUP:
            this._currentRule.setActivationGroup(value);
            break;
        case AGENDAGROUP:
            this._currentRule.setAgendaGroup(value);
            break;
        case RULEFLOWGROUP:
            this._currentRule.setRuleFlowGroup(value);
            break;
        case NOLOOP:
            this._currentRule.setNoLoop(RuleSheetParserUtil.isStringMeaningTrue(value));
            break;
        case LOCKONACTIVE:
            this._currentRule.setLockOnActive(RuleSheetParserUtil.isStringMeaningTrue(value));
            break;
        case AUTOFOCUS:
            this._currentRule.setAutoFocus(RuleSheetParserUtil.isStringMeaningTrue(value));
            break;
        case DURATION:
            try {
                this._currentRule.setDuration(new Long(value));
            } catch (NumberFormatException nfe) {
                throw new DecisionTableParseException("Duration is not an integer literal, in cell " + RuleSheetParserUtil.rc2name(row, column));
            }
            break;
        case TIMER:
            this._currentRule.setTimer(value);
            break;
        case ENABLED:
            this._currentRule.setEnabled(RuleSheetParserUtil.isStringMeaningTrue(value));
            break;
        case CALENDARS:
            this._currentRule.setCalendars(value);
            break;
        case DATEEFFECTIVE:
            this._currentRule.setDateEffective(value);
            break;
        case DATEEXPIRES:
            this._currentRule.setDateExpires(value);
            break;
    }
}
Also used : DecisionTableParseException(org.drools.template.parser.DecisionTableParseException)

Aggregations

DecisionTableParseException (org.drools.template.parser.DecisionTableParseException)13 Test (org.junit.Test)4 Package (org.drools.template.model.Package)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 DRLOutput (org.drools.template.model.DRLOutput)2 Global (org.drools.template.model.Global)2 Rule (org.drools.template.model.Rule)2 SnippetBuilder (org.drools.template.model.SnippetBuilder)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 StringTokenizer (java.util.StringTokenizer)1 Consumer (java.util.function.Consumer)1 Event (javax.enterprise.event.Event)1 IOUtils (org.apache.commons.io.IOUtils)1