use of uk.gov.gchq.gaffer.user.User in project Gaffer by gchq.
the class LoadAndQuery5 method run.
public CloseableIterable<Edge> run() throws OperationException {
// [user] Create a user
// ---------------------------------------------------------
final User basicUser = new User("basicUser");
// ---------------------------------------------------------
// [generate] create some edges from the data file using our data generator class
// ---------------------------------------------------------
final List<Element> elements = new ArrayList<>();
final DataGenerator5 dataGenerator = new DataGenerator5();
for (final String s : DataUtils.loadData(getData())) {
elements.add(dataGenerator.getElement(s));
}
// ---------------------------------------------------------
log("Elements generated from the data file.");
for (final Element element : elements) {
log("GENERATED_EDGES", element.toString());
}
log("");
// [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 AddElements addElements = new AddElements.Builder().elements(elements).build();
graph.execute(addElements, basicUser);
// ---------------------------------------------------------
log("The elements have been added.\n");
log("\nNow run a simple query to get edges\n");
// [get simple] get all the edges that contain the vertex "1"
// ---------------------------------------------------------
final GetEdges<EntitySeed> getEdges = new GetEdges.Builder<EntitySeed>().addSeed(new EntitySeed("1")).build();
final CloseableIterable<Edge> resultsWithBasicUser = graph.execute(getEdges, basicUser);
// ---------------------------------------------------------
for (final Element e : resultsWithBasicUser) {
log("GET_RELATED_EDGES_RESULT", e.toString());
}
log("We get nothing back");
log("\nGet edges with the public visibility. We shouldn't see any of the private ones.\n");
// [get public] get all the edges that contain the vertex "1"
// ---------------------------------------------------------
final User publicUser = new User.Builder().userId("publicUser").dataAuth("public").build();
final GetEdges<EntitySeed> getPublicRelatedEdges = new GetEdges.Builder<EntitySeed>().addSeed(new EntitySeed("1")).build();
final CloseableIterable<Edge> publicResults = graph.execute(getPublicRelatedEdges, publicUser);
// ---------------------------------------------------------
for (final Element e : publicResults) {
log("GET_PUBLIC_RELATED_EDGES_RESULT", e.toString());
}
log("\nGet edges with the private visibility. We should get the public edges too.\n");
// [get private] get all the edges that contain the vertex "1"
// ---------------------------------------------------------
final User privateUser = new User.Builder().userId("privateUser").dataAuth("private").build();
final GetEdges<EntitySeed> getPrivateRelatedEdges = new GetEdges.Builder<EntitySeed>().addSeed(new EntitySeed("1")).build();
final CloseableIterable<Edge> privateResults = graph.execute(getPrivateRelatedEdges, privateUser);
// ---------------------------------------------------------
for (final Element e : privateResults) {
log("GET_PRIVATE_RELATED_EDGES_RESULT", e.toString());
}
return publicResults;
}
use of uk.gov.gchq.gaffer.user.User 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.user.User 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.user.User in project Gaffer by gchq.
the class GetElementsIT method shouldGetElements.
private void shouldGetElements(final List<Element> expectedElements, final SeedMatchingType seedMatching, final IncludeEdgeType includeEdgeType, final Boolean includeEntities, final IncludeIncomingOutgoingType inOutType, final Iterable<ElementSeed> seeds) throws IOException, OperationException {
// Given
final User user = new User();
final GetElements<ElementSeed, Element> op = new GetElements.Builder<>().seedMatching(seedMatching).build();
op.setSeeds(seeds);
op.setIncludeEntities(includeEntities);
op.setIncludeEdges(includeEdgeType);
op.setIncludeIncomingOutGoing(inOutType);
op.setView(new View.Builder().entity(TestGroups.ENTITY).edge(TestGroups.EDGE).build());
// When
final CloseableIterable<? extends Element> results = graph.execute(op, user);
// Then
final List<Element> expectedElementsCopy = Lists.newArrayList(expectedElements);
for (final Element result : results) {
final ElementSeed seed = ElementSeed.createSeed(result);
if (result instanceof Entity) {
Entity entity = (Entity) result;
final Collection<Element> listOfElements = new LinkedList<>();
Iterables.addAll(listOfElements, results);
assertTrue("Entity was not expected: " + entity, expectedElements.contains(entity));
} else {
Edge edge = (Edge) result;
if (edge.isDirected()) {
assertTrue("Edge was not expected: " + edge, expectedElements.contains(edge));
} else {
final Edge edgeReversed = new Edge(TestGroups.EDGE, edge.getDestination(), edge.getSource(), edge.isDirected());
Properties properties = edge.getProperties();
edgeReversed.copyProperties(properties);
expectedElementsCopy.remove(edgeReversed);
assertTrue("Edge was not expected: " + seed, expectedElements.contains(result) || expectedElements.contains(edgeReversed));
}
}
expectedElementsCopy.remove(result);
}
assertEquals("The number of elements returned was not as expected. Missing elements: " + expectedElementsCopy + ". Seeds: " + seeds, expectedElements.size(), Lists.newArrayList(results).size());
assertEquals(new HashSet<>(expectedElements), Sets.newHashSet(results));
}
use of uk.gov.gchq.gaffer.user.User in project Gaffer by gchq.
the class LoadAndQuery8 method run.
public Iterable<Edge> run() throws OperationException {
// [user] Create a user who can see public and private data
// ---------------------------------------------------------
final User user = new User.Builder().userId("user").dataAuths("public", "private").build();
// ---------------------------------------------------------
// [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 OperationChain addOpChain = new OperationChain.Builder().first(new GenerateElements.Builder<String>().generator(new DataGenerator8()).objects(DataUtils.loadData(getData())).build()).then(new AddElements()).build();
graph.execute(addOpChain, user);
// ---------------------------------------------------------
// [get] Get all edges
// ---------------------------------------------------------
final GetAllEdges allEdgesOperation = new GetAllEdges();
final Iterable<Edge> edges = graph.execute(allEdgesOperation, user);
// ---------------------------------------------------------
log("\nAll edges in daily time buckets:");
for (final Edge edge : edges) {
log("GET_ALL_EDGES_RESULT", edge.toString());
}
// [get all edges summarised] Get all edges summarised (merge all time windows together)
// This is achieved by overriding the 'groupBy' start and end time properties.
// ---------------------------------------------------------
final GetAllEdges edgesSummarisedOperation = new GetAllEdges.Builder().view(new View.Builder().edge("data", new ViewElementDefinition.Builder().groupBy().build()).build()).build();
final Iterable<Edge> edgesSummarised = graph.execute(edgesSummarisedOperation, user);
// ---------------------------------------------------------
log("\nAll edges summarised:");
for (final Edge edge : edgesSummarised) {
log("GET_ALL_EDGES_SUMMARISED_RESULT", edge.toString());
}
// [get all edges summarised in time window] Get all edges summarised over a provided 2 day time period
// This is achieved by overriding the 'groupBy' start and end time properties
// and providing a filter.
// ---------------------------------------------------------
final GetAllEdges edgesSummarisedInTimeWindowOperation = new GetAllEdges.Builder().view(new View.Builder().edge("data", new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("startDate").execute(new IsMoreThan(JAN_01_16, true)).select("endDate").execute(new IsLessThan(JAN_02_16, true)).build()).groupBy().build()).build()).build();
final Iterable<Edge> edgesSummarisedInTimeWindow = graph.execute(edgesSummarisedInTimeWindowOperation, user);
// ---------------------------------------------------------
log("\nEdges in 2 day time window:");
for (final Edge edge : edgesSummarisedInTimeWindow) {
log("GET_ALL_EDGES_SUMMARISED_IN_TIME_WINDOW_RESULT", edge.toString());
}
// Repeat with a public user and you will see the private edges are not part of the summarised edges
final User publicUser = new User.Builder().userId("public user").dataAuths("public").build();
final Iterable<Edge> publicEdgesSummarisedInTimeWindow = graph.execute(edgesSummarisedInTimeWindowOperation, publicUser);
log("\nPublic edges in 2 day time window:");
for (final Edge edge : publicEdgesSummarisedInTimeWindow) {
log("GET_PUBLIC_EDGES_SUMMARISED_IN_TIME_WINDOW_RESULT", edge.toString());
}
return publicEdgesSummarisedInTimeWindow;
}
Aggregations