use of uk.gov.gchq.gaffer.operation.impl.function.Transform in project gaffer-doc by gchq.
the class TransformExample method transformACountPropertyIntoACountStringPropertyOnlyForEdgesOfTypeEdge.
public void transformACountPropertyIntoACountStringPropertyOnlyForEdgesOfTypeEdge() {
// ---------------------------------------------------------
final Transform transform = new Transform.Builder().edge("edge", new ElementTransformer.Builder().select("count").execute(new ToString()).project("countString").build()).build();
// ---------------------------------------------------------
showExample(transform, null);
}
use of uk.gov.gchq.gaffer.operation.impl.function.Transform in project Gaffer by gchq.
the class ViewMigration method createMigrationOps.
public static List<Operation> createMigrationOps(final boolean aggregateAfter, final Iterable<ViewMigration> views) {
final ViewMigration viewMig = new ViewMigration(aggregateAfter);
for (final ViewMigration view : views) {
viewMig.edgesPostAggregationFilter.putAll(view.edgesPostAggregationFilter);
viewMig.entitiesPostAggregationFilter.putAll(view.entitiesPostAggregationFilter);
viewMig.edgesPostTransformFilter.putAll(view.edgesPostTransformFilter);
viewMig.entitiesPostTransformFilter.putAll(view.entitiesPostTransformFilter);
viewMig.edgesTransformer.putAll(view.edgesTransformer);
viewMig.entitiesTransformer.putAll(view.entitiesTransformer);
}
final List<Operation> updatedOps = new ArrayList<>();
if (aggregateAfter) {
final Aggregate aggregate = new Aggregate();
updatedOps.add(aggregate);
if (!viewMig.entitiesPostAggregationFilter.isEmpty() || !viewMig.edgesPostAggregationFilter.isEmpty()) {
final Filter postAggregationFilter = new Filter.Builder().entities(viewMig.entitiesPostAggregationFilter).edges(viewMig.edgesPostAggregationFilter).build();
updatedOps.add(postAggregationFilter);
}
if (!viewMig.entitiesTransformer.isEmpty() || !viewMig.edgesTransformer.isEmpty()) {
final Transform transformFunction = new Transform.Builder().entities(viewMig.entitiesTransformer).edges(viewMig.edgesTransformer).build();
updatedOps.add(transformFunction);
}
if (!viewMig.edgesPostTransformFilter.isEmpty() || !viewMig.entitiesPostTransformFilter.isEmpty()) {
final Filter postTransformFilter = new Filter.Builder().entities(viewMig.entitiesPostTransformFilter).edges(viewMig.edgesPostTransformFilter).build();
updatedOps.add(postTransformFilter);
}
}
return updatedOps;
}
use of uk.gov.gchq.gaffer.operation.impl.function.Transform in project Gaffer by gchq.
the class ScoreOperationChainHandlerTest method shouldCorrectlyResolveScoreForNestedOperationWithNullOperationList.
@Test
public void shouldCorrectlyResolveScoreForNestedOperationWithNullOperationList() throws OperationException {
// Given
final ScoreOperationChainHandler handler = new ScoreOperationChainHandler();
final Map<Class<? extends Operation>, ScoreResolver> resolvers = new HashMap<>();
final ScoreResolver scoreResolver = mock(NamedOperationScoreResolver.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 properties = mock(StoreProperties.class);
final GetAllElements op1 = mock(GetAllElements.class);
final Transform op2 = mock(Transform.class);
final Map<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
opScores.put(GetAllElements.class, 2);
opScores.put(Transform.class, 1);
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);
handler.setScoreResolvers(resolvers);
given(scoreResolver.getScore(eq(namedOp), any())).willReturn(3);
final List<? extends Operation> opList = null;
final OperationChain nestedOpChain = new OperationChain(opList);
final OperationChain opChain = new OperationChain(Arrays.asList(op1, op2, nestedOpChain, namedOp));
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(6, result);
}
use of uk.gov.gchq.gaffer.operation.impl.function.Transform in project Gaffer by gchq.
the class TransformHandlerTest method shouldTransformElementsUsingIdentityFunction.
@Test
public void shouldTransformElementsUsingIdentityFunction() throws OperationException {
// Given
given(store.getSchema()).willReturn(schema);
final Entity entity = new Entity.Builder().group(TestGroups.ENTITY).property(TestPropertyNames.PROP_1, TestPropertyNames.INT).property(TestPropertyNames.PROP_2, TestPropertyNames.STRING).build();
final Entity entity1 = new Entity.Builder().group(TestGroups.ENTITY).property(TestPropertyNames.PROP_1, TestPropertyNames.INT).build();
final ElementTransformer transformer = new ElementTransformer.Builder().select(TestPropertyNames.PROP_1).execute(new Identity()).project(TestPropertyNames.PROP_3).build();
final Entity expectedEntity = new Entity.Builder().group(TestGroups.ENTITY).property(TestPropertyNames.PROP_3, TestPropertyNames.INT).property(TestPropertyNames.PROP_2, TestPropertyNames.STRING).build();
final Entity expectedEntity1 = new Entity.Builder().group(TestGroups.ENTITY).property(TestPropertyNames.PROP_3, TestPropertyNames.INT).build();
final Transform transform = new Transform.Builder().input(input).entity(TestGroups.ENTITY, transformer).build();
input.add(entity);
input.add(entity1);
expected.add(expectedEntity);
expected.add(expectedEntity1);
// When
final Iterable<? extends Element> results = handler.doOperation(transform, context, store);
final List<Element> resultsList = Lists.newArrayList(results);
// Then
boolean isSame = false;
for (int i = 0; i < expected.size(); i++) {
isSame = expected.get(i).getProperty(TestPropertyNames.PROP_3).equals(resultsList.get(i).getProperty(TestPropertyNames.PROP_3));
}
assertTrue(isSame);
}
use of uk.gov.gchq.gaffer.operation.impl.function.Transform in project Gaffer by gchq.
the class TransformHandlerTest method shouldTransformElementsUsingInlineFunction.
@Test
public void shouldTransformElementsUsingInlineFunction() throws OperationException {
// Given
final Function<String, Integer> function = String::length;
final Map<String, ElementTransformer> entities = new HashMap<>();
given(store.getSchema()).willReturn(schema);
final Entity entity = new Entity.Builder().group(TestGroups.ENTITY).property(TestPropertyNames.PROP_1, "value").property(TestPropertyNames.PROP_2, 1).build();
final Entity entity1 = new Entity.Builder().group(TestGroups.ENTITY_2).property(TestPropertyNames.PROP_1, "otherValue").property(TestPropertyNames.PROP_2, 3).build();
final ElementTransformer transformer = new ElementTransformer.Builder().select(TestPropertyNames.PROP_1).execute(function).project(TestPropertyNames.PROP_3).build();
entities.put(TestGroups.ENTITY, transformer);
entities.put(TestGroups.ENTITY_2, transformer);
final Entity expectedEntity = new Entity.Builder().group(TestGroups.ENTITY).property(TestPropertyNames.PROP_3, 5).property(TestPropertyNames.PROP_2, 1).build();
final Entity expectedEntity1 = new Entity.Builder().group(TestGroups.ENTITY_2).property(TestPropertyNames.PROP_3, 10).property(TestPropertyNames.PROP_2, 3).build();
final Transform transform = new Transform.Builder().input(input).entities(entities).build();
input.add(entity);
input.add(entity1);
expected.add(expectedEntity);
expected.add(expectedEntity1);
// When
final Iterable<? extends Element> results = handler.doOperation(transform, context, store);
final List<Element> resultsList = Lists.newArrayList(results);
// Then
boolean isSame = false;
for (int i = 0; i < resultsList.size(); i++) {
isSame = expected.get(i).getProperty(TestPropertyNames.PROP_3).equals(resultsList.get(i).getProperty(TestPropertyNames.PROP_3));
}
assertTrue(isSame);
}
Aggregations