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); }
final NodeContainmentContext context) { final String connectorId = rule.getId(); final Graph<?, ? extends Node> graph = context.getGraph(); final Element<? extends Definition<?>> parent = context.getParent(); final Node<? extends Definition<?>, ? extends Edge> candidate = context.getCandidate();
@Override public boolean accepts(final RuleExtension rule, final NodeContainmentContext context) { if (!GraphUtils.hasConnections(context.getCandidate())) { //this is not necessary to check rules in case there is no connections return false; } return acceptsContainment(rule, context); }
@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())); }
@Before @SuppressWarnings("unchecked") public void setup() throws Exception { super.setup(); this.graph = graphHandler.graph; when(graphHandler.definitionAdapter.getId(eq(connectorDef))).thenReturn(DefinitionId.build(DEF_EDGE_ID)); this.connector = graphHandler.newEdge(EDGE_UUID, Optional.of(connectorDef)); when(containmentContext.getGraph()).thenReturn(graph); when(ruleExtension.getId()).thenReturn(DEF_EDGE_ID); when(ruleExtension.getArguments()).thenReturn(new String[]{"violation1"}); tested = new ConnectorParentsMatchContainmentHandler(graphHandler.definitionManager, new TreeWalkTraverseProcessorImpl()); }
@Test @SuppressWarnings("unchecked") public void testEvaluateSuccess() { when(context.getCandidate()).thenReturn(candidate); final RuleViolations violations = tested.evaluate(RULE, context); assertNotNull(violations); assertFalse(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); }
@Before @SuppressWarnings("unchecked") public void setup() throws Exception { super.setup(); when(context.getParent()).thenReturn(element); tested = new NodeContainmentEvaluationHandler(definitionManager, CONTAINMENT_HANDLER); }
@Test @SuppressWarnings("unchecked") public void testAcceptSuccess() { when(context.getParent()).thenReturn(element); when(context.getCandidate()).thenReturn(candidate); final boolean accepts = tested.accepts(RULE, context); assertTrue(accepts); }
@Test @SuppressWarnings("unchecked") public void testEvaluateFailed() { when(context.getCandidate()).thenReturn(candidate); final RuleViolations violations = tested.evaluate(RULE_INVALID, context); assertNotNull(violations); assertTrue(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); } }
private boolean process(final Edge edge) { final Optional<String> eId = getId(definitionManager, edge); if (eId.isPresent() && connectorId.equals(eId.get())) { final Node sourceNode = edge.getSourceNode(); final Node targetNode = edge.getTargetNode(); boolean valid = true; if (null != sourceNode && null != targetNode) { //get parent from the connected node to the candidate final Element connectedParent = Objects.equals(candidate, sourceNode) ? GraphUtils.getParent(targetNode) : GraphUtils.getParent(sourceNode); final Optional<Class<?>> parentType = getParentType(rule, context.getParent(), connectedParent); valid = new FilteredParentsTypeMatcher(definitionManager, parent, candidate, parentType) .test(sourceNode, targetNode); } if (!valid) { addViolation(edge.getUUID(), rule, result); } } return true; } });
@Test @SuppressWarnings("unchecked") public void testAcceptFailed() { when(context.getParent()).thenReturn(parent); when(context.getCandidate()).thenReturn(candidate); final boolean accepts = tested.accepts(RULE, context); assertFalse(accepts); }
@Test @SuppressWarnings("unchecked") public void testNotAccepts() { when(ruleExtension.getTypeArguments()).thenReturn(new Class[]{DefinitionC.class}); when(containmentContext.getCandidate()).thenReturn(nodeA); when(containmentContext.getParent()).thenReturn(parentNode); assertFalse(tested.accepts(ruleExtension, containmentContext)); }
@Test @SuppressWarnings("unchecked") public void testAccepts() { when(ruleExtension.getTypeArguments()).thenReturn(new Class[]{ParentDefinition.class}); when(containmentContext.getCandidate()).thenReturn(nodeA); when(containmentContext.getParent()).thenReturn(parentNode); nodeA.getInEdges().add(edge); when(edge.getContent()).thenReturn(viewConnector); assertTrue(tested.accepts(ruleExtension, containmentContext)); }
@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); } }
@Test @SuppressWarnings("unchecked") public void testEvaluateParentSuccess() { when(ruleExtension.getTypeArguments()).thenReturn(new Class[]{ParentDefinition.class}); when(containmentContext.getCandidate()).thenReturn(nodeA); when(containmentContext.getParent()).thenReturn(parentNode); graphHandler.addEdge(connector, nodeA) .connectTo(connector, nodeB); final RuleViolations violations = tested.evaluate(ruleExtension, containmentContext); assertNotNull(violations); assertViolations(violations, true); }
@Test @SuppressWarnings("unchecked") public void testEvaluateParentFailed() { when(ruleExtension.getTypeArguments()).thenReturn(new Class[]{ParentDefinition.class}); when(containmentContext.getCandidate()).thenReturn(nodeA); when(containmentContext.getParent()).thenReturn(parentNode); graphHandler.addEdge(connector, nodeA) .connectTo(connector, nodeC); final RuleViolations violations = tested.evaluate(ruleExtension, containmentContext); assertNotNull(violations); assertViolations(violations, false); } }
public static void verifyContainment(final NodeContainmentContext containmentContext, final Element<? extends Definition<?>> parent, final Node<? extends Definition<?>, ? extends Edge> candidate) { assertNotNull(containmentContext); final Element<? extends Definition<?>> source = containmentContext.getParent(); final Node<? extends Definition<?>, ? extends Edge> target = containmentContext.getCandidate(); assertNotNull(source); assertEquals(parent, source); assertNotNull(target); assertEquals(candidate, target); }
when(containmentContext.getCandidate()).thenReturn(nodeA); when(containmentContext.getParent()).thenReturn(parentNode); when(containmentContext.getParent()).thenReturn(grandParentNode); when(containmentContext.getParent()).thenReturn(parentNode); when(ruleExtension.getTypeArguments()).thenReturn(new Class[]{RootDefinition.class});