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);
}
}
}
}
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;
}
};
}
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;
}
};
}
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;
}
};
}
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));
}
};
}
Aggregations