Search in sources :

Example 21 with ElementFilter

use of uk.gov.gchq.gaffer.data.element.function.ElementFilter in project Gaffer by gchq.

the class GraphTest method shouldExpandAllEdges.

@Test
public void shouldExpandAllEdges() throws OperationException {
    final Schema twoEdgesNoEntities = new Schema.Builder().type(TestTypes.PROP_STRING, new TypeDefinition.Builder().clazz(String.class).build()).type("vertex", new TypeDefinition.Builder().clazz(String.class).build()).edge("firstEdge", new SchemaEdgeDefinition.Builder().property(TestPropertyNames.PROP_1, TestTypes.PROP_STRING).aggregate(false).source("vertex").destination("vertex").directed(DIRECTED_EITHER).build()).edge("secondEdge", new SchemaEdgeDefinition.Builder().property(TestPropertyNames.PROP_1, TestTypes.PROP_STRING).aggregate(false).source("vertex").destination("vertex").directed(DIRECTED_EITHER).build()).build();
    final Store store = mock(Store.class);
    final ArgumentCaptor<OperationChain> capturedOperation = ArgumentCaptor.forClass(OperationChain.class);
    final ArgumentCaptor<Context> capturedContext = ArgumentCaptor.forClass(Context.class);
    given(store.getSchema()).willReturn(twoEdgesNoEntities);
    given(store.getOriginalSchema()).willReturn(twoEdgesNoEntities);
    given(store.getProperties()).willReturn(mock(StoreProperties.class));
    final Graph graph = new Graph.Builder().config(new GraphConfig.Builder().graphId(GRAPH_ID).build()).storeProperties(StreamUtil.storeProps(getClass())).store(store).addSchema(twoEdgesNoEntities).build();
    final ElementFilter filter = mock(ElementFilter.class);
    final GlobalViewElementDefinition globalEdgeAggregate = new GlobalViewElementDefinition.Builder().postAggregationFilter(filter).build();
    final View view = new View.Builder().allEdges(true).build();
    operation = new GetElements.Builder().view(view).build();
    opChain = new OperationChain.Builder().first(operation).build();
    graph.execute(opChain, context);
    Mockito.verify(store, Mockito.times(1)).execute(capturedOperation.capture(), capturedContext.capture());
    assertEquals(1, capturedOperation.getAllValues().size());
    final OperationChain transformedOpChain = capturedOperation.getAllValues().get(0);
    assertEquals(1, transformedOpChain.getOperations().size());
    assertEquals(GetElements.class, transformedOpChain.getOperations().get(0).getClass());
    final View mergedView = ((GetElements) transformedOpChain.getOperations().get(0)).getView();
    assertTrue(mergedView.isAllEdges());
    assertEquals(2, mergedView.getEdges().size());
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) Schema(uk.gov.gchq.gaffer.store.schema.Schema) Store(uk.gov.gchq.gaffer.store.Store) TestStore(uk.gov.gchq.gaffer.integration.store.TestStore) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) GlobalViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.GlobalViewElementDefinition) OperationView(uk.gov.gchq.gaffer.operation.graph.OperationView) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) SchemaEdgeDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) Test(org.junit.jupiter.api.Test)

Example 22 with ElementFilter

use of uk.gov.gchq.gaffer.data.element.function.ElementFilter 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 23 with ElementFilter

use of uk.gov.gchq.gaffer.data.element.function.ElementFilter in project Gaffer by gchq.

the class SchemaElementDefinitionTest method shouldReturnFullValidatorWithoutIsA.

@Test
public void shouldReturnFullValidatorWithoutIsA() {
    // Given
    final T elementDef = createBuilder().property("property", PROPERTY_STRING_TYPE).property("property1", PROPERTY_STRING_TYPE).property("property2", PROPERTY_STRING_TYPE).validator(new ElementFilter.Builder().select("property1", "property2").execute(new IsXMoreThanY()).build()).build();
    setupSchema(elementDef);
    // When
    final ElementFilter validator = elementDef.getValidator(false);
    // Then
    int i = 0;
    assertEquals(IsXMoreThanY.class, validator.getComponents().get(i).getPredicate().getClass());
    assertArrayEquals(new String[] { "property1", "property2" }, validator.getComponents().get(i).getSelection());
    i++;
    assertEquals(Exists.class, validator.getComponents().get(i).getPredicate().getClass());
    assertArrayEquals(new String[] { "property" }, validator.getComponents().get(i).getSelection());
    i++;
    assertEquals(Exists.class, validator.getComponents().get(i).getPredicate().getClass());
    assertArrayEquals(new String[] { "property1" }, validator.getComponents().get(i).getSelection());
    i++;
    assertEquals(Exists.class, validator.getComponents().get(i).getPredicate().getClass());
    assertArrayEquals(new String[] { "property2" }, validator.getComponents().get(i).getSelection());
    i++;
    assertEquals(i, validator.getComponents().size());
    // Check the validator is cached
    // Check the validator is cached
    assertSame(validator, elementDef.getValidator(false));
    assertNotSame(validator, elementDef.getValidator(true));
}
Also used : ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) IsXMoreThanY(uk.gov.gchq.koryphe.impl.predicate.IsXMoreThanY) Test(org.junit.jupiter.api.Test)

Example 24 with ElementFilter

use of uk.gov.gchq.gaffer.data.element.function.ElementFilter in project Gaffer by gchq.

the class SchemaElementDefinitionTest method shouldBuildElementDefinition.

@Test
public void shouldBuildElementDefinition() {
    // Given
    final ElementFilter validator = mock(ElementFilter.class);
    // When
    final T elementDef = createBuilder().property(TestPropertyNames.PROP_1, "property.integer").property(TestPropertyNames.PROP_2, "property.object").validator(validator).build();
    setupSchema(elementDef);
    // Then
    assertEquals(2, elementDef.getProperties().size());
    assertEquals(Integer.class, elementDef.getPropertyClass(TestPropertyNames.PROP_1));
    assertEquals(Object.class, elementDef.getPropertyClass(TestPropertyNames.PROP_2));
    assertSame(validator, elementDef.getOriginalValidator());
}
Also used : ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) Test(org.junit.jupiter.api.Test)

Example 25 with ElementFilter

use of uk.gov.gchq.gaffer.data.element.function.ElementFilter in project Gaffer by gchq.

the class SchemaElementDefinitionValidatorTest method shouldValidateFunctionSelectionsAndReturnFalseWhenFunctionTypeDoesNotEqualSelectionType.

@Test
public void shouldValidateFunctionSelectionsAndReturnFalseWhenFunctionTypeDoesNotEqualSelectionType() {
    // Given
    final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class);
    given(elementDef.getPropertyClass("selection")).willReturn((Class) String.class);
    final IsMoreThan function = new IsMoreThan(5);
    final ElementFilter elementFilter = new ElementFilter.Builder().select("selection").execute(function).build();
    final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator();
    // When
    final ValidationResult result = validator.validateFunctionArgumentTypes(elementFilter, elementDef);
    // Then
    assertFalse(result.isValid());
    assertEquals("Validation errors: \nControl value class java.lang.Integer is not compatible" + " with the input type: class java.lang.String", result.getErrorString());
}
Also used : ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) ValidationResult(uk.gov.gchq.koryphe.ValidationResult) Test(org.junit.jupiter.api.Test)

Aggregations

ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)43 Test (org.junit.jupiter.api.Test)35 Schema (uk.gov.gchq.gaffer.store.schema.Schema)11 Element (uk.gov.gchq.gaffer.data.element.Element)10 ValidationResult (uk.gov.gchq.koryphe.ValidationResult)10 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)8 JSONSerialisationTest (uk.gov.gchq.gaffer.JSONSerialisationTest)6 Context (uk.gov.gchq.gaffer.store.Context)6 Store (uk.gov.gchq.gaffer.store.Store)6 Exists (uk.gov.gchq.koryphe.impl.predicate.Exists)6 ElementAggregator (uk.gov.gchq.gaffer.data.element.function.ElementAggregator)5 ElementTransformer (uk.gov.gchq.gaffer.data.element.function.ElementTransformer)5 GlobalViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.GlobalViewElementDefinition)5 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)5 ExampleFilterFunction (uk.gov.gchq.gaffer.function.ExampleFilterFunction)5 SchemaElementDefinition (uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition)5 HashMap (java.util.HashMap)4 TestStore (uk.gov.gchq.gaffer.integration.store.TestStore)4 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)4 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)4