Search in sources :

Example 6 with GetAllNamedOperations

use of uk.gov.gchq.gaffer.named.operation.GetAllNamedOperations 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)

Example 7 with GetAllNamedOperations

use of uk.gov.gchq.gaffer.named.operation.GetAllNamedOperations in project Gaffer by gchq.

the class GetAllNamedOperationsHandlerTest method shouldReturnNamedOperationWithInputType.

@Test
public void shouldReturnNamedOperationWithInputType() throws Exception {
    // Given
    AddNamedOperation addNamedOperation = new AddNamedOperation.Builder().name(expectedOperationDetailWithInputType.getOperationName()).description(expectedOperationDetailWithInputType.getDescription()).operationChain(expectedOperationDetailWithInputType.getOperationChainWithDefaultParams()).build();
    addNamedOperationHandler.doOperation(addNamedOperation, context, store);
    // When
    CloseableIterable<NamedOperationDetail> allNamedOperationsList = getAllNamedOperationsHandler.doOperation(new GetAllNamedOperations(), context, store);
    // Then
    assertEquals(1, Iterables.size(allNamedOperationsList));
    assertTrue(Iterables.contains(allNamedOperationsList, expectedOperationDetailWithInputType));
}
Also used : GetAllNamedOperations(uk.gov.gchq.gaffer.named.operation.GetAllNamedOperations) AddNamedOperation(uk.gov.gchq.gaffer.named.operation.AddNamedOperation) NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) Test(org.junit.jupiter.api.Test)

Example 8 with GetAllNamedOperations

use of uk.gov.gchq.gaffer.named.operation.GetAllNamedOperations in project Gaffer by gchq.

the class GetAllNamedOperationsHandlerTest method shouldReturnNullLabelWhenLabelIsNullFromAddNamedOperationRequest.

@Test
public void shouldReturnNullLabelWhenLabelIsNullFromAddNamedOperationRequest() throws Exception {
    final AddNamedOperation addNamedOperationWithNullLabel = new AddNamedOperation.Builder().name("My Operation With Label").labels(null).operationChain("{\"operations\":[{\"class\":\"uk.gov.gchq.gaffer.operation.impl.add.AddElements\",\"skipInvalidElements\":false,\"validate\":true}]}").build();
    addNamedOperationHandler.doOperation(addNamedOperationWithNullLabel, context, store);
    final CloseableIterable<NamedOperationDetail> allNamedOperations = getAllNamedOperationsHandler.doOperation(new GetAllNamedOperations(), context, store);
    assertThat(allNamedOperations.iterator().next().getLabels()).isNull();
}
Also used : GetAllNamedOperations(uk.gov.gchq.gaffer.named.operation.GetAllNamedOperations) AddNamedOperation(uk.gov.gchq.gaffer.named.operation.AddNamedOperation) NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) Test(org.junit.jupiter.api.Test)

Example 9 with GetAllNamedOperations

use of uk.gov.gchq.gaffer.named.operation.GetAllNamedOperations in project Gaffer by gchq.

the class GetAllNamedOperationsHandlerTest method shouldReturnLabelWhenNamedOperationHasLabel.

@Test
public void shouldReturnLabelWhenNamedOperationHasLabel() throws Exception {
    final AddNamedOperation addNamedOperationWithLabel = new AddNamedOperation.Builder().name("My Operation With Label").labels(Arrays.asList("test label")).operationChain("{\"operations\":[{\"class\":\"uk.gov.gchq.gaffer.operation.impl.add.AddElements\",\"skipInvalidElements\":false,\"validate\":true}]}").build();
    addNamedOperationHandler.doOperation(addNamedOperationWithLabel, context, store);
    final CloseableIterable<NamedOperationDetail> allNamedOperations = getAllNamedOperationsHandler.doOperation(new GetAllNamedOperations(), context, store);
    assertEquals(Arrays.asList("test label"), allNamedOperations.iterator().next().getLabels());
}
Also used : GetAllNamedOperations(uk.gov.gchq.gaffer.named.operation.GetAllNamedOperations) AddNamedOperation(uk.gov.gchq.gaffer.named.operation.AddNamedOperation) NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) Test(org.junit.jupiter.api.Test)

Example 10 with GetAllNamedOperations

use of uk.gov.gchq.gaffer.named.operation.GetAllNamedOperations in project Gaffer by gchq.

the class NamedOperationCacheIT method shouldBeAbleToDeleteNamedOperationFromCache.

private void shouldBeAbleToDeleteNamedOperationFromCache() throws OperationException {
    // given
    final Store store = mock(Store.class);
    given(store.getProperties()).willReturn(properties);
    new AddNamedOperationHandler().doOperation(add, context, store);
    DeleteNamedOperation del = new DeleteNamedOperation.Builder().name("op").build();
    GetAllNamedOperations get = new GetAllNamedOperations();
    // when
    deleteNamedOperationHandler.doOperation(del, context, store);
    List<NamedOperationDetail> results = Lists.newArrayList(getAllNamedOperationsHandler1.doOperation(get, context, store));
    // then
    assertThat(results).isEmpty();
}
Also used : GetAllNamedOperations(uk.gov.gchq.gaffer.named.operation.GetAllNamedOperations) Store(uk.gov.gchq.gaffer.store.Store) NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) AddNamedOperationHandler(uk.gov.gchq.gaffer.store.operation.handler.named.AddNamedOperationHandler) DeleteNamedOperation(uk.gov.gchq.gaffer.named.operation.DeleteNamedOperation)

Aggregations

GetAllNamedOperations (uk.gov.gchq.gaffer.named.operation.GetAllNamedOperations)10 NamedOperationDetail (uk.gov.gchq.gaffer.named.operation.NamedOperationDetail)10 AddNamedOperation (uk.gov.gchq.gaffer.named.operation.AddNamedOperation)8 Test (org.junit.jupiter.api.Test)5 Store (uk.gov.gchq.gaffer.store.Store)4 AddNamedOperationHandler (uk.gov.gchq.gaffer.store.operation.handler.named.AddNamedOperationHandler)3 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Response (javax.ws.rs.core.Response)1 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)1 Element (uk.gov.gchq.gaffer.data.element.Element)1 EntityId (uk.gov.gchq.gaffer.data.element.id.EntityId)1 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)1 RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator (uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator)1 Graph (uk.gov.gchq.gaffer.graph.Graph)1 DeleteNamedOperation (uk.gov.gchq.gaffer.named.operation.DeleteNamedOperation)1 NamedOperation (uk.gov.gchq.gaffer.named.operation.NamedOperation)1 ParameterDetail (uk.gov.gchq.gaffer.named.operation.ParameterDetail)1 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)1 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)1