Search in sources :

Example 1 with ToSet

use of uk.gov.gchq.gaffer.operation.impl.output.ToSet in project gaffer-doc by gchq.

the class FullExample 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");
    // ---------------------------------------------------------
    try (final CSVParser parser = CSVParser.parse(getClass().getResource("/FullExample/data.txt"), Charset.defaultCharset(), CSVFormat.DEFAULT.withFirstRecordAsHeader())) {
        final OperationChain<Void> addOpChain = new OperationChain.Builder().first(new GenerateElements.Builder<CSVRecord>().generator(new RoadTrafficCsvElementGenerator()).input(parser).build()).then(new AddElements()).build();
        graph.execute(addOpChain, user);
    }
    // ---------------------------------------------------------
    print("The elements have been added.");
    // [get] Get all road junctions in the South West that were heavily used by buses in the year 2000.
    // ---------------------------------------------------------
    final OperationChain<Iterable<? extends String>> opChain = new OperationChain.Builder().first(new GetAdjacentIds.Builder().input(new EntitySeed("South West")).view(new View.Builder().edge("RegionContainsLocation").build()).build()).then(new GetAdjacentIds.Builder().view(new View.Builder().edge("LocationContainsRoad").build()).build()).then(new ToSet<>()).then(new GetAdjacentIds.Builder().view(new View.Builder().edge("RoadHasJunction").build()).build()).then(new GetElements.Builder().view(new View.Builder().globalElements(new GlobalViewElementDefinition.Builder().groupBy().build()).entity("JunctionUse", new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("startDate", "endDate").execute(new InDateRangeDual.Builder().start("2000/01/01").end("2001/01/01").build()).build()).postAggregationFilter(new ElementFilter.Builder().select("countByVehicleType").execute(new PredicateMap<>("BUS", new IsMoreThan(1000L))).build()).transientProperty("busCount", Long.class).transformer(new ElementTransformer.Builder().select("countByVehicleType").execute(new FreqMapExtractor("BUS")).project("busCount").build()).build()).build()).inOutType(SeededGraphFilters.IncludeIncomingOutgoingType.OUTGOING).build()).then(new Sort.Builder().comparators(new ElementPropertyComparator.Builder().groups("JunctionUse").property("busCount").reverse(true).build()).resultLimit(2).deduplicate(true).build()).then(new ToCsv.Builder().generator(new CsvGenerator.Builder().vertex("Junction").property("busCount", "Bus Count").build()).build()).build();
    // ---------------------------------------------------------
    printJsonAndPython("GET", opChain);
    final Iterable<? extends String> results = graph.execute(opChain, user);
    print("\nAll road junctions in the South West that were heavily used by buses in year 2000.");
    for (final String result : results) {
        print("RESULT", result);
    }
    return results;
}
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) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) ToSet(uk.gov.gchq.gaffer.operation.impl.output.ToSet) Sort(uk.gov.gchq.gaffer.operation.impl.compare.Sort) RoadTrafficCsvElementGenerator(uk.gov.gchq.gaffer.traffic.generator.RoadTrafficCsvElementGenerator) ElementTransformer(uk.gov.gchq.gaffer.data.element.function.ElementTransformer) FreqMapExtractor(uk.gov.gchq.gaffer.types.function.FreqMapExtractor) CsvGenerator(uk.gov.gchq.gaffer.data.generator.CsvGenerator) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) ElementPropertyComparator(uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator) InDateRangeDual(uk.gov.gchq.koryphe.impl.predicate.range.InDateRangeDual) Graph(uk.gov.gchq.gaffer.graph.Graph) CSVParser(org.apache.commons.csv.CSVParser) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) 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)

Example 2 with ToSet

use of uk.gov.gchq.gaffer.operation.impl.output.ToSet in project Gaffer by gchq.

the class Queries method runFullExample.

private void runFullExample(final Graph graph, final User user) throws OperationException {
    final OperationChain<Iterable<? extends String>> opChain = new OperationChain.Builder().first(new GetAdjacentIds.Builder().input(new EntitySeed("South West")).view(new View.Builder().edge("RegionContainsLocation").build()).build()).then(new GetAdjacentIds.Builder().view(new View.Builder().edge("LocationContainsRoad").build()).build()).then(new ToSet<>()).then(new GetAdjacentIds.Builder().view(new View.Builder().edge("RoadHasJunction").build()).build()).then(new GetElements.Builder().view(new View.Builder().globalElements(new GlobalViewElementDefinition.Builder().groupBy().build()).entity("JunctionUse", new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("startDate", "endDate").execute(new InDateRangeDual.Builder().start("2000/01/01").end("2001/01/01").build()).build()).postAggregationFilter(new ElementFilter.Builder().select("countByVehicleType").execute(new PredicateMap<>("BUS", new IsMoreThan(1000L))).build()).transientProperty("busCount", Long.class).transformer(new ElementTransformer.Builder().select("countByVehicleType").execute(new FreqMapExtractor("BUS")).project("busCount").build()).build()).build()).inOutType(SeededGraphFilters.IncludeIncomingOutgoingType.OUTGOING).build()).then(new Sort.Builder().comparators(new ElementPropertyComparator.Builder().groups("JunctionUse").property("busCount").reverse(true).build()).resultLimit(2).deduplicate(true).build()).then(new ToCsv.Builder().generator(new CsvGenerator.Builder().vertex("Junction").property("busCount", "Bus Count").build()).build()).build();
    final Iterable<? extends String> results = graph.execute(opChain, user);
    System.out.println("Full example results:");
    for (final String result : results) {
        System.out.println(result);
    }
}
Also used : GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) Builder(uk.gov.gchq.gaffer.graph.Graph.Builder) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) ToSet(uk.gov.gchq.gaffer.operation.impl.output.ToSet) Sort(uk.gov.gchq.gaffer.operation.impl.compare.Sort) ElementTransformer(uk.gov.gchq.gaffer.data.element.function.ElementTransformer) FreqMapExtractor(uk.gov.gchq.gaffer.types.function.FreqMapExtractor) CsvGenerator(uk.gov.gchq.gaffer.data.generator.CsvGenerator) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) ElementPropertyComparator(uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator) InDateRangeDual(uk.gov.gchq.koryphe.impl.predicate.range.InDateRangeDual) 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)

Example 3 with ToSet

use of uk.gov.gchq.gaffer.operation.impl.output.ToSet in project Gaffer by gchq.

the class AddOperationsToChainTest method shouldAddIfOperation.

@Test
public void shouldAddIfOperation() throws SerialisationException {
    // Given
    final GetWalks getWalks = new GetWalks();
    final uk.gov.gchq.gaffer.operation.impl.Map map = new uk.gov.gchq.gaffer.operation.impl.Map();
    final ToVertices toVertices = new ToVertices();
    final ToSet toSet = new ToSet();
    final Exists exists = new Exists();
    final Limit limit = new Limit();
    final GetAllElements getAllElements = new GetAllElements();
    final GetElements getElements = new GetElements();
    final Conditional conditional = new Conditional();
    conditional.setPredicate(exists);
    final If ifOp = new If.Builder<>().conditional(conditional).then(getElements).otherwise(getAllElements).build();
    final AddOperationsToChain hook = new AddOperationsToChain();
    final Map<String, List<Operation>> after = new HashMap<>();
    final List<Operation> afterOps = new LinkedList<>();
    afterOps.add(ifOp);
    afterOps.add(limit);
    after.put("uk.gov.gchq.gaffer.operation.impl.output.ToSet", afterOps);
    hook.setAfter(after);
    final OperationChain opChain = new OperationChain.Builder().first(getWalks).then(map).then(toVertices).then(toSet).build();
    // When
    hook.preExecute(opChain, new Context());
    // Then
    final OperationChain expectedOpChain = new OperationChain.Builder().first(getWalks).then(map).then(toVertices).then(toSet).then(ifOp).then(limit).build();
    JsonAssert.assertEquals(JSONSerialiser.serialise(expectedOpChain), JSONSerialiser.serialise(opChain));
}
Also used : HashMap(java.util.HashMap) GetWalks(uk.gov.gchq.gaffer.operation.impl.GetWalks) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) Operation(uk.gov.gchq.gaffer.operation.Operation) ToSet(uk.gov.gchq.gaffer.operation.impl.output.ToSet) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) LinkedList(java.util.LinkedList) List(java.util.List) Context(uk.gov.gchq.gaffer.store.Context) ToVertices(uk.gov.gchq.gaffer.operation.impl.output.ToVertices) LinkedList(java.util.LinkedList) Exists(uk.gov.gchq.koryphe.impl.predicate.Exists) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) Limit(uk.gov.gchq.gaffer.operation.impl.Limit) HashMap(java.util.HashMap) Map(java.util.Map) If(uk.gov.gchq.gaffer.operation.impl.If) Test(org.junit.jupiter.api.Test)

Example 4 with ToSet

use of uk.gov.gchq.gaffer.operation.impl.output.ToSet in project Gaffer by gchq.

the class WhileScoreResolverTest method shouldGetScoreWithOperationChainAsOperation.

@Test
public void shouldGetScoreWithOperationChainAsOperation() {
    // Given
    final Object input = new EntitySeed(3);
    final int repeats = 3;
    final GetElements getElements = mock(GetElements.class);
    final Map map = mock(Map.class);
    final ToSet toSet = mock(ToSet.class);
    final OperationChain transformChain = mock(OperationChain.class);
    final List<Operation> transformOps = new LinkedList<>();
    transformOps.add(map);
    transformOps.add(toSet);
    given(transformChain.getOperations()).willReturn(transformOps);
    final Conditional conditional = mock(Conditional.class);
    given(conditional.getTransform()).willReturn(transformChain);
    final While operation = new While.Builder<>().input(input).maxRepeats(repeats).conditional(conditional).operation(getElements).build();
    final LinkedHashMap<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
    opScores.put(Operation.class, 1);
    opScores.put(Map.class, 3);
    opScores.put(ToSet.class, 1);
    opScores.put(GetElements.class, 2);
    final WhileScoreResolver resolver = new WhileScoreResolver();
    final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(opScores);
    // When
    final int score = resolver.getScore(operation, defaultResolver);
    // Then
    assertEquals(18, score);
}
Also used : GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) Operation(uk.gov.gchq.gaffer.operation.Operation) While(uk.gov.gchq.gaffer.operation.impl.While) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) ToSet(uk.gov.gchq.gaffer.operation.impl.output.ToSet) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) LinkedHashMap(java.util.LinkedHashMap) Map(uk.gov.gchq.gaffer.operation.impl.Map) Test(org.junit.jupiter.api.Test)

Example 5 with ToSet

use of uk.gov.gchq.gaffer.operation.impl.output.ToSet in project Gaffer by gchq.

the class MapHandlerTest method shouldProcessWalksWithEdgeExtraction.

@Test
public void shouldProcessWalksWithEdgeExtraction() throws OperationException {
    // Given
    final Iterable<Walk> walks = Arrays.asList(walk, walk1);
    final Map<Iterable<Walk>, Iterable<Edge>> map = new Map.Builder<Iterable<Walk>>().input(walks).first(new IterableFunction.Builder<Walk>().first(new ExtractWalkEdgesFromHop(1)).then(new FirstItem<>()).build()).build();
    final ToVertices toVertices = new ToVertices.Builder().edgeVertices(ToVertices.EdgeVertices.SOURCE).build();
    final ToSet<Object> toSet = new ToSet<>();
    final OperationChain<Set<?>> opChain = new OperationChain.Builder().first(map).then(toVertices).then(toSet).build();
    final OperationChainValidator opChainValidator = mock(OperationChainValidator.class);
    final List<OperationChainOptimiser> opChainOptimisers = Collections.emptyList();
    given(opChainValidator.validate(any(), any(), any())).willReturn(new ValidationResult());
    final OperationChainHandler<Set<?>> opChainHandler = new OperationChainHandler<>(opChainValidator, opChainOptimisers);
    given(store.handleOperation(map, context)).willReturn(Arrays.asList(EDGE_BC, EDGE_BD));
    given(store.handleOperation(toVertices, context)).willReturn(Arrays.asList("B", "B"));
    given(store.handleOperation(toSet, context)).willReturn(Sets.newHashSet("B", "B"));
    // When
    final Iterable<?> results = opChainHandler.doOperation(opChain, context, store);
    // Then
    assertThat((Iterable<String>) results).contains("B");
}
Also used : Walk(uk.gov.gchq.gaffer.data.graph.Walk) ToSet(uk.gov.gchq.gaffer.operation.impl.output.ToSet) Set(java.util.Set) ToVertices(uk.gov.gchq.gaffer.operation.impl.output.ToVertices) OperationChainValidator(uk.gov.gchq.gaffer.store.operation.OperationChainValidator) ValidationResult(uk.gov.gchq.koryphe.ValidationResult) OperationChainOptimiser(uk.gov.gchq.gaffer.store.optimiser.OperationChainOptimiser) ExtractWalkEdgesFromHop(uk.gov.gchq.gaffer.data.graph.function.walk.ExtractWalkEdgesFromHop) ToSet(uk.gov.gchq.gaffer.operation.impl.output.ToSet) IterableFunction(uk.gov.gchq.koryphe.impl.function.IterableFunction) FirstItem(uk.gov.gchq.koryphe.impl.function.FirstItem) Map(uk.gov.gchq.gaffer.operation.impl.Map) Test(org.junit.jupiter.api.Test)

Aggregations

ToSet (uk.gov.gchq.gaffer.operation.impl.output.ToSet)6 Test (org.junit.jupiter.api.Test)4 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)4 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)3 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)3 Map (uk.gov.gchq.gaffer.operation.impl.Map)3 ToVertices (uk.gov.gchq.gaffer.operation.impl.output.ToVertices)3 LinkedList (java.util.LinkedList)2 Set (java.util.Set)2 ElementPropertyComparator (uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator)2 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)2 ElementTransformer (uk.gov.gchq.gaffer.data.element.function.ElementTransformer)2 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)2 CsvGenerator (uk.gov.gchq.gaffer.data.generator.CsvGenerator)2 Operation (uk.gov.gchq.gaffer.operation.Operation)2 Sort (uk.gov.gchq.gaffer.operation.impl.compare.Sort)2 GetAdjacentIds (uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds)2 Conditional (uk.gov.gchq.gaffer.operation.util.Conditional)2 OperationChainValidator (uk.gov.gchq.gaffer.store.operation.OperationChainValidator)2 OperationChainOptimiser (uk.gov.gchq.gaffer.store.optimiser.OperationChainOptimiser)2