private DomainLookupContext newContext() { return new DomainLookupContext(getDefinitionManager(), definitionsRegistry, ruleManager, cache); }
public Set<String> lookupTargetConnectors(final Node<? extends Definition<Object>, ? extends Edge> sourceNode) { final DomainLookupContext context = newContext(); return new LookupTargetConnectors(sourceNode) .execute(context); }
public Set<String> lookupTargetNodes(final Graph<?, ? extends Node> graph, final Node<? extends Definition<Object>, ? extends Edge> sourceNode, final String edgeId, final Predicate<String> definitionIdsAllowedFilter) { final DomainLookupContext context = newContext(); final Set<String> targetRoles = new LookupTargetRoles(sourceNode, edgeId) .execute(context); final Set<String> allowedTargetDefinitions = new LookupAllowedDefinitionsByLabels(graph, targetRoles, definitionIdsAllowedFilter) .execute(context); return new FilterConnectionTargetDefinitions(edgeId, allowedTargetDefinitions).execute(context); }
@Test @SuppressWarnings("unchecked") public void testLookupDefinitionsByLabelsWithFiltering() { Set<String> expected = new HashSet<String>(1) {{ add(DEF_ID1); }}; when(cache.getDefinitions(eq(ROLE2))).thenReturn(expected); LookupDefinitionsByLabels function = new LookupDefinitionsByLabels(new HashSet<String>(1) {{ add(ROLE2); }}, DEF_ID2::equals); assertFalse(function.execute(context).contains(DEF_ID1)); }
@Test @SuppressWarnings("unchecked") public void testLookupDefinitionsByLabels() { Set<String> expected = new HashSet<String>(1) {{ add(DEF_ID1); }}; when(cache.getDefinitions(eq(ROLE2))).thenReturn(expected); LookupDefinitionsByLabels function = new LookupDefinitionsByLabels(new HashSet<String>(1) {{ add(ROLE2); }}, id -> true); assertTrue(function.execute(context).contains(DEF_ID1)); }
@Test @SuppressWarnings("unchecked") public void testLookupAllowedDefinitionsByLabelsWithFiltering() { when(cache.getDefinitions(eq("label1"))).thenReturn(Collections.singleton(TestingGraphInstanceBuilder.DEF1_ID)); when(definitionsCache.getLabels(eq(TestingGraphInstanceBuilder.DEF1_ID))) .thenReturn(TestingGraphInstanceBuilder.DEF1_LABELS); LookupAllowedDefinitionsByLabels function = new LookupAllowedDefinitionsByLabels(graph1Instance.graph, new HashSet<String>(1) {{ add("label1"); }}, DEF_ID2::equals); Set<String> result = function.execute(context); assertTrue(result.isEmpty()); }
@Override public Set<String> execute(final DomainLookupContext context) { final Set<String> labels = context.getDefinitionsRegistry().getLabels(sourceNodeDefId); return context.getCache() .getConnectionRules() .stream() .filter(rule -> rule.getRole().equals(edgeDefId)) .flatMap(rule -> rule.getPermittedConnections().stream()) .filter(pc -> labels.contains(pc.getStartRole())) .map(CanConnect.PermittedConnection::getEndRole) .collect(Collectors.toSet()); } }
public Set<String> lookupTargetNodes(final Graph<?, ? extends Node> graph, final Node<? extends Definition<Object>, ? extends Edge> sourceNode, final String edgeId) { return lookupTargetNodes(graph, sourceNode, edgeId, def -> true); }
@Test public void testIsSourceConnectionAllowed() { assertTrue(isSourceConnectionAllowed(permittedConnections, new HashSet<String>(1) {{ add(ROLE1); }})); assertTrue(isSourceConnectionAllowed(permittedConnections, new HashSet<String>(1) {{ add(ROLE2); }})); assertFalse(isSourceConnectionAllowed(permittedConnections, new HashSet<String>(1) {{ add(ROLE3); }})); }
@Override public Set<String> execute(final DomainLookupContext context) { final DomainLookupsCache cache = context.getCache(); return labels.stream() .flatMap(label -> cache.getDefinitions(label).stream()) .filter(definitionIdsAllowedFilter::test) .collect(Collectors.toSet()); } }
@Test @SuppressWarnings("unchecked") public void testLookupConnectionTargetRoles() { LookupConnectionTargetRoles function = new LookupConnectionTargetRoles(DEF_ID2, DEF_ID1); Set<String> result = function.execute(context); assertEquals(1, result.size()); assertTrue(result.contains(ROLE2)); }
@PreDestroy public void destroy() { if (null != cache) { cache.clear(); cache = null; } }
public CommonDomainLookups get(final String definitionSetId) { CommonDomainLookups lookup = domainLookups.get(definitionSetId); if (null == lookup) { lookup = domainLookupInstances.get(); lookup.setDomain(definitionSetId); domainLookups.put(definitionSetId, lookup); } return lookup; }
@Inject public CommonDomainLookups(final DefinitionUtils definitionUtils, final DefinitionsCacheRegistry definitionsRegistry, final RuleManager ruleManager) { this(definitionUtils, definitionsRegistry, ruleManager, defSetId -> new DomainLookupsCache(definitionUtils.getDefinitionManager(), definitionsRegistry, defSetId)); }
private static <T> int countOutgoingEdges(final DomainLookupContext context, final Node<? extends Definition<T>, ? extends Edge> sourceNode, final String edgeId) { final List<? extends Edge> edges = sourceNode.getOutEdges(); return GraphUtils.countEdges(context.getDefinitionManager(), edgeId, edges); }
@Test public void testCacheTheConnectionRules() { List<CanConnect> rules = tested.getConnectionRules(); assertEquals(1, rules.size()); assertEquals(canConnect1To2, rules.get(0)); }
private void registerDefinition(final DefinitionsCacheRegistry definitionsRegistry, final String id) { definitionsRegistry.getLabels(id).forEach(label -> registerDefinition(label, id)); }
@Test public void testGetDefinitionSetId() { assertEquals(DEF_SET_ID, tested.getDefinitionSetId()); }
@Test @SuppressWarnings("unchecked") public void testLookupTargetNodesWithPredicate() { Predicate<String> filter = TestingGraphInstanceBuilder.DEF1_ID::equals; Set<String> result = tested.lookupTargetNodes(graph.graph, graph.startNode, TestingGraphInstanceBuilder.EDGE1_ID, filter); assertTrue(result.isEmpty()); } }