Search in sources :

Example 6 with Variable

use of com.vaticle.typedb.core.pattern.variable.Variable in project grakn by graknlabs.

the class RuleTest method rule_indexes_created_and_readable.

// ------------ Rule conclusion indexing ------------
@Test
public void rule_indexes_created_and_readable() 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);
                AttributeType age = conceptMgr.putAttributeType("age", AttributeType.ValueType.LONG);
                marriage.setRelates("spouse");
                person.setPlays(friendship.getRelates("friend"));
                person.setPlays(marriage.getRelates("spouse"));
                person.setOwns(name);
                person.setOwns(age);
                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());
                Rule peopleHaveAge10 = logicMgr.putRule("people-have-age-10", TypeQL.parsePattern("{ $x isa person; }").asConjunction(), TypeQL.parseVariable("$x has age 10").asThing());
                Conjunction age10 = peopleHaveAge10.then();
                Variable ageAttr = getVariable(age10.variables(), Identifier.Variable.anon(0));
                assertEquals(set(Label.of("age")), ageAttr.inferredTypes());
                txn.commit();
            }
        }
        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");
                Rule allFriendsRule = txn.logic().getRule("all-people-are-friends");
                assertEquals(set(marriageFriendsRule, allFriendsRule), friendshipRules);
                Set<Rule> hasNameRules = logicMgr.rulesConcludingHas(Label.of("name")).toSet();
                Rule marriageSameName = txn.logic().getRule("marriage-same-name");
                assertEquals(set(marriageSameName), hasNameRules);
                Set<Rule> hasAgeRules = logicMgr.rulesConcludingHas(Label.of("age")).toSet();
                Set<Rule> ageRules = logicMgr.rulesConcluding(Label.of("age")).toSet();
                Rule peopleHaveAge10 = txn.logic().getRule("people-have-age-10");
                assertEquals(set(peopleHaveAge10), hasAgeRules);
                assertEquals(set(peopleHaveAge10), ageRules);
            }
        }
    }
}
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 7 with Variable

use of com.vaticle.typedb.core.pattern.variable.Variable 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 8 with Variable

use of com.vaticle.typedb.core.pattern.variable.Variable in project grakn by graknlabs.

the class Negation method create.

public static Negation create(com.vaticle.typeql.lang.pattern.Negation<?> typeql, VariableRegistry bounds) {
    try (ThreadTrace ignored = traceOnThread(TRACE_PREFIX + "create")) {
        Disjunction disjunction = Disjunction.create(typeql.normalise().pattern(), bounds);
        disjunction.conjunctions().forEach(conjunction -> {
            if (iterate(conjunction.variables()).map(Variable::reference).filter(Reference::isName).noneMatch(bounds::contains)) {
                throw TypeDBException.of(UNBOUNDED_NEGATION);
            }
        });
        return new Negation(disjunction);
    }
}
Also used : Variable(com.vaticle.typedb.core.pattern.variable.Variable) ThreadTrace(com.vaticle.factory.tracing.client.FactoryTracingThreadStatic.ThreadTrace)

Example 9 with Variable

use of com.vaticle.typedb.core.pattern.variable.Variable in project grakn by graknlabs.

the class Resolver method compatibleBounds.

private Optional<ConceptMap> compatibleBounds(Conjunction conjunction, ConceptMap bounds) {
    Map<Retrievable, Concept> newBounds = new HashMap<>();
    for (Map.Entry<Retrievable, ? extends Concept> entry : bounds.concepts().entrySet()) {
        Retrievable id = entry.getKey();
        Concept bound = entry.getValue();
        Variable conjVariable = conjunction.variable(id);
        assert conjVariable != null;
        if (conjVariable.isThing()) {
            if (!conjVariable.asThing().iid().isPresent())
                newBounds.put(id, bound);
            else if (!conjVariable.asThing().iid().get().iid().equals(bound.asThing().getIID())) {
                return Optional.empty();
            }
        } else if (conjVariable.isType()) {
            if (!conjVariable.asType().label().isPresent())
                newBounds.put(id, bound);
            else if (!conjVariable.asType().label().get().properLabel().equals(bound.asType().getLabel())) {
                return Optional.empty();
            }
        } else {
            throw TypeDBException.of(ILLEGAL_STATE);
        }
    }
    return Optional.of(new ConceptMap(newBounds));
}
Also used : Concept(com.vaticle.typedb.core.concept.Concept) Variable(com.vaticle.typedb.core.pattern.variable.Variable) Retrievable(com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable) HashMap(java.util.HashMap) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) HashMap(java.util.HashMap) Map(java.util.Map) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap)

Aggregations

Variable (com.vaticle.typedb.core.pattern.variable.Variable)9 Conjunction (com.vaticle.typedb.core.pattern.Conjunction)7 ConceptManager (com.vaticle.typedb.core.concept.ConceptManager)6 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)5 RelationType (com.vaticle.typedb.core.concept.type.RelationType)5 CoreDatabaseManager (com.vaticle.typedb.core.database.CoreDatabaseManager)5 CoreSession (com.vaticle.typedb.core.database.CoreSession)5 CoreTransaction (com.vaticle.typedb.core.database.CoreTransaction)5 Test (org.junit.Test)5 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)4 Collections.set (com.vaticle.typedb.common.collection.Collections.set)3 TypeDBException (com.vaticle.typedb.core.common.exception.TypeDBException)3 Iterators.iterate (com.vaticle.typedb.core.common.iterator.Iterators.iterate)3 EntityType (com.vaticle.typedb.core.concept.type.EntityType)3 Disjunction (com.vaticle.typedb.core.pattern.Disjunction)3 Identifier (com.vaticle.typedb.core.traversal.common.Identifier)3 ThingVariable (com.vaticle.typeql.lang.pattern.variable.ThingVariable)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 Collections.list (com.vaticle.typedb.common.collection.Collections.list)2