Search in sources :

Example 26 with GraphCommandExecutionContext

use of org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext in project kie-wb-common by kiegroup.

the class AbstractNodeBuilder method afterNodeBuild.

@SuppressWarnings("unchecked")
protected void afterNodeBuild(final BuilderContext context, final T node) {
    // Outgoing connections.
    if (outgoingResourceIds != null && !outgoingResourceIds.isEmpty()) {
        for (String outgoingNodeId : outgoingResourceIds) {
            GraphObjectBuilder<?, ?> outgoingBuilder = getBuilder(context, outgoingNodeId);
            if (outgoingBuilder == null) {
                throw new RuntimeException("No outgoing edge builder for " + outgoingNodeId);
            }
            final List<Command<GraphCommandExecutionContext, RuleViolation>> commands = new LinkedList<>();
            // If outgoing element it's a node means that it's docked.
            if (outgoingBuilder instanceof AbstractNodeBuilder) {
                // Command - Create the docked node.
                Node docked = (Node) outgoingBuilder.build(context);
                commands.add(context.getCommandFactory().addDockedNode(node, docked));
                // Obtain docked position and use those for the docked node.
                final List<Double[]> dockers = ((AbstractNodeBuilder) outgoingBuilder).dockers;
                if (!dockers.isEmpty()) {
                    // TODO: Use not only first docker coordinates?
                    Double[] dCoords = dockers.get(0);
                    double x = dCoords[0];
                    double y = dCoords[1];
                    commands.add(context.getCommandFactory().updatePosition(docked, new Point2D(x, y)));
                }
            } else {
                // Create the outgoing edge.
                AbstractEdgeBuilder edgeBuilder = (AbstractEdgeBuilder) outgoingBuilder;
                Edge edge = (Edge) edgeBuilder.build(context);
                if (edge != null) {
                    // Command - Execute the graph command to set the node as the edge connection's source..
                    Double[] sourceDocker = null;
                    final List<Double[]> dockers = ((AbstractEdgeBuilder) outgoingBuilder).dockers;
                    if (dockers != null && dockers.size() > 1) {
                        sourceDocker = dockers.get(0);
                    }
                    Connection sourceConnection = null;
                    if (null != sourceDocker) {
                        sourceConnection = MagnetConnection.Builder.at(sourceDocker[0], sourceDocker[1]).setAuto(edgeBuilder.isSourceAutoConnection());
                    }
                    commands.add(context.getCommandFactory().setSourceNode(node, edge, sourceConnection));
                }
            }
            if (!commands.isEmpty()) {
                for (Command<GraphCommandExecutionContext, RuleViolation> command : commands) {
                    doExecuteCommand(context, command);
                }
            }
        }
    }
    // Children connections.
    if (childNodeIds != null && !childNodeIds.isEmpty()) {
        for (String childNodeId : childNodeIds) {
            GraphObjectBuilder<?, ?> childNodeBuilder = getBuilder(context, childNodeId);
            if (childNodeBuilder == null) {
                throw new RuntimeException("No child node builder for " + childNodeId);
            }
            Command<GraphCommandExecutionContext, RuleViolation> command = null;
            if (childNodeBuilder instanceof NodeObjectBuilder) {
                // Command - Create the child node and the parent-child relationship.
                Node childNode = (Node) childNodeBuilder.build(context);
                command = context.getCommandFactory().addChildNode(node, childNode);
            }
            if (null != command) {
                doExecuteCommand(context, command);
            }
        }
    }
}
Also used : Node(org.kie.workbench.common.stunner.core.graph.Node) Connection(org.kie.workbench.common.stunner.core.graph.content.view.Connection) MagnetConnection(org.kie.workbench.common.stunner.core.graph.content.view.MagnetConnection) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) LinkedList(java.util.LinkedList) Command(org.kie.workbench.common.stunner.core.command.Command) AddNodeCommand(org.kie.workbench.common.stunner.core.graph.command.impl.AddNodeCommand) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) GraphCommandExecutionContext(org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext) Edge(org.kie.workbench.common.stunner.core.graph.Edge)

Example 27 with GraphCommandExecutionContext

use of org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext in project kie-wb-common by kiegroup.

the class AddOutputClauseCommand method newGraphCommand.

@Override
protected Command<GraphCommandExecutionContext, RuleViolation> newGraphCommand(final AbstractCanvasHandler context) {
    return new AbstractGraphCommand() {

        @Override
        protected CommandResult<RuleViolation> check(final GraphCommandExecutionContext context) {
            return GraphCommandResultBuilder.SUCCESS;
        }

        @Override
        public CommandResult<RuleViolation> execute(final GraphCommandExecutionContext context) {
            final int clauseIndex = uiColumnIndex - DecisionTableUIModelMapperHelper.ROW_INDEX_COLUMN_COUNT - dtable.getInput().size();
            dtable.getOutput().add(clauseIndex, outputClause);
            dtable.getRule().forEach(rule -> {
                final LiteralExpression le = new LiteralExpression();
                le.setText(OUTPUT_CLAUSE_DEFAULT_VALUE);
                rule.getOutputEntry().add(clauseIndex, le);
            });
            return GraphCommandResultBuilder.SUCCESS;
        }

        @Override
        public CommandResult<RuleViolation> undo(final GraphCommandExecutionContext context) {
            final int clauseIndex = dtable.getOutput().indexOf(outputClause);
            dtable.getRule().forEach(rule -> rule.getOutputEntry().remove(clauseIndex));
            dtable.getOutput().remove(outputClause);
            return GraphCommandResultBuilder.SUCCESS;
        }
    };
}
Also used : LiteralExpression(org.kie.workbench.common.dmn.api.definition.v1_1.LiteralExpression) AbstractGraphCommand(org.kie.workbench.common.stunner.core.graph.command.impl.AbstractGraphCommand) GraphCommandExecutionContext(org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation)

Example 28 with GraphCommandExecutionContext

use of org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext in project kie-wb-common by kiegroup.

the class DeleteInputClauseCommand method newGraphCommand.

@Override
protected Command<GraphCommandExecutionContext, RuleViolation> newGraphCommand(final AbstractCanvasHandler handler) {
    return new AbstractGraphCommand() {

        @Override
        protected CommandResult<RuleViolation> check(final GraphCommandExecutionContext gce) {
            return GraphCommandResultBuilder.SUCCESS;
        }

        @Override
        public CommandResult<RuleViolation> execute(final GraphCommandExecutionContext gce) {
            final int clauseIndex = getInputClauseIndex();
            dtable.getRule().forEach(row -> row.getInputEntry().remove(clauseIndex));
            dtable.getInput().remove(clauseIndex);
            return GraphCommandResultBuilder.SUCCESS;
        }

        @Override
        public CommandResult<RuleViolation> undo(final GraphCommandExecutionContext gce) {
            final int clauseIndex = getInputClauseIndex();
            dtable.getInput().add(clauseIndex, oldInputClause);
            IntStream.range(0, dtable.getRule().size()).forEach(rowIndex -> {
                final UnaryTests value = oldColumnData.get(rowIndex);
                dtable.getRule().get(rowIndex).getInputEntry().add(clauseIndex, value);
            });
            return GraphCommandResultBuilder.SUCCESS;
        }
    };
}
Also used : AbstractGraphCommand(org.kie.workbench.common.stunner.core.graph.command.impl.AbstractGraphCommand) GraphCommandExecutionContext(org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) UnaryTests(org.kie.workbench.common.dmn.api.definition.v1_1.UnaryTests)

Example 29 with GraphCommandExecutionContext

use of org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext in project kie-wb-common by kiegroup.

the class DeleteOutputClauseCommand method newGraphCommand.

@Override
protected Command<GraphCommandExecutionContext, RuleViolation> newGraphCommand(final AbstractCanvasHandler handler) {
    return new AbstractGraphCommand() {

        @Override
        protected CommandResult<RuleViolation> check(final GraphCommandExecutionContext gce) {
            return GraphCommandResultBuilder.SUCCESS;
        }

        @Override
        public CommandResult<RuleViolation> execute(final GraphCommandExecutionContext gce) {
            final int clauseIndex = getOutputClauseIndex();
            dtable.getRule().forEach(row -> row.getOutputEntry().remove(clauseIndex));
            dtable.getOutput().remove(clauseIndex);
            return GraphCommandResultBuilder.SUCCESS;
        }

        @Override
        public CommandResult<RuleViolation> undo(final GraphCommandExecutionContext gce) {
            final int clauseIndex = getOutputClauseIndex();
            dtable.getOutput().add(clauseIndex, oldOutputClause);
            IntStream.range(0, dtable.getRule().size()).forEach(rowIndex -> {
                final LiteralExpression value = oldColumnData.get(rowIndex);
                dtable.getRule().get(rowIndex).getOutputEntry().add(clauseIndex, value);
            });
            return GraphCommandResultBuilder.SUCCESS;
        }
    };
}
Also used : LiteralExpression(org.kie.workbench.common.dmn.api.definition.v1_1.LiteralExpression) AbstractGraphCommand(org.kie.workbench.common.stunner.core.graph.command.impl.AbstractGraphCommand) GraphCommandExecutionContext(org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation)

Example 30 with GraphCommandExecutionContext

use of org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext in project kie-wb-common by kiegroup.

the class MoveColumnsCommand method newGraphCommand.

@Override
protected Command<GraphCommandExecutionContext, RuleViolation> newGraphCommand(final AbstractCanvasHandler context) {
    return new AbstractGraphCommand() {

        @Override
        protected CommandResult<RuleViolation> check(final GraphCommandExecutionContext context) {
            return isColumnInValidSection() ? GraphCommandResultBuilder.SUCCESS : GraphCommandResultBuilder.FAILED;
        }

        private boolean isColumnInValidSection() {
            final DecisionTableSection section = DecisionTableUIModelMapperHelper.getSection(dtable, index);
            return section == DecisionTableSection.INPUT_CLAUSES || section == DecisionTableSection.OUTPUT_CLAUSES;
        }

        @Override
        public CommandResult<RuleViolation> execute(final GraphCommandExecutionContext context) {
            return moveClauses(index);
        }

        @Override
        public CommandResult<RuleViolation> undo(final GraphCommandExecutionContext context) {
            return moveClauses(oldIndex);
        }

        private CommandResult<RuleViolation> moveClauses(final int index) {
            final DecisionTableSection section = DecisionTableUIModelMapperHelper.getSection(dtable, index);
            if (section == DecisionTableSection.INPUT_CLAUSES) {
                final int oldIndex = uiModel.getColumns().indexOf(columns.get(0));
                final int relativeIndex = DecisionTableUIModelMapperHelper.getInputEntryIndex(dtable, index);
                final int relativeOldIndex = DecisionTableUIModelMapperHelper.getInputEntryIndex(dtable, oldIndex);
                final List<Integer> inputClauseIndexesToMove = columns.stream().map(c -> uiModel.getColumns().indexOf(c)).map(i -> DecisionTableUIModelMapperHelper.getInputEntryIndex(dtable, i)).collect(Collectors.toList());
                moveClauses(relativeIndex, relativeOldIndex, dtable.getInput(), inputClauseIndexesToMove);
                final List<List<UnaryTests>> decisionRulesInputEntries = dtable.getRule().stream().map(DecisionRule::getInputEntry).collect(Collectors.toList());
                updateDecisionRules(relativeIndex, relativeOldIndex, decisionRulesInputEntries, inputClauseIndexesToMove);
                return GraphCommandResultBuilder.SUCCESS;
            } else if (section == DecisionTableSection.OUTPUT_CLAUSES) {
                final int oldIndex = uiModel.getColumns().indexOf(columns.get(0));
                final int relativeIndex = DecisionTableUIModelMapperHelper.getOutputEntryIndex(dtable, index);
                final int relativeOldIndex = DecisionTableUIModelMapperHelper.getOutputEntryIndex(dtable, oldIndex);
                final List<Integer> outputClauseIndexesToMove = columns.stream().map(c -> uiModel.getColumns().indexOf(c)).map(i -> DecisionTableUIModelMapperHelper.getOutputEntryIndex(dtable, i)).collect(Collectors.toList());
                moveClauses(relativeIndex, relativeOldIndex, dtable.getOutput(), outputClauseIndexesToMove);
                final List<List<LiteralExpression>> decisionRulesOutputEntries = dtable.getRule().stream().map(DecisionRule::getOutputEntry).collect(Collectors.toList());
                updateDecisionRules(relativeIndex, relativeOldIndex, decisionRulesOutputEntries, outputClauseIndexesToMove);
                return GraphCommandResultBuilder.SUCCESS;
            } else {
                return GraphCommandResultBuilder.FAILED;
            }
        }

        private <T> void moveClauses(final int relativeIndex, final int relativeOldIndex, final List<T> clauses, final List<Integer> clauseIndexesToMove) {
            final List<T> clausesToMove = clauseIndexesToMove.stream().map(clauses::get).collect(Collectors.toList());
            clauses.removeAll(clausesToMove);
            if (relativeIndex < relativeOldIndex) {
                clauses.addAll(relativeIndex, clausesToMove);
            } else if (relativeIndex > relativeOldIndex) {
                clauses.addAll(relativeIndex - clausesToMove.size() + 1, clausesToMove);
            }
        }

        private <T> void updateDecisionRules(final int relativeIndex, final int relativeOldIndex, final List<List<T>> clauses, final List<Integer> clauseIndexesToMove) {
            clauses.forEach(row -> moveClauses(relativeIndex, relativeOldIndex, row, clauseIndexesToMove));
        }
    };
}
Also used : DMNGridData(org.kie.workbench.common.dmn.client.widgets.grid.model.DMNGridData) DecisionTable(org.kie.workbench.common.dmn.api.definition.v1_1.DecisionTable) DecisionTableUIModelMapperHelper(org.kie.workbench.common.dmn.client.editors.expressions.types.dtable.DecisionTableUIModelMapperHelper) CanvasViolation(org.kie.workbench.common.stunner.core.client.command.CanvasViolation) DecisionRule(org.kie.workbench.common.dmn.api.definition.v1_1.DecisionRule) LiteralExpression(org.kie.workbench.common.dmn.api.definition.v1_1.LiteralExpression) VetoExecutionCommand(org.kie.workbench.common.dmn.client.commands.VetoExecutionCommand) CanvasCommandResultBuilder(org.kie.workbench.common.stunner.core.client.command.CanvasCommandResultBuilder) GridColumn(org.uberfire.ext.wires.core.grids.client.model.GridColumn) AbstractCanvasHandler(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler) AbstractCanvasCommand(org.kie.workbench.common.stunner.core.client.canvas.command.AbstractCanvasCommand) Collectors(java.util.stream.Collectors) AbstractCanvasGraphCommand(org.kie.workbench.common.stunner.core.client.canvas.command.AbstractCanvasGraphCommand) ArrayList(java.util.ArrayList) List(java.util.List) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) AbstractGraphCommand(org.kie.workbench.common.stunner.core.graph.command.impl.AbstractGraphCommand) UnaryTests(org.kie.workbench.common.dmn.api.definition.v1_1.UnaryTests) Command(org.kie.workbench.common.stunner.core.command.Command) GraphCommandExecutionContext(org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext) GraphCommandResultBuilder(org.kie.workbench.common.stunner.core.graph.command.GraphCommandResultBuilder) VetoUndoCommand(org.kie.workbench.common.dmn.client.commands.VetoUndoCommand) CommandResult(org.kie.workbench.common.stunner.core.command.CommandResult) DecisionTableSection(org.kie.workbench.common.dmn.client.editors.expressions.types.dtable.DecisionTableUIModelMapperHelper.DecisionTableSection) DecisionTableSection(org.kie.workbench.common.dmn.client.editors.expressions.types.dtable.DecisionTableUIModelMapperHelper.DecisionTableSection) LiteralExpression(org.kie.workbench.common.dmn.api.definition.v1_1.LiteralExpression) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) DecisionRule(org.kie.workbench.common.dmn.api.definition.v1_1.DecisionRule) AbstractGraphCommand(org.kie.workbench.common.stunner.core.graph.command.impl.AbstractGraphCommand) GraphCommandExecutionContext(org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

GraphCommandExecutionContext (org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext)60 RuleViolation (org.kie.workbench.common.stunner.core.rule.RuleViolation)60 Test (org.junit.Test)47 LiteralExpression (org.kie.workbench.common.dmn.api.definition.v1_1.LiteralExpression)18 InformationItem (org.kie.workbench.common.dmn.api.definition.v1_1.InformationItem)16 DecisionRule (org.kie.workbench.common.dmn.api.definition.v1_1.DecisionRule)12 List (org.kie.workbench.common.dmn.api.definition.v1_1.List)10 AbstractGraphCommand (org.kie.workbench.common.stunner.core.graph.command.impl.AbstractGraphCommand)10 AbstractCanvasHandler (org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler)8 CanvasViolation (org.kie.workbench.common.stunner.core.client.command.CanvasViolation)8 Binding (org.kie.workbench.common.dmn.api.definition.v1_1.Binding)7 InputClause (org.kie.workbench.common.dmn.api.definition.v1_1.InputClause)6 OutputClause (org.kie.workbench.common.dmn.api.definition.v1_1.OutputClause)5 UnaryTests (org.kie.workbench.common.dmn.api.definition.v1_1.UnaryTests)5 ContextEntry (org.kie.workbench.common.dmn.api.definition.v1_1.ContextEntry)4 DMNGridRow (org.kie.workbench.common.dmn.client.widgets.grid.model.DMNGridRow)4 Command (org.kie.workbench.common.stunner.core.command.Command)4 Name (org.kie.workbench.common.dmn.api.property.dmn.Name)3 CanvasCommandResultBuilder (org.kie.workbench.common.stunner.core.client.command.CanvasCommandResultBuilder)3 CommandResult (org.kie.workbench.common.stunner.core.command.CommandResult)3