Search in sources :

Example 1 with TypeDBException

use of com.vaticle.typedb.core.common.exception.TypeDBException in project grakn by graknlabs.

the class ReiterationTest method test_first_iteration_exhausts_and_second_iteration_recurses_infinitely.

@Test
public void test_first_iteration_exhausts_and_second_iteration_recurses_infinitely() throws InterruptedException {
    try (CoreSession session = schemaSession()) {
        try (CoreTransaction transaction = singleThreadElgTransaction(session)) {
            transaction.query().define(TypeQL.parseQuery("define " + "X sub relation, relates item, plays Y:item;" + "Y sub relation, relates item, plays X:item;" + "object sub entity, plays X:item, plays Y:item;" + "rule rule-b: when {" + "$r(item:$x) isa X;" + "} then {" + "(item:$r) isa Y;" + "};" + "rule rule-a: when {" + "$r(item:$x) isa Y;" + "} then {" + "(item:$r) isa X;" + "};" + "rule rule-c: when {" + "$o isa object;" + "} then {" + "(item:$o) isa X;" + "};"));
            transaction.commit();
        }
    }
    try (CoreSession session = dataSession()) {
        try (CoreTransaction transaction = singleThreadElgTransaction(session)) {
            transaction.query().insert(TypeQL.parseQuery("insert $o isa object;"));
            transaction.commit();
        }
    }
    try (CoreSession session = dataSession()) {
        try (CoreTransaction transaction = singleThreadElgTransaction(session)) {
            Conjunction conjunction = resolvedConjunction("{ $y isa Y; }", transaction.logic());
            Set<Identifier.Variable.Retrievable> filter = new HashSet<>();
            iterate(conjunction.variables()).map(Variable::id).filter(Identifier::isName).map(Identifier.Variable::asName).forEachRemaining(filter::add);
            ResolverRegistry registry = transaction.reasoner().resolverRegistry();
            LinkedBlockingQueue<Match.Finished> responses = new LinkedBlockingQueue<>();
            LinkedBlockingQueue<Integer> failed = new LinkedBlockingQueue<>();
            int[] iteration = { 0 };
            int[] doneInIteration = { 0 };
            boolean[] receivedInferredAnswer = { false };
            ResolutionTracer.get().start();
            Actor.Driver<RootResolver.Conjunction> root = registry.root(conjunction, answer -> {
                if (iterate(answer.conceptMap().concepts().entrySet()).map(e -> e.getValue().asThing().isInferred()).first().isPresent()) {
                    receivedInferredAnswer[0] = true;
                }
                responses.add(answer);
            }, iterDone -> {
                assert iteration[0] == iterDone;
                doneInIteration[0]++;
                failed.add(iterDone);
            }, throwable -> fail());
            Set<Match.Finished> answers = new HashSet<>();
            // iteration 0
            sendRootRequest(root, filter, iteration[0]);
            answers.add(responses.take());
            ResolutionTracer.get().finish();
            ResolutionTracer.get().start();
            sendRootRequest(root, filter, iteration[0]);
            // Block and wait for an failed message
            failed.take();
            ResolutionTracer.get().finish();
            assertTrue(receivedInferredAnswer[0]);
            assertEquals(1, doneInIteration[0]);
            // iteration 1 onwards
            for (int j = 0; j <= 100; j++) {
                ResolutionTracer.get().start();
                sendRootRequest(root, filter, iteration[0]);
                Match.Finished re = responses.poll(100, MILLISECONDS);
                if (re == null) {
                    Integer ex = failed.poll(100, MILLISECONDS);
                    if (ex == null) {
                        ResolutionTracer.get().finish();
                        fail();
                    }
                    // Reset the iteration
                    iteration[0]++;
                    receivedInferredAnswer[0] = false;
                    doneInIteration[0] = 0;
                }
                ResolutionTracer.get().finish();
            }
        } catch (TypeDBException e) {
            e.printStackTrace();
            fail();
        }
    }
}
Also used : LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Match(com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Top.Match) 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) CoreSession(com.vaticle.typedb.core.database.CoreSession) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with TypeDBException

use of com.vaticle.typedb.core.common.exception.TypeDBException 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 3 with TypeDBException

use of com.vaticle.typedb.core.common.exception.TypeDBException 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 4 with TypeDBException

use of com.vaticle.typedb.core.common.exception.TypeDBException in project grakn by graknlabs.

the class TypeDBServer method main.

public static void main(String[] args) {
    try {
        printASCIILogo();
        CoreConfigParser configParser = new CoreConfigParser();
        ArgsParser<ServerSubcommand> argsParser = new ArgsParser<ServerSubcommand>().subcommand(new ServerSubcommandParser.Server(configParser)).subcommand(new ServerSubcommandParser.Import()).subcommand(new ServerSubcommandParser.Export());
        Optional<ServerSubcommand> subcmd = argsParser.parse(args);
        if (subcmd.isEmpty()) {
            LOG.error(UNRECOGNISED_CLI_COMMAND.message(String.join(" ", args)));
            LOG.error(argsParser.usage());
            System.exit(1);
        } else {
            if (subcmd.get().isServer()) {
                ServerSubcommand.Server subcmdServer = subcmd.get().asServer();
                if (subcmdServer.isHelp())
                    System.out.println(argsParser.help());
                else if (subcmdServer.isVersion())
                    System.out.println("Version: " + Version.VERSION);
                else
                    runServer(subcmdServer);
            } else if (subcmd.get().isImport()) {
                importData(subcmd.get().asImport());
            } else if (subcmd.get().isExport()) {
                exportData(subcmd.get().asExport());
            } else
                throw TypeDBException.of(ILLEGAL_STATE);
        }
    } catch (Exception e) {
        if (e instanceof TypeDBException) {
            LOG.error(e.getMessage());
        } else {
            LOG.error(e.getMessage(), e);
            LOG.error(EXITED_WITH_ERROR.message());
        }
        System.exit(1);
    }
    System.exit(0);
}
Also used : TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) ServerSubcommandParser(com.vaticle.typedb.core.server.parameters.ServerSubcommandParser) CoreConfigParser(com.vaticle.typedb.core.server.parameters.CoreConfigParser) BindException(java.net.BindException) IOException(java.io.IOException) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) ServerSubcommand(com.vaticle.typedb.core.server.parameters.ServerSubcommand)

Example 5 with TypeDBException

use of com.vaticle.typedb.core.common.exception.TypeDBException in project grakn by graknlabs.

the class CoreConfigTest method config_invalid_path_throws.

@Test
public void config_invalid_path_throws() {
    Path configMissing = Util.getTypedbDir().resolve("server/test/missing.yml");
    try {
        CoreConfigFactory.config(configMissing, new HashSet<>(), new CoreConfigParser());
        fail();
    } catch (TypeDBException e) {
        assert e.code().isPresent();
        assertEquals(CONFIG_FILE_NOT_FOUND.code(), e.code().get());
    }
}
Also used : Path(java.nio.file.Path) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) CoreConfigParser(com.vaticle.typedb.core.server.parameters.CoreConfigParser) Test(org.junit.Test)

Aggregations

TypeDBException (com.vaticle.typedb.core.common.exception.TypeDBException)16 Test (org.junit.Test)11 Path (java.nio.file.Path)9 CoreConfigParser (com.vaticle.typedb.core.server.parameters.CoreConfigParser)7 CoreSession (com.vaticle.typedb.core.database.CoreSession)5 CoreTransaction (com.vaticle.typedb.core.database.CoreTransaction)5 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)4 Actor (com.vaticle.typedb.core.concurrent.actor.Actor)4 Conjunction (com.vaticle.typedb.core.pattern.Conjunction)4 Disjunction (com.vaticle.typedb.core.pattern.Disjunction)4 Util.resolvedConjunction (com.vaticle.typedb.core.reasoner.resolution.Util.resolvedConjunction)4 Match (com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Top.Match)4 Identifier (com.vaticle.typedb.core.traversal.common.Identifier)4 IOException (java.io.IOException)4 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 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