Search in sources :

Example 51 with Entity

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

the class QueryCacheTest method onStartup.

@Before
public void onStartup() {
    assumeTrue(GraknTestUtil.usingTinker());
    graph = testContext.tx();
    String recordPatternString = "{(role1: $x, role2: $y) isa reifiable-relation;}";
    String retrievePatternString = "{(role1: $p1, role2: $p2) isa reifiable-relation;}";
    Conjunction<VarPatternAdmin> recordPattern = conjunction(recordPatternString, graph);
    Conjunction<VarPatternAdmin> retrievePattern = conjunction(retrievePatternString, graph);
    recordQuery = ReasonerQueries.atomic(recordPattern, graph);
    retrieveQuery = ReasonerQueries.atomic(retrievePattern, graph);
    retrieveToRecordUnifier = retrieveQuery.getMultiUnifier(recordQuery).getUnifier();
    recordToRetrieveUnifier = retrieveToRecordUnifier.inverse();
    Entity entity = graph.getEntityType("anotherNoRoleEntity").instances().findFirst().orElse(null);
    singleAnswer = new QueryAnswer(ImmutableMap.of(var("x"), entity, var("y"), entity));
}
Also used : Entity(ai.grakn.concept.Entity) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Before(org.junit.Before)

Example 52 with Entity

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

the class ShortestPathTest method testMultipleIndependentShortestPaths.

@Test
public void testMultipleIndependentShortestPaths() throws InvalidKBException {
    Set<List<ConceptId>> validPaths = new HashSet<>();
    ConceptId startId;
    ConceptId endId;
    int numberOfPaths = 3;
    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 relationshipType = graph.putRelationshipType(related).relates(role1).relates(role2);
        Entity start = entityType.addEntity();
        Entity end = entityType.addEntity();
        startId = start.getId();
        endId = end.getId();
        // create N identical length paths
        for (int i = 0; i < numberOfPaths; i++) {
            List<ConceptId> validPath = new ArrayList<>();
            validPath.add(startId);
            Entity middle = entityType.addEntity();
            ConceptId middleId = middle.getId();
            ConceptId assertion1 = relationshipType.addRelationship().addRolePlayer(role1, start).addRolePlayer(role2, middle).getId();
            validPath.add(assertion1);
            validPath.add(middleId);
            ConceptId assertion2 = relationshipType.addRelationship().addRolePlayer(role1, middle).addRolePlayer(role2, end).getId();
            validPath.add(assertion2);
            validPath.add(endId);
            validPaths.add(validPath);
        }
        graph.commit();
    }
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        List<List<Concept>> allPaths = graph.graql().compute().paths().from(startId).to(endId).execute();
        assertEquals(numberOfPaths, allPaths.size());
        Set<List<ConceptId>> computedPaths = allPaths.stream().map(path -> path.stream().map(Concept::getId).collect(Collectors.toList())).collect(Collectors.toSet());
        assertEquals(validPaths, 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) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType) ArrayList(java.util.ArrayList) ConceptId(ai.grakn.concept.ConceptId) EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 53 with Entity

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

the class ShortestPathTest method addSchemaAndEntities.

private void addSchemaAndEntities() throws InvalidKBException {
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType entityType1 = graph.putEntityType(thing);
        EntityType entityType2 = graph.putEntityType(anotherThing);
        Entity entity1 = entityType1.addEntity();
        Entity entity2 = entityType1.addEntity();
        Entity entity3 = entityType1.addEntity();
        Entity entity4 = entityType2.addEntity();
        Entity entity5 = entityType1.addEntity();
        entityId1 = entity1.getId();
        entityId2 = entity2.getId();
        entityId3 = entity3.getId();
        entityId4 = entity4.getId();
        entityId5 = entity5.getId();
        Role role1 = graph.putRole("role1");
        Role role2 = graph.putRole("role2");
        entityType1.plays(role1).plays(role2);
        entityType2.plays(role1).plays(role2);
        RelationshipType relationshipType = graph.putRelationshipType(related).relates(role1).relates(role2);
        relationId12 = relationshipType.addRelationship().addRolePlayer(role1, entity1).addRolePlayer(role2, entity2).getId();
        relationId13 = relationshipType.addRelationship().addRolePlayer(role1, entity1).addRolePlayer(role2, entity3).getId();
        relationId24 = relationshipType.addRelationship().addRolePlayer(role1, entity2).addRolePlayer(role2, entity4).getId();
        relationId34 = relationshipType.addRelationship().addRolePlayer(role1, entity3).addRolePlayer(role2, entity4).getId();
        graph.commit();
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType)

Example 54 with Entity

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

the class ShortestPathTest method testResourceEdges.

@Test
public void testResourceEdges() {
    ConceptId startId;
    ConceptId endId;
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType person = graph.putEntityType("person");
        AttributeType<String> name = graph.putAttributeType("name", AttributeType.DataType.STRING);
        person.attribute(name);
        Entity aPerson = person.addEntity();
        startId = aPerson.getId();
        Attribute<String> jason = name.putAttribute("jason");
        aPerson.attribute(jason);
        endId = jason.getId();
        graph.commit();
    }
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        List<List<Concept>> allPaths = graph.graql().compute().paths().from(startId).includeAttribute().to(endId).execute();
        assertEquals(1, allPaths.size());
        assertEquals(3, allPaths.get(0).size());
        assertEquals("@has-name", allPaths.get(0).get(1).asRelationship().type().getLabel().getValue());
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) ArrayList(java.util.ArrayList) List(java.util.List) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

Example 55 with Entity

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

the class ShortestPathTest method testMultiplePathsSharing3Instances.

@Test
public void testMultiplePathsSharing3Instances() 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");
        Role role5 = graph.putRole("role5");
        entityType.plays(role3).plays(role4).plays(role5);
        RelationshipType relationshipType2 = graph.putRelationshipType(veryRelated).relates(role3).relates(role4).relates(role5);
        Entity start = entityType.addEntity();
        Entity end = entityType.addEntity();
        Entity middle = entityType.addEntity();
        Entity middleA = entityType.addEntity();
        Entity middleB = entityType.addEntity();
        startId = start.getId();
        endId = end.getId();
        ConceptId middleId = middle.getId();
        ConceptId middleAId = middleA.getId();
        ConceptId middleBId = middleB.getId();
        ConceptId assertion1 = relationshipType1.addRelationship().addRolePlayer(role1, start).addRolePlayer(role2, middle).getId();
        ConceptId assertion2 = relationshipType2.addRelationship().addRolePlayer(role3, middle).addRolePlayer(role4, middleA).addRolePlayer(role5, middleB).getId();
        ConceptId assertion1A = relationshipType1.addRelationship().addRolePlayer(role1, middleA).addRolePlayer(role2, end).getId();
        ConceptId assertion1B = relationshipType1.addRelationship().addRolePlayer(role1, middleB).addRolePlayer(role2, end).getId();
        List<ConceptId> sharedPath = Lists.newArrayList(startId, assertion1, middleId, assertion2);
        List<ConceptId> path1 = new ArrayList<>(sharedPath);
        path1.addAll(Lists.newArrayList(middleAId, assertion1A, endId));
        List<ConceptId> path2 = new ArrayList<>(sharedPath);
        path2.addAll(Lists.newArrayList(middleBId, assertion1B, endId));
        correctPaths.add(path1);
        correctPaths.add(path2);
        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 : 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) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType) ArrayList(java.util.ArrayList) ConceptId(ai.grakn.concept.ConceptId) EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Entity (ai.grakn.concept.Entity)99 Test (org.junit.Test)81 EntityType (ai.grakn.concept.EntityType)74 Role (ai.grakn.concept.Role)53 RelationshipType (ai.grakn.concept.RelationshipType)50 GraknTx (ai.grakn.GraknTx)44 Attribute (ai.grakn.concept.Attribute)18 Set (java.util.Set)16 Relationship (ai.grakn.concept.Relationship)14 Label (ai.grakn.concept.Label)11 ConceptId (ai.grakn.concept.ConceptId)10 HashSet (java.util.HashSet)10 ArrayList (java.util.ArrayList)8 AttributeType (ai.grakn.concept.AttributeType)7 List (java.util.List)7 GraknSession (ai.grakn.GraknSession)6 Before (org.junit.Before)6 GraknTxType (ai.grakn.GraknTxType)5 Concept (ai.grakn.concept.Concept)5 GraqlQueryException (ai.grakn.exception.GraqlQueryException)5