use of ai.grakn.concept.Entity in project grakn by graknlabs.
the class KCoreTest method addSchemaAndEntities.
private void addSchemaAndEntities() throws InvalidKBException {
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
EntityType entityType1 = graph.putEntityType(thing);
EntityType entityType2 = graph.putEntityType(anotherThing);
Role role1 = graph.putRole("role1");
Role role2 = graph.putRole("role2");
RelationshipType relationshipType1 = graph.putRelationshipType(related).relates(role1).relates(role2);
Role role3 = graph.putRole("role3");
Role role4 = graph.putRole("role4");
RelationshipType relationshipType2 = graph.putRelationshipType(veryRelated).relates(role3).relates(role4);
entityType1.plays(role1).plays(role2).plays(role3).plays(role4);
entityType2.plays(role1).plays(role2).plays(role3).plays(role4);
Entity entity1 = entityType1.addEntity();
Entity entity2 = entityType1.addEntity();
Entity entity3 = entityType1.addEntity();
Entity entity4 = entityType1.addEntity();
relationshipType1.addRelationship().addRolePlayer(role1, entity1).addRolePlayer(role2, entity2);
relationshipType1.addRelationship().addRolePlayer(role1, entity2).addRolePlayer(role2, entity3);
relationshipType1.addRelationship().addRolePlayer(role1, entity3).addRolePlayer(role2, entity4);
relationshipType1.addRelationship().addRolePlayer(role1, entity4).addRolePlayer(role2, entity1);
relationshipType2.addRelationship().addRolePlayer(role3, entity1).addRolePlayer(role4, entity3);
relationshipType2.addRelationship().addRolePlayer(role3, entity2).addRolePlayer(role4, entity4);
entityId1 = entity1.getId();
entityId2 = entity2.getId();
entityId3 = entity3.getId();
entityId4 = entity4.getId();
graph.commit();
}
}
use of ai.grakn.concept.Entity 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.Entity 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.Entity 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();
}
}
use of ai.grakn.concept.Entity in project grakn by graknlabs.
the class JsonMigratorTest method whenMigratorExecutedOverDataWithAllDataTypes_AllDataIsPersistedInGraph.
@Test
public void whenMigratorExecutedOverDataWithAllDataTypes_AllDataIsPersistedInGraph() throws FileNotFoundException {
load(factory, getFile("json", "all-types/schema.gql"));
String template = "" + "insert $x isa thingy\n" + " has a-boolean <a-boolean>\n" + " has a-number <a-number>\n" + " for (int in <array-of-ints> ) do {\n" + " has a-int <int>\n" + " }\n" + " has a-string <a-string>\n" + " if (<a-null> != null) do {has a-null <a-null>};";
declareAndLoad(template, "all-types/data.json");
try (GraknTx graph = factory.open(GraknTxType.READ)) {
EntityType rootType = graph.getEntityType("thingy");
Set<Entity> things = rootType.instances().collect(toSet());
assertEquals(1, things.size());
Entity thing = things.iterator().next();
Collection<Object> integers = getResources(graph, thing, Label.of("a-int")).map(Attribute::getValue).collect(toSet());
assertEquals(Sets.newHashSet(1L, 2L, 3L), integers);
Attribute aBoolean = getResource(graph, thing, Label.of("a-boolean"));
assertEquals(true, aBoolean.getValue());
Attribute aNumber = getResource(graph, thing, Label.of("a-number"));
assertEquals(42.1, aNumber.getValue());
Attribute aString = getResource(graph, thing, Label.of("a-string"));
assertEquals("hi", aString.getValue());
assertEquals(0, graph.getAttributeType("a-null").instances().count());
}
}
Aggregations