Search in sources :

Example 11 with TupleAdaptedPredicate

use of uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate 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 12 with TupleAdaptedPredicate

use of uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate in project Gaffer by gchq.

the class FilterValidator method validateFilterPropertyClasses.

/**
 * Validates that the predicates to be executed are assignable to the corresponding properties
 *
 * @param elementDef The SchemaElementDefinition to validate against
 * @param filter     The ElementFilter to be validated against
 * @return ValidationResult of the validation
 */
private ValidationResult validateFilterPropertyClasses(final SchemaElementDefinition elementDef, final ElementFilter filter) {
    final ValidationResult result = new ValidationResult();
    if (null != elementDef) {
        final List<TupleAdaptedPredicate<String, ?>> components = filter.getComponents();
        for (final TupleAdaptedPredicate<String, ?> component : components) {
            final Map<String, String> properties = elementDef.getPropertyMap();
            if (!properties.isEmpty()) {
                if (null == component.getPredicate()) {
                    result.addError(filter.getClass().getSimpleName() + " contains a null function.");
                } else {
                    final Class[] selectionClasses = getTypeClasses(component.getSelection(), elementDef);
                    if (!ArrayUtils.contains(selectionClasses, null)) {
                        final Signature inputSig = Signature.getInputSignature(component.getPredicate());
                        result.add(inputSig.assignable(selectionClasses));
                    }
                }
            }
        }
    }
    return result;
}
Also used : TupleAdaptedPredicate(uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate) Signature(uk.gov.gchq.koryphe.signature.Signature) ValidationResult(uk.gov.gchq.koryphe.ValidationResult)

Example 13 with TupleAdaptedPredicate

use of uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate in project Gaffer by gchq.

the class AccumuloStoreRelationTest method testBuildScanRestrictViewByProperty.

@Test
public void testBuildScanRestrictViewByProperty() throws OperationException, StoreException {
    final List<TupleAdaptedPredicate<String, ?>> filters = new ArrayList<>();
    filters.add(new TupleAdaptedPredicate<>(new IsMoreThan(5, false), new String[] { "property1" }));
    final View view = new View.Builder().edge(GetDataFrameOfElementsHandlerTest.EDGE_GROUP, new ViewElementDefinition.Builder().postAggregationFilterFunctions(filters).build()).build();
    final Predicate<Element> returnElement = (Element element) -> element.getGroup().equals(GetDataFrameOfElementsHandlerTest.EDGE_GROUP) && (Integer) element.getProperty("property1") > 5;
    testBuildScanWithView("testBuildScanRestrictViewByProperty", view, returnElement);
}
Also used : TupleAdaptedPredicate(uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) Test(org.junit.jupiter.api.Test)

Aggregations

TupleAdaptedPredicate (uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate)13 Test (org.junit.jupiter.api.Test)8 Predicate (java.util.function.Predicate)7 IsMoreThan (uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)7 ArrayList (java.util.ArrayList)6 IsLessThan (uk.gov.gchq.koryphe.impl.predicate.IsLessThan)6 GreaterThan (org.apache.spark.sql.sources.GreaterThan)5 LessThan (org.apache.spark.sql.sources.LessThan)5 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)5 SparkSession (org.apache.spark.sql.SparkSession)4 Filter (org.apache.spark.sql.sources.Filter)4 Operation (uk.gov.gchq.gaffer.operation.Operation)4 GraphFilters (uk.gov.gchq.gaffer.operation.graph.GraphFilters)4 Schema (uk.gov.gchq.gaffer.store.schema.Schema)4 HashSet (java.util.HashSet)3 FilterPredicate (org.apache.parquet.filter2.predicate.FilterPredicate)3 EqualTo (org.apache.spark.sql.sources.EqualTo)3 And (org.apache.spark.sql.sources.And)2 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)2 EntityId (uk.gov.gchq.gaffer.data.element.id.EntityId)2