Search in sources :

Example 6 with IsA

use of uk.gov.gchq.koryphe.impl.predicate.IsA in project Gaffer by gchq.

the class SchemaTest method testLoadingSchemaFromJson.

@Test
public void testLoadingSchemaFromJson() {
    // Edge definitions
    SchemaElementDefinition edgeDefinition = schema.getEdge(TestGroups.EDGE);
    assertNotNull(edgeDefinition);
    assertEquals(EDGE_DESCRIPTION, edgeDefinition.getDescription());
    final Map<String, String> propertyMap = edgeDefinition.getPropertyMap();
    assertThat(propertyMap).hasSize(3);
    assertEquals("prop.string", propertyMap.get(TestPropertyNames.PROP_2));
    assertEquals("prop.date", propertyMap.get(TestPropertyNames.DATE));
    assertEquals("timestamp", propertyMap.get(TestPropertyNames.TIMESTAMP));
    assertEquals(Sets.newLinkedHashSet(Collections.singletonList(TestPropertyNames.DATE)), edgeDefinition.getGroupBy());
    // Check validator
    ElementFilter validator = edgeDefinition.getValidator();
    List<TupleAdaptedPredicate<String, ?>> valContexts = validator.getComponents();
    int index = 0;
    TupleAdaptedPredicate<String, ?> tuplePredicate = valContexts.get(index++);
    assertTrue(tuplePredicate.getPredicate() instanceof IsA);
    assertThat(tuplePredicate.getSelection()).hasSize(1);
    assertEquals(IdentifierType.SOURCE.name(), tuplePredicate.getSelection()[0]);
    tuplePredicate = valContexts.get(index++);
    assertTrue(tuplePredicate.getPredicate() instanceof IsA);
    assertThat(tuplePredicate.getSelection()).hasSize(1);
    assertEquals(IdentifierType.DESTINATION.name(), tuplePredicate.getSelection()[0]);
    tuplePredicate = valContexts.get(index++);
    assertTrue(tuplePredicate.getPredicate() instanceof IsA);
    assertThat(tuplePredicate.getSelection()).hasSize(1);
    assertEquals(IdentifierType.DIRECTED.name(), tuplePredicate.getSelection()[0]);
    tuplePredicate = valContexts.get(index++);
    assertTrue(tuplePredicate.getPredicate() instanceof ExampleFilterFunction);
    assertThat(tuplePredicate.getSelection()).hasSize(1);
    assertEquals(IdentifierType.DIRECTED.name(), tuplePredicate.getSelection()[0]);
    tuplePredicate = valContexts.get(index++);
    assertTrue(tuplePredicate.getPredicate() instanceof IsA);
    assertThat(tuplePredicate.getSelection()).hasSize(1);
    assertEquals(TestPropertyNames.PROP_2, tuplePredicate.getSelection()[0]);
    tuplePredicate = valContexts.get(index++);
    assertTrue(tuplePredicate.getPredicate() instanceof ExampleFilterFunction);
    assertThat(tuplePredicate.getSelection()).hasSize(1);
    assertEquals(TestPropertyNames.PROP_2, tuplePredicate.getSelection()[0]);
    tuplePredicate = valContexts.get(index++);
    assertTrue(tuplePredicate.getPredicate() instanceof IsA);
    assertThat(tuplePredicate.getSelection()).hasSize(1);
    assertEquals(TestPropertyNames.DATE, tuplePredicate.getSelection()[0]);
    tuplePredicate = valContexts.get(index++);
    assertTrue(tuplePredicate.getPredicate() instanceof IsA);
    assertThat(tuplePredicate.getSelection()).hasSize(1);
    assertEquals(TestPropertyNames.TIMESTAMP, tuplePredicate.getSelection()[0]);
    assertEquals(index, valContexts.size());
    TypeDefinition type = edgeDefinition.getPropertyTypeDef(TestPropertyNames.DATE);
    assertEquals(Date.class, type.getClazz());
    assertEquals(DATE_TYPE_DESCRIPTION, type.getDescription());
    assertNull(type.getSerialiser());
    assertTrue(type.getAggregateFunction() instanceof ExampleAggregateFunction);
    // Entity definitions
    SchemaElementDefinition entityDefinition = schema.getEntity(TestGroups.ENTITY);
    assertNotNull(entityDefinition);
    assertEquals(ENTITY_DESCRIPTION, entityDefinition.getDescription());
    assertTrue(entityDefinition.containsProperty(TestPropertyNames.PROP_1));
    type = entityDefinition.getPropertyTypeDef(TestPropertyNames.PROP_1);
    assertEquals(0, entityDefinition.getGroupBy().size());
    assertEquals(STRING_TYPE_DESCRIPTION, type.getDescription());
    assertEquals(String.class, type.getClazz());
    assertNull(type.getSerialiser());
    assertTrue(type.getAggregateFunction() instanceof ExampleAggregateFunction);
    validator = entityDefinition.getValidator();
    valContexts = validator.getComponents();
    index = 0;
    tuplePredicate = valContexts.get(index++);
    assertTrue(tuplePredicate.getPredicate() instanceof IsXMoreThanY);
    assertThat(tuplePredicate.getSelection()).hasSize(2);
    assertEquals(TestPropertyNames.PROP_1, tuplePredicate.getSelection()[0]);
    assertEquals(TestPropertyNames.VISIBILITY, tuplePredicate.getSelection()[1]);
    tuplePredicate = valContexts.get(index++);
    assertTrue(tuplePredicate.getPredicate() instanceof IsA);
    assertThat(tuplePredicate.getSelection()).hasSize(1);
    assertEquals(IdentifierType.VERTEX.name(), tuplePredicate.getSelection()[0]);
    tuplePredicate = valContexts.get(index++);
    assertTrue(tuplePredicate.getPredicate() instanceof IsA);
    assertThat(tuplePredicate.getSelection()).hasSize(1);
    assertEquals(TestPropertyNames.PROP_1, tuplePredicate.getSelection()[0]);
    final ElementAggregator aggregator = edgeDefinition.getFullAggregator();
    final List<TupleAdaptedBinaryOperator<String, ?>> aggContexts = aggregator.getComponents();
    assertThat(aggContexts).hasSize(3);
    TupleAdaptedBinaryOperator<String, ?> aggContext = aggContexts.get(0);
    assertTrue(aggContext.getBinaryOperator() instanceof ExampleAggregateFunction);
    assertThat(aggContext.getSelection()).hasSize(1);
    assertEquals(TestPropertyNames.PROP_2, aggContext.getSelection()[0]);
    aggContext = aggContexts.get(1);
    assertTrue(aggContext.getBinaryOperator() instanceof ExampleAggregateFunction);
    assertThat(aggContext.getSelection()).hasSize(1);
    assertEquals(TestPropertyNames.DATE, aggContext.getSelection()[0]);
    TypeDefinition mapTypeDef = schema.getType(TestTypes.PROP_MAP);
    assertEquals(LinkedHashMap.class, mapTypeDef.getClazz());
    assertEquals(MAP_TYPE_DESCRIPTION, mapTypeDef.getDescription());
    Serialiser serialiser = mapTypeDef.getSerialiser();
    assertEquals(MapSerialiser.class, serialiser.getClass());
    MapSerialiser mapSerialiser = (MapSerialiser) serialiser;
    assertEquals(StringSerialiser.class, mapSerialiser.getKeySerialiser().getClass());
    assertEquals(RawLongSerialiser.class, mapSerialiser.getValueSerialiser().getClass());
    assertNull(mapSerialiser.getMapClass());
}
Also used : TupleAdaptedPredicate(uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate) RawLongSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.raw.RawLongSerialiser) MapSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.MapSerialiser) Serialiser(uk.gov.gchq.gaffer.serialisation.Serialiser) StringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.StringSerialiser) JavaSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.JavaSerialiser) ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) StringToStringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.tostring.StringToStringSerialiser) MapSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.MapSerialiser) IsA(uk.gov.gchq.koryphe.impl.predicate.IsA) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ExampleAggregateFunction(uk.gov.gchq.gaffer.function.ExampleAggregateFunction) TupleAdaptedBinaryOperator(uk.gov.gchq.koryphe.tuple.binaryoperator.TupleAdaptedBinaryOperator) ExampleFilterFunction(uk.gov.gchq.gaffer.function.ExampleFilterFunction) IsXMoreThanY(uk.gov.gchq.koryphe.impl.predicate.IsXMoreThanY) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator) Test(org.junit.jupiter.api.Test)

Example 7 with IsA

use of uk.gov.gchq.koryphe.impl.predicate.IsA in project Gaffer by gchq.

the class IfIT method shouldReturnOriginalInputWhenConditionIsFalseAndNoOtherwise.

@Test
public void shouldReturnOriginalInputWhenConditionIsFalseAndNoOtherwise() throws OperationException {
    // Given
    final If<Object, Object> ifOperation = new If<>();
    ifOperation.setInput(INPUT_CAMEL_CASE);
    ifOperation.setConditional(new Conditional(new IsA("java.lang.Integer")));
    ifOperation.setThen(new Map<>(Lists.newArrayList(new ToLong(), new ToList())));
    // When
    final Object output = graph.execute(ifOperation, getUser());
    // Then
    assertThat(output).isEqualTo(INPUT_CAMEL_CASE).isInstanceOf(String.class);
}
Also used : ToLong(uk.gov.gchq.koryphe.impl.function.ToLong) IsA(uk.gov.gchq.koryphe.impl.predicate.IsA) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) If(uk.gov.gchq.gaffer.operation.impl.If) ToList(uk.gov.gchq.koryphe.impl.function.ToList) Test(org.junit.Test)

Example 8 with IsA

use of uk.gov.gchq.koryphe.impl.predicate.IsA in project Gaffer by gchq.

the class IfIT method shouldReturnOriginalInputWhenConditionIsTrueAndNoThen.

@Test
public void shouldReturnOriginalInputWhenConditionIsTrueAndNoThen() throws OperationException {
    // Given
    final If<Object, Object> ifOperation = new If<>();
    ifOperation.setInput(INPUT_CAMEL_CASE);
    ifOperation.setConditional(new Conditional(new IsA("java.lang.String")));
    ifOperation.setOtherwise(new Map<>(Lists.newArrayList(new ToLong(), new ToList())));
    // When
    final Object output = graph.execute(ifOperation, getUser());
    // Then
    assertThat(output).isEqualTo(INPUT_CAMEL_CASE).isInstanceOf(String.class);
}
Also used : ToLong(uk.gov.gchq.koryphe.impl.function.ToLong) IsA(uk.gov.gchq.koryphe.impl.predicate.IsA) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) If(uk.gov.gchq.gaffer.operation.impl.If) ToList(uk.gov.gchq.koryphe.impl.function.ToList) Test(org.junit.Test)

Example 9 with IsA

use of uk.gov.gchq.koryphe.impl.predicate.IsA in project Gaffer by gchq.

the class IfIT method shouldRunThenOperationWhenConditionIsTrue.

@Test
public void shouldRunThenOperationWhenConditionIsTrue() throws OperationException {
    // Given
    final If<Object, Object> ifOperation = new If<>();
    ifOperation.setInput(INPUT_CAMEL_CASE);
    ifOperation.setConditional(new Conditional(new IsA("java.lang.String")));
    ifOperation.setThen(new Map<>(Lists.newArrayList(new ToUpperCase(), new ToList())));
    ifOperation.setOtherwise(new Map<>(Lists.newArrayList(new ToLowerCase(), new ToList())));
    // When
    final Object output = graph.execute(ifOperation, getUser());
    // Then
    assertThat(output).isEqualTo(Lists.newArrayList(INPUT_CAMEL_CASE.toUpperCase())).isInstanceOf(List.class);
}
Also used : IsA(uk.gov.gchq.koryphe.impl.predicate.IsA) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) If(uk.gov.gchq.gaffer.operation.impl.If) ToLowerCase(uk.gov.gchq.koryphe.impl.function.ToLowerCase) ToList(uk.gov.gchq.koryphe.impl.function.ToList) ToUpperCase(uk.gov.gchq.koryphe.impl.function.ToUpperCase) Test(org.junit.Test)

Aggregations

IsA (uk.gov.gchq.koryphe.impl.predicate.IsA)9 Test (org.junit.Test)5 If (uk.gov.gchq.gaffer.operation.impl.If)5 Conditional (uk.gov.gchq.gaffer.operation.util.Conditional)5 ToList (uk.gov.gchq.koryphe.impl.function.ToList)5 ToLowerCase (uk.gov.gchq.koryphe.impl.function.ToLowerCase)3 ToLong (uk.gov.gchq.koryphe.impl.function.ToLong)2 ToUpperCase (uk.gov.gchq.koryphe.impl.function.ToUpperCase)2 Test (org.junit.jupiter.api.Test)1 ElementAggregator (uk.gov.gchq.gaffer.data.element.function.ElementAggregator)1 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)1 ExampleAggregateFunction (uk.gov.gchq.gaffer.function.ExampleAggregateFunction)1 ExampleFilterFunction (uk.gov.gchq.gaffer.function.ExampleFilterFunction)1 Serialiser (uk.gov.gchq.gaffer.serialisation.Serialiser)1 ToBytesSerialiser (uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser)1 JavaSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.JavaSerialiser)1 MapSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.MapSerialiser)1 StringSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.StringSerialiser)1 RawLongSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.raw.RawLongSerialiser)1 StringToStringSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.tostring.StringToStringSerialiser)1