Search in sources :

Example 1 with ParentTypesMatcher

use of org.kie.workbench.common.stunner.core.graph.util.ParentTypesMatcher in project kie-wb-common by kiegroup.

the class ConnectorParentsMatchContainmentHandler method evaluateSingleContainment.

@SuppressWarnings("unchecked")
private void evaluateSingleContainment(final DefaultRuleViolations result, final RuleExtension rule, final NodeContainmentContext context, final Node<? extends Definition<?>, ? extends Edge> candidate) {
    final GraphEvaluationState state = context.getState();
    final Graph<?, ? extends Node> graph = context.getState().getGraph();
    final String connectorId = rule.getId();
    // Walk throw the graph and evaluate connector source and target nodes parent match.
    treeWalkTraverseProcessor.traverse(graph, candidate, new AbstractTreeTraverseCallback<Graph, Node, Edge>() {

        @Override
        public boolean startNodeTraversal(final Node node) {
            // Process incoming edges into the node as well.
            final List<? extends Edge> inEdges = node.getInEdges();
            if (null != inEdges) {
                inEdges.stream().forEach(this::process);
            }
            return true;
        }

        @Override
        public boolean startEdgeTraversal(final Edge edge) {
            return process(edge);
        }

        private boolean process(final Edge edge) {
            final Optional<String> eId = getId(definitionManager, edge);
            if (eId.isPresent() && connectorId.equals(eId.get())) {
                final Node sourceNode = state.getConnectionState().getSource(edge);
                final Node targetNode = state.getConnectionState().getTarget(edge);
                final boolean valid = new ParentTypesMatcher(() -> definitionManager, e -> getParent(context, e), rule.getTypeArguments()).matcher().test(sourceNode, targetNode);
                if (!valid) {
                    addViolation(edge.getUUID(), rule, result);
                }
            }
            return true;
        }
    });
}
Also used : StatefulContainmentState.getParent(org.kie.workbench.common.stunner.core.rule.context.impl.StatefulGraphEvaluationState.StatefulContainmentState.getParent) Collection(java.util.Collection) Edge(org.kie.workbench.common.stunner.core.graph.Edge) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) Definition(org.kie.workbench.common.stunner.core.graph.content.definition.Definition) GraphEvaluationState(org.kie.workbench.common.stunner.core.rule.context.GraphEvaluationState) GraphUtils(org.kie.workbench.common.stunner.core.graph.util.GraphUtils) Inject(javax.inject.Inject) NodeContainmentContext(org.kie.workbench.common.stunner.core.rule.context.NodeContainmentContext) List(java.util.List) Graph(org.kie.workbench.common.stunner.core.graph.Graph) TreeWalkTraverseProcessor(org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessor) Stream(java.util.stream.Stream) ParentTypesMatcher(org.kie.workbench.common.stunner.core.graph.util.ParentTypesMatcher) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations) GraphEvaluationHandlerUtils(org.kie.workbench.common.stunner.core.rule.handler.impl.GraphEvaluationHandlerUtils) AbstractTreeTraverseCallback(org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.AbstractTreeTraverseCallback) Optional(java.util.Optional) DefinitionManager(org.kie.workbench.common.stunner.core.api.DefinitionManager) ApplicationScoped(javax.enterprise.context.ApplicationScoped) RuleExtension(org.kie.workbench.common.stunner.core.rule.ext.RuleExtension) Node(org.kie.workbench.common.stunner.core.graph.Node) Graph(org.kie.workbench.common.stunner.core.graph.Graph) Optional(java.util.Optional) ParentTypesMatcher(org.kie.workbench.common.stunner.core.graph.util.ParentTypesMatcher) Node(org.kie.workbench.common.stunner.core.graph.Node) List(java.util.List) Edge(org.kie.workbench.common.stunner.core.graph.Edge) GraphEvaluationState(org.kie.workbench.common.stunner.core.rule.context.GraphEvaluationState)

Example 2 with ParentTypesMatcher

use of org.kie.workbench.common.stunner.core.graph.util.ParentTypesMatcher in project kie-wb-common by kiegroup.

the class ConnectorParentsMatchConnectionHandler method evaluateConnection.

@SuppressWarnings("unchecked")
private RuleViolations evaluateConnection(final RuleExtension rule, final GraphConnectionContext context) {
    final Edge connector = context.getConnector();
    final GraphEvaluationState.ConnectionState connectionState = context.getState().getConnectionState();
    final Node<? extends View<?>, ? extends Edge> sourceNode = (Node<? extends View<?>, ? extends Edge>) context.getSource().orElse(connectionState.getSource(connector));
    final Node<? extends View<?>, ? extends Edge> targetNode = (Node<? extends View<?>, ? extends Edge>) context.getTarget().orElse(connectionState.getTarget(connector));
    final DefaultRuleViolations result = new DefaultRuleViolations();
    final GraphEvaluationState.ContainmentState containmentState = context.getState().getContainmentState();
    final boolean isValid = new ParentTypesMatcher(() -> definitionManager, containmentState::getParent, rule.getTypeArguments()).matcher().test(sourceNode, targetNode);
    if (!isValid) {
        addViolation(context.getConnector().getUUID(), rule, result);
    }
    return result;
}
Also used : ParentTypesMatcher(org.kie.workbench.common.stunner.core.graph.util.ParentTypesMatcher) Node(org.kie.workbench.common.stunner.core.graph.Node) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) Edge(org.kie.workbench.common.stunner.core.graph.Edge) View(org.kie.workbench.common.stunner.core.graph.content.view.View) GraphEvaluationState(org.kie.workbench.common.stunner.core.rule.context.GraphEvaluationState)

Aggregations

Edge (org.kie.workbench.common.stunner.core.graph.Edge)2 Node (org.kie.workbench.common.stunner.core.graph.Node)2 ParentTypesMatcher (org.kie.workbench.common.stunner.core.graph.util.ParentTypesMatcher)2 GraphEvaluationState (org.kie.workbench.common.stunner.core.rule.context.GraphEvaluationState)2 DefaultRuleViolations (org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations)2 Collection (java.util.Collection)1 List (java.util.List)1 Optional (java.util.Optional)1 Stream (java.util.stream.Stream)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1 Inject (javax.inject.Inject)1 DefinitionManager (org.kie.workbench.common.stunner.core.api.DefinitionManager)1 Graph (org.kie.workbench.common.stunner.core.graph.Graph)1 Definition (org.kie.workbench.common.stunner.core.graph.content.definition.Definition)1 View (org.kie.workbench.common.stunner.core.graph.content.view.View)1 AbstractTreeTraverseCallback (org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.AbstractTreeTraverseCallback)1 TreeWalkTraverseProcessor (org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessor)1 GraphUtils (org.kie.workbench.common.stunner.core.graph.util.GraphUtils)1 RuleViolations (org.kie.workbench.common.stunner.core.rule.RuleViolations)1 NodeContainmentContext (org.kie.workbench.common.stunner.core.rule.context.NodeContainmentContext)1