Search in sources :

Example 61 with RelationshipType

use of ai.grakn.concept.RelationshipType 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 62 with RelationshipType

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

Example 63 with RelationshipType

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

the class ShortestPathTest method testResourceVerticesAndEdges.

@Test
public void testResourceVerticesAndEdges() {
    ConceptId idPerson1;
    ConceptId idPerson2;
    ConceptId idPerson3;
    ConceptId idPower1;
    ConceptId idPower2;
    ConceptId idPower3;
    ConceptId idRelationPerson1Power1;
    ConceptId idRelationPerson2Power2;
    ConceptId idRelationPerson1Person3;
    List<ConceptId> pathPerson2Power1 = new ArrayList<>();
    List<ConceptId> pathPower3Power1 = new ArrayList<>();
    List<ConceptId> pathPerson3Power3 = new ArrayList<>();
    try (GraknTx tx = session.open(GraknTxType.WRITE)) {
        EntityType person = tx.putEntityType("person");
        AttributeType<Long> power = tx.putAttributeType("power", AttributeType.DataType.LONG);
        person.attribute(power);
        // manually construct the attribute relation
        Role resourceOwner = tx.getRole(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of("power")).getValue());
        Role resourceValue = tx.getRole(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of("power")).getValue());
        RelationshipType relationType = tx.getRelationshipType(Schema.ImplicitType.HAS.getLabel(Label.of("power")).getValue());
        Entity person1 = person.addEntity();
        idPerson1 = person1.getId();
        Entity person2 = person.addEntity();
        idPerson2 = person2.getId();
        Entity person3 = person.addEntity();
        idPerson3 = person3.getId();
        Attribute power1 = power.putAttribute(1L);
        idPower1 = power1.getId();
        Attribute power2 = power.putAttribute(2L);
        idPower2 = power2.getId();
        Attribute power3 = power.putAttribute(3L);
        idPower3 = power3.getId();
        assert relationType != null;
        idRelationPerson1Power1 = relationType.addRelationship().addRolePlayer(resourceOwner, person1).addRolePlayer(resourceValue, power1).getId();
        idRelationPerson2Power2 = relationType.addRelationship().addRolePlayer(resourceOwner, person2).addRolePlayer(resourceValue, power2).getId();
        // add implicit resource relationships as well
        person.attribute(power);
        person1.attribute(power2);
        person3.attribute(power3);
        // finally add a relation between persons to make it more interesting
        Role role1 = tx.putRole("role1");
        Role role2 = tx.putRole("role2");
        person.plays(role1).plays(role2);
        RelationshipType relationTypePerson = tx.putRelationshipType(related).relates(role1).relates(role2);
        idRelationPerson1Person3 = relationTypePerson.addRelationship().addRolePlayer(role1, person1).addRolePlayer(role2, person3).getId();
        tx.commit();
    }
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        List<List<Concept>> allPaths;
        // Path from power3 to power3
        pathPerson3Power3.add(idPerson3);
        if (null != getResourceEdgeId(graph, idPower3, idPerson3)) {
            pathPerson3Power3.add(getResourceEdgeId(graph, idPower3, idPerson3));
        }
        pathPerson3Power3.add(idPower3);
        allPaths = graph.graql().compute().paths().from(idPerson3).to(idPower3).includeAttribute().execute();
        assertEquals(1, allPaths.size());
        checkPathsAreEqual(pathPerson3Power3, allPaths.get(0));
        // Path from person2 to power1
        pathPerson2Power1.add(idPerson2);
        pathPerson2Power1.add(idRelationPerson2Power2);
        pathPerson2Power1.add(idPower2);
        if (null != getResourceEdgeId(graph, idPerson1, idPower2)) {
            pathPerson2Power1.add(getResourceEdgeId(graph, idPerson1, idPower2));
        }
        pathPerson2Power1.add(idPerson1);
        pathPerson2Power1.add(idRelationPerson1Power1);
        pathPerson2Power1.add(idPower1);
        allPaths = graph.graql().compute().paths().from(idPerson2).to(idPower1).includeAttribute().execute();
        assertEquals(1, allPaths.size());
        checkPathsAreEqual(pathPerson2Power1, allPaths.get(0));
        // Path from power3 to power1
        pathPower3Power1.add(idPower3);
        if (null != getResourceEdgeId(graph, idPower3, idPerson3)) {
            pathPower3Power1.add(getResourceEdgeId(graph, idPower3, idPerson3));
        }
        pathPower3Power1.add(idPerson3);
        pathPower3Power1.add(idRelationPerson1Person3);
        pathPower3Power1.add(idPerson1);
        pathPower3Power1.add(idRelationPerson1Power1);
        pathPower3Power1.add(idPower1);
        allPaths = graph.graql().compute().paths().includeAttribute().from(idPower3).to(idPower1).execute();
        assertEquals(1, allPaths.size());
        checkPathsAreEqual(pathPower3Power1, allPaths.get(0));
    }
}
Also used : Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) ArrayList(java.util.ArrayList) RelationshipType(ai.grakn.concept.RelationshipType) 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) Test(org.junit.Test)

Example 64 with RelationshipType

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

the class StatisticsTest method testHasResourceVerticesAndEdges.

@Test
public void testHasResourceVerticesAndEdges() {
    try (GraknTx tx = session.open(GraknTxType.WRITE)) {
        // manually construct the relation type and instance
        AttributeType<Long> power = tx.putAttributeType("power", AttributeType.DataType.LONG);
        EntityType person = tx.putEntityType("person").attribute(power);
        Role resourceOwner = tx.getRole(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of("power")).getValue());
        Role resourceValue = tx.getRole(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of("power")).getValue());
        person.attribute(power);
        Entity person1 = person.addEntity();
        Entity person2 = person.addEntity();
        Entity person3 = person.addEntity();
        Attribute power1 = power.putAttribute(1L);
        Attribute power2 = power.putAttribute(2L);
        Attribute power3 = power.putAttribute(3L);
        RelationshipType relationType = tx.putRelationshipType(Schema.ImplicitType.HAS.getLabel(Label.of("power"))).relates(resourceOwner).relates(resourceValue);
        relationType.addRelationship().addRolePlayer(resourceOwner, person1).addRolePlayer(resourceValue, power1);
        relationType.addRelationship().addRolePlayer(resourceOwner, person2).addRolePlayer(resourceValue, power2);
        person1.attribute(power2);
        person3.attribute(power3);
        tx.commit();
    }
    Optional<Number> result;
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        // No need to test all statistics as most of them share the same vertex program
        result = graph.graql().compute().min().of("power").in().execute();
        assertEquals(1L, result.get().longValue());
        result = graph.graql().compute().max().of("power").in().execute();
        assertEquals(3L, result.get().longValue());
        result = graph.graql().compute().sum().of("power").in().execute();
        assertEquals(8L, result.get().longValue());
        result = graph.graql().compute().median().of("power").in().execute();
        assertEquals(2L, result.get().longValue());
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 65 with RelationshipType

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

the class ConnectedComponentTest 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();
        entityId1 = entity1.getId();
        entityId2 = entity2.getId();
        entityId3 = entity3.getId();
        entityId4 = entity4.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);
        relationshipType.addRelationship().addRolePlayer(role1, entity1).addRolePlayer(role2, entity2).getId();
        relationshipType.addRelationship().addRolePlayer(role1, entity2).addRolePlayer(role2, entity3).getId();
        relationshipType.addRelationship().addRolePlayer(role1, entity2).addRolePlayer(role2, entity4).getId();
        List<AttributeType> attributeTypeList = new ArrayList<>();
        attributeTypeList.add(graph.putAttributeType(resourceType1, AttributeType.DataType.DOUBLE));
        attributeTypeList.add(graph.putAttributeType(resourceType2, AttributeType.DataType.LONG));
        attributeTypeList.add(graph.putAttributeType(resourceType3, AttributeType.DataType.LONG));
        attributeTypeList.add(graph.putAttributeType(resourceType4, AttributeType.DataType.STRING));
        attributeTypeList.add(graph.putAttributeType(resourceType5, AttributeType.DataType.LONG));
        attributeTypeList.add(graph.putAttributeType(resourceType6, AttributeType.DataType.DOUBLE));
        attributeTypeList.add(graph.putAttributeType(resourceType7, AttributeType.DataType.DOUBLE));
        attributeTypeList.forEach(attributeType -> {
            entityType1.attribute(attributeType);
            entityType2.attribute(attributeType);
        });
        graph.commit();
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) ArrayList(java.util.ArrayList)

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