@Inject public ConnectorParentsMatchContainmentHandler(final DefinitionManager definitionManager, final TreeWalkTraverseProcessor treeWalkTraverseProcessor) { this.definitionManager = definitionManager; this.treeWalkTraverseProcessor = treeWalkTraverseProcessor; this.evalUtils = new GraphEvaluationHandlerUtils(definitionManager); }
public static RuleViolations addViolationsSourceUUID(final String uuid, final RuleViolations result) { result.violations().forEach(v -> addViolationSourceUUID(uuid, v)); return result; }
private boolean acceptsConnection(final RuleExtension rule, final GraphConnectionContext context) { final Edge<? extends View<?>, ? extends Node> connector = context.getConnector(); return evalUtils.getElementDefinitionId(connector).equals(rule.getId()); }
@Test @SuppressWarnings("unchecked") public void testEvaluateInMaxOneSucess() { when(evalUtils.countEdges(anyString(), any(List.class))).thenReturn(0); when(context.getDirection()).thenReturn(EdgeCardinalityContext.Direction.INCOMING); when(context.getOperation()).thenReturn(Optional.of(CardinalityContext.Operation.ADD)); RuleViolations violations = tested.evaluate(RULE_IN_MAX_1, context); assertNotNull(violations); assertFalse(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); }
@Override @SuppressWarnings("unchecked") public RuleViolations evaluate(final EdgeOccurrences rule, final ConnectorCardinalityContext context) { final DefaultRuleViolations result = new DefaultRuleViolations(); final Node<? extends View<?>, ? extends Edge> candidate = (Node<? extends View<?>, ? extends Edge>) context.getCandidate(); final Edge<? extends View<?>, Node> edge = context.getEdge(); final Optional<CardinalityContext.Operation> operation = context.getOperation(); final EdgeCardinalityContext.Direction direction = context.getDirection(); final List<? extends Edge> edges = isIncoming(direction) ? candidate.getInEdges() : candidate.getOutEdges(); final String edgeId = evalUtils.getElementDefinitionId(edge); final int count = evalUtils.countEdges(edgeId, edges); // Delegate to the domain model cardinality rule manager. result.addViolations( edgeCardinalityEvaluationHandler .evaluate(rule, RuleContextBuilder.DomainContexts.edgeCardinality(candidate.getLabels(), edgeId, count, rule.getDirection(), operation)) ); return GraphEvaluationHandlerUtils.addViolationsSourceUUID(edge.getUUID(), result); }
@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); } }
@Override public boolean accepts(final EdgeOccurrences rule, final ConnectorCardinalityContext context) { final Edge<? extends View<?>, Node> edge = context.getEdge(); final Element<? extends View<?>> candidate = context.getCandidate(); final String edgeId = evalUtils.getElementDefinitionId(edge); final Set<String> candidateRoles = evalUtils.getLabels(candidate); // Take into account that there is no need to provide the candidate count value, as not necessary // just to check if the handler accepts the runtime rule and candidates. return edgeCardinalityEvaluationHandler.accepts(rule, RuleContextBuilder.DomainContexts.edgeCardinality(candidateRoles, edgeId, -1, context.getDirection(), context.getOperation())); }
@Override public boolean accepts(final CanConnect rule, final GraphConnectionContext context) { final Edge<? extends View<?>, ? extends Node> connector = context.getConnector(); final Set<String> labels = evalUtils.getLabels(connector); return labels.stream() .filter(cr -> rule.getRole().equals(cr) && // As for acceptance the delegated handler only needs the connector id, no need // to calculate roles for current source/target nodes. connectionEvaluationHandler.accepts(rule, RuleContextBuilder.DomainContexts.connection(cr, Optional.empty(), Optional.empty()))) .findAny() .isPresent(); }
@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; }
@SuppressWarnings("unchecked") public String getElementDefinitionId(final Element<?> element) { String targetId = null; if (element.getContent() instanceof View) { Object definition = ((View) element.getContent()).getDefinition(); targetId = getDefinitionId(definition); } else if (element.getContent() instanceof DefinitionSet) { targetId = ((DefinitionSet) element.getContent()).getDefinition(); } return targetId; }
@Test @SuppressWarnings("unchecked") public void testEvaluateInNoLimit() { when(evalUtils.countEdges(anyString(), any(List.class))).thenReturn(0); when(context.getDirection()).thenReturn(EdgeCardinalityContext.Direction.INCOMING); when(context.getOperation()).thenReturn(Optional.of(CardinalityContext.Operation.ADD)); RuleViolations violations = tested.evaluate(RULE_IN_NO_LIMIT, context); assertNotNull(violations); assertFalse(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); }
@Override public RuleViolations evaluate(final CanContain rule, final NodeContainmentContext context) { final Element<? extends Definition<?>> source = context.getParent(); final Node<? extends Definition<?>, ? extends Edge> target = context.getCandidate(); final Set<String> candidateLabels = evalUtils.getLabels(target); final Set<String> parentLabels = evalUtils.getLabels(source); final DefaultRuleViolations result = new DefaultRuleViolations(); result.addViolations( containmentHandler .evaluate(rule, RuleContextBuilder.DomainContexts.containment(parentLabels, candidateLabels)) ); return GraphEvaluationHandlerUtils.addViolationsSourceUUID(target.getUUID(), result); } }
@Override public boolean accepts(final CanContain rule, final NodeContainmentContext context) { final Set<String> parenteLabels = evalUtils.getLabels(context.getParent()); // As for acceptance checking, the delegated handler only needs the parent node id, no need // to calculate roles for the candidate node. return containmentHandler.accepts(rule, RuleContextBuilder.DomainContexts.containment(parenteLabels, Collections.emptySet())); }
@Inject public ElementCardinalityEvaluationHandler(final DefinitionManager definitionManager, final CardinalityEvaluationHandler cardinalityEvaluationHandler) { this.cardinalityEvaluationHandler = cardinalityEvaluationHandler; this.evalUtils = new GraphEvaluationHandlerUtils(definitionManager); }
@Test @SuppressWarnings("unchecked") public void testEvaluateInMinOneSucess() { when(evalUtils.countEdges(anyString(), any(List.class))).thenReturn(0); when(context.getDirection()).thenReturn(EdgeCardinalityContext.Direction.INCOMING); when(context.getOperation()).thenReturn(Optional.of(CardinalityContext.Operation.ADD)); RuleViolations violations = tested.evaluate(RULE_IN_MIN_1, context); assertNotNull(violations); assertFalse(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); }
@Override public RuleViolations evaluate(final CanDock rule, final NodeDockingContext context) { final Node<? extends Definition<?>, ? extends Edge> target = context.getCandidate(); final Set<String> parentLabels = evalUtils.getLabels(context.getParent()); final Set<String> candidateLabels = evalUtils.getLabels(target); final DefaultRuleViolations result = new DefaultRuleViolations(); result.addViolations( dockingHandler .evaluate(rule, RuleContextBuilder.DomainContexts.docking(parentLabels, candidateLabels)) ); return GraphEvaluationHandlerUtils.addViolationsSourceUUID(target.getUUID(), result); } }
@Override public boolean accepts(final CanDock rule, final NodeDockingContext context) { final Set<String> parentLabels = evalUtils.getLabels(context.getParent()); // As for acceptance checking, the delegated handler only needs the parent node id, no need // to calculate roles for the candidate node, so using an empty set. return dockingHandler.accepts(rule, RuleContextBuilder.DomainContexts.docking(parentLabels, Collections.emptySet())); }
private boolean acceptsContainment(final RuleExtension rule, final NodeContainmentContext context) { final Optional<Class<?>> parentType = getParentType(rule, context.getParent()); if (!hasParentType(rule) || !parentType.isPresent()) { return true; } final String expectedParentId = parentType.map(BindableAdapterUtils::getDefinitionId).orElse(null); final Element<? extends Definition<?>> parent = context.getParent(); final Node<? extends Definition<?>, ? extends Edge> candidate = context.getCandidate(); final String parentId = evalUtils.getElementDefinitionId(parent); return parentId.equals(expectedParentId) || hasOldParentType(candidate, expectedParentId); }
@Inject public NodeDockingEvaluationHandler(final DefinitionManager definitionManager, final DockingEvaluationHandler dockingHandler) { this.dockingHandler = dockingHandler; this.evalUtils = new GraphEvaluationHandlerUtils(definitionManager); }
@Test @SuppressWarnings("unchecked") public void testEvaluateInMaxOneFailed() { when(evalUtils.countEdges(anyString(), any(List.class))).thenReturn(1); when(context.getDirection()).thenReturn(EdgeCardinalityContext.Direction.INCOMING); when(context.getOperation()).thenReturn(Optional.of(CardinalityContext.Operation.ADD)); RuleViolations violations = tested.evaluate(RULE_IN_MAX_1, context); assertNotNull(violations); assertTrue(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); }