Search in sources :

Example 1 with ViewParameterDetail

use of uk.gov.gchq.gaffer.data.elementdefinition.view.ViewParameterDetail in project gaffer-doc by gchq.

the class AddNamedViewExample method addNamedViewWithParameter.

public void addNamedViewWithParameter() {
    // ---------------------------------------------------------
    final String viewJson = "{\"edges\" : {\n" + "  \"testEdge\" : {\n" + "    \"preAggregationFilterFunctions\" : [ {\n" + "      \"selection\" : [ \"count\" ],\n" + "      \"predicate\" : {\n" + "        \"class\" : \"uk.gov.gchq.koryphe.impl.predicate.IsMoreThan\",\n" + "        \"orEqualTo\" : false,\n" + "        \"value\" : \"${countThreshold}\"\n" + "      }\n" + "    } ]\n" + "  }\n" + "}}";
    final ViewParameterDetail param = new ViewParameterDetail.Builder().defaultValue(1L).description("count threshold").valueClass(Long.class).build();
    final Map<String, ViewParameterDetail> paramMap = Maps.newHashMap();
    paramMap.put("countThreshold", param);
    final AddNamedView op = new AddNamedView.Builder().name("isMoreThan").description("example test NamedView").overwrite(true).view(viewJson).parameters(paramMap).writeAccessRoles("auth1").build();
    // ---------------------------------------------------------
    runExampleNoResult(op, null);
    try {
        getGraph().execute(new DeleteNamedView.Builder().name("isMoreThan").build(), createContext());
    } catch (final OperationException e) {
        throw new RuntimeException("Unable to delete named view: isMoreThan", e);
    }
}
Also used : ViewParameterDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewParameterDetail) DeleteNamedView(uk.gov.gchq.gaffer.named.view.DeleteNamedView) AddNamedView(uk.gov.gchq.gaffer.named.view.AddNamedView) OperationException(uk.gov.gchq.gaffer.operation.OperationException)

Example 2 with ViewParameterDetail

use of uk.gov.gchq.gaffer.data.elementdefinition.view.ViewParameterDetail in project Gaffer by gchq.

the class NamedViewResolverTest method shouldResolveNamedViewWithParameter.

@Test
public void shouldResolveNamedViewWithParameter() throws CacheOperationFailedException {
    Map<String, Object> paramMap = Maps.newHashMap();
    paramMap.put(EDGE_NAME_PARAM_KEY, TestGroups.EDGE_2);
    ViewParameterDetail param = new ViewParameterDetail.Builder().defaultValue(TestGroups.EDGE).description("edge name param").valueClass(String.class).build();
    Map<String, ViewParameterDetail> paramDetailMap = Maps.newHashMap();
    paramDetailMap.put(EDGE_NAME_PARAM_KEY, param);
    // Make a real View with a parameter
    final View extendedView = new View.Builder().edge("${" + EDGE_NAME_PARAM_KEY + "}", new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select(IdentifierType.VERTEX.name()).execute(new ExampleFilterFunction()).build()).build()).build();
    final NamedViewDetail extendedNamedViewDetail = new NamedViewDetail.Builder().name(NAMED_VIEW_NAME).view(extendedView).parameters(paramDetailMap).build();
    given(CACHE.getNamedView(NAMED_VIEW_NAME, CONTEXT.getUser())).willReturn(extendedNamedViewDetail);
    final OperationChain<?> opChain = new OperationChain.Builder().first(new GetElements.Builder().view(new NamedView.Builder().name(NAMED_VIEW_NAME).build()).build()).build();
    // When
    RESOLVER.preExecute(opChain, CONTEXT);
    GetElements getElements = (GetElements) opChain.getOperations().get(0);
    // Then
    assertTrue(getElements.getView().getEdge(TestGroups.EDGE) != null);
    assertTrue(getElements.getView().getEdge(TestGroups.EDGE_2) == null);
    final OperationChain<?> opChain1 = new OperationChain.Builder().first(new GetElements.Builder().view(new NamedView.Builder().name(NAMED_VIEW_NAME).parameters(paramMap).build()).build()).build();
    // When
    RESOLVER.preExecute(opChain1, CONTEXT);
    GetElements getElements1 = (GetElements) opChain1.getOperations().get(0);
    // Then
    assertTrue(getElements1.getView().getEdge(TestGroups.EDGE_2) != null);
}
Also used : ViewParameterDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewParameterDetail) NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) OperationView(uk.gov.gchq.gaffer.operation.graph.OperationView) NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) NamedViewDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ExampleFilterFunction(uk.gov.gchq.gaffer.function.ExampleFilterFunction) Test(org.junit.jupiter.api.Test)

Example 3 with ViewParameterDetail

use of uk.gov.gchq.gaffer.data.elementdefinition.view.ViewParameterDetail in project Gaffer by gchq.

the class NamedViewResolverTest method shouldResolveNamedViewWithParametersToMakeCompleteFilter.

@Test
public void shouldResolveNamedViewWithParametersToMakeCompleteFilter() throws CacheOperationFailedException {
    Map<String, Object> paramMap = Maps.newHashMap();
    paramMap.put(IS_MORE_THAN_X_PARAM_KEY, 7L);
    ViewParameterDetail param = new ViewParameterDetail.Builder().defaultValue(2L).description("more than filter").valueClass(Long.class).build();
    Map<String, ViewParameterDetail> paramDetailMap = Maps.newHashMap();
    paramDetailMap.put(IS_MORE_THAN_X_PARAM_KEY, param);
    // Make a real View with a parameter
    final View extendedView = new View.Builder().edge(TestGroups.EDGE, new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select(IdentifierType.VERTEX.name()).execute(new IsMoreThan("${" + IS_MORE_THAN_X_PARAM_KEY + "}")).build()).build()).build();
    final NamedViewDetail extendedNamedViewDetail = new NamedViewDetail.Builder().name(NAMED_VIEW_NAME).view(extendedView).parameters(paramDetailMap).build();
    given(CACHE.getNamedView(NAMED_VIEW_NAME, CONTEXT.getUser())).willReturn(extendedNamedViewDetail);
    final OperationChain<?> opChain = new OperationChain.Builder().first(new GetElements.Builder().view(new NamedView.Builder().name(NAMED_VIEW_NAME).build()).build()).build();
    // When
    RESOLVER.preExecute(opChain, CONTEXT);
    GetElements getElements = (GetElements) opChain.getOperations().get(0);
    // Then
    assertTrue(new String(getElements.getView().toCompactJson()).contains(VALUE_JSON_STRING + 2));
    final OperationChain<?> opChain1 = new OperationChain.Builder().first(new GetElements.Builder().view(new NamedView.Builder().name(NAMED_VIEW_NAME).parameters(paramMap).build()).build()).build();
    // When
    RESOLVER.preExecute(opChain1, CONTEXT);
    GetElements getElements1 = (GetElements) opChain1.getOperations().get(0);
    // Then
    assertTrue(new String(getElements1.getView().toCompactJson()).contains(VALUE_JSON_STRING + 7));
}
Also used : ViewParameterDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewParameterDetail) NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) OperationView(uk.gov.gchq.gaffer.operation.graph.OperationView) NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) NamedViewDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) Test(org.junit.jupiter.api.Test)

Example 4 with ViewParameterDetail

use of uk.gov.gchq.gaffer.data.elementdefinition.view.ViewParameterDetail 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 5 with ViewParameterDetail

use of uk.gov.gchq.gaffer.data.elementdefinition.view.ViewParameterDetail in project Gaffer by gchq.

the class NamedViewResolverTest method shouldResolveNamedViewJsonWithParameters.

@Test
public void shouldResolveNamedViewJsonWithParameters() throws CacheOperationFailedException {
    Map<String, Object> paramMap = Maps.newHashMap();
    paramMap.put(IS_MORE_THAN_X_PARAM_KEY, 7L);
    paramMap.put(SELECTION_PARAM_KEY, "VERTEX");
    ViewParameterDetail param = new ViewParameterDetail.Builder().defaultValue(2L).description("more than filter").valueClass(Long.class).build();
    ViewParameterDetail param2 = new ViewParameterDetail.Builder().description("selection filter").valueClass(String.class).required(true).build();
    Map<String, ViewParameterDetail> paramDetailMap = Maps.newHashMap();
    paramDetailMap.put(IS_MORE_THAN_X_PARAM_KEY, param);
    paramDetailMap.put(SELECTION_PARAM_KEY, param2);
    // real View json with a parameter
    final String extendedViewString = "{\n" + "  \"edges\" : {\n" + "    \"BasicEdge\" : {\n" + "      \"preAggregationFilterFunctions\" : [ {\n" + "        \"predicate\" : {\n" + "          \"class\" : \"uk.gov.gchq.koryphe.impl.predicate.IsMoreThan\",\n" + "          \"orEqualTo\" : false,\n" + "          \"value\" : \"${IS_MORE_THAN_X}\"\n" + "        },\n" + "        \"selection\" : [ \"${SELECTION}\" ]\n" + "      } ]\n" + "    }\n" + "  },\n" + "  \"entities\" : { }\n" + "}\n";
    final NamedViewDetail extendedNamedViewDetail = new NamedViewDetail.Builder().name(NAMED_VIEW_NAME).view(extendedViewString).parameters(paramDetailMap).build();
    given(CACHE.getNamedView(NAMED_VIEW_NAME, CONTEXT.getUser())).willReturn(extendedNamedViewDetail);
    final OperationChain<?> opChain = new OperationChain.Builder().first(new GetElements.Builder().view(new NamedView.Builder().name(NAMED_VIEW_NAME).parameters(paramMap).build()).build()).build();
    // When
    RESOLVER.preExecute(opChain, CONTEXT);
    GetElements getElements = (GetElements) opChain.getOperations().get(0);
    // Then
    assertTrue(new String(getElements.getView().toCompactJson()).contains(VALUE_JSON_STRING + 7));
    assertTrue(new String(getElements.getView().toCompactJson()).contains("VERTEX"));
}
Also used : ViewParameterDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewParameterDetail) NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) NamedViewDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail) Test(org.junit.jupiter.api.Test)

Aggregations

ViewParameterDetail (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewParameterDetail)5 NamedView (uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView)4 NamedViewDetail (uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail)4 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)4 Test (org.junit.jupiter.api.Test)3 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)3 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)2 AddNamedView (uk.gov.gchq.gaffer.named.view.AddNamedView)2 OperationView (uk.gov.gchq.gaffer.operation.graph.OperationView)2 RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator (uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator)1 ExampleFilterFunction (uk.gov.gchq.gaffer.function.ExampleFilterFunction)1 Graph (uk.gov.gchq.gaffer.graph.Graph)1 DeleteNamedView (uk.gov.gchq.gaffer.named.view.DeleteNamedView)1 GetAllNamedViews (uk.gov.gchq.gaffer.named.view.GetAllNamedViews)1 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)1 OperationException (uk.gov.gchq.gaffer.operation.OperationException)1 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)1 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)1 User (uk.gov.gchq.gaffer.user.User)1 IsMoreThan (uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)1