Search in sources :

Example 21 with RelationType

use of com.vaticle.typedb.core.concept.type.RelationType in project grakn by graknlabs.

the class RuleTest method rule_indexes_update_on_rule_delete.

@Test
public void rule_indexes_update_on_rule_delete() throws IOException {
    Util.resetDirectory(dataDir);
    try (CoreDatabaseManager databaseMgr = CoreDatabaseManager.open(options)) {
        databaseMgr.create(database);
        try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                LogicManager logicMgr = txn.logic();
                EntityType person = conceptMgr.putEntityType("person");
                RelationType friendship = conceptMgr.putRelationType("friendship");
                friendship.setRelates("friend");
                RelationType marriage = conceptMgr.putRelationType("marriage");
                AttributeType name = conceptMgr.putAttributeType("name", AttributeType.ValueType.STRING);
                marriage.setRelates("spouse");
                person.setPlays(friendship.getRelates("friend"));
                person.setPlays(marriage.getRelates("spouse"));
                person.setOwns(name);
                Rule marriageFriendsRule = logicMgr.putRule("marriage-is-friendship", TypeQL.parsePattern("{ $x isa person; $y isa person; (spouse: $x, spouse: $y) isa marriage; }").asConjunction(), TypeQL.parseVariable("(friend: $x, friend: $y) isa friendship").asThing());
                Conjunction marriageFriendsThen = marriageFriendsRule.then();
                Variable marriageFriendsRelation = getVariable(marriageFriendsThen.variables(), Identifier.Variable.anon(0));
                assertEquals(set(Label.of("friendship")), marriageFriendsRelation.inferredTypes());
                Rule allFriendsRule = logicMgr.putRule("all-people-are-friends", TypeQL.parsePattern("{ $x isa person; $y isa person; $t type friendship; }").asConjunction(), TypeQL.parseVariable("(friend: $x, friend: $y) isa $t").asThing());
                Conjunction allFriendsThen = allFriendsRule.then();
                Variable allFriendsRelation = getVariable(allFriendsThen.variables(), Identifier.Variable.anon(0));
                assertEquals(set(Label.of("friendship")), allFriendsRelation.inferredTypes());
                Rule marriageSameName = logicMgr.putRule("marriage-same-name", TypeQL.parsePattern("{ $x isa person, has name $a; $y isa person; (spouse:$x, spouse: $y) isa marriage; }").asConjunction(), TypeQL.parseVariable("$y has $a").asThing());
                Conjunction sameName = marriageSameName.then();
                Variable nameAttr = getVariable(sameName.variables(), Identifier.Variable.name("a"));
                assertEquals(set(Label.of("name")), nameAttr.inferredTypes());
                txn.commit();
            }
            // check index after commit, and delete some rules
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                LogicManager logicMgr = txn.logic();
                Set<Rule> friendshipRules = logicMgr.rulesConcluding(Label.of("friendship")).toSet();
                Rule marriageFriendsRule = txn.logic().getRule("marriage-is-friendship");
                Rule allFriendsRule = txn.logic().getRule("all-people-are-friends");
                assertEquals(set(marriageFriendsRule, allFriendsRule), friendshipRules);
                allFriendsRule.delete();
                Set<Rule> hasNameRules = logicMgr.rulesConcludingHas(Label.of("name")).toSet();
                Rule marriageSameName = txn.logic().getRule("marriage-same-name");
                assertEquals(set(marriageSameName), hasNameRules);
                marriageSameName.delete();
                txn.commit();
            }
        }
        // check indexed types, should only includes rules that are still present
        try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.DATA)) {
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.READ)) {
                LogicManager logicMgr = txn.logic();
                Set<Rule> friendshipRules = logicMgr.rulesConcluding(Label.of("friendship")).toSet();
                Rule marriageFriendsRule = txn.logic().getRule("marriage-is-friendship");
                assertEquals(set(marriageFriendsRule), friendshipRules);
                Set<Rule> hasNameRules = logicMgr.rulesConcludingHas(Label.of("name")).toSet();
                assertEquals(set(), hasNameRules);
            }
        }
    }
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) Variable(com.vaticle.typedb.core.pattern.variable.Variable) ThingVariable(com.vaticle.typeql.lang.pattern.variable.ThingVariable) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) Conjunction(com.vaticle.typedb.core.pattern.Conjunction) CoreSession(com.vaticle.typedb.core.database.CoreSession) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Test(org.junit.Test)

Example 22 with RelationType

use of com.vaticle.typedb.core.concept.type.RelationType in project grakn by graknlabs.

the class RuleTest method rule_relation_does_not_materialise_when_present.

@Test
public void rule_relation_does_not_materialise_when_present() throws IOException {
    Util.resetDirectory(dataDir);
    try (CoreDatabaseManager databaseMgr = CoreDatabaseManager.open(options)) {
        databaseMgr.create(database);
        try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                LogicManager logicMgr = txn.logic();
                EntityType person = conceptMgr.putEntityType("person");
                RelationType friendship = conceptMgr.putRelationType("friendship");
                friendship.setRelates("friend");
                RelationType marriage = conceptMgr.putRelationType("marriage");
                marriage.setRelates("spouse");
                person.setPlays(friendship.getRelates("friend"));
                person.setPlays(marriage.getRelates("spouse"));
                logicMgr.putRule("marriage-is-friendship", TypeQL.parsePattern("{ $x isa person; $y isa person; (spouse: $x, spouse: $y) isa marriage; }").asConjunction(), TypeQL.parseVariable("(friend: $x, friend: $y) isa friendship").asThing());
                txn.commit();
            }
        }
        try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.DATA)) {
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                RelationType friendship = conceptMgr.getRelationType("friendship");
                txn.query().insert(TypeQL.parseQuery("insert $x isa person; $y isa person; " + "(spouse: $x, spouse: $y) isa marriage;" + "(friend: $x, friend: $y) isa friendship;").asInsert());
                List<? extends Relation> friendshipInstances = friendship.getInstances().toList();
                assertEquals(1, friendshipInstances.size());
                EntityType person = conceptMgr.getEntityType("person");
                List<? extends Entity> people = person.getInstances().toList();
                assertEquals(2, people.size());
                Rule rule = txn.logic().getRule("marriage-is-friendship");
                ConceptMap whenAnswer = new ConceptMap(map(pair(Identifier.Variable.name("x"), people.get(0)), pair(Identifier.Variable.name("y"), people.get(1))));
                List<Map<Identifier.Variable, Concept>> materialisations = rule.conclusion().materialise(whenAnswer, txn.traversal(), conceptMgr).toList();
                assertEquals(1, materialisations.size());
                assertEquals(5, materialisations.get(0).size());
                friendshipInstances = friendship.getInstances().toList();
                assertEquals(1, friendshipInstances.size());
                assertEquals(friendshipInstances.get(0), materialisations.get(0).get(Identifier.Variable.anon(0)));
                assertEquals(friendship, materialisations.get(0).get(Identifier.Variable.label("friendship")));
            }
        }
    }
}
Also used : CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) RelationType(com.vaticle.typedb.core.concept.type.RelationType) CoreSession(com.vaticle.typedb.core.database.CoreSession) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Map(java.util.Map) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Test(org.junit.Test)

Example 23 with RelationType

use of com.vaticle.typedb.core.concept.type.RelationType in project grakn by graknlabs.

the class ThingTypeSteps method thing_type_set_supertype.

@When("{root_label}\\( ?{type_label} ?) set supertype: {type_label}")
public void thing_type_set_supertype(RootLabel rootLabel, String typeLabel, String superLabel) {
    switch(rootLabel) {
        case ENTITY:
            EntityType entitySuperType = tx().concepts().getEntityType(superLabel);
            tx().concepts().getEntityType(typeLabel).setSupertype(entitySuperType);
            break;
        case ATTRIBUTE:
            AttributeType attributeSuperType = tx().concepts().getAttributeType(superLabel);
            tx().concepts().getAttributeType(typeLabel).setSupertype(attributeSuperType);
            break;
        case RELATION:
            RelationType relationSuperType = tx().concepts().getRelationType(superLabel);
            tx().concepts().getRelationType(typeLabel).setSupertype(relationSuperType);
            break;
    }
}
Also used : EntityType(com.vaticle.typedb.core.concept.type.EntityType) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) When(io.cucumber.java.en.When)

Example 24 with RelationType

use of com.vaticle.typedb.core.concept.type.RelationType in project grakn by graknlabs.

the class RelationTypeImpl method setRelates.

@Override
public void setRelates(String roleLabel) {
    validateIsNotDeleted();
    TypeVertex roleTypeVertex = graphMgr.schema().getType(roleLabel, vertex.label());
    if (roleTypeVertex == null) {
        if (getSupertypes().filter(t -> !t.equals(this) && t.isRelationType()).map(TypeImpl::asRelationType).flatMap(RelationType::getRelates).anyMatch(role -> role.getLabel().name().equals(roleLabel))) {
            throw exception(TypeDBException.of(RELATION_RELATES_ROLE_FROM_SUPERTYPE, roleLabel, getLabel()));
        } else {
            RoleTypeImpl roleType = RoleTypeImpl.of(graphMgr, roleLabel, vertex.label());
            assert roleType.getSupertype() != null;
            if (this.isAbstract())
                roleType.setAbstract();
            vertex.outs().put(RELATES, roleType.vertex);
            vertex.outs().edge(RELATES, roleType.vertex).overridden(((TypeImpl) roleType.getSupertype()).vertex);
        }
    }
}
Also used : TYPE_ROOT_MISMATCH(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeRead.TYPE_ROOT_MISMATCH) TypeEdge(com.vaticle.typedb.core.graph.edge.TypeEdge) RELATION_RELATES_ROLE_FROM_SUPERTYPE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.RELATION_RELATES_ROLE_FROM_SUPERTYPE) Order(com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.Order) RelationImpl(com.vaticle.typedb.core.concept.thing.impl.RelationImpl) RelationType(com.vaticle.typedb.core.concept.type.RelationType) ASC(com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.ASC) Forwardable.iterateSorted(com.vaticle.typedb.core.common.iterator.sorted.SortedIterators.Forwardable.iterateSorted) RELATION_NO_ROLE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.RELATION_NO_ROLE) RELATION_ABSTRACT_ROLE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.RELATION_ABSTRACT_ROLE) RELATES(com.vaticle.typedb.core.graph.common.Encoding.Edge.Type.RELATES) RELATION(com.vaticle.typedb.core.graph.common.Encoding.Vertex.Type.Root.RELATION) ROLE(com.vaticle.typedb.core.graph.common.Encoding.Vertex.Type.Root.ROLE) RELATION_RELATES_ROLE_NOT_AVAILABLE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.RELATION_RELATES_ROLE_NOT_AVAILABLE) RELATION_TYPE(com.vaticle.typedb.core.graph.common.Encoding.Vertex.Type.RELATION_TYPE) GraphManager(com.vaticle.typedb.core.graph.GraphManager) FunctionalIterator(com.vaticle.typedb.core.common.iterator.FunctionalIterator) Forwardable(com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.Forwardable) RoleType(com.vaticle.typedb.core.concept.type.RoleType) ThingVertex(com.vaticle.typedb.core.graph.vertex.ThingVertex) Objects(java.util.Objects) List(java.util.List) Relation(com.vaticle.typedb.core.concept.thing.Relation) ROOT_TYPE_MUTATION(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.ROOT_TYPE_MUTATION) Optional(java.util.Optional) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) TypeVertex(com.vaticle.typedb.core.graph.vertex.TypeVertex) TYPE_HAS_INSTANCES_SET_ABSTRACT(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.TYPE_HAS_INSTANCES_SET_ABSTRACT) Type(com.vaticle.typedb.core.concept.type.Type) TypeVertex(com.vaticle.typedb.core.graph.vertex.TypeVertex)

Example 25 with RelationType

use of com.vaticle.typedb.core.concept.type.RelationType in project grakn by graknlabs.

the class Util method getRoleType.

public static RoleType getRoleType(Relation relation, Thing player, RelationConstraint.RolePlayer rolePlayer) {
    try (FactoryTracingThreadStatic.ThreadTrace ignored = traceOnThread(TRACE_PREFIX + "get_role_type")) {
        RoleType roleType;
        Set<? extends RoleType> inferred;
        if (rolePlayer.roleType().isPresent()) {
            RelationType relationType = relation.getType();
            TypeVariable var = rolePlayer.roleType().get();
            if ((roleType = relationType.getRelates(var.label().get().label())) == null) {
                throw TypeDBException.of(TYPE_NOT_FOUND, Label.of(var.label().get().label(), relationType.getLabel().name()));
            }
        } else if ((inferred = player.getType().getPlays().filter(rt -> rt.getRelationType().equals(relation.getType())).toSet()).size() == 1) {
            roleType = inferred.iterator().next();
        } else if (inferred.size() > 1) {
            throw TypeDBException.of(ROLE_TYPE_AMBIGUOUS, rolePlayer.player().reference());
        } else {
            throw TypeDBException.of(ROLE_TYPE_MISSING, rolePlayer.player().reference());
        }
        return roleType;
    }
}
Also used : TypeVariable(com.vaticle.typedb.core.pattern.variable.TypeVariable) RoleType(com.vaticle.typedb.core.concept.type.RoleType) FactoryTracingThreadStatic(com.vaticle.factory.tracing.client.FactoryTracingThreadStatic) RelationType(com.vaticle.typedb.core.concept.type.RelationType)

Aggregations

RelationType (com.vaticle.typedb.core.concept.type.RelationType)25 EntityType (com.vaticle.typedb.core.concept.type.EntityType)20 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)18 Test (org.junit.Test)17 ConceptManager (com.vaticle.typedb.core.concept.ConceptManager)14 CoreSession (com.vaticle.typedb.core.database.CoreSession)12 CoreTransaction (com.vaticle.typedb.core.database.CoreTransaction)12 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)9 RoleType (com.vaticle.typedb.core.concept.type.RoleType)9 CoreDatabaseManager (com.vaticle.typedb.core.database.CoreDatabaseManager)9 TypeDB (com.vaticle.typedb.core.TypeDB)7 LogicManager (com.vaticle.typedb.core.logic.LogicManager)7 Options (com.vaticle.typedb.core.common.parameters.Options)4 Conjunction (com.vaticle.typedb.core.pattern.Conjunction)4 Variable (com.vaticle.typedb.core.pattern.variable.Variable)4 ThingVariable (com.vaticle.typeql.lang.pattern.variable.ThingVariable)4 FunctionalIterator (com.vaticle.typedb.core.common.iterator.FunctionalIterator)3 MB (com.vaticle.typedb.core.common.collection.Bytes.MB)2 Arguments (com.vaticle.typedb.core.common.parameters.Arguments)2 Database (com.vaticle.typedb.core.common.parameters.Options.Database)2