Search in sources :

Example 1 with JPLException

use of org.jpl7.JPLException in project packages-jpl by SWI-Prolog.

the class Term method putParams.

/**
 * This internal method is public because it needs to be callable via JNI, but it is not part of JPL's public API
 * and should not be used by applications.
 *
 * @param ps
 * @return
 * @throws JPLException
 *             if there are more actual than formal parameters.
 */
public Term putParams(Term[] ps) {
    // necessarily (?) public
    IntHolder next = new IntHolder();
    next.value = 0;
    Term t2 = this.putParams1(next, ps);
    if (next.value != ps.length) {
        throw new JPLException("more actual params than formal");
    }
    return t2;
}
Also used : IntHolder(org.jpl7.fli.IntHolder)

Example 2 with JPLException

use of org.jpl7.JPLException in project packages-jpl by SWI-Prolog.

the class Query method close.

/**
 * This method can be used to close an open query before its solutions are
 * exhausted. It is called automatically when solutions are exhausted.
 * Calling close() on an already closed Query has no effect.
 * <p>
 *
 * Here is one way to get the first three solutions to a Query:
 *
 * <pre>
 * Query q = new Query(predicate, args);
 * Map&lt;String, Term&gt; sub1 = q.nextSolution();
 * Map&lt;String, Term&gt; sub2 = q.nextSolution();
 * Map&lt;String, Term&gt; sub3 = q.nextSolution();
 * q.close();
 * </pre>
 * <p>
 */
public final void close() {
    if (!open) {
        // it is not an error to attempt to close a closed Query
        return;
    }
    if (Prolog.thread_self() == -1) {
        throw new JPLException("no engine is attached to this thread");
    }
    if (Prolog.current_engine().value != engine.value) {
        throw new JPLException("this Query's engine is not that which is attached to this thread");
    }
    qid_t topmost = Prolog.current_query();
    if (topmost.value != this.qid.value) {
        throw new JPLException("this Query (" + this.hashCode() + ":" + this.toString() + ") is not topmost (" + topmost.hashCode() + ":" + topmost.toString() + ") within its engine[" + engine.value + "]");
    }
    Prolog.close_query(qid);
    // for tidiness
    qid = null;
    org.jpl7.fli.Prolog.discard_foreign_frame(fid);
    // for tidiness
    fid = null;
    if (Prolog.current_query() == null) {
        // only Query open in this engine?
        if (Prolog.current_engine_is_pool()) {
            // this (Query's) engine is
            // from the pool?
            Prolog.release_pool_engine();
        // System.out.println("JPL releasing engine[" + engine.value +
        // "]");
        } else {
        // System.out.println("JPL leaving engine[" + engine.value +
        // "]");
        }
    } else {
    // System.out.println("JPL retaining engine[" + engine.value + "]
    }
    // this Query is now closed
    open = false;
    // this Query, being closed, is no longer associated with
    engine = null;
// any Prolog engine
}
Also used : org.jpl7.fli.qid_t(org.jpl7.fli.qid_t)

Example 3 with JPLException

use of org.jpl7.JPLException in project packages-jpl by SWI-Prolog.

the class TestJUnit method testInteger1.

public void testInteger1() {
    try {
        // long but not
        Term i = Query.oneSolution("I is 2**40").get("I");
        // int
        i.intValue();
        fail("intValue() of bigger-than-int value failed to throw an exception");
    } catch (JPLException e) {
        if (e.getMessage().endsWith("cannot represent value as an int")) {
        // OK: an appropriate exception was thrown
        } else {
            fail("intValue() of bigger-than-int value threw incorrect JPLException: " + e);
        }
    } catch (Exception e) {
        fail("intValue() of bigger-than-int value threw unexpected class of exception: " + e);
    }
}
Also used : JPLException(org.jpl7.JPLException) Term(org.jpl7.Term) PrologException(org.jpl7.PrologException) JPLException(org.jpl7.JPLException)

Example 4 with JPLException

use of org.jpl7.JPLException in project packages-jpl by SWI-Prolog.

the class Term method getTerm.

/**
 * create and return a org.jpl7.Term representation of the given Prolog term
 *
 * @param vars_to_Vars
 *            A Map from Prolog variables to org.jpl7.Variable instances
 * @param term
 *            The Prolog term (in a term_t holder) to convert
 * @return The converted Term subclass instance.
 */
protected static Term getTerm(Map<term_t, Variable> vars_to_Vars, term_t term) {
    StringHolder hString;
    IntHolder hInt;
    Int64Holder hInt64;
    ObjectHolder hObject;
    switch(Prolog.term_type(term)) {
        case // 1
        Prolog.VARIABLE:
            for (Iterator<term_t> i = vars_to_Vars.keySet().iterator(); i.hasNext(); ) {
                // a previously seen Prolog
                term_t varX = (term_t) i.next();
                // variable
                if (Prolog.compare(varX, term) == 0) {
                    // return the
                    return (Term) vars_to_Vars.get(varX);
                // associated JPL
                // Variable
                }
            }
            // otherwise, the Prolog variable in term has not been seen before
            // allocate a new (sequentially
            Variable Var = new Variable();
            // named) Variable to represent it
            // this should become redundant...
            Var.term_ = term;
            // use Hashtable(var,null), but only
            vars_to_Vars.put(term, Var);
            // need set(var)
            return Var;
        case // 2
        Prolog.ATOM:
            hString = new StringHolder();
            // ignore return val; assume
            Prolog.get_atom_chars(term, hString);
            // success...
            return new Atom(hString.value, "text");
        case // 5
        Prolog.STRING:
            hString = new StringHolder();
            // ignore return val; assume
            Prolog.get_string_chars(term, hString);
            // success...
            return new Atom(hString.value, "string");
        case // 3
        Prolog.INTEGER:
            hInt64 = new Int64Holder();
            if (Prolog.get_integer(term, hInt64)) {
                // Java long...
                return new org.jpl7.Integer(hInt64.value);
            } else {
                hString = new StringHolder();
                if (Prolog.get_integer_big(term, hString)) {
                    // System.out.println("bigint = " + hString.value);
                    return new org.jpl7.Integer(new java.math.BigInteger(hString.value));
                } else {
                    // arbitrary
                    return new org.jpl7.Integer(-3);
                }
            }
        case // 4
        Prolog.FLOAT:
            DoubleHolder hFloatValue = new DoubleHolder();
            // assume it succeeds...
            Prolog.get_float(term, hFloatValue);
            return new org.jpl7.Float(hFloatValue.value);
        // 6
        case Prolog.COMPOUND:
        case // 9
        Prolog.LIST_PAIR:
            hString = new StringHolder();
            hInt = new IntHolder();
            // assume it succeeds
            Prolog.get_name_arity(term, hString, hInt);
            Term[] args = new Term[hInt.value];
            // term_t term1 = Prolog.new_term_refs(hArity.value);
            for (int i = 1; i <= hInt.value; i++) {
                term_t termi = Prolog.new_term_ref();
                Prolog.get_arg(i, term, termi);
                args[i - 1] = Term.getTerm(vars_to_Vars, termi);
            }
            return new Compound(hString.value, args);
        case // 7
        Prolog.LIST_NIL:
            return JPL.LIST_NIL;
        case // 8
        Prolog.BLOB:
            hObject = new ObjectHolder();
            if (Prolog.get_jref_object(term, hObject)) {
                if (hObject.value == null) {
                    return JPL.JNULL;
                } else {
                    return new JRef(hObject.value);
                }
            } else {
                throw new JPLException("unsupported blob type passed from Prolog");
            }
        default:
            // should never happen
            throw new JPLException("unknown term type=" + Prolog.term_type(term));
    }
}
Also used : DoubleHolder(org.jpl7.fli.DoubleHolder) Int64Holder(org.jpl7.fli.Int64Holder) BigInteger(java.math.BigInteger) ObjectHolder(org.jpl7.fli.ObjectHolder) StringHolder(org.jpl7.fli.StringHolder) org.jpl7.fli.term_t(org.jpl7.fli.term_t) IntHolder(org.jpl7.fli.IntHolder) BigInteger(java.math.BigInteger)

Example 5 with JPLException

use of org.jpl7.JPLException in project packages-jpl by SWI-Prolog.

the class TestJUnit method testRef7.

public void testRef7() {
    Term badJRef = new Compound("@", new Term[] { new Atom("foobar") });
    try {
        // should throw exception
        badJRef.jrefToObject();
        // shouldn't
        fail("@(foobar) .jrefToObject() shoulda thrown JPLException");
    // get
    // to
    // here
    } catch (JPLException e) {
        // expected exception class
        if (e.getMessage().endsWith("term is not a JRef")) {
        // OK: an appropriate exception was thrown
        } else {
            fail("@(foobar) .jrefToObject() threw wrong JPLException: " + e);
        }
    } catch (Exception e) {
        fail("@(foobar) .jrefToObject() threw wrong exception class: " + e);
    }
}
Also used : JPLException(org.jpl7.JPLException) Compound(org.jpl7.Compound) Term(org.jpl7.Term) Atom(org.jpl7.Atom) PrologException(org.jpl7.PrologException) JPLException(org.jpl7.JPLException)

Aggregations

JPLException (org.jpl7.JPLException)4 PrologException (org.jpl7.PrologException)4 Query (org.jpl7.Query)2 Term (org.jpl7.Term)2 IntHolder (org.jpl7.fli.IntHolder)2 BigInteger (java.math.BigInteger)1 Atom (org.jpl7.Atom)1 Compound (org.jpl7.Compound)1 DoubleHolder (org.jpl7.fli.DoubleHolder)1 Int64Holder (org.jpl7.fli.Int64Holder)1 ObjectHolder (org.jpl7.fli.ObjectHolder)1 StringHolder (org.jpl7.fli.StringHolder)1 org.jpl7.fli.qid_t (org.jpl7.fli.qid_t)1 org.jpl7.fli.term_t (org.jpl7.fli.term_t)1