Search in sources :

Example 1 with IsEqual

use of uk.gov.gchq.koryphe.impl.predicate.IsEqual in project gaffer-doc by gchq.

the class Cardinalities method run.

public CloseableIterable<? extends Element> run() throws OperationException, IOException {
    // [graph] create a graph using our schema and store properties
    // ---------------------------------------------------------
    final Graph graph = new Graph.Builder().config(getDefaultGraphConfig()).addSchemas(StreamUtil.openStreams(getClass(), schemaPath)).storeProperties(getDefaultStoreProperties()).build();
    // ---------------------------------------------------------
    // [user] Create a user
    // ---------------------------------------------------------
    final User user = new User("user01");
    // ---------------------------------------------------------
    // [add] Create a data generator and add the edges to the graph using an operation chain consisting of:
    // generateElements - generating edges from the data (note these are directed edges)
    // addElements - add the edges to the graph
    // ---------------------------------------------------------
    final OperationChain<Void> addOpChain = new OperationChain.Builder().first(new GenerateElements.Builder<String>().generator(new RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator()).input(IOUtils.readLines(StreamUtil.openStream(getClass(), dataPath))).build()).then(new AddElements()).build();
    graph.execute(addOpChain, user);
    // ---------------------------------------------------------
    print("The elements have been added.");
    // [get] Get all edges
    // ---------------------------------------------------------
    final CloseableIterable<? extends Element> edges = graph.execute(new GetAllElements(), user);
    // ---------------------------------------------------------
    print("\nAll edges:");
    for (final Element edge : edges) {
        print("GET_ALL_EDGES_RESULT", edge.toString());
    }
    // [get all cardinalities] Get all cardinalities
    // ---------------------------------------------------------
    final GetAllElements getAllCardinalities = new GetAllElements.Builder().view(new View.Builder().entity("Cardinality").build()).build();
    // ---------------------------------------------------------
    final CloseableIterable<? extends Element> allCardinalities = graph.execute(getAllCardinalities, user);
    print("\nAll cardinalities");
    for (final Element cardinality : allCardinalities) {
        final String edgeGroup = cardinality.getProperty("edgeGroup").toString();
        print("ALL_CARDINALITIES_RESULT", "Vertex " + ((Entity) cardinality).getVertex() + " " + edgeGroup + ": " + ((HyperLogLogPlus) cardinality.getProperty("hllp")).cardinality());
    }
    // [get all summarised cardinalities] Get all summarised cardinalities over all edges
    // ---------------------------------------------------------
    final GetAllElements getAllSummarisedCardinalities = new GetAllElements.Builder().view(new View.Builder().entity("Cardinality", new ViewElementDefinition.Builder().groupBy().build()).build()).build();
    // ---------------------------------------------------------
    final CloseableIterable<? extends Element> allSummarisedCardinalities = graph.execute(getAllSummarisedCardinalities, user);
    print("\nAll summarised cardinalities");
    for (final Element cardinality : allSummarisedCardinalities) {
        final String edgeGroup = cardinality.getProperty("edgeGroup").toString();
        print("ALL_SUMMARISED_CARDINALITIES_RESULT", "Vertex " + ((Entity) cardinality).getVertex() + " " + edgeGroup + ": " + ((HyperLogLogPlus) cardinality.getProperty("hllp")).cardinality());
    }
    // [get roaduse edge cardinality 10] Get the cardinality value at vertex 10 for RoadUse edges
    // ---------------------------------------------------------
    final GetElements getCardinalities = new GetElements.Builder().input(new EntitySeed("10")).view(new View.Builder().entity("Cardinality", new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("edgeGroup").execute(new IsEqual(CollectionUtil.treeSet("RoadUse"))).build()).build()).build()).build();
    // ---------------------------------------------------------
    final Element roadUse10Cardinality;
    try (final CloseableIterable<? extends Element> elements = graph.execute(getCardinalities, user)) {
        roadUse10Cardinality = elements.iterator().next();
    }
    print("\nRoadUse edge cardinality at vertex 10:");
    final String edgeGroup = (roadUse10Cardinality.getProperty("edgeGroup")).toString();
    print("CARDINALITY_OF_10_RESULT", "Vertex " + ((Entity) roadUse10Cardinality).getVertex() + " " + edgeGroup + ": " + ((HyperLogLogPlus) roadUse10Cardinality.getProperty("hllp")).cardinality());
    // [get 2 hops with a cardinality filter] 2 hops with a cardinality filter
    // ---------------------------------------------------------
    final OperationChain<CloseableIterable<? extends Element>> twoHopsWithCardinalityFilter = new OperationChain.Builder().first(new GetElements.Builder().input(new EntitySeed("M5")).view(new View.Builder().edge("RoadHasJunction").build()).build()).then(new GetElements.Builder().view(new View.Builder().entity("Cardinality", new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("edgeGroup").execute(new IsEqual(CollectionUtil.treeSet("RoadUse"))).build()).groupBy().postAggregationFilter(new ElementFilter.Builder().select("hllp").execute(new HyperLogLogPlusIsLessThan(5)).build()).build()).build()).build()).then(new GetElements.Builder().view(new View.Builder().edge("RoadUse").build()).build()).build();
    // ---------------------------------------------------------
    final CloseableIterable<? extends Element> twoHopsWithCardinalityFilterResult = graph.execute(twoHopsWithCardinalityFilter, user);
    print("\n2 hops with cardinality filter result:");
    for (final Element element : twoHopsWithCardinalityFilterResult) {
        print("2_HOP_RESULT", element.toString());
    }
    return allSummarisedCardinalities;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) Entity(uk.gov.gchq.gaffer.data.element.Entity) User(uk.gov.gchq.gaffer.user.User) Element(uk.gov.gchq.gaffer.data.element.Element) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) HyperLogLogPlusIsLessThan(uk.gov.gchq.gaffer.sketches.clearspring.cardinality.predicate.HyperLogLogPlusIsLessThan) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) IsEqual(uk.gov.gchq.koryphe.impl.predicate.IsEqual) Graph(uk.gov.gchq.gaffer.graph.Graph) HyperLogLogPlus(com.clearspring.analytics.stream.cardinality.HyperLogLogPlus) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed)

Example 2 with IsEqual

use of uk.gov.gchq.koryphe.impl.predicate.IsEqual in project gaffer-doc by gchq.

the class IsEqualExample method isEqualTo5.

public void isEqualTo5() {
    // ---------------------------------------------------------
    final IsEqual function = new IsEqual(5);
    // ---------------------------------------------------------
    runExample(function, null, 5, 5L, "5", '5');
}
Also used : IsEqual(uk.gov.gchq.koryphe.impl.predicate.IsEqual)

Example 3 with IsEqual

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

the class ElementFilterTest method shouldExecuteOrPredicates.

@Test
public void shouldExecuteOrPredicates() {
    final ElementFilter filter = new ElementFilter.Builder().select(TestPropertyNames.PROP_1, TestPropertyNames.PROP_2).execute(new Or.Builder<>().select(0).execute(new IsMoreThan(2)).select(1).execute(new IsEqual("some value")).build()).build();
    final Entity element1 = makeEntity(3, "some value");
    final Entity element2 = makeEntity(1, "some value");
    final Entity element3 = makeEntity(3, "some invalid value");
    final Entity element4 = makeEntity(1, "some invalid value");
    // When
    final boolean result1 = filter.test(element1);
    final boolean result2 = filter.test(element2);
    final boolean result3 = filter.test(element3);
    final boolean result4 = filter.test(element4);
    // Then
    assertTrue(result1);
    assertTrue(result2);
    assertTrue(result3);
    assertFalse(result4);
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) Or(uk.gov.gchq.koryphe.impl.predicate.Or) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) IsEqual(uk.gov.gchq.koryphe.impl.predicate.IsEqual) JSONSerialisationTest(uk.gov.gchq.gaffer.JSONSerialisationTest) Test(org.junit.jupiter.api.Test)

Example 4 with IsEqual

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

the class ElementFilterTest method shouldExecuteNotPredicates.

@Test
public void shouldExecuteNotPredicates() {
    final ElementFilter filter = new ElementFilter.Builder().select(TestPropertyNames.PROP_1, TestPropertyNames.PROP_2).execute(new Not<>(new Or.Builder<>().select(0).execute(new IsMoreThan(2)).select(1).execute(new IsEqual("some value")).build())).build();
    final Entity element1 = makeEntity(3, "some value");
    final Entity element2 = makeEntity(1, "some value");
    final Entity element3 = makeEntity(3, "some invalid value");
    final Entity element4 = makeEntity(1, "some invalid value");
    // When
    final boolean result1 = filter.test(element1);
    final boolean result2 = filter.test(element2);
    final boolean result3 = filter.test(element3);
    final boolean result4 = filter.test(element4);
    // Then
    assertFalse(result1);
    assertFalse(result2);
    assertFalse(result3);
    assertTrue(result4);
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) Not(uk.gov.gchq.koryphe.impl.predicate.Not) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) IsEqual(uk.gov.gchq.koryphe.impl.predicate.IsEqual) JSONSerialisationTest(uk.gov.gchq.gaffer.JSONSerialisationTest) Test(org.junit.jupiter.api.Test)

Example 5 with IsEqual

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

the class ViewValidatorTest method shouldValidateAndReturnTrueForOrFilter.

@Test
public void shouldValidateAndReturnTrueForOrFilter() {
    // Given
    final ViewValidator validator = new ViewValidator();
    final View view = new View.Builder().entity(TestGroups.ENTITY, new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select(TestPropertyNames.PROP_1, TestPropertyNames.PROP_2).execute(new Or.Builder().select(0).execute(new IsEqual("some value")).select(1).execute(new IsEqual("some other value")).build()).build()).build()).build();
    final Schema schema = new Schema.Builder().type("obj", Object.class).type("string", String.class).entity(TestGroups.ENTITY, new SchemaEntityDefinition.Builder().property(TestPropertyNames.PROP_1, "obj").property(TestPropertyNames.PROP_2, "string").build()).build();
    // When
    final ValidationResult result = validator.validate(view, schema, ALL_STORE_TRAITS);
    // Then
    assertTrue(result.isValid(), result.getErrorString());
}
Also used : Or(uk.gov.gchq.koryphe.impl.predicate.Or) ValidationResult(uk.gov.gchq.koryphe.ValidationResult) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) IsEqual(uk.gov.gchq.koryphe.impl.predicate.IsEqual) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) Test(org.junit.jupiter.api.Test)

Aggregations

IsEqual (uk.gov.gchq.koryphe.impl.predicate.IsEqual)32 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)20 Test (org.junit.jupiter.api.Test)18 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)16 Element (uk.gov.gchq.gaffer.data.element.Element)12 IsMoreThan (uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)10 Test (org.junit.Test)7 TraitRequirement (uk.gov.gchq.gaffer.integration.TraitRequirement)7 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)7 Entity (uk.gov.gchq.gaffer.data.element.Entity)6 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)6 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)6 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)6 ValidationResult (uk.gov.gchq.koryphe.ValidationResult)6 IsLessThan (uk.gov.gchq.koryphe.impl.predicate.IsLessThan)6 Or (uk.gov.gchq.koryphe.impl.predicate.Or)6 Graph (uk.gov.gchq.gaffer.graph.Graph)5 ElementId (uk.gov.gchq.gaffer.data.element.id.ElementId)4 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)4 User (uk.gov.gchq.gaffer.user.User)4