use of uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters.IncludeIncomingOutgoingType 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.operation.graph.SeededGraphFilters.IncludeIncomingOutgoingType 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);
}
}
}
}
}
}
use of uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters.IncludeIncomingOutgoingType in project Gaffer by gchq.
the class GetAdjacentIdsIT method shouldGetEntityIds.
@Test
public void shouldGetEntityIds() throws Exception {
final List<DirectedType> directedTypes = Lists.newArrayList(DirectedType.values());
directedTypes.add(null);
final List<IncludeIncomingOutgoingType> inOutTypes = Lists.newArrayList(IncludeIncomingOutgoingType.values());
inOutTypes.add(null);
for (final IncludeIncomingOutgoingType inOutType : inOutTypes) {
for (final DirectedType directedType : directedTypes) {
final List<String> expectedSeeds = new ArrayList<>();
if (DirectedType.DIRECTED != directedType) {
expectedSeeds.add(DEST_1);
expectedSeeds.add(SOURCE_2);
expectedSeeds.add(DEST_3);
expectedSeeds.add(SOURCE_3);
expectedSeeds.add("A1");
expectedSeeds.add("B1");
expectedSeeds.add("C1");
expectedSeeds.add("D1");
}
if (IncludeIncomingOutgoingType.INCOMING != inOutType) {
if (DirectedType.UNDIRECTED != directedType) {
expectedSeeds.add(DEST_DIR + "1");
expectedSeeds.add(DEST_DIR_3);
expectedSeeds.add("A1");
expectedSeeds.add("B1");
expectedSeeds.add("C1");
expectedSeeds.add("D1");
}
}
if (IncludeIncomingOutgoingType.OUTGOING != inOutType) {
if (DirectedType.UNDIRECTED != directedType) {
expectedSeeds.add(SOURCE_DIR_2);
expectedSeeds.add(SOURCE_DIR_3);
}
}
shouldGetEntityIds(expectedSeeds, inOutType, directedType);
}
}
}
use of uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters.IncludeIncomingOutgoingType in project Gaffer by gchq.
the class RowRangeFactory method getRowRange.
private List<RowRange> getRowRange(final Object vertex, final GraphFilters operation, final boolean includeEdgesParam) throws SerialisationException {
final IncludeIncomingOutgoingType inOutType = (operation instanceof SeededGraphFilters) ? ((SeededGraphFilters) operation).getIncludeIncomingOutGoing() : IncludeIncomingOutgoingType.OUTGOING;
final DirectedType directedType = operation.getDirectedType();
final boolean includeEdges;
final boolean includeEntities;
final boolean seedEqual = (operation instanceof SeedMatching) && SeedMatchingType.EQUAL.equals(((SeedMatching) operation).getSeedMatching());
if (seedEqual) {
includeEdges = false;
includeEntities = true;
} else {
includeEdges = includeEdgesParam;
includeEntities = operation.getView().hasEntities();
}
byte[] serialisedVertex = serialiser.serialiseVertex(vertex);
if (!includeEntities && !includeEdges) {
throw new IllegalArgumentException("Need to include either Entities or Edges or both when getting RowRange");
}
if (!includeEdges) {
// return only entities
return Collections.singletonList(new RowRange(getEntityRowId(serialisedVertex, false), true, getEntityRowId(serialisedVertex, true), true));
} else {
if (includeEntities) {
if (directedType == DirectedType.DIRECTED) {
// return onlyDirectedEdges and entities
if (inOutType == IncludeIncomingOutgoingType.INCOMING) {
return Arrays.asList(new RowRange(getEntityRowId(serialisedVertex, false), true, getEntityRowId(serialisedVertex, true), true), new RowRange(getDirectedEdgeRowIdDestFirst(serialisedVertex, false), true, getDirectedEdgeRowIdDestFirst(serialisedVertex, true), true));
} else if (inOutType == IncludeIncomingOutgoingType.OUTGOING) {
return Collections.singletonList(new RowRange(getEntityRowId(serialisedVertex, false), true, getDirectedEdgeRowIdSourceFirst(serialisedVertex, true), true));
} else {
return Collections.singletonList(new RowRange(getEntityRowId(serialisedVertex, false), true, getDirectedEdgeRowIdDestFirst(serialisedVertex, true), false));
}
} else if (directedType == DirectedType.UNDIRECTED) {
// Entity only range and undirected only range
return Arrays.asList(new RowRange(getUndirectedEdgeRowId(serialisedVertex, false), true, getUndirectedEdgeRowId(serialisedVertex, true), true), new RowRange(getEntityRowId(serialisedVertex, false), true, getEntityRowId(serialisedVertex, true), true));
} else {
// Return everything
if (inOutType == IncludeIncomingOutgoingType.INCOMING) {
return Arrays.asList(new RowRange(getEntityRowId(serialisedVertex, false), true, getEntityRowId(serialisedVertex, true), true), new RowRange(getDirectedEdgeRowIdDestFirst(serialisedVertex, false), true, getUndirectedEdgeRowId(serialisedVertex, true), true));
} else if (inOutType == IncludeIncomingOutgoingType.OUTGOING) {
return Arrays.asList(new RowRange(getEntityRowId(serialisedVertex, false), true, getDirectedEdgeRowIdSourceFirst(serialisedVertex, true), true), new RowRange(getUndirectedEdgeRowId(serialisedVertex, false), true, getUndirectedEdgeRowId(serialisedVertex, true), true));
} else {
return Collections.singletonList(new RowRange(getEntityRowId(serialisedVertex, false), true, getUndirectedEdgeRowId(serialisedVertex, true), true));
}
}
} else if (directedType == DirectedType.DIRECTED) {
if (inOutType == IncludeIncomingOutgoingType.INCOMING) {
return Collections.singletonList(new RowRange(getDirectedEdgeRowIdDestFirst(serialisedVertex, false), true, getDirectedEdgeRowIdDestFirst(serialisedVertex, true), true));
} else if (inOutType == IncludeIncomingOutgoingType.OUTGOING) {
return Collections.singletonList(new RowRange(getDirectedEdgeRowIdSourceFirst(serialisedVertex, false), true, getDirectedEdgeRowIdSourceFirst(serialisedVertex, true), true));
} else {
return Collections.singletonList(new RowRange(getDirectedEdgeRowIdSourceFirst(serialisedVertex, false), true, getDirectedEdgeRowIdDestFirst(serialisedVertex, true), true));
}
} else if (directedType == DirectedType.UNDIRECTED) {
return Collections.singletonList(new RowRange(getUndirectedEdgeRowId(serialisedVertex, false), true, getUndirectedEdgeRowId(serialisedVertex, true), true));
} else {
// return all edges
if (inOutType == IncludeIncomingOutgoingType.INCOMING) {
return Collections.singletonList(new RowRange(getDirectedEdgeRowIdDestFirst(serialisedVertex, false), true, getUndirectedEdgeRowId(serialisedVertex, true), true));
} else if (inOutType == IncludeIncomingOutgoingType.OUTGOING) {
return Arrays.asList(new RowRange(getDirectedEdgeRowIdSourceFirst(serialisedVertex, false), true, getDirectedEdgeRowIdSourceFirst(serialisedVertex, true), true), new RowRange(getUndirectedEdgeRowId(serialisedVertex, false), true, getUndirectedEdgeRowId(serialisedVertex, true), true));
} else {
final Pair<byte[], byte[]> keys = getAllEdgeOnlyRowIds(serialisedVertex);
return Collections.singletonList(new RowRange(keys.getFirst(), false, keys.getSecond(), false));
}
}
}
}
use of uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters.IncludeIncomingOutgoingType in project Gaffer by gchq.
the class ByteEntityRangeFactory method getRange.
@Override
protected List<Range> getRange(final Object vertex, final GraphFilters operation, final boolean includeEdgesParam) throws RangeFactoryException {
final IncludeIncomingOutgoingType inOutType = (operation instanceof SeededGraphFilters) ? ((SeededGraphFilters) operation).getIncludeIncomingOutGoing() : IncludeIncomingOutgoingType.OUTGOING;
final DirectedType directedType = operation.getDirectedType();
final boolean includeEdges;
final boolean includeEntities;
final boolean seedEqual = (operation instanceof SeedMatching) && SeedMatchingType.EQUAL.equals(((SeedMatching) operation).getSeedMatching());
if (seedEqual) {
includeEdges = false;
includeEntities = true;
} else {
includeEdges = includeEdgesParam;
includeEntities = operation.getView().hasEntities();
}
byte[] serialisedVertex;
try {
serialisedVertex = ByteArrayEscapeUtils.escape(((ToBytesSerialiser) schema.getVertexSerialiser()).serialise(vertex));
} catch (final SerialisationException e) {
throw new RangeFactoryException("Failed to serialise identifier", e);
}
if (!includeEntities && !includeEdges) {
throw new IllegalArgumentException("Need to include either Entities or Edges or both when getting Range");
}
if (!includeEdges) {
// return only entities
return Collections.singletonList(new Range(getEntityKey(serialisedVertex, false), true, getEntityKey(serialisedVertex, true), true));
} else {
if (includeEntities) {
if (directedType == DirectedType.DIRECTED) {
// return onlyDirectedEdges and entities
if (inOutType == IncludeIncomingOutgoingType.INCOMING) {
return Arrays.asList(new Range(getEntityKey(serialisedVertex, false), true, getEntityKey(serialisedVertex, true), true), new Range(getDirectedEdgeKeyDestinationFirst(serialisedVertex, false), true, getDirectedEdgeKeyDestinationFirst(serialisedVertex, true), true));
} else if (inOutType == IncludeIncomingOutgoingType.OUTGOING) {
return Collections.singletonList(new Range(getEntityKey(serialisedVertex, false), true, getDirectedEdgeKeySourceFirst(serialisedVertex, true), true));
} else {
return Collections.singletonList(new Range(getEntityKey(serialisedVertex, false), false, getDirectedEdgeKeyDestinationFirst(serialisedVertex, true), false));
}
} else if (directedType == DirectedType.UNDIRECTED) {
// Entity only range and undirected only range
return Arrays.asList(new Range(getUnDirectedEdgeKey(serialisedVertex, false), true, getUnDirectedEdgeKey(serialisedVertex, true), true), new Range(getEntityKey(serialisedVertex, false), true, getEntityKey(serialisedVertex, true), true));
} else {
// Return everything
if (inOutType == IncludeIncomingOutgoingType.INCOMING) {
return Arrays.asList(new Range(getEntityKey(serialisedVertex, false), true, getEntityKey(serialisedVertex, true), true), new Range(getDirectedEdgeKeyDestinationFirst(serialisedVertex, false), true, getUnDirectedEdgeKey(serialisedVertex, true), true));
} else if (inOutType == IncludeIncomingOutgoingType.OUTGOING) {
return Arrays.asList(new Range(getEntityKey(serialisedVertex, false), true, getDirectedEdgeKeySourceFirst(serialisedVertex, true), true), new Range(getUnDirectedEdgeKey(serialisedVertex, false), true, getUnDirectedEdgeKey(serialisedVertex, true), true));
} else {
return Collections.singletonList(new Range(getEntityKey(serialisedVertex, false), true, getUnDirectedEdgeKey(serialisedVertex, true), true));
}
}
} else if (directedType == DirectedType.DIRECTED) {
if (inOutType == IncludeIncomingOutgoingType.INCOMING) {
return Collections.singletonList(new Range(getDirectedEdgeKeyDestinationFirst(serialisedVertex, false), true, getDirectedEdgeKeyDestinationFirst(serialisedVertex, true), true));
} else if (inOutType == IncludeIncomingOutgoingType.OUTGOING) {
return Collections.singletonList(new Range(getDirectedEdgeKeySourceFirst(serialisedVertex, false), true, getDirectedEdgeKeySourceFirst(serialisedVertex, true), true));
} else {
return Collections.singletonList(new Range(getDirectedEdgeKeySourceFirst(serialisedVertex, false), true, getDirectedEdgeKeyDestinationFirst(serialisedVertex, true), true));
}
} else if (directedType == DirectedType.UNDIRECTED) {
return Collections.singletonList(new Range(getUnDirectedEdgeKey(serialisedVertex, false), true, getUnDirectedEdgeKey(serialisedVertex, true), true));
} else {
// return all edges
if (inOutType == IncludeIncomingOutgoingType.INCOMING) {
return Collections.singletonList(new Range(getDirectedEdgeKeyDestinationFirst(serialisedVertex, false), true, getUnDirectedEdgeKey(serialisedVertex, true), true));
} else if (inOutType == IncludeIncomingOutgoingType.OUTGOING) {
return Arrays.asList(new Range(getDirectedEdgeKeySourceFirst(serialisedVertex, false), true, getDirectedEdgeKeySourceFirst(serialisedVertex, true), true), new Range(getUnDirectedEdgeKey(serialisedVertex, false), true, getUnDirectedEdgeKey(serialisedVertex, true), true));
} else {
final Pair<Key, Key> keys = getAllEdgeOnlyKeys(serialisedVertex);
return Collections.singletonList(new Range(keys.getFirst(), false, keys.getSecond(), false));
}
}
}
}
Aggregations