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