Search in sources :

Example 11 with CloseableIterable

use of uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable in project Gaffer by gchq.

the class CoreOperationChainOptimiserTest method shouldAddValidateOperationsForAllValidatableOperations.

@Test
public void shouldAddValidateOperationsForAllValidatableOperations() throws Exception {
    // Given
    final Store store = mock(Store.class);
    final CoreOperationChainOptimiser optimiser = new CoreOperationChainOptimiser(store);
    final CloseableIterable<Element> elements = mock(CloseableIterable.class);
    final Validatable<Integer> validatable1 = mock(Validatable.class);
    final Operation<Iterable<Element>, Iterable<Element>> nonValidatable1 = mock(Operation.class);
    final Validatable<Iterable<Element>> validatable2 = mock(Validatable.class);
    final Validatable<Iterable<Element>> validatable3 = mock(Validatable.class);
    final Operation<Iterable<Element>, Iterable<Element>> nonValidatable2 = mock(Operation.class);
    final boolean skipInvalidElements = true;
    final OperationChain<Integer> opChain = new OperationChain.Builder().first(nonValidatable2).then(validatable3).then(validatable2).then(nonValidatable1).then(validatable1).build();
    given(validatable1.getElements()).willReturn(elements);
    given(validatable1.isSkipInvalidElements()).willReturn(skipInvalidElements);
    given(validatable2.isSkipInvalidElements()).willReturn(skipInvalidElements);
    given(validatable1.isValidate()).willReturn(true);
    given(validatable2.isValidate()).willReturn(true);
    given(validatable3.isValidate()).willReturn(false);
    // When
    final OperationChain<Integer> optimisedOpChain = optimiser.optimise(opChain);
    // Then
    assertEquals(7, optimisedOpChain.getOperations().size());
    assertSame(nonValidatable2, optimisedOpChain.getOperations().get(0));
    assertSame(validatable3, optimisedOpChain.getOperations().get(1));
    assertTrue(optimisedOpChain.getOperations().get(2) instanceof Validate);
    assertSame(validatable2, optimisedOpChain.getOperations().get(3));
    assertSame(nonValidatable1, optimisedOpChain.getOperations().get(4));
    assertTrue(optimisedOpChain.getOperations().get(5) instanceof Validate);
    assertSame(elements, ((Validate) optimisedOpChain.getOperations().get(5)).getElements());
    assertSame(validatable1, optimisedOpChain.getOperations().get(6));
    verify(validatable2).setElements(null);
    verify(validatable1).setElements(null);
    verify(validatable3, never()).setElements(null);
}
Also used : CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) Element(uk.gov.gchq.gaffer.data.element.Element) Store(uk.gov.gchq.gaffer.store.Store) Validate(uk.gov.gchq.gaffer.operation.impl.Validate) Test(org.junit.Test)

Example 12 with CloseableIterable

use of uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable in project Gaffer by gchq.

the class LoadAndQuery method run.

/**
     * Finds average reviews (from other users) of all films viewed by user02.
     * <ul>
     * <li>Starts from a seed of user02.</li>
     * <li>Finds all filmIds connected to user02 (adjacent entity seeds)</li>
     * <li>Then finds all reviews that have those filmIds.</li>
     * <li>Then filters out all reviews from user02.</li>
     * <li>Then aggregates the reviews together.</li>
     * <li>Then transforms the rating from a percent to a 5 star rating and stores the value in a transient property called starRating</li>
     * <li>Then returns the reviews (Entities)</li>
     * </ul>
     * This query can be written in JSON and executed over a rest service - see
     * resources/example/films/json/load.json and resources/example/films/json/query.json
     *
     * @return the review entities
     * @throws OperationException if operation chain fails to be executed on the graph
     */
public CloseableIterable<Entity> run() throws OperationException {
    // Setup graph
    final Graph graph = new Graph.Builder().storeProperties(StreamUtil.openStream(getClass(), "/example/films/mockaccumulostore.properties", true)).addSchemas(StreamUtil.openStreams(getClass(), "/example/films/schema", true)).build();
    // Populate the graph with some example data
    // Create an operation chain. The output from the first operation is passed in as the input the second operation.
    // So the chain operation will generate elements from the domain objects then add these elements to the graph.
    final OperationChain<Void> populateChain = new OperationChain.Builder().first(new GenerateElements.Builder<>().objects(new SampleData().generate()).generator(new DataGenerator()).build()).then(new AddElements.Builder().build()).build();
    // Execute the populate operation chain on the graph
    graph.execute(populateChain, USER);
    // Run a query on the graph to fetch average star ratings for all films user02 has watched.
    // Create an operation chain.
    // So the chain operation will get the adjacent review entity seeds then get the review entities.
    final OperationChain<CloseableIterable<Entity>> queryChain = new OperationChain.Builder().first(new GetAdjacentEntitySeeds.Builder().view(new View.Builder().edge(Group.VIEWING).build()).addSeed(new EntitySeed("user02")).build()).then(new GetEntities.Builder().view(new View.Builder().entity(Group.REVIEW, new ViewElementDefinition.Builder().transientProperty(TransientProperty.FIVE_STAR_RATING, Float.class).preAggregationFilter(new ElementFilter.Builder().select(Property.USER_ID).execute(new Not(new IsEqual("user02"))).build()).groupBy().transformer(new ElementTransformer.Builder().select(Property.RATING, Property.COUNT).project(TransientProperty.FIVE_STAR_RATING).execute(new StarRatingTransform()).build()).build()).build()).build()).build();
    // Execute the query operation chain on the graph.
    return graph.execute(queryChain, USER);
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) StarRatingTransform(uk.gov.gchq.gaffer.example.films.function.transform.StarRatingTransform) SampleData(uk.gov.gchq.gaffer.example.films.data.SampleData) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) IsEqual(uk.gov.gchq.gaffer.function.filter.IsEqual) Not(uk.gov.gchq.gaffer.function.filter.Not) Graph(uk.gov.gchq.gaffer.graph.Graph) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) DataGenerator(uk.gov.gchq.gaffer.example.films.generator.DataGenerator) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed)

Example 13 with CloseableIterable

use of uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable in project Gaffer by gchq.

the class OperationChainTest method shouldBuildOperationChain.

@Test
public void shouldBuildOperationChain() {
    // Given
    final AddElements addElements = mock(AddElements.class);
    final GetElements getAdj1 = mock(GetElements.class);
    final GetElements getAdj2 = mock(GetElements.class);
    final GetElements<EntitySeed, Element> getRelElements = mock(GetElements.class);
    // When
    final OperationChain<CloseableIterable<Element>> opChain = new Builder().first(addElements).then(getAdj1).then(getAdj2).then(getRelElements).build();
    // Then
    assertArrayEquals(new Operation[] { addElements, getAdj1, getAdj2, getRelElements }, opChain.getOperationArray());
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) Element(uk.gov.gchq.gaffer.data.element.Element) Builder(uk.gov.gchq.gaffer.operation.OperationChain.Builder) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Test(org.junit.Test)

Example 14 with CloseableIterable

use of uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable in project Gaffer by gchq.

the class OperationChainTest method shouldReturnReadableStringForToString.

@Test
public void shouldReturnReadableStringForToString() {
    // Given
    final AddElements addElements = new AddElements();
    final GetAdjacentEntitySeeds getAdj1 = new GetAdjacentEntitySeeds();
    final GetAdjacentEntitySeeds getAdj2 = new GetAdjacentEntitySeeds();
    final GetElements<EntitySeed, Element> getRelElements = new GetElements<>();
    final OperationChain<CloseableIterable<Element>> opChain = new Builder().first(addElements).then(getAdj1).then(getAdj2).then(getRelElements).build();
    // When
    final String toString = opChain.toString();
    // Then
    final String expectedToString = "OperationChain[AddElements->GetAdjacentEntitySeeds->GetAdjacentEntitySeeds->GetElements]";
    assertEquals(expectedToString, toString);
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) GetAdjacentEntitySeeds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentEntitySeeds) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) Element(uk.gov.gchq.gaffer.data.element.Element) Builder(uk.gov.gchq.gaffer.operation.OperationChain.Builder) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Test(org.junit.Test)

Example 15 with CloseableIterable

use of uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable in project Gaffer by gchq.

the class ArrayListStoreTest method shouldAddAndGetEdgesThenEntities.

@Test
public void shouldAddAndGetEdgesThenEntities() throws OperationException {
    final Graph graph = createGraph();
    addElementsToGraph(graph);
    //set up the operation to fetch the entities
    final OperationChain<CloseableIterable<SimpleEntityDataObject>> opChain = new OperationChain.Builder().first(new GetEdges.Builder<>().addSeed(new EntitySeed(1)).build()).then(new GenerateObjects.Builder<Edge, EntitySeed>().generator(new EntitySeedExtractor(IdentifierType.DESTINATION)).build()).then(new GetEntities.Builder().view(new View.Builder().entity(TestGroups.ENTITY, new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select(TestPropertyNames.INT).execute(new IsLessThan(2)).build()).build()).build()).build()).then(new GenerateObjects.Builder<Entity, SimpleEntityDataObject>().generator(new SimpleEntityGenerator()).build()).build();
    //now do the hop
    final CloseableIterable<SimpleEntityDataObject> results = graph.execute(opChain, new User());
    //check the results by converting our edges back into SimpleDataObjects
    if (!results.iterator().hasNext()) {
        fail("No results returned");
    } else {
        for (final SimpleEntityDataObject obj : results) {
            LOGGER.info(obj.toString());
        }
        final List<SimpleEntityDataObject> resultList = Lists.newArrayList(results);
        assertEquals(1, resultList.size());
        assertEquals(1, resultList.get(0).getId());
        assertEquals(1, resultList.get(0).getVisibility());
        assertEquals("Red", resultList.get(0).getProperties());
    }
    results.close();
}
Also used : SimpleEntityGenerator(uk.gov.gchq.gaffer.arrayliststore.data.generator.SimpleEntityGenerator) User(uk.gov.gchq.gaffer.user.User) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) GetEntities(uk.gov.gchq.gaffer.operation.impl.get.GetEntities) Graph(uk.gov.gchq.gaffer.graph.Graph) IsLessThan(uk.gov.gchq.gaffer.function.filter.IsLessThan) 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) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) SimpleEntityDataObject(uk.gov.gchq.gaffer.arrayliststore.data.SimpleEntityDataObject) Test(org.junit.Test)

Aggregations

CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)21 Test (org.junit.Test)16 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)11 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)11 User (uk.gov.gchq.gaffer.user.User)11 Graph (uk.gov.gchq.gaffer.graph.Graph)10 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)6 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)6 Element (uk.gov.gchq.gaffer.data.element.Element)5 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)5 IsLessThan (uk.gov.gchq.gaffer.function.filter.IsLessThan)5 GetEdges (uk.gov.gchq.gaffer.operation.impl.get.GetEdges)4 SimpleEntityDataObject (uk.gov.gchq.gaffer.arrayliststore.data.SimpleEntityDataObject)3 SimpleEntityGenerator (uk.gov.gchq.gaffer.arrayliststore.data.generator.SimpleEntityGenerator)3 JSONSerialiser (uk.gov.gchq.gaffer.jsonserialisation.JSONSerialiser)3 Builder (uk.gov.gchq.gaffer.operation.OperationChain.Builder)3 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)3 SimpleEdgeDataObject (uk.gov.gchq.gaffer.arrayliststore.data.SimpleEdgeDataObject)2 SimpleEdgeGenerator (uk.gov.gchq.gaffer.arrayliststore.data.generator.SimpleEdgeGenerator)2 JobDetail (uk.gov.gchq.gaffer.jobtracker.JobDetail)2