This method returns true if JPL was able to initiate a "call" of this Query within the Prolog engine. It is designed to be used with the getSolution() and close() methods to retrieve one or more substitutions in the form of Maps. Query q = // obtain Query reference Map soln; q.open(); while (
()
| 363 | * be set up for whatever reason, then a JPLException will be thrown. |
| 364 | */ |
| 365 | public final void open() { |
| 366 | if (open) { |
| 367 | throw new JPLException("Query is already open"); |
| 368 | } |
| 369 | if (Prolog.thread_self() == -1) { // this Java thread has no attached |
| 370 | // Prolog engine? |
| 371 | engine = Prolog.attach_pool_engine(); // may block for a while, or |
| 372 | // fail |
| 373 | //System.out.println("JPL attaching engine[" + engine.value + "] for " + |
| 374 | // this.hashCode() + ":" + this.toString()); |
| 375 | } else { // this Java thread has an attached engine |
| 376 | engine = Prolog.current_engine(); |
| 377 | //System.out.println("JPL reusing engine[" + engine.value + "] for " + |
| 378 | // this.hashCode() + ":" + this.toString()); |
| 379 | } |
| 380 | // |
| 381 | // here, we must check for a module prefix, e.g. |
| 382 | // jpl:jpl_modifier_bit(volatile,T) |
| 383 | String module; |
| 384 | Term goal; |
| 385 | if (goal_.hasFunctor(":", 2)) { |
| 386 | if (goal_.arg(1).isAtom()) { |
| 387 | module = goal_.arg(1).name(); |
| 388 | } else if (goal_.arg(1).isVariable()) { |
| 389 | throw new PrologException(Util.textParamsToTerm("error(instantiation_error,?)", new Term[] { goal_ })); |
| 390 | } else { |
| 391 | throw new PrologException( |
| 392 | Util.textParamsToTerm("error(type_error(atom,?),?)", new Term[] { goal_.arg(1), goal_ })); |
| 393 | } |
| 394 | goal = goal_.arg(2); |
| 395 | } else { |
| 396 | module = contextModule; |
| 397 | goal = goal_; |
| 398 | } |
| 399 | predicate = Prolog.predicate(goal.name(), goal.arity(), module); // was |
| 400 | // hostModule |
| 401 | fid = Prolog.open_foreign_frame(); |
| 402 | Map<String, term_t> varnames_to_vars = new HashMap<String, term_t>(); |
| 403 | term0 = Term.putArgs(goal, varnames_to_vars); |
| 404 | // THINKS: invert varnames_to_Vars and use it when getting |
| 405 | // substitutions? |
| 406 | qid = Prolog.open_query(Prolog.new_module(Prolog.new_atom(module)), Prolog.Q_CATCH_EXCEPTION, predicate, |
| 407 | term0); |
| 408 | open = true; |
| 409 | hasNextSolution = null; // we have not fetch yet any solution |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Reset the query to its start: closed and no solution found so far. This will allow a query to re-start |