Search in sources :

Example 1 with org.jpl7.fli.term_t

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

the class FamilyMT method run.

public void run() {
    Map<String, Term> solution;
    // Variable X = new Variable("X");
    // 
    Query q2 = new Query("child_of(joe,ralf)");
    System.err.println("child_of(joe,ralf) is " + (q2.hasSolution() ? "provable" : "not provable"));
    new Query("sleep(?)", new Term[] { new org.jpl7.Integer(delay) }).hasSolution();
    // 
    Query q3 = new Query("descendent_of(steve,ralf)");
    System.err.println("descendent_of(steve,ralf) is " + (q3.hasSolution() ? "provable" : "not provable"));
    delay();
    // 
    Query q4 = new Query("descendent_of(X, ralf)");
    solution = q4.oneSolution();
    System.err.println("first solution of descendent_of(X, ralf)");
    System.err.println("X = " + solution.get("X"));
    delay();
    // 
    Map<String, Term>[] solutions = q4.allSolutions();
    System.err.println("all solutions of descendent_of(X, ralf)");
    for (int i = 0; i < solutions.length; i++) {
        System.err.println("X = " + solutions[i].get("X"));
    }
    delay();
    // 
    System.err.println("each solution of descendent_of(X, ralf)");
    while (q4.hasMoreSolutions()) {
        solution = q4.nextSolution();
        System.err.println("X = " + solution.get("X"));
    }
    delay();
    // 
    Query q5 = new Query("descendent_of(X, Y)");
    System.err.println(id + ": each solution of descendent_of(X, Y)");
    while (q5.hasMoreSolutions()) {
        solution = q5.nextSolution();
        System.err.println(id + ": X = " + solution.get("X") + ", Y = " + solution.get("Y"));
        delay();
    }
}
Also used : Query(org.jpl7.Query) Term(org.jpl7.Term) Map(java.util.Map)

Example 2 with org.jpl7.fli.term_t

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

the class Term method putTerms.

/**
 * This static method converts an array of Terms to a *consecutive* sequence of term_t objects. Note that the first
 * term_t object returned is a term_t class (structure); the succeeding term_t objects are consecutive references
 * obtained by incrementing the *value* field of the term_t.
 *
 * @param varnames_to_vars
 *            Map from variable names to JPL Variables.
 * @param args
 *            An array of org.jpl7.Term references.
 * @return consecutive term_t references (first of which is a structure)
 */
protected static term_t putTerms(Map<String, term_t> varnames_to_vars, Term[] args) {
    // First create a sequence of term_ts.
    // The 0th term_t will be a org.jpl7.fli.term_t.
    // Successive Prolog term_t references will reside in the Prolog engine, and can be obtained by term0.value+i.
    term_t term0 = Prolog.new_term_refs(args.length);
    // For each new term ref, construct a Prolog term by putting an appropriate Prolog type into the ref.
    long ith_term_t = term0.value;
    for (int i = 0; i < args.length; ++i, ++ith_term_t) {
        term_t term = new term_t();
        term.value = ith_term_t;
        // each subclass defines its own put()
        args[i].put(varnames_to_vars, term);
    }
    return term0;
}
Also used : org.jpl7.fli.term_t(org.jpl7.fli.term_t)

Example 3 with org.jpl7.fli.term_t

use of org.jpl7.fli.term_t 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 4 with org.jpl7.fli.term_t

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

the class TestJUnit method testBigInteger1.

public void testBigInteger1() {
    BigInteger a = new BigInteger(Long.toString(51L));
    // 51**51, too big for a long
    BigInteger b = a.pow(51);
    Term x = Query.oneSolution("X is 51**51").get("X");
    assertTrue("X is an org.jpl7.Integer", x.isInteger());
    // System.out.println("X.bigValue() = " + x.bigValue().toString());
    // System.out.println("b.bigValue() = " + b.toString());
    assertTrue("X is a big integer", x.isBigInteger());
    assertTrue("X's big value is 51**51", x.bigValue().equals(b));
}
Also used : BigInteger(java.math.BigInteger) Term(org.jpl7.Term)

Example 5 with org.jpl7.fli.term_t

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

the class Versions method main.

public static void main(String[] argv) {
    System.out.println("command line args: (" + argv.length + ")");
    for (int i = 0; i < argv.length; i++) {
        System.out.println("  argv[" + i + "]: " + argv[i]);
    }
    System.out.println();
    System.out.println("old (built in) default init args:");
    String[] defaultInitArgsOld = org.jpl7.JPL.getDefaultInitArgs();
    for (int i = 0; i < defaultInitArgsOld.length; i++) {
        System.out.println("  arg[" + i + "]: " + defaultInitArgsOld[i]);
    }
    System.out.println();
    String[] defaultInitArgsNew1;
    if (argv.length == 1 && argv[0].equals("traditional")) {
        defaultInitArgsNew1 = new String[] { "swipl", "-g", "true", "--nosignals", "--traditional" };
    } else {
        defaultInitArgsNew1 = new String[] { "swipl", "-g", "true", "--nosignals" };
    }
    org.jpl7.JPL.setDefaultInitArgs(defaultInitArgsNew1);
    System.out.println("new (stashed) default init args:");
    String[] defaultInitArgsNew2 = org.jpl7.JPL.getDefaultInitArgs();
    for (int i = 0; i < defaultInitArgsNew2.length; i++) {
        System.out.println("  arg[" + i + "]: " + defaultInitArgsNew2[i]);
    }
    System.out.println();
    if (!(new Query("consult", new Atom("jpl/test/Versions.pl"))).hasSolution()) {
        System.out.println("Warning: failed to consult Versions.pl");
        System.out.println();
    }
    // String swiplHome = ((Term) (new
    // Query("current_prolog_flag(home,Home)")).oneSolution().get("Home")).name();
    // System.out.println(" SWI-Prolog home dir: " + swiplHome );
    System.out.println("home1 = " + (new Atom("c:/swipl-7.1.26")).toString());
    Query q1 = new Query("current_prolog_flag", new Term[] { new Atom("home"), new Variable("Home") });
    Map<String, Term> h1 = q1.oneSolution();
    Term home = (Term) h1.get("Home");
    // System.out.println("Home = " + home.debugString());
    System.out.println("Home = " + home.toString());
    try {
        URL jarPathJpl = Class.forName("org.jpl7.JPL").getProtectionDomain().getCodeSource().getLocation();
        System.out.println("package jpl loaded from: " + jarPathJpl);
    } catch (ClassNotFoundException e) {
        System.out.println("org.jpl7.JPL not found");
    }
    String prologVersion = ((Term) (new Query("jpl_pl_lib_version(V)")).oneSolution().get("V")).name();
    System.out.println(" prolog library version: " + prologVersion);
    String javaVersion = org.jpl7.JPL.version_string();
    System.out.println("   java library version: " + javaVersion);
    String cVersion = org.jpl7.fli.Prolog.get_c_lib_version();
    System.out.println("      c library version: " + cVersion);
    System.out.println("      SWI Prolog syntax: " + org.jpl7.fli.Prolog.get_syntax());
    // if ( prologVersion.equals(javaVersion) &&
    // javaVersion.equals(cVersion) ) {
    // System.out.println("BINGO! you appear to have the same version of
    // each library installed");
    // } else {
    // System.out.println("WHOOPS! you appear not to have the same version
    // of each library installed");
    // }
    System.out.println();
}
Also used : Variable(org.jpl7.Variable) Query(org.jpl7.Query) Term(org.jpl7.Term) Atom(org.jpl7.Atom) URL(java.net.URL)

Aggregations

Term (org.jpl7.Term)10 Query (org.jpl7.Query)8 Variable (org.jpl7.Variable)7 Compound (org.jpl7.Compound)5 BigInteger (java.math.BigInteger)4 Map (java.util.Map)4 Integer (org.jpl7.Integer)4 Atom (org.jpl7.Atom)2 org.jpl7.fli.term_t (org.jpl7.fli.term_t)2 URL (java.net.URL)1 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