use of uk.gov.gchq.gaffer.data.element.id.DirectedType 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;
}
use of uk.gov.gchq.gaffer.data.element.id.DirectedType in project Gaffer by gchq.
the class AbstractLoaderIT method getAllElements.
private void getAllElements(final List<Element> expectedElements) throws Exception {
for (final boolean includeEntities : Arrays.asList(true, false)) {
for (final boolean includeEdges : Arrays.asList(true, false)) {
if (!includeEntities && !includeEdges) {
// Cannot query for nothing!
continue;
}
for (final DirectedType directedType : DirectedType.values()) {
try {
final View.Builder viewBuilder = new View.Builder();
if (includeEntities) {
viewBuilder.entity(TestGroups.ENTITY);
}
if (includeEdges) {
viewBuilder.edge(TestGroups.EDGE);
}
getAllElements(expectedElements, directedType, viewBuilder.build());
} catch (final AssertionError e) {
throw new AssertionError("GetAllElements failed with parameters: includeEntities=" + includeEntities + ", includeEdges=" + includeEdges + ", directedType=" + directedType.name(), e);
}
}
}
}
}
use of uk.gov.gchq.gaffer.data.element.id.DirectedType in project Gaffer by gchq.
the class QueryGenerator method seedToPredicate.
private FilterPredicate seedToPredicate(final ParquetElementSeed seed, final SeededGraphFilters.IncludeIncomingOutgoingType includeIncomingOutgoingType, final SeedMatching.SeedMatchingType seedMatchingType, final String group, final boolean reversed) {
final boolean isEntityGroup = schemaUtils.getEntityGroups().contains(group);
FilterPredicate filter = null;
final ElementId elementId = seed.getElementId();
// Is it an entity group?
if (isEntityGroup) {
// EntityId case
if (elementId instanceof EntityId) {
filter = getIsEqualFilter(ParquetStore.VERTEX, ((ParquetEntitySeed) seed).getSeed(), group);
} else {
// EdgeId case
// Does the seed type need to match the group type?
final ParquetEdgeSeed edgeSeed = (ParquetEdgeSeed) seed;
if (seedMatchingType != SeedMatching.SeedMatchingType.EQUAL) {
// Vertex = source of edge seed or Vertex = destination of edge seed
// look in partition 0 with filter src = A and partition 1 with filter src = B
filter = getIsEqualFilter(ParquetStore.VERTEX, edgeSeed.getSource(), group);
if (null != ((ParquetEdgeSeed) seed).getDestination()) {
filter = FilterPredicateUtils.or(filter, getIsEqualFilter(ParquetStore.VERTEX, edgeSeed.getDestination(), group));
}
}
}
} else {
// EntityId case
if (elementId instanceof EntityId) {
// If seedMatchingType is EQUAL then we can't find anything in an edge group
if (seedMatchingType != SeedMatching.SeedMatchingType.EQUAL) {
if (includeIncomingOutgoingType == SeededGraphFilters.IncludeIncomingOutgoingType.INCOMING) {
if (reversed) {
// Dst is seed
filter = getIsEqualFilter(ParquetStore.DESTINATION, ((ParquetEntitySeed) seed).getSeed(), group);
} else {
// Src is seed and edge is undirected
filter = getIsEqualFilter(ParquetStore.SOURCE, ((ParquetEntitySeed) seed).getSeed(), group);
filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { false }, group));
}
} else if (includeIncomingOutgoingType == SeededGraphFilters.IncludeIncomingOutgoingType.OUTGOING) {
if (reversed) {
// Dst is seed and edge is undirected
filter = getIsEqualFilter(ParquetStore.DESTINATION, ((ParquetEntitySeed) seed).getSeed(), group);
filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { false }, group));
} else {
// Src is seed
filter = getIsEqualFilter(ParquetStore.SOURCE, ((ParquetEntitySeed) seed).getSeed(), group);
}
} else {
if (reversed) {
// Dst is seed
filter = getIsEqualFilter(ParquetStore.DESTINATION, ((ParquetEntitySeed) seed).getSeed(), group);
} else {
// Src is seed
filter = getIsEqualFilter(ParquetStore.SOURCE, ((ParquetEntitySeed) seed).getSeed(), group);
}
}
}
} else {
// EdgeId case
final ParquetEdgeSeed edgeSeed = (ParquetEdgeSeed) seed;
if (!reversed) {
// Src is source of edge seed and destination is destination of edge seed
filter = getIsEqualFilter(ParquetStore.SOURCE, edgeSeed.getSource(), group);
// WRONG seed is already serialised source and dest - now fixed?
filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DESTINATION, edgeSeed.getDestination(), group));
final DirectedType directedType = edgeSeed.getDirectedType();
if (directedType == DirectedType.DIRECTED) {
filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { true }, group));
} else if (directedType == DirectedType.UNDIRECTED) {
filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { false }, group));
}
} else {
// TODO Optimise this - there are times this is unnecessary
filter = getIsEqualFilter(ParquetStore.DESTINATION, edgeSeed.getSource(), group);
filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.SOURCE, edgeSeed.getDestination(), group));
final DirectedType directedType = edgeSeed.getDirectedType();
if (directedType == DirectedType.DIRECTED) {
filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { true }, group));
} else if (directedType == DirectedType.UNDIRECTED) {
filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { false }, group));
}
}
}
}
LOGGER.debug("Returning {} from seedToPredicate", filter);
return filter;
}
use of uk.gov.gchq.gaffer.data.element.id.DirectedType in project Gaffer by gchq.
the class GetWalksIT method shouldGetPathsWithEntities.
@Test
public void shouldGetPathsWithEntities() throws Exception {
// Given
final GetElements getEntities = new GetElements.Builder().directedType(DirectedType.DIRECTED).view(new View.Builder().entity(TestGroups.ENTITY).build()).build();
final GetElements getElements = new GetElements.Builder().directedType(DirectedType.DIRECTED).inOutType(SeededGraphFilters.IncludeIncomingOutgoingType.OUTGOING).view(new View.Builder().entity(TestGroups.ENTITY).edge(TestGroups.EDGE).build()).build();
final GetWalks op = new GetWalks.Builder().input(seedA).operations(getElements, getElements, getEntities).build();
// When
final List<Walk> results = Lists.newArrayList(graph.execute(op, getUser()));
// Then
assertThat(getPaths(results)).isEqualTo("AED,ABC");
results.forEach(r -> r.getEntities().forEach(l -> {
assertThat(l).isNotEmpty();
}));
}
use of uk.gov.gchq.gaffer.data.element.id.DirectedType in project Gaffer by gchq.
the class GetElementsIT method shouldGetElements.
@Test
public void shouldGetElements() {
final List<DirectedType> directedTypes = Lists.newArrayList(DirectedType.values());
directedTypes.add(null);
final List<IncludeIncomingOutgoingType> inOutTypes = Lists.newArrayList(IncludeIncomingOutgoingType.values());
inOutTypes.add(null);
for (final boolean includeEntities : Arrays.asList(true, false)) {
for (final boolean includeEdges : Arrays.asList(true, false)) {
if (!includeEntities && !includeEdges) {
// Cannot query for nothing!
continue;
}
for (final DirectedType directedType : directedTypes) {
for (final IncludeIncomingOutgoingType inOutType : inOutTypes) {
try {
shouldGetElementsBySeed(includeEntities, includeEdges, directedType, inOutType);
} catch (final Throwable e) {
throw new AssertionError("GetElementsBySeed failed with parameters: \nincludeEntities=" + includeEntities + " \nincludeEdges=" + includeEdges + " \ndirectedType=" + directedType + " \ninOutType=" + inOutType, e);
}
try {
shouldGetRelatedElements(includeEntities, includeEdges, directedType, inOutType);
} catch (final Throwable e) {
throw new AssertionError("GetRelatedElements failed with parameters: \nincludeEntities=" + includeEntities + " \nincludeEdges=" + includeEdges + " \ndirectedType=" + directedType + " \ninOutType=" + inOutType, e);
}
}
}
}
}
}
Aggregations