Search in sources :

Example 11 with InterpolationVariable

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

the class TemplateDropDownManager method getCurrentValueMap.

/**
 * Create a map of Field Values keyed on Field Names used by
 * SuggestionCompletionEngine.getEnums(String, String, Map<String, String>)
 * to drive dependent enumerations.
 * @param context The Context of the cell being edited containing physical
 * coordinate in the data-space.
 */
@Override
public Map<String, String> getCurrentValueMap(Context context) {
    Map<String, String> currentValueMap = new HashMap<String, String>();
    final int iBaseRowIndex = context.getIndex();
    final int iBaseColIndex = context.getColumn();
    // Get variable for the column being edited
    InterpolationVariable[] allVariables = this.model.getInterpolationVariablesList();
    InterpolationVariable baseVariable = allVariables[iBaseColIndex];
    final String baseVariableName = baseVariable.getVarName();
    // Get other variables (and literals) in the same scope as the base variable
    final RuleModelPeerVariableVisitor peerVariableVisitor = new RuleModelPeerVariableVisitor(model, baseVariableName);
    List<RuleModelPeerVariableVisitor.ValueHolder> peerVariables = peerVariableVisitor.getPeerVariables();
    // Add other variables values
    for (RuleModelPeerVariableVisitor.ValueHolder valueHolder : peerVariables) {
        switch(valueHolder.getType()) {
            case TEMPLATE_KEY:
                final int iCol = getVariableColumnIndex(valueHolder.getValue());
                final InterpolationVariable variable = allVariables[iCol];
                final String field = variable.getFactField();
                // The generic class CellValue can have different data-types so
                // we need to convert the cell's value to a String used by the
                // dependent enumerations services
                final CellValue<?> cv = this.data.get(iBaseRowIndex).get(iCol);
                final TemplateDataColumn column = cellValueFactory.makeModelColumn(variable);
                final String value = cellValueFactory.convertToModelCell(column, cv);
                currentValueMap.put(field, value);
                break;
            case VALUE:
                currentValueMap.put(valueHolder.getFieldName(), valueHolder.getValue());
        }
    }
    return currentValueMap;
}
Also used : HashMap(java.util.HashMap) InterpolationVariable(org.drools.workbench.models.datamodel.rule.InterpolationVariable)

Example 12 with InterpolationVariable

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

the class BRLActionColumnPlugin method getDefinedVariables.

boolean getDefinedVariables(RuleModel ruleModel) {
    Map<InterpolationVariable, Integer> ivs = new HashMap<InterpolationVariable, Integer>();
    RuleModelVisitor rmv = new RuleModelVisitor(ivs);
    rmv.visit(ruleModel);
    // Update column and UI
    editingCol.setChildColumns(convertInterpolationVariables(ivs));
    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)

Example 13 with InterpolationVariable

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

the class BRLActionColumnPlugin method convertInterpolationVariables.

private List<BRLActionVariableColumn> convertInterpolationVariables(Map<InterpolationVariable, Integer> ivs) {
    // If there are no variables add a boolean column to specify whether the fragment should apply
    if (ivs.isEmpty()) {
        BRLActionVariableColumn variable = new BRLActionVariableColumn("", DataType.TYPE_BOOLEAN);
        variable.setHeader(editingCol.getHeader());
        variable.setHideColumn(editingCol.isHideColumn());
        List<BRLActionVariableColumn> variables = new ArrayList<BRLActionVariableColumn>();
        variables.add(variable);
        return variables;
    }
    // Convert to columns for use in the Decision Table
    BRLActionVariableColumn[] variables = new BRLActionVariableColumn[ivs.size()];
    for (Map.Entry<InterpolationVariable, Integer> me : ivs.entrySet()) {
        InterpolationVariable iv = me.getKey();
        int index = me.getValue();
        BRLActionVariableColumn variable = new BRLActionVariableColumn(iv.getVarName(), iv.getDataType(), iv.getFactType(), iv.getFactField());
        variable.setHeader(editingCol.getHeader());
        variable.setHideColumn(editingCol.isHideColumn());
        variables[index] = variable;
    }
    // Convert the array into a mutable list (Arrays.toList provides an immutable list)
    List<BRLActionVariableColumn> variableList = new ArrayList<BRLActionVariableColumn>();
    for (BRLActionVariableColumn variable : variables) {
        variableList.add(variable);
    }
    return variableList;
}
Also used : InterpolationVariable(org.drools.workbench.models.datamodel.rule.InterpolationVariable) ArrayList(java.util.ArrayList) BRLActionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn) HashMap(java.util.HashMap) Map(java.util.Map)

Example 14 with InterpolationVariable

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

the class BRLConditionColumnPlugin method getDefinedVariables.

boolean getDefinedVariables(RuleModel ruleModel) {
    Map<InterpolationVariable, Integer> ivs = new HashMap<InterpolationVariable, Integer>();
    RuleModelVisitor rmv = new RuleModelVisitor(ivs);
    rmv.visit(ruleModel);
    // Update column and UI
    editingCol.setChildColumns(convertInterpolationVariables(ivs));
    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)

Example 15 with InterpolationVariable

use of org.drools.workbench.models.datamodel.rule.InterpolationVariable 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)

Aggregations

InterpolationVariable (org.drools.workbench.models.datamodel.rule.InterpolationVariable)31 HashMap (java.util.HashMap)14 RuleModelVisitor (org.drools.workbench.models.datamodel.rule.visitors.RuleModelVisitor)10 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)8 FieldConstraint (org.drools.workbench.models.datamodel.rule.FieldConstraint)8 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)8 ArrayList (java.util.ArrayList)7 HashSet (java.util.HashSet)6 CompositeFieldConstraint (org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint)6 ConnectiveConstraint (org.drools.workbench.models.datamodel.rule.ConnectiveConstraint)6 Map (java.util.Map)4 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 BRLActionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn)3 BRLConditionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionVariableColumn)3 BRLRuleModel (org.drools.workbench.models.guided.dtable.shared.model.BRLRuleModel)3 FreeFormLine (org.drools.workbench.models.datamodel.rule.FreeFormLine)2 BRLVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLVariableColumn)2 DTCellValue52 (org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52)2