use of org.kie.workbench.common.stunner.core.rule.RuleViolations in project kie-wb-common by kiegroup.
the class CanvasLayoutUtils method canContain.
private boolean canContain(final CanvasHandler canvasHandler, final Node container, final Node candidate) {
boolean canContain = true;
NodeContainmentContext containmentContext = RuleContextBuilder.GraphContexts.containment(canvasHandler.getDiagram().getGraph(), container, candidate);
String definitionSetId = canvasHandler.getDiagram().getMetadata().getDefinitionSetId();
Object definitionSet = definitionManager.definitionSets().getDefinitionSetById(definitionSetId);
RuleSet ruleSet = definitionManager.adapters().forRules().getRuleSet(definitionSet);
RuleViolations violations = ruleManager.evaluate(ruleSet, containmentContext);
if (violations.violations(Violation.Type.ERROR).iterator().hasNext()) {
canContain = false;
}
return canContain;
}
use of org.kie.workbench.common.stunner.core.rule.RuleViolations in project kie-wb-common by kiegroup.
the class CommonLookups method getAllowedTargetDefinitions.
/**
* Returns the allowed definition identifiers that can be used as target node for the given source node and
* the given edge (connector) identifier.
* <p>
* TODO: Handle several result pages.
*/
@SuppressWarnings("unchecked")
public <T> Set<Object> getAllowedTargetDefinitions(final String defSetId, final Graph<?, ? extends Node> graph, final Node<? extends Definition<T>, ? extends Edge> sourceNode, final String edgeId, final int page, final int pageSize) {
final Set<Object> result = new LinkedHashSet<>();
if (null != defSetId && null != graph && null != sourceNode && null != edgeId) {
final T definition = sourceNode.getContent().getDefinition();
final RuleSet ruleSet = getRuleSet(defSetId);
log(Level.FINEST, "*** Checking the target definitions allowed " + "for [" + definition + "] and using the " + "connector [" + edgeId + "] ***");
// Check outgoing connectors cardinality for the source node ( plus the new one to be added ).
final int outConnectorsCount = countOutgoingEdges(sourceNode, edgeId);
log(Level.FINEST, "The source node has " + outConnectorsCount + "] outgoing connections.");
final RuleViolations oev = ruleManager.evaluate(ruleSet, RuleContextBuilder.DomainContexts.edgeCardinality(sourceNode.getLabels(), edgeId, outConnectorsCount, EdgeCardinalityContext.Direction.OUTGOING, Optional.of(CardinalityContext.Operation.ADD)));
final boolean oeCardinalityAllowed = pass(oev);
log(Level.FINEST, "Outgoing edge cardinality rules evaluation " + "result = [" + oeCardinalityAllowed + "]");
if (oeCardinalityAllowed) {
// Obtain allowed target roles that pass connection rules.
final Set<String> allowedConnectionRoles = getConnectionRulesAllowedTargets(defSetId, definition, edgeId, page, pageSize);
log(Level.FINEST, "Allowed target roles that pass connection rules " + "ARE [" + allowedConnectionRoles + "]");
if (null != allowedConnectionRoles) {
// Obtain a first set of candidate Defintiion identifiers.
final Set<String> allowedDefinitions = getDefinitions(defSetId, allowedConnectionRoles);
log(Level.FINEST, "Allowed target definitions that pass connection rules " + "ARE [" + allowedConnectionRoles + "]");
if (null != allowedDefinitions) {
final Map<String, Integer> graphLabelCount = GraphUtils.getLabelsCount(graph, allowedConnectionRoles);
final int inConnectorsCount = countIncomingEdges(sourceNode, edgeId);
allowedDefinitions.stream().forEach(defId -> {
final Object targetDefinition = createDefinition(defId);
if (null != targetDefinition) {
final Set<String> targetDefinitionRoles = getDefinitionManager().adapters().forDefinition().getLabels(targetDefinition);
// Check cardinality for each of the roles for this potential target node.
final boolean hasCardinalityViolations = targetDefinitionRoles.stream().filter(role -> {
final Integer roleCount = Optional.ofNullable(graphLabelCount.get(role)).orElse(0);
final RuleViolations violations = ruleManager.evaluate(ruleSet, RuleContextBuilder.DomainContexts.cardinality(Collections.singleton(role), roleCount, Optional.of(CardinalityContext.Operation.ADD)));
return !pass(violations);
}).findFirst().isPresent();
log(Level.FINEST, "Cardinality rules evaluation " + "result = [" + hasCardinalityViolations + "]");
if (!hasCardinalityViolations) {
// Check incoming connector cardinality for each the target node.
final RuleViolations iev = ruleManager.evaluate(ruleSet, RuleContextBuilder.DomainContexts.edgeCardinality(Collections.singleton(defId), edgeId, inConnectorsCount, EdgeCardinalityContext.Direction.INCOMING, Optional.of(CardinalityContext.Operation.ADD)));
final boolean ieCardinalityAllowed = pass(iev);
log(Level.FINEST, "Incoming edge cardinality rules evaluation " + "result = [" + ieCardinalityAllowed + "]");
if (ieCardinalityAllowed) {
// This potential node can be used as target one, as it passes all rule checks.
result.add(targetDefinition);
}
}
}
});
return result;
}
}
}
}
return result;
}
use of org.kie.workbench.common.stunner.core.rule.RuleViolations in project kie-wb-common by kiegroup.
the class GraphConnectionEvaluationHandler method evaluate.
@Override
public RuleViolations evaluate(final CanConnect rule, final GraphConnectionContext context) {
final Edge<? extends View<?>, ? extends Node> connector = context.getConnector();
final Node<? extends View<?>, ? extends Edge> source = context.getSource().orElse(null);
final Node<? extends View<?>, ? extends Edge> target = context.getTarget().orElse(null);
if (source == null || target == null) {
return new DefaultRuleViolations();
}
final Set<String> edgeLabels = evalUtils.getLabels(connector);
final Optional<Set<String>> sourceLabels = Optional.of(evalUtils.getLabels(source));
final Optional<Set<String>> targetLabels = Optional.of(evalUtils.getLabels(target));
final DefaultRuleViolations result = new DefaultRuleViolations();
edgeLabels.stream().filter(pr -> rule.getRole().equals(pr)).forEach(pr -> result.addViolations(connectionEvaluationHandler.evaluate(rule, RuleContextBuilder.DomainContexts.connection(pr, sourceLabels, targetLabels))));
return GraphEvaluationHandlerUtils.addViolationsSourceUUID(connector.getUUID(), result);
}
use of org.kie.workbench.common.stunner.core.rule.RuleViolations in project kie-wb-common by kiegroup.
the class AddChildNodeCommandTest method testNotAllowed.
@Test
@SuppressWarnings("unchecked")
public void testNotAllowed() {
final RuleViolations FAILED_VIOLATIONS = new DefaultRuleViolations().addViolation(new ContainmentRuleViolation(graph.getUUID(), PARENT_UUID));
when(ruleManager.evaluate(any(RuleSet.class), any(RuleEvaluationContext.class))).thenReturn(FAILED_VIOLATIONS);
CommandResult<RuleViolation> result = tested.allow(graphCommandExecutionContext);
assertEquals(CommandResult.Type.ERROR, result.getType());
}
use of org.kie.workbench.common.stunner.core.rule.RuleViolations in project kie-wb-common by kiegroup.
the class AddNodeCommandTest method testExecuteCheckFailed.
@Test
@SuppressWarnings("unchecked")
public void testExecuteCheckFailed() {
final RuleViolations FAILED_VIOLATIONS = new DefaultRuleViolations().addViolation(new ContainmentRuleViolation(graph.getUUID(), UUID));
when(ruleManager.evaluate(any(RuleSet.class), any(RuleEvaluationContext.class))).thenReturn(FAILED_VIOLATIONS);
CommandResult<RuleViolation> result = tested.execute(graphCommandExecutionContext);
assertEquals(CommandResult.Type.ERROR, result.getType());
verify(graph, times(0)).addNode(eq(node));
verify(graphIndex, times(0)).addNode(eq(node));
verify(graphIndex, times(0)).addEdge(any(Edge.class));
verify(graph, times(0)).removeNode(eq(UUID));
verify(graphIndex, times(0)).removeNode(eq(node));
verify(graphIndex, times(0)).removeEdge(any(Edge.class));
}
Aggregations