upgrade JPL

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1936 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
vsc
2007-09-27 15:25:34 +00:00
parent 5f9555baa4
commit 31ff28d3ee
70 changed files with 12020 additions and 9030 deletions

View File

@@ -0,0 +1,78 @@
package jpl.test;
/**
* CelsiusConverter.java is a 1.4 application that
* demonstrates the use of JButton, JTextField and
* JLabel. It requires no other files.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CelsiusConverter implements ActionListener {
JFrame converterFrame;
JPanel converterPanel;
JTextField tempCelsius;
JLabel celsiusLabel, fahrenheitLabel;
JButton convertTemp;
public CelsiusConverter() { // initially locate the window at top-left of desktop
this(0, 0);
}
public CelsiusConverter(int left, int top) { // initially locate the window at top-left of desktop
// create and set up the window
converterFrame = new JFrame("Convert Celsius to Fahrenheit");
converterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
converterFrame.setSize(new Dimension(120, 40));
converterFrame.setLocation(left, top);
// create and set up the panel
converterPanel = new JPanel(new GridLayout(2, 2));
// create widgets
tempCelsius = new JTextField(2);
celsiusLabel = new JLabel("Celsius", SwingConstants.LEFT);
celsiusLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
//
convertTemp = new JButton("Convert");
fahrenheitLabel = new JLabel("Fahrenheit", SwingConstants.LEFT);
// listen to events from the Convert button
convertTemp.addActionListener(this);
// add the widgets to the container
converterPanel.add(tempCelsius);
converterPanel.add(celsiusLabel);
converterPanel.add(convertTemp);
converterPanel.add(fahrenheitLabel);
fahrenheitLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
converterFrame.getRootPane().setDefaultButton(convertTemp); // make "convert" the window's default button
// add the panel to the window
converterFrame.getContentPane().add(converterPanel, BorderLayout.CENTER);
// display the window
converterFrame.pack();
converterFrame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
// parse degrees Celsius as a double
double tC = (Double.parseDouble(tempCelsius.getText()));
//
// convert to Fahrenheit (in Java)
// int tempFahr = (int) (tC * 1.8 + 32);
//
// convert to Fahrenheit (in Prolog, via JPL)
int tempFahr = ((jpl.Float) jpl.Query.oneSolution("TF is ? * 1.8 + 32", new jpl.Term[] {new jpl.Float(tC)}).get("TF")).intValue();
//
// display the result
fahrenheitLabel.setText(tempFahr + " Fahrenheit");
}
public static void spawnGUI(final int left, final int top) {
// schedule a job for the event-dispatching thread: create and show an instance of this application at (left,top)
javax.swing.SwingUtilities.invokeLater(new Runnable() {
int x = left;
int y = top;
public void run() {
new CelsiusConverter(x, y); // can we be sure this won't be garbage collected?
}
});
}
public static void main(String[] args) {
// just for fun, we ask Prolog to start five instances of this class (at stepped offsets from top-left of display)
jpl.Query.allSolutions("between(1, 5, N), X is 10*N, Y is 20*N, jpl_call('jpl.test.CelsiusConverter', spawnGUI, [X,Y], _)");
}
}

View File

@@ -0,0 +1,96 @@
package jpl.test;
import jpl.Atom;
import jpl.Query;
import jpl.Term;
import jpl.Variable;
public class Family extends Thread {
int id; // client thread id
private static final int delay = 0;
Family(int i) {
this.id = i;
}
public static void main(String argv[]) {
Query q1 = new Query("consult", new Term[] { new Atom("jpl/test/family.pl")});
System.err.println("consult " + (q1.hasSolution() ? "succeeded" : "failed"));
for (int i = 0; i < 20; i++) {
System.out.println("spawning client[" + i + "]");
new Family(i).start();
}
}
public void run() {
java.util.Hashtable 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 jpl.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 jpl.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 jpl.Integer(delay)}).hasSolution();
//--------------------------------------------------
java.util.Hashtable[] 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 jpl.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 jpl.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 jpl.Integer(delay)}).hasSolution();
}
}
}

View File

@@ -0,0 +1,18 @@
package jpl.test;
import jpl.Query;
import jpl.Term;
public class FetchBigTree {
public static void main(String[] args) {
// Prolog.set_default_init_args(new String[] { "libpl.dll", "-f", "D:/pcm/bin/pcm.ini", "-g", "pcm_2000" });
(new Query("consult('jpl/test/test.pl')")).oneSolution();
Term t = (Term)((new Query("p(18,T)")).oneSolution().get("T"));
int i = 1;
while ( t.hasFunctor("a", 2)){
t = t.arg(2);
i = i+1;
}
System.err.println("got a tree of " + i+" generations");
}
}

View File

@@ -0,0 +1,17 @@
package jpl.test;
import jpl.Query;
import jpl.Term;
public class FetchLongList {
public static void main(String[] args) {
// Prolog.set_default_init_args(new String[] { "libpl.dll", "-f", "D:/pcm/bin/pcm.ini", "-g", "pcm_2000" });
Term t = (Term)((new Query("findall(foo(N,bar),between(1,2308,N),L)")).oneSolution().get("L"));
int i = 0;
while ( t.hasFunctor(".", 2)){
t = t.arg(2);
i = i+1;
}
System.err.println("got a list of " + i+" members");
}
}

View File

@@ -0,0 +1,23 @@
package jpl.test;
import jpl.Query;
public class Ga {
public static void main(String argv[]) {
// Prolog.set_default_init_args(new String[] { "libpl.dll", "-f", "D:/pcm/bin/pcm.ini", "-g", "pcm_2000" });
// (new Query("loadall(jpl_test:jr)")).hasSolution();
// System.err.println("jr " + ((new Query("jr")).hasSolution() ? "succeeded" : "failed"));
// System.err.println( "something " + (new Query("statistics(atoms,X)")).oneSolution().get("X"));
// Query.hasSolution("statistics");
// (new Query("x")).hasSolution();
// (new Query("statistics,x")).hasSolution();
// (new Query(new Atom("statistics"))).hasSolution();
// Query.hasSolution("write(hello),nl");
// Query.hasSolution("write(hello),nl");
// (new Query("nl")).hasSolution();
(new Query("nl,nl")).hasSolution();
// (new Query("user:nl")).hasSolution();
}
}

View File

@@ -0,0 +1,10 @@
package jpl.test;
import jpl.Query;
public class Ga2 {
public static void main(String argv[]) {
// Prolog.set_default_init_args(new String[] { "libpl.dll", "-f", "D:/pcm/bin/pcm.ini", "-g", "pcm_2000" });
(new Query("current_prolog_flag(K,V),write(K-V),nl,fail")).oneSolution();
}
}

View File

@@ -0,0 +1,19 @@
package jpl.test;
public class Garbo {
public static int created = 0;
public static int destroyed = 0;
//
public final int i;
public Garbo( ) {
this.i = created++;
}
protected void finalize() throws Throwable {
try {
destroyed++;
// System.out.println("gced["+i+"]");
} finally {
super.finalize();
}
}
}

View File

@@ -0,0 +1,93 @@
/*
* JPLTest.java
* JUnit based test
*
* Created on 13 February 2006, 11:31
*/
package jpl.test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import junit.framework.*;
import jpl.*;
/**
*
* @author rick
*/
public class JPLTest extends TestCase {
// private static final Logger logger = Logger.getLogger(JPLTest.class.getName());
private CountDownLatch latch;
public JPLTest(String testName) {
super(testName);
}
protected void setUp() throws Exception {
/*
* Prolog file can be an empty file. The JVM seems to crash with a
* SIGSEGV if you don't consult a file prior to interacting with JPL.
final String prologFile = "jpl/test/test.pl"; // was "/home/rick/temp/test.pl";
System.out.println("prolog file is: " + prologFile);
String qString = "consult('" + prologFile + "')";
System.out.println("about to: " + qString);
Query query = new Query(qString);
System.out.println("Generated Query: " + query);
if (!query.hasSolution()) {
System.out.println(qString + " failed");
fail("Failed to consult prolog file.");
}
(new Query("true")).hasSolution();
*/
}
public void testThreadedAdds() {
latch = new CountDownLatch(4);
final AddWithThreads[] addTasks = { new AddWithThreads("a", latch), new AddWithThreads("b", latch), new AddWithThreads("c", latch), new AddWithThreads("d", latch) };
// System.out.println("Starting threads...");
for (int i = 0; i < addTasks.length; i++) {
addTasks[i].start();
}
try {
// System.out.println("Latch is waiting");
assertTrue("Timed out waiting for action to execute", latch.await(20, TimeUnit.SECONDS));
// System.out.println("Latch has been flipped");
} catch (final InterruptedException e) {
fail("Waiting thread was interrupted: " + e);
}
for (int i = 0; i < AddWithThreads.REPS; i++) {
for (int j = 0; j < addTasks.length; j++) {
Query query = new Query(addTasks[j].getNamespace() + "(test('" + i + "'))");
// System.out.println("query: " + query);
boolean ret = query.hasMoreElements();
query.close();
}
}
}
}
class AddWithThreads extends Thread {
private final CountDownLatch latch;
private final String namespace;
private static final Logger logger = Logger.getLogger(JPLTest.class.getName());
public static final int REPS = 2000; // was 200
public AddWithThreads(final String namespace, final CountDownLatch latch) {
this.latch = latch;
this.namespace = namespace;
setName("namespace" + namespace); //set thread name for debugging
}
public String getNamespace() {
return namespace;
}
public void run() {
for (int i = 0; i < REPS; i++) {
// System.out.println("Asserting test('" + i + "')");
Query queryA = new Query("assert(" + namespace + "(test('" + i + "')))");
Thread.yield();
// System.out.println("adding query: " + queryA);
boolean retA = queryA.hasMoreElements();
queryA.close();
}
latch.countDown();
}
}

View File

@@ -0,0 +1,42 @@
package jpl.test;
import jpl.Query;
import jpl.fli.Prolog;
public class Masstest extends Thread {
public static void main(String[] args) {
// String[] dia = Prolog.get_default_init_args();
// String s = "default init args: ";
// for (int i = 0; i < dia.length; i++) {
// s += " " + dia[i];
// }
// System.out.println(s);
//
// Prolog.set_default_init_args(new String[] { "libpl.dll", "-f", "none", "-g", "true", "-q" });
// empirically, needs this at least:
// Prolog.set_default_init_args(new String[] { "libpl.dll" });
// Prolog.set_default_init_args(new String[] { "pl" });
//
// (new Query("assert(diagnose_declaration(_,_,_,[not,a,real,error]))")).hasSolution();
//
int STUDENTSNUMBER = 5;
Masstest[] threads = new Masstest[STUDENTSNUMBER];
for (int i = 0; i < STUDENTSNUMBER; i++) {
threads[i] = new Masstest();
threads[i].start();
}
}
public void predQuery() {
String st = "diagnose_declaration(1,[(sp, 'prefix', [('arg1', '+', 'list', 'Liste1'), ('arg2', '+', 'list', 'Liste2')])], DecMap, ErrorList)";
Query stQuery = new Query(st);
String errString = stQuery.oneSolution().get("ErrorList").toString();
System.out.println("errString=" + errString);
}
public void run() {
try {
predQuery();
} catch (Exception e) {
System.err.println("ERROR: " + e);
}
}
}

View File

@@ -0,0 +1,4 @@
package jpl.test;
public class MaxObjects {
}

View File

@@ -0,0 +1,13 @@
/*
* Created on 22-Nov-2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package jpl.test;
public class ShadowA {
public int shadow = -1;
public static int fieldStaticInt;
}

View File

@@ -0,0 +1,16 @@
/*
* Created on 22-Nov-2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package jpl.test;
public class ShadowB extends ShadowA {
public String shadow;
public ShadowB(String s) {
shadow = s;
}
public static int fieldStaticInt;
}

View File

@@ -0,0 +1,10 @@
package jpl.test;
import jpl.Query;
public class SyntaxError {
public static void main(String argv[]) {
Query q = new Query("syntax)error");
System.err.println(q.hasSolution() ? "yes" : "no");
}
}

View File

@@ -0,0 +1,283 @@
package jpl.test;
import jpl.Query;
import jpl.Term;
// This class contains members which support those tests which are performed from Prolog.
// See also TestJUnit
public class Test {
public Test() {
}
public Test(Term t) {
this.termFromConstructor = t;
}
public Term termFromConstructor;
//
public static boolean fieldStaticBoolean;
public static final boolean fieldStaticBoolean1 = false;
public static final boolean fieldStaticBoolean2 = true;
//
public static char fieldStaticChar;
public static final char fieldStaticChar1 = '\u0000';
public static final char fieldStaticChar2 = '\uFFFF';
//
public static byte fieldStaticByte;
public static final byte fieldStaticByte1 = -(1 << 7);
public static final byte fieldStaticByte2 = -1;
public static final byte fieldStaticByte3 = 0;
public static final byte fieldStaticByte4 = 1;
public static final byte fieldStaticByte5 = (1 << 7) - 1;
//
public static short fieldStaticShort;
public static final short fieldStaticShort1 = -(1 << 15);
public static final short fieldStaticShort2 = -(1 << 7);
public static final short fieldStaticShort3 = -1;
public static final short fieldStaticShort4 = 0;
public static final short fieldStaticShort5 = 1;
public static final short fieldStaticShort6 = (1 << 7) - 1;
public static final short fieldStaticShort7 = (1 << 15) - 1;
//
public static int fieldStaticInt;
public static final int fieldStaticInt1 = -(1 << 31);
public static final int fieldStaticInt2 = -(1 << 15);
public static final int fieldStaticInt3 = -(1 << 7);
public static final int fieldStaticInt4 = -1;
public static final int fieldStaticInt5 = 0;
public static final int fieldStaticInt6 = 1;
public static final int fieldStaticInt7 = (1 << 7) - 1;
public static final int fieldStaticInt8 = (1 << 15) - 1;
public static final int fieldStaticInt9 = (1 << 31) - 1;
//
public static long fieldStaticLong;
public static final long fieldStaticLong1 = -(1 << 63);
public static final long fieldStaticLong2 = -(1 << 31);
public static final long fieldStaticLong3 = -(1 << 15);
public static final long fieldStaticLong4 = -(1 << 7);
public static final long fieldStaticLong5 = -1;
public static final long fieldStaticLong6 = 0;
public static final long fieldStaticLong7 = 1;
public static final long fieldStaticLong8 = (1 << 7) - 1;
public static final long fieldStaticLong9 = (1 << 15) - 1;
public static final long fieldStaticLong10 = (1 << 31) - 1;
public static final long fieldStaticLong11 = (1 << 63) - 1;
//
public static float fieldStaticFloat;
public static final float fieldStaticFloat1 = 12345.6789F;
public static final float fieldStaticFloat2 = 3.4e+38F; // nearly MAX_VALUE
public static final float fieldStaticFloat3 = 1.4e-45F; // nearly MIN_VALUE
public static final float fieldStaticFloat4 = 0.0F;
public static final float fieldStaticFloat5 = java.lang.Float.MIN_VALUE;
public static final float fieldStaticFloat6 = java.lang.Float.MAX_VALUE;
public static final float fieldStaticFloat7 = java.lang.Float.NEGATIVE_INFINITY;
public static final float fieldStaticFloat8 = java.lang.Float.POSITIVE_INFINITY;
public static final float fieldStaticFloat9 = java.lang.Float.NaN;
//
public static double fieldStaticDouble;
public static final double fieldStaticDouble1 = 12345.6789D;
public static final double fieldStaticDouble2 = 2.3456789e+100D;
public static final double fieldStaticDouble3 = 3.456789e-100D;
public static final double fieldStaticDouble4 = 0.0D;
public static final double fieldStaticDouble5 = Double.MIN_VALUE;
public static final double fieldStaticDouble6 = Double.MAX_VALUE;
public static final double fieldStaticDouble7 = Double.NEGATIVE_INFINITY;
public static final double fieldStaticDouble8 = Double.POSITIVE_INFINITY;
public static final double fieldStaticDouble9 = Double.NaN;
//
public static Object[] fieldStaticObjectArray; // can assign e.g. String[]
public static long[] fieldStaticLongArray; // cannot assign e.g. int[]
//
public static long fac(long n) { // complements jpl:jpl_test_fac(+integer,-integer)
if (n == 1) {
return 1;
} else if (n > 1) {
// return n * ((Integer) new Query(new Compound("jpl_test_fac", new Term[] { new Integer(n - 1), new Variable("F") })).oneSolution().get("F")).intValue();
return n * ((jpl.Integer) Query.oneSolution("jpl_test_fac(?,F)", new Term[] {new jpl.Integer(n-1)}).get("F")).longValue();
} else {
return 0;
}
}
static void packageMethod() { // not callable via JPL
return;
}
public static void publicMethod() {
return;
}
protected static void protectedMethod() { // not callable via JPL
return;
}
private static void privateMethod() { // not callable via JPL
return;
}
public boolean fieldInstanceBoolean;
public final boolean fieldInstanceBoolean1 = false;
public final boolean fieldInstanceBoolean2 = true;
public byte fieldInstanceByte;
public final byte fieldInstanceByte1 = -(1 << 7);
public final byte fieldInstanceByte2 = -1;
public final byte fieldInstanceByte3 = 0;
public final byte fieldInstanceByte4 = 1;
public final byte fieldInstanceByte5 = (1 << 7) - 1;
public char fieldInstanceChar;
public final char fieldInstanceChar1 = '\u0000';
public final char fieldInstanceChar2 = '\uFFFF';
public double fieldInstanceDouble;
public final double fieldInstanceDouble1 = 12345.6789D;
public final double fieldInstanceDouble2 = 2.3456789e+100D;
public final double fieldInstanceDouble3 = 3.456789e-100D;
public final double fieldInstanceDouble4 = 0.0D;
public final double fieldInstanceDouble5 = Double.MIN_VALUE;
public final double fieldInstanceDouble6 = Double.MAX_VALUE;
public final double fieldInstanceDouble7 = Double.NEGATIVE_INFINITY;
public final double fieldInstanceDouble8 = Double.POSITIVE_INFINITY;
public final double fieldInstanceDouble9 = Double.NaN;
public float fieldInstanceFloat;
public final float fieldInstanceFloat1 = 12345.6789F;
public final float fieldInstanceFloat2 = 3.4e+38F;
public final float fieldInstanceFloat3 = 1.4e-45F;
public final float fieldInstanceFloat4 = 0.0F;
public final float fieldInstanceFloat5 = java.lang.Float.MIN_VALUE;
public final float fieldInstanceFloat6 = java.lang.Float.MAX_VALUE;
public final float fieldInstanceFloat7 = java.lang.Float.NEGATIVE_INFINITY;
public final float fieldInstanceFloat8 = java.lang.Float.POSITIVE_INFINITY;
public final float fieldInstanceFloat9 = java.lang.Float.NaN;
public int fieldInstanceInt;
public final int fieldInstanceInt1 = -(1 << 31);
public final int fieldInstanceInt2 = -(1 << 15);
public final int fieldInstanceInt3 = -(1 << 7);
public final int fieldInstanceInt4 = -1;
public final int fieldInstanceInt5 = 0;
public final int fieldInstanceInt6 = 1;
public final int fieldInstanceInt7 = (1 << 7) - 1;
public final int fieldInstanceInt8 = (1 << 15) - 1;
public final int fieldInstanceInt9 = (1 << 31) - 1;
public long fieldInstanceLong;
public final long fieldInstanceLong1 = -(1 << 63);
public final long fieldInstanceLong10 = (1 << 31) - 1;
public final long fieldInstanceLong11 = (1 << 63) - 1;
public final long fieldInstanceLong2 = -(1 << 31);
public final long fieldInstanceLong3 = -(1 << 15);
public final long fieldInstanceLong4 = -(1 << 7);
public final long fieldInstanceLong5 = -1;
public final long fieldInstanceLong6 = 0;
public final long fieldInstanceLong7 = 1;
public final long fieldInstanceLong8 = (1 << 7) - 1;
public final long fieldInstanceLong9 = (1 << 15) - 1;
public short fieldInstanceShort;
public final short fieldInstanceShort1 = -(1 << 15);
public final short fieldInstanceShort2 = -(1 << 7);
public final short fieldInstanceShort3 = -1;
public final short fieldInstanceShort4 = 0;
public final short fieldInstanceShort5 = 1;
public final short fieldInstanceShort6 = (1 << 7) - 1;
public final short fieldInstanceShort7 = (1 << 15) - 1;
//
public Term term; // obsolete
public static Term staticTerm;
public Term instanceTerm;
//
// for testing accessibility of non-public fields:
static boolean fieldPackageStaticBoolean;
protected static boolean fieldProtectedStaticBoolean;
private static boolean fieldPrivateStaticBoolean;
//
// for testing update of final field:
public static final int fieldStaticFinalInt = 7;
//
// for testing passing general terms in from Prolog:
public static Term fieldStaticTerm;
public Term fieldInstanceTerm;
public static boolean methodStaticTerm(Term t) {
return t != null;
}
public boolean methodInstanceTerm(Term t) {
return t != null;
}
public static Term methodStaticEchoTerm(Term t) {
return t;
}
public static boolean methodStaticEchoBoolean(boolean v) {
return v;
}
public static char methodStaticEchoChar(char v) {
return v;
}
public static byte methodStaticEchoByte(byte v) {
return v;
}
public static short methodStaticEchoShort(short v) {
return v;
}
public static int methodStaticEchoInt(int v) {
return v;
}
public static long methodStaticEchoLong(long v) {
return v;
}
public static float methodStaticEchoFloat(float v) {
return v;
}
public static double methodStaticEchoDouble(double v) {
return v;
}
public Term methodInstanceTermEcho(Term t) {
return t;
}
public static boolean methodStaticTermIsJNull(Term t) {
return t.hasFunctor("@", 1) && t.arg(1).hasFunctor("null", 0);
}
public boolean methodInstanceTermIsJNull(Term t) {
return t.hasFunctor("@", 1) && t.arg(1).hasFunctor("null", 0);
}
public static void hello() {
System.out.println("hello");
}
public static boolean[] newArrayBooleanFromValue(boolean v) {
boolean[] a = new boolean[1];
a[0] = v;
return a;
}
public static byte[] newArrayByteFromValue(byte v) {
byte[] a = new byte[1];
a[0] = v;
return a;
}
public static char[] newArrayCharFromValue(char v) {
char[] a = new char[1];
a[0] = v;
return a;
}
public static short[] newArrayShortFromValue(short v) {
short[] a = new short[1];
a[0] = v;
return a;
}
public static int[] newArrayIntFromValue(int v) {
int[] a = new int[1];
a[0] = v;
return a;
}
public static long[] newArrayLongFromValue(long v) {
long[] a = new long[1];
a[0] = v;
return a;
}
public static float[] newArrayFloatFromValue(float v) {
float[] a = new float[1];
a[0] = v;
return a;
}
public static double[] newArrayDoubleFromValue(double v) {
double[] a = new double[1];
a[0] = v;
return a;
}
public static String methodStaticArray(long[] a) {
return "long[]";
}
public static String methodStaticArray(int[] a) {
return "int[]";
}
public static String methodStaticArray(short[] a) {
return "short[]";
}
}

View File

@@ -0,0 +1,532 @@
// Created on 25-Jul-2004
package jpl.test;
import java.util.Map;
import jpl.Atom;
import jpl.Compound;
import jpl.Integer;
import jpl.JPL;
import jpl.JRef;
import jpl.PrologException;
import jpl.Query;
import jpl.Term;
import jpl.Util;
import jpl.Variable;
import jpl.fli.Prolog;
import junit.framework.TestCase;
import junit.framework.TestSuite;
// This class defines all the tests which are run from Java.
// It needs junit.framework.TestCase and junit.framework.TestSuite, which are not supplied with JPL.
public class TestJUnit extends TestCase {
//
public static long fac(long n) { // complements jpl:jpl_test_fac(+integer,-integer)
if (n == 1) {
return 1;
} else if (n > 1) {
return n * ((jpl.Integer) Query.oneSolution("jpl_test_fac(?,F)", new Term[] { new jpl.Integer(n - 1) }).get("F")).longValue();
} else {
return 0;
}
}
public TestJUnit(String name) {
super(name);
}
public static junit.framework.Test suite() {
return new TestSuite(TestJUnit.class);
}
public static void main(String args[]) {
junit.textui.TestRunner.run(suite());
}
protected void setUp() {
// initialization code
// Prolog.set_default_init_args(new String[] { "libpl.dll", "-f", "none", "-g", "set_prolog_flag(debug_on_error,false)", "-q" });
Prolog.set_default_init_args(new String[] { "libpl.dll", "-f", "none", "-g", "true", "-q" });
assertTrue((new Query("consult(test_jpl)")).hasSolution());
}
protected void tearDown() {
// cleanup code
}
//
public void testMasstest() {
assertTrue((new Query("assert(diagnose_declaration(_,_,_,[not,a,real,error]))")).hasSolution());
}
public void testSameLibVersions1() {
String java_lib_version = JPL.version_string();
String c_lib_version = jpl.fli.Prolog.get_c_lib_version();
assertTrue("java_lib_version(" + java_lib_version + ") is same as c_lib_version(" + c_lib_version + ")", java_lib_version.equals(c_lib_version));
}
public void testSameLibVersions2() {
String java_lib_version = JPL.version_string();
String pl_lib_version = ((Term) (new Query(new Compound("jpl_pl_lib_version", new Term[] { new Variable("V") })).oneSolution().get("V"))).name();
assertTrue("java_lib_version(" + java_lib_version + ") is same as pl_lib_version(" + pl_lib_version + ")", java_lib_version.equals(pl_lib_version));
}
public void testAtomName1() {
String name = "fred";
Atom a = new Atom(name);
assertEquals("an Atom's name is that with which it was created", a.name(), name);
}
public void testAtomName2() {
String name = "ha ha";
Atom a = new Atom(name);
assertEquals("an Atom's name is that with which it was created", a.name(), name);
}
public void testAtomName3() {
String name = "3";
Atom a = new Atom(name);
assertEquals("an Atom's name is that with which it was created", a.name(), name);
}
public void testAtomToString1() {
String name = "fred";
String toString = "fred";
Atom a = new Atom(name);
assertEquals("an Atom's .toString() value is quoted iff appropriate", a.toString(), toString);
}
public void testAtomToString2() {
String name = "ha ha";
String toString = "'ha ha'";
Atom a = new Atom(name);
assertEquals("an Atom's .toString() value is quoted iff appropriate", a.toString(), toString);
}
public void testAtomToString3() {
String name = "3";
String toString = "'3'";
Atom a = new Atom(name);
assertEquals("an Atom's .toString() value is quoted iff appropriate", a.toString(), toString);
}
public void testAtomArity() {
Atom a = new Atom("willy");
assertEquals("an Atom has arity zero", a.arity(), 0);
}
public void testAtomEquality1() {
String name = "fred";
Atom a1 = new Atom(name);
Atom a2 = new Atom(name);
assertEquals("two Atoms created with the same name are equal", a1, a2);
}
public void testAtomIdentity() { // how could this fail?!
String name = "fred";
Atom a1 = new Atom(name);
Atom a2 = new Atom(name);
assertNotSame("two Atoms created with the same name are not identical", a1, a2);
}
public void testAtomHasFunctorNameZero() {
String name = "sam";
Atom a = new Atom(name);
assertTrue(a.hasFunctor(name, 0));
}
public void testAtomHasFunctorWrongName() {
assertFalse("an Atom does not have a functor whose name is other than that with which the Atom was created", new Atom("wally").hasFunctor("poo", 0));
}
public void testAtomHasFunctorWrongArity() {
String name = "ted";
assertFalse("an Atom does not have a functor whose arity is other than zero", new Atom(name).hasFunctor(name, 1));
}
public void testVariableBinding1() {
Term lhs = new Compound("p", new Term[] { new Variable("X"), new Variable("Y") });
Term rhs = new Compound("p", new Term[] { new Atom("a"), new Atom("b") });
Term goal = new Compound("=", new Term[] { lhs, rhs });
Map soln = new Query(goal).oneSolution();
assertTrue("two variables with different names can bind to distinct atoms", soln != null && ((Term) soln.get("X")).name().equals("a") && ((Term) soln.get("Y")).name().equals("b"));
}
public void testVariableBinding2() {
Term lhs = new Compound("p", new Term[] { new Variable("X"), new Variable("X") });
Term rhs = new Compound("p", new Term[] { new Atom("a"), new Atom("b") });
Term goal = new Compound("=", new Term[] { lhs, rhs });
assertFalse("two distinct Variables with same name cannot unify with distinct atoms", new Query(goal).hasSolution());
}
public void testVariableBinding3() {
Variable X = new Variable("X");
Term lhs = new Compound("p", new Term[] { X, X });
Term rhs = new Compound("p", new Term[] { new Atom("a"), new Atom("b") });
Term goal = new Compound("=", new Term[] { lhs, rhs });
assertFalse("two occurrences of same named Variable cannot unify with distinct atoms", new Query(goal).hasSolution());
}
public void testVariableBinding4() {
Term lhs = new Compound("p", new Term[] { new Variable("_"), new Variable("_") });
Term rhs = new Compound("p", new Term[] { new Atom("a"), new Atom("b") });
Term goal = new Compound("=", new Term[] { lhs, rhs });
assertTrue("two distinct anonymous Variables can unify with distinct atoms", new Query(goal).hasSolution());
}
public void testVariableBinding5() {
Variable Anon = new Variable("_");
Term lhs = new Compound("p", new Term[] { Anon, Anon });
Term rhs = new Compound("p", new Term[] { new Atom("a"), new Atom("b") });
Term goal = new Compound("=", new Term[] { lhs, rhs });
assertTrue("two occurrences of same anonymous Variable can unify with distinct atoms", new Query(goal).hasSolution());
}
public void testAtomEquality2() {
Atom a = new Atom("a");
assertTrue("two occurrences of same Atom are equal by .equals()", a.equals(a));
}
public void testAtomEquality3() {
assertTrue("two distinct Atoms with same names are equal by .equals()", (new Atom("a")).equals(new Atom("a")));
}
public void testTextToTerm1() {
String text = "fred(B,p(A),[A,B,C])";
Term t = Util.textToTerm(text);
assertTrue("Util.textToTerm() converts \"fred(B,p(A),[A,B,C])\" to a corresponding Term", t.hasFunctor("fred", 3) && t.arg(1).isVariable() && t.arg(1).name().equals("B")
&& t.arg(2).hasFunctor("p", 1) && t.arg(2).arg(1).isVariable() && t.arg(2).arg(1).name().equals("A"));
}
public void testArrayToList1() {
Term l2 = Util.termArrayToList(new Term[] { new Atom("a"), new Atom("b"), new Atom("c"), new Atom("d"), new Atom("e") });
Query q9 = new Query(new Compound("append", new Term[] { new Variable("Xs"), new Variable("Ys"), l2 }));
assertTrue("append(Xs,Ys,[a,b,c,d,e]) has 6 solutions", q9.allSolutions().length == 6);
}
public void testArrayToList2() {
String goal = "append(Xs,Ys,[a,b,c,d,e])";
assertTrue(goal + " has 6 solutions", Query.allSolutions(goal).length == 6);
}
public void testLength1() {
Query q5 = new Query(new Compound("length", new Term[] { new Variable("Zs"), new jpl.Integer(2) }));
Term zs = (Term) (q5.oneSolution().get("Zs"));
assertTrue("length(Zs,2) binds Zs to a list of two distinct variables " + zs.toString(), zs.hasFunctor(".", 2) && zs.arg(1).isVariable() && zs.arg(2).hasFunctor(".", 2)
&& zs.arg(2).arg(1).isVariable() && zs.arg(2).arg(2).hasFunctor("[]", 0) && !zs.arg(1).name().equals(zs.arg(2).arg(1).name()));
}
public void testGenerate1() { // we chickened out of verifying each solution :-)
String goal = "append(Xs,Ys,[_,_,_,_,_])";
assertTrue(goal + " has 6 solutions", Query.allSolutions(goal).length == 6);
}
public void testPrologException1() {
try {
new Query("p(]"); // writes junk to stderr and enters debugger unless flag debug_on_error = false
} catch (PrologException e) {
assertTrue("new Query(\"p(]\") throws a PrologException " + e.toString(), true);
return;
}
fail("new Query(\"p(]\") oughta throw a PrologException");
}
public void testAtom1() {
assertTrue("new Atom(\"3 3\")" + (new Atom("3 3")).toString(), true);
}
public void testTextToTerm2() {
String text1 = "fred(?,2,?)";
String text2 = "[first(x,y),A]";
Term plist = Util.textToTerm(text2);
Term[] ps = plist.toTermArray();
Term t = Util.textToTerm(text1).putParams(ps);
assertTrue("fred(?,2,?) .putParams( [first(x,y),A] )", t.hasFunctor("fred", 3) && t.arg(1).hasFunctor("first", 2) && t.arg(1).arg(1).hasFunctor("x", 0) && t.arg(1).arg(2).hasFunctor("y", 0)
&& t.arg(2).hasFunctor(2, 0) && t.arg(3).isVariable() && t.arg(3).name().equals("A"));
}
public void testDontTellMeMode1() {
final Query q = new Query("setof(_M,current_module(_M),_Ms),length(_Ms,N)");
JPL.setDTMMode(true);
assertTrue("in dont-tell-me mode, setof(_M,current_module(_M),_Ms),length(_Ms,N) returns binding for just one variable", q.oneSolution().keySet().size() == 1);
}
public void testDontTellMeMode2() {
final Query q = new Query("setof(_M,current_module(_M),_Ms),length(_Ms,N)");
JPL.setDTMMode(false);
assertTrue("not in dont-tell-me mode, setof(_M,current_module(_M),_Ms),length(_Ms,N) returns binding for three variables", q.oneSolution().keySet().size() == 3);
}
public void testModulePrefix1() {
assertTrue(Query.hasSolution("call(user:true)"));
}
private void testMutualRecursion(int n, long f) { // f is the expected result for fac(n)
try {
assertEquals("mutual recursive Java<->Prolog factorial: fac(" + n + ") = " + f, fac(n), f);
} catch (Exception e) {
fail("fac(" + n + ") threw " + e);
}
}
public void testMutualRecursion1() {
testMutualRecursion(1, 1);
}
public void testMutualRecursion2() {
testMutualRecursion(2, 2);
}
public void testMutualRecursion3() {
testMutualRecursion(3, 6);
}
public void testMutualRecursion10() {
testMutualRecursion(10, 3628800);
}
public void testIsJNull1() {
Term t = (Term) (new Query("X = @(null)")).oneSolution().get("X");
assertTrue("@(null) . isJNull() succeeds", t.isJNull());
}
public void testIsJNull2() {
Term t = (Term) (new Query("X = @(3)")).oneSolution().get("X");
assertFalse("@(3) . isJNull() fails", t.isJNull());
}
public void testIsJNull3() {
Term t = (Term) (new Query("X = _")).oneSolution().get("X");
assertFalse("_ . isJNull() fails", t.isJNull());
}
public void testIsJNull4() {
Term t = (Term) (new Query("X = @(true)")).oneSolution().get("X");
assertFalse("@(true) . isJNull() fails", t.isJNull());
}
public void testIsJNull5() {
Term t = (Term) (new Query("X = @(false)")).oneSolution().get("X");
assertFalse("@(false) . isJNull() fails", t.isJNull());
}
public void testIsJTrue1() {
Term t = (Term) (new Query("X = @(true)")).oneSolution().get("X");
assertTrue("@(true) . isJTrue() succeeds", t.isJTrue());
}
public void testIsJTrue2() {
Term t = (Term) (new Query("X = @(3)")).oneSolution().get("X");
assertFalse("@(3) . isJTrue() fails", t.isJTrue());
}
public void testIsJTrue3() {
Term t = (Term) (new Query("X = _")).oneSolution().get("X");
assertFalse("_ . isJTrue() fails", t.isJTrue());
}
public void testIsJTrue4() {
Term t = (Term) (new Query("X = @(false)")).oneSolution().get("X");
assertFalse("@(false) . isJTrue() fails", t.isJTrue());
}
public void testIsJVoid1() {
Term t = (Term) (new Query("X = @(void)")).oneSolution().get("X");
assertTrue("@(void) . isJVoid() succeeds", t.isJVoid());
}
public void testIsJVoid2() {
Term t = (Term) (new Query("X = @(3)")).oneSolution().get("X");
assertFalse("@(3) . isJVoid() fails", t.isJVoid());
}
public void testIsJVoid3() {
Term t = (Term) (new Query("X = _")).oneSolution().get("X");
assertFalse("_ . isJVoid() fails", t.isJVoid());
}
public void testTypeName1() {
assertEquals("Y = foo binds Y to an Atom", ((Term) Query.oneSolution("Y = foo").get("Y")).typeName(), "Atom");
}
public void testTypeName2() {
assertEquals("Y = 3.14159 binds Y to a Float", ((Term) Query.oneSolution("Y = 3.14159").get("Y")).typeName(), "Float");
}
public void testTypeName4() {
assertEquals("Y = 6 binds Y to an Integer", ((Term) Query.oneSolution("Y = 6").get("Y")).typeName(), "Integer");
}
public void testTypeName5() {
assertEquals("Y = _ binds Y to a Variable", ((Term) Query.oneSolution("Y = _").get("Y")).typeName(), "Variable");
}
public void testTypeName3() {
assertEquals("Y = f(x) binds Y to a Compound", ((Term) Query.oneSolution("Y = f(x)").get("Y")).typeName(), "Compound");
}
public void testGoalWithModulePrefix1() {
String goal = "jpl:jpl_modifier_bit(volatile,I)";
assertTrue(goal + " binds I to an integer", ((Term) Query.oneSolution(goal).get("I")).isInteger());
}
public void testGoalWithModulePrefix2() {
String goal = "user:length([],0)";
assertTrue(goal + " succeeds", Query.hasSolution(goal));
}
public void testGoalWithModulePrefix3() {
try {
(new Query("3:length([],0)")).hasSolution();
// shouldn't get to here
fail("(new Query(\"3:length([],0)\")).hasSolution() didn't throw exception");
} catch (jpl.PrologException e) {
// correct exception class, but is it correct in detail?
if (e.term().hasFunctor("error", 2) && e.term().arg(1).hasFunctor("type_error", 2) && e.term().arg(1).arg(1).hasFunctor("atom", 0)) {
// OK: an appropriate exception was thrown
} else {
fail("(new Query(\"3:length([],0)\")).hasSolution() threw incorrect PrologException: " + e);
}
} catch (Exception e) {
fail("(new Query(\"3:length([],0)\")).hasSolution() threw wrong class of exception: " + e);
}
}
public void testGoalWithModulePrefix4() {
try {
(new Query("_:length([],0)")).hasSolution();
// shouldn't get to here
fail("bad (unbound) module prefix");
} catch (jpl.PrologException e) {
// correct exception class, but is it correct in detail?
if (e.term().hasFunctor("error", 2) && e.term().arg(1).hasFunctor("instantiation_error", 0)) {
// OK: an appropriate exception was thrown
} else {
fail("(new Query(\"_:length([],0)\")).hasSolution() threw incorrect PrologException: " + e);
}
} catch (Exception e) {
fail("(new Query(\"_:length([],0)\")).hasSolution() threw wrong class of exception: " + e);
}
}
public void testGoalWithModulePrefix5() {
try {
(new Query("f(x):length([],0)")).hasSolution();
// shouldn't get to here
fail("bad (compound) module prefix");
} catch (jpl.PrologException e) {
// correct exception class, but is it correct in detail?
if (e.term().hasFunctor("error", 2) && e.term().arg(1).hasFunctor("type_error", 2) && e.term().arg(1).arg(1).hasFunctor("atom", 0)) {
// OK: an appropriate exception was thrown
} else {
fail("(new Query(\"f(x):length([],0)\")).hasSolution() threw incorrect PrologException: " + e);
}
} catch (Exception e) {
fail("(new Query(\"f(x):length([],0)\")).hasSolution() threw wrong class of exception: " + e);
}
}
public void testGoalWithModulePrefix6() {
try {
(new Query("no_such_module:no_such_predicate(0)")).hasSolution();
// shouldn't get to here
fail("bad (nonexistent) module prefix");
} catch (jpl.PrologException e) {
// correct exception class, but is it correct in detail?
if (e.term().hasFunctor("error", 2) && e.term().arg(1).hasFunctor("existence_error", 2) && e.term().arg(1).arg(1).hasFunctor("procedure", 0)) {
// OK: an appropriate exception was thrown
} else {
fail("(new Query(\"f(x):length([],0)\")).hasSolution() threw incorrect PrologException: " + e);
}
} catch (Exception e) {
fail("(new Query(\"f(x):length([],0)\")).hasSolution() threw wrong class of exception: " + e);
}
}
// public void testFetchCyclicTerm(){
// assertTrue((new Query("X=f(X)")).hasSolution());
// }
public void testFetchLongList0() {
assertTrue((new Query("findall(foo(N),between(0,10,N),L)")).hasSolution());
}
public void testFetchLongList1() {
assertTrue((new Query("findall(foo(N),between(0,100,N),L)")).hasSolution());
}
public void testFetchLongList2() {
assertTrue((new Query("findall(foo(N),between(0,1000,N),L)")).hasSolution());
}
public void testFetchLongList2c() {
assertTrue((new Query("findall(foo(N),between(0,1023,N),L)")).hasSolution());
}
public void testFetchLongList2a() {
assertTrue((new Query("findall(foo(N),between(0,2000,N),L)")).hasSolution());
}
// public void testFetchLongList2b() {
// assertTrue((new Query("findall(foo(N),between(0,3000,N),L)")).hasSolution());
// }
// public void testFetchLongList3() {
// assertTrue((new Query("findall(foo(N),between(0,10000,N),L)")).hasSolution());
// }
public void testUnicode0() {
assertTrue(Query.hasSolution("atom_codes(?,[32])", new Term[] { new Atom(" ") }));
}
public void testUnicode0a() {
assertTrue(Query.hasSolution("atom_codes(?,[32])", new Term[] { new Atom("\u0020") }));
}
public void testUnicode0b() {
assertTrue(Query.hasSolution("atom_codes(?,[0])", new Term[] { new Atom("\u0000") }));
}
public void testUnicode0c() {
assertTrue(Query.hasSolution("atom_codes(?,[1])", new Term[] { new Atom("\u0001") }));
}
public void testUnicode0d() {
assertTrue(Query.hasSolution("atom_codes(?,[127])", new Term[] { new Atom("\u007F") }));
}
public void testUnicode0e() {
assertTrue(Query.hasSolution("atom_codes(?,[128])", new Term[] { new Atom("\u0080") }));
}
public void testUnicode0f() {
assertTrue(Query.hasSolution("atom_codes(?,[255])", new Term[] { new Atom("\u00FF") }));
}
public void testUnicode0g() {
assertTrue(Query.hasSolution("atom_codes(?,[256])", new Term[] { new Atom("\u0100") }));
}
public void testUnicode1() {
assertTrue(Query.hasSolution("atom_codes(?,[0,127,128,255])", new Term[] { new Atom("\u0000\u007F\u0080\u00FF") }));
}
public void testUnicode2() {
assertTrue(Query.hasSolution("atom_codes(?,[256,32767,32768,65535])", new Term[] { new Atom("\u0100\u7FFF\u8000\uFFFF") }));
}
public void testStringXput1() {
Term a = (Term) (Query.oneSolution("string_concat(foo,bar,S)").get("S"));
assertTrue(a.name().equals("foobar"));
}
public void testStringXput2() {
String s1 = "\u0000\u007F\u0080\u00FF";
String s2 = "\u0100\u7FFF\u8000\uFFFF";
String s = s1 + s2;
Term a1 = new Atom(s1);
Term a2 = new Atom(s2);
Term a = (Term) (Query.oneSolution("string_concat(?,?,S)", new Term[] { a1, a2 }).get("S"));
assertEquals(a.name(), s);
}
// public void testMaxInteger1(){
// assertEquals(((Term)(Query.oneSolution("current_prolog_flag(max_integer,I)").get("I"))).longValue(), java.lang.Long.MAX_VALUE); // i.e. 9223372036854775807L
// }
// public void testSingleton1() {
// assertTrue(Query.hasSolution("style_check(-singleton),consult('test_singleton.pl')"));
// }
public void testStaticQueryInvalidSourceText2() {
String goal = "p(]";
try {
Query.hasSolution(goal);
} catch (jpl.PrologException e) {
if (e.term().hasFunctor("error", 2) && e.term().arg(1).hasFunctor("syntax_error", 1) && e.term().arg(1).arg(1).hasFunctor("cannot_start_term", 0)) {
// OK: an appropriate exception was thrown
} else {
fail("Query.hasSolution(" + goal + ") threw incorrect PrologException: " + e);
}
} catch (Exception e) {
fail("Query.hasSolution(" + goal + ") threw wrong class of exception: " + e);
}
}
public void testStaticQueryInvalidSourceText1() {
String goal = "bad goal";
try {
Query.hasSolution(goal);
} catch (jpl.PrologException e) {
if (e.term().hasFunctor("error", 2) && e.term().arg(1).hasFunctor("syntax_error", 1) && e.term().arg(1).arg(1).hasFunctor("operator_expected", 0)) {
// OK: an appropriate exception was thrown
} else {
fail("Query.hasSolution(" + goal + ") threw incorrect PrologException: " + e);
}
} catch (Exception e) {
fail("Query.hasSolution(" + goal + ") threw wrong class of exception: " + e);
}
}
public void testStaticQueryNSolutions1() {
String goal = "member(X, [0,1,2,3,4,5,6,7,8,9])";
int n = 5;
assertTrue("Query.nSolutions(" + goal + ", " + n + ") returns " + n + " solutions", Query.nSolutions(goal, n).length == n);
}
public void testStaticQueryNSolutions2() {
String goal = "member(X, [0,1,2,3,4,5,6,7,8,9])";
int n = 0;
assertTrue("Query.nSolutions(" + goal + ", " + n + ") returns " + n + " solutions", Query.nSolutions(goal, n).length == n);
}
public void testStaticQueryNSolutions3() {
String goal = "member(X, [0,1,2,3,4,5,6,7,8,9])";
int n = 20;
assertTrue("Query.nSolutions(" + goal + ", " + n + ") returns 10 solutions", Query.nSolutions(goal, n).length == 10);
}
public void testStaticQueryAllSolutions1() {
String goal = "member(X, [0,1,2,3,4,5,6,7,8,9])";
assertTrue("Query.allSolutions(" + goal + ") returns 10 solutions", Query.allSolutions(goal).length == 10);
}
public void testStaticQueryHasSolution1() {
String goal = "memberchk(13, [?,?,?])";
Term[] params = new Term[] { new Integer(12), new Integer(13), new Integer(14) };
assertTrue(Query.hasSolution(goal, params));
}
public void testStaticQueryHasSolution2() {
String goal = "memberchk(23, [?,?,?])";
Term[] params = new Term[] { new Integer(12), new Integer(13), new Integer(14) };
assertFalse(Query.hasSolution(goal, params));
}
public void testUtilListToTermArray1() {
String goal = "T = [a,b,c]";
Term list = (Term) Query.oneSolution(goal).get("T");
Term[] array = Util.listToTermArray(list);
assertTrue(array[2].isAtom() && array[2].name().equals("c"));
}
public void testTermToTermArray1() {
String goal = "T = [a,b,c]";
Term list = (Term) Query.oneSolution(goal).get("T");
Term[] array = list.toTermArray();
assertTrue(array[2].isAtom() && array[2].name().equals("c"));
}
public void testJRef1() {
// System.out.println("java.library.path=" + System.getProperties().get("java.library.path"));
// System.out.println("jpl.c version = " + jpl.fli.Prolog.get_c_lib_version());
int i = 76543;
Integer I = new Integer(i);
Query q = new Query("jpl_call(?,intValue,[],I2)", new Term[] { new JRef(I) });
Term I2 = (Term) q.oneSolution().get("I2");
assertTrue(I2.isInteger() && I2.intValue() == i);
}
public void testBerhhard1() {
assertTrue(Query.allSolutions( "consult(library('lists'))" ).length == 1);
}
}

View File

@@ -0,0 +1,142 @@
package jpl.test;
import java.util.Map;
import jpl.Atom;
import jpl.Compound;
import jpl.Integer;
import jpl.JPL;
import jpl.PrologException;
import jpl.Query;
import jpl.Term;
import jpl.Util;
import jpl.Variable;
import jpl.fli.Prolog;
// This class is nearly obsolete; most of its tests have been migrated to TestJUnit.
public class TestOLD {
private static void test10() {
System.err.println("test10:");
System.err.println(" java_lib_version = " + JPL.version_string());
System.err.println(" c_lib_version = " + jpl.fli.Prolog.get_c_lib_version());
System.err.println(" pl_lib_version = " + new Query(new Compound("jpl_pl_lib_version", new Term[] { new Variable("V") })).oneSolution().get("V"));
System.err.println(" java.version = " + System.getProperty("java.version"));
System.err.println(" os.name = " + System.getProperty("os.name"));
System.err.println(" os.arch = " + System.getProperty("os.arch"));
System.err.println(" os.version = " + System.getProperty("os.version"));
System.err.println();
}
private static void test10j() {
Term l2 = Util.termArrayToList(new Term[] { new Atom("a"), new Atom("b"), new Atom("c"), new Atom("d"), new Atom("e") });
Query q9 = new Query(new Compound("append", new Term[] { new Variable("Xs"), new Variable("Ys"), l2 }));
Map[] s9s = q9.allSolutions();
System.err.println("test10j:");
for (int i = 0; i < s9s.length; i++) {
System.err.println(" append(Xs,Ys,[a,b,c,d,e]) -> " + Util.toString(s9s[i]));
}
System.err.println();
}
private static void test10k() {
String[] args = jpl.fli.Prolog.get_default_init_args();
String which;
String s = "";
System.err.println("test10k:");
if (args == null) {
args = jpl.fli.Prolog.get_actual_init_args();
which = "actual";
} else {
which = "default";
}
for (int i = 0; i < args.length; i++) {
s = s + args[i] + " ";
}
System.err.println(" " + which + "_init_args = " + s + '\n');
}
private static void test10l() {
Query q5 = new Query(new Compound("length", new Term[] { new Variable("Zs"), new jpl.Integer(5) }));
Map s5 = q5.oneSolution();
System.err.println("test10l:");
System.err.println(" length(Zs,5)");
System.err.println(" " + Util.toString(s5));
System.err.println(" Zs = " + (Term) s5.get("Zs"));
System.err.println();
}
private static void test10m() {
String text = "append(Xs,Ys,[_,_,_,_,_])";
Query q = new Query(text);
Map[] ss = q.allSolutions();
System.err.println("test10m:");
System.err.println(" all solutions of " + text);
for (int i = 0; i < ss.length; i++) {
System.err.println(" " + Util.toString(ss[i]));
}
System.err.println();
}
private static void test10o() {
System.err.println("test10o:");
Term l2b = Util.termArrayToList(new Term[] { new Variable("A"), new Variable("B"), new Variable("C"), new Variable("D"), new Variable("E") });
Query q9b = new Query(new Compound("append", new Term[] { new Variable("Xs"), new Variable("Ys"), l2b }));
Map[] s9bs = q9b.allSolutions();
for (int i = 0; i < s9bs.length; i++) {
System.err.println(" append(Xs,Ys,[A,B,C,D,E]) -> " + Util.toString(s9bs[i]));
}
System.err.println();
}
private static void test10q() {
System.err.println("test10q:");
System.err.println((new Compound("Bad Name", new Term[] { new Atom("3 3") })).toString());
System.err.println();
}
private static void test10s() {
final Query q = new Query("jpl_slow_goal"); // 10 successive sleep(1)
System.err.println("test10s:");
Thread t = new Thread(new Runnable() {
public void run() {
try {
System.err.println("q.hasSolution() ... ");
System.err.println(q.hasSolution() ? "finished" : "failed");
} catch (Exception e) {
System.err.println("q.hasSolution() threw " + e);
}
}
});
t.start(); // call the query in a separate thread
System.err.println("pausing for 2 secs...");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
;
} // wait a coupla seconds for it to get started
// (new Query("set_prolog_flag(abort_with_exception, true)")).hasSolution();
System.err.println("calling q.abort()...");
q.abort();
System.err.println();
}
public static void main(String argv[]) {
Prolog.set_default_init_args(new String[] { "libpl.dll", "-f", "none", "-g", "set_prolog_flag(debug_on_error,false)", "-q" });
System.err.println("tag = " + Prolog.object_to_tag(new Query("hello")));
test10k();
test10();
// test10h();
// test10i();
test10j();
test10k();
test10l();
test10m();
// test10n();
test10o();
//test10p();
test10q();
// test10r();
// test10s();
// test10t();
// test10u();
// test10v();
String s = new String("" + '\0' + '\377');
System.err.println("s.length = " + s.length());
for (int i = 0; i < s.length(); i++) {
System.err.print((new Integer(s.charAt(i))).toString() + " ");
}
System.err.println();
System.err.println(new Query("atom_codes(A,[127,128,255,0])").oneSolution().toString());
}
}