use of uk.gov.gchq.gaffer.example.films.data.SampleData 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);
}
Aggregations