Search in sources :

Example 21 with OperationChain

use of uk.gov.gchq.gaffer.operation.OperationChain in project Gaffer by gchq.

the class OperationServiceIT method shouldThrowErrorOnEmptyOperationChain.

@Test
public void shouldThrowErrorOnEmptyOperationChain() throws IOException {
    // When
    final Response response = RestApiTestUtil.executeOperationChain(new OperationChain());
    assertEquals(500, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) Test(org.junit.Test)

Example 22 with OperationChain

use of uk.gov.gchq.gaffer.operation.OperationChain in project Gaffer by gchq.

the class LoadAndQuery12 method run.

public Iterable<Entity> run() throws OperationException {
    // [user] Create a user
    // ---------------------------------------------------------
    final User user = new User("user01");
    // ---------------------------------------------------------
    // [graph] create a graph using our schema and store properties
    // ---------------------------------------------------------
    final Graph graph = new Graph.Builder().addSchemas(getSchemas()).storeProperties(getStoreProperties()).build();
    // ---------------------------------------------------------
    // [add] add the edges to the graph
    // ---------------------------------------------------------
    final Set<String> dummyData = Collections.singleton("");
    final OperationChain addOpChain = new OperationChain.Builder().first(new GenerateElements.Builder<String>().generator(new DataGenerator12()).objects(dummyData).build()).then(new AddElements()).build();
    graph.execute(addOpChain, user);
    // ---------------------------------------------------------
    log("Added the edge A-B 1000 times each time with a ReservoirItemsUnion<String> containing a random string." + " Also added 500 edges X-Y0, X-Y1, ..., X-Y499 each and for each an Entity on X with a" + " ReservoirItemsUnion<String> containing the destination node.");
    // [get red edge] Get the red edge
    // ---------------------------------------------------------
    final GetAllEdges getAllEdges = new GetAllEdges.Builder().view(new View.Builder().edge("red").build()).build();
    final Iterable<Edge> allEdges = graph.execute(getAllEdges, user);
    // ---------------------------------------------------------
    log("\nThe red edge A-B:");
    for (final Edge edge : allEdges) {
        log("GET_A-B_EDGE_RESULT", edge.toString());
    }
    // [get strings sample from the red edge] Get the edge A-B and print out the sample of strings
    // ---------------------------------------------------------
    final GetEdges<EdgeSeed> query = new GetEdges.Builder<EdgeSeed>().addSeed(new EdgeSeed("A", "B", false)).build();
    final Iterable<Edge> edges = graph.execute(query, user);
    final Edge edge = edges.iterator().next();
    final ReservoirItemsSketch<String> stringsSketch = ((ReservoirItemsUnion<String>) edge.getProperty("stringsSample")).getResult();
    final String[] samples = stringsSketch.getSamples();
    final StringBuilder sb = new StringBuilder("10 samples: ");
    for (int i = 0; i < 10 && i < samples.length; i++) {
        if (i > 0) {
            sb.append(", ");
        }
        sb.append(samples[i]);
    }
    // ---------------------------------------------------------
    log("\nEdge A-B with a sample of the strings");
    log("GET_SAMPLE_FOR_RED_EDGE", sb.toString());
    // [get sample from the blue entity] Get the entity Y and print a sample of the neighbours
    // ---------------------------------------------------------
    final GetEntities<EntitySeed> query2 = new GetEntities.Builder<EntitySeed>().addSeed(new EntitySeed("X")).build();
    final Iterable<Entity> entities = graph.execute(query2, user);
    final Entity entity = entities.iterator().next();
    final ReservoirItemsSketch<String> neighboursSketch = ((ReservoirItemsUnion<String>) entity.getProperty("neighboursSample")).getResult();
    final String[] neighboursSample = neighboursSketch.getSamples();
    sb.setLength(0);
    sb.append("10 samples: ");
    for (int i = 0; i < 10 && i < neighboursSample.length; i++) {
        if (i > 0) {
            sb.append(", ");
        }
        sb.append(neighboursSample[i]);
    }
    // ---------------------------------------------------------
    log("\nEntity for vertex X with a sample of its neighbouring vertices");
    log("GET_SAMPLES_FOR_X_RESULT", sb.toString());
    return null;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) Entity(uk.gov.gchq.gaffer.data.element.Entity) User(uk.gov.gchq.gaffer.user.User) DataGenerator12(uk.gov.gchq.gaffer.example.gettingstarted.generator.DataGenerator12) Graph(uk.gov.gchq.gaffer.graph.Graph) GetAllEdges(uk.gov.gchq.gaffer.operation.impl.get.GetAllEdges) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) EdgeSeed(uk.gov.gchq.gaffer.operation.data.EdgeSeed) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Edge(uk.gov.gchq.gaffer.data.element.Edge) ReservoirItemsUnion(com.yahoo.sketches.sampling.ReservoirItemsUnion)

Example 23 with OperationChain

use of uk.gov.gchq.gaffer.operation.OperationChain in project Gaffer by gchq.

the class LoadAndQuery6 method run.

public CloseableIterable<String> run() throws OperationException {
    // [user] Create a user
    // ---------------------------------------------------------
    final User user = new User("user01");
    // ---------------------------------------------------------
    // [graph] create a graph using our schema and store properties
    // ---------------------------------------------------------
    final Graph graph = new Graph.Builder().addSchemas(getSchemas()).storeProperties(getStoreProperties()).build();
    // ---------------------------------------------------------
    // [add] Create a data generator and add the edges to the graph using an operation chain consisting of:
    // generateElements - generating edges from the data (note these are directed edges)
    // addElements - add the edges to the graph
    // ---------------------------------------------------------
    final DataGenerator6 dataGenerator = new DataGenerator6();
    final List<String> data = DataUtils.loadData(getData());
    final OperationChain addOpChain = new OperationChain.Builder().first(new GenerateElements.Builder<String>().generator(dataGenerator).objects(data).build()).then(new AddElements()).build();
    graph.execute(addOpChain, user);
    // ---------------------------------------------------------
    // [get] Create and execute an operation chain consisting of 3 operations:
    //GetAdjacentEntitySeeds - starting at vertex 1 get all adjacent vertices (vertices at other end of outbound edges)
    //GetRelatedEdges - get outbound edges
    //GenerateObjects - convert the edges back into comma separated strings
    // ---------------------------------------------------------
    final OperationChain<CloseableIterable<String>> opChain = new OperationChain.Builder().first(new GetAdjacentEntitySeeds.Builder().addSeed(new EntitySeed("1")).inOutType(IncludeIncomingOutgoingType.OUTGOING).build()).then(new GetEdges.Builder<EntitySeed>().inOutType(IncludeIncomingOutgoingType.OUTGOING).build()).then(new GenerateObjects.Builder<Edge, String>().generator(dataGenerator).build()).build();
    final CloseableIterable<String> results = graph.execute(opChain, user);
    // ---------------------------------------------------------
    log("\nFiltered edges converted back into comma separated strings. The counts have been aggregated\n");
    for (final String result : results) {
        log("RESULT", result);
    }
    return results;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) GetAdjacentEntitySeeds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentEntitySeeds) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) DataGenerator6(uk.gov.gchq.gaffer.example.gettingstarted.generator.DataGenerator6) Graph(uk.gov.gchq.gaffer.graph.Graph) GetEdges(uk.gov.gchq.gaffer.operation.impl.get.GetEdges) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed)

Example 24 with OperationChain

use of uk.gov.gchq.gaffer.operation.OperationChain in project Gaffer by gchq.

the class LoadAndQuery7 method run.

public Iterable<Edge> run() throws OperationException {
    // [user] Create a user
    // ---------------------------------------------------------
    final User user = new User("user01");
    // ---------------------------------------------------------
    // [graph] create a graph using our schema and store properties
    // ---------------------------------------------------------
    final Graph graph = new Graph.Builder().addSchemas(getSchemas()).storeProperties(getStoreProperties()).build();
    // ---------------------------------------------------------
    // [add] add the edges to the graph using an operation chain consisting of:
    // generateElements - generating edges from the data (note these are directed edges)
    // addElements - add the edges to the graph
    // ---------------------------------------------------------
    final OperationChain addOpChain = new OperationChain.Builder().first(new GenerateElements.Builder<String>().generator(new DataGenerator7()).objects(DataUtils.loadData(getData())).build()).then(new AddElements()).build();
    graph.execute(addOpChain, user);
    // ---------------------------------------------------------
    // Create some starting seeds for the sub graph.
    final Iterable<EntitySeed> seeds = Lists.newArrayList(new EntitySeed("1"));
    // Create a view to return only edges that have a count more than 1
    // Note we could have used a different view for each hop in order to
    // specify the edges we wish to hop down or to attempt to prevent caching
    // duplicate edges.
    final View view = new View.Builder().edge("data", new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("count").execute(new IsMoreThan(1)).build()).build()).build();
    // [extractor] Create a generator that will extract entity seeds
    // This generator will extract just the destination vertices from edges
    // and skip any entities.
    // ---------------------------------------------------------
    final EntitySeedExtractor destVerticesExtractor = new EntitySeedExtractor(new IsEdgeValidator(), new AlwaysValid<>(), true, IdentifierType.DESTINATION);
    // ---------------------------------------------------------
    // [get] Create a sub graph
    // Start getting related edges with the given seeds.
    // Then update the export with the results
    // Between each hop we need to extract the destination vertices of the
    // previous edges.
    // Finally finish off by returning all the edges in the export.
    // ---------------------------------------------------------
    final OperationChain opChain = new OperationChain.Builder().first(new GetEdges.Builder<EntitySeed>().seeds(seeds).inOutType(IncludeIncomingOutgoingType.OUTGOING).view(view).build()).then(new ExportToSet()).then(new GenerateObjects<Edge, EntitySeed>(destVerticesExtractor)).then(new GetEdges.Builder<EntitySeed>().inOutType(IncludeIncomingOutgoingType.OUTGOING).view(view).build()).then(new ExportToSet()).then(new GetSetExport()).build();
    final Iterable<Edge> subGraph = (Iterable<Edge>) graph.execute(opChain, user);
    // ---------------------------------------------------------
    log("\nSub graph:");
    for (final Edge edge : subGraph) {
        log("SUB_GRAPH", edge.toString());
    }
    return subGraph;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) ExportToSet(uk.gov.gchq.gaffer.operation.impl.export.set.ExportToSet) GetSetExport(uk.gov.gchq.gaffer.operation.impl.export.set.GetSetExport) IsEdgeValidator(uk.gov.gchq.gaffer.data.IsEdgeValidator) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) DataGenerator7(uk.gov.gchq.gaffer.example.gettingstarted.generator.DataGenerator7) Graph(uk.gov.gchq.gaffer.graph.Graph) EntitySeedExtractor(uk.gov.gchq.gaffer.operation.data.generator.EntitySeedExtractor) GetEdges(uk.gov.gchq.gaffer.operation.impl.get.GetEdges) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) IsMoreThan(uk.gov.gchq.gaffer.function.filter.IsMoreThan) Edge(uk.gov.gchq.gaffer.data.element.Edge)

Example 25 with OperationChain

use of uk.gov.gchq.gaffer.operation.OperationChain in project Gaffer by gchq.

the class GeneratorsIT method shouldConvertFromDomainObjects.

@Test
public void shouldConvertFromDomainObjects() throws OperationException, UnsupportedEncodingException {
    // Given
    final OperationChain<Void> opChain = new OperationChain.Builder().first(new GenerateElements.Builder<DomainObject>().generator(new BasicGenerator()).objects(Arrays.asList(new EntityDomainObject(NEW_VERTEX, "1", null), new EdgeDomainObject(NEW_SOURCE, NEW_DEST, false, 1, 1L))).build()).then(new AddElements()).build();
    // When - add
    graph.execute(opChain, getUser());
    // Then - check they were added correctly
    final List<Element> results = Lists.newArrayList(graph.execute(new GetElements.Builder<>().addSeed(new EntitySeed(NEW_VERTEX)).addSeed(new EdgeSeed(NEW_SOURCE, NEW_DEST, false)).build(), getUser()));
    final Edge expectedEdge = new Edge(TestGroups.EDGE, NEW_SOURCE, NEW_DEST, false);
    expectedEdge.putProperty(TestPropertyNames.INT, 1);
    expectedEdge.putProperty(TestPropertyNames.COUNT, 1L);
    final Entity expectedEntity = new Entity(TestGroups.ENTITY, NEW_VERTEX);
    expectedEntity.putProperty(TestPropertyNames.STRING, "1");
    assertNotNull(results);
    assertEquals(2, results.size());
    assertThat(results, IsCollectionContaining.hasItems(expectedEntity, expectedEdge));
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) Entity(uk.gov.gchq.gaffer.data.element.Entity) BasicGenerator(uk.gov.gchq.gaffer.integration.generators.BasicGenerator) Element(uk.gov.gchq.gaffer.data.element.Element) EntityDomainObject(uk.gov.gchq.gaffer.integration.domain.EntityDomainObject) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) EdgeDomainObject(uk.gov.gchq.gaffer.integration.domain.EdgeDomainObject) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) EdgeSeed(uk.gov.gchq.gaffer.operation.data.EdgeSeed) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Edge(uk.gov.gchq.gaffer.data.element.Edge) Test(org.junit.Test)

Aggregations

OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)65 Test (org.junit.Test)52 User (uk.gov.gchq.gaffer.user.User)38 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)17 Graph (uk.gov.gchq.gaffer.graph.Graph)16 Store (uk.gov.gchq.gaffer.store.Store)13 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)12 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)11 GenerateObjects (uk.gov.gchq.gaffer.operation.impl.generate.GenerateObjects)11 Schema (uk.gov.gchq.gaffer.store.schema.Schema)10 GetAllEdges (uk.gov.gchq.gaffer.operation.impl.get.GetAllEdges)9 GetAdjacentEntitySeeds (uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentEntitySeeds)8 Edge (uk.gov.gchq.gaffer.data.element.Edge)7 Element (uk.gov.gchq.gaffer.data.element.Element)7 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)7 JobDetail (uk.gov.gchq.gaffer.jobtracker.JobDetail)7 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)7 InOrder (org.mockito.InOrder)6 UnauthorisedException (uk.gov.gchq.gaffer.commonutil.exception.UnauthorisedException)6 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)6