use of org.kie.workbench.common.stunner.core.rule.RuleViolation in project kie-wb-common by kiegroup.
the class SetConnectionSourceNodeCommand method check.
@SuppressWarnings("unchecked")
protected CommandResult<RuleViolation> check(final GraphCommandExecutionContext context) {
final GraphCommandResultBuilder resultBuilder = new GraphCommandResultBuilder();
final Node<View<?>, Edge> sourceNode = (Node<View<?>, Edge>) getSourceNode(context);
final Edge<View<?>, Node> edge = (Edge<View<?>, Node>) getEdge(context);
final Node<? extends View<?>, Edge> lastSourceNode = edge.getSourceNode();
// Only check for rules in case the connector's source node is a different one.
if ((null == lastSourceNode && null != sourceNode) || (null != lastSourceNode && (!lastSourceNode.equals(sourceNode)))) {
// New connection being made
final Collection<RuleViolation> connectionRuleViolations = doEvaluate(context, RuleContextBuilder.GraphContexts.connection(getGraph(context), edge, Optional.ofNullable(sourceNode), Optional.ofNullable(targetNode)));
resultBuilder.addViolations(connectionRuleViolations);
final Node<View<?>, Edge> currentSource = edge.getSourceNode();
// If the edge has an outoutgoing source node, check cardinality for removing it.
if (null != currentSource) {
final Collection<RuleViolation> cardinalityRuleViolations = doEvaluate(context, RuleContextBuilder.GraphContexts.edgeCardinality(getGraph(context), currentSource, edge, EdgeCardinalityContext.Direction.OUTGOING, Optional.of(CardinalityContext.Operation.DELETE)));
resultBuilder.addViolations(cardinalityRuleViolations);
}
// If the new source node exist, evaluate cardinality rules for this edge.
if (null != sourceNode) {
final Collection<RuleViolation> cardinalityRuleViolations = doEvaluate(context, RuleContextBuilder.GraphContexts.edgeCardinality(getGraph(context), sourceNode, edge, EdgeCardinalityContext.Direction.OUTGOING, Optional.of(CardinalityContext.Operation.ADD)));
resultBuilder.addViolations(cardinalityRuleViolations);
}
}
return resultBuilder.build();
}
use of org.kie.workbench.common.stunner.core.rule.RuleViolation in project kie-wb-common by kiegroup.
the class SetParentNodeCommand method execute.
@Override
@SuppressWarnings("unchecked")
public CommandResult<RuleViolation> execute(final GraphCommandExecutionContext context) {
final CommandResult<RuleViolation> results = allow(context);
if (!results.getType().equals(CommandResult.Type.ERROR)) {
final Node<?, Edge> parent = getParent(context);
final Node<?, Edge> candidate = getCandidate(context);
// TODO: Create a ParentEdgeFactory iface extending EdgeFactory using as content generics type Relationship
final String uuid = UUID.uuid();
final Edge<Parent, Node> edge = new EdgeImpl<>(uuid);
edge.setContent(new Parent());
edge.setSourceNode(parent);
edge.setTargetNode(candidate);
parent.getOutEdges().add(edge);
candidate.getInEdges().add(edge);
getMutableIndex(context).addEdge(edge);
}
return results;
}
use of org.kie.workbench.common.stunner.core.rule.RuleViolation in project kie-wb-common by kiegroup.
the class GraphValidatorImpl method validate.
/**
* Performs the validation for the <code>graph</code> instance.
* @param graph The instance to validate.
* @param aRuleSet An optional rule set instance to validate against it. If not present, the default
* rule set for the the graph will be used.
* @param graphValidatorConsumer An optional consumer for the graph instance when is being validated.
* @param nodeValidatorConsumer An optional consumer each node instance when being validated.
* @param edgeValidatorConsumer An optional consumer each edge instance when being validated.
* @param resultConsumer The consumer for all the resulting validation violations produced during the
* validator for the graph, and all of its nodes and edges. It's being called once the
* validation has been completed.
*/
@SuppressWarnings("unchecked")
void validate(final Graph graph, final Optional<RuleSet> aRuleSet, final Optional<BiConsumer<Graph, Collection<RuleViolation>>> graphValidatorConsumer, final Optional<BiConsumer<Node, Collection<RuleViolation>>> nodeValidatorConsumer, final Optional<BiConsumer<Edge, Collection<RuleViolation>>> edgeValidatorConsumer, Consumer<Collection<RuleViolation>> resultConsumer) {
final RuleSet ruleSet = aRuleSet.orElse(getRuleSet(graph));
final ViolationsSet violations = new ViolationsSet();
treeWalkTraverseProcessor.traverse(graph, new AbstractTreeTraverseCallback<org.kie.workbench.common.stunner.core.graph.Graph, Node, Edge>() {
private final Stack<Node> currentParents = new Stack<Node>();
@Override
public void startGraphTraversal(final org.kie.workbench.common.stunner.core.graph.Graph graph) {
super.startGraphTraversal(graph);
currentParents.clear();
// Evaluate the graph's cardinality rules.
final Set<RuleViolation> graphCardinalityViolations = violations.addViolations(evaluateCardinality(ruleSet, graph));
graphValidatorConsumer.ifPresent(g -> g.accept(graph, graphCardinalityViolations));
}
@Override
public boolean startEdgeTraversal(final Edge edge) {
super.startEdgeTraversal(edge);
final Object content = edge.getContent();
final ViolationsSet edgeViolations = new ViolationsSet();
if (content instanceof Child) {
this.currentParents.push(edge.getSourceNode());
} else if (content instanceof View) {
final Optional<Node<? extends View<?>, ? extends Edge>> sourceOpt = Optional.ofNullable(edge.getSourceNode());
final Optional<Node<? extends View<?>, ? extends Edge>> targetOpt = Optional.ofNullable(edge.getTargetNode());
// Check not empty connections.
final Optional<RuleViolation> emptyConnectionViolation = evaluateNotEmptyConnections(graph, edge, sourceOpt, targetOpt);
emptyConnectionViolation.ifPresent(edgeViolations::add);
// Evaluate connection rules.
edgeViolations.addViolations(evaluateConnection(ruleSet, graph, edge, sourceOpt, targetOpt));
// Evaluate connector cardinality rules for this edge.
if (null != edge.getTargetNode()) {
edgeViolations.addViolations(evaluateIncomingEdgeCardinality(ruleSet, graph, edge));
}
if (null != edge.getSourceNode()) {
edgeViolations.addViolations(evaluateOutgoingEdgeCardinality(ruleSet, graph, edge));
}
} else if (content instanceof Dock) {
final Node parent = edge.getSourceNode();
final Node docked = edge.getTargetNode();
// Evaluate docking rules for the source & target nodes.
edgeViolations.addViolations(evaluateDocking(ruleSet, graph, parent, docked));
}
edgeValidatorConsumer.ifPresent(c -> c.accept(edge, edgeViolations));
violations.addAll(edgeViolations);
return true;
}
@Override
public void endEdgeTraversal(final Edge edge) {
super.endEdgeTraversal(edge);
if (edge.getContent() instanceof Child) {
this.currentParents.pop();
}
}
@Override
public boolean startNodeTraversal(final Node node) {
super.startNodeTraversal(node);
final Collection<RuleViolation> nodeViolations = evaluateNode(node, currentParents.isEmpty() ? null : currentParents.peek());
nodeValidatorConsumer.ifPresent(c -> c.accept(node, nodeViolations));
return true;
}
@Override
public void endGraphTraversal() {
super.endGraphTraversal();
// Finished - feed the consumer instance.
resultConsumer.accept(violations);
}
private Collection<RuleViolation> evaluateNode(final Node node, final Node parent) {
// Evaluate containment rules for this node.
return violations.addViolations(evaluateContainment(ruleSet, graph, null != parent ? parent : graph, node));
}
});
}
use of org.kie.workbench.common.stunner.core.rule.RuleViolation in project kie-wb-common by kiegroup.
the class CompositeCommandTest method testExecuteFailedThenUndo.
@Test
@SuppressWarnings("unchecked")
public void testExecuteFailedThenUndo() {
Command c1 = mock(Command.class);
when(c1.allow(eq(commandExecutionContext))).thenReturn(GraphCommandResultBuilder.SUCCESS);
when(c1.execute(eq(commandExecutionContext))).thenReturn(GraphCommandResultBuilder.SUCCESS);
Command c2 = mock(Command.class);
when(c2.allow(eq(commandExecutionContext))).thenReturn(GraphCommandResultBuilder.SUCCESS);
when(c2.execute(eq(commandExecutionContext))).thenReturn(GraphCommandResultBuilder.SUCCESS);
CommandResult<RuleViolation> failed = new CommandResultImpl<>(CommandResult.Type.ERROR, Collections.singletonList(new RuleViolationImpl("failed")));
Command c3 = mock(Command.class);
when(c3.allow(eq(commandExecutionContext))).thenReturn(GraphCommandResultBuilder.SUCCESS);
when(c3.execute(eq(commandExecutionContext))).thenReturn(failed);
CompositeCommand composite = new CompositeCommand.Builder<>().addCommand(c1).addCommand(c2).addCommand(c3).build();
CommandResult result = composite.execute(commandExecutionContext);
assertEquals(CommandResult.Type.ERROR, result.getType());
verify(c1, times(1)).undo(eq(commandExecutionContext));
verify(c2, times(1)).undo(eq(commandExecutionContext));
verify(c3, times(1)).undo(eq(commandExecutionContext));
}
use of org.kie.workbench.common.stunner.core.rule.RuleViolation in project kie-wb-common by kiegroup.
the class CanvasHighlightTest method testViolations.
@Test
@SuppressWarnings("unchecked")
public void testViolations() {
final List<RuleViolation> violations = new LinkedList<>();
final RuleViolationImpl v1 = new RuleViolationImpl("m1");
v1.setUUID(ID1);
final RuleViolationImpl v2 = new RuleViolationImpl("m2");
v2.setUUID(ID2);
violations.add(v1);
violations.add(v2);
tested.invalid(violations);
verify(shape1, times(1)).applyState(eq(ShapeState.INVALID));
verify(shape2, times(1)).applyState(eq(ShapeState.INVALID));
verify(canvasView, times(2)).setCursor(eq(AbstractCanvas.Cursors.NOT_ALLOWED));
verify(canvas, times(1)).draw();
}
Aggregations