Search in sources :

Example 6 with RuleModelVisitor

use of org.drools.workbench.models.datamodel.rule.visitors.RuleModelVisitor in project drools-wb by kiegroup.

the class GuidedDecisionTablePopulater method addIndirectSourceBuildersColumns.

private void addIndirectSourceBuildersColumns() {
    final List<BRLVariableColumn> variableColumns = new ArrayList<BRLVariableColumn>();
    for (GuidedDecisionTableSourceBuilder sb : sourceBuilders) {
        if (sb instanceof GuidedDecisionTableSourceBuilderIndirect) {
            for (BRLVariableColumn variableColumn : ((GuidedDecisionTableSourceBuilderIndirect) sb).getVariableColumns()) {
                variableColumns.add(variableColumn);
            }
        }
    }
    // Convert the DRL to a RuleModel from which we construct BRLFragment columns
    final StringBuilder rule = new StringBuilder();
    if (!(dmo.getPackageName() == null || dmo.getPackageName().isEmpty())) {
        rule.append("package ").append(dmo.getPackageName()).append("\n");
    }
    rule.append("rule 'temp' \n").append("when \n");
    for (GuidedDecisionTableSourceBuilder sb : sourceBuilders) {
        if (sb instanceof GuidedDecisionTableLHSBuilder) {
            rule.append(sb.getResult());
        }
    }
    rule.append("\nthen \n");
    for (GuidedDecisionTableSourceBuilder sb : sourceBuilders) {
        if (sb instanceof GuidedDecisionTableRHSBuilder) {
            rule.append(sb.getResult());
        }
    }
    rule.append("end");
    final RuleModel rm = RuleModelDRLPersistenceImpl.getInstance().unmarshal(rule.toString(), Collections.emptyList(), dmo);
    if (rm.lhs != null) {
        for (IPattern pattern : rm.lhs) {
            final BRLConditionColumn column = new BRLConditionColumn();
            column.getDefinition().add(pattern);
            dtable.getConditions().add(column);
            final Map<InterpolationVariable, Integer> templateKeys = new HashMap<>();
            final RuleModelVisitor rmv = new RuleModelVisitor(templateKeys);
            rmv.visit(pattern);
            final List<InterpolationVariable> ivs = new ArrayList<>(templateKeys.keySet());
            for (BRLVariableColumn variableColumn : variableColumns) {
                final Iterator<InterpolationVariable> ivsIts = ivs.iterator();
                while (ivsIts.hasNext()) {
                    final InterpolationVariable iv = ivsIts.next();
                    if (iv.getVarName().equals(variableColumn.getVarName())) {
                        final BRLConditionVariableColumn source = (BRLConditionVariableColumn) variableColumn;
                        final BRLConditionVariableColumn target = makeBRLConditionVariableColumn(source, iv);
                        column.getChildColumns().add(target);
                        ivsIts.remove();
                    }
                }
            }
            if (column.getChildColumns().isEmpty()) {
                setZeroParameterConditionColumnHeader(column, variableColumns);
            } else {
                setCompositeColumnHeader(column);
            }
        }
    }
    if (rm.rhs != null) {
        for (IAction action : rm.rhs) {
            final BRLActionColumn column = new BRLActionColumn();
            column.getDefinition().add(action);
            dtable.getActionCols().add(column);
            if (action instanceof ActionRetractFact) {
                final ActionRetractFact arf = (ActionRetractFact) action;
                createActionRetractFactChildColumns(arf, column, variableColumns);
            } else {
                createRHSChildColumns(rm, action, column, variableColumns);
            }
            if (column.getChildColumns().isEmpty()) {
                setZeroParameterActionColumnHeader(column, variableColumns);
            } else {
                setCompositeColumnHeader(column);
            }
        }
    }
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) BRLConditionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLConditionColumn) GuidedDecisionTableRHSBuilder(org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableRHSBuilder) IAction(org.drools.workbench.models.datamodel.rule.IAction) InterpolationVariable(org.drools.workbench.models.datamodel.rule.InterpolationVariable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GuidedDecisionTableSourceBuilderIndirect(org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableSourceBuilderIndirect) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) BRLConditionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLConditionVariableColumn) ActionRetractFact(org.drools.workbench.models.datamodel.rule.ActionRetractFact) BRLActionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn) GuidedDecisionTableSourceBuilder(org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableSourceBuilder) RuleModelVisitor(org.drools.workbench.models.datamodel.rule.visitors.RuleModelVisitor) BRLVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLVariableColumn) GuidedDecisionTableLHSBuilder(org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableLHSBuilder)

Example 7 with RuleModelVisitor

use of org.drools.workbench.models.datamodel.rule.visitors.RuleModelVisitor in project drools by kiegroup.

the class GuidedDTDRLPersistence method hasVariables.

private boolean hasVariables(BRLActionColumn column) {
    Map<InterpolationVariable, Integer> ivs = new HashMap<InterpolationVariable, Integer>();
    RuleModel rm = new RuleModel();
    for (IAction action : column.getDefinition()) {
        rm.addRhsItem(action);
    }
    RuleModelVisitor rmv = new RuleModelVisitor(ivs);
    rmv.visit(rm);
    return ivs.size() > 0;
}
Also used : IAction(org.drools.workbench.models.datamodel.rule.IAction) RuleModelVisitor(org.drools.workbench.models.datamodel.rule.visitors.RuleModelVisitor) InterpolationVariable(org.drools.workbench.models.datamodel.rule.InterpolationVariable) HashMap(java.util.HashMap) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) BRLRuleModel(org.drools.workbench.models.guided.dtable.shared.model.BRLRuleModel)

Example 8 with RuleModelVisitor

use of org.drools.workbench.models.datamodel.rule.visitors.RuleModelVisitor in project drools by kiegroup.

the class GuidedDTDRLPersistence method doAction.

private void doAction(List<BaseColumn> allColumns, BRLActionColumn column, List<LabelledAction> actions, TemplateDataProvider rowDataProvider, List<DTCellValue52> row, RuleModel rm) {
    // Check whether the parameter-less BRL fragment needs inclusion
    if (!hasVariables(column)) {
        final BRLActionVariableColumn variableColumn = column.getChildColumns().get(0);
        final int index = allColumns.indexOf(variableColumn);
        final DTCellValue52 dcv = row.get(index);
        if (dcv.getBooleanValue()) {
            for (IAction action : column.getDefinition()) {
                addAction(action, actions);
            }
        }
    } else {
        for (IAction action : column.getDefinition()) {
            boolean addAction = false;
            // Get interpolation variables used by the Action
            Map<InterpolationVariable, Integer> ivs = new HashMap<InterpolationVariable, Integer>();
            RuleModelVisitor rmv = new RuleModelVisitor(action, ivs);
            rmv.visit(action);
            if (ivs.size() == 0) {
                addAction = true;
            } else if (ivs.size() > 0) {
                // Ensure every key has a value and substitute keys for values
                int templateKeyCount = 0;
                for (InterpolationVariable variable : ivs.keySet()) {
                    String value = rowDataProvider.getTemplateKeyValue(variable.getVarName());
                    if (!"".equals(value)) {
                        templateKeyCount++;
                    }
                }
                // Ensure at least one key has a value (FreeFormLines need all values to be provided)
                if (action instanceof FreeFormLine) {
                    addAction = templateKeyCount == ivs.size();
                } else if (templateKeyCount > 0) {
                    addAction = true;
                }
            }
            if (addAction) {
                addAction(action, actions);
            }
        }
    }
}
Also used : FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) IAction(org.drools.workbench.models.datamodel.rule.IAction) RuleModelVisitor(org.drools.workbench.models.datamodel.rule.visitors.RuleModelVisitor) InterpolationVariable(org.drools.workbench.models.datamodel.rule.InterpolationVariable) HashMap(java.util.HashMap) BRLActionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn) DTCellValue52(org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)

Example 9 with RuleModelVisitor

use of org.drools.workbench.models.datamodel.rule.visitors.RuleModelVisitor in project drools-wb by kiegroup.

the class DefaultGuidedDecisionTableLinkManager method hasTemplateKeys.

private boolean hasTemplateKeys(final BRLActionColumn column) {
    final Map<InterpolationVariable, Integer> ivs = new HashMap<>();
    final RuleModel rm = new RuleModel();
    column.getDefinition().forEach(rm::addRhsItem);
    final RuleModelVisitor rmv = new RuleModelVisitor(ivs);
    rmv.visit(rm);
    return ivs.size() > 0;
}
Also used : RuleModelVisitor(org.drools.workbench.models.datamodel.rule.visitors.RuleModelVisitor) InterpolationVariable(org.drools.workbench.models.datamodel.rule.InterpolationVariable) HashMap(java.util.HashMap) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) BRLRuleModel(org.drools.workbench.models.guided.dtable.shared.model.BRLRuleModel)

Example 10 with RuleModelVisitor

use of org.drools.workbench.models.datamodel.rule.visitors.RuleModelVisitor in project drools-wb by kiegroup.

the class GuidedDecisionTablePopulater method createRHSChildColumns.

private void createRHSChildColumns(final RuleModel rm, final IAction action, final BRLActionColumn column, final List<BRLVariableColumn> variableColumns) {
    final Map<InterpolationVariable, Integer> templateKeys = new HashMap<>();
    final RuleModelVisitor rmv = new RuleModelVisitor(rm.lhs, templateKeys);
    rmv.visit(action);
    final List<InterpolationVariable> ivs = new ArrayList<>(templateKeys.keySet());
    for (BRLVariableColumn variableColumn : variableColumns) {
        final Iterator<InterpolationVariable> ivsIts = ivs.iterator();
        while (ivsIts.hasNext()) {
            final InterpolationVariable iv = ivsIts.next();
            if (iv.getVarName().equals(variableColumn.getVarName())) {
                final BRLActionVariableColumn source = (BRLActionVariableColumn) variableColumn;
                final BRLActionVariableColumn target = makeBRLActionVariableColumn(source, iv);
                column.getChildColumns().add(target);
                ivsIts.remove();
            }
        }
    }
}
Also used : RuleModelVisitor(org.drools.workbench.models.datamodel.rule.visitors.RuleModelVisitor) InterpolationVariable(org.drools.workbench.models.datamodel.rule.InterpolationVariable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BRLVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLVariableColumn) BRLActionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn)

Aggregations

HashMap (java.util.HashMap)10 InterpolationVariable (org.drools.workbench.models.datamodel.rule.InterpolationVariable)10 RuleModelVisitor (org.drools.workbench.models.datamodel.rule.visitors.RuleModelVisitor)10 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)4 IAction (org.drools.workbench.models.datamodel.rule.IAction)3 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)3 BRLRuleModel (org.drools.workbench.models.guided.dtable.shared.model.BRLRuleModel)3 ArrayList (java.util.ArrayList)2 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)2 FieldConstraint (org.drools.workbench.models.datamodel.rule.FieldConstraint)2 FreeFormLine (org.drools.workbench.models.datamodel.rule.FreeFormLine)2 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)2 BRLActionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn)2 BRLConditionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionVariableColumn)2 BRLVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLVariableColumn)2 DTCellValue52 (org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52)2 ActionRetractFact (org.drools.workbench.models.datamodel.rule.ActionRetractFact)1 BRLActionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn)1 BRLConditionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionColumn)1 GuidedDecisionTableLHSBuilder (org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableLHSBuilder)1