Search in sources :

Example 21 with Thing

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);
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) Thing(ai.grakn.concept.Thing) Test(org.junit.Test)

Example 22 with Thing

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;
}
Also used : Role(ai.grakn.concept.Role) VarPattern(ai.grakn.graql.VarPattern) Pattern(ai.grakn.graql.Pattern) HashSet(java.util.HashSet) Set(java.util.Set) Var(ai.grakn.graql.Var) VarPattern(ai.grakn.graql.VarPattern) Map(java.util.Map) Thing(ai.grakn.concept.Thing) HashSet(java.util.HashSet)

Example 23 with Thing

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));
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) RelationshipType(ai.grakn.concept.RelationshipType) Thing(ai.grakn.concept.Thing) Test(org.junit.Test)

Example 24 with Thing

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]));
            }
        }
    }
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) RelationshipType(ai.grakn.concept.RelationshipType) Thing(ai.grakn.concept.Thing) ConceptId(ai.grakn.concept.ConceptId)

Example 25 with Thing

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()));
}
Also used : EntityType(ai.grakn.concept.EntityType) Thing(ai.grakn.concept.Thing) Test(org.junit.Test)

Aggregations

Thing (ai.grakn.concept.Thing)47 Role (ai.grakn.concept.Role)30 Test (org.junit.Test)29 RelationshipType (ai.grakn.concept.RelationshipType)17 EntityType (ai.grakn.concept.EntityType)14 Relationship (ai.grakn.concept.Relationship)12 HashSet (java.util.HashSet)9 Set (java.util.Set)9 GraknTx (ai.grakn.GraknTx)7 Attribute (ai.grakn.concept.Attribute)7 ConceptId (ai.grakn.concept.ConceptId)7 AttributeType (ai.grakn.concept.AttributeType)6 Concept (ai.grakn.concept.Concept)6 Entity (ai.grakn.concept.Entity)6 Collectors (java.util.stream.Collectors)5 Label (ai.grakn.concept.Label)4 Schema (ai.grakn.util.Schema)4 Collection (java.util.Collection)4 Map (java.util.Map)4 GraknSession (ai.grakn.GraknSession)3