Search in sources :

Example 6 with RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator

use of uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator in project gaffer-doc by gchq.

the class NamedViews method run.

@Override
public CloseableIterable<? extends Element> 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");
    // ---------------------------------------------------------
    // [add] Create a data generator and add the edges to the graph using an operation chain consisting of:
    // generateElements - generating edges from the data (note these are directed edges)
    // addElements - add the edges to the graph
    // ---------------------------------------------------------
    final OperationChain<Void> addOpChain = new OperationChain.Builder().first(new GenerateElements.Builder<String>().generator(new RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator()).input(IOUtils.readLines(StreamUtil.openStream(getClass(), "RoadAndRoadUseWithTimesAndCardinalities/data.txt"))).build()).then(new AddElements()).build();
    graph.execute(addOpChain, user);
    // ---------------------------------------------------------
    // [add named view] add a NamedView to be reused
    // ---------------------------------------------------------
    final AddNamedView addNamedView = new AddNamedView.Builder().name("RoadUse edges").description("NamedView to get only RoadUse edges").view(new View.Builder().edge("RoadUse").build()).overwrite(false).build();
    // ---------------------------------------------------------
    printJsonAndPython("ADD_NAMED_VIEW", addNamedView);
    graph.execute(addNamedView, user);
    // [get all named views] Get all named views
    // ---------------------------------------------------------
    final GetAllNamedViews getAllNamedViews = new GetAllNamedViews();
    // ---------------------------------------------------------
    printJsonAndPython("GET_ALL_NAMED_VIEWS", new GetAllNamedViews());
    final CloseableIterable<NamedViewDetail> namedViewDetails = graph.execute(getAllNamedViews, user);
    for (final NamedViewDetail detail : namedViewDetails) {
        print("ALL_NAMED_VIEW", detail.toString());
    }
    // [get elements with named view] create the operation using the NamedView
    // ---------------------------------------------------------
    final GetElements operation = new GetElements.Builder().view(new NamedView.Builder().name("RoadUse edges").build()).input(new EntitySeed("10")).build();
    // ---------------------------------------------------------
    printJsonAndPython("GET_ELEMENTS_WITH_NAMED_VIEW", operation);
    final CloseableIterable<? extends Element> namedViewResults = graph.execute(operation, user);
    for (final Object result : namedViewResults) {
        print("GET_ELEMENTS_WITH_NAMED_VIEW_RESULTS", result.toString());
    }
    // [add named view with parameters] create an operation chain to be executed as a named operation
    // with parameters
    // ---------------------------------------------------------
    final String viewJson = "{" + "      \"edges\" : {" + "        \"RoadUse\" : {  " + "           \"preAggregationFilterFunctions\" : [ {" + "               \"predicate\" : {" + "                   \"class\" : \"uk.gov.gchq.koryphe.impl.predicate.IsMoreThan\"," + "                   \"orEqualTo\" : false," + "                     \"value\": {" + "                         \"java.lang.Long\" : \"${isMoreThan}\"" + "                       }" + "                 }," + "             \"selection\" : [ \"${property}\" ]" + "         } ] }" + "      }," + "      \"entities\" : { }" + "}";
    final ViewParameterDetail propertyParam = new ViewParameterDetail.Builder().description("Property to select").valueClass(String.class).required(true).build();
    final ViewParameterDetail valueParam = new ViewParameterDetail.Builder().description("Value for the is more than predicate").defaultValue(0L).valueClass(Long.class).build();
    final Map<String, ViewParameterDetail> paramDetailMap = Maps.newHashMap();
    paramDetailMap.put("property", propertyParam);
    paramDetailMap.put("isMoreThan", valueParam);
    final AddNamedView addNamedViewWithParams = new AddNamedView.Builder().name("customCountView").description("named View with count param").view(viewJson).parameters(paramDetailMap).overwrite(true).build();
    // ---------------------------------------------------------
    printJsonAndPython("ADD_NAMED_VIEW_WITH_PARAMETERS", addNamedViewWithParams);
    graph.execute(addNamedViewWithParams, user);
    // [get elements with named view with parameters] create the named operation with a parameter
    // ---------------------------------------------------------
    final Map<String, Object> paramMap = Maps.newHashMap();
    paramMap.put("property", "count");
    paramMap.put("isMoreThan", 1L);
    final GetElements operationUsingParams = new GetElements.Builder().view(new NamedView.Builder().name("customCountView").parameters(paramMap).build()).input(new EntitySeed("10")).build();
    // ---------------------------------------------------------
    printJsonAndPython("GET_ELEMENTS_WITH_NAMED_VIEW_WITH_PARAMETERS", operationUsingParams);
    final CloseableIterable<? extends Element> namedViewResultsWithParams = graph.execute(operationUsingParams, user);
    for (final Object result : namedViewResultsWithParams) {
        print("GET_ELEMENTS_WITH_NAMED_VIEW_WITH_PARAMETERS_RESULTS", result.toString());
    }
    return namedViewResultsWithParams;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) NamedViewDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail) RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator) ViewParameterDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewParameterDetail) NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) AddNamedView(uk.gov.gchq.gaffer.named.view.AddNamedView) AddNamedView(uk.gov.gchq.gaffer.named.view.AddNamedView) GetAllNamedViews(uk.gov.gchq.gaffer.named.view.GetAllNamedViews) NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) AddNamedView(uk.gov.gchq.gaffer.named.view.AddNamedView) Graph(uk.gov.gchq.gaffer.graph.Graph) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed)

Example 7 with RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator

use of uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator in project gaffer-doc by gchq.

the class NamedOperations method run.

public CloseableIterable<? extends Element> 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");
    // ---------------------------------------------------------
    // [add] Create a data generator and add the edges to the graph using an operation chain consisting of:
    // generateElements - generating edges from the data (note these are directed edges)
    // addElements - add the edges to the graph
    // ---------------------------------------------------------
    final OperationChain<Void> addOpChain = new OperationChain.Builder().first(new GenerateElements.Builder<String>().generator(new RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator()).input(IOUtils.readLines(StreamUtil.openStream(getClass(), "RoadAndRoadUseWithTimesAndCardinalities/data.txt"))).build()).then(new AddElements()).build();
    graph.execute(addOpChain, user);
    // ---------------------------------------------------------
    // [add named operation] create an operation chain to be executed as a named operation
    // ---------------------------------------------------------
    final AddNamedOperation addOperation = new AddNamedOperation.Builder().operationChain(new OperationChain.Builder().first(new GetElements.Builder().view(new View.Builder().edge("RoadUse").build()).build()).then(new Limit.Builder<>().resultLimit(10).build()).build()).description("named operation limit query").name("2-limit").readAccessRoles("read-user").writeAccessRoles("write-user").score(2).overwrite().build();
    graph.execute(addOperation, user);
    // ---------------------------------------------------------
    // [get all named operations] Get all named operations
    // ---------------------------------------------------------
    final CloseableIterable<NamedOperationDetail> details = graph.execute(new GetAllNamedOperations(), user);
    // ---------------------------------------------------------
    for (final NamedOperationDetail detail : details) {
        print("ALL_NAMED_OPERATIONS", detail.toString());
    }
    // [create named operation] create the named operation
    // ---------------------------------------------------------
    final NamedOperation<EntityId, CloseableIterable<? extends Element>> operation = new NamedOperation.Builder<EntityId, CloseableIterable<? extends Element>>().name("2-limit").input(new EntitySeed("10")).build();
    // ---------------------------------------------------------
    // [execute named operation] Get the results
    // ---------------------------------------------------------
    final CloseableIterable<? extends Element> results = graph.execute(operation, user);
    // ---------------------------------------------------------
    for (final Object result : results) {
        print("NAMED_OPERATION_RESULTS", result.toString());
    }
    // [add named operation with parameters] create an operation chain to be executed as a named operation
    // with parameters
    // ---------------------------------------------------------
    String opChainString = "{" + "  \"operations\" : [ {" + "    \"class\" : \"uk.gov.gchq.gaffer.operation.impl.get.GetElements\"," + "    \"view\" : {" + "      \"edges\" : {" + "        \"RoadUse\" : { }" + "      }," + "      \"entities\" : { }" + "    }" + "  }, {" + "    \"class\" : \"uk.gov.gchq.gaffer.operation.impl.Limit\"," + "    \"resultLimit\" : \"${limitParam}\"" + "  } ]" + "}";
    ParameterDetail param = new ParameterDetail.Builder().defaultValue(1L).description("Limit param").valueClass(Long.class).build();
    Map<String, ParameterDetail> paramDetailMap = Maps.newHashMap();
    paramDetailMap.put("limitParam", param);
    final AddNamedOperation addOperationWithParams = new AddNamedOperation.Builder().operationChain(opChainString).description("named operation limit query").name("custom-limit").readAccessRoles("read-user").writeAccessRoles("write-user").parameters(paramDetailMap).overwrite().build();
    graph.execute(addOperationWithParams, user);
    // ---------------------------------------------------------
    // [create named operation with parameters] create the named operation with a parameter
    // ---------------------------------------------------------
    Map<String, Object> paramMap = Maps.newHashMap();
    paramMap.put("limitParam", 3L);
    final NamedOperation<EntityId, CloseableIterable<? extends Element>> operationWithParams = new NamedOperation.Builder<EntityId, CloseableIterable<? extends Element>>().name("custom-limit").input(new EntitySeed("10")).parameters(paramMap).build();
    // ---------------------------------------------------------
    // [execute named operation with parameters] Get the results
    // ---------------------------------------------------------
    final CloseableIterable<? extends Element> namedOperationResults = graph.execute(operationWithParams, user);
    for (final Object result : namedOperationResults) {
        print("NAMED_OPERATION_WITH_PARAMETER_RESULTS", result.toString());
    }
    return namedOperationResults;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) Element(uk.gov.gchq.gaffer.data.element.Element) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator) GetAllNamedOperations(uk.gov.gchq.gaffer.named.operation.GetAllNamedOperations) NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) AddNamedOperation(uk.gov.gchq.gaffer.named.operation.AddNamedOperation) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) AddNamedOperation(uk.gov.gchq.gaffer.named.operation.AddNamedOperation) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) ParameterDetail(uk.gov.gchq.gaffer.named.operation.ParameterDetail) Graph(uk.gov.gchq.gaffer.graph.Graph) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed)

Aggregations

RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator (uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator)7 Graph (uk.gov.gchq.gaffer.graph.Graph)7 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)7 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)7 User (uk.gov.gchq.gaffer.user.User)7 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)6 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)6 Element (uk.gov.gchq.gaffer.data.element.Element)5 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)5 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)4 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)3 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)2 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)2 HyperLogLogPlusIsLessThan (uk.gov.gchq.gaffer.sketches.clearspring.cardinality.predicate.HyperLogLogPlusIsLessThan)2 IsMoreThan (uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)2 HyperLogLogPlus (com.clearspring.analytics.stream.cardinality.HyperLogLogPlus)1 HashMapCacheService (uk.gov.gchq.gaffer.cache.impl.HashMapCacheService)1 Entity (uk.gov.gchq.gaffer.data.element.Entity)1 EntityId (uk.gov.gchq.gaffer.data.element.id.EntityId)1 GlobalViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.GlobalViewElementDefinition)1