Search in sources :

Example 1 with GraphEvaluationState

use of org.kie.workbench.common.stunner.core.rule.context.GraphEvaluationState 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 GraphEvaluationState state = context.getState();
    final Graph<?, Node> graph = (Graph<?, Node>) state.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;
}
Also used : RuleViolationImpl(org.kie.workbench.common.stunner.core.rule.violations.RuleViolationImpl) Node(org.kie.workbench.common.stunner.core.graph.Node) View(org.kie.workbench.common.stunner.core.graph.content.view.View) TreeWalkTraverseProcessor(org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessor) Graph(org.kie.workbench.common.stunner.core.graph.Graph) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) Edge(org.kie.workbench.common.stunner.core.graph.Edge) GraphEvaluationState(org.kie.workbench.common.stunner.core.rule.context.GraphEvaluationState) HashSet(java.util.HashSet)

Example 2 with GraphEvaluationState

use of org.kie.workbench.common.stunner.core.rule.context.GraphEvaluationState 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 3 with GraphEvaluationState

use of org.kie.workbench.common.stunner.core.rule.context.GraphEvaluationState in project kie-wb-common by kiegroup.

the class ElementCardinalityEvaluationHandler method evaluate.

@Override
@SuppressWarnings("unchecked")
public RuleViolations evaluate(final Occurrences rule, final ElementCardinalityContext context) {
    final GraphEvaluationState state = context.getState();
    final String role = rule.getRole();
    final Set<String> roles = Collections.singleton(role);
    final Map<String, Integer> graphLabelCount = countLabels(state, roles);
    final Collection<Element<? extends View<?>>> candidates = context.getCandidates();
    final Collection<Element<? extends View<?>>> filteredCandidates = candidates.stream().filter(candidate -> accepts(rule, context, candidate)).collect(Collectors.toSet());
    final int size = filteredCandidates.size();
    final int count = graphLabelCount.isEmpty() ? 0 : graphLabelCount.get(role);
    final Optional<CardinalityContext.Operation> operation = context.getOperation();
    final DefaultRuleViolations results = new DefaultRuleViolations();
    // Ensure processing the role even if not used along the graph, so
    // cardinality min rules can be evaluated.
    final Function<String, RuleViolations> evaluator = uuid -> evaluate(rule, uuid, roles, count, size, operation);
    if (size == 0) {
        results.addViolations(evaluator.apply(null));
    } else {
        filteredCandidates.forEach(candidate -> results.addViolations(evaluator.apply(candidate.getUUID())));
    }
    return results;
}
Also used : RuleEvaluationContextBuilder(org.kie.workbench.common.stunner.core.rule.context.impl.RuleEvaluationContextBuilder) HashMap(java.util.HashMap) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) Function(java.util.function.Function) View(org.kie.workbench.common.stunner.core.graph.content.view.View) GraphEvaluationState(org.kie.workbench.common.stunner.core.rule.context.GraphEvaluationState) GraphUtils(org.kie.workbench.common.stunner.core.graph.util.GraphUtils) Inject(javax.inject.Inject) ElementCardinalityContext(org.kie.workbench.common.stunner.core.rule.context.ElementCardinalityContext) Map(java.util.Map) Element(org.kie.workbench.common.stunner.core.graph.Element) DefinitionManager(org.kie.workbench.common.stunner.core.api.DefinitionManager) StreamSupport(java.util.stream.StreamSupport) CardinalityContext(org.kie.workbench.common.stunner.core.rule.context.CardinalityContext) RuleEvaluationHandler(org.kie.workbench.common.stunner.core.rule.RuleEvaluationHandler) Collection(java.util.Collection) Set(java.util.Set) Occurrences(org.kie.workbench.common.stunner.core.rule.impl.Occurrences) Collectors(java.util.stream.Collectors) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Collections(java.util.Collections) Node(org.kie.workbench.common.stunner.core.graph.Node) GraphEvaluationHandlerUtils.addViolationsSourceUUID(org.kie.workbench.common.stunner.core.rule.handler.impl.GraphEvaluationHandlerUtils.addViolationsSourceUUID) Element(org.kie.workbench.common.stunner.core.graph.Element) View(org.kie.workbench.common.stunner.core.graph.content.view.View) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) GraphEvaluationState(org.kie.workbench.common.stunner.core.rule.context.GraphEvaluationState)

Example 4 with GraphEvaluationState

use of org.kie.workbench.common.stunner.core.rule.context.GraphEvaluationState in project kie-wb-common by kiegroup.

the class ConnectorParentsMatchContainmentHandlerTest method setup.

@Before
@SuppressWarnings("unchecked")
public void setup() throws Exception {
    super.setup();
    candidates = Collections.singletonList(nodeA);
    when(graphHandler.getDefinitionAdapter().getId(eq(connectorDef))).thenReturn(DefinitionId.build(DEF_EDGE_ID));
    this.connector = graphHandler.newEdge(EDGE_UUID, Optional.of(connectorDef));
    GraphEvaluationState state = new StatefulGraphEvaluationState(graphHandler.graph);
    when(containmentContext.getState()).thenReturn(state);
    when(ruleExtension.getId()).thenReturn(DEF_EDGE_ID);
    when(ruleExtension.getArguments()).thenReturn(new String[] { "violation1" });
    tested = new ConnectorParentsMatchContainmentHandler(graphHandler.getDefinitionManager(), new TreeWalkTraverseProcessorImpl());
}
Also used : StatefulGraphEvaluationState(org.kie.workbench.common.stunner.core.rule.context.impl.StatefulGraphEvaluationState) GraphEvaluationState(org.kie.workbench.common.stunner.core.rule.context.GraphEvaluationState) TreeWalkTraverseProcessorImpl(org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessorImpl) StatefulGraphEvaluationState(org.kie.workbench.common.stunner.core.rule.context.impl.StatefulGraphEvaluationState) Before(org.junit.Before)

Example 5 with GraphEvaluationState

use of org.kie.workbench.common.stunner.core.rule.context.GraphEvaluationState in project kie-wb-common by kiegroup.

the class ConnectorParentsMatchConnectionHandlerTest method setup.

@Before
@SuppressWarnings("unchecked")
public void setup() throws Exception {
    super.setup();
    this.graph = graphHandler.graph;
    when(graphHandler.getDefinitionAdapter().getId(eq(connectorDef))).thenReturn(DefinitionId.build(DEF_EDGE_ID));
    this.connector = graphHandler.newEdge(EDGE_UUID, Optional.of(connectorDef));
    GraphEvaluationState state = new StatefulGraphEvaluationState(graph);
    when(connectionContext.getState()).thenReturn(state);
    when(connectionContext.getConnector()).thenReturn(connector);
    when(ruleExtension.getId()).thenReturn(DEF_EDGE_ID);
    when(ruleExtension.getArguments()).thenReturn(new String[] { "violation1" });
    tested = new ConnectorParentsMatchConnectionHandler(graphHandler.getDefinitionManager());
}
Also used : StatefulGraphEvaluationState(org.kie.workbench.common.stunner.core.rule.context.impl.StatefulGraphEvaluationState) GraphEvaluationState(org.kie.workbench.common.stunner.core.rule.context.GraphEvaluationState) StatefulGraphEvaluationState(org.kie.workbench.common.stunner.core.rule.context.impl.StatefulGraphEvaluationState) Before(org.junit.Before)

Aggregations

GraphEvaluationState (org.kie.workbench.common.stunner.core.rule.context.GraphEvaluationState)5 Node (org.kie.workbench.common.stunner.core.graph.Node)3 DefaultRuleViolations (org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations)3 Collection (java.util.Collection)2 Optional (java.util.Optional)2 ApplicationScoped (javax.enterprise.context.ApplicationScoped)2 Inject (javax.inject.Inject)2 Before (org.junit.Before)2 DefinitionManager (org.kie.workbench.common.stunner.core.api.DefinitionManager)2 Edge (org.kie.workbench.common.stunner.core.graph.Edge)2 Graph (org.kie.workbench.common.stunner.core.graph.Graph)2 View (org.kie.workbench.common.stunner.core.graph.content.view.View)2 TreeWalkTraverseProcessor (org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessor)2 GraphUtils (org.kie.workbench.common.stunner.core.graph.util.GraphUtils)2 RuleViolations (org.kie.workbench.common.stunner.core.rule.RuleViolations)2 StatefulGraphEvaluationState (org.kie.workbench.common.stunner.core.rule.context.impl.StatefulGraphEvaluationState)2 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1