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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations