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);
}
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);
}
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);
}
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;
}
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;
}
Aggregations