Search in sources :

Example 1 with SeedMatchingType

use of uk.gov.gchq.gaffer.operation.SeedMatching.SeedMatchingType in project Gaffer by gchq.

the class GetElementsUtil method getRelevantElements.

public static Set<Element> getRelevantElements(final MapImpl mapImpl, final ElementId elementId, final View view, final DirectedType directedType, final IncludeIncomingOutgoingType inOutType, final SeedMatchingType seedMatchingType) {
    final Set<Element> relevantElements;
    final Set<String> groups = view.getGroups();
    Predicate<Element> isFiltered = e -> !groups.contains(e.getGroup());
    if (elementId instanceof EntityId) {
        final Collection<Element> elements = mapImpl.lookup(new EntitySeed(((EntityId) elementId).getVertex()));
        if (elements.isEmpty()) {
            return Collections.emptySet();
        }
        relevantElements = new HashSet<>(elements);
        // Apply inOutType options - if option is EITHER then nothing to do
        if (inOutType == IncludeIncomingOutgoingType.INCOMING) {
            isFiltered = isFiltered.or(e -> e instanceof Edge && ((Edge) e).isDirected() && (EdgeId.MatchedVertex.SOURCE == ((Edge) e).getMatchedVertex()));
        } else if (inOutType == IncludeIncomingOutgoingType.OUTGOING) {
            isFiltered = isFiltered.or(e -> e instanceof Edge && ((Edge) e).isDirected() && (EdgeId.MatchedVertex.DESTINATION == ((Edge) e).getMatchedVertex()));
        }
        // Apply seedMatching option - if option is RELATED then nothing to do
        if (seedMatchingType == SeedMatchingType.EQUAL) {
            isFiltered = isFiltered.or(e -> e instanceof Edge);
        }
    } else {
        relevantElements = new HashSet<>();
        final EdgeId edgeId = (EdgeId) elementId;
        if (DirectedType.isEither(edgeId.getDirectedType())) {
            relevantElements.addAll(mapImpl.lookup(new EdgeSeed(edgeId.getSource(), edgeId.getDestination(), false)));
            relevantElements.addAll(mapImpl.lookup(new EdgeSeed(edgeId.getSource(), edgeId.getDestination(), true)));
        } else {
            relevantElements.addAll(mapImpl.lookup(new EdgeSeed(edgeId.getSource(), edgeId.getDestination(), edgeId.getDirectedType())));
        }
        mapImpl.lookup(new EntitySeed(edgeId.getSource())).stream().filter(e -> e instanceof Entity).forEach(relevantElements::add);
        mapImpl.lookup(new EntitySeed(edgeId.getDestination())).stream().filter(e -> e instanceof Entity).forEach(relevantElements::add);
        // If option is RELATED then nothing to do
        if (seedMatchingType == SeedMatchingType.EQUAL) {
            isFiltered = isFiltered.or(e -> e instanceof Entity);
        }
    }
    // Apply directedType flag
    if (directedType == DirectedType.DIRECTED) {
        isFiltered = isFiltered.or(e -> e instanceof Edge && !((Edge) e).isDirected());
    } else if (directedType == DirectedType.UNDIRECTED) {
        isFiltered = isFiltered.or(e -> e instanceof Edge && ((Edge) e).isDirected());
    }
    relevantElements.removeIf(isFiltered);
    return relevantElements;
}
Also used : User(uk.gov.gchq.gaffer.user.User) LoggerFactory(org.slf4j.LoggerFactory) EdgeId(uk.gov.gchq.gaffer.data.element.id.EdgeId) Element(uk.gov.gchq.gaffer.data.element.Element) Authorisations(uk.gov.gchq.gaffer.commonutil.elementvisibilityutil.Authorisations) HashSet(java.util.HashSet) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) ElementTransformer(uk.gov.gchq.gaffer.data.element.function.ElementTransformer) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) SeedMatchingType(uk.gov.gchq.gaffer.operation.SeedMatching.SeedMatchingType) VisibilityParseException(uk.gov.gchq.gaffer.commonutil.elementvisibilityutil.exception.VisibilityParseException) StreamSupport(java.util.stream.StreamSupport) Edge(uk.gov.gchq.gaffer.data.element.Edge) IncludeIncomingOutgoingType(uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters.IncludeIncomingOutgoingType) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) Logger(org.slf4j.Logger) ElementVisibility(uk.gov.gchq.gaffer.commonutil.elementvisibilityutil.ElementVisibility) DirectedType(uk.gov.gchq.gaffer.data.element.id.DirectedType) AggregatorUtil(uk.gov.gchq.gaffer.store.util.AggregatorUtil) VisibilityEvaluator(uk.gov.gchq.gaffer.commonutil.elementvisibilityutil.VisibilityEvaluator) Predicate(java.util.function.Predicate) Collection(java.util.Collection) EdgeSeed(uk.gov.gchq.gaffer.operation.data.EdgeSeed) Set(java.util.Set) Entity(uk.gov.gchq.gaffer.data.element.Entity) Collectors(java.util.stream.Collectors) Stream(java.util.stream.Stream) Schema(uk.gov.gchq.gaffer.store.schema.Schema) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) ElementId(uk.gov.gchq.gaffer.data.element.id.ElementId) Collections(java.util.Collections) Entity(uk.gov.gchq.gaffer.data.element.Entity) Element(uk.gov.gchq.gaffer.data.element.Element) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) EdgeId(uk.gov.gchq.gaffer.data.element.id.EdgeId) EdgeSeed(uk.gov.gchq.gaffer.operation.data.EdgeSeed) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Edge(uk.gov.gchq.gaffer.data.element.Edge)

Aggregations

Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 StreamSupport (java.util.stream.StreamSupport)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Authorisations (uk.gov.gchq.gaffer.commonutil.elementvisibilityutil.Authorisations)1 ElementVisibility (uk.gov.gchq.gaffer.commonutil.elementvisibilityutil.ElementVisibility)1 VisibilityEvaluator (uk.gov.gchq.gaffer.commonutil.elementvisibilityutil.VisibilityEvaluator)1 VisibilityParseException (uk.gov.gchq.gaffer.commonutil.elementvisibilityutil.exception.VisibilityParseException)1 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)1 Edge (uk.gov.gchq.gaffer.data.element.Edge)1 Element (uk.gov.gchq.gaffer.data.element.Element)1 Entity (uk.gov.gchq.gaffer.data.element.Entity)1 ElementTransformer (uk.gov.gchq.gaffer.data.element.function.ElementTransformer)1 DirectedType (uk.gov.gchq.gaffer.data.element.id.DirectedType)1