Search in sources :

Example 16 with NamedOperation

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

the class NamedOperationHandler method exposeNamedOperations.

private List<Operation> exposeNamedOperations(final OperationChain<?> opChain, final User user, final INamedOperationCache cache) throws CacheOperationFailedException {
    ArrayList<Operation> operations = new ArrayList<>();
    for (final Operation operation : opChain.getOperations()) {
        if (operation instanceof NamedOperation) {
            final NamedOperation namedOp = ((NamedOperation) operation);
            OperationChain<?> innerChain = cache.getNamedOperation(namedOp.getOperationName(), user).getOperationChain();
            updateOperationInput(innerChain.getOperations().get(0), namedOp.getSeeds());
            operations.addAll(exposeNamedOperations(innerChain, user, cache));
        } else {
            operations.add(operation);
        }
    }
    return operations;
}
Also used : ArrayList(java.util.ArrayList) ExtendedNamedOperation(uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Operation(uk.gov.gchq.gaffer.operation.Operation) ExtendedNamedOperation(uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation)

Example 17 with NamedOperation

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

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

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

the class ScoreOperationChainHandlerTest method shouldResolveScoreOperationChainWithMultipleScoreResolvers.

@Test
public void shouldResolveScoreOperationChainWithMultipleScoreResolvers() throws OperationException {
    // Given
    final ScoreOperationChainHandler handler = new ScoreOperationChainHandler();
    final Map<Class<? extends Operation>, ScoreResolver> resolvers = new HashMap<>();
    final ScoreResolver scoreResolver = mock(NamedOperationScoreResolver.class);
    final ScoreResolver scoreResolver1 = mock(DefaultScoreResolver.class);
    final Context context = mock(Context.class);
    final Store store = mock(Store.class);
    final User user = mock(User.class);
    final ScoreOperationChain scoreOperationChain = mock(ScoreOperationChain.class);
    final StoreProperties storeProperties = mock(StoreProperties.class);
    final GetAdjacentIds op1 = new GetAdjacentIds();
    final AddElements op2 = new AddElements();
    final Map<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
    opScores.put(GetAdjacentIds.class, 2);
    handler.setOpScores(opScores);
    final String opName = "namedOp";
    final NamedOperation<Iterable<? extends Element>, Iterable<? extends Element>> namedOp = mock(NamedOperation.class);
    namedOp.setOperationName(opName);
    resolvers.put(namedOp.getClass(), scoreResolver);
    resolvers.put(op2.getClass(), scoreResolver1);
    handler.setScoreResolvers(resolvers);
    given(scoreResolver.getScore(eq(namedOp), any())).willReturn(3);
    given(scoreResolver1.getScore(eq(op2), any())).willReturn(5);
    final OperationChain opChain = new OperationChain(Arrays.asList(op1, op2, namedOp));
    given(context.getUser()).willReturn(user);
    Set<String> opAuths = new HashSet<>();
    opAuths.add("TEST_USER");
    given(user.getOpAuths()).willReturn(opAuths);
    given(scoreOperationChain.getOperationChain()).willReturn(opChain);
    given(store.getProperties()).willReturn(storeProperties);
    // When
    final Object result = handler.doOperation(new ScoreOperationChain.Builder().operationChain(opChain).build(), context, store);
    // Then
    assertEquals(10, result);
}
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) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Element(uk.gov.gchq.gaffer.data.element.Element) Store(uk.gov.gchq.gaffer.store.Store) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Operation(uk.gov.gchq.gaffer.operation.Operation) ScoreOperationChain(uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain) LinkedHashMap(java.util.LinkedHashMap) NamedOperationScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.named.NamedOperationScoreResolver) DefaultScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.DefaultScoreResolver) ScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.ScoreResolver) IfScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.IfScoreResolver) HashSet(java.util.HashSet) Context(uk.gov.gchq.gaffer.store.Context) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) ScoreOperationChain(uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) Test(org.junit.jupiter.api.Test)

Example 20 with NamedOperation

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

the class NamedOperationResolver method resolveNamedOperations.

private void resolveNamedOperations(final Operations<?> operations, final User user) {
    final List<Operation> updatedOperations = new ArrayList<>(operations.getOperations().size());
    for (final Operation operation : operations.getOperations()) {
        if (operation instanceof NamedOperation) {
            updatedOperations.addAll(resolveNamedOperation((NamedOperation) operation, user));
        } else {
            if (operation instanceof Operations) {
                resolveNamedOperations(((Operations<?>) operation), user);
            }
            updatedOperations.add(operation);
        }
    }
    operations.updateOperations((List) updatedOperations);
}
Also used : ArrayList(java.util.ArrayList) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Operation(uk.gov.gchq.gaffer.operation.Operation) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Operations(uk.gov.gchq.gaffer.operation.Operations)

Aggregations

NamedOperation (uk.gov.gchq.gaffer.named.operation.NamedOperation)25 Operation (uk.gov.gchq.gaffer.operation.Operation)14 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)12 ExtendedNamedOperation (uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation)10 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)10 Test (org.junit.jupiter.api.Test)9 Element (uk.gov.gchq.gaffer.data.element.Element)9 LinkedHashMap (java.util.LinkedHashMap)8 Test (org.junit.Test)8 AddNamedOperation (uk.gov.gchq.gaffer.named.operation.AddNamedOperation)8 HashMap (java.util.HashMap)7 NamedOperationScoreResolver (uk.gov.gchq.gaffer.store.operation.resolver.named.NamedOperationScoreResolver)7 HashSet (java.util.HashSet)5 Limit (uk.gov.gchq.gaffer.operation.impl.Limit)5 ScoreOperationChain (uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain)5 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)5 GetAdjacentIds (uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds)5 DefaultScoreResolver (uk.gov.gchq.gaffer.store.operation.resolver.DefaultScoreResolver)5 ScoreResolver (uk.gov.gchq.gaffer.store.operation.resolver.ScoreResolver)5 User (uk.gov.gchq.gaffer.user.User)5