use of org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext in project kie-wb-common by kiegroup.
the class SafeDeleteNodeCommand method initialize.
@Override
@SuppressWarnings("unchecked")
protected SafeDeleteNodeCommand initialize(final GraphCommandExecutionContext context) {
super.initialize(context);
final Graph<?, Node> graph = getGraph(context);
final Node<Definition<?>, Edge> candidate = (Node<Definition<?>, Edge>) getCandidate(context);
new SafeDeleteNodeProcessor(new ChildrenTraverseProcessorImpl(new TreeWalkTraverseProcessorImpl()), graph, candidate).run(new SafeDeleteNodeProcessor.Callback() {
private final Set<String> processedConnectors = new HashSet<String>();
@Override
public void deleteCandidateConnector(final Edge<? extends View<?>, Node> edge) {
// This command will delete candidate's connectors once deleting the candidate node later on,
// as it potentially performs the connectors shortcut operation.
}
@Override
public void deleteConnector(final Edge<? extends View<?>, Node> edge) {
doDeleteConnector(edge);
}
@Override
public void removeChild(final Element<?> parent, final Node<?, Edge> candidate) {
log("RemoveChildCommand [parent=" + parent.getUUID() + ", candidate=" + candidate.getUUID() + "]");
addCommand(new RemoveChildCommand((Node<?, Edge>) parent, candidate));
safeDeleteCallback.ifPresent(c -> c.removeChild(parent, candidate));
}
@Override
public void removeDock(final Node<?, Edge> parent, final Node<?, Edge> candidate) {
log("UnDockNodeCommand [parent=" + parent.getUUID() + ", candidate=" + candidate.getUUID() + "]");
addCommand(new UnDockNodeCommand(parent, candidate));
safeDeleteCallback.ifPresent(c -> c.removeDock(parent, candidate));
}
@Override
public void deleteCandidateNode(final Node<?, Edge> node) {
processCandidateConnectors();
deleteNode(node);
}
@Override
public void deleteNode(final Node<?, Edge> node) {
log("DeregisterNodeCommand [node=" + node.getUUID() + "]");
addCommand(new DeregisterNodeCommand(node));
safeDeleteCallback.ifPresent(c -> c.deleteNode(node));
}
private void processCandidateConnectors() {
if (options.isDeleteCandidateConnectors()) {
if (options.isShortcutCandidateConnectors() && hasSingleIncomingEdge().and(hasSingleOutgoingEdge()).test(candidate)) {
final Edge<? extends ViewConnector<?>, Node> in = getViewConnector().apply(candidate.getInEdges());
final Edge<? extends ViewConnector<?>, Node> out = getViewConnector().apply(candidate.getOutEdges());
shortcut(in, out);
} else {
Stream.concat(candidate.getInEdges().stream(), candidate.getOutEdges().stream()).filter(e -> e.getContent() instanceof ViewConnector).forEach(this::deleteConnector);
}
}
}
private void shortcut(final Edge<? extends ViewConnector<?>, Node> in, final Edge<? extends ViewConnector<?>, Node> out) {
final ViewConnector<?> outContent = out.getContent();
final Node targetNode = out.getTargetNode();
addCommand(new DeleteConnectorCommand(out));
safeDeleteCallback.ifPresent(c -> c.deleteCandidateConnector(out));
addCommand(new SetConnectionTargetNodeCommand(targetNode, in, outContent.getTargetConnection().orElse(null)));
safeDeleteCallback.ifPresent(c -> c.setEdgeTargetNode(targetNode, in));
}
private void doDeleteConnector(final Edge<? extends View<?>, Node> edge) {
if (!processedConnectors.contains(edge.getUUID())) {
log("IN DoDeleteConnector [edge=" + edge.getUUID() + "]");
addCommand(new DeleteConnectorCommand(edge));
safeDeleteCallback.ifPresent(c -> c.deleteConnector(edge));
processedConnectors.add(edge.getUUID());
}
}
});
return this;
}
use of org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext in project kie-wb-common by kiegroup.
the class AddDecisionRuleCommand 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) {
dtable.getRule().add(uiRowIndex, rule);
for (int ie = 0; ie < dtable.getInput().size(); ie++) {
final UnaryTests ut = new UnaryTests();
ut.setText(AddInputClauseCommand.INPUT_CLAUSE_DEFAULT_VALUE);
rule.getInputEntry().add(ut);
}
for (int oe = 0; oe < dtable.getOutput().size(); oe++) {
final LiteralExpression le = new LiteralExpression();
le.setText(AddOutputClauseCommand.OUTPUT_CLAUSE_DEFAULT_VALUE);
rule.getOutputEntry().add(le);
}
final Description d = new Description();
d.setValue(DESCRIPTION_DEFAULT_VALUE);
rule.setDescription(d);
return GraphCommandResultBuilder.SUCCESS;
}
@Override
public CommandResult<RuleViolation> undo(final GraphCommandExecutionContext context) {
dtable.getRule().remove(rule);
return GraphCommandResultBuilder.SUCCESS;
}
};
}
use of org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext in project kie-wb-common by kiegroup.
the class DeleteDecisionRuleCommandTest method testGraphCommandExecuteRemoveFromMiddle.
@Test
public void testGraphCommandExecuteRemoveFromMiddle() throws Exception {
final DecisionRule firstRule = mock(DecisionRule.class);
final DecisionRule lastRule = mock(DecisionRule.class);
dtable.getRule().add(0, firstRule);
dtable.getRule().add(lastRule);
uiModel.appendRow(new BaseGridRow());
uiModel.appendRow(new BaseGridRow());
makeCommand(1);
assertEquals(3, dtable.getRule().size());
final Command<GraphCommandExecutionContext, RuleViolation> graphCommand = command.newGraphCommand(canvasHandler);
assertEquals(GraphCommandResultBuilder.SUCCESS, graphCommand.execute(graphCommandExecutionContext));
assertEquals(2, dtable.getRule().size());
assertEquals(firstRule, dtable.getRule().get(0));
assertEquals(lastRule, dtable.getRule().get(1));
}
use of org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext in project kie-wb-common by kiegroup.
the class DeleteOutputClauseCommandTest method testGraphCommandExecuteRemoveMiddle.
@Test
public void testGraphCommandExecuteRemoveMiddle() {
final OutputClause firstOutput = mock(OutputClause.class);
final OutputClause lastOutput = mock(OutputClause.class);
dtable.getOutput().add(0, firstOutput);
dtable.getOutput().add(lastOutput);
final LiteralExpression outputOneValue = mock(LiteralExpression.class);
final LiteralExpression outputTwoValue = mock(LiteralExpression.class);
final LiteralExpression outputThreeValue = mock(LiteralExpression.class);
final DecisionRule rule = new DecisionRule();
rule.getOutputEntry().add(outputOneValue);
rule.getOutputEntry().add(outputTwoValue);
rule.getOutputEntry().add(outputThreeValue);
dtable.getRule().add(rule);
makeCommand(DecisionTableUIModelMapperHelper.ROW_INDEX_COLUMN_COUNT + dtable.getInput().size() + 1);
final Command<GraphCommandExecutionContext, RuleViolation> graphCommand = command.newGraphCommand(canvasHandler);
assertEquals(GraphCommandResultBuilder.SUCCESS, graphCommand.execute(graphCommandExecutionContext));
assertEquals(2, dtable.getOutput().size());
assertEquals(firstOutput, dtable.getOutput().get(0));
assertEquals(lastOutput, dtable.getOutput().get(1));
assertEquals(2, dtable.getRule().get(0).getOutputEntry().size());
assertEquals(outputOneValue, dtable.getRule().get(0).getOutputEntry().get(0));
assertEquals(outputThreeValue, dtable.getRule().get(0).getOutputEntry().get(1));
}
use of org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext in project kie-wb-common by kiegroup.
the class AddInputClauseCommandTest method testGraphCommandUndoJustLastInputClauseColumn.
@Test
public void testGraphCommandUndoJustLastInputClauseColumn() throws Exception {
makeCommand(DecisionTableUIModelMapperHelper.ROW_INDEX_COLUMN_COUNT);
final String ruleOneOldInput = "old rule 1";
final String ruleTwoOldInput = "old rule 2";
dtable.getInput().add(new InputClause());
addRuleWithInputClauseValues(ruleOneOldInput);
addRuleWithInputClauseValues(ruleTwoOldInput);
assertEquals(1, dtable.getInput().size());
final Command<GraphCommandExecutionContext, RuleViolation> graphCommand = command.newGraphCommand(canvasHandler);
assertEquals(GraphCommandResultBuilder.SUCCESS, graphCommand.execute(graphCommandExecutionContext));
assertEquals(GraphCommandResultBuilder.SUCCESS, graphCommand.undo(graphCommandExecutionContext));
assertEquals(1, dtable.getInput().size());
// first rule
assertEquals(1, dtable.getRule().get(0).getInputEntry().size());
assertEquals(ruleOneOldInput, dtable.getRule().get(0).getInputEntry().get(0).getText().getValue());
// second rule
assertEquals(1, dtable.getRule().get(1).getInputEntry().size());
assertEquals(ruleTwoOldInput, dtable.getRule().get(1).getInputEntry().get(0).getText().getValue());
}
Aggregations