Search in sources :

Example 1 with GuidedDecisionTableSourceBuilderIndirect

use of org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableSourceBuilderIndirect in project drools-wb by kiegroup.

the class GuidedDecisionTablePopulater method addIndirectSourceBuildersColumns.

private void addIndirectSourceBuildersColumns() {
    // Extract BRLVariableColumn and SourceBuilders for respective sections of the rule
    final List<BRLVariableColumn> variableColumns = new ArrayList<>();
    final List<GuidedDecisionTableLHSBuilder> lhsBuilders = new ArrayList<>();
    final List<GuidedDecisionTableRHSBuilder> rhsBuilders = new ArrayList<>();
    for (GuidedDecisionTableSourceBuilder sb : sourceBuilders) {
        if (sb instanceof GuidedDecisionTableSourceBuilderIndirect) {
            for (BRLVariableColumn variableColumn : ((GuidedDecisionTableSourceBuilderIndirect) sb).getVariableColumns()) {
                variableColumns.add(variableColumn);
            }
        }
        if (sb instanceof GuidedDecisionTableLHSBuilder) {
            lhsBuilders.add((GuidedDecisionTableLHSBuilder) sb);
        }
        if (sb instanceof GuidedDecisionTableRHSBuilder) {
            rhsBuilders.add((GuidedDecisionTableRHSBuilder) sb);
        }
    }
    // -- PROCESS LHS PATTERNS
    // For each GuidedDecisionTableLHSBuilder gather IPatterns generated
    // from each accumulative iteration and convert to RuleModel
    int previousPatternCount = 0;
    for (int i = 0; i < lhsBuilders.size(); i++) {
        // Construct DRL for this and previous builders
        final String rule = assembleRuleDRL(i, lhsBuilders, Collections.emptyList());
        final RuleModel rm = RuleModelDRLPersistenceImpl.getInstance().unmarshal(rule, Collections.emptyList(), dmo);
        if (Objects.nonNull(rm.lhs)) {
            final Map<IPattern, Map<InterpolationVariable, Integer>> patternTemplateKeys = new HashMap<>();
            final BRLConditionColumn column = new BRLConditionColumn();
            dtable.getConditions().add(column);
            // Extract Template Keys for Patterns in iteration
            boolean allPatternsHaveTemplateKeys = true;
            for (int iPattern = previousPatternCount; iPattern < rm.lhs.length; iPattern++) {
                final IPattern pattern = rm.lhs[iPattern];
                final Map<InterpolationVariable, Integer> templateKeys = new HashMap<>();
                final RuleModelVisitor rmv = new RuleModelVisitor(templateKeys);
                rmv.visit(pattern);
                patternTemplateKeys.put(pattern, templateKeys);
                if (templateKeys.isEmpty()) {
                    allPatternsHaveTemplateKeys = false;
                }
            }
            // If ALL patterns in the iteration have Template keys we can use IPattern otherwise fallback to FreeFormLine
            if (allPatternsHaveTemplateKeys) {
                for (int iPattern = previousPatternCount; iPattern < rm.lhs.length; iPattern++) {
                    final IPattern pattern = rm.lhs[iPattern];
                    column.getDefinition().add(pattern);
                }
            } else {
                final FreeFormLine ffl = new FreeFormLine();
                ffl.setText(lhsBuilders.get(i).getResult());
                column.setDefinition(Collections.singletonList(ffl));
            }
            // Copy variables to column
            for (int iPattern = previousPatternCount; iPattern < rm.lhs.length; iPattern++) {
                final IPattern pattern = rm.lhs[iPattern];
                final Map<InterpolationVariable, Integer> templateKeys = patternTemplateKeys.get(pattern);
                if (Objects.nonNull(templateKeys)) {
                    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 (Objects.equals(iv.getVarName(), variableColumn.getVarName())) {
                                final BRLConditionVariableColumn source = (BRLConditionVariableColumn) variableColumn;
                                final BRLConditionVariableColumn target = makeBRLConditionVariableColumn(source, iv);
                                column.getChildColumns().add(target);
                                ivsIts.remove();
                            }
                        }
                    }
                }
            }
            // Give the column a header
            if (column.getChildColumns().isEmpty()) {
                setZeroParameterConditionColumnHeader(column, variableColumns);
            } else {
                setCompositeColumnHeader(column);
            }
        }
        previousPatternCount = rm.lhs.length;
    }
    // -- PROCESS RHS ACTIONS
    final String rule = assembleRuleDRL(lhsBuilders.size() - 1, lhsBuilders, rhsBuilders);
    final RuleModel rm = RuleModelDRLPersistenceImpl.getInstance().unmarshal(rule, Collections.emptyList(), dmo, true);
    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) HashMap(java.util.HashMap) InterpolationVariable(org.drools.workbench.models.datamodel.rule.InterpolationVariable) ArrayList(java.util.ArrayList) GuidedDecisionTableSourceBuilderIndirect(org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableSourceBuilderIndirect) 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) RuleModelVisitor(org.drools.workbench.models.datamodel.rule.visitors.RuleModelVisitor) GuidedDecisionTableRHSBuilder(org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableRHSBuilder) IAction(org.drools.workbench.models.datamodel.rule.IAction) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) GuidedDecisionTableSourceBuilder(org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableSourceBuilder) BRLVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLVariableColumn) GuidedDecisionTableLHSBuilder(org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableLHSBuilder) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with GuidedDecisionTableSourceBuilderIndirect

use of org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableSourceBuilderIndirect in project drools-wb by kiegroup.

the class GuidedDecisionTablePopulater method getOrderedValueBuilders.

private List<ParameterizedValueBuilder> getOrderedValueBuilders() {
    // Get ordered list of ParameterizedValueBuilder for all GuidedDecisionTableSourceBuilderIndirect instances
    // An ordered list of ParameterizedValueBuilder guarantees they are checked in the same order as columns
    // were added to the Guided Decision Table.
    final List<ParameterizedValueBuilder> valueBuilders = new ArrayList<>();
    for (GuidedDecisionTableSourceBuilder sb : sourceBuilders) {
        if (sb instanceof GuidedDecisionTableSourceBuilderIndirect) {
            final GuidedDecisionTableSourceBuilderIndirect isb = (GuidedDecisionTableSourceBuilderIndirect) sb;
            final Set<Integer> sortedIndexes = new TreeSet<>(isb.getValueBuilders().keySet());
            for (Integer index : sortedIndexes) {
                final ParameterizedValueBuilder vb = isb.getValueBuilders().get(index);
                valueBuilders.add(vb);
            }
        }
    }
    return valueBuilders;
}
Also used : ParameterizedValueBuilder(org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.ParameterizedValueBuilder) GuidedDecisionTableSourceBuilder(org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableSourceBuilder) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) GuidedDecisionTableSourceBuilderIndirect(org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableSourceBuilderIndirect)

Example 3 with GuidedDecisionTableSourceBuilderIndirect

use of org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableSourceBuilderIndirect in project drools-wb by kiegroup.

the class GuidedDecisionTableGeneratorListener method assertValueBuildersRowData.

private void assertValueBuildersRowData() {
    // new row is created the GuidedDecisionTableSourceBuilders contain sufficient row data
    if (_sourceBuilders != null) {
        final int rowCount = getRowCount();
        final int columnCount = _sourceBuilders.size();
        for (GuidedDecisionTableSourceBuilder sb : _sourceBuilders) {
            if (sb instanceof GuidedDecisionTableSourceBuilderDirect) {
                final GuidedDecisionTableSourceBuilderDirect sbd = (GuidedDecisionTableSourceBuilderDirect) sb;
                if (sbd.getRowCount() < rowCount) {
                    sbd.addCellValue(rowCount, columnCount, "");
                }
            } else if (sb instanceof GuidedDecisionTableSourceBuilderIndirect) {
                final GuidedDecisionTableSourceBuilderIndirect sbi = (GuidedDecisionTableSourceBuilderIndirect) sb;
                for (ParameterizedValueBuilder pvb : sbi.getValueBuilders().values()) {
                    if (pvb.getColumnData().size() < rowCount) {
                        pvb.addCellValue(rowCount, columnCount, "");
                    }
                }
            }
        }
    }
}
Also used : ParameterizedValueBuilder(org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.ParameterizedValueBuilder) GuidedDecisionTableSourceBuilder(org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableSourceBuilder) GuidedDecisionTableSourceBuilderDirect(org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableSourceBuilderDirect) GuidedDecisionTableSourceBuilderIndirect(org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableSourceBuilderIndirect)

Aggregations

GuidedDecisionTableSourceBuilder (org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableSourceBuilder)3 GuidedDecisionTableSourceBuilderIndirect (org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableSourceBuilderIndirect)3 ArrayList (java.util.ArrayList)2 ParameterizedValueBuilder (org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.ParameterizedValueBuilder)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 ActionRetractFact (org.drools.workbench.models.datamodel.rule.ActionRetractFact)1 FreeFormLine (org.drools.workbench.models.datamodel.rule.FreeFormLine)1 IAction (org.drools.workbench.models.datamodel.rule.IAction)1 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)1 InterpolationVariable (org.drools.workbench.models.datamodel.rule.InterpolationVariable)1 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)1 RuleModelVisitor (org.drools.workbench.models.datamodel.rule.visitors.RuleModelVisitor)1 BRLActionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn)1 BRLConditionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionColumn)1 BRLConditionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionVariableColumn)1 BRLVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLVariableColumn)1 GuidedDecisionTableLHSBuilder (org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableLHSBuilder)1 GuidedDecisionTableRHSBuilder (org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableRHSBuilder)1