Search in sources :

Example 71 with RelationshipType

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

the class KCoreTest method addSchemaAndEntities.

private void addSchemaAndEntities() throws InvalidKBException {
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType entityType1 = graph.putEntityType(thing);
        EntityType entityType2 = graph.putEntityType(anotherThing);
        Role role1 = graph.putRole("role1");
        Role role2 = graph.putRole("role2");
        RelationshipType relationshipType1 = graph.putRelationshipType(related).relates(role1).relates(role2);
        Role role3 = graph.putRole("role3");
        Role role4 = graph.putRole("role4");
        RelationshipType relationshipType2 = graph.putRelationshipType(veryRelated).relates(role3).relates(role4);
        entityType1.plays(role1).plays(role2).plays(role3).plays(role4);
        entityType2.plays(role1).plays(role2).plays(role3).plays(role4);
        Entity entity1 = entityType1.addEntity();
        Entity entity2 = entityType1.addEntity();
        Entity entity3 = entityType1.addEntity();
        Entity entity4 = entityType1.addEntity();
        relationshipType1.addRelationship().addRolePlayer(role1, entity1).addRolePlayer(role2, entity2);
        relationshipType1.addRelationship().addRolePlayer(role1, entity2).addRolePlayer(role2, entity3);
        relationshipType1.addRelationship().addRolePlayer(role1, entity3).addRolePlayer(role2, entity4);
        relationshipType1.addRelationship().addRolePlayer(role1, entity4).addRolePlayer(role2, entity1);
        relationshipType2.addRelationship().addRolePlayer(role3, entity1).addRolePlayer(role4, entity3);
        relationshipType2.addRelationship().addRolePlayer(role3, entity2).addRolePlayer(role4, entity4);
        entityId1 = entity1.getId();
        entityId2 = entity2.getId();
        entityId3 = entity3.getId();
        entityId4 = 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 72 with RelationshipType

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

the class CountTest method testHasResourceEdges.

@Test
public void testHasResourceEdges() {
    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();
        aPerson.attribute(name.putAttribute("jason"));
        graph.commit();
    }
    long count;
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        count = graph.graql().compute().count().execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().includeAttribute().execute();
        assertEquals(3L, count);
        count = graph.graql().compute().count().in("name").execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().in("@has-name").execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().in("@has-name", "thing").execute();
        assertEquals(3L, count);
        count = graph.graql().compute().count().in("@has-name", "name").execute();
        assertEquals(2L, count);
        count = graph.graql().compute().count().in("relationship").execute();
        assertEquals(0L, count);
        count = graph.graql().compute().count().in("relationship").includeAttribute().execute();
        assertEquals(1L, count);
    }
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        // manually construct the relation type and instance
        EntityType person = graph.getEntityType("person");
        Entity aPerson = person.addEntity();
        AttributeType<String> name = graph.putAttributeType("name", AttributeType.DataType.STRING);
        Attribute jason = name.putAttribute("jason");
        Role resourceOwner = graph.putRole(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of("name")));
        person.plays(resourceOwner);
        Role resourceValue = graph.putRole(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of("name")));
        name.plays(resourceValue);
        RelationshipType relationshipType = graph.putRelationshipType(Schema.ImplicitType.HAS.getLabel(Label.of("name"))).relates(resourceOwner).relates(resourceValue);
        relationshipType.addRelationship().addRolePlayer(resourceOwner, aPerson).addRolePlayer(resourceValue, jason);
        graph.commit();
    }
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        count = graph.graql().compute().count().execute();
        assertEquals(2L, count);
        count = graph.graql().compute().count().includeAttribute().execute();
        assertEquals(5L, count);
        count = graph.graql().compute().count().in("name").execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().includeAttribute().in("name").execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().in("@has-name").execute();
        assertEquals(2L, count);
        count = graph.graql().compute().count().in("@has-name", "thing").execute();
        assertEquals(5L, count);
        count = graph.graql().compute().count().in("@has-name", "name").execute();
        assertEquals(3L, count);
        count = graph.graql().compute().count().in("relationship").execute();
        assertEquals(0L, count);
        count = graph.graql().compute().count().in("relationship").includeAttribute().execute();
        assertEquals(2L, count);
    }
}
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 73 with RelationshipType

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

the class CountTest method testHasResourceVerticesAndEdges.

@Test
public void testHasResourceVerticesAndEdges() {
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        // manually construct the relation type and instance
        EntityType person = graph.putEntityType("person");
        Entity aPerson = person.addEntity();
        AttributeType<String> name = graph.putAttributeType("name", AttributeType.DataType.STRING);
        Attribute jason = name.putAttribute("jason");
        person.attribute(name);
        Role resourceOwner = graph.putRole(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of("name")));
        person.plays(resourceOwner);
        Role resourceValue = graph.putRole(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of("name")));
        name.plays(resourceValue);
        RelationshipType relationshipType = graph.putRelationshipType(Schema.ImplicitType.HAS.getLabel(Label.of("name"))).relates(resourceOwner).relates(resourceValue);
        // here relationship type is still implicit
        relationshipType.addRelationship().addRolePlayer(resourceOwner, aPerson).addRolePlayer(resourceValue, jason);
        graph.commit();
    }
    long count;
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        count = graph.graql().compute().count().execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().includeAttribute().execute();
        assertEquals(3L, count);
        count = graph.graql().compute().count().in("name").execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().in("@has-name").execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().in("@has-name", "name").execute();
        assertEquals(2L, count);
        count = graph.graql().compute().count().in("relationship").execute();
        assertEquals(0L, count);
        count = graph.graql().compute().count().in("relationship").includeAttribute().execute();
        assertEquals(1L, count);
    }
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType person = graph.getEntityType("person");
        AttributeType<String> name = graph.putAttributeType("name", AttributeType.DataType.STRING);
        Entity aPerson = person.addEntity();
        aPerson.attribute(name.getAttribute("jason"));
        graph.commit();
    }
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        count = graph.graql().compute().count().includeAttribute().execute();
        assertEquals(5L, count);
        count = graph.graql().compute().count().in("name").execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().in("@has-name").execute();
        assertEquals(2L, count);
        count = graph.graql().compute().count().in("@has-name", "name").execute();
        assertEquals(3L, count);
        count = graph.graql().compute().count().in("relationship").execute();
        assertEquals(0L, count);
        count = graph.graql().compute().count().in("relationship").includeAttribute().execute();
        assertEquals(2L, count);
    }
}
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 74 with RelationshipType

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

the class GraqlTest method addSchemaAndEntities.

private void addSchemaAndEntities() throws InvalidKBException {
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType entityType1 = graph.putEntityType(thingy);
        EntityType entityType2 = graph.putEntityType(anotherThing);
        Entity entity1 = entityType1.addEntity();
        Entity entity2 = entityType1.addEntity();
        Entity entity3 = entityType1.addEntity();
        Entity entity4 = entityType2.addEntity();
        entityId1 = entity1.getId().getValue();
        entityId2 = entity2.getId().getValue();
        entityId3 = entity3.getId().getValue();
        entityId4 = entity4.getId().getValue();
        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().getValue();
        relationId24 = relationshipType.addRelationship().addRolePlayer(role1, entity2).addRolePlayer(role2, entity4).getId().getValue();
        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 75 with RelationshipType

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

the class TypeInferenceQueryTest method typeInference.

private void typeInference(List<RelationshipType> possibleTypes, String pattern, String subbedPattern, EmbeddedGraknTx<?> graph) {
    ReasonerAtomicQuery query = ReasonerQueries.atomic(conjunction(pattern, graph), graph);
    ReasonerAtomicQuery subbedQuery = ReasonerQueries.atomic(conjunction(subbedPattern, graph), graph);
    RelationshipAtom atom = (RelationshipAtom) query.getAtom();
    RelationshipAtom subbedAtom = (RelationshipAtom) subbedQuery.getAtom();
    List<Type> relationshipTypes = atom.inferPossibleTypes(new QueryAnswer());
    List<Type> subbedRelationshipTypes = subbedAtom.inferPossibleTypes(new QueryAnswer());
    if (possibleTypes.size() == 1) {
        assertEquals(possibleTypes, relationshipTypes);
        assertEquals(relationshipTypes, subbedRelationshipTypes);
        assertEquals(atom.getSchemaConcept(), Iterables.getOnlyElement(possibleTypes));
        assertEquals(subbedAtom.getSchemaConcept(), Iterables.getOnlyElement(possibleTypes));
    } else {
        assertTrue(CollectionUtils.isEqualCollection(possibleTypes, relationshipTypes));
        assertTrue(CollectionUtils.isEqualCollection(relationshipTypes, subbedRelationshipTypes));
        assertEquals(atom.getSchemaConcept(), null);
        assertEquals(subbedAtom.getSchemaConcept(), null);
    }
    typeInferenceQueries(possibleTypes, pattern, graph);
    typeInferenceQueries(possibleTypes, subbedPattern, graph);
}
Also used : Type(ai.grakn.concept.Type) RelationshipType(ai.grakn.concept.RelationshipType) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)

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