Search in sources :

Example 41 with Term

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

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

the class SemWeb method main.

public static void main(String[] argv) {
    String t1 = "use_module(library('semweb/rdf_db'))";
    System.out.println(t1 + " " + (Query.hasSolution(t1) ? "succeeded" : "failed"));
    // 
    String t2 = "rdf_load('test.rdf')";
    System.out.println(Query.hasSolution(t2) ? "loaded" : "load failed");
    // 
    String t3 = "rdf(S,P,O)";
    Query q3 = new Query(t3);
    while (q3.hasMoreSolutions()) {
        Map<String, Term> s3 = q3.nextSolution();
        System.out.println("{" + s3.get("S") + ", " + s3.get("P") + ", " + s3.get("O") + "}");
    }
}
Also used : Query(org.jpl7.Query) Term(org.jpl7.Term)

Example 43 with Term

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

the class Test method test_2.

static void test_2() {
    System.out.print("test 2...");
    Query q2 = new Query("p", new Term[] { f_a });
    if (!q2.hasSolution()) {
        System.out.println("p(f(a)) failed");
    // System.exit(1);
    }
    System.out.println("passed");
}
Also used : Query(org.jpl7.Query)

Example 44 with Term

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

the class Test method test_11.

static void test_11() {
    System.out.print("test 11...");
    Term tuple = new Compound("t", new Term[] { new Atom("a"), new Atom("b"), new Atom("c"), new Atom("d"), new Atom("e") });
    try {
        Variable X = new Variable("X");
        Query q11 = new Query("tuple", new Term[] { X });
        Term result = q11.oneSolution().get("X");
        if (result == null || !result.equals(tuple)) {
            System.out.println("failed:");
            System.out.println("\tresult: " + result);
            System.out.println("\ttuple:  " + tuple);
        // System.exit(1);
        }
        if (result.arg(1) == null || !result.arg(1).equals(new Atom("a"))) {
            System.out.println("failed:");
            System.out.println("\tresult.arg(1): " + result.arg(1));
        // System.exit(1);
        }
        if (result.arg(2) == null || !result.arg(2).equals(new Atom("b"))) {
            System.out.println("failed:");
            System.out.println("\tresult.arg(2): " + result.arg(2));
        // System.exit(1);
        }
        if (result.arg(5) == null || !result.arg(5).equals(new Atom("e"))) {
            System.out.println("failed:");
            System.out.println("\tresult.arg(5): " + result.arg(5));
        // System.exit(1);
        }
    // arg0(6) throws an exception, as I'd expect it to...
    // if ( ((Compound)result).arg( 7 ) != null ){
    // System.out.println( "failed:" );
    // System.out.println( "\t((Compound)result).arg( 7 ): " + ((Compound)result).arg( 7 ) );
    // System.out.println( "\tshould be null" );
    // System.exit( 1 );
    // }
    } catch (PrologException e) {
        System.out.println("failed");
        e.printStackTrace();
    // System.exit(1);
    }
    System.out.println("passed");
}
Also used : Variable(org.jpl7.Variable) Query(org.jpl7.Query) PrologException(org.jpl7.PrologException) Compound(org.jpl7.Compound) Term(org.jpl7.Term) Atom(org.jpl7.Atom)

Example 45 with Term

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

the class Test method test_7.

static void test_7() {
    System.out.print("test 7...");
    String t7 = "r(f(X,X), Y)";
    Variable vX = new Variable("X");
    Variable vY = new Variable("Y");
    Query q7 = new Query("r", new Term[] { new Compound("f", new Term[] { vX, vX }), vY });
    Map<String, Term>[] solutions = q7.allSolutions();
    if (solutions.length != 2) {
        System.out.println(t7 + " failed:");
        System.out.println("\tExpected: 2 solutions");
        System.out.println("\tGot:      " + solutions.length);
    // System.exit(1);
    }
    Term X = solutions[0].get("X");
    Term Y = solutions[0].get("Y");
    if (X != Y) {
        System.out.println(t7 + " failed:");
        System.out.println(Util.toString(solutions[0]));
        System.out.println("\tThe variables to which X and Y are bound in the first solution should be identical.");
    // System.exit(1);
    }
    X = solutions[1].get("X");
    Y = solutions[1].get("Y");
    if (X == Y) {
        System.out.println(t7 + " failed:");
        System.out.println(Util.toString(solutions[1]));
        System.out.println("\tThe variables to which X and Y are bound in the second solution should be distinct.");
    // System.exit(1);
    }
    if (X.equals(Y)) {
        System.out.println(t7 + " failed:");
        System.out.println(Util.toString(solutions[1]));
        System.out.println("\tThe variables to which X and Y are bound in the second solution should not be \"equal\".");
    // System.exit(1);
    }
    System.out.println("passed");
}
Also used : Variable(org.jpl7.Variable) Query(org.jpl7.Query) Compound(org.jpl7.Compound) Term(org.jpl7.Term) Map(java.util.Map)

Aggregations

Term (org.jpl7.Term)62 Query (org.jpl7.Query)42 Variable (org.jpl7.Variable)24 Atom (org.jpl7.Atom)21 Compound (org.jpl7.Compound)21 Map (java.util.Map)12 BigInteger (java.math.BigInteger)6 Integer (org.jpl7.Integer)6 org.jpl7.fli.term_t (org.jpl7.fli.term_t)4 Term (org.apache.cassandra.cql3.Term)3 MarshalException (org.apache.cassandra.serializers.MarshalException)3 PrologException (org.jpl7.PrologException)3 JPLException (org.jpl7.JPLException)2 IntHolder (org.jpl7.fli.IntHolder)2 URL (java.net.URL)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 TestSuite (junit.framework.TestSuite)1 DoubleHolder (org.jpl7.fli.DoubleHolder)1 Int64Holder (org.jpl7.fli.Int64Holder)1 ObjectHolder (org.jpl7.fli.ObjectHolder)1