Search in sources :

Example 21 with User

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;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) DataGenerator5(uk.gov.gchq.gaffer.example.gettingstarted.generator.DataGenerator5) Graph(uk.gov.gchq.gaffer.graph.Graph) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Edge(uk.gov.gchq.gaffer.data.element.Edge)

Example 22 with User

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;
}
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 23 with User

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;
}
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 24 with User

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));
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) User(uk.gov.gchq.gaffer.user.User) Element(uk.gov.gchq.gaffer.data.element.Element) Properties(uk.gov.gchq.gaffer.data.element.Properties) LinkedList(java.util.LinkedList) ElementSeed(uk.gov.gchq.gaffer.operation.data.ElementSeed) Edge(uk.gov.gchq.gaffer.data.element.Edge)

Example 25 with User

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;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) DataGenerator8(uk.gov.gchq.gaffer.example.gettingstarted.generator.DataGenerator8) Graph(uk.gov.gchq.gaffer.graph.Graph) IsLessThan(uk.gov.gchq.gaffer.function.filter.IsLessThan) GetAllEdges(uk.gov.gchq.gaffer.operation.impl.get.GetAllEdges) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) IsMoreThan(uk.gov.gchq.gaffer.function.filter.IsMoreThan) Edge(uk.gov.gchq.gaffer.data.element.Edge)

Aggregations

User (uk.gov.gchq.gaffer.user.User)378 Test (org.junit.jupiter.api.Test)188 Graph (uk.gov.gchq.gaffer.graph.Graph)155 Element (uk.gov.gchq.gaffer.data.element.Element)143 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)128 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)110 HashSet (java.util.HashSet)109 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)104 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)103 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)98 Edge (uk.gov.gchq.gaffer.data.element.Edge)85 Context (uk.gov.gchq.gaffer.store.Context)85 Entity (uk.gov.gchq.gaffer.data.element.Entity)77 Test (org.junit.Test)61 ArrayList (java.util.ArrayList)57 OperationException (uk.gov.gchq.gaffer.operation.OperationException)52 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)49 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)48 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)45 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)43