Search in sources :

Example 21 with GetAdjacentIds

use of uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds in project Gaffer by gchq.

the class GetAdjacentIdsTest method shouldFailValidationWhenEntityHasFilter.

@Test
public void shouldFailValidationWhenEntityHasFilter() throws OperationException {
    // Given
    final Graph graph = GetAllElementsHandlerTest.getGraph();
    final AddElements addElements = new AddElements.Builder().input(GetAllElementsHandlerTest.getElements()).build();
    graph.execute(addElements, new User());
    // When / Then
    try {
        final GetAdjacentIds getAdjacentIds = new GetAdjacentIds.Builder().input(new EntitySeed("A"), new EntitySeed("Y2")).view(new View.Builder().edge(GetAllElementsHandlerTest.BASIC_EDGE1, new ViewElementDefinition.Builder().postAggregationFilter(new ElementFilter.Builder().select(GetAllElementsHandlerTest.COUNT).execute(new IsMoreThan(5)).build()).build()).entity(GetAllElementsHandlerTest.BASIC_ENTITY, new ViewElementDefinition.Builder().postAggregationFilter(new ElementFilter.Builder().select(GetAllElementsHandlerTest.PROPERTY1).execute(new IsEqual("string")).build()).build()).build()).build();
        final CloseableIterable<? extends EntityId> results = graph.execute(getAdjacentIds, new User());
        fail("Exception expected");
    } catch (IllegalArgumentException e) {
        assertTrue(e.getMessage().contains("View should not have entities with filters."));
    }
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) IsEqual(uk.gov.gchq.koryphe.impl.predicate.IsEqual) Graph(uk.gov.gchq.gaffer.graph.Graph) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) Test(org.junit.jupiter.api.Test)

Example 22 with GetAdjacentIds

use of uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds in project Gaffer by gchq.

the class GetAdjacentIdsTest method shouldGetElementsByEntityIdWithViewRestrictedByGroupAndAPreAggregationFilter.

@Test
public void shouldGetElementsByEntityIdWithViewRestrictedByGroupAndAPreAggregationFilter() throws OperationException {
    // Given
    final Graph graph = GetAllElementsHandlerTest.getGraph();
    final AddElements addElements = new AddElements.Builder().input(GetAllElementsHandlerTest.getElements()).build();
    graph.execute(addElements, new User());
    // When
    final GetAdjacentIds getAdjacentIds = new GetAdjacentIds.Builder().input(new EntitySeed("A"), new EntitySeed("Y2")).view(new View.Builder().edge(GetAllElementsHandlerTest.BASIC_EDGE1, new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select(GetAllElementsHandlerTest.COUNT).execute(new IsMoreThan(5)).build()).build()).build()).build();
    final CloseableIterable<? extends EntityId> results = graph.execute(getAdjacentIds, new User());
    // Then
    final Set<EntityId> resultsSet = new HashSet<>();
    Streams.toStream(results).forEach(resultsSet::add);
    final Set<EntityId> expectedResults = new HashSet<>();
    GetAllElementsHandlerTest.getElements().stream().filter(element -> element instanceof Edge).filter(element -> element.getGroup().equals(GetAllElementsHandlerTest.BASIC_EDGE1)).filter(element -> {
        final Edge edge = (Edge) element;
        return edge.getSource().equals("A") || edge.getDestination().equals("A") || edge.getSource().equals("Y2") || edge.getDestination().equals("Y2");
    }).filter(element -> ((Integer) element.getProperty(GetAllElementsHandlerTest.COUNT)) > 5).map(element -> {
        final Edge edge = (Edge) element;
        final Set<EntityId> nodes = new HashSet<>();
        nodes.add(new EntitySeed(edge.getSource()));
        nodes.add(new EntitySeed(edge.getDestination()));
        return nodes;
    }).flatMap(nodes -> nodes.stream()).forEach(expectedResults::add);
    expectedResults.remove(new EntitySeed("A"));
    expectedResults.remove(new EntitySeed("Y2"));
    assertEquals(expectedResults, resultsSet);
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) Assertions.fail(org.junit.jupiter.api.Assertions.fail) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) User(uk.gov.gchq.gaffer.user.User) Set(java.util.Set) IsEqual(uk.gov.gchq.koryphe.impl.predicate.IsEqual) VERTEX_1(uk.gov.gchq.gaffer.mapstore.impl.VisibilityTest.VERTEX_1) Test(org.junit.jupiter.api.Test) HashSet(java.util.HashSet) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Graph(uk.gov.gchq.gaffer.graph.Graph) TestCase.assertTrue(junit.framework.TestCase.assertTrue) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) Edge(uk.gov.gchq.gaffer.data.element.Edge) OperationException(uk.gov.gchq.gaffer.operation.OperationException) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Streams(uk.gov.gchq.gaffer.commonutil.stream.Streams) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) Collections(java.util.Collections) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) User(uk.gov.gchq.gaffer.user.User) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) Graph(uk.gov.gchq.gaffer.graph.Graph) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) Edge(uk.gov.gchq.gaffer.data.element.Edge) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 23 with GetAdjacentIds

use of uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds in project Gaffer by gchq.

the class GetAdjacentIdsTest method shouldGetAdjacentEntityIdWithViewRestrictedByGroup.

@Test
public void shouldGetAdjacentEntityIdWithViewRestrictedByGroup() throws OperationException {
    // Given
    final Graph graph = GetAllElementsHandlerTest.getGraph();
    final AddElements addElements = new AddElements.Builder().input(GetAllElementsHandlerTest.getElements()).build();
    graph.execute(addElements, new User());
    // When
    final GetAdjacentIds getAdjacentIds = new GetAdjacentIds.Builder().input(new EntitySeed("A"), new EntitySeed("Y2")).view(new View.Builder().edge(GetAllElementsHandlerTest.BASIC_EDGE2).build()).build();
    final CloseableIterable<? extends EntityId> results = graph.execute(getAdjacentIds, new User());
    // Then
    final Set<EntityId> resultsSet = new HashSet<>();
    Streams.toStream(results).forEach(resultsSet::add);
    final Set<EntityId> expectedResults = new HashSet<>();
    GetAllElementsHandlerTest.getElements().stream().filter(element -> element instanceof Edge).filter(element -> element.getGroup().equals(GetAllElementsHandlerTest.BASIC_EDGE2)).filter(element -> {
        final Edge edge = (Edge) element;
        return edge.getSource().equals("A") || edge.getDestination().equals("A") || edge.getSource().equals("Y2") || edge.getDestination().equals("Y2");
    }).map(element -> {
        final Edge edge = (Edge) element;
        final Set<EntityId> nodes = new HashSet<>();
        nodes.add(new EntitySeed(edge.getSource()));
        nodes.add(new EntitySeed(edge.getDestination()));
        return nodes;
    }).flatMap(nodes -> nodes.stream()).forEach(expectedResults::add);
    expectedResults.remove(new EntitySeed("A"));
    expectedResults.remove(new EntitySeed("Y2"));
    assertEquals(expectedResults, resultsSet);
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) Assertions.fail(org.junit.jupiter.api.Assertions.fail) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) User(uk.gov.gchq.gaffer.user.User) Set(java.util.Set) IsEqual(uk.gov.gchq.koryphe.impl.predicate.IsEqual) VERTEX_1(uk.gov.gchq.gaffer.mapstore.impl.VisibilityTest.VERTEX_1) Test(org.junit.jupiter.api.Test) HashSet(java.util.HashSet) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Graph(uk.gov.gchq.gaffer.graph.Graph) TestCase.assertTrue(junit.framework.TestCase.assertTrue) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) Edge(uk.gov.gchq.gaffer.data.element.Edge) OperationException(uk.gov.gchq.gaffer.operation.OperationException) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Streams(uk.gov.gchq.gaffer.commonutil.stream.Streams) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) Collections(java.util.Collections) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) User(uk.gov.gchq.gaffer.user.User) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) Graph(uk.gov.gchq.gaffer.graph.Graph) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Edge(uk.gov.gchq.gaffer.data.element.Edge) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 24 with GetAdjacentIds

use of uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds in project Gaffer by gchq.

the class GetAdjacentIdsTest method shouldGetElementsByEntityIdWithViewRestrictedByGroupAndAPostAggregationFilter.

@Test
public void shouldGetElementsByEntityIdWithViewRestrictedByGroupAndAPostAggregationFilter() throws OperationException {
    // Given
    final Graph graph = GetAllElementsHandlerTest.getGraph();
    final AddElements addElements = new AddElements.Builder().input(GetAllElementsHandlerTest.getElements()).build();
    graph.execute(addElements, new User());
    // When
    final GetAdjacentIds getAdjacentIds = new GetAdjacentIds.Builder().input(new EntitySeed("A"), new EntitySeed("Y2")).view(new View.Builder().edge(GetAllElementsHandlerTest.BASIC_EDGE1, new ViewElementDefinition.Builder().postAggregationFilter(new ElementFilter.Builder().select(GetAllElementsHandlerTest.COUNT).execute(new IsMoreThan(5)).build()).build()).build()).build();
    final CloseableIterable<? extends EntityId> results = graph.execute(getAdjacentIds, new User());
    // Then
    final Set<EntityId> resultsSet = new HashSet<>();
    Streams.toStream(results).forEach(resultsSet::add);
    final Set<EntityId> expectedResults = new HashSet<>();
    GetAllElementsHandlerTest.getElements().stream().filter(element -> element instanceof Edge).filter(element -> element.getGroup().equals(GetAllElementsHandlerTest.BASIC_EDGE1)).filter(element -> {
        final Edge edge = (Edge) element;
        return edge.getSource().equals("A") || edge.getDestination().equals("A") || edge.getSource().equals("Y2") || edge.getDestination().equals("Y2");
    }).filter(element -> ((Integer) element.getProperty(GetAllElementsHandlerTest.COUNT)) > 5).map(element -> {
        final Edge edge = (Edge) element;
        final Set<EntityId> nodes = new HashSet<>();
        nodes.add(new EntitySeed(edge.getSource()));
        nodes.add(new EntitySeed(edge.getDestination()));
        return nodes;
    }).flatMap(nodes -> nodes.stream()).forEach(expectedResults::add);
    expectedResults.remove(new EntitySeed("A"));
    expectedResults.remove(new EntitySeed("Y2"));
    assertEquals(expectedResults, resultsSet);
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) Assertions.fail(org.junit.jupiter.api.Assertions.fail) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) User(uk.gov.gchq.gaffer.user.User) Set(java.util.Set) IsEqual(uk.gov.gchq.koryphe.impl.predicate.IsEqual) VERTEX_1(uk.gov.gchq.gaffer.mapstore.impl.VisibilityTest.VERTEX_1) Test(org.junit.jupiter.api.Test) HashSet(java.util.HashSet) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Graph(uk.gov.gchq.gaffer.graph.Graph) TestCase.assertTrue(junit.framework.TestCase.assertTrue) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) Edge(uk.gov.gchq.gaffer.data.element.Edge) OperationException(uk.gov.gchq.gaffer.operation.OperationException) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Streams(uk.gov.gchq.gaffer.commonutil.stream.Streams) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) Collections(java.util.Collections) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) User(uk.gov.gchq.gaffer.user.User) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) Graph(uk.gov.gchq.gaffer.graph.Graph) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) Edge(uk.gov.gchq.gaffer.data.element.Edge) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 25 with GetAdjacentIds

use of uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds in project gaffer-doc by gchq.

the class OperationChains method run.

@Override
public Iterable<? extends String> run() throws OperationException, IOException {
    // [graph] create a graph using our schema and store properties
    // ---------------------------------------------------------
    final Graph graph = new Graph.Builder().config(getDefaultGraphConfig()).addSchemas(StreamUtil.openStreams(getClass(), schemaPath)).storeProperties(getDefaultStoreProperties()).build();
    // ---------------------------------------------------------
    // [user] Create a user
    // ---------------------------------------------------------
    final User user = new User("user01");
    // ---------------------------------------------------------
    // [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 OperationChain<Void> addOpChain = new OperationChain.Builder().first(new GenerateElements.Builder<String>().generator(new RoadAndRoadUseElementGenerator()).input(IOUtils.readLines(StreamUtil.openStream(getClass(), dataPath))).build()).then(new AddElements()).build();
    graph.execute(addOpChain, user);
    // ---------------------------------------------------------
    print("The elements have been added.");
    // [get] Create and execute an operation chain consisting of 2 operations:
    // GetAdjacentIds - starting at vertex M5 get all adjacent vertices (vertices at other end of outbound edges)
    // GetElements - get outbound edges
    // ---------------------------------------------------------
    final OperationChain<? extends Iterable<? extends Element>> opChain = new OperationChain.Builder().first(new GetAdjacentIds.Builder().input(new EntitySeed("M5")).inOutType(IncludeIncomingOutgoingType.OUTGOING).build()).then(new GetElements.Builder().inOutType(IncludeIncomingOutgoingType.OUTGOING).build()).build();
    final Iterable<? extends Element> results = graph.execute(opChain, user);
    // ---------------------------------------------------------
    print("\n2nd hop edges\n");
    for (final Element result : results) {
        print("RESULT", result.toString());
    }
    // [get and convert] Create and execute an operation chain consisting of 3 operations:
    // GetAdjacentIds - starting at vertex 1 get all adjacent vertices (vertices at other end of outbound edges)
    // GetElements - get outbound edges
    // GenerateObjects - convert the edges into csv
    // ---------------------------------------------------------
    final OperationChain<Iterable<? extends String>> opChainWithCSV = new OperationChain.Builder().first(new GetAdjacentIds.Builder().input(new EntitySeed("M5")).inOutType(IncludeIncomingOutgoingType.OUTGOING).build()).then(new GetElements.Builder().inOutType(IncludeIncomingOutgoingType.OUTGOING).build()).then(new GenerateObjects.Builder<String>().generator(new RoadUseCsvGenerator()).build()).build();
    final Iterable<? extends String> csvResults = graph.execute(opChainWithCSV, user);
    // ---------------------------------------------------------
    print("\n2nd hop edges converted back into comma separated strings.\n");
    for (final String result : csvResults) {
        print("CSV_RESULT", result);
    }
    return csvResults;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) RoadAndRoadUseElementGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseElementGenerator) Element(uk.gov.gchq.gaffer.data.element.Element) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) RoadUseCsvGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadUseCsvGenerator) Graph(uk.gov.gchq.gaffer.graph.Graph) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed)

Aggregations

GetAdjacentIds (uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds)57 Test (org.junit.jupiter.api.Test)53 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)32 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)32 User (uk.gov.gchq.gaffer.user.User)32 Context (uk.gov.gchq.gaffer.store.Context)25 Operation (uk.gov.gchq.gaffer.operation.Operation)20 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)19 EntityId (uk.gov.gchq.gaffer.data.element.id.EntityId)13 NamedOperation (uk.gov.gchq.gaffer.named.operation.NamedOperation)12 Limit (uk.gov.gchq.gaffer.operation.impl.Limit)12 HashSet (java.util.HashSet)11 LinkedHashMap (java.util.LinkedHashMap)11 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)11 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)9 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)9 Graph (uk.gov.gchq.gaffer.graph.Graph)8 DiscardOutput (uk.gov.gchq.gaffer.operation.impl.DiscardOutput)8 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)7 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)6