Search in sources :

Example 6 with org.jpl7

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

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

the class Family method run.

public void run() {
    Map<String, Term> solution;
    Variable X = new Variable("X");
    // --------------------------------------------------
    Query q2 = new Query("child_of", new Term[] { new Atom("joe"), new Atom("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", new Term[] { new Atom("steve"), new Atom("ralf") });
    System.err.println("descendent_of(steve,ralf) is " + (q3.hasSolution() ? "provable" : "not provable"));
    new Query("sleep", new Term[] { new org.jpl7.Integer(delay) }).hasSolution();
    // --------------------------------------------------
    Query q4 = new Query("descendent_of", new Term[] { X, new Atom("ralf") });
    solution = q4.oneSolution();
    System.err.println("first solution of descendent_of(X, ralf)");
    System.err.println("X = " + solution.get(X.name));
    new Query("sleep", new Term[] { new org.jpl7.Integer(delay) }).hasSolution();
    // --------------------------------------------------
    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.name));
    }
    new Query("sleep", new Term[] { new org.jpl7.Integer(delay) }).hasSolution();
    // --------------------------------------------------
    System.err.println("each solution of descendent_of(X, ralf)");
    while (q4.hasMoreSolutions()) {
        solution = q4.nextSolution();
        System.err.println("X = " + solution.get(X.name));
    }
    new Query("sleep", new Term[] { new org.jpl7.Integer(delay) }).hasSolution();
    // --------------------------------------------------
    Variable Y = new Variable("Y");
    Query q5 = new Query("descendent_of", new Term[] { 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.name) + ", Y = " + solution.get(Y.name));
        new Query("sleep", new Term[] { new org.jpl7.Integer(delay) }).hasSolution();
    }
}
Also used : Variable(org.jpl7.Variable) Query(org.jpl7.Query) Term(org.jpl7.Term) Map(java.util.Map) Atom(org.jpl7.Atom)

Example 8 with org.jpl7

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

the class Test_Integer method testBigInteger2.

@Test
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());
    assertEquals("X's value is as expected", x.bigValue(), b);
}
Also used : Integer(org.jpl7.Integer) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Example 9 with org.jpl7

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

the class Test_Report method reportConfiguration.

@Test
public void reportConfiguration() {
    System.out.println("\t JPL testing under the following configuration:");
    // Query.hasSolution("use_module(library(jpl))"); // only because we call e.g. jpl_pl_syntax/1 below
    useJPLmodule();
    Term swi = Query.oneSolution("current_prolog_flag(version_data,Swi)").get("Swi");
    System.out.println("\t swipl.version = " + swi.arg(1) + "." + swi.arg(2) + "." + swi.arg(3));
    // This is the problematic one as it wil trigger loading library(jpl)
    System.out.println("\t swipl.syntax = " + Query.oneSolution("jpl_pl_syntax(Syntax)").get("Syntax"));
    System.out.println("\t swipl.home = " + Query.oneSolution("current_prolog_flag(home,Home)").get("Home").name());
    System.out.println("\t jpl.jar = " + JPL.version_string());
    System.out.println("\t jpl.dll = " + org.jpl7.fli.Prolog.get_c_lib_version());
    System.out.println("\t jpl.pl = " + Query.oneSolution("jpl_pl_lib_version(V)").get("V").name());
    System.out.println("\t java.version = " + System.getProperty("java.version"));
    System.out.println("\t java_lib_version = " + JPL.version_string());
    System.out.println("\t c_lib_version = " + org.jpl7.fli.Prolog.get_c_lib_version());
    System.out.println("\t pl_lib_version = " + Query.oneSolution("jpl:jpl_pl_lib_version(V)").get("V").name() + " " + Query.oneSolution("module_property(jpl, file(F))").get("F").name());
    System.out.println("\t os.name = " + System.getProperty("os.name"));
    System.out.println("\t os.arch = " + System.getProperty("os.arch"));
    System.out.println("\t os.version = " + System.getProperty("os.version"));
}
Also used : Term(org.jpl7.Term) Test(org.junit.Test)

Example 10 with org.jpl7

use of org.jpl7 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)

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