use of org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessor in project kie-wb-common by kiegroup.
the class AcyclicDirectedGraphRule method evaluate.
@Override
@SuppressWarnings("unchecked")
public RuleViolations evaluate(final RuleExtension rule, final GraphConnectionContext context) {
final Graph<?, Node> graph = (Graph<?, Node>) context.getGraph();
final Optional<Node<? extends View<?>, ? extends Edge>> oSource = context.getSource();
final Optional<Node<? extends View<?>, ? extends Edge>> oTarget = context.getTarget();
final Edge<? extends View<?>, ? extends Node> oConnector = context.getConnector();
final DefaultRuleViolations result = new DefaultRuleViolations();
// Only validate DAG when source and target nodes are set
if (!(oSource.isPresent() && oTarget.isPresent())) {
return result;
}
final Node<?, Edge> source = (Node<?, Edge>) oSource.get();
final Node<?, Edge> target = (Node<?, Edge>) oTarget.get();
final Edge<?, Node> connector = (Edge<?, Node>) oConnector;
try {
final TreeWalkTraverseProcessor walker = getTreeWalker(source, target, connector);
walker.traverse(graph, new TreeTraverseCallback<Graph, Node, Edge>() {
final Set<Node> inProgress = new HashSet<>();
@Override
public void startGraphTraversal(final Graph graph) {
}
@Override
public boolean startNodeTraversal(final Node node) {
if (inProgress.contains(node)) {
throw new DirectedAcrylicGraphViolationException();
}
inProgress.add(node);
return true;
}
@Override
public boolean startEdgeTraversal(final Edge edge) {
return true;
}
@Override
public void endNodeTraversal(final Node node) {
inProgress.remove(node);
}
@Override
public void endEdgeTraversal(final Edge edge) {
}
@Override
public void endGraphTraversal() {
}
});
} catch (DirectedAcrylicGraphViolationException e) {
result.addViolation(new RuleViolationImpl(ERROR_MESSAGE));
}
return result;
}
use of org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessor in project kie-wb-common by kiegroup.
the class GraphImpl method hashCode.
@Override
public int hashCode() {
// dirty trick to allow inner class to modify variable
int[] hashArr = { 0 };
final TreeWalkTraverseProcessor treeWalkTraverseProcessor = new TreeWalkTraverseProcessorImpl();
treeWalkTraverseProcessor.traverse(this, new AbstractTreeTraverseCallback<Graph, Node, Edge>() {
int[] myHashArr = hashArr;
@Override
public boolean startEdgeTraversal(final Edge edge) {
super.startEdgeTraversal(edge);
final Object content = edge.getContent();
myHashArr[0] = HashUtil.combineHashCodes(myHashArr[0], content.hashCode());
return true;
}
@Override
public boolean startNodeTraversal(final Node node) {
super.startNodeTraversal(node);
myHashArr[0] = HashUtil.combineHashCodes(myHashArr[0], node.hashCode());
if (!(node.getContent() instanceof DefinitionSet) && node.getContent() instanceof Definition) {
Object def = ((Definition) (node.getContent())).getDefinition();
myHashArr[0] = HashUtil.combineHashCodes(myHashArr[0], def.hashCode());
}
return true;
}
});
// Get the hash from the graph traversal
return hashArr[0];
}
use of org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessor in project kie-wb-common by kiegroup.
the class CanvasHighlightVisitor method prepareVisit.
@SuppressWarnings("unchecked")
private void prepareVisit(final Command command) {
final Graph graph = canvasHandler.getDiagram().getGraph();
final TreeWalkTraverseProcessor treeWalkTraverseProcessor = new TreeWalkTraverseProcessorImpl().useStartNodePredicate(this::isStartNode);
new ViewTraverseProcessorImpl(treeWalkTraverseProcessor).traverse(graph, new ContentTraverseCallback<View<?>, Node<View, Edge>, Edge<View<?>, Node>>() {
@Override
public void startGraphTraversal(final Graph<DefinitionSet, Node<View, Edge>> graph) {
}
@Override
public void startEdgeTraversal(final Edge<View<?>, Node> edge) {
addShape(edge.getUUID());
}
@Override
public void endEdgeTraversal(final Edge<View<?>, Node> edge) {
}
@Override
public void startNodeTraversal(final Node<View, Edge> node) {
addShape(node.getUUID());
}
@Override
public void endNodeTraversal(final Node<View, Edge> node) {
}
@Override
public void endGraphTraversal() {
command.execute();
}
private void addShape(final String uuid) {
final Shape shape = canvasHandler.getCanvas().getShape(uuid);
if (null != shape) {
shapes.add(shape);
}
}
});
}
Aggregations