use of ai.grakn.concept.Entity in project grakn by graknlabs.
the class JsonMigratorTest method whenMigratorExecutedOverSimpleJson_DataIsPersistedInGraph.
@Test
public void whenMigratorExecutedOverSimpleJson_DataIsPersistedInGraph() {
load(factory, getFile("json", "simple-schema/schema.gql"));
String template = " \n" + "insert " + "$person isa person;\n" + " \n" + "$address isa address\n" + " has city <address.city>;\n" + "\n" + "$street isa street-address\n" + " has street <address.streetAddress.street>\n" + " has number <address.streetAddress.number>;\n" + "\n" + "(address-with-street: $address, street-of-address: $street) isa address-has-street;\n" + "\n" + "(person-with-address: $person, address-of-person: $address) isa has-address;\n" + "\n" + "for ( <phoneNumber> ) do {\n" + " $phone isa phone-number\n" + " has location <location>\n" + " has code <code>;\n" + " \n" + " (person-with-phone: $person, phone-of-person: $phone) isa has-phone;\n" + " \n" + "} ";
declareAndLoad(template, "simple-schema/data.json");
try (GraknTx graph = factory.open(GraknTxType.READ)) {
EntityType personType = graph.getEntityType("person");
assertEquals(1, personType.instances().count());
Entity person = personType.instances().iterator().next();
Entity address = getProperty(graph, person, "has-address").asEntity();
Entity streetAddress = getProperty(graph, address, "address-has-street").asEntity();
Attribute number = getResource(graph, streetAddress, Label.of("number"));
assertEquals(21L, number.getValue());
Attribute street = getResource(graph, streetAddress, Label.of("street"));
assertEquals("2nd Street", street.getValue());
Attribute city = getResource(graph, address, Label.of("city")).asAttribute();
assertEquals("New York", city.getValue());
Collection<Thing> phoneNumbers = getProperties(graph, person, "has-phone");
assertEquals(2, phoneNumbers.size());
boolean phoneNumbersCorrect = phoneNumbers.stream().allMatch(phoneNumber -> {
Object location = getResource(graph, phoneNumber, Label.of("location")).getValue();
Object code = getResource(graph, phoneNumber, Label.of("code")).getValue();
return ((location.equals("home") && code.equals(44L)) || (location.equals("work") && code.equals(45L)));
});
assertTrue(phoneNumbersCorrect);
}
}
use of ai.grakn.concept.Entity in project grakn by graknlabs.
the class CorenessTest 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 = entityType2.addEntity();
Entity entity4 = entityType2.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 CorenessTest method testOnGraphWithTwoEntitiesAndTwoRelationships.
@Test
public void testOnGraphWithTwoEntitiesAndTwoRelationships() {
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
EntityType entityType = graph.putEntityType(thing);
Entity entity1 = entityType.addEntity();
Entity entity2 = entityType.addEntity();
Role role1 = graph.putRole("role1");
Role role2 = graph.putRole("role2");
entityType.plays(role1).plays(role2);
graph.putRelationshipType(related).relates(role1).relates(role2).addRelationship().addRolePlayer(role1, entity1).addRolePlayer(role2, entity2);
Role role3 = graph.putRole("role3");
Role role4 = graph.putRole("role4");
entityType.plays(role3).plays(role4);
graph.putRelationshipType(veryRelated).relates(role3).relates(role4).addRelationship().addRolePlayer(role3, entity1).addRolePlayer(role4, entity2);
Map<Long, Set<String>> result = graph.graql().compute().centrality().usingKCore().execute();
assertTrue(result.isEmpty());
}
}
use of ai.grakn.concept.Entity in project grakn by graknlabs.
the class EdgeTest method checkEqualityBetweenEdgesBasedOnID.
@Test
public void checkEqualityBetweenEdgesBasedOnID() {
Entity entity2 = entityType.addEntity();
Edge tinkerEdge = tx.getTinkerTraversal().V().has(Schema.VertexProperty.ID.name(), entity2.getId().getValue()).outE().next();
EdgeElement edge2 = new EdgeElement(tx, tinkerEdge);
assertEquals(edge, edge);
assertNotEquals(edge, edge2);
}
use of ai.grakn.concept.Entity in project grakn by graknlabs.
the class AttributeTest method whenGettingTheRelationsOfResources_EnsureIncomingResourceEdgesAreTakingIntoAccount.
@Test
public void whenGettingTheRelationsOfResources_EnsureIncomingResourceEdgesAreTakingIntoAccount() {
AttributeType<String> attributeType = tx.putAttributeType("Attribute Type Thingy", AttributeType.DataType.STRING);
Attribute<String> attribute = attributeType.putAttribute("Thingy");
EntityType entityType = tx.putEntityType("Entity Type Thingy").key(attributeType);
Entity e1 = entityType.addEntity();
Entity e2 = entityType.addEntity();
assertThat(attribute.relationships().collect(toSet()), empty());
e1.attribute(attribute);
e2.attribute(attribute);
Relationship rel1 = Iterables.getOnlyElement(e1.relationships().collect(toSet()));
Relationship rel2 = Iterables.getOnlyElement(e2.relationships().collect(toSet()));
assertThat(attribute.relationships().collect(toSet()), containsInAnyOrder(rel1, rel2));
}
Aggregations