Search in sources :

Example 16 with AddNamedOperation

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

the class GraphHooksIT method shouldResolveNamedViewWithinNamedOperation.

@Test
public void shouldResolveNamedViewWithinNamedOperation() throws OperationException {
    // Given
    final Edge edge1 = getEdges().get(new EdgeSeed(SOURCE_1, DEST_1, false)).emptyClone();
    edge1.putProperty(TestPropertyNames.INT, 100);
    final Edge edge2 = edge1.emptyClone();
    edge2.putProperty(TestPropertyNames.INT, 101);
    graph.execute(new AddElements.Builder().input(edge1, edge2).build(), getUser());
    final AddNamedView addNamedView = new AddNamedView.Builder().name("Test View").view(new View.Builder().edge(TestGroups.EDGE, new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select(TestPropertyNames.INT).execute(new IsIn(Arrays.asList((Object) 100))).build()).build()).build()).overwrite(true).build();
    graph.execute(addNamedView, getUser());
    final AddNamedOperation addNamedOperation = new AddNamedOperation.Builder().operationChain(new OperationChain.Builder().first(new GetAllElements.Builder().view(new NamedView.Builder().name("Test View").build()).build()).build()).description("named operation GetAllElements test query").name("GetAllElements test").labels(Arrays.asList("label 1", "Label 2")).overwrite(true).build();
    graph.execute(addNamedOperation, getUser());
    final NamedOperation<EntityId, CloseableIterable<? extends Element>> operation = new NamedOperation.Builder<EntityId, CloseableIterable<? extends Element>>().name("GetAllElements test").input(new EntitySeed("10")).build();
    // When
    final CloseableIterable<? extends Element> results = graph.execute(operation, getUser());
    // Then
    final List<Element> resultList = Lists.newArrayList(results);
    assertThat(resultList).hasSize(1).contains((Element) edge1);
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) Element(uk.gov.gchq.gaffer.data.element.Element) AddNamedView(uk.gov.gchq.gaffer.named.view.AddNamedView) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) AddNamedOperation(uk.gov.gchq.gaffer.named.operation.AddNamedOperation) 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) IsIn(uk.gov.gchq.koryphe.impl.predicate.IsIn) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) EdgeSeed(uk.gov.gchq.gaffer.operation.data.EdgeSeed) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Edge(uk.gov.gchq.gaffer.data.element.Edge) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) AddNamedOperation(uk.gov.gchq.gaffer.named.operation.AddNamedOperation) Test(org.junit.Test)

Example 17 with AddNamedOperation

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

the class AddNamedOperationHandlerTest method shouldNotAllowUpdateToNamedOperationIfItCausesRecursion.

@Test
public void shouldNotAllowUpdateToNamedOperationIfItCausesRecursion() throws OperationException {
    String innocentOpName = "innocent";
    OperationChain innocent = new OperationChain.Builder().first(new GetElements<>()).build();
    addNamedOperation.setOperationName(innocentOpName);
    addNamedOperation.setOperationChain(innocent);
    handler.doOperation(addNamedOperation, context, store);
    OperationChain parent = new OperationChain.Builder().first(new NamedOperation(innocentOpName, "call down to currently innocent named op")).build();
    addNamedOperation.setOperationChain(parent);
    addNamedOperation.setOperationName(OPERATION_NAME);
    handler.doOperation(addNamedOperation, context, store);
    OperationChain recursive = new OperationChain.Builder().first(new NamedOperation(OPERATION_NAME, "")).build();
    addNamedOperation.setOperationName(innocentOpName);
    addNamedOperation.setOperationChain(recursive);
    addNamedOperation.setOverwriteFlag(true);
    exception.expect(OperationException.class);
    handler.doOperation(addNamedOperation, context, store);
}
Also used : OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) AddNamedOperation(uk.gov.gchq.gaffer.named.operation.AddNamedOperation) ExtendedNamedOperation(uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Test(org.junit.Test)

Example 18 with AddNamedOperation

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

the class AddNamedOperationHandlerTest method shouldNotAddNamedOperationIfItContainsAnOperationWhichReferencesTheParent.

@Test
public void shouldNotAddNamedOperationIfItContainsAnOperationWhichReferencesTheParent() throws CacheOperationFailedException, OperationException {
    NamedOperation op = new NamedOperation("parent", "this is the parent which has not yet been created");
    OperationChain opChain = new OperationChain.Builder().first(op).build();
    ExtendedNamedOperation child = new ExtendedNamedOperation.Builder().operationChain(opChain).operationName(OPERATION_NAME).build();
    handler.getCache().addNamedOperation(child, false, context.getUser());
    OperationChain parentOpChain = new OperationChain.Builder().first(operation).build();
    addNamedOperation.setOperationName("parent");
    addNamedOperation.setOperationChain(parentOpChain);
    exception.expect(OperationException.class);
    handler.doOperation(addNamedOperation, context, store);
}
Also used : OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) ExtendedNamedOperation(uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation) AddNamedOperation(uk.gov.gchq.gaffer.named.operation.AddNamedOperation) ExtendedNamedOperation(uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Test(org.junit.Test)

Example 19 with AddNamedOperation

use of uk.gov.gchq.gaffer.named.operation.AddNamedOperation 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 20 with AddNamedOperation

use of uk.gov.gchq.gaffer.named.operation.AddNamedOperation in project gaffer-doc by gchq.

the class NamedOperationExample method addNamedOperationWithScore.

public void addNamedOperationWithScore() {
    // ---------------------------------------------------------
    final AddNamedOperation operation = new AddNamedOperation.Builder().operationChain(new OperationChain.Builder().first(new GetAdjacentIds.Builder().inOutType(SeededGraphFilters.IncludeIncomingOutgoingType.OUTGOING).build()).build()).description("1 hop query").name("1-hop").readAccessRoles("read-user").writeAccessRoles("write-user").overwrite().score(2).build();
    // ---------------------------------------------------------
    runExampleNoResult(operation, null);
}
Also used : AddNamedOperation(uk.gov.gchq.gaffer.named.operation.AddNamedOperation)

Aggregations

AddNamedOperation (uk.gov.gchq.gaffer.named.operation.AddNamedOperation)25 Test (org.junit.jupiter.api.Test)10 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)10 NamedOperationDetail (uk.gov.gchq.gaffer.named.operation.NamedOperationDetail)9 GetAllNamedOperations (uk.gov.gchq.gaffer.named.operation.GetAllNamedOperations)8 NamedOperation (uk.gov.gchq.gaffer.named.operation.NamedOperation)7 Test (org.junit.Test)5 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)5 Context (uk.gov.gchq.gaffer.store.Context)5 User (uk.gov.gchq.gaffer.user.User)5 ExtendedNamedOperation (uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation)4 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)4 UnauthorisedException (uk.gov.gchq.gaffer.commonutil.exception.UnauthorisedException)3 ParameterDetail (uk.gov.gchq.gaffer.named.operation.ParameterDetail)3 OperationException (uk.gov.gchq.gaffer.operation.OperationException)3 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)2 Element (uk.gov.gchq.gaffer.data.element.Element)2 EntityId (uk.gov.gchq.gaffer.data.element.id.EntityId)2 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)2 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)2