use of org.kie.workbench.common.stunner.core.graph.Node 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();
}
use of org.kie.workbench.common.stunner.core.graph.Node in project kie-wb-common by kiegroup.
the class SetParentNodeCommand 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);
// TODO: Create a ParentEdgeFactory iface extending EdgeFactory using as content generics type Relationship
final String uuid = UUID.uuid();
final Edge<Parent, Node> edge = new EdgeImpl<>(uuid);
edge.setContent(new Parent());
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.Node 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.Node in project kie-wb-common by kiegroup.
the class MapIndexBuilder method doWork.
private MapIndex doWork(final Graph<?, Node> graph, final MapIndex current) {
final Map<String, Node> nodes = new HashMap<>();
final Map<String, Edge> edges = new HashMap<>();
Iterable<Node> nodesIter = graph.nodes();
for (Node node : nodesIter) {
processNode(nodes, edges, node);
}
if (null == current) {
// Requesting a new index.
return new MapIndex(graph, nodes, edges);
} else {
// Updating an existing index.
current.nodes.clear();
current.nodes.putAll(nodes);
current.edges.clear();
current.edges.putAll(edges);
return current;
}
}
use of org.kie.workbench.common.stunner.core.graph.Node in project kie-wb-common by kiegroup.
the class ConnectorParentsMatchConnectionHandler method evaluateConnection.
private RuleViolations evaluateConnection(final RuleExtension rule, final GraphConnectionContext context) {
LOGGER.log(Level.INFO, "Evaluating rule handler [" + getClass().getName() + "]...");
final Optional<Node<? extends View<?>, ? extends Edge>> sourceNode = context.getSource();
final Optional<Node<? extends View<?>, ? extends Edge>> targetNode = context.getTarget();
final Class<?>[] typeArguments = rule.getTypeArguments();
final Class<?> parentType = null != typeArguments ? typeArguments[0] : null;
final DefaultRuleViolations result = new DefaultRuleViolations();
boolean isValid = true;
if (sourceNode.isPresent() && targetNode.isPresent()) {
isValid = new ParentsTypeMatcher(definitionManager).forParentType(parentType).test(sourceNode.get(), targetNode.get());
}
if (!isValid) {
addViolation(context.getConnector().getUUID(), rule, result);
}
return result;
}
Aggregations