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();
}
}
}
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);
}
}
}
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);
}
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);
}
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());
}
}
Aggregations