Search in sources :

Example 16 with org.jpl7

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

the class Family method main.

public static void main(String[] argv) {
    // JPL.setTraditional();
    // 
    // only because we call e.g. jpl_pl_syntax/1 below
    Query.hasSolution("use_module(library(jpl))");
    Term swi = Query.oneSolution("current_prolog_flag(version_data,Swi)").get("Swi");
    System.out.println("swipl.version = " + swi.arg(1) + "." + swi.arg(2) + "." + swi.arg(3));
    System.out.println("swipl.syntax = " + Query.oneSolution("jpl_pl_syntax(Syntax)").get("Syntax"));
    System.out.println("swipl.home = " + Query.oneSolution("current_prolog_flag(home,Home)").get("Home").name());
    System.out.println("jpl.jar = " + JPL.version_string());
    System.out.println("jpl.dll = " + org.jpl7.fli.Prolog.get_c_lib_version());
    System.out.println("jpl.pl = " + Query.oneSolution("jpl_pl_lib_version(V)").get("V").name());
    // 
    String t1 = "consult('family.pl')";
    System.out.println(t1 + " " + (Query.hasSolution(t1) ? "succeeded" : "failed"));
    // 
    String t2 = "child_of(joe, ralf)";
    System.out.println(t2 + " is " + (Query.hasSolution(t2) ? "provable" : "not provable"));
    // 
    String t3 = "descendent_of(steve, ralf)";
    System.out.println(t3 + " is " + (Query.hasSolution(t3) ? "provable" : "not provable"));
    // 
    String t4 = "descendent_of(X, ralf)";
    System.out.println("first solution of " + t4 + ": X = " + Query.oneSolution(t4).get("X"));
    Map<String, Term>[] ss4 = Query.allSolutions(t4);
    System.out.println("all solutions of " + t4);
    for (int i = 0; i < ss4.length; i++) {
        System.out.println("X = " + ss4[i].get("X"));
    }
    System.out.println("each solution of " + t4);
    Query q4 = new Query(t4);
    while (q4.hasMoreSolutions()) {
        Map<String, Term> s4 = q4.nextSolution();
        System.out.println("X = " + s4.get("X"));
    }
    // 
    String t5 = "descendent_of(X,Y)";
    Query q5 = new Query(t5);
    System.out.println("each solution of " + t5);
    while (q5.hasMoreSolutions()) {
        Map<String, Term> s5 = q5.nextSolution();
        System.out.println("X = " + s5.get("X") + ", Y = " + s5.get("Y"));
    }
}
Also used : Query(org.jpl7.Query) Term(org.jpl7.Term) Map(java.util.Map)

Example 17 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 18 with org.jpl7

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

the class Family method main.

public static void main(String[] argv) {
    // JPL.setTraditional();
    // 
    // only because we call e.g. jpl_pl_syntax/1 below
    Query.hasSolution("use_module(library(jpl))");
    Term swi = Query.oneSolution("current_prolog_flag(version_data,Swi)").get("Swi");
    System.out.println("swipl.version = " + swi.arg(1) + "" + swi.arg(2) + "." + swi.arg(3));
    System.out.println("swipl.syntax = " + Query.oneSolution("jpl_pl_syntax(Syntax)").get("Syntax"));
    System.out.println("swipl.home = " + Query.oneSolution("current_prolog_flag(home,Home)").get("Home").name());
    System.out.println("jpl.jar = " + JPL.version_string());
    System.out.println("jpl.dll = " + org.jpl7.fli.Prolog.get_c_lib_version());
    System.out.println("jpl.pl = " + Query.oneSolution("jpl_pl_lib_version(V)").get("V").name());
    // 
    String t1 = "consult('family.pl')";
    System.out.println(t1 + " " + (Query.hasSolution(t1) ? "succeeded" : "failed"));
    // 
    String t2 = "child_of(joe, ralf)";
    System.out.println(t2 + " is " + (Query.hasSolution(t2) ? "provable" : "not provable"));
    // 
    String t3 = "descendent_of(steve, ralf)";
    System.out.println(t3 + " is " + (Query.hasSolution(t3) ? "provable" : "not provable"));
    // 
    String t4 = "descendent_of(X, ralf)";
    System.out.println("first solution of " + t4 + ": X = " + Query.oneSolution(t4).get("X"));
    Map<String, Term>[] ss4 = Query.allSolutions(t4);
    System.out.println("all solutions of " + t4);
    for (int i = 0; i < ss4.length; i++) {
        System.out.println("X = " + ss4[i].get("X"));
    }
    System.out.println("each solution of " + t4);
    Query q4 = new Query(t4);
    while (q4.hasMoreSolutions()) {
        Map<String, Term> s4 = q4.nextSolution();
        System.out.println("X = " + s4.get("X"));
    }
    // 
    String t5 = "descendent_of(X,Y)";
    Query q5 = new Query(t5);
    System.out.println("each solution of " + t5);
    while (q5.hasMoreSolutions()) {
        Map<String, Term> s5 = q5.nextSolution();
        System.out.println("X = " + s5.get("X") + ", Y = " + s5.get("Y"));
    }
}
Also used : Query(org.jpl7.Query) Term(org.jpl7.Term) Map(java.util.Map)

Example 19 with org.jpl7

use of org.jpl7 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", "--no-signals", "-f", "none", "--no-packs", "--traditional" };
    } else {
        defaultInitArgsNew1 = new String[] { "swipl", "-g", "true", "--no-signals", "-f", "none", "--no-packs" };
    }
    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 = 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 = 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)

Example 20 with org.jpl7

use of org.jpl7 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;
    Term[] args;
    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 = 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.RATIONAL:
            hString = new StringHolder();
            if (Prolog.get_rational(term, hString)) {
                // System.out.println("bigint = " + hString.value);
                return new org.jpl7.Rational(hString.value);
            } else {
                // arbitrary
                return new org.jpl7.Integer(-3);
            }
        case // 5
        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);
            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");
            }
        case // 44
        Prolog.DICT:
            hString = new StringHolder();
            hInt = new IntHolder();
            Prolog.get_name_arity(term, hString, hInt);
            // assume it succeeds hString = "dict" / term = dict(tag, v1, key1, v2, key2, ...)
            // first get the tag name of the dictionary
            term_t term_tag = Prolog.new_term_ref();
            Prolog.get_arg(1, term, term_tag);
            Term tagAtomOrVar = Term.getTerm(vars_to_Vars, term_tag);
            Map<Atom, Term> map = new HashMap<Atom, Term>();
            for (int i = 2; i <= hInt.value - 1; i = i + 2) {
                // get the value
                term_t termValue = Prolog.new_term_ref();
                Prolog.get_arg(i, term, termValue);
                Term value = Term.getTerm(vars_to_Vars, termValue);
                // Get the key
                term_t termKey = Prolog.new_term_ref();
                Prolog.get_arg(i + 1, term, termKey);
                Atom key = (Atom) Term.getTerm(vars_to_Vars, termKey);
                // add key-value to mapping
                map.put(key, value);
            }
            return new Dict(tagAtomOrVar, map);
        default:
            // should never happen
            throw new JPLException("unknown term type=" + Prolog.term_type(term));
    }
}
Also used : DoubleHolder(org.jpl7.fli.DoubleHolder) HashMap(java.util.HashMap) 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)

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