Search in sources :

Example 1 with MeanQuery

use of ai.grakn.graql.analytics.MeanQuery in project grakn by graknlabs.

the class GraqlTest method testStatisticsMethods.

@Test
public void testStatisticsMethods() throws InvalidKBException {
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        Label resourceTypeId = Label.of("my-resource");
        AttributeType<Long> resource = graph.putAttributeType(resourceTypeId, AttributeType.DataType.LONG);
        EntityType thingy = graph.putEntityType("thingy");
        thingy.attribute(resource);
        Entity theResourceOwner = thingy.addEntity();
        Role resourceOwner = graph.getRole(Schema.ImplicitType.HAS_OWNER.getLabel(resourceTypeId).getValue());
        Role resourceValue = graph.getRole(Schema.ImplicitType.HAS_VALUE.getLabel(resourceTypeId).getValue());
        RelationshipType relationshipType = graph.getRelationshipType(Schema.ImplicitType.HAS.getLabel(resourceTypeId).getValue());
        relationshipType.addRelationship().addRolePlayer(resourceOwner, theResourceOwner).addRolePlayer(resourceValue, resource.putAttribute(1L));
        relationshipType.addRelationship().addRolePlayer(resourceOwner, theResourceOwner).addRolePlayer(resourceValue, resource.putAttribute(2L));
        relationshipType.addRelationship().addRolePlayer(resourceOwner, theResourceOwner).addRolePlayer(resourceValue, resource.putAttribute(3L));
        graph.commit();
    }
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        // use graql to compute various statistics
        Optional<? extends Number> result = graph.graql().<SumQuery>parse("compute sum of my-resource;").execute();
        assertEquals(Optional.of(6L), result);
        result = graph.graql().<MinQuery>parse("compute min of my-resource;").execute();
        assertEquals(Optional.of(1L), result);
        result = graph.graql().<MaxQuery>parse("compute max of my-resource;").execute();
        assertEquals(Optional.of(3L), result);
        result = graph.graql().<MeanQuery>parse("compute mean of my-resource;").execute();
        assert result.isPresent();
        assertEquals(2.0, (Double) result.get(), 0.1);
        result = graph.graql().<MedianQuery>parse("compute median of my-resource;").execute();
        assertEquals(Optional.of(2L), result);
    }
}
Also used : Entity(ai.grakn.concept.Entity) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) MedianQuery(ai.grakn.graql.analytics.MedianQuery) MeanQuery(ai.grakn.graql.analytics.MeanQuery) MaxQuery(ai.grakn.graql.analytics.MaxQuery) EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) MinQuery(ai.grakn.graql.analytics.MinQuery) Test(org.junit.Test)

Aggregations

GraknTx (ai.grakn.GraknTx)1 Entity (ai.grakn.concept.Entity)1 EntityType (ai.grakn.concept.EntityType)1 Label (ai.grakn.concept.Label)1 RelationshipType (ai.grakn.concept.RelationshipType)1 Role (ai.grakn.concept.Role)1 MaxQuery (ai.grakn.graql.analytics.MaxQuery)1 MeanQuery (ai.grakn.graql.analytics.MeanQuery)1 MedianQuery (ai.grakn.graql.analytics.MedianQuery)1 MinQuery (ai.grakn.graql.analytics.MinQuery)1 Test (org.junit.Test)1