Search in sources :

Example 21 with NamedOperation

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

the class DefaultScoreResolverTest method shouldGetScoreForOperationChainContainingNamedOperation.

@Test
public void shouldGetScoreForOperationChainContainingNamedOperation() throws OperationException {
    // Given
    final ScoreResolver mockResolver = mock(NamedOperationScoreResolver.class);
    final Map<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
    opScores.put(Operation.class, 1);
    opScores.put(GetAdjacentIds.class, 2);
    opScores.put(GetElements.class, 3);
    opScores.put(Limit.class, 4);
    final Map<Class<? extends Operation>, ScoreResolver> resolvers = new HashMap<>();
    final String opName = "basicOp";
    final NamedOperation<Object, Object> namedOp = new Builder<>().name(opName).build();
    resolvers.put(NamedOperation.class, mockResolver);
    given(mockResolver.getScore(eq(namedOp), any())).willReturn(5);
    final OperationChain opChain = new OperationChain.Builder().first(new GetAdjacentIds()).then(new GetElements()).then(new Limit()).then(new GetWalks.Builder().addOperations(namedOp, new GetElements()).build()).build();
    final DefaultScoreResolver scoreResolver = new DefaultScoreResolver(opScores, resolvers);
    // When
    final Object result = scoreResolver.getScore(opChain);
    // Then
    assertEquals(17, result);
}
Also used : GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Builder(uk.gov.gchq.gaffer.named.operation.NamedOperation.Builder) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Operation(uk.gov.gchq.gaffer.operation.Operation) LinkedHashMap(java.util.LinkedHashMap) NamedOperationScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.named.NamedOperationScoreResolver) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) Limit(uk.gov.gchq.gaffer.operation.impl.Limit) Test(org.junit.jupiter.api.Test)

Example 22 with NamedOperation

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

the class DefaultScoreResolverTest method shouldGetScoreForOperationChainWhenNamedOperationScoreIsNull.

@Test
public void shouldGetScoreForOperationChainWhenNamedOperationScoreIsNull() throws OperationException {
    // Given
    final Map<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
    opScores.put(Operation.class, 1);
    opScores.put(GetAdjacentIds.class, 2);
    opScores.put(GetElements.class, 1);
    opScores.put(Limit.class, 1);
    final Map<Class<? extends Operation>, ScoreResolver> resolvers = new HashMap<>();
    final String opName = "basicOp";
    final NamedOperation<Object, Object> namedOp = new Builder<>().name(opName).build();
    final ScoreResolver mockResolver = mock(NamedOperationScoreResolver.class);
    resolvers.put(NamedOperation.class, mockResolver);
    given(mockResolver.getScore(eq(namedOp), any())).willReturn(null);
    final OperationChain opChain = new OperationChain.Builder().first(new GetAdjacentIds()).then(new GetElements()).then(new Limit()).then(namedOp).build();
    final DefaultScoreResolver scoreResolver = new DefaultScoreResolver(opScores, resolvers);
    // When
    final Object result = scoreResolver.getScore(opChain);
    // Then
    assertEquals(5, result);
}
Also used : GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Builder(uk.gov.gchq.gaffer.named.operation.NamedOperation.Builder) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Operation(uk.gov.gchq.gaffer.operation.Operation) LinkedHashMap(java.util.LinkedHashMap) NamedOperationScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.named.NamedOperationScoreResolver) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) Limit(uk.gov.gchq.gaffer.operation.impl.Limit) Test(org.junit.jupiter.api.Test)

Example 23 with NamedOperation

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

the class NamedOperationResolverTest method shouldReturnOperationsInParameters.

@Test
public void shouldReturnOperationsInParameters() {
    // Given
    final NamedOperation namedOperation = new NamedOperation();
    Operation operation = new GetElements();
    Map<String, Object> paramMap = Maps.newHashMap();
    paramMap.put("test param", operation);
    namedOperation.setParameters(paramMap);
    // When
    List<Operation> paramOperations = namedOperation.getOperations();
    Operation op = paramOperations.get(0);
    // Then
    assertEquals(paramOperations.size(), 1);
    assertEquals(op.getClass(), GetElements.class);
}
Also used : GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Operation(uk.gov.gchq.gaffer.operation.Operation) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Test(org.junit.jupiter.api.Test)

Example 24 with NamedOperation

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

the class DefaultScoreResolver method getScore.

@Override
public Integer getScore(final Operation operation) {
    if (null == operation) {
        return 0;
    }
    // Named operations should use their custom resolver despite implementing Operations
    if (operation instanceof Operations && !(operation instanceof NamedOperation)) {
        int score = 0;
        for (final Operation op : ((Operations<?>) operation).getOperations()) {
            score += getScore(op);
        }
        return score;
    } else {
        final Class<? extends Operation> opClass = operation.getClass();
        // Check the operation is not the same instance
        if (null == skipResolvingOperation || skipResolvingOperation != operation) {
            ScoreResolver resolver = scoreResolvers.get(opClass);
            if (null != resolver) {
                // Create a delegate resolver with the current resolver skipped to prevent an infinite loop
                final DefaultScoreResolver delegateResolver = new DefaultScoreResolver(opScores, scoreResolvers, operation);
                Integer opScore = resolver.getScore(operation, delegateResolver);
                if (null != opScore) {
                    return opScore;
                }
            }
        }
        // Use the operation scores map to get the score.
        final List<Class<? extends Operation>> keys = new ArrayList<>(opScores.keySet());
        for (int i = keys.size() - 1; i >= 0; i--) {
            final Class<? extends Operation> key = keys.get(i);
            if (key.isAssignableFrom(opClass)) {
                return opScores.get(key);
            }
        }
    }
    return DEFAULT_OPERATION_SCORE;
}
Also used : ArrayList(java.util.ArrayList) Operation(uk.gov.gchq.gaffer.operation.Operation) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Operations(uk.gov.gchq.gaffer.operation.Operations) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation)

Example 25 with NamedOperation

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

the class NamedOperationScoreResolver method getScore.

@Override
public Integer getScore(final NamedOperation operation, final ScoreResolver defaultScoreResolver) {
    Integer namedOpScore = null;
    NamedOperationDetail namedOpDetail = null;
    if (null == operation) {
        return 0;
    }
    try {
        namedOpDetail = cache.getFromCache(operation.getOperationName());
    } catch (final CacheOperationFailedException e) {
        LOGGER.warn("Error accessing cache for Operation '{}': " + e.getMessage(), operation.getClass().getName());
    }
    if (null != namedOpDetail) {
        namedOpScore = namedOpDetail.getScore();
        if (null == namedOpScore && null != defaultScoreResolver) {
            namedOpScore = defaultScoreResolver.getScore(namedOpDetail.getOperationChain(operation.getParameters()));
        }
    }
    if (null != defaultScoreResolver) {
        if (null == namedOpScore) {
            namedOpScore = 0;
        }
        List parameterOperations = operation.getOperations();
        if (null != parameterOperations) {
            for (final Object objectOperation : parameterOperations) {
                Operation op = (Operation) objectOperation;
                Integer parameterOpScore = defaultScoreResolver.getScore(op);
                namedOpScore += parameterOpScore;
            }
        }
    }
    return namedOpScore;
}
Also used : NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) List(java.util.List) Operation(uk.gov.gchq.gaffer.operation.Operation) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) CacheOperationFailedException(uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException)

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