Search in sources :

Example 46 with RelationshipType

use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.

the class GraknTxTest method whenAttemptingToMutateReadOnlyGraph_Throw.

@Test
public void whenAttemptingToMutateReadOnlyGraph_Throw() {
    Keyspace keyspace = Keyspace.of("myreadonlygraph");
    String entityType = "My Entity Type";
    String roleType1 = "My Role Type 1";
    String roleType2 = "My Role Type 2";
    String relationType1 = "My Relationship Type 1";
    String relationType2 = "My Relationship Type 2";
    String resourceType = "My Attribute Type";
    // Fail Some Mutations
    tx = EmbeddedGraknSession.create(keyspace, Grakn.IN_MEMORY).open(GraknTxType.READ);
    failMutation(tx, () -> tx.putEntityType(entityType));
    failMutation(tx, () -> tx.putRole(roleType1));
    failMutation(tx, () -> tx.putRelationshipType(relationType1));
    // Pass some mutations
    tx.close();
    tx = EmbeddedGraknSession.create(keyspace, Grakn.IN_MEMORY).open(GraknTxType.WRITE);
    EntityType entityT = tx.putEntityType(entityType);
    entityT.addEntity();
    Role roleT1 = tx.putRole(roleType1);
    Role roleT2 = tx.putRole(roleType2);
    RelationshipType relationT1 = tx.putRelationshipType(relationType1).relates(roleT1);
    RelationshipType relationT2 = tx.putRelationshipType(relationType2).relates(roleT2);
    AttributeType<String> resourceT = tx.putAttributeType(resourceType, AttributeType.DataType.STRING);
    tx.commit();
    // Fail some mutations again
    tx = EmbeddedGraknSession.create(keyspace, Grakn.IN_MEMORY).open(GraknTxType.READ);
    failMutation(tx, entityT::addEntity);
    failMutation(tx, () -> resourceT.putAttribute("A resource"));
    failMutation(tx, () -> tx.putEntityType(entityType));
    failMutation(tx, () -> entityT.plays(roleT1));
    failMutation(tx, () -> relationT1.relates(roleT2));
    failMutation(tx, () -> relationT2.relates(roleT1));
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) Keyspace(ai.grakn.Keyspace) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 47 with RelationshipType

use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.

the class GrpcServerIT method whenDefiningASchema_TheSchemaIsDefined.

@Test
public void whenDefiningASchema_TheSchemaIsDefined() {
    try (GraknTx tx = remoteSession.open(GraknTxType.WRITE)) {
        EntityType animal = tx.putEntityType("animal");
        EntityType dog = tx.putEntityType("dog").sup(animal);
        EntityType cat = tx.putEntityType("cat");
        animal.sub(cat);
        cat.setLabel(Label.of("feline"));
        dog.setAbstract(true).setAbstract(false);
        cat.setAbstract(true);
        RelationshipType chases = tx.putRelationshipType("chases");
        Role chased = tx.putRole("chased");
        Role chaser = tx.putRole("chaser");
        chases.relates(chased).relates(chaser);
        Role pointlessRole = tx.putRole("pointless-role");
        tx.putRelationshipType("pointless").relates(pointlessRole);
        chases.relates(pointlessRole).deleteRelates(pointlessRole);
        dog.plays(chaser);
        cat.plays(chased);
        AttributeType<String> name = tx.putAttributeType("name", DataType.STRING);
        AttributeType<String> id = tx.putAttributeType("id", DataType.STRING).setRegex("(good|bad)-dog");
        AttributeType<Long> age = tx.putAttributeType("age", DataType.LONG);
        animal.attribute(name);
        animal.key(id);
        dog.attribute(age).deleteAttribute(age);
        cat.key(age).deleteKey(age);
        cat.plays(chaser).deletePlays(chaser);
        Entity dunstan = dog.addEntity();
        Attribute<String> dunstanId = id.putAttribute("good-dog");
        assertNotNull(dunstan.attributeRelationship(dunstanId));
        Attribute<String> dunstanName = name.putAttribute("Dunstan");
        dunstan.attribute(dunstanName).deleteAttribute(dunstanName);
        chases.addRelationship().addRolePlayer(chaser, dunstan);
        tx.commit();
    }
    try (GraknTx tx = localSession.open(GraknTxType.READ)) {
        EntityType animal = tx.getEntityType("animal");
        EntityType dog = tx.getEntityType("dog");
        EntityType cat = tx.getEntityType("feline");
        RelationshipType chases = tx.getRelationshipType("chases");
        Role chased = tx.getRole("chased");
        Role chaser = tx.getRole("chaser");
        AttributeType<String> name = tx.getAttributeType("name");
        AttributeType<String> id = tx.getAttributeType("id");
        Entity dunstan = Iterators.getOnlyElement(dog.instances().iterator());
        Relationship aChase = Iterators.getOnlyElement(chases.instances().iterator());
        assertEquals(animal, dog.sup());
        assertEquals(animal, cat.sup());
        assertEquals(ImmutableSet.of(chased, chaser), chases.relates().collect(toSet()));
        assertEquals(ImmutableSet.of(chaser), dog.plays().filter(role -> !role.isImplicit()).collect(toSet()));
        assertEquals(ImmutableSet.of(chased), cat.plays().filter(role -> !role.isImplicit()).collect(toSet()));
        assertEquals(ImmutableSet.of(name, id), animal.attributes().collect(toSet()));
        assertEquals(ImmutableSet.of(id), animal.keys().collect(toSet()));
        assertEquals(ImmutableSet.of(name, id), dog.attributes().collect(toSet()));
        assertEquals(ImmutableSet.of(id), dog.keys().collect(toSet()));
        assertEquals(ImmutableSet.of(name, id), cat.attributes().collect(toSet()));
        assertEquals(ImmutableSet.of(id), cat.keys().collect(toSet()));
        assertEquals("good-dog", Iterables.getOnlyElement(dunstan.keys(id).collect(toSet())).getValue());
        ImmutableMap<Role, ImmutableSet<?>> expectedRolePlayers = ImmutableMap.of(chaser, ImmutableSet.of(dunstan), chased, ImmutableSet.of());
        assertEquals(expectedRolePlayers, aChase.allRolePlayers());
        assertEquals("(good|bad)-dog", id.getRegex());
        assertFalse(dog.isAbstract());
        assertTrue(cat.isAbstract());
    }
}
Also used : Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType) EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) ImmutableSet(com.google.common.collect.ImmutableSet) Relationship(ai.grakn.concept.Relationship) Test(org.junit.Test)

Example 48 with RelationshipType

use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.

the class PathTreeKB method buildTree.

void buildTree(GraknTx tx, Role fromRole, Role toRole, int n, int children) {
    long startTime = System.currentTimeMillis();
    EntityType vertex = tx.getEntityType("vertex");
    EntityType startVertex = tx.getEntityType("start-vertex");
    RelationshipType arc = tx.getRelationshipType("arc");
    putEntityWithResource(tx, "a0", startVertex, getKey());
    int outputThreshold = 500;
    for (int i = 1; i <= n; i++) {
        int m = IntMath.pow(children, i);
        for (int j = 0; j < m; j++) {
            putEntityWithResource(tx, "a" + i + "," + j, vertex, getKey());
            if (j != 0 && j % outputThreshold == 0) {
                System.out.println(j + " entities out of " + m + " inserted");
            }
        }
    }
    for (int j = 0; j < children; j++) {
        arc.addRelationship().addRolePlayer(fromRole, getInstance(tx, "a0")).addRolePlayer(toRole, getInstance(tx, "a1," + j));
    }
    for (int i = 1; i < n; i++) {
        int m = IntMath.pow(children, i);
        for (int j = 0; j < m; j++) {
            for (int c = 0; c < children; c++) {
                arc.addRelationship().addRolePlayer(fromRole, getInstance(tx, "a" + i + "," + j)).addRolePlayer(toRole, getInstance(tx, "a" + (i + 1) + "," + (j * children + c)));
            }
            if (j != 0 && j % outputThreshold == 0) {
                System.out.println("level " + i + "/" + (n - 1) + ": " + j + " entities out of " + m + " connected");
            }
        }
    }
    long loadTime = System.currentTimeMillis() - startTime;
    System.out.println("PathKB loading time: " + loadTime + " ms");
}
Also used : EntityType(ai.grakn.concept.EntityType) RelationshipType(ai.grakn.concept.RelationshipType)

Example 49 with RelationshipType

use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.

the class DiagonalKB method buildExtensionalDB.

private void buildExtensionalDB(GraknTx tx, int n, int m) {
    Role relFrom = tx.getRole("rel-from");
    Role relTo = tx.getRole("rel-to");
    EntityType entity1 = tx.getEntityType("entity1");
    RelationshipType horizontal = tx.getRelationshipType("horizontal");
    RelationshipType vertical = tx.getRelationshipType("vertical");
    ConceptId[][] instanceIds = new ConceptId[n][m];
    long inserts = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            instanceIds[i][j] = putEntityWithResource(tx, "a" + i + "," + j, entity1, key).getId();
            inserts++;
            if (inserts % 100 == 0)
                System.out.println("inst inserts: " + inserts);
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (i < n - 1) {
                vertical.addRelationship().addRolePlayer(relFrom, tx.getConcept(instanceIds[i][j])).addRolePlayer(relTo, tx.getConcept(instanceIds[i + 1][j]));
                inserts++;
            }
            if (j < m - 1) {
                horizontal.addRelationship().addRolePlayer(relFrom, tx.getConcept(instanceIds[i][j])).addRolePlayer(relTo, tx.getConcept(instanceIds[i][j + 1]));
                inserts++;
            }
            if (inserts % 100 == 0)
                System.out.println("rel inserts: " + inserts);
        }
    }
    System.out.println("Extensional DB loaded.");
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) RelationshipType(ai.grakn.concept.RelationshipType) ConceptId(ai.grakn.concept.ConceptId)

Example 50 with RelationshipType

use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.

the class NguyenKB method buildExtensionalDB.

private void buildExtensionalDB(GraknTx graph, int n) {
    Role Rfrom = graph.getRole("R-rA");
    Role Rto = graph.getRole("R-rB");
    Role qfrom = graph.getRole("Q-rA");
    Role qto = graph.getRole("Q-rB");
    Role Pfrom = graph.getRole("P-rA");
    Role Pto = graph.getRole("P-rB");
    EntityType entity = graph.getEntityType("entity2");
    EntityType aEntity = graph.getEntityType("a-entity");
    EntityType bEntity = graph.getEntityType("b-entity");
    RelationshipType r = graph.getRelationshipType("R");
    RelationshipType p = graph.getRelationshipType("P");
    RelationshipType q = graph.getRelationshipType("Q");
    ConceptId cId = putEntityWithResource(graph, "c", entity, key).getId();
    ConceptId dId = putEntityWithResource(graph, "d", entity, key).getId();
    ConceptId eId = putEntityWithResource(graph, "e", entity, key).getId();
    ConceptId[] aInstancesIds = new ConceptId[n + 2];
    ConceptId[] bInstancesIds = new ConceptId[n + 2];
    aInstancesIds[n + 1] = putEntityWithResource(graph, "a" + (n + 1), aEntity, key).getId();
    for (int i = 0; i <= n; i++) {
        aInstancesIds[i] = putEntityWithResource(graph, "a" + i, aEntity, key).getId();
        bInstancesIds[i] = putEntityWithResource(graph, "b" + i, bEntity, key).getId();
    }
    p.addRelationship().addRolePlayer(Pfrom, graph.getConcept(cId)).addRolePlayer(Pto, graph.getConcept(dId));
    r.addRelationship().addRolePlayer(Rfrom, graph.getConcept(dId)).addRolePlayer(Rto, graph.getConcept(eId));
    q.addRelationship().addRolePlayer(qfrom, graph.getConcept(eId)).addRolePlayer(qto, graph.getConcept(aInstancesIds[0]));
    for (int i = 0; i <= n; i++) {
        p.addRelationship().addRolePlayer(Pfrom, graph.getConcept(bInstancesIds[i])).addRolePlayer(Pto, graph.getConcept(cId));
        p.addRelationship().addRolePlayer(Pfrom, graph.getConcept(cId)).addRolePlayer(Pto, graph.getConcept(bInstancesIds[i]));
        q.addRelationship().addRolePlayer(qfrom, graph.getConcept(aInstancesIds[i])).addRolePlayer(qto, graph.getConcept(bInstancesIds[i]));
        q.addRelationship().addRolePlayer(qfrom, graph.getConcept(bInstancesIds[i])).addRolePlayer(qto, graph.getConcept(aInstancesIds[i + 1]));
    }
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) RelationshipType(ai.grakn.concept.RelationshipType) ConceptId(ai.grakn.concept.ConceptId)

Aggregations

RelationshipType (ai.grakn.concept.RelationshipType)127 Role (ai.grakn.concept.Role)105 Test (org.junit.Test)91 EntityType (ai.grakn.concept.EntityType)80 Entity (ai.grakn.concept.Entity)52 GraknTx (ai.grakn.GraknTx)39 Relationship (ai.grakn.concept.Relationship)25 ConceptId (ai.grakn.concept.ConceptId)23 Label (ai.grakn.concept.Label)21 HashSet (java.util.HashSet)20 Set (java.util.Set)20 AttributeType (ai.grakn.concept.AttributeType)17 Thing (ai.grakn.concept.Thing)17 Attribute (ai.grakn.concept.Attribute)16 Schema (ai.grakn.util.Schema)12 Collectors (java.util.stream.Collectors)12 List (java.util.List)11 GraknSession (ai.grakn.GraknSession)10 GraknTxType (ai.grakn.GraknTxType)10 Concept (ai.grakn.concept.Concept)10