use of ai.grakn.graql.analytics.MedianQuery 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);
}
}
Aggregations