use of ai.grakn.concept.Thing 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.Thing in project grakn by graknlabs.
the class RelationshipConverter method pattern.
public Pattern pattern(Relationship concept) {
VarPattern relationPattern = Graql.var();
Set<Pattern> idPatterns = new HashSet<>();
for (Map.Entry<Role, Set<Thing>> entry : concept.allRolePlayers().entrySet()) {
for (Thing var : entry.getValue()) {
Var rolePlayer = Graql.var();
relationPattern = relationPattern.rel(Graql.label(entry.getKey().getLabel()), rolePlayer);
idPatterns.add(rolePlayer.asUserDefined().id(var.getId()));
}
}
relationPattern = relationPattern.isa(Graql.label(concept.type().getLabel()));
Pattern pattern = relationPattern;
for (Pattern idPattern : idPatterns) {
pattern = pattern.and(idPattern);
}
return pattern;
}
use of ai.grakn.concept.Thing in project grakn by graknlabs.
the class AttributeTest method whenAttachingResourcesToInstances_EnsureInstancesAreReturnedAsOwners.
@Test
public void whenAttachingResourcesToInstances_EnsureInstancesAreReturnedAsOwners() throws Exception {
EntityType randomThing = tx.putEntityType("A Thing");
AttributeType<String> attributeType = tx.putAttributeType("A Attribute Thing", AttributeType.DataType.STRING);
RelationshipType hasResource = tx.putRelationshipType("Has Attribute");
Role resourceRole = tx.putRole("Attribute Role");
Role actorRole = tx.putRole("Actor");
Thing pacino = randomThing.addEntity();
Thing jennifer = randomThing.addEntity();
Thing bob = randomThing.addEntity();
Thing alice = randomThing.addEntity();
Attribute<String> birthDate = attributeType.putAttribute("10/10/10");
hasResource.relates(resourceRole).relates(actorRole);
assertThat(birthDate.ownerInstances().collect(toSet()), empty());
hasResource.addRelationship().addRolePlayer(resourceRole, birthDate).addRolePlayer(actorRole, pacino);
hasResource.addRelationship().addRolePlayer(resourceRole, birthDate).addRolePlayer(actorRole, jennifer);
hasResource.addRelationship().addRolePlayer(resourceRole, birthDate).addRolePlayer(actorRole, bob);
hasResource.addRelationship().addRolePlayer(resourceRole, birthDate).addRolePlayer(actorRole, alice);
assertThat(birthDate.ownerInstances().collect(toSet()), containsInAnyOrder(pacino, jennifer, bob, alice));
}
use of ai.grakn.concept.Thing in project grakn by graknlabs.
the class TransitivityMatrixKB method buildExtensionalDB.
private void buildExtensionalDB(GraknTx graph, int n, int m) {
Role qfrom = graph.getRole("Q-from");
Role qto = graph.getRole("Q-to");
EntityType aEntity = graph.getEntityType("a-entity");
RelationshipType q = graph.getRelationshipType("Q");
Thing aInst = putEntityWithResource(graph, "a", graph.getEntityType("entity2"), key);
ConceptId[][] aInstanceIds = new ConceptId[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
aInstanceIds[i][j] = putEntityWithResource(graph, "a" + i + "," + j, aEntity, key).getId();
}
}
q.addRelationship().addRolePlayer(qfrom, aInst).addRolePlayer(qto, graph.getConcept(aInstanceIds[0][0]));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i < n - 1) {
q.addRelationship().addRolePlayer(qfrom, graph.getConcept(aInstanceIds[i][j])).addRolePlayer(qto, graph.getConcept(aInstanceIds[i + 1][j]));
}
if (j < m - 1) {
q.addRelationship().addRolePlayer(qfrom, graph.getConcept(aInstanceIds[i][j])).addRolePlayer(qto, graph.getConcept(aInstanceIds[i][j + 1]));
}
}
}
}
use of ai.grakn.concept.Thing in project grakn by graknlabs.
the class ConceptTest method checkToStringHasMinimalInformation.
@Test
public void checkToStringHasMinimalInformation() {
EntityType concept = tx.putEntityType("a");
Thing entity = concept.addEntity();
assertTrue(entity.toString().contains(Schema.BaseType.ENTITY.name()));
assertTrue(entity.toString().contains(entity.getId().getValue()));
}
Aggregations