Search in sources :

Example 26 with EntityId

use of uk.gov.gchq.gaffer.data.element.id.EntityId 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 27 with EntityId

use of uk.gov.gchq.gaffer.data.element.id.EntityId in project Gaffer by gchq.

the class FullSchemaLoader method createEntities.

@Override
public Map<EntityId, Entity> createEntities() {
    final Map<EntityId, Entity> entities = new HashMap<>();
    for (int i = 0; i <= 10; i++) {
        for (int j = 0; j < VERTEX_PREFIXES.length; j++) {
            final FreqMap freqMap = new FreqMap();
            freqMap.upsert("key");
            final Entity entity = new Entity(TestGroups.ENTITY, VERTEX_PREFIXES[j] + i);
            entity.putProperty(TestPropertyNames.COUNT, 1L);
            entity.putProperty(TestPropertyNames.PROP_1, 1);
            entity.putProperty(TestPropertyNames.PROP_2, 1L);
            entity.putProperty(TestPropertyNames.PROP_3, "1");
            entity.putProperty(TestPropertyNames.PROP_4, freqMap);
            entity.putProperty(TestPropertyNames.PROP_5, "property");
            entity.putProperty(TestPropertyNames.DATE, Date.from(Instant.ofEpochMilli(1)));
            entity.putProperty(TestPropertyNames.TIMESTAMP, 1L);
            entity.putProperty(TestPropertyNames.VISIBILITY, "public");
            addToMap(entity, entities);
        }
        final FreqMap freqMap2 = new FreqMap();
        freqMap2.upsert("key");
        final Entity secondEntity = new Entity(TestGroups.ENTITY, SOURCE + i);
        secondEntity.putProperty(TestPropertyNames.COUNT, 1L);
        secondEntity.putProperty(TestPropertyNames.PROP_1, 1);
        secondEntity.putProperty(TestPropertyNames.PROP_2, 1L);
        secondEntity.putProperty(TestPropertyNames.PROP_3, "1");
        secondEntity.putProperty(TestPropertyNames.PROP_4, freqMap2);
        secondEntity.putProperty(TestPropertyNames.PROP_5, "property");
        secondEntity.putProperty(TestPropertyNames.DATE, Date.from(Instant.ofEpochMilli(1)));
        secondEntity.putProperty(TestPropertyNames.TIMESTAMP, 1L);
        secondEntity.putProperty(TestPropertyNames.VISIBILITY, "public");
        addToMap(secondEntity, entities);
        final FreqMap freqMap3 = new FreqMap();
        freqMap3.upsert("key");
        final Entity thirdEntity = new Entity(TestGroups.ENTITY, DEST + i);
        thirdEntity.putProperty(TestPropertyNames.COUNT, 1L);
        thirdEntity.putProperty(TestPropertyNames.PROP_1, 1);
        thirdEntity.putProperty(TestPropertyNames.PROP_2, 1L);
        thirdEntity.putProperty(TestPropertyNames.PROP_3, "1");
        thirdEntity.putProperty(TestPropertyNames.PROP_4, freqMap3);
        thirdEntity.putProperty(TestPropertyNames.PROP_5, "property");
        thirdEntity.putProperty(TestPropertyNames.DATE, Date.from(Instant.ofEpochMilli(1)));
        thirdEntity.putProperty(TestPropertyNames.TIMESTAMP, 1L);
        thirdEntity.putProperty(TestPropertyNames.VISIBILITY, "private");
        addToMap(thirdEntity, entities);
        final FreqMap freqMap4 = new FreqMap();
        freqMap4.upsert("key");
        final Entity fourthEntity = new Entity(TestGroups.ENTITY, SOURCE_DIR + i);
        fourthEntity.putProperty(TestPropertyNames.COUNT, 1L);
        fourthEntity.putProperty(TestPropertyNames.PROP_1, 1);
        fourthEntity.putProperty(TestPropertyNames.PROP_2, 1L);
        fourthEntity.putProperty(TestPropertyNames.PROP_3, "1");
        fourthEntity.putProperty(TestPropertyNames.PROP_4, freqMap4);
        fourthEntity.putProperty(TestPropertyNames.PROP_5, "property");
        fourthEntity.putProperty(TestPropertyNames.DATE, Date.from(Instant.ofEpochMilli(1)));
        fourthEntity.putProperty(TestPropertyNames.TIMESTAMP, 1L);
        fourthEntity.putProperty(TestPropertyNames.VISIBILITY, "public");
        addToMap(fourthEntity, entities);
        final FreqMap freqMap5 = new FreqMap();
        freqMap5.upsert("key");
        final Entity fifthEntity = new Entity(TestGroups.ENTITY, DEST_DIR + i);
        fifthEntity.putProperty(TestPropertyNames.COUNT, 1L);
        fifthEntity.putProperty(TestPropertyNames.PROP_1, 1);
        fifthEntity.putProperty(TestPropertyNames.PROP_2, 1L);
        fifthEntity.putProperty(TestPropertyNames.PROP_3, "1");
        fifthEntity.putProperty(TestPropertyNames.PROP_4, freqMap5);
        fifthEntity.putProperty(TestPropertyNames.PROP_5, "property");
        fifthEntity.putProperty(TestPropertyNames.DATE, Date.from(Instant.ofEpochMilli(1)));
        fifthEntity.putProperty(TestPropertyNames.TIMESTAMP, 1L);
        fifthEntity.putProperty(TestPropertyNames.VISIBILITY, "private");
        addToMap(fifthEntity, entities);
    }
    return entities;
}
Also used : EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) Entity(uk.gov.gchq.gaffer.data.element.Entity) FreqMap(uk.gov.gchq.gaffer.types.FreqMap) HashMap(java.util.HashMap)

Example 28 with EntityId

use of uk.gov.gchq.gaffer.data.element.id.EntityId in project Gaffer by gchq.

the class VisibilitySchemaLoader method createEntities.

@Override
public Map<EntityId, Entity> createEntities() {
    final Map<EntityId, Entity> entities = new HashMap<>();
    for (int i = 0; i <= 10; i++) {
        for (int j = 0; j < VERTEX_PREFIXES.length; j++) {
            final Entity entity = new Entity(TestGroups.ENTITY, VERTEX_PREFIXES[j] + i);
            entity.putProperty(TestPropertyNames.COUNT, 1L);
            entity.putProperty(TestPropertyNames.VISIBILITY, "public");
            addToMap(entity, entities);
        }
        final Entity secondEntity = new Entity(TestGroups.ENTITY, SOURCE + i);
        secondEntity.putProperty(TestPropertyNames.COUNT, 1L);
        secondEntity.putProperty(TestPropertyNames.VISIBILITY, "private");
        addToMap(secondEntity, entities);
        final Entity thirdEntity = new Entity(TestGroups.ENTITY, DEST + i);
        thirdEntity.putProperty(TestPropertyNames.COUNT, 1L);
        thirdEntity.putProperty(TestPropertyNames.VISIBILITY, "public");
        addToMap(thirdEntity, entities);
        final Entity fourthEntity = new Entity(TestGroups.ENTITY, SOURCE_DIR + i);
        fourthEntity.putProperty(TestPropertyNames.COUNT, 1L);
        fourthEntity.putProperty(TestPropertyNames.VISIBILITY, "public");
        addToMap(fourthEntity, entities);
        final Entity fifthEntity = new Entity(TestGroups.ENTITY, DEST_DIR + i);
        fifthEntity.putProperty(TestPropertyNames.COUNT, 1L);
        fifthEntity.putProperty(TestPropertyNames.VISIBILITY, "private");
        addToMap(fifthEntity, entities);
    }
    return entities;
}
Also used : EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) Entity(uk.gov.gchq.gaffer.data.element.Entity) HashMap(java.util.HashMap)

Example 29 with EntityId

use of uk.gov.gchq.gaffer.data.element.id.EntityId in project Gaffer by gchq.

the class FilterToOperationConverterTest method testSpecifyVertexAndPropertyFilter.

@Test
public void testSpecifyVertexAndPropertyFilter() {
    final Schema schema = getSchema();
    final SparkSession sparkSession = SparkSessionProvider.getSparkSession();
    // Specify vertex and a filter on property1
    Filter[] filters = new Filter[2];
    filters[0] = new GreaterThan("property1", 5);
    filters[1] = new EqualTo(SchemaToStructTypeConverter.VERTEX_COL_NAME, "0");
    FiltersToOperationConverter converter = new FiltersToOperationConverter(getViewFromSchema(schema), schema, filters);
    Operation operation = converter.getOperation();
    assertTrue(operation instanceof GetRDDOfElements);
    assertEquals(1, ((GraphFilters) operation).getView().getEntityGroups().size());
    assertEquals(0, ((GraphFilters) operation).getView().getEdgeGroups().size());
    final Set<EntityId> seeds = new HashSet<>();
    for (final Object seed : ((GetRDDOfElements) operation).getInput()) {
        seeds.add((EntitySeed) seed);
    }
    assertEquals(Collections.singleton(new EntitySeed("0")), seeds);
    View opView = ((GraphFilters) operation).getView();
    List<TupleAdaptedPredicate<String, ?>> entityPostAggFilters = opView.getEntity(ENTITY_GROUP).getPostAggregationFilterFunctions();
    assertThat(entityPostAggFilters).hasSize(1);
    final ArrayList<String> expectedProperties = new ArrayList<>();
    expectedProperties.add("property1");
    assertThat(entityPostAggFilters.get(0).getSelection()).hasSize(1);
    assertEquals(expectedProperties.get(0), entityPostAggFilters.get(0).getSelection()[0]);
    final ArrayList<Predicate> expectedFunctions = new ArrayList<>();
    expectedFunctions.add(new IsMoreThan(5, false));
    assertEquals(expectedFunctions.get(0), entityPostAggFilters.get(0).getPredicate());
    // Specify vertex and filters on properties property1 and property4
    filters = new Filter[3];
    filters[0] = new GreaterThan("property1", 5);
    filters[1] = new EqualTo(SchemaToStructTypeConverter.VERTEX_COL_NAME, "0");
    filters[2] = new LessThan("property4", 8);
    converter = new FiltersToOperationConverter(getViewFromSchema(schema), schema, filters);
    operation = converter.getOperation();
    assertTrue(operation instanceof GetRDDOfElements);
    assertEquals(1, ((GraphFilters) operation).getView().getEntityGroups().size());
    assertEquals(0, ((GraphFilters) operation).getView().getEdgeGroups().size());
    seeds.clear();
    for (final Object seed : ((GetRDDOfElements) operation).getInput()) {
        seeds.add((EntitySeed) seed);
    }
    assertEquals(Collections.singleton(new EntitySeed("0")), seeds);
    opView = ((GraphFilters) operation).getView();
    entityPostAggFilters = opView.getEntity(ENTITY_GROUP).getPostAggregationFilterFunctions();
    assertThat(entityPostAggFilters).hasSize(2);
    expectedProperties.clear();
    expectedProperties.add("property1");
    expectedProperties.add("property4");
    assertThat(entityPostAggFilters.get(0).getSelection()).hasSize(1);
    assertEquals(expectedProperties.get(0), entityPostAggFilters.get(0).getSelection()[0]);
    assertThat(entityPostAggFilters.get(1).getSelection()).hasSize(1);
    assertEquals(expectedProperties.get(1), entityPostAggFilters.get(1).getSelection()[0]);
    expectedFunctions.clear();
    expectedFunctions.add(new IsMoreThan(5, false));
    expectedFunctions.add(new IsLessThan(8, false));
    assertEquals(expectedFunctions.get(0), entityPostAggFilters.get(0).getPredicate());
    assertEquals(expectedFunctions.get(1), entityPostAggFilters.get(1).getPredicate());
}
Also used : SparkSession(org.apache.spark.sql.SparkSession) Schema(uk.gov.gchq.gaffer.store.schema.Schema) ArrayList(java.util.ArrayList) GraphFilters(uk.gov.gchq.gaffer.operation.graph.GraphFilters) Operation(uk.gov.gchq.gaffer.operation.Operation) TupleAdaptedPredicate(uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate) Predicate(java.util.function.Predicate) IsLessThan(uk.gov.gchq.koryphe.impl.predicate.IsLessThan) LessThan(org.apache.spark.sql.sources.LessThan) IsLessThan(uk.gov.gchq.koryphe.impl.predicate.IsLessThan) GreaterThan(org.apache.spark.sql.sources.GreaterThan) GetRDDOfElements(uk.gov.gchq.gaffer.spark.operation.scalardd.GetRDDOfElements) HashSet(java.util.HashSet) TupleAdaptedPredicate(uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) EqualTo(org.apache.spark.sql.sources.EqualTo) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) Filter(org.apache.spark.sql.sources.Filter) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) Test(org.junit.jupiter.api.Test)

Example 30 with EntityId

use of uk.gov.gchq.gaffer.data.element.id.EntityId in project Gaffer by gchq.

the class FilterToOperationConverterTest method testSpecifyVertex.

@Test
public void testSpecifyVertex() {
    final Schema schema = getSchema();
    final SparkSession sparkSession = SparkSessionProvider.getSparkSession();
    final Filter[] filters = new Filter[1];
    filters[0] = new EqualTo(SchemaToStructTypeConverter.VERTEX_COL_NAME, "0");
    final FiltersToOperationConverter converter = new FiltersToOperationConverter(getViewFromSchema(schema), schema, filters);
    final Operation operation = converter.getOperation();
    assertTrue(operation instanceof GetRDDOfElements);
    assertEquals(Collections.singleton(ENTITY_GROUP), ((GraphFilters) operation).getView().getEntityGroups());
    assertEquals(0, ((GraphFilters) operation).getView().getEdgeGroups().size());
    final Set<EntityId> seeds = new HashSet<>();
    for (final Object seed : ((GetRDDOfElements) operation).getInput()) {
        seeds.add((EntitySeed) seed);
    }
    assertEquals(Collections.singleton(new EntitySeed("0")), seeds);
}
Also used : SparkSession(org.apache.spark.sql.SparkSession) Schema(uk.gov.gchq.gaffer.store.schema.Schema) GraphFilters(uk.gov.gchq.gaffer.operation.graph.GraphFilters) Operation(uk.gov.gchq.gaffer.operation.Operation) EqualTo(org.apache.spark.sql.sources.EqualTo) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) Filter(org.apache.spark.sql.sources.Filter) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) GetRDDOfElements(uk.gov.gchq.gaffer.spark.operation.scalardd.GetRDDOfElements) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

EntityId (uk.gov.gchq.gaffer.data.element.id.EntityId)93 Test (org.junit.jupiter.api.Test)60 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)41 HashSet (java.util.HashSet)22 User (uk.gov.gchq.gaffer.user.User)21 JSONSerialisationTest (uk.gov.gchq.gaffer.JSONSerialisationTest)15 Entity (uk.gov.gchq.gaffer.data.element.Entity)15 EdgeId (uk.gov.gchq.gaffer.data.element.id.EdgeId)15 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)15 Edge (uk.gov.gchq.gaffer.data.element.Edge)14 Element (uk.gov.gchq.gaffer.data.element.Element)14 ElementId (uk.gov.gchq.gaffer.data.element.id.ElementId)14 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)13 GetAdjacentIds (uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds)13 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)12 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)10 ArrayList (java.util.ArrayList)8 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)7 Graph (uk.gov.gchq.gaffer.graph.Graph)7 Context (uk.gov.gchq.gaffer.store.Context)7