Search in sources :

Example 1 with Walk

use of uk.gov.gchq.gaffer.data.graph.Walk in project Gaffer by gchq.

the class GetWalksHandler method doOperation.

@Override
public Iterable<Walk> doOperation(final GetWalks getWalks, final Context context, final Store store) throws OperationException {
    // Check input
    if (null == getWalks.getInput()) {
        return null;
    }
    // Check there are some operations
    if (null == getWalks.getOperations()) {
        return new EmptyClosableIterable<>();
    }
    final Integer resultLimit = getWalks.getResultsLimit();
    final int hops = getWalks.getNumberOfGetEdgeOperations();
    final LimitedCloseableIterable limitedInputItr = new LimitedCloseableIterable<>(getWalks.getInput(), 0, resultLimit, false);
    final List<EntityId> originalInput = Lists.newArrayList(limitedInputItr);
    // Check hops and maxHops (if set)
    if (hops == 0) {
        return new EmptyClosableIterable<>();
    } else if (maxHops != null && hops > maxHops) {
        throw new OperationException("GetWalks operation contains " + hops + " hops. The maximum number of hops is: " + maxHops);
    }
    final AdjacencyMaps adjacencyMaps = prune && !getWalks.isIncludePartial() ? new PrunedAdjacencyMaps() : new SimpleAdjacencyMaps();
    final EntityMaps entityMaps = new SimpleEntityMaps();
    List<?> seeds = originalInput;
    // Execute the operations
    for (final OperationChain<Iterable<Element>> operation : getWalks.getOperations()) {
        if (isWhileOperation(operation)) {
            seeds = executeWhileOperation(operation, seeds, resultLimit, context, store, hops, adjacencyMaps, entityMaps);
        } else {
            seeds = executeOperation(operation, seeds, resultLimit, context, store, hops, adjacencyMaps, entityMaps);
        }
    }
    // requested by the user.
    if (entityMaps.size() == adjacencyMaps.size()) {
        entityMaps.add(new EntityMap());
    }
    final GraphWindow graphWindow = new GraphWindow(adjacencyMaps, entityMaps);
    // Track/recombine the edge objects and convert to return type
    final Stream<Walk> walks = Streams.toStream(originalInput).flatMap(seed -> walk(seed.getVertex(), null, graphWindow, new LinkedList<>(), new LinkedList<>(), hops, getWalks.isIncludePartial()).stream());
    return applyConditionalFiltering(walks, getWalks, context, store);
}
Also used : Walk(uk.gov.gchq.gaffer.data.graph.Walk) LimitedCloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.LimitedCloseableIterable) LimitedCloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.LimitedCloseableIterable) EmptyClosableIterable(uk.gov.gchq.gaffer.commonutil.iterable.EmptyClosableIterable) EmptyClosableIterable(uk.gov.gchq.gaffer.commonutil.iterable.EmptyClosableIterable) PrunedAdjacencyMaps(uk.gov.gchq.gaffer.data.graph.adjacency.PrunedAdjacencyMaps) SimpleEntityMaps(uk.gov.gchq.gaffer.data.graph.entity.SimpleEntityMaps) UnwrapEntityId(uk.gov.gchq.gaffer.data.element.function.UnwrapEntityId) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) GraphWindow(uk.gov.gchq.gaffer.data.graph.GraphWindow) SimpleEntityMaps(uk.gov.gchq.gaffer.data.graph.entity.SimpleEntityMaps) EntityMaps(uk.gov.gchq.gaffer.data.graph.entity.EntityMaps) EntityMap(uk.gov.gchq.gaffer.data.graph.entity.EntityMap) PrunedAdjacencyMaps(uk.gov.gchq.gaffer.data.graph.adjacency.PrunedAdjacencyMaps) AdjacencyMaps(uk.gov.gchq.gaffer.data.graph.adjacency.AdjacencyMaps) SimpleAdjacencyMaps(uk.gov.gchq.gaffer.data.graph.adjacency.SimpleAdjacencyMaps) SimpleAdjacencyMaps(uk.gov.gchq.gaffer.data.graph.adjacency.SimpleAdjacencyMaps) OperationException(uk.gov.gchq.gaffer.operation.OperationException)

Example 2 with Walk

use of uk.gov.gchq.gaffer.data.graph.Walk in project Gaffer by gchq.

the class GetWalksHandlerTest method shouldHandleNullInput.

@Test
public void shouldHandleNullInput() throws Exception {
    // Given
    final GetElements getElements = new GetElements.Builder().view(new View.Builder().edge(TestGroups.EDGE).build()).build();
    final GetWalks operation = new GetWalks.Builder().operations(getElements).build();
    final GetWalksHandler handler = new GetWalksHandler();
    // When
    final Iterable<Walk> result = handler.doOperation(operation, null, null);
    // Then
    assertThat(result).isNull();
}
Also used : Walk(uk.gov.gchq.gaffer.data.graph.Walk) GetWalks(uk.gov.gchq.gaffer.operation.impl.GetWalks) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Test(org.junit.jupiter.api.Test)

Example 3 with Walk

use of uk.gov.gchq.gaffer.data.graph.Walk in project Gaffer by gchq.

the class ExtractWalkEntitiesTest method shouldReturnEntitiesFromWalkObject.

@Test
public void shouldReturnEntitiesFromWalkObject() {
    // Given
    final Function<Walk, Iterable<Set<Entity>>> function = new ExtractWalkEntities();
    final Walk walk = new Walk.Builder().entity(ENTITY_A).edge(EDGE_AB).entity(ENTITY_B).edge(EDGE_BC).entity(ENTITY_C).edge(EDGE_CA).entity(ENTITY_A).build();
    // When
    final Iterable<Set<Entity>> results = function.apply(walk);
    // Then
    assertThat(results).containsOnly(Sets.newHashSet(ENTITY_A), Sets.newHashSet(ENTITY_B), Sets.newHashSet(ENTITY_C), Sets.newHashSet(ENTITY_A));
}
Also used : Walk(uk.gov.gchq.gaffer.data.graph.Walk) Entity(uk.gov.gchq.gaffer.data.element.Entity) Set(java.util.Set) Test(org.junit.jupiter.api.Test)

Example 4 with Walk

use of uk.gov.gchq.gaffer.data.graph.Walk 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();
    }));
}
Also used : Walk(uk.gov.gchq.gaffer.data.graph.Walk) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ExtractProperty(uk.gov.gchq.gaffer.data.element.function.ExtractProperty) GraphConfig(uk.gov.gchq.gaffer.graph.GraphConfig) SchemaEntityDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) Edge(uk.gov.gchq.gaffer.data.element.Edge) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) TestGroups(uk.gov.gchq.gaffer.commonutil.TestGroups) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition) StringConcat(uk.gov.gchq.koryphe.impl.binaryoperator.StringConcat) Exists(uk.gov.gchq.koryphe.impl.predicate.Exists) Predicate(java.util.function.Predicate) TestPropertyNames(uk.gov.gchq.gaffer.commonutil.TestPropertyNames) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) ForEach(uk.gov.gchq.gaffer.operation.impl.ForEach) AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) OperationException(uk.gov.gchq.gaffer.operation.OperationException) StoreTrait(uk.gov.gchq.gaffer.store.StoreTrait) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) AgeOff(uk.gov.gchq.koryphe.impl.predicate.AgeOff) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) SchemaEdgeDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition) Limit(uk.gov.gchq.gaffer.operation.impl.Limit) HashMap(java.util.HashMap) HashSet(java.util.HashSet) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Lists(com.google.common.collect.Lists) While(uk.gov.gchq.gaffer.operation.impl.While) Map(uk.gov.gchq.gaffer.operation.impl.Map) Max(uk.gov.gchq.koryphe.impl.binaryoperator.Max) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) Sum(uk.gov.gchq.koryphe.impl.binaryoperator.Sum) IsLessThan(uk.gov.gchq.koryphe.impl.predicate.IsLessThan) DirectedType(uk.gov.gchq.gaffer.data.element.id.DirectedType) Walk(uk.gov.gchq.gaffer.data.graph.Walk) AddOperationsToChain(uk.gov.gchq.gaffer.graph.hook.AddOperationsToChain) GetWalks(uk.gov.gchq.gaffer.operation.impl.GetWalks) CollectionContains(uk.gov.gchq.koryphe.impl.predicate.CollectionContains) Test(org.junit.Test) Entity(uk.gov.gchq.gaffer.data.element.Entity) KorypheFunction(uk.gov.gchq.koryphe.function.KorypheFunction) IterableConcat(uk.gov.gchq.koryphe.impl.function.IterableConcat) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) ExtractWalkEntities(uk.gov.gchq.gaffer.data.graph.function.walk.ExtractWalkEntities) AbstractStoreIT(uk.gov.gchq.gaffer.integration.AbstractStoreIT) TraitRequirement(uk.gov.gchq.gaffer.integration.TraitRequirement) Operation(uk.gov.gchq.gaffer.operation.Operation) Schema(uk.gov.gchq.gaffer.store.schema.Schema) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) TestTypes(uk.gov.gchq.gaffer.store.TestTypes) Builder(uk.gov.gchq.gaffer.operation.impl.GetWalks.Builder) SeededGraphFilters(uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters) GetWalks(uk.gov.gchq.gaffer.operation.impl.GetWalks) Builder(uk.gov.gchq.gaffer.operation.impl.GetWalks.Builder) Builder(uk.gov.gchq.gaffer.operation.impl.GetWalks.Builder) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) Test(org.junit.Test)

Example 5 with Walk

use of uk.gov.gchq.gaffer.data.graph.Walk in project Gaffer by gchq.

the class GetWalksIT method shouldGetPathsWithMultipleEdgeTypes.

@Test
public void shouldGetPathsWithMultipleEdgeTypes() throws Exception {
    // Given
    final GetElements operation = new GetElements.Builder().directedType(DirectedType.DIRECTED).view(new View.Builder().edge(TestGroups.EDGE, new ViewElementDefinition.Builder().properties(TestPropertyNames.COUNT).build()).edge(TestGroups.EDGE_2, new ViewElementDefinition.Builder().properties(TestPropertyNames.COUNT).build()).build()).inOutType(SeededGraphFilters.IncludeIncomingOutgoingType.OUTGOING).build();
    final GetWalks op = new GetWalks.Builder().input(seedA).operations(operation, operation).build();
    // When
    final Iterable<Walk> results = graph.execute(op, getUser());
    // Then
    assertThat(getPaths(results)).isEqualTo("AED,AEF,ABC");
}
Also used : Walk(uk.gov.gchq.gaffer.data.graph.Walk) GetWalks(uk.gov.gchq.gaffer.operation.impl.GetWalks) Builder(uk.gov.gchq.gaffer.operation.impl.GetWalks.Builder) Builder(uk.gov.gchq.gaffer.operation.impl.GetWalks.Builder) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) Test(org.junit.Test)

Aggregations

Walk (uk.gov.gchq.gaffer.data.graph.Walk)36 Test (org.junit.Test)25 GetWalks (uk.gov.gchq.gaffer.operation.impl.GetWalks)19 Builder (uk.gov.gchq.gaffer.operation.impl.GetWalks.Builder)19 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)19 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)17 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)13 Test (org.junit.jupiter.api.Test)7 Set (java.util.Set)6 Conditional (uk.gov.gchq.gaffer.operation.util.Conditional)5 CollectionContains (uk.gov.gchq.koryphe.impl.predicate.CollectionContains)5 IsMoreThan (uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)5 HashMap (java.util.HashMap)4 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)4 Map (uk.gov.gchq.gaffer.operation.impl.Map)4 Edge (uk.gov.gchq.gaffer.data.element.Edge)3 Entity (uk.gov.gchq.gaffer.data.element.Entity)3 TraitRequirement (uk.gov.gchq.gaffer.integration.TraitRequirement)3 OperationException (uk.gov.gchq.gaffer.operation.OperationException)3 List (java.util.List)2