use of uk.gov.gchq.gaffer.data.element.id.EntityId 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.EntityId in project Gaffer by gchq.
the class EntitySeedTest method shouldBeRelatedToEntityId.
@Test
public void shouldBeRelatedToEntityId() {
// Given
final EntityId seed1 = new EntitySeed("vertex");
final EntityId seed2 = new EntitySeed("vertex");
// Then
assertTrue(seed1.isRelated(seed2).isMatch());
}
use of uk.gov.gchq.gaffer.data.element.id.EntityId in project Gaffer by gchq.
the class EntitySeedTest method shouldBeEqualWhenVerticesEqual.
@Test
public void shouldBeEqualWhenVerticesEqual() {
// Given
final String vertex = "vertex";
final EntityId seed1 = new EntitySeed(vertex);
final EntityId seed2 = new EntitySeed(vertex);
// Then
assertEquals(seed1, seed2);
assertEquals(seed1.hashCode(), seed2.hashCode());
}
use of uk.gov.gchq.gaffer.data.element.id.EntityId in project Gaffer by gchq.
the class EntitySeedTest method shouldBeRelatedToEdgeIdWhenSourceEqualsVertex.
@Test
public void shouldBeRelatedToEdgeIdWhenSourceEqualsVertex() {
// Given
final String source = "source";
final String destination = "destination";
final EntityId seed = new EntitySeed(source);
final EdgeId relatedSeed = mock(EdgeId.class);
given(relatedSeed.getSource()).willReturn(source);
given(relatedSeed.getDestination()).willReturn(destination);
// Then
assertTrue(seed.isRelated((ElementId) relatedSeed).isMatch());
}
use of uk.gov.gchq.gaffer.data.element.id.EntityId in project Gaffer by gchq.
the class EntitySeedTest method shouldBeRelatedToEdgeIdWhenDestinationEqualsVertex.
@Test
public void shouldBeRelatedToEdgeIdWhenDestinationEqualsVertex() {
// Given
final String source = "source";
final String destination = "destination";
final EntityId seed = new EntitySeed(destination);
final EdgeId relatedSeed = mock(EdgeId.class);
given(relatedSeed.getSource()).willReturn(source);
given(relatedSeed.getDestination()).willReturn(destination);
// When
final boolean isRelated = seed.isRelated((ElementId) relatedSeed).isMatch();
// Then
assertTrue(isRelated);
}
Aggregations