Search in sources :

Example 1 with Root

use of com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Partial.Compound.Root in project grakn by graknlabs.

the class ResolutionTest method test_exceptions_are_propagated.

@Test
public void test_exceptions_are_propagated() throws InterruptedException {
    try (CoreSession session = schemaSession()) {
        try (CoreTransaction transaction = singleThreadElgTransaction(session)) {
            transaction.query().define(TypeQL.parseQuery("define person sub entity, owns age, plays twins:twin1, plays twins:twin2;" + "age sub attribute, value long;" + "twins sub relation, relates twin1, relates twin2;"));
            transaction.commit();
        }
    }
    try (CoreSession session = dataSession()) {
        try (CoreTransaction transaction = singleThreadElgTransaction(session)) {
            transaction.query().insert(TypeQL.parseQuery("insert $p1 isa person, has age 24; $t(twin1: $p1, twin2: $p2) isa twins; $p2 isa person;"));
            transaction.query().insert(TypeQL.parseQuery("insert $p1 isa person, has age 24; $t(twin1: $p1, twin2: $p2) isa twins; $p2 isa person;"));
            transaction.query().insert(TypeQL.parseQuery("insert $p1 isa person, has age 24; $t(twin1: $p1, twin2: $p2) isa twins; $p2 isa person;"));
            transaction.commit();
        }
    }
    try (CoreSession session = dataSession()) {
        try (CoreTransaction transaction = singleThreadElgTransaction(session)) {
            Conjunction conjunctionPattern = resolvedConjunction("{ $t(twin1: $p1, twin2: $p2) isa twins; $p1 has age $a; }", transaction.logic());
            ResolverRegistry registry = transaction.reasoner().resolverRegistry();
            LinkedBlockingQueue<Match.Finished> responses = new LinkedBlockingQueue<>();
            AtomicLong doneReceived = new AtomicLong(0L);
            LinkedBlockingQueue<Throwable> exceptions = new LinkedBlockingQueue<>();
            Actor.Driver<RootResolver.Conjunction> root;
            try {
                root = registry.root(conjunctionPattern, responses::add, iterDone -> doneReceived.incrementAndGet(), exceptions::add);
            } catch (TypeDBException e) {
                fail();
            }
            Exception e = new RuntimeException();
            registry.terminateResolvers(e);
            Throwable receivedException = exceptions.poll(100, TimeUnit.MILLISECONDS);
            assertEquals(e, receivedException);
        }
    }
}
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) IOException(java.io.IOException) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) AtomicLong(java.util.concurrent.atomic.AtomicLong) 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) CoreSession(com.vaticle.typedb.core.database.CoreSession) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) Test(org.junit.Test)

Example 2 with Root

use of com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Partial.Compound.Root 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 Root

use of com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Partial.Compound.Root in project grakn by graknlabs.

the class ResolutionTest method createRootAndAssertResponses.

private void createRootAndAssertResponses(CoreTransaction transaction, Disjunction disjunction, Set<Identifier.Variable.Retrievable> filter, long answerCount, long explainableAnswers) throws InterruptedException {
    ResolverRegistry registry = transaction.reasoner().resolverRegistry();
    LinkedBlockingQueue<Match.Finished> responses = new LinkedBlockingQueue<>();
    AtomicLong doneReceived = new AtomicLong(0L);
    Actor.Driver<RootResolver.Disjunction> root;
    try {
        root = registry.root(disjunction, 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) AtomicLong(java.util.concurrent.atomic.AtomicLong) Util.resolvedDisjunction(com.vaticle.typedb.core.reasoner.resolution.Util.resolvedDisjunction) Disjunction(com.vaticle.typedb.core.pattern.Disjunction) Actor(com.vaticle.typedb.core.concurrent.actor.Actor) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Aggregations

Collections.set (com.vaticle.typedb.common.collection.Collections.set)3 NamedThreadFactory (com.vaticle.typedb.common.concurrent.NamedThreadFactory)3 MB (com.vaticle.typedb.core.common.collection.Bytes.MB)3 TypeDBException (com.vaticle.typedb.core.common.exception.TypeDBException)3 Iterators.iterate (com.vaticle.typedb.core.common.iterator.Iterators.iterate)3 Arguments (com.vaticle.typedb.core.common.parameters.Arguments)3 Database (com.vaticle.typedb.core.common.parameters.Options.Database)3 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)3 Actor (com.vaticle.typedb.core.concurrent.actor.Actor)3 ActorExecutorGroup (com.vaticle.typedb.core.concurrent.actor.ActorExecutorGroup)3 CoreDatabaseManager (com.vaticle.typedb.core.database.CoreDatabaseManager)3 CoreSession (com.vaticle.typedb.core.database.CoreSession)3 CoreTransaction (com.vaticle.typedb.core.database.CoreTransaction)3 Conjunction (com.vaticle.typedb.core.pattern.Conjunction)3 Disjunction (com.vaticle.typedb.core.pattern.Disjunction)3 Variable (com.vaticle.typedb.core.pattern.variable.Variable)3 Util.resolvedConjunction (com.vaticle.typedb.core.reasoner.resolution.Util.resolvedConjunction)3 Util.resolvedDisjunction (com.vaticle.typedb.core.reasoner.resolution.Util.resolvedDisjunction)3 Root (com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Partial.Compound.Root)3 Match (com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Top.Match)3