use of ai.grakn.concept.EntityType in project grakn by graknlabs.
the class CountTest method testHasResourceEdges.
@Test
public void testHasResourceEdges() {
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
EntityType person = graph.putEntityType("person");
AttributeType<String> name = graph.putAttributeType("name", AttributeType.DataType.STRING);
person.attribute(name);
Entity aPerson = person.addEntity();
aPerson.attribute(name.putAttribute("jason"));
graph.commit();
}
long count;
try (GraknTx graph = session.open(GraknTxType.READ)) {
count = graph.graql().compute().count().execute();
assertEquals(1L, count);
count = graph.graql().compute().count().includeAttribute().execute();
assertEquals(3L, count);
count = graph.graql().compute().count().in("name").execute();
assertEquals(1L, count);
count = graph.graql().compute().count().in("@has-name").execute();
assertEquals(1L, count);
count = graph.graql().compute().count().in("@has-name", "thing").execute();
assertEquals(3L, count);
count = graph.graql().compute().count().in("@has-name", "name").execute();
assertEquals(2L, count);
count = graph.graql().compute().count().in("relationship").execute();
assertEquals(0L, count);
count = graph.graql().compute().count().in("relationship").includeAttribute().execute();
assertEquals(1L, count);
}
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
// manually construct the relation type and instance
EntityType person = graph.getEntityType("person");
Entity aPerson = person.addEntity();
AttributeType<String> name = graph.putAttributeType("name", AttributeType.DataType.STRING);
Attribute jason = name.putAttribute("jason");
Role resourceOwner = graph.putRole(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of("name")));
person.plays(resourceOwner);
Role resourceValue = graph.putRole(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of("name")));
name.plays(resourceValue);
RelationshipType relationshipType = graph.putRelationshipType(Schema.ImplicitType.HAS.getLabel(Label.of("name"))).relates(resourceOwner).relates(resourceValue);
relationshipType.addRelationship().addRolePlayer(resourceOwner, aPerson).addRolePlayer(resourceValue, jason);
graph.commit();
}
try (GraknTx graph = session.open(GraknTxType.READ)) {
count = graph.graql().compute().count().execute();
assertEquals(2L, count);
count = graph.graql().compute().count().includeAttribute().execute();
assertEquals(5L, count);
count = graph.graql().compute().count().in("name").execute();
assertEquals(1L, count);
count = graph.graql().compute().count().includeAttribute().in("name").execute();
assertEquals(1L, count);
count = graph.graql().compute().count().in("@has-name").execute();
assertEquals(2L, count);
count = graph.graql().compute().count().in("@has-name", "thing").execute();
assertEquals(5L, count);
count = graph.graql().compute().count().in("@has-name", "name").execute();
assertEquals(3L, count);
count = graph.graql().compute().count().in("relationship").execute();
assertEquals(0L, count);
count = graph.graql().compute().count().in("relationship").includeAttribute().execute();
assertEquals(2L, count);
}
}
use of ai.grakn.concept.EntityType in project grakn by graknlabs.
the class CountTest method testConcurrentCount.
@Test
public void testConcurrentCount() throws Exception {
assumeFalse(GraknTestUtil.usingTinker());
String nameThing = "thingy";
String nameAnotherThing = "another";
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
EntityType thingy = graph.putEntityType(nameThing);
thingy.addEntity();
thingy.addEntity();
EntityType anotherThing = graph.putEntityType(nameAnotherThing);
anotherThing.addEntity();
graph.commit();
}
List<Long> list = new ArrayList<>(4);
long workerNumber = 6L;
for (long i = 0L; i < workerNumber; i++) {
list.add(i);
}
// running 4 jobs at the same time
// collecting the result in the end so engine won't stop before the test finishes
Set<Long> result;
result = list.parallelStream().map(i -> executeCount(session)).collect(Collectors.toSet());
Assert.assertEquals(1, result.size());
Assert.assertEquals(3L, result.iterator().next().longValue());
result = list.parallelStream().map(i -> executeCount(session)).collect(Collectors.toSet());
Assert.assertEquals(1, result.size());
Assert.assertEquals(3L, result.iterator().next().longValue());
}
use of ai.grakn.concept.EntityType in project grakn by graknlabs.
the class CountTest method testCountAfterCommit.
@Test
public void testCountAfterCommit() throws Exception {
String nameThing = "thingy";
String nameAnotherThing = "another";
// assert the graph is empty
try (GraknTx graph = session.open(GraknTxType.READ)) {
Assert.assertEquals(0L, Graql.compute().count().withTx(graph).execute().longValue());
Assert.assertEquals(0L, graph.graql().compute().count().includeAttribute().execute().longValue());
}
// add 2 instances
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
EntityType thingy = graph.putEntityType(nameThing);
thingy.addEntity();
thingy.addEntity();
graph.commit();
}
try (GraknTx graph = session.open(GraknTxType.READ)) {
Assert.assertEquals(2L, Graql.compute().withTx(graph).count().in(nameThing).execute().longValue());
}
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
EntityType anotherThing = graph.putEntityType(nameAnotherThing);
anotherThing.addEntity();
graph.commit();
}
try (GraknTx graph = session.open(GraknTxType.READ)) {
// assert computer returns the correct count of instances
Assert.assertEquals(2L, Graql.compute().withTx(graph).count().in(nameThing).includeAttribute().execute().longValue());
Assert.assertEquals(3L, graph.graql().compute().count().execute().longValue());
}
}
use of ai.grakn.concept.EntityType in project grakn by graknlabs.
the class CountTest method testHasResourceVerticesAndEdges.
@Test
public void testHasResourceVerticesAndEdges() {
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
// manually construct the relation type and instance
EntityType person = graph.putEntityType("person");
Entity aPerson = person.addEntity();
AttributeType<String> name = graph.putAttributeType("name", AttributeType.DataType.STRING);
Attribute jason = name.putAttribute("jason");
person.attribute(name);
Role resourceOwner = graph.putRole(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of("name")));
person.plays(resourceOwner);
Role resourceValue = graph.putRole(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of("name")));
name.plays(resourceValue);
RelationshipType relationshipType = graph.putRelationshipType(Schema.ImplicitType.HAS.getLabel(Label.of("name"))).relates(resourceOwner).relates(resourceValue);
// here relationship type is still implicit
relationshipType.addRelationship().addRolePlayer(resourceOwner, aPerson).addRolePlayer(resourceValue, jason);
graph.commit();
}
long count;
try (GraknTx graph = session.open(GraknTxType.READ)) {
count = graph.graql().compute().count().execute();
assertEquals(1L, count);
count = graph.graql().compute().count().includeAttribute().execute();
assertEquals(3L, count);
count = graph.graql().compute().count().in("name").execute();
assertEquals(1L, count);
count = graph.graql().compute().count().in("@has-name").execute();
assertEquals(1L, count);
count = graph.graql().compute().count().in("@has-name", "name").execute();
assertEquals(2L, count);
count = graph.graql().compute().count().in("relationship").execute();
assertEquals(0L, count);
count = graph.graql().compute().count().in("relationship").includeAttribute().execute();
assertEquals(1L, count);
}
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
EntityType person = graph.getEntityType("person");
AttributeType<String> name = graph.putAttributeType("name", AttributeType.DataType.STRING);
Entity aPerson = person.addEntity();
aPerson.attribute(name.getAttribute("jason"));
graph.commit();
}
try (GraknTx graph = session.open(GraknTxType.READ)) {
count = graph.graql().compute().count().includeAttribute().execute();
assertEquals(5L, count);
count = graph.graql().compute().count().in("name").execute();
assertEquals(1L, count);
count = graph.graql().compute().count().in("@has-name").execute();
assertEquals(2L, count);
count = graph.graql().compute().count().in("@has-name", "name").execute();
assertEquals(3L, count);
count = graph.graql().compute().count().in("relationship").execute();
assertEquals(0L, count);
count = graph.graql().compute().count().in("relationship").includeAttribute().execute();
assertEquals(2L, count);
}
}
use of ai.grakn.concept.EntityType in project grakn by graknlabs.
the class GraqlTest 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();
}
}
Aggregations