Search in sources :

Example 11 with org.jpl7

use of org.jpl7 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>
 */
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 any Prolog engine
    engine = null;
}
Also used : org.jpl7.fli.qid_t(org.jpl7.fli.qid_t)

Example 12 with org.jpl7

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

the class Test_Dict method test_dict4.

@Test
public void test_dict4() {
    Map<Atom, Term> map = new HashMap<Atom, Term>();
    map.put(new Atom("x"), new org.jpl7.Integer(12));
    map.put(new Atom("y"), new org.jpl7.Integer(23));
    map.put(new Atom("z"), new Integer(312));
    Dict dict = new Dict(new Variable("W"), map);
    assertEquals("W{x:12, z:312, y:23}", dict.toString());
    assertEquals(Prolog.DICT, dict.type());
}
Also used : org.jpl7(org.jpl7) Integer(org.jpl7.Integer) HashMap(java.util.HashMap) Integer(org.jpl7.Integer) Test(org.junit.Test)

Example 13 with org.jpl7

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

the class TestJUnit method testBigInteger2.

public void testBigInteger2() {
    BigInteger b = new BigInteger("12345678901234567890123456789");
    // too big for a long
    Term i = new Integer(b);
    Term g = new Compound("is", new Term[] { new Variable("X"), i });
    Term x = Query.oneSolution(g).get("X");
    assertTrue("X is an org.jpl7.Integer", x.isInteger());
    assertTrue("X is a big org.jpl7.Integer", x.isBigInteger());
    assertTrue("X's value is as expected", x.bigValue().equals(b));
}
Also used : Integer(org.jpl7.Integer) BigInteger(java.math.BigInteger) Variable(org.jpl7.Variable) BigInteger(java.math.BigInteger) Compound(org.jpl7.Compound) Term(org.jpl7.Term)

Example 14 with org.jpl7

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

the class TestJUnit method testWouter1.

public void testWouter1() {
    // Wouter says this fails under OS X Mavericks
    // 10.9 x86-64
    // too big for an int
    long n = 7381783232223l;
    Compound term = new Compound("is", new Term[] { new Variable("X"), new org.jpl7.Integer(n) });
    Map<String, Term>[] solutions = new Query(term).allSolutions();
    assertEquals(1, solutions.length);
    Map<String, Term> solution = solutions[0];
    assertTrue(solution.containsKey("X"));
    Object result = solution.get("X");
    assertTrue(result instanceof org.jpl7.Integer);
    assertEquals(n, ((org.jpl7.Integer) result).longValue());
}
Also used : Variable(org.jpl7.Variable) Query(org.jpl7.Query) Integer(org.jpl7.Integer) Compound(org.jpl7.Compound) Term(org.jpl7.Term) Map(java.util.Map)

Example 15 with org.jpl7

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

the class TestJUnit method testLength1.

public void testLength1() {
    Query q5 = new Query(new Compound("length", new Term[] { new Variable("Zs"), new org.jpl7.Integer(2) }));
    Term zs = q5.oneSolution().get("Zs");
    assertTrue("length(Zs,2) binds Zs to a list of two distinct variables " + zs.toString(), zs.isListPair() && zs.arg(1).isVariable() && zs.arg(2).isListPair() && zs.arg(2).arg(1).isVariable() && zs.arg(2).arg(2).isListNil() && !zs.arg(1).name().equals(zs.arg(2).arg(1).name()));
}
Also used : Integer(org.jpl7.Integer) BigInteger(java.math.BigInteger) Variable(org.jpl7.Variable) Query(org.jpl7.Query) Compound(org.jpl7.Compound) Term(org.jpl7.Term)

Aggregations

Term (org.jpl7.Term)15 Query (org.jpl7.Query)12 Variable (org.jpl7.Variable)9 Map (java.util.Map)7 Integer (org.jpl7.Integer)7 BigInteger (java.math.BigInteger)5 Compound (org.jpl7.Compound)5 Atom (org.jpl7.Atom)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 URL (java.net.URL)2 org.jpl7 (org.jpl7)2 org.jpl7.fli.term_t (org.jpl7.fli.term_t)2 DoubleHolder (org.jpl7.fli.DoubleHolder)1 Int64Holder (org.jpl7.fli.Int64Holder)1 IntHolder (org.jpl7.fli.IntHolder)1 ObjectHolder (org.jpl7.fli.ObjectHolder)1 StringHolder (org.jpl7.fli.StringHolder)1 org.jpl7.fli.qid_t (org.jpl7.fli.qid_t)1