use of org.kie.workbench.common.stunner.core.graph.Edge 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.Edge in project kie-wb-common by kiegroup.
the class SetChildNodeCommand method execute.
@Override
@SuppressWarnings("unchecked")
public CommandResult<RuleViolation> execute(final GraphCommandExecutionContext context) {
final CommandResult<RuleViolation> results = allow(context);
if (!results.getType().equals(CommandResult.Type.ERROR)) {
final Node<?, Edge> parent = getParent(context);
final Node<?, Edge> candidate = getCandidate(context);
final String uuid = UUID.uuid();
final Edge<Child, Node> edge = new EdgeImpl<>(uuid);
edge.setContent(new Child());
edge.setSourceNode(parent);
edge.setTargetNode(candidate);
parent.getOutEdges().add(edge);
candidate.getInEdges().add(edge);
getMutableIndex(context).addEdge(edge);
}
return results;
}
use of org.kie.workbench.common.stunner.core.graph.Edge in project kie-wb-common by kiegroup.
the class SetChildNodeCommand method check.
@SuppressWarnings("unchecked")
protected CommandResult<RuleViolation> check(final GraphCommandExecutionContext context) {
final Element<? extends Definition<?>> parent = (Element<? extends Definition<?>>) getParent(context);
final Node<Definition<?>, Edge> candidate = (Node<Definition<?>, Edge>) getCandidate(context);
final Collection<RuleViolation> containmentRuleViolations = doEvaluate(context, RuleContextBuilder.GraphContexts.containment(getGraph(context), parent, candidate));
return new GraphCommandResultBuilder(containmentRuleViolations).build();
}
use of org.kie.workbench.common.stunner.core.graph.Edge in project kie-wb-common by kiegroup.
the class SetConnectionSourceNodeCommand method execute.
@Override
@SuppressWarnings("unchecked")
public CommandResult<RuleViolation> execute(final GraphCommandExecutionContext context) {
final CommandResult<RuleViolation> results = allow(context);
if (!results.getType().equals(CommandResult.Type.ERROR)) {
final Edge<? extends View, Node> edge = getEdge(context);
final Node<?, Edge> sourceNode = getSourceNode(context);
final Node<? extends View<?>, Edge> lastSourceNode = edge.getSourceNode();
// New connection being made
if (null != lastSourceNode) {
this.lastSourceNodeUUID = lastSourceNode.getUUID();
lastSourceNode.getOutEdges().remove(edge);
}
if (null != sourceNode) {
sourceNode.getOutEdges().add(edge);
}
edge.setSourceNode(sourceNode);
// Magnet being moved on node
ViewConnector connectionContent = (ViewConnector) edge.getContent();
lastConnection = (Connection) connectionContent.getSourceConnection().orElse(null);
connectionContent.setSourceConnection(connection);
}
return results;
}
use of org.kie.workbench.common.stunner.core.graph.Edge in project kie-wb-common by kiegroup.
the class SetConnectionSourceNodeCommand method check.
@SuppressWarnings("unchecked")
protected CommandResult<RuleViolation> check(final GraphCommandExecutionContext context) {
final GraphCommandResultBuilder resultBuilder = new GraphCommandResultBuilder();
final Node<View<?>, Edge> sourceNode = (Node<View<?>, Edge>) getSourceNode(context);
final Edge<View<?>, Node> edge = (Edge<View<?>, Node>) getEdge(context);
final Node<? extends View<?>, Edge> lastSourceNode = edge.getSourceNode();
// Only check for rules in case the connector's source node is a different one.
if ((null == lastSourceNode && null != sourceNode) || (null != lastSourceNode && (!lastSourceNode.equals(sourceNode)))) {
// New connection being made
final Collection<RuleViolation> connectionRuleViolations = doEvaluate(context, RuleContextBuilder.GraphContexts.connection(getGraph(context), edge, Optional.ofNullable(sourceNode), Optional.ofNullable(targetNode)));
resultBuilder.addViolations(connectionRuleViolations);
final Node<View<?>, Edge> currentSource = edge.getSourceNode();
// If the edge has an outoutgoing source node, check cardinality for removing it.
if (null != currentSource) {
final Collection<RuleViolation> cardinalityRuleViolations = doEvaluate(context, RuleContextBuilder.GraphContexts.edgeCardinality(getGraph(context), currentSource, edge, EdgeCardinalityContext.Direction.OUTGOING, Optional.of(CardinalityContext.Operation.DELETE)));
resultBuilder.addViolations(cardinalityRuleViolations);
}
// If the new source node exist, evaluate cardinality rules for this edge.
if (null != sourceNode) {
final Collection<RuleViolation> cardinalityRuleViolations = doEvaluate(context, RuleContextBuilder.GraphContexts.edgeCardinality(getGraph(context), sourceNode, edge, EdgeCardinalityContext.Direction.OUTGOING, Optional.of(CardinalityContext.Operation.ADD)));
resultBuilder.addViolations(cardinalityRuleViolations);
}
}
return resultBuilder.build();
}
Aggregations