use of ai.grakn.GraknTx 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);
}
}
use of ai.grakn.GraknTx in project grakn by graknlabs.
the class GraqlTest method testAnalyticsDoesNotCommitByMistake.
@Test
public void testAnalyticsDoesNotCommitByMistake() throws InvalidKBException {
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
graph.putAttributeType("number", AttributeType.DataType.LONG);
graph.commit();
}
Set<String> analyticsCommands = new HashSet<>(Arrays.asList("compute count;", "compute degrees;", "compute mean of number;"));
analyticsCommands.forEach(command -> {
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
// insert a node but do not commit it
graph.graql().parse("define thingy sub entity;").execute();
// use analytics
graph.graql().parse(command).execute();
}
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
// see if the node was commited
assertNull(graph.getEntityType("thingy"));
}
});
}
use of ai.grakn.GraknTx in project grakn by graknlabs.
the class AnalyticsTest method testSubgraphContainingRoleDoesNotBreakAnalytics.
@Test
public void testSubgraphContainingRoleDoesNotBreakAnalytics() {
expectedEx.expect(GraqlQueryException.class);
expectedEx.expectMessage(GraqlQueryException.labelNotFound(Label.of("role")).getMessage());
try (GraknTx graph = session.open(GraknTxType.READ)) {
graph.graql().compute().count().in("role").execute();
}
}
use of ai.grakn.GraknTx in project grakn by graknlabs.
the class AnalyticsTest method addSchemaAndEntities.
private void addSchemaAndEntities() throws InvalidKBException {
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
EntityType entityType1 = graph.putEntityType(thingy);
EntityType entityType2 = graph.putEntityType(anotherThing);
Entity entity1 = entityType1.addEntity();
Entity entity2 = entityType1.addEntity();
Entity entity3 = entityType1.addEntity();
Entity entity4 = entityType2.addEntity();
entityId1 = entity1.getId().getValue();
entityId2 = entity2.getId().getValue();
entityId3 = entity3.getId().getValue();
entityId4 = entity4.getId().getValue();
Role role1 = graph.putRole("role1");
Role role2 = graph.putRole("role2");
entityType1.plays(role1).plays(role2);
entityType2.plays(role1).plays(role2);
RelationshipType relationshipType = graph.putRelationshipType(related).relates(role1).relates(role2);
relationId12 = relationshipType.addRelationship().addRolePlayer(role1, entity1).addRolePlayer(role2, entity2).getId().getValue();
relationId24 = relationshipType.addRelationship().addRolePlayer(role1, entity2).addRolePlayer(role2, entity4).getId().getValue();
graph.commit();
}
}
use of ai.grakn.GraknTx in project grakn by graknlabs.
the class AnalyticsTest method testNullResourceDoesNotBreakAnalytics.
@Test
public void testNullResourceDoesNotBreakAnalytics() throws InvalidKBException {
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
// make slightly odd graph
Label resourceTypeId = Label.of("degree");
EntityType thingy = graph.putEntityType("thingy");
AttributeType<Long> attribute = graph.putAttributeType(resourceTypeId, AttributeType.DataType.LONG);
thingy.attribute(attribute);
Role degreeOwner = graph.getRole(Schema.ImplicitType.HAS_OWNER.getLabel(resourceTypeId).getValue());
Role degreeValue = graph.getRole(Schema.ImplicitType.HAS_VALUE.getLabel(resourceTypeId).getValue());
RelationshipType relationshipType = graph.putRelationshipType(Schema.ImplicitType.HAS.getLabel(resourceTypeId)).relates(degreeOwner).relates(degreeValue);
thingy.plays(degreeOwner);
Entity thisThing = thingy.addEntity();
relationshipType.addRelationship().addRolePlayer(degreeOwner, thisThing);
graph.commit();
}
// the null role-player caused analytics to fail at some stage
try (GraknTx graph = session.open(GraknTxType.READ)) {
graph.graql().compute().centrality().usingDegree().execute();
} catch (RuntimeException e) {
e.printStackTrace();
fail();
}
}
Aggregations