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());
}
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;
}
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;
}
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;
}
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));
}
Aggregations