Search in sources :

Example 1 with DefaultRuleViolations

use of org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations 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;
}
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) HashSet(java.util.HashSet)

Example 2 with DefaultRuleViolations

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

the class RuleManagerImpl method evaluate.

@Override
public RuleViolations evaluate(final RuleSet ruleSet, final RuleEvaluationContext context) {
    checkNotNull("ruleSet", ruleSet);
    checkNotNull("context", context);
    /*
            Consider:
            - If no rules present on the rule set, no resulting rule violation instances
            are expected
            - If rules present but no rule accepts the runtime context inputs, the context type
            defines if allow/or deny the evaluation
            - Otherwise return the rule violations produced by the handlers or extensions
         */
    final DefaultRuleViolations results = new DefaultRuleViolations();
    final boolean hasRules = ruleSet.getRules().iterator().hasNext();
    if (hasRules) {
        final boolean[] hasEvaluations = { false };
        ruleSet.getRules().forEach(rule -> {
            final Optional<RuleViolations> violations = evaluate(rule, context);
            if (violations.isPresent()) {
                hasEvaluations[0] = true;
                LOGGER.info("Rule Evaluation [" + rule + ", " + violations + "]");
                results.addViolations(violations.get());
            }
        });
        if (!hasEvaluations[0] && context.isDefaultDeny()) {
            return getDefaultViolationForContext(context);
        }
    }
    return results;
}
Also used : DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations)

Example 3 with DefaultRuleViolations

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

the class RuleExtensionMultiHandler method evaluate.

@Override
@SuppressWarnings("unchecked")
public RuleViolations evaluate(final RuleExtension rule, final RuleEvaluationContext context) {
    final List<RuleExtensionHandler> candidates = handlers.stream().filter(h -> isHandlerAccepted(h, rule, context)).collect(Collectors.toList());
    final DefaultRuleViolations result = new DefaultRuleViolations();
    candidates.forEach(candidate -> result.addViolations(candidate.evaluate(rule, context)));
    return result;
}
Also used : RuleEvaluationContext(org.kie.workbench.common.stunner.core.rule.RuleEvaluationContext) List(java.util.List) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations) Dependent(javax.enterprise.context.Dependent) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) LinkedList(java.util.LinkedList) Collectors(java.util.stream.Collectors) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations)

Example 4 with DefaultRuleViolations

use of org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations 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)

Example 5 with DefaultRuleViolations

use of org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations 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 DefaultRuleViolations results = new DefaultRuleViolations();
    final Optional<Element<? extends View<?>>> candidate = context.getCandidate();
    final String role = rule.getRole();
    final Set<String> roles = Collections.singleton(role);
    final Map<String, Integer> graphLabelCount = countLabels(context.getGraph(), roles);
    // Ensure processing the role even if not used along the graph, so
    // cardinality min rules can be evaluated.
    final int count = graphLabelCount.isEmpty() ? 0 : graphLabelCount.get(role);
    final Optional<CardinalityContext.Operation> operation = context.getOperation();
    results.addViolations(cardinalityEvaluationHandler.evaluate(rule, RuleContextBuilder.DomainContexts.cardinality(roles, count, operation)));
    if (candidate.isPresent()) {
        return GraphEvaluationHandlerUtils.addViolationsSourceUUID(candidate.get().getUUID(), results);
    }
    return results;
}
Also used : Element(org.kie.workbench.common.stunner.core.graph.Element) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) View(org.kie.workbench.common.stunner.core.graph.content.view.View)

Aggregations

DefaultRuleViolations (org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations)37 RuleEvaluationContext (org.kie.workbench.common.stunner.core.rule.RuleEvaluationContext)18 RuleSet (org.kie.workbench.common.stunner.core.rule.RuleSet)18 RuleViolations (org.kie.workbench.common.stunner.core.rule.RuleViolations)16 Test (org.junit.Test)13 RuleViolation (org.kie.workbench.common.stunner.core.rule.RuleViolation)13 ContainmentRuleViolation (org.kie.workbench.common.stunner.core.rule.violations.ContainmentRuleViolation)12 Edge (org.kie.workbench.common.stunner.core.graph.Edge)11 Node (org.kie.workbench.common.stunner.core.graph.Node)9 View (org.kie.workbench.common.stunner.core.graph.content.view.View)7 Before (org.junit.Before)5 Graph (org.kie.workbench.common.stunner.core.graph.Graph)5 ArrayList (java.util.ArrayList)4 LinkedList (java.util.LinkedList)4 List (java.util.List)3 TestScopeModelFactory (org.kie.workbench.common.stunner.backend.definition.factory.TestScopeModelFactory)3 BPMNDefinitionSet (org.kie.workbench.common.stunner.bpmn.BPMNDefinitionSet)3 BPMNDiagramMarshaller (org.kie.workbench.common.stunner.bpmn.backend.BPMNDiagramMarshaller)3 BPMNDirectDiagramMarshaller (org.kie.workbench.common.stunner.bpmn.backend.BPMNDirectDiagramMarshaller)3 BPMNGraphObjectBuilderFactory (org.kie.workbench.common.stunner.bpmn.backend.marshall.json.builder.BPMNGraphObjectBuilderFactory)3