Search in sources :

Example 1 with StarRatingTransform

use of uk.gov.gchq.gaffer.example.films.function.transform.StarRatingTransform 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)

Aggregations

CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)1 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)1 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)1 SampleData (uk.gov.gchq.gaffer.example.films.data.SampleData)1 StarRatingTransform (uk.gov.gchq.gaffer.example.films.function.transform.StarRatingTransform)1 DataGenerator (uk.gov.gchq.gaffer.example.films.generator.DataGenerator)1 IsEqual (uk.gov.gchq.gaffer.function.filter.IsEqual)1 Not (uk.gov.gchq.gaffer.function.filter.Not)1 Graph (uk.gov.gchq.gaffer.graph.Graph)1 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)1 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)1 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)1