Search in sources :

Example 1 with Variable

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

the class RuleTest method rule_contains_indexes_prevent_undefining_contained_types.

@Test
public void rule_contains_indexes_prevent_undefining_contained_types() 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 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();
            }
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                EntityType person = conceptMgr.getEntityType("person");
                assertThrowsTypeDBException(person::delete, ErrorMessage.TypeWrite.TYPE_REFERENCED_IN_RULES.code());
            }
        }
    }
}
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 2 with Variable

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

the class ResolutionTest method createRootAndAssertResponses.

private void createRootAndAssertResponses(CoreTransaction transaction, Conjunction conjunction, long answerCount, long explainableAnswers) throws InterruptedException {
    ResolverRegistry registry = transaction.reasoner().resolverRegistry();
    LinkedBlockingQueue<Match.Finished> responses = new LinkedBlockingQueue<>();
    AtomicLong doneReceived = new AtomicLong(0L);
    Set<Identifier.Variable.Retrievable> filter = new HashSet<>();
    iterate(conjunction.variables()).map(Variable::id).filter(Identifier::isName).map(Identifier.Variable::asName).forEachRemaining(filter::add);
    Actor.Driver<RootResolver.Conjunction> root;
    try {
        root = registry.root(conjunction, responses::add, iterDone -> doneReceived.incrementAndGet(), (throwable) -> fail());
    } catch (TypeDBException e) {
        fail();
        return;
    }
    assertResponses(root, filter, responses, doneReceived, answerCount, explainableAnswers);
}
Also used : Util.resolvedConjunction(com.vaticle.typedb.core.reasoner.resolution.Util.resolvedConjunction) Collections.set(com.vaticle.typedb.common.collection.Collections.set) Variable(com.vaticle.typedb.core.pattern.variable.Variable) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) MB(com.vaticle.typedb.core.common.collection.Bytes.MB) Arguments(com.vaticle.typedb.core.common.parameters.Arguments) HashSet(java.util.HashSet) Root(com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Partial.Compound.Root) Util.resolvedDisjunction(com.vaticle.typedb.core.reasoner.resolution.Util.resolvedDisjunction) After(org.junit.After) InitialImpl(com.vaticle.typedb.core.reasoner.resolution.answer.AnswerStateImpl.TopImpl.MatchImpl.InitialImpl) Assert.fail(org.junit.Assert.fail) Path(java.nio.file.Path) Before(org.junit.Before) Conjunction(com.vaticle.typedb.core.pattern.Conjunction) TypeQL(com.vaticle.typeql.lang.TypeQL) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) Set(java.util.Set) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Test(org.junit.Test) IOException(java.io.IOException) RootResolver(com.vaticle.typedb.core.reasoner.resolution.resolver.RootResolver) Actor(com.vaticle.typedb.core.concurrent.actor.Actor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Disjunction(com.vaticle.typedb.core.pattern.Disjunction) TimeUnit(java.util.concurrent.TimeUnit) CoreSession(com.vaticle.typedb.core.database.CoreSession) AtomicLong(java.util.concurrent.atomic.AtomicLong) Stream(java.util.stream.Stream) NamedThreadFactory(com.vaticle.typedb.common.concurrent.NamedThreadFactory) Paths(java.nio.file.Paths) TestCase.assertTrue(junit.framework.TestCase.assertTrue) ActorExecutorGroup(com.vaticle.typedb.core.concurrent.actor.ActorExecutorGroup) Database(com.vaticle.typedb.core.common.parameters.Options.Database) Iterators.iterate(com.vaticle.typedb.core.common.iterator.Iterators.iterate) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) Request(com.vaticle.typedb.core.reasoner.resolution.framework.Request) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Match(com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Top.Match) Resolver(com.vaticle.typedb.core.reasoner.resolution.framework.Resolver) Util(com.vaticle.typedb.core.test.integration.util.Util) TestCase.assertEquals(junit.framework.TestCase.assertEquals) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) AtomicLong(java.util.concurrent.atomic.AtomicLong) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) Actor(com.vaticle.typedb.core.concurrent.actor.Actor) Util.resolvedConjunction(com.vaticle.typedb.core.reasoner.resolution.Util.resolvedConjunction) Conjunction(com.vaticle.typedb.core.pattern.Conjunction) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) HashSet(java.util.HashSet)

Example 3 with Variable

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

the class Rule method pruneThenResolvedTypes.

/**
 * Remove type hints in the `then` pattern that are not valid in the `when` pattern
 */
private void pruneThenResolvedTypes() {
    then.variables().stream().filter(variable -> variable.id().isName()).forEach(thenVar -> {
        Variable whenVar = when.variable(thenVar.id());
        thenVar.retainInferredTypes(whenVar.inferredTypes());
        if (thenVar.inferredTypes().isEmpty())
            then.setCoherent(false);
    });
}
Also used : RULE_THEN_INVALID_VALUE_ASSIGNMENT(com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.RULE_THEN_INVALID_VALUE_ASSIGNMENT) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) LoggerFactory(org.slf4j.LoggerFactory) RelationType(com.vaticle.typedb.core.concept.type.RelationType) RULE_CONCLUSION_AMBIGUOUS_LABELLED_TYPE(com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.RULE_CONCLUSION_AMBIGUOUS_LABELLED_TYPE) COLON(com.vaticle.typeql.lang.common.TypeQLToken.Char.COLON) Map(java.util.Map) ValueConstraint(com.vaticle.typedb.core.pattern.constraint.thing.ValueConstraint) ThingConstraint(com.vaticle.typedb.core.pattern.constraint.thing.ThingConstraint) Collections.list(com.vaticle.typedb.common.collection.Collections.list) Thing(com.vaticle.typedb.core.concept.thing.Thing) THEN(com.vaticle.typeql.lang.common.TypeQLToken.Schema.THEN) NEW_LINE(com.vaticle.typeql.lang.common.TypeQLToken.Char.NEW_LINE) FunctionalIterator(com.vaticle.typedb.core.common.iterator.FunctionalIterator) Negation(com.vaticle.typedb.core.pattern.Negation) IsaConstraint(com.vaticle.typedb.core.pattern.constraint.thing.IsaConstraint) Set(java.util.Set) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Pattern(com.vaticle.typeql.lang.pattern.Pattern) HasConstraint(com.vaticle.typedb.core.pattern.constraint.thing.HasConstraint) CURLY_OPEN(com.vaticle.typeql.lang.common.TypeQLToken.Char.CURLY_OPEN) List(java.util.List) RelationConstraint(com.vaticle.typedb.core.pattern.constraint.thing.RelationConstraint) Optional(java.util.Optional) VariableRegistry(com.vaticle.typedb.core.pattern.variable.VariableRegistry) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) RULE(com.vaticle.typeql.lang.common.TypeQLToken.Schema.RULE) ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) ILLEGAL_STATE(com.vaticle.typedb.core.common.exception.ErrorMessage.Internal.ILLEGAL_STATE) TraversalEngine(com.vaticle.typedb.core.traversal.TraversalEngine) Collections.set(com.vaticle.typedb.common.collection.Collections.set) Variable(com.vaticle.typedb.core.pattern.variable.Variable) SEMICOLON(com.vaticle.typeql.lang.common.TypeQLToken.Char.SEMICOLON) HashMap(java.util.HashMap) Objects.className(com.vaticle.typedb.common.util.Objects.className) RULE_THEN_CANNOT_BE_SATISFIED(com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.RULE_THEN_CANNOT_BE_SATISFIED) SPACE(com.vaticle.typeql.lang.common.TypeQLToken.Char.SPACE) RULE_CONCLUSION_ILLEGAL_INSERT(com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.RULE_CONCLUSION_ILLEGAL_INSERT) Reference(com.vaticle.typeql.lang.pattern.variable.Reference) Concept(com.vaticle.typedb.core.concept.Concept) HashSet(java.util.HashSet) Concludable(com.vaticle.typedb.core.logic.resolvable.Concludable) Label(com.vaticle.typedb.core.common.parameters.Label) GraphManager(com.vaticle.typedb.core.graph.GraphManager) Conjunction(com.vaticle.typedb.core.pattern.Conjunction) RelationTraversal(com.vaticle.typedb.core.traversal.RelationTraversal) Logger(org.slf4j.Logger) ThingVariable(com.vaticle.typedb.core.pattern.variable.ThingVariable) RuleStructure(com.vaticle.typedb.core.graph.structure.RuleStructure) Iterators(com.vaticle.typedb.core.common.iterator.Iterators) RULE_WHEN_CANNOT_BE_SATISFIED(com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.RULE_WHEN_CANNOT_BE_SATISFIED) RoleType(com.vaticle.typedb.core.concept.type.RoleType) Disjunction(com.vaticle.typedb.core.pattern.Disjunction) CURLY_CLOSE(com.vaticle.typeql.lang.common.TypeQLToken.Char.CURLY_CLOSE) Attribute(com.vaticle.typedb.core.concept.thing.Attribute) TypeVariable(com.vaticle.typedb.core.pattern.variable.TypeVariable) INVALID_CASTING(com.vaticle.typedb.core.common.exception.ErrorMessage.Pattern.INVALID_CASTING) Iterators.iterate(com.vaticle.typedb.core.common.iterator.Iterators.iterate) INVALID_NEGATION_CONTAINS_DISJUNCTION(com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.INVALID_NEGATION_CONTAINS_DISJUNCTION) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) WHEN(com.vaticle.typeql.lang.common.TypeQLToken.Schema.WHEN) Variable(com.vaticle.typedb.core.pattern.variable.Variable) ThingVariable(com.vaticle.typedb.core.pattern.variable.ThingVariable) TypeVariable(com.vaticle.typedb.core.pattern.variable.TypeVariable)

Example 4 with Variable

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

the class Reasoner method mayReason.

private boolean mayReason(Disjunction disjunction) {
    for (Conjunction conj : disjunction.conjunctions()) {
        Set<Variable> vars = conj.variables();
        List<Negation> negs = conj.negations();
        if (iterate(vars).flatMap(v -> iterate(v.inferredTypes())).distinct().anyMatch(this::hasRule))
            return true;
        if (!negs.isEmpty() && iterate(negs).anyMatch(n -> mayReason(n.disjunction())))
            return true;
    }
    return false;
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) Context(com.vaticle.typedb.core.common.parameters.Context) TraversalEngine(com.vaticle.typedb.core.traversal.TraversalEngine) Collections.set(com.vaticle.typedb.common.collection.Collections.set) Variable(com.vaticle.typedb.core.pattern.variable.Variable) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) UnboundVariable(com.vaticle.typeql.lang.pattern.variable.UnboundVariable) UNSATISFIABLE_SUB_PATTERN(com.vaticle.typedb.core.common.exception.ErrorMessage.Pattern.UNSATISFIABLE_SUB_PATTERN) LoggerFactory(org.slf4j.LoggerFactory) PARALLELISATION_FACTOR(com.vaticle.typedb.core.concurrent.executor.Executors.PARALLELISATION_FACTOR) ResolverRegistry(com.vaticle.typedb.core.reasoner.resolution.ResolverRegistry) Arguments(com.vaticle.typedb.core.common.parameters.Arguments) Producers.produce(com.vaticle.typedb.core.concurrent.producer.Producers.produce) LogicManager(com.vaticle.typedb.core.logic.LogicManager) HashSet(java.util.HashSet) Executors.async1(com.vaticle.typedb.core.concurrent.executor.Executors.async1) Either(com.vaticle.typedb.common.collection.Either) Collections.list(com.vaticle.typedb.common.collection.Collections.list) Thing(com.vaticle.typedb.core.concept.thing.Thing) Label(com.vaticle.typedb.core.common.parameters.Label) Conjunction(com.vaticle.typedb.core.pattern.Conjunction) Logger(org.slf4j.Logger) FunctionalIterator(com.vaticle.typedb.core.common.iterator.FunctionalIterator) Negation(com.vaticle.typedb.core.pattern.Negation) Options(com.vaticle.typedb.core.common.parameters.Options) Producer(com.vaticle.typedb.core.concurrent.producer.Producer) Set(java.util.Set) Iterators(com.vaticle.typedb.core.common.iterator.Iterators) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Disjunction(com.vaticle.typedb.core.pattern.Disjunction) UNSATISFIABLE_PATTERN(com.vaticle.typedb.core.common.exception.ErrorMessage.Pattern.UNSATISFIABLE_PATTERN) List(java.util.List) Executors.actor(com.vaticle.typedb.core.concurrent.executor.Executors.actor) Iterators.iterate(com.vaticle.typedb.core.common.iterator.Iterators.iterate) Explanation(com.vaticle.typedb.core.reasoner.resolution.answer.Explanation) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) EXHAUSTIVE(com.vaticle.typedb.core.common.parameters.Arguments.Query.Producer.EXHAUSTIVE) TypeQLMatch(com.vaticle.typeql.lang.query.TypeQLMatch) Type(com.vaticle.typedb.core.concept.type.Type) Producers(com.vaticle.typedb.core.concurrent.producer.Producers) Variable(com.vaticle.typedb.core.pattern.variable.Variable) UnboundVariable(com.vaticle.typeql.lang.pattern.variable.UnboundVariable) Negation(com.vaticle.typedb.core.pattern.Negation) Conjunction(com.vaticle.typedb.core.pattern.Conjunction)

Example 5 with Variable

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

the class RuleTest method new_type_updates_rule_conclusion_index.

@Test
public void new_type_updates_rule_conclusion_index() 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 marriage = conceptMgr.putRelationType("marriage");
                AttributeType name = conceptMgr.putAttributeType("name", AttributeType.ValueType.STRING);
                name.setAbstract();
                AttributeType firstName = conceptMgr.putAttributeType("first-name", AttributeType.ValueType.STRING);
                firstName.setSupertype(name);
                marriage.setRelates("spouse");
                person.setPlays(marriage.getRelates("spouse"));
                person.setOwns(firstName);
                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("first-name")), nameAttr.inferredTypes());
                txn.commit();
            }
            // add a new subtype of an attribute in a rule
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                AttributeType lastName = conceptMgr.putAttributeType("last-name", AttributeType.ValueType.STRING);
                lastName.setSupertype(conceptMgr.getAttributeType("name"));
                conceptMgr.getEntityType("person").setOwns(lastName);
                txn.commit();
            }
            // check the new attribute type is re-indexed in the conclusions index
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.READ)) {
                LogicManager logicMgr = txn.logic();
                Rule marriageSameName = logicMgr.getRule("marriage-same-name");
                assertEquals(set(marriageSameName), logicMgr.rulesConcludingHas(Label.of("last-name")).toSet());
            }
        }
    }
}
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)

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