Search in sources :

Example 6 with ScoreOperationChain

use of uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain in project Gaffer by gchq.

the class ScoreOperationChainHandlerTest method shouldExecuteScoreChainOperation.

@Test
public void shouldExecuteScoreChainOperation() throws OperationException {
    // Given
    final ScoreOperationChainHandler operationHandler = new ScoreOperationChainHandler();
    final Context context = mock(Context.class);
    final Store store = mock(Store.class);
    final User user = mock(User.class);
    final ScoreOperationChain scoreOperationChain = mock(ScoreOperationChain.class);
    StoreProperties storeProperties = new StoreProperties();
    final GetAdjacentIds op1 = mock(GetAdjacentIds.class);
    final GetElements op2 = mock(GetElements.class);
    final OperationChain opChain = new OperationChain(Arrays.asList(op1, op2));
    final Integer expectedResult = 2;
    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 = operationHandler.doOperation(new ScoreOperationChain.Builder().operationChain(opChain).build(), context, store);
    // Then
    assertSame(expectedResult, result);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) User(uk.gov.gchq.gaffer.user.User) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) Store(uk.gov.gchq.gaffer.store.Store) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) ScoreOperationChain(uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) ScoreOperationChain(uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 7 with ScoreOperationChain

use of uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain in project Gaffer by gchq.

the class ScoreOperationChainHandlerTest method shouldExecuteScoreChainOperationForNestedOperationChain.

@Test
public void shouldExecuteScoreChainOperationForNestedOperationChain() throws OperationException {
    // Given
    final ScoreOperationChainHandler operationHandler = new ScoreOperationChainHandler();
    final Context context = mock(Context.class);
    final Store store = mock(Store.class);
    final User user = mock(User.class);
    final ScoreOperationChain scoreOperationChain = mock(ScoreOperationChain.class);
    StoreProperties storeProperties = new StoreProperties();
    final GetAdjacentIds op1 = mock(GetAdjacentIds.class);
    final GetElements op2 = mock(GetElements.class);
    final Limit op3 = mock(Limit.class);
    final OperationChain opChain1 = new OperationChain(Arrays.asList(op1, op2));
    final OperationChain opChain = new OperationChain(Arrays.asList(opChain1, op3));
    final Integer expectedResult = 3;
    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 = operationHandler.doOperation(new ScoreOperationChain.Builder().operationChain(opChain).build(), context, store);
    // Then
    assertSame(expectedResult, result);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) User(uk.gov.gchq.gaffer.user.User) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) Store(uk.gov.gchq.gaffer.store.Store) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) ScoreOperationChain(uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) ScoreOperationChain(uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain) Limit(uk.gov.gchq.gaffer.operation.impl.Limit) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 8 with ScoreOperationChain

use of uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain in project gaffer-doc by gchq.

the class ScoreOperationChainExample method scoreOperationChainWithCustomNamedScore.

public void scoreOperationChainWithCustomNamedScore() {
    // ---------------------------------------------------------
    final ScoreOperationChain scoreOperationChain = new ScoreOperationChain.Builder().operationChain(new OperationChain.Builder().first(new NamedOperation.Builder<EntitySeed, Iterable<? extends Element>>().name("1-hop").input(new EntitySeed(1)).build()).then(new Limit<>(3)).build()).build();
    // ---------------------------------------------------------
    runExample(scoreOperationChain, "Here we have added a NamedOperation to the NamedOperationCache, " + "with a custom score of 3. In our ScoreOperationChainDeclaration.json file, we have also " + "declared that this should be resolved with a NamedOperationScoreResolver. " + "With Limit declared as having a score of 2, the above chain correctly has a score of 5.");
}
Also used : OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) ScoreOperationChain(uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Limit(uk.gov.gchq.gaffer.operation.impl.Limit) ScoreOperationChain(uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain)

Example 9 with ScoreOperationChain

use of uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain 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 10 with ScoreOperationChain

use of uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain in project Gaffer by gchq.

the class ScoreOperationChainHandlerTest method shouldReturnZeroForANullOperationChain.

@Test
public void shouldReturnZeroForANullOperationChain() throws OperationException {
    // Given
    final ScoreOperationChainHandler handler = new ScoreOperationChainHandler();
    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 properties = mock(StoreProperties.class);
    final OperationChain opChain = null;
    given(scoreOperationChain.getOperationChain()).willReturn(opChain);
    given(context.getUser()).willReturn(user);
    Set<String> opAuths = new HashSet<>();
    opAuths.add("TEST_USER");
    given(user.getOpAuths()).willReturn(opAuths);
    given(store.getProperties()).willReturn(properties);
    // When
    final Object result = handler.doOperation(new ScoreOperationChain.Builder().operationChain(opChain).build(), context, store);
    // Then
    assertEquals(0, result);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) User(uk.gov.gchq.gaffer.user.User) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) ScoreOperationChain(uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain) Store(uk.gov.gchq.gaffer.store.Store) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) ScoreOperationChain(uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

ScoreOperationChain (uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain)10 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)9 HashSet (java.util.HashSet)8 Test (org.junit.jupiter.api.Test)8 Context (uk.gov.gchq.gaffer.store.Context)8 Store (uk.gov.gchq.gaffer.store.Store)8 StoreProperties (uk.gov.gchq.gaffer.store.StoreProperties)8 User (uk.gov.gchq.gaffer.user.User)8 NamedOperation (uk.gov.gchq.gaffer.named.operation.NamedOperation)6 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)5 Element (uk.gov.gchq.gaffer.data.element.Element)5 Operation (uk.gov.gchq.gaffer.operation.Operation)5 Limit (uk.gov.gchq.gaffer.operation.impl.Limit)5 GetAdjacentIds (uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds)5 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)5 DefaultScoreResolver (uk.gov.gchq.gaffer.store.operation.resolver.DefaultScoreResolver)5 IfScoreResolver (uk.gov.gchq.gaffer.store.operation.resolver.IfScoreResolver)5 ScoreResolver (uk.gov.gchq.gaffer.store.operation.resolver.ScoreResolver)5 NamedOperationScoreResolver (uk.gov.gchq.gaffer.store.operation.resolver.named.NamedOperationScoreResolver)5