Search in sources :

Example 11 with RelationshipType

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

the class TailRecursionKB method buildExtensionalDB.

private void buildExtensionalDB(GraknTx graph, int n, int m) {
    Role qfrom = graph.getRole("Q-from");
    Role qto = graph.getRole("Q-to");
    EntityType aEntity = graph.getEntityType("a-entity");
    EntityType bEntity = graph.getEntityType("b-entity");
    RelationshipType q = graph.getRelationshipType("Q");
    putEntityWithResource(graph, "a0", aEntity, key);
    for (int i = 1; i <= m + 1; i++) {
        for (int j = 1; j <= n; j++) {
            putEntityWithResource(graph, "b" + i + "," + j, bEntity, key);
        }
    }
    for (int j = 1; j <= n; j++) {
        q.addRelationship().addRolePlayer(qfrom, getInstance(graph, "a0")).addRolePlayer(qto, getInstance(graph, "b1" + "," + j));
        for (int i = 1; i <= m; i++) {
            q.addRelationship().addRolePlayer(qfrom, getInstance(graph, "b" + i + "," + j)).addRolePlayer(qto, getInstance(graph, "b" + (i + 1) + "," + j));
        }
    }
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) RelationshipType(ai.grakn.concept.RelationshipType)

Example 12 with RelationshipType

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

the class TxCacheTest method whenClosingTransaction_EnsureTransactionCacheIsEmpty.

@Test
public void whenClosingTransaction_EnsureTransactionCacheIsEmpty() {
    TxCache cache = tx.txCache();
    // Load some sample data
    AttributeType<String> attributeType = tx.putAttributeType("Attribute Type", AttributeType.DataType.STRING);
    Role role1 = tx.putRole("role 1");
    Role role2 = tx.putRole("role 2");
    EntityType entityType = tx.putEntityType("My Type").plays(role1).plays(role2).attribute(attributeType);
    RelationshipType relationshipType = tx.putRelationshipType("My Relationship Type").relates(role1).relates(role2);
    Entity e1 = entityType.addEntity();
    Entity e2 = entityType.addEntity();
    Attribute<String> r1 = attributeType.putAttribute("test");
    e1.attribute(r1);
    relationshipType.addRelationship().addRolePlayer(role1, e1).addRolePlayer(role2, e2);
    // Check the caches are not empty
    assertThat(cache.getConceptCache().keySet(), not(empty()));
    assertThat(cache.getSchemaConceptCache().keySet(), not(empty()));
    assertThat(cache.getLabelCache().keySet(), not(empty()));
    assertThat(cache.getShardingCount().keySet(), not(empty()));
    assertThat(cache.getModifiedCastings(), not(empty()));
    // Close the transaction
    tx.commit();
    // Check the caches are empty
    assertThat(cache.getConceptCache().keySet(), empty());
    assertThat(cache.getSchemaConceptCache().keySet(), empty());
    assertThat(cache.getLabelCache().keySet(), empty());
    assertThat(cache.getShardingCount().keySet(), empty());
    assertThat(cache.getModifiedThings(), empty());
    assertThat(cache.getModifiedRoles(), empty());
    assertThat(cache.getModifiedRelationshipTypes(), empty());
    assertThat(cache.getModifiedRules(), empty());
    assertThat(cache.getModifiedCastings(), empty());
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 13 with RelationshipType

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

the class RelationshipAtom method materialise.

@Override
public Stream<Answer> materialise() {
    RelationshipType relationType = getSchemaConcept().asRelationshipType();
    Multimap<Role, Var> roleVarMap = getRoleVarMap();
    Answer substitution = getParentQuery().getSubstitution();
    Relationship relationship = RelationshipTypeImpl.from(relationType).addRelationshipInferred();
    roleVarMap.asMap().forEach((key, value) -> value.forEach(var -> relationship.addRolePlayer(key, substitution.get(var).asThing())));
    Answer relationSub = getRoleSubstitution().merge(getVarName().isUserDefinedName() ? new QueryAnswer(ImmutableMap.of(getVarName(), relationship)) : new QueryAnswer());
    return Stream.of(substitution.merge(relationSub));
}
Also used : Role(ai.grakn.concept.Role) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) VarPattern(ai.grakn.graql.VarPattern) RelationshipProperty(ai.grakn.graql.internal.pattern.property.RelationshipProperty) RelationshipTypeImpl(ai.grakn.kb.internal.concept.RelationshipTypeImpl) Graql(ai.grakn.graql.Graql) ReasonerUtils.top(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.top) Type(ai.grakn.concept.Type) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) EntityType(ai.grakn.concept.EntityType) HashMultimap(com.google.common.collect.HashMultimap) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) Map(java.util.Map) ReasonerUtils.supers(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.supers) RoleConverter(ai.grakn.graql.internal.reasoner.utils.conversion.RoleConverter) ConceptId(ai.grakn.concept.ConceptId) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet(com.google.common.collect.ImmutableSet) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) List(java.util.List) Stream(java.util.stream.Stream) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) ReasonerUtils.compatibleRoles(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.compatibleRoles) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Iterables(com.google.common.collect.Iterables) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) ReasonerUtils.compatibleRelationTypesWithRoles(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.compatibleRelationTypesWithRoles) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) Rule(ai.grakn.concept.Rule) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ReasonerUtils.multimapIntersection(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.multimapIntersection) ImmutableList(com.google.common.collect.ImmutableList) CommonUtil(ai.grakn.util.CommonUtil) Predicate(ai.grakn.graql.internal.reasoner.atom.predicate.Predicate) Relationship(ai.grakn.concept.Relationship) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) TypeConverter(ai.grakn.graql.internal.reasoner.utils.conversion.TypeConverter) Nullable(javax.annotation.Nullable) ErrorMessage(ai.grakn.util.ErrorMessage) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Iterator(java.util.Iterator) Memoized(com.google.auto.value.extension.memoized.Memoized) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) ReasonerUtils(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils) VarProperty(ai.grakn.graql.admin.VarProperty) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty) UnifierComparison(ai.grakn.graql.admin.UnifierComparison) RelationPlayer(ai.grakn.graql.admin.RelationPlayer) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Schema(ai.grakn.util.Schema) Pattern(ai.grakn.graql.Pattern) Comparator(java.util.Comparator) Unifier(ai.grakn.graql.admin.Unifier) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Var(ai.grakn.graql.Var) Relationship(ai.grakn.concept.Relationship) RelationshipType(ai.grakn.concept.RelationshipType)

Example 14 with RelationshipType

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

the class StatisticsTest method addSchemaAndEntities.

private void addSchemaAndEntities() throws InvalidKBException {
    try (GraknTx tx = session.open(GraknTxType.WRITE)) {
        EntityType entityType1 = tx.putEntityType(thing);
        EntityType entityType2 = tx.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 relation1 = tx.putRole("relation1");
        Role relation2 = tx.putRole("relation2");
        entityType1.plays(relation1).plays(relation2);
        entityType2.plays(relation1).plays(relation2);
        RelationshipType related = tx.putRelationshipType("related").relates(relation1).relates(relation2);
        related.addRelationship().addRolePlayer(relation1, entity1).addRolePlayer(relation2, entity2);
        related.addRelationship().addRolePlayer(relation1, entity2).addRolePlayer(relation2, entity3);
        related.addRelationship().addRolePlayer(relation1, entity2).addRolePlayer(relation2, entity4);
        AttributeType<Double> attribute1 = tx.putAttributeType(resourceType1, AttributeType.DataType.DOUBLE);
        AttributeType<Long> attribute2 = tx.putAttributeType(resourceType2, AttributeType.DataType.LONG);
        AttributeType<Long> attribute3 = tx.putAttributeType(resourceType3, AttributeType.DataType.LONG);
        AttributeType<String> attribute4 = tx.putAttributeType(resourceType4, AttributeType.DataType.STRING);
        AttributeType<Long> attribute5 = tx.putAttributeType(resourceType5, AttributeType.DataType.LONG);
        AttributeType<Double> attribute6 = tx.putAttributeType(resourceType6, AttributeType.DataType.DOUBLE);
        AttributeType<Double> attribute7 = tx.putAttributeType(resourceType7, AttributeType.DataType.DOUBLE);
        entityType1.attribute(attribute1);
        entityType1.attribute(attribute2);
        entityType1.attribute(attribute3);
        entityType1.attribute(attribute4);
        entityType1.attribute(attribute5);
        entityType1.attribute(attribute6);
        entityType1.attribute(attribute7);
        entityType2.attribute(attribute1);
        entityType2.attribute(attribute2);
        entityType2.attribute(attribute3);
        entityType2.attribute(attribute4);
        entityType2.attribute(attribute5);
        entityType2.attribute(attribute6);
        entityType2.attribute(attribute7);
        tx.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 15 with RelationshipType

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

the class StatisticsTest method addResourceRelations.

private void addResourceRelations() throws InvalidKBException {
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        Entity entity1 = graph.getConcept(entityId1);
        Entity entity2 = graph.getConcept(entityId2);
        Entity entity3 = graph.getConcept(entityId3);
        Entity entity4 = graph.getConcept(entityId4);
        Role resourceOwner1 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType1)));
        Role resourceOwner2 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType2)));
        Role resourceOwner3 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType3)));
        Role resourceOwner4 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType4)));
        Role resourceOwner5 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType5)));
        Role resourceOwner6 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType6)));
        Role resourceValue1 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType1)));
        Role resourceValue2 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType2)));
        Role resourceValue3 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType3)));
        Role resourceValue4 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType4)));
        Role resourceValue5 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType5)));
        Role resourceValue6 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType6)));
        RelationshipType relationshipType1 = graph.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(Label.of(resourceType1)));
        relationshipType1.addRelationship().addRolePlayer(resourceOwner1, entity1).addRolePlayer(resourceValue1, graph.<Double>getAttributeType(resourceType1).putAttribute(1.2));
        relationshipType1.addRelationship().addRolePlayer(resourceOwner1, entity1).addRolePlayer(resourceValue1, graph.<Double>getAttributeType(resourceType1).putAttribute(1.5));
        relationshipType1.addRelationship().addRolePlayer(resourceOwner1, entity3).addRolePlayer(resourceValue1, graph.<Double>getAttributeType(resourceType1).putAttribute(1.8));
        RelationshipType relationshipType2 = graph.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(Label.of(resourceType2)));
        relationshipType2.addRelationship().addRolePlayer(resourceOwner2, entity1).addRolePlayer(resourceValue2, graph.<Long>getAttributeType(resourceType2).putAttribute(4L));
        relationshipType2.addRelationship().addRolePlayer(resourceOwner2, entity1).addRolePlayer(resourceValue2, graph.<Long>getAttributeType(resourceType2).putAttribute(-1L));
        relationshipType2.addRelationship().addRolePlayer(resourceOwner2, entity4).addRolePlayer(resourceValue2, graph.<Long>getAttributeType(resourceType2).putAttribute(0L));
        graph.<Long>getAttributeType(resourceType3).putAttribute(100L);
        RelationshipType relationshipType5 = graph.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(Label.of(resourceType5)));
        relationshipType5.addRelationship().addRolePlayer(resourceOwner5, entity1).addRolePlayer(resourceValue5, graph.<Long>getAttributeType(resourceType5).putAttribute(-7L));
        relationshipType5.addRelationship().addRolePlayer(resourceOwner5, entity2).addRolePlayer(resourceValue5, graph.<Long>getAttributeType(resourceType5).putAttribute(-7L));
        relationshipType5.addRelationship().addRolePlayer(resourceOwner5, entity4).addRolePlayer(resourceValue5, graph.<Long>getAttributeType(resourceType5).putAttribute(-7L));
        RelationshipType relationshipType6 = graph.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(Label.of(resourceType6)));
        relationshipType6.addRelationship().addRolePlayer(resourceOwner6, entity1).addRolePlayer(resourceValue6, graph.<Double>getAttributeType(resourceType6).putAttribute(7.5));
        relationshipType6.addRelationship().addRolePlayer(resourceOwner6, entity2).addRolePlayer(resourceValue6, graph.<Double>getAttributeType(resourceType6).putAttribute(7.5));
        relationshipType6.addRelationship().addRolePlayer(resourceOwner6, entity4).addRolePlayer(resourceValue6, graph.<Double>getAttributeType(resourceType6).putAttribute(7.5));
        // some resources in, but not connect them to any instances
        graph.<Double>getAttributeType(resourceType1).putAttribute(2.8);
        graph.<Long>getAttributeType(resourceType2).putAttribute(-5L);
        graph.<Long>getAttributeType(resourceType5).putAttribute(10L);
        graph.<Double>getAttributeType(resourceType6).putAttribute(0.8);
        graph.commit();
    }
}
Also used : Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType)

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