Search in sources :

Example 21 with ConceptId

use of ai.grakn.concept.ConceptId 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 22 with ConceptId

use of ai.grakn.concept.ConceptId 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)

Example 23 with ConceptId

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

the class ShortestPathTest method testMultiplePathsSharing1Instance.

@Test
public void testMultiplePathsSharing1Instance() throws InvalidKBException {
    ConceptId startId;
    ConceptId endId;
    Set<List<ConceptId>> correctPaths = new HashSet<>();
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType entityType = graph.putEntityType(thing);
        Role role1 = graph.putRole("role1");
        Role role2 = graph.putRole("role2");
        entityType.plays(role1).plays(role2);
        RelationshipType relationshipType1 = graph.putRelationshipType(related).relates(role1).relates(role2);
        Role role3 = graph.putRole("role3");
        Role role4 = graph.putRole("role4");
        entityType.plays(role3).plays(role4);
        RelationshipType relationshipType2 = graph.putRelationshipType(veryRelated).relates(role3).relates(role4);
        Entity start = entityType.addEntity();
        Entity end = entityType.addEntity();
        Entity middle = entityType.addEntity();
        startId = start.getId();
        endId = end.getId();
        ConceptId middleId = middle.getId();
        ConceptId assertion11 = relationshipType1.addRelationship().addRolePlayer(role1, start).addRolePlayer(role2, middle).getId();
        ConceptId assertion12 = relationshipType1.addRelationship().addRolePlayer(role1, middle).addRolePlayer(role2, end).getId();
        ConceptId assertion21 = relationshipType2.addRelationship().addRolePlayer(role3, start).addRolePlayer(role4, middle).getId();
        ConceptId assertion22 = relationshipType2.addRelationship().addRolePlayer(role3, middle).addRolePlayer(role4, end).getId();
        correctPaths.add(Lists.newArrayList(startId, assertion11, middleId, assertion12, endId));
        correctPaths.add(Lists.newArrayList(startId, assertion11, middleId, assertion22, endId));
        correctPaths.add(Lists.newArrayList(startId, assertion21, middleId, assertion12, endId));
        correctPaths.add(Lists.newArrayList(startId, assertion21, middleId, assertion22, endId));
        graph.commit();
    }
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        List<List<Concept>> allPaths = graph.graql().compute().paths().from(startId).to(endId).execute();
        assertEquals(correctPaths.size(), allPaths.size());
        Set<List<ConceptId>> computedPaths = allPaths.stream().map(path -> path.stream().map(Concept::getId).collect(Collectors.toList())).collect(Collectors.toSet());
        assertEquals(correctPaths, computedPaths);
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) InvalidKBException(ai.grakn.exception.InvalidKBException) GraknTestUtil(ai.grakn.util.GraknTestUtil) Role(ai.grakn.concept.Role) Assume.assumeFalse(org.junit.Assume.assumeFalse) Concept(ai.grakn.concept.Concept) Entity(ai.grakn.concept.Entity) Graql(ai.grakn.graql.Graql) EntityType(ai.grakn.concept.EntityType) ArrayList(java.util.ArrayList) Attribute(ai.grakn.concept.Attribute) SessionContext(ai.grakn.test.rule.SessionContext) HashSet(java.util.HashSet) Assert.assertThat(org.junit.Assert.assertThat) Lists(com.google.common.collect.Lists) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) Matchers.empty(org.hamcrest.Matchers.empty) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Utility.getResourceEdgeId(ai.grakn.graql.internal.analytics.Utility.getResourceEdgeId) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) List(java.util.List) Optional(java.util.Optional) Schema(ai.grakn.util.Schema) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType) ArrayList(java.util.ArrayList) List(java.util.List) ConceptId(ai.grakn.concept.ConceptId) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 24 with ConceptId

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

the class ShortestPathTest method testMultiplePathInSubGraph.

@Test
public void testMultiplePathInSubGraph() {
    Set<List<ConceptId>> correctPaths = new HashSet<>();
    List<List<Concept>> allPaths;
    addSchemaAndEntities();
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        if (GraknTestUtil.usingJanus()) {
            // If no path is found in the vertex program, NoResultException is thrown to skip map reduce.
            // Tinker doesn't handle this well, so the next graql query would find the graph is empty.
            allPaths = graph.graql().compute().paths().in(thing, anotherThing).to(entityId1).from(entityId4).execute();
            assertEquals(0, allPaths.size());
        }
        correctPaths.add(Lists.newArrayList(entityId1, relationId12, entityId2, relationId24, entityId4));
        correctPaths.add(Lists.newArrayList(entityId1, relationId13, entityId3, relationId34, entityId4));
        allPaths = graph.graql().compute().paths().from(entityId1).to(entityId4).execute();
        assertEquals(correctPaths.size(), allPaths.size());
        Set<List<ConceptId>> computedPaths = allPaths.stream().map(path -> path.stream().map(Concept::getId).collect(Collectors.toList())).collect(Collectors.toSet());
        assertEquals(correctPaths, computedPaths);
    }
}
Also used : InvalidKBException(ai.grakn.exception.InvalidKBException) GraknTestUtil(ai.grakn.util.GraknTestUtil) Role(ai.grakn.concept.Role) Assume.assumeFalse(org.junit.Assume.assumeFalse) Concept(ai.grakn.concept.Concept) Entity(ai.grakn.concept.Entity) Graql(ai.grakn.graql.Graql) EntityType(ai.grakn.concept.EntityType) ArrayList(java.util.ArrayList) Attribute(ai.grakn.concept.Attribute) SessionContext(ai.grakn.test.rule.SessionContext) HashSet(java.util.HashSet) Assert.assertThat(org.junit.Assert.assertThat) Lists(com.google.common.collect.Lists) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) Matchers.empty(org.hamcrest.Matchers.empty) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Utility.getResourceEdgeId(ai.grakn.graql.internal.analytics.Utility.getResourceEdgeId) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) List(java.util.List) Optional(java.util.Optional) Schema(ai.grakn.util.Schema) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) GraknTx(ai.grakn.GraknTx) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 25 with ConceptId

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

the class DefineQueryTest method whenModifyingAThingInADefineQuery_Throw.

@Test
public void whenModifyingAThingInADefineQuery_Throw() {
    ConceptId id = movies.tx().getEntityType("movie").instances().iterator().next().getId();
    exception.expect(GraqlQueryException.class);
    exception.expectMessage(anyOf(is(GraqlQueryException.defineUnsupportedProperty(HasAttributeProperty.NAME).getMessage()), is(GraqlQueryException.defineUnsupportedProperty(ValueProperty.NAME).getMessage())));
    qb.define(var().id(id).has("title", "Bob")).execute();
}
Also used : ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

Aggregations

ConceptId (ai.grakn.concept.ConceptId)80 Test (org.junit.Test)55 Concept (ai.grakn.concept.Concept)23 Role (ai.grakn.concept.Role)22 RelationshipType (ai.grakn.concept.RelationshipType)19 GraknTx (ai.grakn.GraknTx)18 EntityType (ai.grakn.concept.EntityType)18 Label (ai.grakn.concept.Label)16 GrpcConcept (ai.grakn.rpc.generated.GrpcConcept)14 Var (ai.grakn.graql.Var)12 List (java.util.List)12 Entity (ai.grakn.concept.Entity)10 AttributeType (ai.grakn.concept.AttributeType)9 HashSet (java.util.HashSet)9 Set (java.util.Set)9 Assert.assertEquals (org.junit.Assert.assertEquals)9 ClassRule (org.junit.ClassRule)9 GraknTxType (ai.grakn.GraknTxType)8 Keyspace (ai.grakn.Keyspace)8 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)8