Search in sources :

Example 26 with Node

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();
}
Also used : GraphCommandResultBuilder(org.kie.workbench.common.stunner.core.graph.command.GraphCommandResultBuilder) Node(org.kie.workbench.common.stunner.core.graph.Node) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Edge(org.kie.workbench.common.stunner.core.graph.Edge)

Example 27 with Node

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;
}
Also used : Parent(org.kie.workbench.common.stunner.core.graph.content.relationship.Parent) Node(org.kie.workbench.common.stunner.core.graph.Node) EdgeImpl(org.kie.workbench.common.stunner.core.graph.impl.EdgeImpl) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) Edge(org.kie.workbench.common.stunner.core.graph.Edge)

Example 28 with Node

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];
}
Also used : TreeWalkTraverseProcessor(org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessor) Graph(org.kie.workbench.common.stunner.core.graph.Graph) Node(org.kie.workbench.common.stunner.core.graph.Node) Definition(org.kie.workbench.common.stunner.core.graph.content.definition.Definition) DefinitionSet(org.kie.workbench.common.stunner.core.graph.content.definition.DefinitionSet) Edge(org.kie.workbench.common.stunner.core.graph.Edge) TreeWalkTraverseProcessorImpl(org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessorImpl)

Example 29 with Node

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;
    }
}
Also used : HashMap(java.util.HashMap) Node(org.kie.workbench.common.stunner.core.graph.Node) Edge(org.kie.workbench.common.stunner.core.graph.Edge)

Example 30 with Node

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;
}
Also used : Node(org.kie.workbench.common.stunner.core.graph.Node) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Edge(org.kie.workbench.common.stunner.core.graph.Edge) ParentsTypeMatcher(org.kie.workbench.common.stunner.core.graph.util.ParentsTypeMatcher)

Aggregations

Node (org.kie.workbench.common.stunner.core.graph.Node)153 Edge (org.kie.workbench.common.stunner.core.graph.Edge)85 View (org.kie.workbench.common.stunner.core.graph.content.view.View)59 Test (org.junit.Test)38 Graph (org.kie.workbench.common.stunner.core.graph.Graph)32 ViewConnector (org.kie.workbench.common.stunner.core.graph.content.view.ViewConnector)24 RuleViolation (org.kie.workbench.common.stunner.core.rule.RuleViolation)21 Bounds (org.kie.workbench.common.stunner.core.graph.content.Bounds)17 Point2D (org.kie.workbench.common.stunner.core.graph.content.view.Point2D)15 Element (org.kie.workbench.common.stunner.core.graph.Element)14 Metadata (org.kie.workbench.common.stunner.core.diagram.Metadata)13 DefinitionSet (org.kie.workbench.common.stunner.core.graph.content.definition.DefinitionSet)13 EdgeImpl (org.kie.workbench.common.stunner.core.graph.impl.EdgeImpl)13 Matchers.anyString (org.mockito.Matchers.anyString)13 List (java.util.List)12 BoundImpl (org.kie.workbench.common.stunner.core.graph.content.view.BoundImpl)12 BoundsImpl (org.kie.workbench.common.stunner.core.graph.content.view.BoundsImpl)12 Before (org.junit.Before)11 Definition (org.kie.workbench.common.stunner.core.graph.content.definition.Definition)11 Child (org.kie.workbench.common.stunner.core.graph.content.relationship.Child)11