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