From 7b993ca987584d6c822a23f3dccd9f55d310ca1c Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Sat, 2 May 2009 14:11:10 -0500 Subject: [PATCH] this should be in packages, not in LGPL! --- LGPL/JPL/Makefile.in | 30 - LGPL/JPL/java/Makefile.in | 119 - LGPL/JPL/java/jpl/Atom.java | 165 - LGPL/JPL/java/jpl/Compound.java | 386 -- LGPL/JPL/java/jpl/Float.java | 294 - LGPL/JPL/java/jpl/Integer.java | 268 - LGPL/JPL/java/jpl/JBoolean.java | 217 - LGPL/JPL/java/jpl/JPL.java | 211 - LGPL/JPL/java/jpl/JPLException.java | 63 - LGPL/JPL/java/jpl/JRef.java | 195 - LGPL/JPL/java/jpl/JVoid.java | 185 - LGPL/JPL/java/jpl/PrologException.java | 75 - LGPL/JPL/java/jpl/Query.java | 869 --- LGPL/JPL/java/jpl/Term.java | 783 --- LGPL/JPL/java/jpl/Util.java | 281 - LGPL/JPL/java/jpl/Variable.java | 277 - LGPL/JPL/java/jpl/Version.java | 9 - LGPL/JPL/java/jpl/fli/BooleanHolder.java | 60 - LGPL/JPL/java/jpl/fli/DoubleHolder.java | 60 - LGPL/JPL/java/jpl/fli/Int64Holder.java | 34 - LGPL/JPL/java/jpl/fli/IntHolder.java | 60 - LGPL/JPL/java/jpl/fli/LongHolder.java | 61 - LGPL/JPL/java/jpl/fli/ObjectHolder.java | 60 - LGPL/JPL/java/jpl/fli/PointerHolder.java | 63 - LGPL/JPL/java/jpl/fli/Prolog.java | 237 - LGPL/JPL/java/jpl/fli/StringHolder.java | 60 - LGPL/JPL/java/jpl/fli/atom_t.java | 82 - LGPL/JPL/java/jpl/fli/engine_t.java | 56 - LGPL/JPL/java/jpl/fli/fid_t.java | 60 - LGPL/JPL/java/jpl/fli/functor_t.java | 61 - LGPL/JPL/java/jpl/fli/module_t.java | 61 - LGPL/JPL/java/jpl/fli/predicate_t.java | 61 - LGPL/JPL/java/jpl/fli/qid_t.java | 60 - LGPL/JPL/java/jpl/fli/term_t.java | 133 - LGPL/JPL/java/jpl/test/CelsiusConverter.java | 78 - LGPL/JPL/java/jpl/test/Family.java | 96 - LGPL/JPL/java/jpl/test/FetchBigTree.java | 18 - LGPL/JPL/java/jpl/test/FetchLongList.java | 17 - LGPL/JPL/java/jpl/test/Ga.java | 23 - LGPL/JPL/java/jpl/test/Ga2.java | 10 - LGPL/JPL/java/jpl/test/Garbo.java | 19 - LGPL/JPL/java/jpl/test/JPLTest.java | 93 - LGPL/JPL/java/jpl/test/Masstest.java | 42 - LGPL/JPL/java/jpl/test/MaxObjects.java | 4 - LGPL/JPL/java/jpl/test/ShadowA.java | 13 - LGPL/JPL/java/jpl/test/ShadowB.java | 16 - LGPL/JPL/java/jpl/test/SyntaxError.java | 10 - LGPL/JPL/java/jpl/test/Test.java | 283 - LGPL/JPL/java/jpl/test/TestJUnit.java | 532 -- LGPL/JPL/java/jpl/test/TestOLD.java | 142 - LGPL/JPL/jpl.yap | 4672 --------------- LGPL/JPL/jpl_paths.yap.in | 43 - LGPL/JPL/src/Makefile.in | 63 - LGPL/JPL/src/hacks.c | 73 - LGPL/JPL/src/jpl.c | 5516 ------------------ 55 files changed, 17459 deletions(-) delete mode 100644 LGPL/JPL/Makefile.in delete mode 100644 LGPL/JPL/java/Makefile.in delete mode 100644 LGPL/JPL/java/jpl/Atom.java delete mode 100644 LGPL/JPL/java/jpl/Compound.java delete mode 100644 LGPL/JPL/java/jpl/Float.java delete mode 100644 LGPL/JPL/java/jpl/Integer.java delete mode 100644 LGPL/JPL/java/jpl/JBoolean.java delete mode 100644 LGPL/JPL/java/jpl/JPL.java delete mode 100644 LGPL/JPL/java/jpl/JPLException.java delete mode 100644 LGPL/JPL/java/jpl/JRef.java delete mode 100644 LGPL/JPL/java/jpl/JVoid.java delete mode 100644 LGPL/JPL/java/jpl/PrologException.java delete mode 100644 LGPL/JPL/java/jpl/Query.java delete mode 100644 LGPL/JPL/java/jpl/Term.java delete mode 100644 LGPL/JPL/java/jpl/Util.java delete mode 100644 LGPL/JPL/java/jpl/Variable.java delete mode 100644 LGPL/JPL/java/jpl/Version.java delete mode 100644 LGPL/JPL/java/jpl/fli/BooleanHolder.java delete mode 100644 LGPL/JPL/java/jpl/fli/DoubleHolder.java delete mode 100644 LGPL/JPL/java/jpl/fli/Int64Holder.java delete mode 100644 LGPL/JPL/java/jpl/fli/IntHolder.java delete mode 100644 LGPL/JPL/java/jpl/fli/LongHolder.java delete mode 100644 LGPL/JPL/java/jpl/fli/ObjectHolder.java delete mode 100644 LGPL/JPL/java/jpl/fli/PointerHolder.java delete mode 100644 LGPL/JPL/java/jpl/fli/Prolog.java delete mode 100644 LGPL/JPL/java/jpl/fli/StringHolder.java delete mode 100644 LGPL/JPL/java/jpl/fli/atom_t.java delete mode 100644 LGPL/JPL/java/jpl/fli/engine_t.java delete mode 100644 LGPL/JPL/java/jpl/fli/fid_t.java delete mode 100644 LGPL/JPL/java/jpl/fli/functor_t.java delete mode 100644 LGPL/JPL/java/jpl/fli/module_t.java delete mode 100644 LGPL/JPL/java/jpl/fli/predicate_t.java delete mode 100644 LGPL/JPL/java/jpl/fli/qid_t.java delete mode 100644 LGPL/JPL/java/jpl/fli/term_t.java delete mode 100644 LGPL/JPL/java/jpl/test/CelsiusConverter.java delete mode 100644 LGPL/JPL/java/jpl/test/Family.java delete mode 100644 LGPL/JPL/java/jpl/test/FetchBigTree.java delete mode 100644 LGPL/JPL/java/jpl/test/FetchLongList.java delete mode 100644 LGPL/JPL/java/jpl/test/Ga.java delete mode 100644 LGPL/JPL/java/jpl/test/Ga2.java delete mode 100644 LGPL/JPL/java/jpl/test/Garbo.java delete mode 100644 LGPL/JPL/java/jpl/test/JPLTest.java delete mode 100644 LGPL/JPL/java/jpl/test/Masstest.java delete mode 100644 LGPL/JPL/java/jpl/test/MaxObjects.java delete mode 100644 LGPL/JPL/java/jpl/test/ShadowA.java delete mode 100644 LGPL/JPL/java/jpl/test/ShadowB.java delete mode 100644 LGPL/JPL/java/jpl/test/SyntaxError.java delete mode 100644 LGPL/JPL/java/jpl/test/Test.java delete mode 100644 LGPL/JPL/java/jpl/test/TestJUnit.java delete mode 100644 LGPL/JPL/java/jpl/test/TestOLD.java delete mode 100644 LGPL/JPL/jpl.yap delete mode 100644 LGPL/JPL/jpl_paths.yap.in delete mode 100644 LGPL/JPL/src/Makefile.in delete mode 100644 LGPL/JPL/src/hacks.c delete mode 100644 LGPL/JPL/src/jpl.c diff --git a/LGPL/JPL/Makefile.in b/LGPL/JPL/Makefile.in deleted file mode 100644 index 39be3bc6d..000000000 --- a/LGPL/JPL/Makefile.in +++ /dev/null @@ -1,30 +0,0 @@ -# -# default base directory for YAP installation -# -ROOTDIR = @prefix@ -# -# where the binary should be -# -BINDIR = $(ROOTDIR)/bin -# -# where YAP should look for binary libraries -# -LIBDIR=$(ROOTDIR)/lib/Yap -# -# where YAP should look for architecture-independent Prolog libraries -# -SHAREDIR=$(ROOTDIR)/share -# -# -# You shouldn't need to change what follows. -# -INSTALL=@INSTALL@ -INSTALL_DATA=@INSTALL_DATA@ -INSTALL_PROGRAM=@INSTALL_PROGRAM@ -srcdir=@srcdir@ - -PROGRAMS= $(srcdir)/jpl.yap jpl_paths.yap - -install: $(PROGRAMS) - mkdir -p $(DESTDIR)$(SHAREDIR)/Yap - for p in $(PROGRAMS); do $(INSTALL_DATA) $$p $(DESTDIR)$(SHAREDIR)/Yap; done diff --git a/LGPL/JPL/java/Makefile.in b/LGPL/JPL/java/Makefile.in deleted file mode 100644 index a29e2c8e9..000000000 --- a/LGPL/JPL/java/Makefile.in +++ /dev/null @@ -1,119 +0,0 @@ -################################################################ -# Build jpl.jar -################################################################ - -.SUFFIXES: .java .class - -# -# default base directory for YAP installation -# -ROOTDIR = @prefix@ -# -# where the binary should be -# -BINDIR = $(ROOTDIR)/bin -# -# where YAP should look for binary libraries -# -LIBDIR=$(ROOTDIR)/lib/Yap -# -# where YAP should look for architecture-independent Prolog libraries -# -SHAREDIR=$(ROOTDIR)/share -# -# -# You shouldn't need to change what follows. -# -INSTALL=@INSTALL@ -INSTALL_DATA=@INSTALL_DATA@ -INSTALL_PROGRAM=@INSTALL_PROGRAM@ -SHELL=/bin/sh -RANLIB=@RANLIB@ -srcdir=@srcdir@ -SHLIB_CFLAGS=@SHLIB_CFLAGS@ -SHLIB_SUFFIX=@SHLIB_SUFFIX@ -#4.1VPATH=@srcdir@:@srcdir@/OPTYap -CWD=$(PWD) -# -JAVAC=@JAVAC@ -JAR=@JAR@ -JPLJAR=jpl.jar -TESTJAR=jpltest.jar - -CLS= jpl/Atom.java \ - jpl/Compound.java \ - jpl/Float.java \ - jpl/Integer.java \ - jpl/JRef.java \ - jpl/JPLException.java \ - jpl/JPL.java \ - jpl/PrologException.java \ - jpl/Query.java \ - jpl/Term.java \ - jpl/Util.java \ - jpl/Variable.java \ - jpl/Version.java - -FLI= jpl/fli/atom_t.java \ - jpl/fli/BooleanHolder.java \ - jpl/fli/DoubleHolder.java \ - jpl/fli/engine_t.java \ - jpl/fli/fid_t.java \ - jpl/fli/functor_t.java \ - jpl/fli/IntHolder.java \ - jpl/fli/Int64Holder.java \ - jpl/fli/LongHolder.java \ - jpl/fli/module_t.java \ - jpl/fli/ObjectHolder.java \ - jpl/fli/PointerHolder.java \ - jpl/fli/predicate_t.java \ - jpl/fli/Prolog.java \ - jpl/fli/qid_t.java \ - jpl/fli/StringHolder.java \ - jpl/fli/term_t.java - -TEST= jpl/test/Family.java \ - jpl/test/FetchBigTree.java \ - jpl/test/FetchLongList.java \ - jpl/test/Ga2.java \ - jpl/test/Ga.java \ - jpl/test/Garbo.java \ - jpl/test/Masstest.java \ - jpl/test/MaxObjects.java \ - jpl/test/ShadowA.java \ - jpl/test/ShadowB.java \ - jpl/test/SyntaxError.java \ - jpl/test/Test.java \ - jpl/test/TestJUnit.java \ - jpl/test/TestOLD.java - -JAVA=$(CLS) $(FLI) -CLASSES=$(JAVA:.java=.class) - -TESTCLASSES=$(TEST:.java=.class) - -all: $(JPLJAR) - -$(JAVA): - for f in $(CLS); do cp $(srcdir)/$$f jpl ; done - for f in $(FLI); do cp $(srcdir)/$$f jpl/fli ; done - for f in $(TEST); do cp $(srcdir)/$$f jpl/test ; done - -$(JPLJAR): $(JAVA) - $(JAVAC) $(JAVA) - $(JAR) cf $(JPLJAR) $(CLASSES) - -$(TESTJAR): $(JAVA) $(TEST) - $(JAVAC) -classpath $(JPLJAR):$(JUNIT) $(TEST) - $(JAR) cf $(JPL) $(TESTCLASSES) - -clean:: - rm -f *~ $(CLASSES) - -distclean: clean - rm -r $(JPL) - -install: $(JPLJAR) - mkdir -p $(DESTDIR)$(SHAREDIR)/Yap - $(INSTALL_DATA) $(JPLJAR) $(DESTDIR)$(SHAREDIR)/Yap - diff --git a/LGPL/JPL/java/jpl/Atom.java b/LGPL/JPL/java/jpl/Atom.java deleted file mode 100644 index a56aecee1..000000000 --- a/LGPL/JPL/java/jpl/Atom.java +++ /dev/null @@ -1,165 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: Atom.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// Author: Paul Singleton -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 2004 Paul Singleton -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl; - -import java.util.Map; - -import jpl.fli.Prolog; -import jpl.fli.StringHolder; -import jpl.fli.term_t; - -//----------------------------------------------------------------------/ -// Atom -/** - * Atom is a specialised Compound with zero arguments, representing a Prolog atom with the same name. - * An Atom is constructed with a String parameter (its name, unquoted), which cannot thereafter be changed. - *
Atom a = new Atom("hello");
- * An Atom can be used (and re-used) as an argument of Compound Terms. - * Two Atom instances are equal (by equals()) iff they have equal names. - * - *
- * Copyright (C) 2004 Paul Singleton

- * Copyright (C) 1998 Fred Dushin

- * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

- * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

- *


- * @author Fred Dushin - * @version $Revision: 1.2 $ - * @see jpl.Term - * @see jpl.Compound - */ -public class Atom extends Compound { - - //==================================================================/ - // Attributes (none) - //==================================================================/ - - //==================================================================/ - // Constructors - //==================================================================/ - - /** - * @param name the Atom's name (unquoted) - */ - public Atom(String name) { - super(name); - } - - //==================================================================/ - // Methods (common) - //==================================================================/ - - // these are all inherited from Compound - - public final int type() { - return Prolog.ATOM; - } - - /** - * returns the name of the type of this term, as "Atom" - * - * @return the name of the type of this term, as "Atom" - */ - public String typeName() { // overrides same in jpl.Term - return "Atom"; - } - //==================================================================/ - // Methods (deprecated) - //==================================================================/ - - /** - * Returns a debug-friendly String representation of an Atom. - * - * @return a debug-friendly String representation of an Atom - * @deprecated - */ - public String debugString() { - return "(Atom " + toString() + ")"; - } - - //==================================================================/ - // Converting JPL Terms to Prolog terms - //==================================================================/ - - // (this is done with the put() method inherited from Compound) - - //==================================================================/ - // Converting Prolog terms to JPL Terms - //==================================================================/ - - /** - * Converts a Prolog term (as a term_t), which is known to be an atom, to a new jpl.Atom. - * This is only called from Term.getTerm(), - * and just creates a new Atom object initialised with the atom's name. - * - * @param vars_to_Vars A Map from Prolog variables to JPL Variables. - * @param term The Prolog term to be converted - * @return A new Atom instance - */ - protected static Term getTerm1(Map vars_to_Vars, term_t term) { - StringHolder holder = new StringHolder(); - Prolog.get_atom_chars(term, holder); // ignore return val; assume success... - - return new Atom(holder.value); - } - - /** - * Converts a Prolog term (as a term_t), which is known to be a SWI-Prolog string, to a new jpl.Atom, - * by creating a new Atom object initialised with the string's value. - * JPL users should avoid SWI-Prolog's non-ISO strings, but in some obscure - * circumstances they are returned unavoidably, so we have to handle them - * (and this is how). - * - * @param vars_to_Vars A Map from Prolog variables to JPL Variables. - * @param term The term_t to convert - * @return A new Atom instance - */ - protected static Term getString(Map vars_to_Vars, term_t term) { - StringHolder holder = new StringHolder(); - Prolog.get_string_chars(term, holder); // ignore return val; assume success... - return new Atom(holder.value); - } - - //==================================================================/ - // Computing substitutions - //==================================================================/ - - // (done with the inherited Compound.getSubst() method) - -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/Compound.java b/LGPL/JPL/java/jpl/Compound.java deleted file mode 100644 index c93e29228..000000000 --- a/LGPL/JPL/java/jpl/Compound.java +++ /dev/null @@ -1,386 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: Compound.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 2004 Paul Singleton -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl; - -import java.util.Map; -import jpl.fli.IntHolder; -import jpl.fli.ObjectHolder; -import jpl.fli.Prolog; -import jpl.fli.StringHolder; -import jpl.fli.term_t; - -//----------------------------------------------------------------------/ -// Compound -/** - * A Compound represents a structured term, - * comprising a functor and arguments (Terms). - * Atom is a subclass of Compound, whose instances have zero arguments. - * Direct instances of Compound must have one or more arguments - * (it is an error to attempt to construct a Compound with zero args; - * a JPLException will be thrown). - * For example, this Java expression yields - * a representation of the term f(a): - *
- * new Compound( "f", new Term[] { new Atom("a") } )
- * 
- * Note the use of the "anonymous array" notation to denote the arguments - * (an anonymous array of Term). - *
- * Alternatively, construct the Term from Prolog source syntax: - *
- * Util.textToTerm("f(a)")
- * 
- * The arity of a Compound is the quantity of its arguments. - * Once constructed, neither the name nor the arity of a Compound can be altered. - * An argument of a Compound can be replaced with the setArg() method. - *
- * Copyright (C) 2004 Paul Singleton

- * Copyright (C) 1998 Fred Dushin

- * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

- * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

- *


- * @author Fred Dushin - * @version $Revision: 1.2 $ - * @see jpl.Term - * @see jpl.Atom - */ -public class Compound extends Term { - //==================================================================/ - // Attributes - //==================================================================/ - /** - * the name of this Compound - */ - protected final String name; - /** - * the arguments of this Compound - */ - protected final Term[] args; - //==================================================================/ - // Constructors - //==================================================================/ - /** - * Creates a Compound with name but no args (i.e. an Atom). - * This condsructor is protected (from illegal public use) and is used - * only by Atom, which inherits it. - * - * @param name the name of this Compound - * @param args the arguments of this Compound - */ - protected Compound(String name) { - if (name == null) { - throw new JPLException("jpl.Atom: cannot construct with null name"); - } - this.name = name; - this.args = new Term[] {}; - } - /** - * Creates a Compound with name and args. - * - * @param name the name of this Compound - * @param args the (one or more) arguments of this Compound - */ - public Compound(String name, Term[] args) { - if (name == null) { - throw new JPLException("jpl.Compound: cannot construct with null name"); - } - if (args == null) { - throw new JPLException("jpl.Compound: cannot construct with null args"); - } - if (args.length == 0) { - throw new JPLException("jpl.Compound: cannot construct with zero args"); - } - this.name = name; - this.args = args; - } - /** - * Creates a Compound with name and arity. - * This constructor, along with the setArg method, serves the new, native Prolog-term-to-Java-term routine, - * and is public only so as to be accessible via JNI: it is not intended for general use. - * - * @param name the name of this Compound - * @param arity the arity of this Compound - */ - public Compound(String name, int arity) { - if (name == null) { - throw new JPLException("jpl.Compound: cannot construct with null name"); - } - if (arity < 0) { - throw new JPLException("jpl.Compound: cannot construct with negative arity"); - } - this.name = name; - this.args = new Term[arity]; - } - //==================================================================/ - // Methods (common) - //==================================================================/ - /** - * Returns the ith argument (counting from 1) of this Compound; - * throws an ArrayIndexOutOfBoundsException if i is inappropriate. - * - * @return the ith argument (counting from 1) of this Compound - */ - public final Term arg(int i) { - return args[i - 1]; - } - /** - * Tests whether this Compound's functor has (String) 'name' and 'arity'. - * - * @return whether this Compound's functor has (String) 'name' and 'arity' - */ - public final boolean hasFunctor(String name, int arity) { - return name.equals(this.name) && arity == args.length; // BUGFIX: was just name.equals(name) - } - /** - * whether this Term is a 'jboolean' structure denoting Java's false, i.e. @(false) - * - * @return whether this Term is a 'jboolean' structure denoting Java's false, i.e. @(false) - */ - public boolean isJFalse() { - return hasFunctor("@", 1) && arg(1).hasFunctor("false", 0); - } - /** - * whether this Term is a 'jboolean' structure denoting Java's true, i.e. @(fatruelse) - * - * @return whether this Term is a 'jboolean' structure denoting Java's true, i.e. @(fatruelse) - */ - public boolean isJTrue() { - return hasFunctor("@", 1) && arg(1).hasFunctor("true", 0); - } - /** - * whether this Term is a 'jnull' structure, i.e. @(null) - * - * @return whether this Term is a 'jnull' structure, i.e. @(null) - */ - public boolean isJNull() { - return hasFunctor("@", 1) && arg(1).hasFunctor("null", 0); - } - /** - * whether this Term is a 'jvoid' structure, i.e. @(void) - * - * @return whether this Term is a 'jvoid' structure, i.e. @(void) - */ - public boolean isJVoid() { - return hasFunctor("@", 1) && arg(1).hasFunctor("void", 0); - } - /** - * whether this Term is a 'jobject' structure, i.e. @(Tag) - * - * @return whether this Term is a 'jobject' structure, i.e. @(Tag) - */ - public boolean isJObject() { - return hasFunctor("@", 1) && arg(1).isAtom() && JPL.isTag(arg(1).name()); - } - /** - * whether this Term is a 'jref' structure, i.e. @(Tag) or @(null) - * - * @return whether this Term is a 'jref' structure, i.e. @(Tag) or @(null) - */ - public boolean isJRef() { - return isJObject() || isJNull(); - } - /** - * Returns the name (unquoted) of this Compound. - * - * @return the name (unquoted) of this Compound - */ - public final String name() { - return name; - } - /** - * Returns the arity (1+) of this Compound. - * - * @return the arity (1+) of this Compound - */ - public final int arity() { - return args.length; - } - /** - * Returns a prefix functional representation of a Compound of the form name(arg1,...), - * where 'name' is quoted iff necessary (to be valid Prolog soutce text) - * and each argument is represented according to its toString() method. - * - * @return string representation of an Compound - */ - public String toString() { - return quotedName() + (args.length > 0 ? "(" + Term.toString(args) + ")" : ""); - } - /** - * Two Compounds are equal if they are identical (same object) or their names and arities are equal and their - * respective arguments are equal. - * - * @param obj the Object to compare (not necessarily another Compound) - * @return true if the Object satisfies the above condition - */ - public final boolean equals(Object obj) { - return (this == obj || (obj instanceof Compound && name.equals(((Compound) obj).name) && Term.terms_equals(args, ((Compound) obj).args))); - } - /** - * returns the type of this term, as jpl.fli.Prolog.COMPOUND - * - * @return the type of this term, as jpl.fli.Prolog.COMPOUND - */ - public int type() { - return Prolog.COMPOUND; - } - /** - * returns the name of the type of this term, as "Compound" - * - * @return the name of the type of this term, as "Compound" - */ - public String typeName(){ - return "Compound"; - } - /** - * Sets the i-th (from 1) arg of this Compound to the given Term instance. - * This method, along with the Compound(name,arity) constructor, serves the new, native Prolog-term-to-Java-term routine, - * and is public only so as to be accessible via JNI: it is not intended for general use. - * - * @param i the index (1+) of the arg to be set - * @param arg the Term which is to become the i-th (from 1) arg of this Compound - */ - public void setArg(int i, Term arg) { - if (i <= 0) { - throw new JPLException("jpl.Compound#setArg: bad (non-positive) argument index"); - } - if (i > args.length) { - throw new JPLException("jpl.Compound#setArg: bad (out-of-range) argument index"); - } - if (arg == null) { - throw new JPLException("jpl.Compound#setArg: bad (null) argument"); - } - args[i - 1] = arg; - } - //==================================================================/ - // Methods (protected) - //==================================================================/ - /** - * Returns a quoted (iff necessary) form of the Atom's name, as understood by Prolog read/1 - * (I suspect that there are more efficient ways of doing this) - * - * @return a quoted form of the Atom's name, as understood by Prolog read/1 - */ - protected String quotedName() { - return ((Atom) (new Query(new Compound("sformat", new Term[] { new Variable("S"), new Atom("~q"), new Compound(".", new Term[] { new Atom(this.name), new Atom("[]") }) }))).oneSolution().get( - "S")).name; - } - //==================================================================/ - // Methods (deprecated) - //==================================================================/ - /** - * Returns the arguments of this Compound (1..arity) of this Compound as an array[0..arity-1] of Term. - * - * @return the arguments (1..arity) of this Compound as an array[0..arity-1] of Term - * @deprecated - */ - public final Term[] args() { - return args; - } - /** - * Returns the ith argument (counting from 0) of this Compound. - * - * @return the ith argument (counting from 0) of this Compound - * @deprecated - */ - public final Term arg0(int i) { - return args[i]; - } - /** - * Returns a debug-friendly representation of a Compound. - * - * @return a debug-friendly representation of a Compound - * @deprecated - */ - public String debugString() { - return "(Compound " + name + " " + Term.debugString(args) + ")"; - } - //==================================================================/ - // Converting JPL Terms to Prolog terms - //==================================================================/ - /** - * To put a Compound in a term, we create a sequence of term_t - * references from the Term.terms_to_term_ts() method, and then - * use the Prolog.cons_functor_v() method to create a Prolog compound - * term. - * - * @param varnames_to_vars A Map from variable names to Prolog variables - * @param term A (previously created) term_t which is to be - * set to a Prolog term corresponding to the Term subtype - * (Atom, Variable, Compound, etc.) on which the method is invoked. - */ - protected final void put(Map varnames_to_vars, term_t term) { - Prolog.cons_functor_v(term, Prolog.new_functor(Prolog.new_atom(name), args.length), Term.putTerms(varnames_to_vars, args)); - } - //==================================================================/ - // Converting Prolog terms to JPL Terms - //==================================================================/ - /** - * Converts the Prolog term in term_t (known to be a compound) to a JPL Compound. - * In this case, we create a list of Terms by calling Term.getTerm for each - * term_t reference we get from Prolog.get_arg - * (Not sure why we couldn't get a sequence from there, but...).

- * - * @param varnames_to_vars A Map from variable names to Prolog variables - * @param term The Prolog term to convert - * @return A new Compound - */ - protected static Term getTerm1(Map varnames_to_vars, term_t term) { - ObjectHolder jthing_holder = new ObjectHolder(); - StringHolder name_holder = new StringHolder(); - IntHolder arity_holder = new IntHolder(); - Prolog.get_name_arity(term, name_holder, arity_holder); // assume it succeeds - Term args[] = new Term[arity_holder.value]; - for (int i = 1; i <= arity_holder.value; i++) { - term_t termi = Prolog.new_term_ref(); - Prolog.get_arg(i, term, termi); - args[i - 1] = Term.getTerm(varnames_to_vars, termi); - } - return new Compound(name_holder.value, args); - } - //==================================================================/ - // Computing Substitutions - //==================================================================/ - /** - * Nothing needs to be done except to pass the buck to this Compound's args. - * - * @param varnames_to_Terms A Map from variable names to JPL Terms - * @param vars_to_Vars A Map from Prolog variables to JPL Variables - */ - protected final void getSubst(Map varnames_to_Terms, Map vars_to_Vars) { - Term.getSubsts(varnames_to_Terms, vars_to_Vars, args); - } -} -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/Float.java b/LGPL/JPL/java/jpl/Float.java deleted file mode 100644 index d909337cc..000000000 --- a/LGPL/JPL/java/jpl/Float.java +++ /dev/null @@ -1,294 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: Float.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 2004 Paul Singleton -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl; - -import java.util.Map; - -import jpl.fli.DoubleHolder; -import jpl.fli.Prolog; -import jpl.fli.term_t; - -//----------------------------------------------------------------------/ -// Float -/** - * Float is a specialised Term with a double field, representing a Prolog 64-bit ISO/IEC floating point value. - * Once constructed, a Float's value cannot be altered. - *

- * Float f = new Float( 3.14159265 );
- * 
- * A Float can be used (and re-used) in Compound Terms. - * Two Float instances are equal (by .equals()) iff their (double) values are equal. - * - *
- * Copyright (C) 2004 Paul Singleton

- * Copyright (C) 1998 Fred Dushin

- * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

- * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

- *


- * @author Fred Dushin - * @version $Revision: 1.2 $ - * @see jpl.Term - * @see jpl.Compound - */ -public class Float extends Term { - - //==================================================================/ - // Attributes - //==================================================================/ - - /** - * the Float's immutable value - */ - protected final double value; - - //==================================================================/ - // Constructors and Initialization - //==================================================================/ - - /** - * This constructor creates a Float with the supplied - * (double) value. - * - * @param value this Float's value - */ - public Float(double value) { - this.value = value; - } - - //==================================================================/ - // Methods (common) - //==================================================================/ - - /** - * throws a JPLException (arg(int) is defined only for Compound and Atom) - * - * @return the ith argument (counting from 1) of this Float (never) - */ - public final Term arg(int i) { - throw new JPLException("jpl.Float#arg(int) is undefined"); - } - - /** - * The (nonexistent) args of this Float - * - * @return the (nonexistent) args of this Float - */ - public Term[] args() { - return new Term[] {}; - } - - /** - * Tests whether this Float's functor has (String) 'name' and 'arity' (never) - * - * @return whether this Float's functor has (String) 'name' and 'arity' (never) - */ - public final boolean hasFunctor(String name, int arity) { - return false; - } - - /** - * Tests whether this Float's functor has (int) 'name' and 'arity' (never) - * - * @return whether this Float's functor has (int) 'name' and 'arity' (never) - */ - public final boolean hasFunctor(int val, int arity) { - return false; - } - - /** - * Tests whether this Float's functor has (double) 'name' and 'arity' - * - * @return whether this Float's functor has (double) 'name' and 'arity' - */ - public final boolean hasFunctor(double val, int arity) { - return val == this.value && arity == 0; - } - - /** - * throws a JPLException (name() is defined only for Compound, Atom and Variable) - * - * @return the name of this Float (never) - */ - public final String name() { - throw new JPLException("jpl.Float#name() is undefined"); - } - - /** - * Returns the arity (0) of this Float - * - * @return the arity (0) of this Float - */ - public final int arity() { - return 0; - } - - /** - * returns the (double) value of this Float, converted to an int - * - * @return the (double) value of this Float, converted to an int - */ - public final int intValue() { - return (new Double(value)).intValue(); - } - - /** - * returns the (double) value of this Float, converted to a long - * - * @return the (double) value of this Float, converted to a long - */ - public final long longValue() { - return (new Double(value)).longValue(); - } - - /** - * returns the (double) value of this Float, converted to a float - * - * @return the (double) value of this Float, converted to a float - */ - public final float floatValue() { - return (new Double(value)).floatValue(); - } - - /** - * returns the (double) value of this Float - * - * @return the (double) value of this Float - */ - public final double doubleValue() { - return this.value; - } - - public final int type() { - return Prolog.FLOAT; - } - - public String typeName(){ - return "Float"; - } - - /** - * Returns a Prolog source text representation of this Float - * - * @return a Prolog source text representation of this Float - */ - public String toString() { - return "" + value + ""; - } - - /** - * Two Floats are equal if they are the same object, or their values are equal - * - * @param obj The Object to compare - * @return true if the Object satisfies the above condition - */ - public final boolean equals(Object obj) { - return this == obj || (obj instanceof Float && value == ((Float) obj).value); - } - - //==================================================================/ - // Methods (deprecated) - //==================================================================/ - - /** - * The immutable value of this jpl.Float object, as a Java double - * - * @return the Float's value - * @deprecated - */ - public double value() { - return value; - } - - /** - * Returns a debug-friendly String representation of this Float - * - * @return a debug-friendly String representation of this Float - * @deprecated - */ - public String debugString() { - return "(Float " + toString() + ")"; - } - - //==================================================================/ - // Converting JPL Terms to Prolog terms - //==================================================================/ - - /** - * To convert a JPL Float to a Prolog term, we put its value field into the - * term_t as a float. - * - * @param varnames_to_vars A Map from variable names to Prolog variables. - * @param term A (previously created) term_t which is to be - * set to a Prolog float corresponding to this Float's value - */ - protected final void put(Map varnames_to_vars, term_t term) { - Prolog.put_float(term, value); - } - - //==================================================================/ - // Converting Prolog terms to JPL Terms - //==================================================================/ - - /** - * Converts a Prolog term (known to be a float) to a JPL Float. - * - * @param vars_to_Vars A Map from Prolog variables to JPL Variables - * @param term The Prolog term (a float) to convert - * @return A new Float instance - */ - protected static Term getTerm1(Map vars_to_Vars, term_t term) { - DoubleHolder double_holder = new DoubleHolder(); - - Prolog.get_float(term, double_holder); // assume it succeeds... - return new jpl.Float(double_holder.value); - } - - //==================================================================/ - // Computing Substitutions - //==================================================================/ - - /** - * Nothing needs to be done if the Term is an Atom, Integer or (as in this case) a Float - * - * @param varnames_to_Terms A Map from variable names to JPL Terms - * @param vars_to_Vars A Map from Prolog variables to JPL Variables - */ - protected final void getSubst(Map varnames_to_Terms, Map vars_to_Vars) { - } - -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/Integer.java b/LGPL/JPL/java/jpl/Integer.java deleted file mode 100644 index 0d7252c5f..000000000 --- a/LGPL/JPL/java/jpl/Integer.java +++ /dev/null @@ -1,268 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: Integer.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 2004 Paul Singleton -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl; - -import java.util.Map; -import jpl.fli.Int64Holder; -import jpl.fli.Prolog; -import jpl.fli.term_t; - -//----------------------------------------------------------------------/ -// Integer -/** - * Integer is a specialised Term with a long field, representing a Prolog integer value. - *
- * Integer i = new Integer(1024);
- * 
- * Once constructed, the value of an Integer instance cannot be altered. - * An Integer can be used (and re-used) as an argument of Compounds. - * Beware confusing jpl.Integer with java.lang.Integer. - * - *
- * Copyright (C) 2004 Paul Singleton

- * Copyright (C) 1998 Fred Dushin

- * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

- * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

- *


- * @author Fred Dushin - * @version $Revision: 1.2 $ - * @see jpl.Term - * @see jpl.Compound - */ -public class Integer extends Term { - - //==================================================================/ - // Attributes - //==================================================================/ - - /** - * the Integer's immutable long value - */ - protected final long value; - - //==================================================================/ - // Constructors - //==================================================================/ - - /** - * @param value This Integer's (long) value - */ - public Integer(long value) { - this.value = value; - } - - //==================================================================/ - // Methods (common) - //==================================================================/ - - /** - * The (nonexistent) args of this Integer - * - * @return the (nonexistent) args of this Integer - */ - public Term[] args() { - return new Term[] { - }; - } - - /** - * Tests whether this Integer's functor has (int) 'name' and 'arity' (c.f. functor/3) - * - * @return whether this Integer's functor has (int) 'name' and 'arity' - */ - public final boolean hasFunctor(int val, int arity) { - return val == this.value && arity == 0; - } - - /** - * throws a JPLException (name() is defined only for Compound, Atom and Variable) - * - * @return the name of this Integer (never) - */ - public final String name() { - throw new JPLException("jpl.Integer#name() is undefined"); - } - - /** - * Returns the arity (0) of this jpl.Integer (c.f. functor/3) - * - * @return the arity (0) of this jpl.Integer - */ - public final int arity() { - return 0; - } - - /** - * Returns the value of this Integer as an int if possible, else throws a JPLException - * - * @throws JPLException if the value of this Integer is too great to be represented as a Java int - * @return the int value of this Integer - */ - public final int intValue() { - if (value < java.lang.Integer.MIN_VALUE || value > java.lang.Integer.MAX_VALUE) { - throw new JPLException("cannot represent Integer value as an int"); - } else { - return (int) value; - } - } - - /** - * Returns the value of this Integer as a long - * - * @return the value of this Integer as a long - */ - public final long longValue() { - return value; - } - - /** - * Returns the value of this Integer converted to a float - * - * @return the value of this Integer converted to a float - */ - public final float floatValue() { - return (new java.lang.Long(value)).floatValue(); // safe but inefficient... - } - - /** - * Returns the value of this Integer converted to a double - * - * @return the value of this Integer converted to a double - */ - public final double doubleValue() { - return (new java.lang.Long(value)).doubleValue(); // safe but inefficient... - } - - public final int type() { - return Prolog.INTEGER; - } - - public String typeName(){ - return "Integer"; - } - - /** - * Returns a Prolog source text representation of this Integer's value - * - * @return a Prolog source text representation of this Integer's value - */ - public String toString() { - return "" + value; // hopefully invokes Integer.toString() or equivalent - } - - /** - * Two Integer instances are equal if they are the same object, or if their values are equal - * - * @param obj The Object to compare (not necessarily an Integer) - * @return true if the Object satisfies the above condition - */ - public final boolean equals(Object obj) { - return this == obj || (obj instanceof Integer && value == ((Integer) obj).value); - } - - //==================================================================/ - // Methods (deprecated) - //==================================================================/ - - /** - * Returns the int value of this jpl.Integer - * - * @return the Integer's value - * @deprecated - */ - public final int value() { - return (int) value; - } - - /** - * Returns a debug-friendly representation of this Integer's value - * - * @return a debug-friendly representation of this Integer's value - * @deprecated - */ - public String debugString() { - return "(Integer " + toString() + ")"; - } - - //==================================================================/ - // Converting JPL Terms to Prolog terms - //==================================================================/ - - /** - * To convert an Integer into a Prolog term, we put its value into the term_t. - * - * @param varnames_to_vars A Map from variable names to Prolog variables. - * @param term A (previously created) term_t which is to be - * set to a Prolog integer - */ - protected final void put(Map varnames_to_vars, term_t term) { - Prolog.put_integer(term, value); - } - - //==================================================================/ - // Converting Prolog terms to JPL Terms - //==================================================================/ - - /** - * Converts a Prolog term (known to be an integer) to a new Integer instance. - * - * @param vars_to_Vars A Map from Prolog variables to JPL Variables - * @param term The Prolog term (an integer) which is to be converted - * @return A new Integer instance - */ - protected static Term getTerm1(Map vars_to_Vars, term_t term) { - Int64Holder int64_holder = new Int64Holder(); - - Prolog.get_integer(term, int64_holder); // assume it succeeds... - return new jpl.Integer(int64_holder.value); - } - - //==================================================================/ - // Computing Substitutions - //==================================================================/ - - /** - * Nothing needs to be done if the Term is an Atom, Integer or Float - * - * @param varnames_to_Terms A Map from variable names to Terms. - * @param vars_to_Vars A Map from Prolog variables to JPL Variables. - */ - protected final void getSubst(Map varnames_to_Terms, Map vars_to_Vars) { - } - -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/JBoolean.java b/LGPL/JPL/java/jpl/JBoolean.java deleted file mode 100644 index d5688ff9b..000000000 --- a/LGPL/JPL/java/jpl/JBoolean.java +++ /dev/null @@ -1,217 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: JBoolean.java,v 1.1 2004-08-27 20:27:56 vsc Exp $ -// Date: $Date: 2004-08-27 20:27:56 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 2004 Paul Singleton -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl; - -import java.util.Map; - -import jpl.fli.BooleanHolder; -import jpl.fli.Prolog; -import jpl.fli.term_t; - -//----------------------------------------------------------------------/ -// JBoolean -/** - * A jpl.JBoolean is a specialised Term with a boolean field, representing JPL's Prolog representation of a Java boolean value. - *
- * JBoolean b = new JBoolean( true or false );
- * 
- * A JBoolean can be used (and re-used) in Compound Terms. - * - *
- * Copyright (C) 2004 Paul Singleton

- * Copyright (C) 1998 Fred Dushin

- * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

- * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

- *


- * @author Fred Dushin - * @version $Revision: 1.1 $ - * @see jpl.Term - * @see jpl.Compound - */ -public class JBoolean extends Term { - - //==================================================================/ - // Attributes - //==================================================================/ - - /** - * the JBoolean's value (a boolean) - */ - protected final boolean value; - - //==================================================================/ - // Constructors - //==================================================================/ - - /** - * Constructs a JBoolean with the supplied boolean value. - * - * @param b this JBoolean's value (a boolean) - */ - public JBoolean(boolean b) { - this.value = b; - } - - //==================================================================/ - // Methods (common) - //==================================================================/ - - /** - * Tests whether this JBoolean's functor has (String) 'name' and 'arity' - * - * @return whether this JBoolean's functor has (String) 'name' and 'arity' - */ - public final boolean hasFunctor(String name, int arity) { - return name.equals("@") && arity==1; - } - - /** - * Returns a Prolog source text representation of this JBoolean - * - * @return a Prolog source text representation of this JBoolean - */ - public String toString() { - return (value ? "@(true)" : "@(false)"); - } - - /** - * Two JBooleans are equal if their values are equal - * - * @param obj The Object to compare (not necessarily a JBoolean) - * @return true if the Object satisfies the above condition - */ - public final boolean equals(Object obj) { - return this == obj || (obj instanceof JBoolean && value == ((JBoolean) obj).value); - } - - //==================================================================/ - // Methods (peculiar) - //==================================================================/ - - /** - * The boolean value which this jpl.JBoolean object represents - * - * @return the boolean value which this jpl.JBoolean object represents - */ - public boolean boolValue() { - return value; - } - - public final int type() { - return Prolog.JBOOLEAN; - } - - public String typeName(){ - return "JBoolean"; - } - - //==================================================================/ - // Methods (deprecated) - //==================================================================/ - - /** - * The (nonexistent) args of this JBoolean - * - * @return the (nonexistent) args of this JBoolean - * @deprecated - */ - public Term[] args() { - throw new JPLException("jpl.JBoolean.args() is undefined"); - } - - /** - * Returns a debug-friendly representation of this JBoolean - * - * @return a debug-friendly representation of this JBoolean - * @deprecated - */ - public String debugString() { - return "(JBoolean " + toString() + ")"; - } - - //==================================================================/ - // Converting JPL Terms to Prolog terms - //==================================================================/ - - /** - * To convert a JBoolean to a term, we unify the (freshly created, hence unbound) - * term_t with either @(false) or @(true) as appropriate. - * - * @param varnames_to_vars A Map from variable names to Prolog variables. - * @param term A (newly created) term_t which is to be - * set to a Prolog @(false) or @(true) structure denoting the - * .value of this JBoolean instance - */ - protected final void put(Map varnames_to_vars, term_t term) { - Prolog.put_jboolean(term, value); - } - - //==================================================================/ - // Converting Prolog terms to JPL Terms - //==================================================================/ - - /** - * Converts a term_t to a JBoolean. If the Prolog term is either - * @(false) or @(true), we just create a new JBoolean with a corresponding value. - * NB This conversion is only invoked if "JPL-aware" term import is specified. - * - * @param vars_to_Vars A Map from Prolog variable to JPL Variables. - * @param term The term (either @(false) or @(true)) to convert - * @return A new JBoolean instance - */ - protected static Term getTerm(Map vars_to_Vars, term_t term) { - BooleanHolder b = new BooleanHolder(); - - Prolog.get_jboolean(term, b); // assume it succeeds... - return new jpl.JBoolean(b.value); - } - - //==================================================================/ - // Computing Substitutions - //==================================================================/ - - /** - * Nothing needs to be done if the Term denotes an Atom, Integer, Float, JRef or JBoolean - * - * @param varnames_to_Terms A Map from variable names to Terms. - * @param vars_to_Vars A Map from Prolog variables to JPL Variables. - */ - protected final void getSubst(Map varnames_to_Terms, Map vars_to_Vars) { - } - -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/JPL.java b/LGPL/JPL/java/jpl/JPL.java deleted file mode 100644 index 7c2215430..000000000 --- a/LGPL/JPL/java/jpl/JPL.java +++ /dev/null @@ -1,211 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: JPL.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 2004 Paul Singleton -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl; - -import jpl.fli.Prolog; - -//----------------------------------------------------------------------/ -// JPL -/** - * The jpl.JPL class contains methods which allow (i) inspection and alteration - * of the "default" initialisation arguments (ii) explicit initialisation - * (iii) discovery of whether the Prolog engine is already initialised, - * and if so, with what arguments. - * The Prolog engine must be initialized - * before any queries are made, but this will happen automatically - * (upon the first call to a Prolog FLI routine) if it has not already - * been done explicitly. - * - *
- * Copyright (C) 2004 Paul Singleton

- * Copyright (C) 1998 Fred Dushin

- * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

- * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

- *


- * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -public class JPL { - protected static final boolean DEBUG = false; - // - public static final Term JFALSE = new Compound("@", new Term[] {new Atom("false")}); - public static final Term JTRUE = new Compound("@", new Term[] {new Atom("true")}); - public static final Term JNULL = new Compound("@", new Term[] {new Atom("null")}); - public static final Term JVOID = new Compound("@", new Term[] {new Atom("void")}); - - protected static boolean modeDontTellMe = true; - - // setDTMMode - /** - * Sets the global "dont-tell-me" mode (default value: true). - * When 'true', bindings will *not* be returned for any variable (in a Query's goal) - * whose name begins with an underscore character (except for "anonymous" variables, - * i.e. those whose name comprises just the underscore character, whose bindings are never returned). - * When 'false', bindings are returned for *all* variables except anonymous ones; - * this mode may be useful when traditional top-level interpreter behaviour is wanted, - * e.g. in a Java-based Prolog IDE or debugger.

- * This method should be regarded as experimental, and may subsequently be deprecated - * in favour of some more general mechanism for setting options, perhaps per-Query and - * per-call as well as globally. - * - * @param dtm new "dont-tell-me" mode value - */ - public static void setDTMMode( boolean dtm){ - modeDontTellMe = dtm; - } - - // getDefaultInitArgs - /** - * Returns, in an array of String, the sequence of command-line - * arguments that would be used if the Prolog engine were to be initialised now. - * Returns null if the Prolog VM has already been initialised (in which - * case the default init args are irrelevant and the actual init args are of interest)

- * - * @see jpl.JPL#getActualInitArgs - * @return current default initialisation arguments, or null if already initialised - */ - public static String[] getDefaultInitArgs() { - return Prolog.get_default_init_args(); - } - - // setDefaultInitArgs - /** - * Specifies, in an array of String, the sequence of command-line - * arguments that should be used if the Prolog engine is subsequently initialised.

- * - * @param args new default initialization arguments - */ - public static void setDefaultInitArgs(String[] args) { - Prolog.set_default_init_args(args); - } - - // getActualInitArgs - /** - * Returns, in an array of String, the sequence of command-line - * arguments that were actually used when the Prolog engine was formerly initialised. - * - * This method returns null if the Prolog engine has not yet been initialised, - * and thus may be used to test this condition. - * - * @return actual initialization arguments - */ - public static String[] getActualInitArgs() { - return Prolog.get_actual_init_args(); - } - - // init - /** - * Initializes the Prolog engine, using the String argument - * parameters passed. This method need be called only if you want to both - * (i) initialise the Prolog engine with parameters other than the default ones - * and (ii) force initialisation to occur - * (rather than allow it to occur automatically at the first query). - * For parameter options, consult your local - * Prolog documentation. The parameter values are passed directly - * to initialization routines for the Prolog environment.

- * - * This method must be called before making any queries. - * - * @param args Initialization parameter list - */ - public static boolean init(String[] args) { - return Prolog.set_default_init_args(args) && init(); - } - - // init - /** - * Initialises the Prolog engine using the current default initialisation parameters, - * and returns 'true' (or 'false' if already initialised). - */ - public static boolean init() { - return Prolog.initialise(); - } - - // isTag(String) - /** - * whether the String arg is a plausible tag, e.g. "J#0123456789". - */ - public static boolean isTag(String s) { - return s.length()==12 && s.charAt(0)=='J' && s.charAt(1)=='#' && Character.isDigit(s.charAt(2)) && Character.isDigit(s.charAt(3)) && Character.isDigit(s.charAt(4)) && Character.isDigit(s.charAt(5)) && Character.isDigit(s.charAt(6)) && Character.isDigit(s.charAt(7)) && Character.isDigit(s.charAt(8)) && Character.isDigit(s.charAt(9)) && Character.isDigit(s.charAt(10)) && Character.isDigit(s.charAt(11)); - } - - // newJRef(Object) - /** - * returns a new Term instance which represents the given object - */ - public static Term newJRef(Object obj) { - return new Compound( "@", new Term[]{new Atom(Prolog.object_to_tag(obj))}); - } - - // halt - /** - * Terminates the Prolog session.

- * - * Note. This method calls the FLI halt() method with a - * status of 0, but the halt method currently is a no-op in SWI. - * @deprecated - */ - public static void halt() { - Prolog.halt(0); - } - - // a static reference to the current Version - private static final Version version_ = new Version(); - - // version - /** - * Returns (as a Version) an identification of this version of JPL. - * @return the running version of JPL. - */ - public static Version version() { - return version_; - } - - // version_string - /** - * Returns a String (eg "3.0.0-alpha") identifying this version of JPL. - * @return a String (eg "3.0.0-alpha") identifying this version of JPL. - */ - public static String version_string() { - return version_.major + "." + version_.minor + "." + version_.patch + "-" + version_.status; - } - - public static void main(String[] args) { - System.out.println(version_string()); - } -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/JPLException.java b/LGPL/JPL/java/jpl/JPLException.java deleted file mode 100644 index f5588634e..000000000 --- a/LGPL/JPL/java/jpl/JPLException.java +++ /dev/null @@ -1,63 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: JPLException.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 2004 Paul Singleton -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl; - -//----------------------------------------------------------------------/ -// JPLException -/** - * This is the base class for exceptions thrown by JPL's Java-calls-Prolog interface. - * Such exceptions represent errors and exceptional conditions within the interface code itself; - * see jpl.PrologException for the way Prolog exceptions are returned to calling Java code. - *


- * Copyright (C) 2004 Paul Singleton

- * Copyright (C) 1998 Fred Dushin

- * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

- * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

- *


- * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -public class JPLException extends RuntimeException { - private static final long serialVersionUID = 1L; - - public JPLException() { - super(); - } - - public JPLException(String s) { - super(s); - } -} diff --git a/LGPL/JPL/java/jpl/JRef.java b/LGPL/JPL/java/jpl/JRef.java deleted file mode 100644 index 98a575ae7..000000000 --- a/LGPL/JPL/java/jpl/JRef.java +++ /dev/null @@ -1,195 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: JRef.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 2004 Paul Singleton -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl; - -import java.util.Map; -import jpl.fli.Prolog; -import jpl.fli.term_t; - -//----------------------------------------------------------------------/ -// JRef -/** - * JRef is a specialised Term with an Object field, representing JPL's Prolog references to Java objects (or to null). - *
- * JRef r = new JRef( non_String_object_or_null );
- * 
- * A JRef can be used (and re-used) in Compound Terms. - * - *
- * Copyright (C) 2004 Paul Singleton

- * Copyright (C) 1998 Fred Dushin

- * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

- * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

- *


- * @author Fred Dushin - * @version $Revision: 1.2 $ - * @see jpl.Term - * @see jpl.Compound - * - * @deprecated - */ -public class JRef extends Term { - - //==================================================================/ - // Attributes - //==================================================================/ - - /** - * the JRef's value (a non-String Object or null) - */ - protected final Object ref; - - //==================================================================/ - // Constructors - //==================================================================/ - - /** - * This constructor creates a JRef, initialized with the supplied - * non-String object (or null). - * - * @param ref this JRef's value (a non-String object, or null) - */ - public JRef(Object ref) { - if (ref instanceof String) { - throw new JPLException("a JRef cannot have a String value (String maps to atom)"); - } else { - this.ref = ref; - } - } - - //==================================================================/ - // Methods (common) - //==================================================================/ - - /** - * Returns a Prolog source text representation of this JRef - * - * @return a Prolog source text representation of this JRef - */ - public String toString() { - return "" + ref + ""; // WRONG - } - - /** - * Two JRefs are equal if their references are identical (?) - * - * @param obj The Object to compare - * @return true if the Object satisfies the above condition - */ - public final boolean equals(Object obj) { - return this == obj || (obj instanceof JRef && ref == ((JRef) obj).ref); - } - - public final int type() { - return Prolog.JREF; - } - - public String typeName(){ - return "JRef"; - } - - //==================================================================/ - // Methods (peculiar) - //==================================================================/ - - /** - * The non-String object (or null) which this jpl.JRef represents - * - * @return the non-String object (or null) which this jpl.JRef represents - */ - public Object ref() { - return ref; - } - - //==================================================================/ - // Methods (deprecated) - //==================================================================/ - - /** - * The (nonexistent) args of this JRef - * - * @return the (nonexistent) args of this JRef - * @deprecated - */ - public Term[] args() { - return new Term[] { - }; - } - - /** - * Returns a debug-friendly representation of this JRef - * - * @return a debug-friendly representation of this JRef - * @deprecated - */ - public String debugString() { - return "(JRef " + toString() + ")"; - } - - //==================================================================/ - // Converting JPL Terms to Prolog terms - //==================================================================/ - - /** - * To convert a JRef to a term, we put its Object field (.value) into the - * term_t as a JPL ref (i.e. @/1) structure. - * - * @param varnames_to_vars A Map from variable names to Prolog variables. - * @param term A (newly created) term_t which is to be - * set to a Prolog 'ref' (i.e. @/1) structure denoting the - * .value of this JRef instance - */ - protected final void put(Map varnames_to_vars, term_t term) { - - Prolog.put_jref(term, ref); - } - - //==================================================================/ - // Computing Substitutions - //==================================================================/ - - /** - * Nothing needs to be done if the Term is an Atom, Integer, Float or JRef - * - * @param varnames_to_Terms A Map from variable names to Terms. - * @param vars_to_Vars A Map from Prolog variables to JPL Variables. - */ - protected final void getSubst(Map varnames_to_Terms, Map vars_to_Vars) { - } - -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 \ No newline at end of file diff --git a/LGPL/JPL/java/jpl/JVoid.java b/LGPL/JPL/java/jpl/JVoid.java deleted file mode 100644 index 5ada281ce..000000000 --- a/LGPL/JPL/java/jpl/JVoid.java +++ /dev/null @@ -1,185 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: JVoid.java,v 1.1 2004-08-27 20:27:56 vsc Exp $ -// Date: $Date: 2004-08-27 20:27:56 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 2004 Paul Singleton -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl; - -import java.util.Map; - -import jpl.fli.Prolog; -import jpl.fli.term_t; - -//----------------------------------------------------------------------/ -// JVoid -/** - * A jpl.JVoid is a specialised Term. Instances of this class - * denote JPL 'jvoid' values in Prolog, i.e. @(void): - *
- * JVoid b = new JVoid();
- * 
- * A JVoid can be used (and re-used) in Compound Terms. - * - *
- * Copyright (C) 2004 Paul Singleton

- * Copyright (C) 1998 Fred Dushin

- * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

- * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

- *


- * @author Fred Dushin - * @version $Revision: 1.1 $ - * @see jpl.Term - * @see jpl.Compound - */ -public class JVoid extends Term { - - //==================================================================/ - // Attributes (none) - //==================================================================/ - - //==================================================================/ - // Constructors - //==================================================================/ - - /** - * This constructor creates a JVoid. - * - */ - public JVoid() { - } - - //==================================================================/ - // Methods (common) - //==================================================================/ - - /** - * Returns a Prolog source text representation of this JVoid - * - * @return a Prolog source text representation of this JVoid - */ - public String toString() { - return "@(void)"; - } - - /** - * Two JVoids are equal - * - * @param obj The Object to compare (not necessarily another JVoid) - * @return true if the Object satisfies the above condition - */ - public final boolean equals(Object obj) { - return this == obj || (obj instanceof JVoid); - } - - public final int type() { - return Prolog.JVOID; - } - - public String typeName(){ - return "JVoid"; - } - - //==================================================================/ - // Methods (deprecated) - //==================================================================/ - - /** - * The (nonexistent) args of this JVoid - * - * @return the (nonexistent) args of this JVoid - * @deprecated - */ - public Term[] args() { - return new Term[] {}; - } - - /** - * Returns a debug-friendly representation of this JVoid - * - * @return a debug-friendly representation of this JVoid - * @deprecated - */ - public String debugString() { - return "(JVoid)"; - } - - //==================================================================/ - // Converting JPL Terms to Prolog terms - //==================================================================/ - - /** - * To convert a JVoid to a term, we unify the (freshly created, hence unbound) - * term_t with @(void). - * - * @param varnames_to_vars A Map from variable names to Prolog variables. - * @param term A (previously created and unbound) term_t which is to be - * assigned a Prolog @(void) structure - */ - protected final void put(Map varnames_to_vars, term_t term) { - Prolog.put_jvoid(term); - } - - //==================================================================/ - // Converting Prolog terms to JPL Terms - //==================================================================/ - - /** - * Converts a term_t to a JVoid. Assuming the Prolog term to be - * @(void), we just create a new JVoid instance. - * NB This conversion is only invoked if "JPL-aware" term import is specified. - * - * @param vars_to_Vars A Map from Prolog variables to JPL Variables. - * @param term The term_t to convert - * @return A new JVoid instance - */ - protected static Term getTerm(Map vars_to_Vars, term_t term) { - return new jpl.JVoid(); - } - - //==================================================================/ - // Computing Substitutions - //==================================================================/ - - /** - * Nothing needs to be done if the Term denotes an Atom, Integer, Float, JRef, JBoolean or JVoid - * - * @param varnames_to_Terms A Map from variable names to Terms. - * @param vars_to_Vars A Map from Prolog variables to JPL Variables. - * Variables. - */ - protected final void getSubst(Map varnames_to_Terms, Map vars_to_Vars) { - } - -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/PrologException.java b/LGPL/JPL/java/jpl/PrologException.java deleted file mode 100644 index 5f66a798c..000000000 --- a/LGPL/JPL/java/jpl/PrologException.java +++ /dev/null @@ -1,75 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: PrologException.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 2004 Paul Singleton -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl; - -//----------------------------------------------------------------------/ -// PrologException -/** - * PrologException instances wrap Prolog exceptions thrown (either by a Prolog engine or by user code) - * in the course of finding a solution to a Query. See JPLException for the handling of errors within the JPL Java-calls-Prolog interface. - *

- * This class allows Java code which uses JPL's Java-calls-Prolog API to handle - * Prolog exceptions, which is in general necessary for hybrid Java+Prolog programming. - *

- * Use the term() accessor to obtain a Term representation of the term that was - * thrown from within Prolog. - * - *


- * Copyright (C) 2004 Paul Singleton

- * Copyright (C) 1998 Fred Dushin

- * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

- * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

- *


- * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -public final class PrologException extends JPLException { - private static final long serialVersionUID = 1L; - private Term term_ = null; - - protected PrologException(Term term) { - super("PrologException: " + term.toString()); - - this.term_ = term; - } - - /** - * @return a reference to the Term thrown by the call to throw/1 - */ - public Term term() { - return this.term_; - } -} diff --git a/LGPL/JPL/java/jpl/Query.java b/LGPL/JPL/java/jpl/Query.java deleted file mode 100644 index 2f4907b85..000000000 --- a/LGPL/JPL/java/jpl/Query.java +++ /dev/null @@ -1,869 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: Query.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 2004 Paul Singleton -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl; - -import java.util.Enumeration; -import java.util.Hashtable; -import java.util.Map; -import java.util.Vector; -import jpl.fli.*; - -//----------------------------------------------------------------------/ -// Query -/** - * A Query instance is created by an application in order to query the Prolog database - * (or to invoke a built-in predicate). - * It is initialised with a - * Compound (or Atom) denoting the goal which is to be called, and also contains assorted private state - * relating to solutions. In some future version, it may contain details of the module - * in which the goal is to be called.

- * A Query is either open or closed: when closed, it has no connection to the Prolog system; - * when open, it is linked to an active goal within a Prolog engine.

- * The Query class implements the Enumeration interface, - * through which one can obtain successive solutions. The Enumeration - * hasMoreElements() method returns true if the call or redo succeeded (otherwise - * false), and if the call or redo did succeed, the nextElement() method returns - * a Hashtable representing variable bindings; the elements in the - * Hashtable are Terms, indexed by the (String) names of the Variables with which they are associated. - * For example, if p(a) and p(b) are facts in the Prolog - * database, then the following is equivalent to printing all - * the solutions to the Prolog query p(X): - *

- * Variable X = new Variable("X");
- * Term arg[] = { X };
- * Query    q = new Query("p", arg);
- * 
- * while (q.hasMoreElements()){
- *     Term bound_to_x = ((Hashtable) q.nextElement()).get("X");
- *     System.out.println(bound_to_x);
- * }
- * 
- * Make sure to close the Query (using the close() method) if you do not need - * any further solutions which it may have. - * It is safe (although redundant) to close a Query whose solutions are already exhausted, - * or which is already closed. - * - * To obtain just one solution from a Query, use the oneSolution() method. - * - * To obtain all solutions, use the allSolutions() method. - * - * To obtain at most N solutions, use the nSolutions() method. - * - * To determine merely whether the Query is provable, - * use the hasSolution() method - * (i.e. has at least one solution). - *
- * - * Copyright (C) 2007 Paul Singleton

- * Copyright (C) 1998 Fred Dushin - *

- * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - *

- * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

- * - *


- * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -// Implementation notes: -// -//----------------------------------------------------------------------/ -public class Query implements Enumeration { - //==================================================================/ - // Attributes - //==================================================================/ - private static Map m = new Hashtable(); // maps (engine_t) engine handle to (Query) topmost query - /** - * the Compound (hence perhaps an Atom, but not Integer, Float or Variable) corresponding to the goal of this Query - */ - protected final Compound goal_; // set by all initialisers - protected final String hostModule = "user"; // until revised constructors allow this to be specified - protected final String contextModule = "user"; // until revised constructors allow this to be specified - /** - * @deprecated Use .goal().name() instead. - * @return the name of this Query's goal (redundant, deprecated) - */ - public final String name() { - return goal_.name(); // it can only be a Compound or Atom - } - /** - * @deprecated Use .goal().args() instead. - * @return the arguments of this Query's goal (redundant, deprecated) - */ - public final Term[] args() { - return goal_.args(); - } - /** - * Returns the Compound (hence perhaps an Atom) which is the goal of this Query - * @return a Term representing the goal of this Query - */ - public final Compound goal() { - return goal_; - } - //==================================================================/ - // Constructors and Initialization - //==================================================================/ - //------------------------------------------------------------------/ - // Query - /** - * This constructor creates a Query whose goal is the specified Term. - * The Query is initially closed. - * NB Creating an instance of the Query class does not - * result in a call to a Prolog engine. - * NB The goal can be a Compound or an Atom (Atom extends Compound), but cannot be an instance - * of jpl.Float, jpl.Integer or jpl.Variable. - * @param t the goal of this Query - */ - public Query(Term t) { // formerly insisted (confusingly) on a Compound (or Atom) - this.goal_ = Query1(t); - } - private Compound Query1(Term t) { - if (t instanceof Compound) { - return (Compound) t; - } else if (t instanceof Integer) { - throw new JPLException("a Query's goal must be an Atom or Compound (not an Integer)"); - } else if (t instanceof Float) { - throw new JPLException("a Query's goal must be an Atom or Compound (not a Float)"); - } else if (t instanceof Variable) { - throw new JPLException("a Query's goal must be an Atom or Compound (not a Variable)"); - } else { - throw new JPLException("a Query's goal must be an Atom or Compound"); - } - } - // Query - /** - * If text denotes an atom, this constructor is shorthand for - * new Query(new Compound(name,args)), - * but if text denotes a term containing N query (?) symbols - * and there are N args, each query is replaced by its corresponding arg - * to provide the new Query's goal. - * - * @param text the name of the principal functor of this Query's goal - * @param args the arguments of this Query's goal - */ - public Query(String text, Term[] args) { - this(Query1(text, args)); - } - // convenience case for a single arg - public Query(String text, Term arg) { - this(Query1(text, new Term[] { arg })); - } - private static Term Query1(String text, Term[] args) { - Term t = Util.textToTerm(text); - if (t instanceof Atom) { - return new Compound(text, args); - } else { - return t.putParams(args); - } - } - // Query - /** - * This constructor builds a Query from the given Prolog source text. - * Throws PrologException containing error(syntax_error(_),_) if text is invalid. - * - * @param text the Prolog source text of this Query - */ - public Query(String text) { - this(Util.textToTerm(text)); - } - //==================================================================/ - // Making Prolog Queries - //==================================================================/ - /** - * These variables are used and set across the hasMoreElements - * and nextElement Enumeration interface implementation - */ - private boolean open = false; - // the following state variables are used and defined only if this query is open: - // private boolean called = false; // open/get/close vs. hasMoreSolutions/nextSolution - private engine_t engine = null; // handle of attached Prolog engine iff open, else null - private Query subQuery = null; // the open Query (if any) on top of which this open Query is stacked, else null - private predicate_t predicate = null; // handle of this Query's predicate iff open, else undefined - // private fid_t fid = null; // id of current Prolog foreign frame iff open, else null - // private fid_t fid2 = null; // id of experimental inner frame - private term_t term0 = null; // term refs of this Query's args iff open, else undefined - private qid_t qid = null; // id of current Prolog query iff open, else null - //------------------------------------------------------------------/ - // hasMoreSolutions - /** - * This method returns true if JPL was able to initiate a "call" of this - * Query within a Prolog engine. It is designed to be used - * with the nextSolution() method to retrieve one or - * more substitutions in the form of Hashtables. To iterate through - * all the solutions to a Query, for example, one might write - *
-	 * Query q = // obtain Query reference
-	 * while (q.hasMoreSolutions()) {
-	 *     Hashtable solution = q.nextSolution();
-	 *     // process solution...
-	 * }
-	 * 
- * To ensure thread-safety, you should wrap sequential calls to - * this method in a synchronized block, using the static - * lock method to obtain the monitor. - *
-	 * Query q = // obtain Query reference
-	 * synchronized ( jpl.Query.lock() ){
-	 *     while ( q.hasMoreElements() ){
-	 *          Hashtable solution = q.nextSolution();
-	 *          // process solution...
-	 *     }
-	 * }
-	 * 
- * - * @return true if the Prolog query succeeds; otherwise false. - */ - public synchronized final boolean hasMoreSolutions() { - if (!open) { - open(); - } - return get1(); - } - //------------------------------------------------------------------/ - // open - /** - * This method returns true if JPL was able to initiate a "call" of this - * Query within the Prolog engine. It is designed to be used - * with the getSolution() and close() methods to retrieve one or - * more substitutions in the form of Hashtables. - * To ensure thread-safety, you should wrap sequential calls to - * this method in a synchronized block, using the static - * lock method to obtain the monitor. - *
-	 * Query q = // obtain Query reference
-	 * synchronized ( jpl.Query.lock() ){
-	 *     while ( q.hasMoreElements() ){
-	 *          Hashtable solution = q.nextSolution();
-	 *          // process solution...
-	 *     }
-	 * }
-	 * 
- *

- * If this method is called on an already-open Query, - * or if the query cannot be set up for whatever reason, - * then a JPLException will be thrown. - */ - public synchronized final void open() { - if (open) { - throw new JPLException("Query is already open"); - } - // int self = Prolog.thread_self(); - // System.out.println("JPL thread_self()=" + self); - if (Prolog.thread_self() == -1) { // this Java thread has no attached Prolog engine? - engine = Prolog.attach_pool_engine(); // may block for a while, or fail - // System.out.println("JPL attaching engine[" + engine.value + "] for " + this.hashCode() + ":" + this.toString()); - } else { // this Java thread has an attached engine - engine = Prolog.current_engine(); - // System.out.println("JPL reusing engine[" + engine.value + "] for " + this.hashCode() + ":" + this.toString()); - } - if (m.containsKey(new Long(engine.value))) { - subQuery = (Query) m.get(new Long(engine.value)); // get this engine's previous topmost query - // System.out.println("JPL reusing engine[" + engine.value + "] pushing " + subQuery.hashCode() + ":" + subQuery.toString()); - } else { - subQuery = null; - } - m.put(new Long(engine.value), this); // update this engine's topmost query - // - // here, we must check for a module prefis, e.g. jpl:jpl_modifier_bit(volatile,T) - String module; - Term goal; - if (goal_.hasFunctor(":", 2)) { - if (goal_.arg(1).isAtom()) { - module = goal_.arg(1).name(); - } else if (goal_.arg(1).isVariable()) { - throw new PrologException(Util.textParamsToTerm("error(instantiation_error,?)", new Term[] { goal_ })); - } else { - throw new PrologException(Util.textParamsToTerm("error(type_error(atom,?),?)", new Term[] { goal_.arg(1), goal_ })); - } - goal = goal_.arg(2); - } else { - module = contextModule; - goal = goal_; - } - predicate = Prolog.predicate(goal.name(), goal.arity(), module); // was hostModule - // fid = Prolog.open_foreign_frame(); // always succeeds? - Map varnames_to_vars = new Hashtable(); - term0 = Term.putTerms(varnames_to_vars, goal.args()); - // THINKS: invert varnames_to_Vars and use it when getting substitutions? - qid = Prolog.open_query(Prolog.new_module(Prolog.new_atom(contextModule)), Prolog.Q_CATCH_EXCEPTION, predicate, term0); - open = true; - // called = false; - } - private final boolean get1() { // try to get the next solution; if none, close the query; - // if (fid2 != null) { // PS 23/Mar/2007 ensure inner frame is closed - // Prolog.close_foreign_frame(fid2); - // fid2 = null; - // } - if (Prolog.next_solution(qid)) { - // fid2 = Prolog.open_foreign_frame(); // PS 23/Mar/2007 open an inner frame - // called = true; // OK to call get2() - return true; - } else { - // if failure was due to throw/1, build exception term and throw it - term_t exception_term_t = Prolog.exception(qid); - if (exception_term_t.value != 0L) { - Term exception_term = Term.getTerm(new Hashtable(), exception_term_t); - close(); - throw new PrologException(exception_term); - } else { - close(); - return false; - } - } - } - //------------------------------------------------------------------/ - // getSolution - /** - * This method returns a java.util.Hashtable, which represents - * a set of bindings from the names of query variables to terms within the solution. - *

- * For example, if a Query has an occurrence of a jpl.Variable, - * say, named "X", one can obtain the Term bound to "X" in the solution - * by looking up "X" in the Hashtable. - *

-	 * Variable x = new Variable("X");
-	 * Query q = // obtain Query reference (with x in the Term array)
-	 * while (q.hasMoreSolutions()) {
-	 *     Hashtable solution = q.nextSolution();
-	 *     // make t the Term bound to "X" in the solution
-	 *     Term t = (Term) solution.get("X");
-	 *     // ...
-	 * }
-	 * 
- * Programmers should obey the following rules when using this method. - * - *
  • The nextSolution() method should only be called after the - * hasMoreSolutions() method returns true; otherwise a JPLException - * will be raised, indicating that the Query is no longer open. - *
  • The nextSolution() and hasMoreSolutions() should be called - * in the same thread of execution, for a given Query - * instance. - *
  • - * - * This method will throw a JPLException if Query is not open. - * - * @return A Hashtable representing a substitution, or null - */ - public synchronized final Hashtable getSolution() { - // oughta check: Query is open and thread has its engine - if (get1()) { - return get2(); - } else { - return null; - } - } - public synchronized final Hashtable getSubstWithNameVars() { - // oughta check: Query is open and thread has its engine - if (get1()) { - return get2WithNameVars(); - } else { - return null; - } - } - //------------------------------------------------------------------/ - // nextSolution - /** - * This method returns a java.util.Hashtable, which represents - * a binding from the names of query variables to terms within the solution. - *

    - * For example, if a Query has an occurrence of a jpl.Variable, - * say, named "X", one can obtain the Term bound to "X" in the solution - * by looking up "X" in the Hashtable. - *

    -	 * Variable x = new Variable("X");
    -	 * Query q = // obtain Query reference (with x in the Term array)
    -	 * while (q.hasMoreSolutions()) {
    -	 *     Hashtable solution = q.nextSolution();
    -	 *     // make t the Term bound to "X" in the solution
    -	 *     Term t = (Term) solution.get("X");
    -	 *     // ...
    -	 * }
    -	 * 
    - * Programmers should obey the following rules when using this method. - * - *
  • The nextSolution() method should only be called after the - * hasMoreSolutions() method returns true; otherwise a JPLException - * will be raised, indicating that the Query is no longer open. - *
  • The nextSolution() and hasMoreSolutions() should be called - * in the same thread of execution, for a given Query - * instance. - *
  • - * - * This method will throw a JPLException if Query is not open. - * - * @return A Hashtable representing a substitution. - */ - public synchronized final Hashtable nextSolution() { - return get2(); - } - private final Hashtable get2() { - if (!open) { - throw new JPLException("Query is not open"); - } else { - Hashtable substitution = new Hashtable(); - // NB I reckon computeSubstitutions needn't be in Term (but where else?) - Term.getSubsts(substitution, new Hashtable(), goal_.args); - return substitution; - } - } - // assumes that Query's last arg is a Variable which will be bound to a [Name=Var,..] dict - private final Hashtable get2WithNameVars() { - if (!open) { - throw new JPLException("Query is not open"); - } else { - Term[] args = goal_.args; // for slight convenience below - Term argNV = args[args.length - 1]; // the Query's last arg - String nameNV = ((Variable) argNV).name; // its name - // get the [Name=Var,..] dict from the last arg - Map varnames_to_Terms1 = new Hashtable(); - Map vars_to_Vars1 = new Hashtable(); - args[args.length - 1].getSubst(varnames_to_Terms1, vars_to_Vars1); - Hashtable varnames_to_Terms2 = new Hashtable(); - Term nvs = (Term) varnames_to_Terms1.get(nameNV); - Map vars_to_Vars2 = Util.namevarsToMap(nvs); - for (int i = 0; i < args.length - 1; ++i) { - args[i].getSubst(varnames_to_Terms2, vars_to_Vars2); - } - return varnames_to_Terms2; - } - } - //------------------------------------------------------------------/ - // hasMoreElements - /** - * This method implements part of the java.util.Enumeration - * interface. It is a wrapper for hasMoreSolutions. - * - * @return true if the Prolog query yields a (or another) solution, else false. - */ - public synchronized final boolean hasMoreElements() { - return hasMoreSolutions(); - } - //------------------------------------------------------------------/ - // nextElement - /** - * This method implements part of the java.util.Enumeration - * interface. It is a wrapper for nextSolution. - *

    - * - * @return A Hashtable representing a substitution. - */ - public synchronized final Object nextElement() { - return nextSolution(); - } - public synchronized final void rewind() { - close(); - } - /** - * This method can be used to close an open query before its solutions are exhausted. - * It is called automatically when solutions are exhausted, i.e. when hasMoreSolutions() fails. - * Calling close() on an already closed Query is harmless (has no effect).

    - * - * Here is one way to get the first three solutions to a Query: - *

    -	 * Query q = new Query(predicate, args);
    -	 * Hashtable sub1 = (Hashtable) q.nextSolution();
    -	 * Hashtable sub2 = (Hashtable) q.nextSolution();
    -	 * Hashtable sub3 = (Hashtable) q.nextSolution();
    -	 * q.close();
    -	 * 

    - */ - public synchronized final void close() { - if (!open) { - return; // it is not an error to attempt to close a closed Query - } - 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"); - } - Query topmost = (Query) m.get(new Long(engine.value)); - if (topmost != this) { - 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); - qid = null; // for tidiness - m.remove(new Long(engine.value)); - if (subQuery == 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 { - m.put(new Long(engine.value), subQuery); - // System.out.println("JPL retaining engine[" + engine.value + "] popping subQuery(" + subQuery.hashCode() + ":" + subQuery.toString() + ")"); - } - open = false; // this Query is now closed - engine = null; // this Query, being closed, is no longer associated with any Prolog engine - subQuery = null; // this Query, being closed, is not stacked upon any other Query - } - /** - * calls the Query's goal to exhaustion - * and returns an array of zero or more Hashtables of zero or more variablename-to-term bindings (each Hashtable represents a solution, in the order in which they were found). - * @return an array of zero or more Hashtables of zero or more variablename-to-term bindings (each Hashtable represents a solution, in the order in which they were found) - * NB in JPL 1.0.1, this method (inconsistently) returned null when a Query had no solutions; - * in JPL 2.x onwards it returns an empty array (thus the length of the array is, in every case, - * the quantity of solutions).

    - * NB in JPL 1.0.1, bindings were keyed (awkwardly) by Variable instances; - * in JPL 2.x onwards they are keyed by the (String) names of variables, - * which is consistent with the Term type being just a concrete syntax for terms (and hence queries).

    - */ - public synchronized final Hashtable[] allSolutions() { - if (open) { - throw new JPLException("Query is already open"); - } else { - // get a vector of solutions: - Vector v = new Vector(); - while (hasMoreSolutions()) { - v.addElement(nextSolution()); - } - // turn the vector into an array: - Hashtable solutions[] = new Hashtable[v.size()]; // 0 solutions -> Hashtable[0] - v.copyInto(solutions); - return solutions; - } - } - /** - * This static method creates a Query whose goal is the given Term, - * calls it to exhaustion, - * and returns an array of zero or more Hashtables of zero or more variablename-to-term bindings (each Hashtable represents a solution, in the order in which they were found). - * Throws JPLException if goal is neither a jpl.Atom nor a jpl.Compound. - * - * @return an array of zero or more Hashtables of zero or more variablename-to-term bindings (each Hashtable represents a solution, in the order in which they were found) - * - * @param goal the goal of this Query - */ - public static final Hashtable[] allSolutions(Term goal) { - return (new Query(goal)).allSolutions(); - } - /** - * This static method creates a Query from the given Prolog source text fragment, - * calls it to exhaustion, - * and returns an array of zero or more Hashtables of zero or more variablename-to-term bindings (each Hashtable represents a solution, in the order in which they were found). - * Throws PrologException containing error(syntax_error(_),_) if text is invalid. - * - * @return an array of zero or more Hashtables of zero or more variablename-to-term bindings (each Hashtable represents a solution, in the order in which they were found) - * - * @param text a Prolog source text fragment denoting a goal - */ - public static final Hashtable[] allSolutions(String text) { - return (new Query(text)).allSolutions(); - } - /** - * If text denotes (in traditional Prolog source syntax) a term containing N questionmark (?) symbols and there are N accompanying Term params, - * this static method replaces each questionmark symbol by its respective param, - * calls the resulting goal to exhaustion, - * and returns an array of zero or more Hashtables of zero or more variablename-to-term bindings (each Hashtable represents a solution, in the order in which they were found). - * - * Otherwise, if text denotes an atom, this static method creates a Query - * where text is the name of the goal and params are the args; - * the resulting goal is then called as above. - * This letter mode is redundant, deprecated (informally), and retained only for backward compatibility. - * - * @return an array of zero or more Hashtables of zero or more variablename-to-term bindings (each Hashtable represents a solution, in the order in which they were found) - * - * @param text the Prolog source text of a goal, in which questionmarks are regarded as substitutible parameters - * @param params terms to be substituted for the respective questionmarks in the query text - */ - public static final Hashtable[] allSolutions(String text, Term[] params) { - return (new Query(text, params)).allSolutions(); - } - /** - * calls the Query's goal to exhaustion or until N solutions are found, whichever is sooner, - * and returns an array containing (as possibly empty Hashtables of variablename-to-term bindings) every found solution (in the order in which they were found). - * @return an array of Hashtables (possibly none), each of which is a solution - * (in the order in which they were found) of the Query; at most 'n' solutions will be found and returned. - * NB in JPL 1.0.1, this method (inconsistently) returned null when a Query had no solutions; - * in JPL 2.x onwards it returns an empty array (thus the length of the array is, in every case, - * the quantity of solutions).

    - * NB in JPL 1.0.1, bindings were keyed (awkwardly) by Variable instances; - * in JPL 2.x onwards they are keyed by the (String) names of variables, - * which is consistent with the Term type being just a concrete syntax for terms (and hence queries).

    - */ - public synchronized final Hashtable[] nSolutions(long n) { - if (open) { - throw new JPLException("Query is already open"); - } else { - // get a vector of solutions: - Vector v = new Vector(); - for (long i = 0; i++ < n && hasMoreSolutions();) { - v.addElement(nextSolution()); - } - // turn the vector into an array: - Hashtable solutions[] = new Hashtable[v.size()]; // 0 solutions -> Hashtable[0] - v.copyInto(solutions); - return solutions; - } - } - /** - * This static method creates a Query whose goal is the given Term, - * calls it to exhaustion or until N solutions are found, whichever is sooner, - * and returns an array containing (as possibly empty Hashtables of variablename-to-term bindings) every found solution (in the order in which they were found). - * Throws JPLException if goal is neither a jpl.Atom nor a jpl.Compound. - * - * @param goal the goal of this Query - */ - public static final Hashtable[] nSolutions(Term goal, long n) { - return (new Query(goal)).nSolutions(n); - } - /** - * This static method creates a Query from the given Prolog source text fragment, - * calls it to exhaustion or until N solutions are found, whichever is sooner, - * and returns an array containing (as possibly empty Hashtables of variablename-to-term bindings) every found solution (in the order in which they were found). - * Throws PrologException containing error(syntax_error(_),_) if text is invalid. - * - * @param text a Prolog source text fragment denoting a goal - */ - public static final Hashtable[] nSolutions(String text, long n) { - return (new Query(text)).nSolutions(n); - } - /** - * If text denotes (in traditional Prolog source syntax) a term containing N questionmark (?) symbols and there are N accompanying params, - * this static method replaces each questionmark symbol by its respective param, - * calls the resulting goal to exhaustion or until N solutions are found, whichever is sooner, - * and returns an array containing (as possibly empty Hashtables of variablename-to-term bindings) every found solution (in the order in which they were found). - * - * Otherwise, if text denotes an atom, this static method creates a Query - * where text is the name of the goal and params are the args; - * the resulting goal is then called as above. - * This latter mode is redundant, deprecated (informally), and retained only for backward compatibility. - * - * @param text the Prolog source text of a goal, in which questionmarks are regarded as substitutible parameters - * @param params terms to be substituted for the respective questionmarks in the query text - */ - public static final Hashtable[] nSolutions(String text, Term[] params, long n) { - return (new Query(text, params)).nSolutions(n); - } - /** - * Returns the first solution, if any, as a (possibly empty) Hashtable of variablename-to-term bindings, else null. - * - * This method will throw a JPLException if this Query is already open (and the Query will remain open as before). - * Otherwise, upon return, the Query will be closed. - * @return the first solution, if the query has one, as a (possibly empty) Hashtable. - * If the return value is null, this means that the Query has no solutions.

    - */ - public synchronized final Hashtable oneSolution() { - if (open) { - throw new JPLException("Query is already open"); - } else { - Hashtable solution; - if (hasMoreSolutions()) { - solution = nextSolution(); - close(); // safe, whether or not this is the only solution - } else { - solution = null; - } - return solution; - } - } - /** - * This static method creates a Query (whose goal is the specified Term) - * and calls it at most once, returning the first solution, if there is one, as a (possibly empty) Hashtable, else null. - * The goal can be a jpl.Atom or a jpl.Compound, but cannot be an instance - * of jpl.Float, jpl.Integer or jpl.Variable. - * - * @param goal the goal of this Query - */ - public static final Hashtable oneSolution(Term goal) { - return (new Query(goal)).oneSolution(); - } - /** - * This static method creates a Query from the given Prolog source text fragment, - * and calls it at most once, returning the first solution, if there is one, as a (possibly empty) Hashtable, else null. - * Throws PrologException containing error(syntax_error(_),_) if text is invalid. - * - * @param text a Prolog source text fragment denoting a goal - */ - public static final Hashtable oneSolution(String text) { - return (new Query(text)).oneSolution(); - } - /** - * If text denotes (in traditional Prolog source syntax) a term containing N questionmark (?) symbols - * and there are N params, each questionmark symbol is replaced by its respective param - * to provide the goal of this query: - * the resulting goal is then called (at most once) and the first solution, if there is one, is returned as a (possibly empty) Hashtable, else null. - * - * Otherwise, if text denotes an atom, this static method creates a Query - * where text is the name of the goal and params are the args; - * the resulting goal is then called as above. - * This latter mode is redundant, deprecated (informally), and retained only for backward compatibility. - * - * @param text the Prolog source text of a goal, in which questionmarks are regarded as substitutible parameters - * @param params terms to be substituted for the respective questionmarks in the query text - */ - public static final Hashtable oneSolution(String text, Term[] params) { - return (new Query(text, params)).oneSolution(); - } - /** - * This method will attempt to call this Query's goal within an available Prolog engine. - * @return the provability of the Query, i.e. 'true' if it has at least - * one solution, 'false' if the call fails without finding a solution.

    - * - * Only the first solution (if there is one) will be found; - * any bindings will be discarded, and the Query will be closed.

    - * This method will throw a JPLException if this Query is already open. - * - * @deprecated Use .hasSolution() instead. - */ - public synchronized final boolean query() { - return oneSolution() != null; - } - /** - * This method will attempt to call this Query's goal within an available Prolog engine. - * @return the provability of the Query, i.e. 'true' if it has at least - * one solution, 'false' if the call fails without finding a solution.

    - * - * Only the first solution (if there is one) will be found; - * any bindings will be discarded, and the Query will be closed.

    - * This method will throw a JPLException if this Query is already open. - */ - public synchronized final boolean hasSolution() { - return oneSolution() != null; - } - /** - * This static method creates a Query (whose goal is the specified Term) - * and calls it at most once, returning true if a solution was found, else false. - * The goal can be a jpl.Atom or a jpl.Compound, but cannot be an instance - * of jpl.Float, jpl.Integer or jpl.Variable. - * - * @param goal the goal of this Query - */ - public static final boolean hasSolution(Term goal) { - return (new Query(goal)).hasSolution(); - } - /** - * This static method creates a Query from the given Prolog source text - * and calls it at most once, returning true if a solution was found, else false. - * Throws PrologException containing error(syntax_error(_),_) if text is invalid. - * - * @param text the goal of this Query, as Prolog source text - */ - public static final boolean hasSolution(String text) { - return (new Query(text)).hasSolution(); - } - /** - * If text denotes (in traditional Prolog source syntax) a term containing N questionmark (?) symbols - * and there are N params, each questionmark symbol is replaced by its corresponding arg - * to provide the new Query's goal: the resulting Query is called as described above. - * - * Otherwise, if text denotes an atom, this static method creates a Query - * where text is the name of its goal and args are its args; - * it then calls this goal (at most once) and returns true if a solution was found, else false. - * This latter mode is redundant, deprecated (informally), and retained only for backward compatibility. - * - * @param text the Prolog source text of a goal, in which questionmarks are regarded as substitutible parameters - * @param params terms to be substituted for the respective questionmarks in the query text - */ - public static final boolean hasSolution(String text, Term[] params) { - return (new Query(text, params)).hasSolution(); - } - // - // this method doesn't work, but is intended to be called from another thread, - // to abort a Query which is open and possibly currently executing nextSolution() or similar - public final int abort() { - if (open) { - (new Thread(new Runnable() { - public void run() { - try { - int rc1 = Prolog.attach_engine(engine); - System.out.println("q.abort(): attach_engine() returns " + rc1); - int rc2 = Prolog.action_abort(); - System.out.println("q.abort(): action_abort() returns " + rc2); - // int rc3 = Prolog.release_pool_engine(); - // System.out.println("q.abort(): release_pool_engine() returns " + rc3); - } catch (Exception e) { - } - } - })).start(); // call the query in a separate thread - /* - int rc0a = Prolog.pool_engine_id(this.engine); - System.out.println("q.abort(): this.engine has id=" + rc0a); - - engine_t e = Prolog.current_engine(); - System.out.println("q.abort(): " + (e == null ? "no current engine" : "current engine id=" + Prolog.pool_engine_id(e))); - - int rc0b = Prolog.release_pool_engine(); - System.err.println("q.abort(): release_pool_engine() returns " + rc0b); - - engine_t e2 = Prolog.current_engine(); - System.out.println("q.abort(): " + (e == null ? "no current engine" : "current engine id=" + Prolog.pool_engine_id(e2))); - - int rc1 = Prolog.attach_engine(this.engine); - System.out.println("q.abort(): attach_engine() returns " + rc1); - - engine_t e3 = Prolog.current_engine(); - System.out.println("q.abort(): " + (e == null ? "no current engine" : "current engine id=" + Prolog.pool_engine_id(e3))); - - int rc2 = Prolog.action_abort(); - System.out.println("q.abort(): action_abort() returns " + rc2); - - int rc3 = Prolog.release_pool_engine(); - System.out.println("q.abort(): release_pool_engine() returns " + rc3); - - int rc4 = Prolog.attach_engine(e); - System.out.println("q.abort(): attach_engine() returns " + rc4); - */ - return 0; - } else { - System.out.println("q.abort(): query is not open"); - return -1; - } - } - //==================================================================/ - // misc - //==================================================================/ - /** - * Returns a crude String representation of a Query. - * - * @return a crude String representation of a Query - */ - public String toString() { - return goal_.name + "( " + Term.toString(goal_.args) + " )"; - } - //==================================================================/ - // Methods (deprecated) - //==================================================================/ - /** - * Returns a debug-friendly representation of a Query - * - * @return a debug-friendly representation of a Query - * @deprecated - */ - public String debugString() { - return "(Query " + goal_.name + " " + Term.debugString(goal_.args) + ")"; - } -} diff --git a/LGPL/JPL/java/jpl/Term.java b/LGPL/JPL/java/jpl/Term.java deleted file mode 100644 index 8e5f92d14..000000000 --- a/LGPL/JPL/java/jpl/Term.java +++ /dev/null @@ -1,783 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: Term.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 2004 Paul Singleton -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl; - -import java.util.Hashtable; -import java.util.Iterator; -import java.util.Map; -import jpl.fli.DoubleHolder; -import jpl.fli.Int64Holder; -import jpl.fli.IntHolder; -import jpl.fli.Prolog; -import jpl.fli.StringHolder; -import jpl.fli.term_t; - -//----------------------------------------------------------------------/ -// Term -/** - * Term is the abstract base class for - * Compound, Atom, Variable, Integer and Float, which comprise a Java-oriented concrete syntax for Prolog. - * You cannot create instances of Term directly; rather, you should create - * instances of Term's concrete subclasses. - * Alternatively, use textToTerm() to construct a Term from its conventional - * Prolog source text representation. - * - *


    - * Copyright (C) 2004 Paul Singleton

    - * Copyright (C) 1998 Fred Dushin

    - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

    - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

    - *


    - * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -public abstract class Term { - //==================================================================/ - // Attributes - //==================================================================/ - - //==================================================================/ - // Constructors - //==================================================================/ - - /** - * This default constructor is provided in order for subclasses - * to be able to define their own default constructors. - */ - protected Term() { - } - - //==================================================================/ - // Methods (abstract, common) - //==================================================================/ - - /** - * returns the ano-th (1+) argument of a (Compound) Term - * throws a JPLException for any other subclass - * - * @return the ano-th argument of a (Compound) Term - */ - public Term arg(int ano) { - throw new JPLException("jpl." + this.typeName() + ".arg() is undefined"); - }; - - /** - * returns, as a Term[], the arguments of a Compound - * returns an empty Term[] from an Atom, Integer or Float - * throws a JPLException from a Variable - * - * @return the arguments of a Compound as a Term[ - */ - public abstract Term[] args(); - - /** - * Tests whether this Term's functor has (String) 'name' and 'arity' - * Returns false if called inappropriately - * - * @return whether this Term's functor has (String) 'name' and 'arity' - */ - public boolean hasFunctor(String name, int arity) { - return false; - } - - /** - * Tests whether this Term's functor has (int) 'name' and 'arity' - * Returns false if called inappropriately - * - * @return whether this Term's functor has (int) 'name' and 'arity' - */ - public boolean hasFunctor(int value, int arity) { - return false; - } - - /** - * Tests whether this Term's functor has (double) 'name' and 'arity' - * Returns false if called inappropriately - * - * @return whether this Term's functor has (double) 'name' and 'arity' - */ - public boolean hasFunctor(double value, int arity) { - return false; - } - - /** - * returns, as a String, the name of a Compound, Atom or Variable - * throws a JPLException from an Integer or Float - * - * @return the name of a Compound, Atom or Variable - */ - public String name() { - throw new JPLException("jpl." + this.typeName() + ".name() is undefined"); - }; - - /** - * returns, as an int, the arity of a Compound, Atom, Integer or Float - * throws a JPLException from a Variable - * - * @return the arity of a Compound, Atom, Integer or Float - */ - public int arity() { - throw new JPLException("jpl." + this.typeName() + ".arity() is undefined"); - }; - - /** - * returns the value (as an int) of an Integer or Float - * throws a JPLException from a Compound, Atom or Variable - * - * @return the value (as an int) of an Integer or Float - */ - public int intValue() { - throw new JPLException("jpl." + this.typeName() + ".intValue() is undefined"); - } - /** - * returns the value (as a long) of an Integer or Float - * throws a JPLException from a Compound, Atom or Variable - * - * @return the value (as a long) of an Integer or Float - */ - public long longValue() { - throw new JPLException("jpl." + this.typeName() + ".longValue() is undefined"); - } - /** - * returns the value (as a float) of an Integer or Float - * throws a JPLException from a Compound, Atom or Variable - * - * @return the value (as a float) of an Integer or Float - */ - public float floatValue() { - throw new JPLException("jpl." + this.typeName() + ".floatValue() is undefined"); - } - - /** - * returns the value (as a double) of an Integer or Float - * throws a JPLException from any other subclass - * - * @return the value (as an double) of an Integer or Float - */ - public double doubleValue() { - throw new JPLException("jpl." + this.typeName() + ".doubleValue() is undefined"); - } - - //==================================================================/ - // Methods (common) - //==================================================================/ - - /** - * returns the type of this term, as one of jpl.fli.Prolog.COMPOUND, .ATOM, .VARIABLE, .INTEGER, .FLOAT etc - * - * @return the type of this term, as one of jpl.fli.Prolog.COMPOUND, .ATOM, .VARIABLE, .INTEGER, .FLOAT etc - */ - public abstract int type(); - - /** - * returns the name of the type of this term, as one of "Compound", "Atom", "Variable", "Integer", "Float" etc - * - * @return the name of the type of this term, as one of "Compound", "Atom", "Variable", "Integer", "Float" etc - */ - public abstract String typeName(); - - /** - * whether this Term represents an atom - * - * @return whether this Term represents an atom - */ - public boolean isAtom() { - return this instanceof Atom; - } - - /** - * whether this Term represents a compound term - * - * @return whether this Term represents a compound atom - */ - public boolean isCompound() { - return this instanceof Compound; - } - - /** - * whether this Term represents an atom - * - * @return whether this Term represents an atom - */ - public boolean isFloat() { - return this instanceof Float; - } - - /** - * whether this Term represents an atom - * - * @return whether this Term represents an atom - */ - public boolean isInteger() { - return this instanceof Integer; - } - - /** - * whether this Term is a variable - * - * @return whether this Term is a variable - */ - public boolean isVariable() { - return this instanceof Variable; - } - - /** - * whether this Term is a 'jfalse' structure, i.e. @(false) - * - * @return whether this Term is a 'jfalse' structure, i.e. @(false) - */ - public boolean isJFalse() { - return false; // overridden in Compound, where it might sometimes be true - } - - /** - * whether this Term is a 'jtrue' structure, i.e. @(true) - * - * @return whether this Term is a 'jtrue' structure, i.e. @(true) - */ - public boolean isJTrue() { - return false; // overridden in Compound, where it might sometimes be true - } - - /** - * whether this Term is a 'jnull' structure, i.e. @(null) - * - * @return whether this Term is a 'jnull' structure, i.e. @(null) - */ - public boolean isJNull() { - return false; // overridden in Compound, where it might sometimes be true - } - - /** - * whether this Term is a 'jvoid' structure, i.e. @(void) - * - * @return whether this Term is a 'jvoid' structure, i.e. @(void) - */ - public boolean isJVoid() { - return false; // overridden in Compound, where it might sometimes be true - } - - /** - * whether this Term is a 'jobject' structure, i.e. @(Tag) - * - * @return whether this Term is a 'jobject' structure, i.e. @(Tag) - */ - public boolean isJObject() { - return false; // overridden in Compound, where it might sometimes be true - } - - /** - * whether this Term is a 'jref' structure, i.e. @(Tag) or @(null) - * - * @return whether this Term is a 'jref' structure, i.e. @(Tag) or @(null) - */ - public boolean isJRef() { - return false; // overridden in Compound, where it might sometimes be true - } - - public Term putParams(Term[] ps) { - IntHolder next = new IntHolder(); - next.value = 0; - Term t2 = this.putParams1(next, ps); - if (next.value != ps.length) { - throw new JPLException("Term.putParams: more actual params than formal"); - } - return t2; - } - - public Term putParams(Term plist) { - Term[] ps = plist.toTermArray(); - return putParams(ps); - } - - protected Term putParams1(IntHolder next, Term[] ps) { - switch (this.type()) { - case Prolog.COMPOUND : - return new Compound(this.name(), putParams2(this.args(), next, ps)); - case Prolog.ATOM : - if (this.name().equals("?")) { - if (next.value >= ps.length) { - throw new JPLException("Term.putParams: fewer actual params than formal params"); - } - return ps[next.value++]; - } // else drop through to default - default : - return this; - } - } - - static protected Term[] putParams2(Term[] ts, IntHolder next, Term[] ps) { - int n = ts.length; - Term[] ts2 = new Term[n]; - for (int i = 0; i < n; i++) { - ts2[i] = ts[i].putParams1(next, ps); - } - return ts2; - } - - /** - * the length of this list, iff it is one, else an exception is thrown - * - * @throws JPLException - * @return the length (as an int) of this list, iff it is one - */ - public int listLength() { - int len = 0; - - if (this.hasFunctor(".", 2)) { - return 1 + this.arg(2).listLength(); - } else if (this.hasFunctor("[]", 0)) { - return 0; - } else { - throw new JPLException("Term.listLength: term is not a list"); - } - } - - /** returns an array of terms which are the successive members of this list, if it is a list, else throws an exception - * - * @throws JPLException - * @return an array of terms which are the successive members of this list, if it is a list - */ - public Term[] toTermArray() { - try { - int len = this.listLength(); - Term[] ts = new Term[len]; - Term t = this; - - for (int i = 0; i < len; i++) { - ts[i] = t.arg(1); - t = t.arg(2); - } - return ts; - } catch (JPLException e) { - throw new JPLException("Term.toTermArray: term is not a proper list"); - } - } - - //==================================================================/ - // Methods (deprecated) - //==================================================================/ - - /** - * Returns a debug-friendly representation of a Term - * - * @return a debug-friendly representation of a Term - * @deprecated - */ - public abstract String debugString(); - - /** - * Returns a debug-friendly representation of a list of Terms - * - * @return a debug-friendly representation of a list of Terms - * @deprecated - */ - public static String debugString(Term arg[]) { - String s = "["; - - for (int i = 0; i < arg.length; ++i) { - s += arg[i].debugString(); - if (i != arg.length - 1) { - s += ", "; - } - } - return s + "]"; - } - - //==================================================================/ - // Converting JPL Terms to Prolog terms - // - // To convert a Term to a term_t, we need to traverse the Term - // structure and build a corresponding Prolog term_t object. - // There are some issues: - // - // - Prolog term_ts rely on the *consecutive* nature of term_t - // references. In particular, to build a compound structure - // in the Prolog FLI, one must *first* determine the arity of the - // compound, create a *sequence* of term_t references, and then - // put atoms, functors, etc. into those term references. We - // do this in these methods by first determining the arity of the - // Compound, and then by "put"-ing a type into a term_t. - // The "put" method is implemented differently in each of Term's - // five subclasses. - // - // - What if we are trying to make a term_t from a Term, but the - // Term has multiple instances of the same Variable? We want - // to ensure that _one_ Prolog variable will be created, or else - // queries will give incorrect answers. We thus pass a Hashtable - // (var_table) through these methods. The table contains term_t - // instances, keyed on Variable instances. - //==================================================================/ - - public void put( term_t term){ - put( new Hashtable(), term); - } - /** - * Cache the reference to the Prolog term_t here. - * - * @param varnames_to_vars A Map from variable names to JPL Variables. - * @param term A (previously created) term_t which is to be - * put with a Prolog term-type appropriate to the Term type - * (e.g., Atom, Variable, Compound, etc.) on which the method is - * invoked.) - */ - protected abstract void put(Map varnames_to_vars, term_t term); - - /** - * 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 jpl.Term references. - * @return consecutive term_t references (first of which is - * a structure) - */ - protected static term_t putTerms(Map varnames_to_vars, Term[] args) { - - // first create a sequence of term_ts. The 0th term_t - // will be a jpl.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 reference, construct a Prolog term - // by putting an appropriate Prolog type into the reference. - // - 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; - args[i].put(varnames_to_vars, term); // each subclass defines its own put() - } - - return term0; - } - - // experiment: for jni_jobject_to_term_byval/2 in jpl.c - public static void putTerm( Object obj, term_t termref){ - if (obj instanceof Term){ - ((Term)obj).put(termref); - } else { - throw new JPLException("not a Term"); - } - } - - //==================================================================/ - // Converting Prolog terms to JPL Terms - // - // Converting back from term_ts to Terms is simple, since - // the (simplified) Term representation is canonical (there is only one - // correct structure for any given Prolog term). - // - // One problem concerns variable bindings. We illustrate - // with several examples. First, consider the Prolog fact - // - // p( f(X,X)). - // - // And the query - // - // ?- p( Y). - // - // A solution should be - // - // y = f(X,X) - // - // and indeed, if this query is run, the term_t to which Y will - // be unified is a compound, f(X,X). The problem is, how do - // we know, in converting the term_ts to Terms in the compound f/2 - // whether we should create one Variable or two? This begs the - // question, how do we _identify_ Variables in JPL? The answer - // to the latter question is, by reference; two Variable (Java) - // references refer to the same variable iff they are, in memory, - // the same Variable object. That is, they satisfy the Java == relation. - // (Note that this condition is _not_ true of the other Term types.) - // - // Given this design decision, therefore, we should create a - // single Variable instance and a Compound instance whose two arg - // values refer to the same Variable object. We therefore need to keep - // track, in converting a term_t to a Term (in particular, in - // converting a term_t whose type is variable to a Variable), of - // which Variables have been created. We do this by using the vars - // Hashtable, which gets passed recursively though the from_term_t - // methods; this table holds the Variable instances that have been - // created, keyed by the unique and internal-to-Prolog string - // representation of the variable (I'm not sure about this...). - //==================================================================/ - - /** - * This method calls from_term_t on each term in the n consecutive term_ts. - * A temporary jpl.term_t "holder" (byref) structure must be created - * in order to extract type information from the Prolog engine. - * - * @param vars_to_Vars A Map from Prolog variables to jpl.Variable instances - * @param n The number of consecutive term_ts - * @param term0 The 0th term_t (structure); subsequent - * term_ts are not structures. - * @return An array of converted Terms - */ - /* - protected static Term[] from_term_ts(Map vars_to_Vars, int n, term_t term0) { - - // create an (uninitialised) array of n Term references - Term[] terms = new Term[n]; - - // for each term_t (from 0...n-1), create a term_t - // (temporary) structure and dispatch the translation - // to a Term to the static from_term_t method of the Term - // class. This will perform (Prolog) type analysis on the - // term_t and call the appropriate static method to create - // a Term of the right type (e.g., Atom, Variable, List, etc.) - // - long ith_term_t = term0.value; - for (int i = 0; i < n; ++i, ++ith_term_t) { - term_t term = new term_t(); - term.value = ith_term_t; - - terms[i] = Term.from_term_t(vars_to_Vars, term); - } - - return terms; - } - */ - - /** - * We discover the Prolog type of the term, then forward the - * call to the appropriate subclass - * - * @param vars A Map from Prolog variables to jpl.Variable instances - * @param term The Prolog term (in a term_t holder) to convert - * @return The converted Term subtype instance. - */ - protected static Term getTerm1(Map vars_to_Vars, term_t term) { - int type = Prolog.term_type(term); - - switch (type) { - case Prolog.VARIABLE : - return Variable.getTerm(vars_to_Vars, term); - case Prolog.ATOM : - return Atom.getTerm(vars_to_Vars, term); - case Prolog.STRING : - return Atom.getString(vars_to_Vars, term); - case Prolog.INTEGER : - return Integer.getTerm(vars_to_Vars, term); - case Prolog.FLOAT : - return Float.getTerm(vars_to_Vars, term); - case Prolog.COMPOUND : - return Compound.getTerm(vars_to_Vars, term); - default : - // should never happen... - throw new JPLException("Term.from_term_t: unknown term type=" + type); - } - } - - protected static Term getTerm(Map vars_to_Vars, term_t term) { - StringHolder hString; - IntHolder hInt; - Int64Holder hInt64; - // int type = Prolog.term_type(term); - switch (Prolog.term_type(term)) { - case Prolog.VARIABLE: - for (Iterator i = vars_to_Vars.keySet().iterator(); i.hasNext();) { - term_t varX = (term_t) i.next(); // a previously seen Prolog variable - if (Prolog.compare(varX, term) == 0) { // identical Prolog variables? - return (Term) vars_to_Vars.get(varX); // return the associated JPL Variable - } - } - // otherwise, the Prolog variable in term has not been seen before - Variable Var = new Variable(); // allocate a new (sequentially named) Variable to represent it - Var.term_ = term; // this should become redundant... - vars_to_Vars.put(term, Var); // use Hashtable(var,null), but only need set(var) - return Var; - case Prolog.ATOM: // return Atom.getTerm(vars_to_Vars, term); - hString = new StringHolder(); - Prolog.get_atom_chars(term, hString); // ignore return val; assume success... - return new Atom(hString.value); - case Prolog.STRING: // return Atom.getString(vars_to_Vars, term); - hString = new StringHolder(); - Prolog.get_string_chars(term, hString); // ignore return val; assume success... - return new Atom(hString.value); - case Prolog.INTEGER: // return Integer.getTerm(vars_to_Vars, term); - hInt64 = new Int64Holder(); - Prolog.get_integer(term, hInt64); // assume it succeeds... - return new jpl.Integer(hInt64.value); - case Prolog.FLOAT: // return Float.getTerm(vars_to_Vars, term); - DoubleHolder hFloatValue = new DoubleHolder(); - Prolog.get_float(term, hFloatValue); // assume it succeeds... - return new jpl.Float(hFloatValue.value); - case Prolog.COMPOUND: // return Compound.getTerm(vars_to_Vars, term); - hString = new StringHolder(); - hInt = new IntHolder(); - Prolog.get_name_arity(term, hString, hInt); // assume it succeeds - Term 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); - default: - // should never happen... - throw new JPLException("Term.from_term_t: unknown term type=" + Prolog.term_type(term)); - } - } - - protected static Term getTerm( term_t term){ - return getTerm( new Hashtable(), term); - } - - //==================================================================/ - // Computing Substitutions - // - // Once a solution has been found, the Prolog term_t references - // will have been instantiated and will refer to new terms. To compute - // a substitution, we traverse the (original) Term structure, looking - // at the term_t reference in the Term. The only case we really care - // about is if the (original) Term is a Variable; if so, the term_t - // back in the Prolog engine may be instantiated (non Variable parts - // of the original Term cannot change or become uninstantiated). In - // this case, we can store this term in a Hashtable, keyed by the - // Variable with which the term was unified. - //==================================================================/ - - //------------------------------------------------------------------/ - // getSubst - /** - * This method computes a substitution from a Term. The bindings - * Hashtable stores Terms, keyed by Variables. Thus, a - * substitution is as it is in mathematical logic, a sequence - * of the form \sigma = {t_0/x_0, ..., t_n/x_n}. Once the - * substitution is computed, the substitution should satisfy - * - * \sigma T = t - * - * where T is the Term from which the substitution is computed, - * and t is the term_t which results from the Prolog query.

    - * - * A second Hashtable, vars, is required; this table holds - * the Variables that occur (thus far) in the unified term. - * The Variable instances in this table are guaranteed to be - * unique and are keyed on Strings which are Prolog internal - * representations of the variables. - * - * @param bindings table holding Term substitutions, keyed on - * Variables. - * @param vars A Hashtable holding the Variables that occur - * thus far in the term; keyed by internal (Prolog) string rep. - */ - protected abstract void getSubst(Map varnames_to_Terms, Map vars_to_Vars); - - //------------------------------------------------------------------/ - // getSubsts - /** - * Just calls computeSubstitution for each Term in the array. - * - * @param varnames_to_Terms a Map from variable names to Terms - * @param vars_to_Vars a Map from Prolog variables to JPL Variables - * @param arg a list of Terms - */ - protected static void getSubsts(Map varnames_to_Terms, Map vars_to_Vars, Term[] args) { - for (int i = 0; i < args.length; ++i) { - args[i].getSubst(varnames_to_Terms, vars_to_Vars); - } - } - - //------------------------------------------------------------------/ - // terms_equals - /** - * This method is used (by Compound.equals) to determine the Terms in two Term arrays - * are pairwise equal, where two Terms are equal if they satisfy - * the equals predicate (defined differently in each Term subclass). - * - * @param t1 an array of Terms - * @param t2 another array of Terms - * @return true if all of the Terms in the (same-length) arrays are pairwise equal - */ - protected static boolean terms_equals(Term[] t1, Term[] t2) { - if (t1.length != t2.length) { - return false; - } - - for (int i = 0; i < t1.length; ++i) { - if (!t1[i].equals(t2[i])) { - return false; - } - } - return true; - } - - // newJRef - /** - * This method is used (by Compound.equals) to determine the Terms in two Term arrays - * are pairwise equal, where two Terms are equal if they satisfy - * the equals predicate (defined differently in each Term subclass). - * - * @param t1 an array of Terms - * @param t2 another array of Terms - * @return true if all of the Terms in the (same-length) arrays are pairwise equal - */ - protected static Term newJRef(Object obj) { - return JPL.JNULL; // not yet implemented - } - - //------------------------------------------------------------------/ - // toString - /** - * Converts a list of Terms to a String. - * - * @param args An array of Terms to convert - * @return String representation of a list of Terms - */ - public static String toString(Term[] args) { - String s = ""; - - for (int i = 0; i < args.length; ++i) { - s += args[i].toString(); - if (i != args.length - 1) { - s += ", "; - } - } - - return s; - } - -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/Util.java b/LGPL/JPL/java/jpl/Util.java deleted file mode 100644 index c329b28d9..000000000 --- a/LGPL/JPL/java/jpl/Util.java +++ /dev/null @@ -1,281 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: Util.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 2004 Paul Singleton -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl; - -import java.util.Hashtable; -import java.util.Map; - -//----------------------------------------------------------------------/ -// Util -/** - * This class provides a bunch of static utility methods for the JPL - * High-Level Interface. - * - *


    - * Copyright (C) 2004 Paul Singleton

    - * Copyright (C) 1998 Fred Dushin

    - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

    - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

    - *


    - * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -public final class Util { - //------------------------------------------------------------------/ - // termArrayToList - /** - * Converts an array of Terms to a JPL representation of a Prolog list of terms - * whose members correspond to the respective array elements. - * - * @param terms An array of Term - * @return Term a list of the array elements - */ - public static Term termArrayToList(Term[] terms) { - Term list = new Atom("[]"); - - for (int i = terms.length - 1; i >= 0; --i) { - list = new Compound(".", new Term[] { terms[i], list }); - } - return list; - } - - /** - * Converts a solution hashtable to an array of Terms. - * - * @param varnames_to_Terms A Map from variable names to Terms - * @return Term[] An array of the Terms to which successive variables are bound - */ - public static Term[] bindingsToTermArray(Map varnames_to_Terms) { - Term[] ts = new Term[varnames_to_Terms.size()]; - - for (java.util.Iterator i = varnames_to_Terms.keySet().iterator(); i.hasNext();) { - Variable k = (Variable) i.next(); - ts[k.index] = (Term) (varnames_to_Terms.get(k)); - } - return ts; - } - - //------------------------------------------------------------------/ - // toString - /** - * Converts a substitution, in the form of a Map from variable names to Terms, to a String. - * - * @param varnames_to_Terms A Map from variable names to Terms. - * @return String A String representation of the variable bindings - */ - public static String toString(Map varnames_to_Terms) { - if (varnames_to_Terms == null) { - return "[no solution]"; - } - java.util.Iterator varnames = varnames_to_Terms.keySet().iterator(); - - String s = "Bindings: "; - while (varnames.hasNext()) { - String varname = (String) varnames.next(); - s += varname + "=" + varnames_to_Terms.get(varname).toString() + "; "; - } - return s; - } - - //------------------------------------------------------------------/ - // namevarsToMap - /** - * Converts a (JPL) list of Name=Var pairs (as yielded by atom_to_term/3) - * to a Map from Prolog variables (necessarily in term_t holders) to named JPL Variables - * - * @param nvs A JPL list of Name=Var pairs (as yielded by atom_to_term/3) - * @return Map A Map from Prolog variables (necessarily in term_t holders) to named JPL Variables - */ - public static Map namevarsToMap(Term nvs) { - - try { - Map vars_to_Vars = new Hashtable(); - - /* - while (nvs.hasFunctor(".", 2) && ((Compound) nvs).arg(1).hasFunctor("=", 2)) { - Atom name = (Atom) ((Compound) ((Compound) nvs).arg(1)).arg(1); // get the Name of the =/2 pair - Variable var = (Variable) ((Compound) ((Compound) nvs).arg(1)).arg(2); // get the Var of the =/2 pair - - vars_to_Vars.put(var.term_, new Variable(name.name())); // map the Prolog variable to a new, named Variable - nvs = ((Compound) nvs).arg(2); // advance to next list cell - } - */ - while (nvs.hasFunctor(".", 2) && nvs.arg(1).hasFunctor("=", 2)) { - // the cast to Variable is necessary to access the (protected) .term_ field - vars_to_Vars.put(((Variable)nvs.arg(1).arg(2)).term_, new Variable(nvs.arg(1).arg(1).name())); // map the Prolog variable to a new, named Variable - nvs = nvs.arg(2); // advance to next list cell - } - - // maybe oughta check that nvs is [] ? - return vars_to_Vars; - } catch (java.lang.ClassCastException e) { // nvs is not of the expected structure - return null; - } - } - - //------------------------------------------------------------------/ - // textToTerm - /** - * Converts a Prolog source text to a corresponding JPL Term - * (in which each Variable has the appropriate name from the source text). - * Throws PrologException containing error(syntax_error(_),_) if text is invalid. - * - * @param text A Prolog source text denoting a term - * @return Term a JPL Term equivalent to the given source text - */ - public static Term textToTerm(String text) { - // it might be better to use PL_chars_to_term() - Query q = new Query(new Compound("atom_to_term", new Term[] { new Atom(text), new Variable("Term"), new Variable("NVdict")})); - q.open(); - Map s = q.getSubstWithNameVars(); - if (s != null) { - q.close(); - return (Term) s.get("Term"); - } else { - return null; - } - } - // - // textParamsToTerm - /** - * Converts a Prolog source text to a corresponding JPL Term (in which each Variable has the appropriate name from the source text), replacing successive occurrences of ? in the text by the - * corresponding element of Term[] params. (New in JPL 3.0.4) - * - * Throws PrologException containing error(syntax_error(_),_) if text is invalid. - * - * @param text - * A Prolog source text denoting a term - * @return Term a JPL Term equivalent to the given source text - */ - public static Term textParamsToTerm(String text, Term[] params) { - return Util.textToTerm(text).putParams(params); - } - // - /** - * Converts an array of String to a corresponding JPL list - * - * @param a - * An array of String objects - * @return Term a JPL list corresponding to the given String array - */ - public static Term stringArrayToList(String[] a) { - Term list = new Atom("[]"); - for (int i = a.length - 1; i >= 0; i--) { - list = new Compound(".", new Term[]{new Atom(a[i]), list}); - } - return list; - } - // - /** - * Converts an array of int to a corresponding JPL list - * - * @param a - * An array of int values - * @return Term a JPL list corresponding to the given int array - */ - public static Term intArrayToList(int[] a) { - Term list = new Atom("[]"); - for (int i = a.length - 1; i >= 0; i--) { - list = new Compound(".", new Term[]{new jpl.Integer(a[i]), list}); - } - return list; - } - // - /** - * Converts an array of arrays of int to a corresponding JPL list of lists - * - * @param a - * An array of arrays of int values - * @return Term a JPL list of lists corresponding to the given int array of arrays - */ - public static Term intArrayArrayToList(int[][] a) { - Term list = new Atom("[]"); - for (int i = a.length - 1; i >= 0; i--) { - list = new Compound(".", new Term[]{intArrayToList(a[i]), list}); - } - return list; - } - public static int listToLength(Term t) { - int length = 0; - Term head = t; - while (head.hasFunctor(".", 2)) { - length++; - head = head.arg(2); - } - return (head.hasFunctor("[]", 0) ? length : -1); - } - /** converts a proper list to an array of terms, else throws an exception - * - * @throws JPLException - * @return an array of terms whose successive elements are the corresponding members of the list (if it is a list) - */ - public static Term[] listToTermArray(Term t) { - try { - int len = t.listLength(); - Term[] ts = new Term[len]; - - for (int i = 0; i < len; i++) { - ts[i] = t.arg(1); - t = t.arg(2); - } - return ts; - } catch (JPLException e) { - throw new JPLException("Util.listToTermArray: term is not a proper list"); - } - } - - public static String[] atomListToStringArray( Term t){ - int n = listToLength(t); - String[] a; - if ( n<0){ - return null; - } else { - a = new String[n]; - } - int i = 0; - Term head = t; - while ( head.hasFunctor(".", 2)){ - Term x = head.arg(1); - if ( x.isAtom()){ - a[i++]=x.name(); - } else { - return null; - } - head = head.arg(2); - } - return (head.hasFunctor("[]", 0) ? a : null ); - } -} diff --git a/LGPL/JPL/java/jpl/Variable.java b/LGPL/JPL/java/jpl/Variable.java deleted file mode 100644 index 888e92032..000000000 --- a/LGPL/JPL/java/jpl/Variable.java +++ /dev/null @@ -1,277 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: Variable.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 2004 Paul Singleton -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl; - -import java.util.Iterator; -import java.util.Map; -import jpl.fli.Prolog; -import jpl.fli.term_t; - -//----------------------------------------------------------------------/ -// Variable -/** - * This class supports Java representations of Prolog variables.

    - * - * A jpl.Variable instance is equivalent to a variable in a fragment of Prolog source text: - * it is *not* a "live" variable within a Prolog stack or heap. - * A corresponding Prolog variable is created only upon opening - * a Query whose goal refers to a Variable (and then only temporarily). - * - *


    - * Copyright (C) 2004 Paul Singleton

    - * Copyright (C) 1998 Fred Dushin

    - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

    - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

    - *


    - * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -// Implementation notes: -// -//----------------------------------------------------------------------/ -public class Variable extends Term { - //==================================================================/ - // Attributes - //==================================================================/ - private static long n = 0; // the integral part of the next automatic variable name to be allocated - public final String name; // the name of this Variable - protected transient term_t term_ = null; // defined between Query.open() and Query.get2() - protected transient int index; // only used by (redundant?) - //==================================================================/ - // Constructors - //==================================================================/ - /** - * Create a new Variable with 'name' (which must not be null or ""), - * and may one day be constrained to comply with traditional Prolog syntax. - * - * @param name the source name of this Variable - */ - public Variable(String name) { - if (name == null) { - throw new JPLException("constructor jpl.Variable(name): name cannot be null"); - } - if (!isValidName(name)) { - throw new JPLException("constructor jpl.Variable(name): name cannot be empty String"); - } - this.name = name; - } - /** - * Create a new Variable with new sequential name of the form "_261". - * - */ - public Variable() { - this.name = "_" + Long.toString(n++); // e.g. _0, _1 etc. - } - //==================================================================/ - // Methods (common) - //==================================================================/ - /** - * The (nonexistent) args of this Variable - * @throws JPLException - * - * @return the (nonexistent) args of this Variable (never) - */ - public Term[] args() { - throw new JPLException("jpl.Variable#args(): call is improper"); - } - /** - * returns the lexical name of this Variable - * - * @return the lexical name of this Variable - */ - public final String name() { - return this.name; - } - /** - * returns the type of this subclass of Term, i.e. Prolog.VARIABLE - * - * @return the type of this subclass of Term, i.e. Prolog.VARIABLE - */ - public final int type() { - return Prolog.VARIABLE; - } - /** - * returns the typeName of this subclass of Term, i.e. "Variable" - * - * @return the typeName of this subclass of Term, i.e. "Variable" - */ - public String typeName() { - return "Variable"; - } - /** - * Returns a Prolog source text representation of this Variable - * - * @return a Prolog source text representation of this Variable - */ - public String toString() { - return this.name; - } - /** - * A Variable is equal to another if their names are the same and they are not anonymous. - * - * @param obj The Object to compare. - * @return true if the Object is a Variable and the above condition apply. - */ - public final boolean equals(Object obj) { - return obj instanceof Variable && !this.name.equals("_") && this.name.equals(((Variable) obj).name); - } - //==================================================================/ - // Methods (private) - //==================================================================/ - /** - * Tests the lexical validity of s as a variable's name - * - * @return the lexical validity of s as a variable's name - * @deprecated - */ - private boolean isValidName(String s) { - if (s == null) { - throw new java.lang.NullPointerException(); // JPL won't call it this way - } - int len = s.length(); - if (len == 0) { - throw new JPLException("invalid variable name"); - } - char c = s.charAt(0); - if (!(c == '_' || c >= 'A' && c <= 'Z')) { - return false; - } - for (int i = 1; i < len; i++) { - c = s.charAt(i); - if (!(c == '_' || c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c >= '0' && c <= '9')) { - return false; - } - } - return true; - } - //==================================================================/ - // Methods (deprecated) - //==================================================================/ - /** - * Returns a debug-friendly String representation of an Atom. - * - * @return a debug-friendly String representation of an Atom - * @deprecated - */ - public String debugString() { - return "(Variable " + toString() + ")"; - } - //==================================================================/ - // Converting JPL Terms to Prolog terms - //==================================================================/ - /** - * To put a Variable, we must check whether a (non-anonymous) variable with the same name - * has already been put in the Term. If one has, then the corresponding Prolog variable has - * been stashed in the varnames_to_vars Map, keyed by the Variable name, so we can look - * it up and reuse it (this way, the sharing of variables in the Prolog term - * reflects the sharing of Variable names in the Term. - * Otherwise, if this Variable name has not - * already been seen in the Term, then we put a new Prolog variable and add it into the Map - * (keyed by this Variable name). - * - * @param varnames_to_vars A Map from variable names to Prolog variables. - * @param term A (previously created) term_t which is to be - * set to a (new or reused) Prolog variable. - */ - protected final void put(Map varnames_to_vars, term_t term) { - term_t var; - // if this var is anonymous or as yet unseen, put a new Prolog variable - if (this.name.equals("_") || (var = (term_t) varnames_to_vars.get(this.name)) == null) { - this.term_ = term; - this.index = varnames_to_vars.size(); // i.e. first var in is #0 etc. - Prolog.put_variable(term); - if (!this.name.equals("_")) { - varnames_to_vars.put(this.name, term); - } - } else { - this.term_ = var; - Prolog.put_term(term, var); - } - } - //==================================================================/ - // Converting Prolog terms to JPL Terms - //==================================================================/ - /** - * Converts a term_t (known to refer to a Prolog variable) to a Variable. - * If the variable has already been seen (and hence converted), - * return its corresponding Variable from the map, - * else create a new Variable, stash it in the map (keyed by the Prolog variable), - * and return it. - * - * @param vars_to_Vars a map from Prolog to JPL variables - * @param var The term_t (known to be a variable) to convert - * @return A new or reused Variable - */ - protected static Term getTerm1(Map vars_to_Vars, term_t var) { - for (Iterator i = vars_to_Vars.keySet().iterator(); i.hasNext();) { - term_t varX = (term_t) i.next(); // a previously seen Prolog variable - if (Prolog.compare(varX, var) == 0) { // identical Prolog variables? - return (Term) vars_to_Vars.get(varX); // return the associated JPL Variable - } - } - // otherwise, the Prolog variable in term has not been seen before - Variable Var = new Variable(); // allocate a new (sequentially named) Variable to represent it - Var.term_ = var; // this should become redundant... - vars_to_Vars.put(var, Var); // use Hashtable(var,null), but only need set(var) - return Var; - } - //==================================================================/ - // Computing Substitutions - //==================================================================/ - /** - * If this Variable instance is not an anonymous or (in dont-tell-me mode) a dont-tell-me variable, and its binding is not already in the varnames_to_Terms Map, - * put the result of converting the term_t to which this variable - * has been unified to a Term in the Map, keyed on this Variable's name. - * - * @param varnames_to_Terms A Map of bindings from variable names to JPL Terms. - * @param vars_to_Vars A Map from Prolog variables to JPL Variables. - */ - protected final void getSubst(Map varnames_to_Terms, Map vars_to_Vars) { - // NB a Variable.name cannot be "" i.e. of 0 length - // if (!(this.name.charAt(0) == '_') && varnames_to_Terms.get(this.name) == null) { - if (tellThem() && varnames_to_Terms.get(this.name) == null) { - varnames_to_Terms.put(this.name, Term.getTerm(vars_to_Vars, this.term_)); - } - } - // whether, according to prevailing policy and theis Variable's name, - // any binding should be returned - // (yes, unless it's anonymous or we're in dont-tell-me mode and its a dont-tell-me variable) - private final boolean tellThem() { - return !(this.name.equals("_") || jpl.JPL.modeDontTellMe && this.name.charAt(0) == '_'); - // return !this.name.equals("_"); - } -} -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/Version.java b/LGPL/JPL/java/jpl/Version.java deleted file mode 100644 index cec971f6b..000000000 --- a/LGPL/JPL/java/jpl/Version.java +++ /dev/null @@ -1,9 +0,0 @@ -// $Id: Version.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -package jpl; - -class Version { - public final int major = 3; - public final int minor = 1; - public final int patch = 3; - public final String status = "alpha"; -} diff --git a/LGPL/JPL/java/jpl/fli/BooleanHolder.java b/LGPL/JPL/java/jpl/fli/BooleanHolder.java deleted file mode 100644 index 99edaf588..000000000 --- a/LGPL/JPL/java/jpl/fli/BooleanHolder.java +++ /dev/null @@ -1,60 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: BooleanHolder.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl.fli; - - - -//----------------------------------------------------------------------/ -// BooleanHolder -/** - * A BooleanHolder is merely a Holder class for a boolean value. - * - *
    - * Copyright (C) 1998 Fred Dushin

    - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

    - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

    - *


    - * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -// Implementation notes: -// -//----------------------------------------------------------------------/ -public class BooleanHolder -{ - public boolean value; -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/fli/DoubleHolder.java b/LGPL/JPL/java/jpl/fli/DoubleHolder.java deleted file mode 100644 index 646a64723..000000000 --- a/LGPL/JPL/java/jpl/fli/DoubleHolder.java +++ /dev/null @@ -1,60 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: DoubleHolder.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl.fli; - - - -//----------------------------------------------------------------------/ -// DoubleHolder -/** - * A DoubleHolder is merely a Holder class for a double value. - * - *
    - * Copyright (C) 1998 Fred Dushin

    - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

    - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

    - *


    - * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -// Implementation notes: -// -//----------------------------------------------------------------------/ -public class DoubleHolder -{ - public double value; -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/fli/Int64Holder.java b/LGPL/JPL/java/jpl/fli/Int64Holder.java deleted file mode 100644 index bb721e363..000000000 --- a/LGPL/JPL/java/jpl/fli/Int64Holder.java +++ /dev/null @@ -1,34 +0,0 @@ -package jpl.fli; - - - -//----------------------------------------------------------------------/ -//Int64Holder -/** -* An Int64Holder is merely a Holder class for an int64 value. -* -*
    -* Copyright (C) 2005 Paul Singleton

    -* -* This library is free software; you can redistribute it and/or -* modify it under the terms of the GNU Library Public License -* as published by the Free Software Foundation; either version 2 -* of the License, or (at your option) any later version.

    -* -* This library is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU Library Public License for more details.

    -*


    -* @author Paul Singleton -* @version $Revision: 1.1 $ -*/ -//Implementation notes: -// -//----------------------------------------------------------------------/ -public class Int64Holder -{ - public long value; -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/fli/IntHolder.java b/LGPL/JPL/java/jpl/fli/IntHolder.java deleted file mode 100644 index 5c41403b6..000000000 --- a/LGPL/JPL/java/jpl/fli/IntHolder.java +++ /dev/null @@ -1,60 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: IntHolder.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl.fli; - - - -//----------------------------------------------------------------------/ -// IntHolder -/** - * An IntHolder is merely a Holder class for an Int value. - * - *
    - * Copyright (C) 1998 Fred Dushin

    - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

    - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

    - *


    - * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -// Implementation notes: -// -//----------------------------------------------------------------------/ -public class IntHolder -{ - public int value; -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/fli/LongHolder.java b/LGPL/JPL/java/jpl/fli/LongHolder.java deleted file mode 100644 index 950b18993..000000000 --- a/LGPL/JPL/java/jpl/fli/LongHolder.java +++ /dev/null @@ -1,61 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: LongHolder.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl.fli; - -//----------------------------------------------------------------------/ -// LongHolder -/** - * A Long Holder merely holds a long value. - * - *
    - * Copyright (C) 1998 Fred Dushin

    - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

    - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

    - *


    - * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -// Implementation notes: -// -//----------------------------------------------------------------------/ -public class LongHolder { - public long value = 0L; - - public boolean equals(LongHolder lh) { - return lh.value == this.value; - } -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/fli/ObjectHolder.java b/LGPL/JPL/java/jpl/fli/ObjectHolder.java deleted file mode 100644 index 2de7031f9..000000000 --- a/LGPL/JPL/java/jpl/fli/ObjectHolder.java +++ /dev/null @@ -1,60 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: ObjectHolder.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl.fli; - - - -//----------------------------------------------------------------------/ -// ObjectHolder -/** - * A ObjectHolder is merely a Holder class for an Object reference (or null). - * - *
    - * Copyright (C) 1998 Fred Dushin

    - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

    - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

    - *


    - * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -// Implementation notes: -// -//----------------------------------------------------------------------/ -public class ObjectHolder -{ - public Object value; -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/fli/PointerHolder.java b/LGPL/JPL/java/jpl/fli/PointerHolder.java deleted file mode 100644 index c8f012390..000000000 --- a/LGPL/JPL/java/jpl/fli/PointerHolder.java +++ /dev/null @@ -1,63 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: PointerHolder.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl.fli; - - - -//----------------------------------------------------------------------/ -// PointerHolder -/** - * A PointerHolder is a trivial extension of a LongHolder. This is sort of - * a no-no in Java, as the long value stored herein is sometimes a - * machine address. (Don't tell Sun.) - * - *
    - * Copyright (C) 1998 Fred Dushin

    - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

    - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

    - *


    - * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -// Implementation notes: -// There could be issues in the future with signedness, since Java -// does not have an unsigned type; make sure not to do any arithmetic -// with the stored value. -//----------------------------------------------------------------------/ -public class PointerHolder extends LongHolder -{ -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/fli/Prolog.java b/LGPL/JPL/java/jpl/fli/Prolog.java deleted file mode 100644 index 48d3a518f..000000000 --- a/LGPL/JPL/java/jpl/fli/Prolog.java +++ /dev/null @@ -1,237 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: Prolog.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl.fli; - - -//----------------------------------------------------------------------/ -// Prolog -/** - * This class consists only of constants (static finals) and static - * native methods. The constants and methods defined herein are in - * (almost) strict 1-1 correspondence with the functions in the Prolog - * FLI by the same name (except without the PL_, SQ_, etc. prefixes).

    - * - * See the file jpl.c for the native (ANSI C) implementations of these - * methods. Refer to your local Prolog FLI documentations for the meanings - * of these methods, and observe the following:

    - * - *

    - *
  • The types and signatures of the following methods are almost - * in 1-1 correspondence with the Prolog FLI. The Prolog types - * term_t, atom_t, functor_t, etc. are mirrored in this package with - * classes by the same name, making the C and Java uses of these - * interfaces similar.
  • - *
  • As term_t, functor_t, etc. types are Java classes, they are - * passed to these methods by value; however, calling these - * methods on such class instances does have side effects. In general, - * the value fields of these instances will be modified, in much the - * same way the term_t, functor_t, etc. Prolog instances would be - * modified.
  • - *
  • The exceptions to this rule occur when maintaining the same - * signature would be impossible, e.g., when the Prolog FLI functions - * require pointers; in this case, the signatures have been - * modified to take *Holder classes (Int, Double, String, etc.), - * to indicate a call by reference parameter. - *
  • Functions which take variable-length argument lists in C - * take arrays in Java; from Java 1.1 onwards, anonymous arrays - * can be used e.g. Term[] { new Atom("a"), new Atom ("b") } - *
  • - * - *
    - * Copyright (C) 1998 Fred Dushin

    - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

    - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

    - *


    - * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -public final class Prolog { - static { - System.loadLibrary("jpl"); - } - - /* term types */ - public static final int VARIABLE = 1; - public static final int ATOM = 2; - public static final int INTEGER = 3; - public static final int FLOAT = 4; - public static final int STRING = 5; - public static final int COMPOUND = 6; - - public static final int JBOOLEAN = 101; - public static final int JREF = 102; - public static final int JVOID = 103; - - /** - * @deprecated use Prolog.COMPOUND - */ - public static final int TERM = 6; - - public static final int succeed = 1; - public static final int fail = 0; - - /* query flags */ - public static final int Q_NORMAL = 0x02; - public static final int Q_NODEBUG = 0x04; - public static final int Q_CATCH_EXCEPTION = 0x08; - public static final int Q_PASS_EXCEPTION = 0x10; - - /* conversion flags */ - public static final int CVT_ATOM = 0x0001; - public static final int CVT_STRING = 0x0002; - public static final int CVT_LIST = 0x0004; - public static final int CVT_INTEGER = 0x0008; - public static final int CVT_FLOAT = 0x0010; - public static final int CVT_VARIABLE = 0x0020; - public static final int CVT_NUMBER = (CVT_INTEGER | CVT_FLOAT); - public static final int CVT_ATOMIC = (CVT_NUMBER | CVT_ATOM | CVT_STRING); - public static final int CVT_ALL = 0x00ff; - public static final int BUF_DISCARDABLE = 0x0000; - public static final int BUF_RING = 0x0100; - public static final int BUF_MALLOC = 0x0200; - - /* new, for revised term_t-to-Variable stuff */ - public static native int compare(term_t t1, term_t t2); // returns -1, 0 or 1 - - /* Creating and destroying term-refs */ - public static native term_t new_term_ref(); - public static native term_t new_term_refs(int n); - public static native term_t copy_term_ref(term_t from); // NOT USED - - /* Constants */ - public static native atom_t new_atom(String s); - public static native String atom_chars(atom_t a); - public static native functor_t new_functor(atom_t f, int a); - - public static native void unregister_atom(atom_t a); // called from atom_t's finalize() - - /* Get Java-values from Prolog terms */ - public static native boolean get_atom_chars(term_t t, StringHolder a); - public static native boolean get_string_chars(term_t t, StringHolder s); - public static native boolean get_integer(term_t t, Int64Holder i); - public static native boolean get_float(term_t t, DoubleHolder d); - public static native boolean get_name_arity(term_t t, StringHolder name, IntHolder arity); - public static native boolean get_arg(int index, term_t t, term_t a); - - public static native String object_to_tag(Object obj); - - /* Verify types */ - public static native int term_type(term_t t); - - /* Assign to term-references */ - public static native void put_variable(term_t t); - public static native void put_integer(term_t t, long i); - public static native void put_float(term_t t, double f); - public static native void put_term(term_t t1, term_t t2); - public static native void put_jref(term_t t, Object ref); - - /* ... */ - public static native void cons_functor_v(term_t h, functor_t fd, term_t a0); - - // predicates: - public static native predicate_t predicate(String name, int arity, String module); - - // querying (general): - public static native qid_t open_query(module_t m, int flags, predicate_t pred, term_t t0); - public static native boolean next_solution(qid_t qid); - public static native void close_query(qid_t qid); - - // modules: - public static native module_t new_module(atom_t name); - - // exceptions: - public static native term_t exception(qid_t qid); - - // initialisation: - public static native String[] get_default_init_args(); - public static native boolean set_default_init_args(String argv[]); - public static native boolean initialise(); - public static native String[] get_actual_init_args(); - public static native void halt(int status); - - // thread & engine management: - public static native int thread_self(); - public static native engine_t attach_pool_engine(); - public static native int release_pool_engine(); - public static native engine_t current_engine(); - public static native boolean current_engine_is_pool(); - public static native int attach_engine(engine_t e); - - // misc. - public static native String get_c_lib_version(); - - public static native int action_abort(); - - // not used: - // public static native void reset_term_refs(term_t r); - // public static native atom_t functor_name(functor_t f); - // public static native int functor_arity(functor_t f); - // public static native boolean get_atom(term_t t, atom_t a); - // public static native boolean get_pointer(term_t t, PointerHolder ptr); - // public static native boolean get_functor(term_t t, functor_t f); - // public static native boolean get_module(term_t t, module_t module); - // public static native boolean get_jref(term_t t, ObjectHolder obj); - // public static native boolean get_jboolean(term_t t, BooleanHolder b); - // public static native boolean get_jpl_term(term_t t, ObjectHolder obj); // withdrawn 17/Oct/2004 - // public static native boolean is_variable(term_t t); - // public static native boolean is_atom(term_t t); - // public static native boolean is_integer(term_t t); - // public static native boolean is_float(term_t t); - // public static native boolean is_compound(term_t t); - // public static native boolean is_functor(term_t t, functor_t f); - // public static native boolean is_atomic(term_t t); - // public static native boolean is_number(term_t t); - // public static native void put_atom(term_t t, atom_t a); - // public static native void put_pointer(term_t t, PointerHolder ptr); - // public static native void put_functor(term_t t, functor_t functor); - // public static native void put_jboolean(term_t t, boolean b); - // public static native void put_jvoid(term_t t); - // public static native void cons_list(term_t l, term_t h, term_t t); - // public static native int unify(term_t t1, term_t t2); - // public static native predicate_t pred(functor_t f, module_t m); - // public static native int predicate_info(predicate_t pred, atom_t name, IntHolder arity, module_t module); - // public static native void cut_query(qid_t qid); - // public static native boolean call(term_t t, module_t m); - // public static native boolean call_predicate(module_t m, int debug, predicate_t pred, term_t t0); - // public static native fid_t open_foreign_frame(); // NOT USED - // public static native void close_foreign_frame(fid_t cid); // NOT USED - // public static native void discard_foreign_frame(fid_t cid); // NOT USED - // public static native module_t context(); - // public static native atom_t module_name(module_t module); - // public static native int strip_module(term_t in, module_t m, term_t out); - // public static native int pool_engine_id(engine_t e); -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/fli/StringHolder.java b/LGPL/JPL/java/jpl/fli/StringHolder.java deleted file mode 100644 index 728312d25..000000000 --- a/LGPL/JPL/java/jpl/fli/StringHolder.java +++ /dev/null @@ -1,60 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: StringHolder.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl.fli; - - - -//----------------------------------------------------------------------/ -// StringHolder -/** - * A StringHolder is merely a Holder class for a String value. - * - *
    - * Copyright (C) 1998 Fred Dushin

    - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

    - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

    - *


    - * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -// Implementation notes: -// -//----------------------------------------------------------------------/ -public class StringHolder -{ - public String value; -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/fli/atom_t.java b/LGPL/JPL/java/jpl/fli/atom_t.java deleted file mode 100644 index e5ad07b17..000000000 --- a/LGPL/JPL/java/jpl/fli/atom_t.java +++ /dev/null @@ -1,82 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: atom_t.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl.fli; - - - -//----------------------------------------------------------------------/ -// atom_t -/** - * An atom_t is a specialised LongHolder which decrements its atom's - * reference count when garbage-collected (finalized). - * - *
    - * Copyright (C) 1998 Fred Dushin

    - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

    - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

    - *


    - * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -// Implementation notes: -// -//----------------------------------------------------------------------/ -public class atom_t -extends LongHolder -{ - //------------------------------------------------------------------/ - // toString - /** - * The String representation of an atom_t is just the atom's name. - * - * @return atom's name - */ - // Implementation notes: - // - //------------------------------------------------------------------/ - public String - toString() - { - return Prolog.atom_chars( this ); - } - - protected void finalize() throws Throwable { - - super.finalize(); - Prolog.unregister_atom( this); - } -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/fli/engine_t.java b/LGPL/JPL/java/jpl/fli/engine_t.java deleted file mode 100644 index b38093e67..000000000 --- a/LGPL/JPL/java/jpl/fli/engine_t.java +++ /dev/null @@ -1,56 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: engine_t.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl.fli; - -//----------------------------------------------------------------------/ -// engine_t -/** - * A engine_t holds a reference to a Prolog engine. - * - *
    - * Copyright (C) 1998 Fred Dushin

    - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

    - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

    - *


    - * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -// Implementation notes: Note that a engine_t is not a term, -// consistent with the treatment in the Prolog FLI. -//----------------------------------------------------------------------/ -public class engine_t extends LongHolder { -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/fli/fid_t.java b/LGPL/JPL/java/jpl/fli/fid_t.java deleted file mode 100644 index 805e8f944..000000000 --- a/LGPL/JPL/java/jpl/fli/fid_t.java +++ /dev/null @@ -1,60 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: fid_t.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl.fli; - - - -//----------------------------------------------------------------------/ -// fid_t -/** - * An fid_t holds the value of a frame id in the Prolog Engine. - * - *
    - * Copyright (C) 1998 Fred Dushin

    - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

    - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

    - *


    - * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -// Implementation notes: -// -//----------------------------------------------------------------------/ -public class fid_t -extends LongHolder -{ -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/fli/functor_t.java b/LGPL/JPL/java/jpl/fli/functor_t.java deleted file mode 100644 index fff58595c..000000000 --- a/LGPL/JPL/java/jpl/fli/functor_t.java +++ /dev/null @@ -1,61 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: functor_t.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl.fli; - - - -//----------------------------------------------------------------------/ -// functor_t -/** - * A functor_t holds a reference to a Prolog functor_t in the - * Prolog engine. - * - *
    - * Copyright (C) 1998 Fred Dushin

    - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

    - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

    - *


    - * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -// Implementation notes: Note that a functor_t is not a term, -// consistent with the treatment in the Prolog FLI. -//----------------------------------------------------------------------/ -public class functor_t -extends LongHolder -{ -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/fli/module_t.java b/LGPL/JPL/java/jpl/fli/module_t.java deleted file mode 100644 index 25017d972..000000000 --- a/LGPL/JPL/java/jpl/fli/module_t.java +++ /dev/null @@ -1,61 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: module_t.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl.fli; - - - -//----------------------------------------------------------------------/ -// module_t -/** - * A module_t is a PointerHolder type which holds a reference to a Prolog - * module_t reference. - * - *
    - * Copyright (C) 1998 Fred Dushin

    - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

    - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

    - *


    - * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -// Implementation notes: -// -//----------------------------------------------------------------------/ -public class module_t -extends PointerHolder -{ -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/fli/predicate_t.java b/LGPL/JPL/java/jpl/fli/predicate_t.java deleted file mode 100644 index e8bebc9c3..000000000 --- a/LGPL/JPL/java/jpl/fli/predicate_t.java +++ /dev/null @@ -1,61 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: predicate_t.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl.fli; - - - -//----------------------------------------------------------------------/ -// predicate_t -/** - * A predicate_t is a PointerHolder class whose value is a reference to a - * Prolog predicate_t. - * - *
    - * Copyright (C) 1998 Fred Dushin

    - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

    - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

    - *


    - * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -// Implementation notes: -// -//----------------------------------------------------------------------/ -public class predicate_t -extends PointerHolder -{ -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/fli/qid_t.java b/LGPL/JPL/java/jpl/fli/qid_t.java deleted file mode 100644 index 0e40b81ab..000000000 --- a/LGPL/JPL/java/jpl/fli/qid_t.java +++ /dev/null @@ -1,60 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: qid_t.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl.fli; - - - -//----------------------------------------------------------------------/ -// qid_t -/** - * A qid_t holds a reference to a Prolog qid_t. - * - *
    - * Copyright (C) 1998 Fred Dushin

    - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

    - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

    - *


    - * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -// Implementation notes: -// -//----------------------------------------------------------------------/ -public class qid_t -extends LongHolder -{ -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/fli/term_t.java b/LGPL/JPL/java/jpl/fli/term_t.java deleted file mode 100644 index 879678d1e..000000000 --- a/LGPL/JPL/java/jpl/fli/term_t.java +++ /dev/null @@ -1,133 +0,0 @@ -//tabstop=4 -//*****************************************************************************/ -// Project: jpl -// -// File: $Id: term_t.java,v 1.2 2007-09-27 15:25:32 vsc Exp $ -// Date: $Date: 2007-09-27 15:25:32 $ -// Author: Fred Dushin -// -// -// Description: -// -// -// ------------------------------------------------------------------------- -// Copyright (c) 1998 Fred Dushin -// All rights reserved. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library Public License for more details. -//*****************************************************************************/ -package jpl.fli; - - - -//----------------------------------------------------------------------/ -// term_t -/** - * A term_t is a simple class which mirrors the term_t type in - * the Prolog FLI. All it really does is hold a term reference, - * which is an internal representation of a term in the Prolog - * Engine. - * - *
    - * Copyright (C) 1998 Fred Dushin

    - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version.

    - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library Public License for more details.

    - *


    - * @author Fred Dushin - * @version $Revision: 1.2 $ - */ -// Implementation notes: -// -//----------------------------------------------------------------------/ -public class term_t -extends LongHolder -{ - public static final long UNASSIGNED = -1L; - - public - term_t() - { - value = UNASSIGNED; - } - - //------------------------------------------------------------------/ - // toString - /** - * This static method converts a term_t, which is assumed to contain - * a reference to a *consecutive* list of term_t references to a - * String representation of a list of terms, in this case, a comma - * separated list. - * - * @param n the number of consecutive term_ts - * @param term0 a term_t whose value is the 0th term_t. - */ - // Implementation notes: - // - //------------------------------------------------------------------/ - public static String - toString( int n, term_t term0 ) - { - String s = ""; - int i; - long ith_term_t; - - for ( i = 0, ith_term_t = term0.value; i < n; ++i, ++ith_term_t ){ - term_t term = new term_t(); - term.value = ith_term_t; - s += term.toString(); - - if ( i != n - 1 ){ - s += ", "; - } - } - - return s; - } - - - //------------------------------------------------------------------/ - // equals - /** - * Instances of term_ts are stored in Term objects (see jpl.Term), - * and these term_ts are in some cases stored in Hashtables. - * Supplying this predicate provides the right behavior in Hashtable - * lookup (see the rules for Hashtable lookup in java.util).

    - * - * Note. Two term_ts are *not* equal if their values have not - * been assigned. (Since Prolog FLI term_ts are unsigned values and - * the UNASSIGNED value is -1, this should work). - * - * @param obj the Object to comapre. - * @return true if the supplied object is a term_t instances - * and the long values are the same - */ - // Implementation notes: - // - //------------------------------------------------------------------/ - public boolean - equals( Object obj ) - { - return - (obj instanceof term_t) && - this.value == ((term_t)obj).value && - this.value != UNASSIGNED; - } -} - -//345678901234567890123456789012346578901234567890123456789012345678901234567890 diff --git a/LGPL/JPL/java/jpl/test/CelsiusConverter.java b/LGPL/JPL/java/jpl/test/CelsiusConverter.java deleted file mode 100644 index 74248498c..000000000 --- a/LGPL/JPL/java/jpl/test/CelsiusConverter.java +++ /dev/null @@ -1,78 +0,0 @@ -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], _)"); - } -} diff --git a/LGPL/JPL/java/jpl/test/Family.java b/LGPL/JPL/java/jpl/test/Family.java deleted file mode 100644 index 714feba14..000000000 --- a/LGPL/JPL/java/jpl/test/Family.java +++ /dev/null @@ -1,96 +0,0 @@ -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(); - } - - } - -} diff --git a/LGPL/JPL/java/jpl/test/FetchBigTree.java b/LGPL/JPL/java/jpl/test/FetchBigTree.java deleted file mode 100644 index 0983453a7..000000000 --- a/LGPL/JPL/java/jpl/test/FetchBigTree.java +++ /dev/null @@ -1,18 +0,0 @@ -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"); - } -} diff --git a/LGPL/JPL/java/jpl/test/FetchLongList.java b/LGPL/JPL/java/jpl/test/FetchLongList.java deleted file mode 100644 index 76a84ae4f..000000000 --- a/LGPL/JPL/java/jpl/test/FetchLongList.java +++ /dev/null @@ -1,17 +0,0 @@ -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"); - } -} diff --git a/LGPL/JPL/java/jpl/test/Ga.java b/LGPL/JPL/java/jpl/test/Ga.java deleted file mode 100644 index 53f2b6c7e..000000000 --- a/LGPL/JPL/java/jpl/test/Ga.java +++ /dev/null @@ -1,23 +0,0 @@ -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(); - } -} diff --git a/LGPL/JPL/java/jpl/test/Ga2.java b/LGPL/JPL/java/jpl/test/Ga2.java deleted file mode 100644 index 64c32a5c7..000000000 --- a/LGPL/JPL/java/jpl/test/Ga2.java +++ /dev/null @@ -1,10 +0,0 @@ -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(); - } -} diff --git a/LGPL/JPL/java/jpl/test/Garbo.java b/LGPL/JPL/java/jpl/test/Garbo.java deleted file mode 100644 index 0d1216945..000000000 --- a/LGPL/JPL/java/jpl/test/Garbo.java +++ /dev/null @@ -1,19 +0,0 @@ -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(); - } - } -} diff --git a/LGPL/JPL/java/jpl/test/JPLTest.java b/LGPL/JPL/java/jpl/test/JPLTest.java deleted file mode 100644 index 3907b7a64..000000000 --- a/LGPL/JPL/java/jpl/test/JPLTest.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * 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(); - } -} diff --git a/LGPL/JPL/java/jpl/test/Masstest.java b/LGPL/JPL/java/jpl/test/Masstest.java deleted file mode 100644 index 59ccafc57..000000000 --- a/LGPL/JPL/java/jpl/test/Masstest.java +++ /dev/null @@ -1,42 +0,0 @@ -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); - } - } -} diff --git a/LGPL/JPL/java/jpl/test/MaxObjects.java b/LGPL/JPL/java/jpl/test/MaxObjects.java deleted file mode 100644 index 16bcf92c7..000000000 --- a/LGPL/JPL/java/jpl/test/MaxObjects.java +++ /dev/null @@ -1,4 +0,0 @@ -package jpl.test; - -public class MaxObjects { -} diff --git a/LGPL/JPL/java/jpl/test/ShadowA.java b/LGPL/JPL/java/jpl/test/ShadowA.java deleted file mode 100644 index c537ae15c..000000000 --- a/LGPL/JPL/java/jpl/test/ShadowA.java +++ /dev/null @@ -1,13 +0,0 @@ -/* - * 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; -} \ No newline at end of file diff --git a/LGPL/JPL/java/jpl/test/ShadowB.java b/LGPL/JPL/java/jpl/test/ShadowB.java deleted file mode 100644 index 37c1a8637..000000000 --- a/LGPL/JPL/java/jpl/test/ShadowB.java +++ /dev/null @@ -1,16 +0,0 @@ -/* - * 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; -} \ No newline at end of file diff --git a/LGPL/JPL/java/jpl/test/SyntaxError.java b/LGPL/JPL/java/jpl/test/SyntaxError.java deleted file mode 100644 index c224b6510..000000000 --- a/LGPL/JPL/java/jpl/test/SyntaxError.java +++ /dev/null @@ -1,10 +0,0 @@ -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"); - } -} diff --git a/LGPL/JPL/java/jpl/test/Test.java b/LGPL/JPL/java/jpl/test/Test.java deleted file mode 100644 index f61aea713..000000000 --- a/LGPL/JPL/java/jpl/test/Test.java +++ /dev/null @@ -1,283 +0,0 @@ -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[]"; - } -} diff --git a/LGPL/JPL/java/jpl/test/TestJUnit.java b/LGPL/JPL/java/jpl/test/TestJUnit.java deleted file mode 100644 index bef6438e2..000000000 --- a/LGPL/JPL/java/jpl/test/TestJUnit.java +++ /dev/null @@ -1,532 +0,0 @@ -// 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); - } -} diff --git a/LGPL/JPL/java/jpl/test/TestOLD.java b/LGPL/JPL/java/jpl/test/TestOLD.java deleted file mode 100644 index 496fb3800..000000000 --- a/LGPL/JPL/java/jpl/test/TestOLD.java +++ /dev/null @@ -1,142 +0,0 @@ -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()); - } -} \ No newline at end of file diff --git a/LGPL/JPL/jpl.yap b/LGPL/JPL/jpl.yap deleted file mode 100644 index 3c0876b44..000000000 --- a/LGPL/JPL/jpl.yap +++ /dev/null @@ -1,4672 +0,0 @@ -/* $Id: jpl.yap,v 1.9 2007-09-27 15:25:32 vsc Exp $ - - Part of JPL -- SWI-Prolog/Java interface - - Author: Paul Singleton, Fred Dushin and Jan Wielemaker - E-mail: paul@jbgb.com - WWW: http://www.swi-prolog.org - Copyright (C): 1985-2004, Paul Singleton - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - - As a special exception, if you link this library with other files, - compiled with a Free Software compiler, to produce an executable, this - library does not by itself cause the resulting executable to be covered - by the GNU General Public License. This exception does not however - invalidate any other reasons why the executable file might be covered by - the GNU General Public License. -*/ - -:- module(jpl, - [ jpl_get_default_jvm_opts/1, - jpl_set_default_jvm_opts/1, - jpl_get_actual_jvm_opts/1, - jpl_pl_lib_version/1, - jpl_c_lib_version/1, - jpl_new/3, - jpl_call/4, - jpl_get/3, - jpl_set/3, - jpl_servlet_byref/3, - jpl_servlet_byval/3, - jpl_class_to_classname/2, - jpl_class_to_type/2, - jpl_classname_to_class/2, - jpl_classname_to_type/2, - jpl_datum_to_type/2, - jpl_false/1, - jpl_is_class/1, - jpl_is_false/1, - jpl_is_null/1, - jpl_is_object/1, - jpl_is_object_type/1, - jpl_is_ref/1, - jpl_is_true/1, - jpl_is_type/1, - jpl_is_void/1, - jpl_null/1, - jpl_object_to_class/2, - jpl_object_to_type/2, - jpl_primitive_type/1, - jpl_ref_to_type/2, - jpl_true/1, - jpl_type_to_class/2, - jpl_type_to_classname/2, - jpl_void/1, - jpl_array_to_length/2, - jpl_array_to_list/2, - jpl_datums_to_array/2, - jpl_enumeration_element/2, - jpl_enumeration_to_list/2, - jpl_hashtable_pair/2, - jpl_iterator_element/2, - jpl_list_to_array/2, - jpl_list_to_array/3, % vsc: hack to speedup copying big lists - jpl_terms_to_array/2, - jpl_map_element/2, - jpl_set_element/2 - ]). -:- use_module(library(lists)). - -% vsc: emulate swi in YAP - -:- dynamic yap/0. - -:- predicate_property(yap_flag(_,_),built_in), [jpl_paths]. -% - -% suppress debugging this library -:- set_prolog_flag(generate_debug_info, false). - -%------------------------------------------------------------------------------ - -jpl_get_default_jvm_opts( Opts) :- - jni_get_default_jvm_opts( Opts). - -%------------------------------------------------------------------------------ - -jpl_set_default_jvm_opts( Opts) :- - is_list( Opts), - length( Opts, N), - jni_set_default_jvm_opts( N, Opts). - -%------------------------------------------------------------------------------ - -jpl_get_actual_jvm_opts( Opts) :- - jni_get_actual_jvm_opts( Opts). - -%------------------------------------------------------------------------------ - -jpl_assert( Fact) :- - ( jpl_assert_policy( Fact, yes) - -> assert( Fact) - ; true - ). - -%------------------------------------------------------------------------------ - -jpl_assert_policy( jpl_field_spec_cache(_,_,_,_,_,_), yes). -jpl_assert_policy( jpl_method_spec_cache(_,_,_,_,_,_,_,_), yes). -jpl_assert_policy( jpl_class_tag_type_cache(_,_), yes). -jpl_assert_policy( jpl_classname_type_cache(_,_), yes). -jpl_assert_policy( jpl_iref_type_cache(_,_), no). % must correspond to JPL_CACHE_TYPE_OF_REF in jpl.c - -jpl_assert_policy( jpl_field_spec_is_cached(_), YN) :- - jpl_assert_policy( jpl_field_spec_cache(_,_,_,_,_,_), YN). -jpl_assert_policy( jpl_method_spec_is_cached(_), YN) :- - jpl_assert_policy( jpl_method_spec_cache(_,_,_,_,_,_,_,_), YN). - -%------------------------------------------------------------------------------ - -% jpl_tidy_iref_type_cache( +Iref) :- -% delete the cached type info, if any, under Iref; -% called from jpl.c's jni_free_iref() via jni_tidy_iref_type_cache() - -jpl_tidy_iref_type_cache( Iref) :- - % write( '[decaching types for iref='), write( Iref), write( ']'), nl, - retractall( jpl_iref_type_cache(Iref,_)), - true. - -%------------------------------------------------------------------------------ - -% jpl_call(+X, +MethodSpec, +Params, -Result) :- -% X should be: -% an object reference -% (for static or instance methods) -% a classname, descriptor or type -% (for static methods of the denoted class) -% -% MethodSpec should be: -% a method name (as an atom) -% (may involve dynamic overload resolution based on inferred types of params) -% -% Params should be: -% a proper list (perhaps empty) of suitable actual parameters for the named method -% -% finally, an attempt will be made to unify Result with the returned result - -jpl_call(X, Mspec, Params, R) :- - ( jpl_object_to_type(X, Type) % the usual case (goal fails safely if X is var or rubbish) - -> Obj = X, - Kind = instance - ; var(X) - -> throw(error(instantiation_error, - context(jpl_call/4, - '1st arg must be bound to an object, classname, descriptor or type'))) - ; atom(X) - -> ( jpl_classname_to_type( X, Type) % does this attempt to load the class? - -> ( jpl_type_to_class( Type, ClassObj) - -> Kind = static - ; throw(error(existence_error(class,X), - context(jpl_call/4, - 'the named class cannot be found'))) - ) - ; throw(error(type_error(class_name_or_descriptor,X), - context(jpl_call/4, '1st arg must be an object, classname, descriptor or type'))) - ) - ; X = class(_,_) - -> Type = X, - jpl_type_to_class( Type, ClassObj), - Kind = static - ; X = array(_) - -> throw(error(type_error(object_or_class,X), - context(jpl_call/4, 'cannot call a static method of an array type, as none exists'))) - ; throw(error(domain_error(object_or_class,X), - context(jpl_call/4, - '1st arg must be an object, classname, descriptor or type'))) - ), - ( atom(Mspec) % the usual case, i.e. a method name - -> true - ; var(Mspec) - -> throw(error(instantiation_error, - context(jpl_call/4, '2nd arg must be an atom naming a public method of the class or object'))) - ; throw(error(type_error(method_name,Mspec), - context(jpl_call/4, '2nd arg must be an atom naming a public method of the class or object'))) - ), - ( is_list(Params) - -> ( catch( - jpl_datums_to_types(Params, Taps), - error(type_error(acyclic,Te),context(jpl_datum_to_type/2,Msg)), - throw(error(type_error(acyclic,Te),context(jpl_call/4,Msg))) - ) - -> true - ; throw(error(type_error(method_params,Params), - context(jpl_call/4, 'not all actual parameters are convertible to Java values or references'))) - ), - length( Params, A) - ; var(Params) - -> throw(error(instantiation_error, - context(jpl_call/4, '3rd arg must be a proper list of actual parameters for the named method'))) - ; throw(error(type_error(method_params,Params), - context(jpl_call/4, '3rd arg must be a proper list of actual parameters for the named method'))) - ), - ( Kind == instance - -> jpl_call_instance(Type, Obj, Mspec, Params, Taps, A, Rx) - ; jpl_call_static(Type, ClassObj, Mspec, Params, Taps, A, Rx) - ), - ( nonvar(R), - R = {Term} % yucky way of requesting Term->term conversion - -> ( jni_jref_to_term( Rx, TermX) % fails if Rx isn't a JRef to a jpl.Term - -> Term = TermX - ; throw(error(type_error, - context(jpl_call/4, 'result is not a jpl.Term instance as required'))) - ) - ; R = Rx - ). - -%------------------------------------------------------------------------------ - -%% jpl_call_instance(+ObjectType, +Object, +MethodName, Params, -%% ActualParamTypes, Arity, -Result) -% -% call the MethodName-d method (instance or static) of Object -% (which is of ObjectType), which most specifically applies to -% Params, which we have found to be (respectively) of -% ActualParamTypes, and of which there are Arity, yielding Result - -jpl_call_instance(Type, Obj, Mname, Params, Taps, A, Rx) :- - findall( % get remaining details of all accessible methods of Obj's class (as denoted by Type) - z5(I,Mods,MID,Tr,Tfps), - jpl_method_spec(Type, I, Mname, A, Mods, MID, Tr, Tfps), - Z5s - ), - ( Z5s = [] - -> throw(error(existence_error(method,Mname/A), - context(jpl_call/4, - 'the class or object has no public methods with the given name and quantity of parameters'))) - ; findall( - z5(I,Mods,MID,Tr,Tfps), % those to which Params is assignable - ( member(z5(I,Mods,MID,Tr,Tfps), Z5s), - jpl_types_fit_types(Taps, Tfps) % assignability test: actual param types "fit" formal param types - ), - Z5sA % Params-assignable methods - ), - ( Z5sA == [] - -> throw(error(type_error(method_params,Params), - context(jpl_call/4, - 'the actual parameters are not assignable to the formal parameters of any of the named methods'))) - - ; Z5sA = [z5(I,Mods,MID,Tr,Tfps)] - -> true % exactly one applicable method - ; jpl_z5s_to_most_specific_z5(Z5sA, z5(I,Mods,MID,Tr,Tfps)) - -> true % exactly one most-specific applicable method - ; throw(error(existence_error(most_specific_method,Mname/Params), - context(jpl_call/4, - 'more than one most-specific method is found for the actual parameters (this should not happen)'))) - ) - ), - ( member(static, Mods) % if the chosen method is static - -> jpl_object_to_class(Obj, ClassObj), % get a java.lang.Class instance which personifies Obj's class - jpl_call_static_method(Tr, ClassObj, MID, Tfps, Params, Rx) % call static method w.r.t. associated Class object - ; jpl_call_instance_method(Tr, Obj, MID, Tfps, Params, Rx) % else call (non-static) method w.r.t. object itself - ). - -%------------------------------------------------------------------------------ - -%% jpl_call_static(+ClassType, +ClassObject, +MethodName, Params, -%% ActualParamTypes, Arity, -Result) -% -% call the MethodName-d static method of the class (which is of -% ClassType, and which is represented by the java.lang.Class -% instance ClassObject) which most specifically applies to Params, -% which we have found to be (respectively) of ActualParamTypes, -% and of which there are Arity, yielding Result - -jpl_call_static(Type, ClassObj, Mname, Params, Taps, A, Rx) :- - findall( % get all accessible static methods of the class denoted by Type and ClassObj - z5(I,Mods,MID,Tr,Tfps), - ( jpl_method_spec(Type, I, Mname, A, Mods, MID, Tr, Tfps), - member(static, Mods) - ), - Z5s - ), - ( Z5s = [] - -> throw(error(existence_error(method,Mname/A), - context(jpl_call/4, - 'the class has no public static methods with the given name and quantity of parameters'))) - ; findall( - z5(I,Mods,MID,Tr,Tfps), - ( member(z5(I,Mods,MID,Tr,Tfps), Z5s), - jpl_types_fit_types(Taps, Tfps) % assignability test: actual param types "fit" formal param types - ), - Z5sA % Params-assignable methods - ), - ( Z5sA == [] - -> throw(error(type_error(method_params,Params), - context(jpl_call/4, - 'the actual parameters are not assignable to the formal parameters of any of the named methods'))) - ; Z5sA = [z5(I,Mods,MID,Tr,Tfps)] - -> true % exactly one applicable method - ; jpl_z5s_to_most_specific_z5(Z5sA, z5(I,Mods,MID,Tr,Tfps)) - -> true % exactly one most-specific applicable method - ; throw(error(existence_error(most_specific_method,Mname/Params), - context(jpl_call/4, - 'more than one most-specific method is found for the actual parameters (this should not happen)'))) - ) - ), - jpl_call_static_method(Tr, ClassObj, MID, Tfps, Params, Rx). - -%------------------------------------------------------------------------------ - -% jpl_call_instance_method(+Type, +ClassObject, +MethodID, +FormalParamTypes, +Params, -Result) :- - -jpl_call_instance_method(void, Class, MID, Tfps, Ps, R) :- - jCallVoidMethod(Class, MID, Tfps, Ps), - jpl_void(R). - -jpl_call_instance_method(boolean, Class, MID, Tfps, Ps, R) :- - jCallBooleanMethod(Class, MID, Tfps, Ps, R). - -jpl_call_instance_method(byte, Class, MID, Tfps, Ps, R) :- - jCallByteMethod(Class, MID, Tfps, Ps, R). - -jpl_call_instance_method(char, Class, MID, Tfps, Ps, R) :- - jCallCharMethod(Class, MID, Tfps, Ps, R). - -jpl_call_instance_method(short, Class, MID, Tfps, Ps, R) :- - jCallShortMethod(Class, MID, Tfps, Ps, R). - -jpl_call_instance_method(int, Class, MID, Tfps, Ps, R) :- - jCallIntMethod(Class, MID, Tfps, Ps, R). - -jpl_call_instance_method(long, Class, MID, Tfps, Ps, R) :- - jCallLongMethod(Class, MID, Tfps, Ps, R). - -jpl_call_instance_method(float, Class, MID, Tfps, Ps, R) :- - jCallFloatMethod(Class, MID, Tfps, Ps, R). - -jpl_call_instance_method(double, Class, MID, Tfps, Ps, R) :- - jCallDoubleMethod(Class, MID, Tfps, Ps, R). - -jpl_call_instance_method(array(_), Class, MID, Tfps, Ps, R) :- - jCallObjectMethod(Class, MID, Tfps, Ps, R). - -jpl_call_instance_method(class(_,_), Class, MID, Tfps, Ps, R) :- - jCallObjectMethod(Class, MID, Tfps, Ps, R). - -%------------------------------------------------------------------------------ - -% jpl_call_static_method(+Type, +ClassObject, +MethodID, +FormalParamTypes, +Params, -Result) :- - -jpl_call_static_method(void, Class, MID, Tfps, Ps, R) :- - jCallStaticVoidMethod(Class, MID, Tfps, Ps), - jpl_void(R). - -jpl_call_static_method(boolean, Class, MID, Tfps, Ps, R) :- - jCallStaticBooleanMethod(Class, MID, Tfps, Ps, R). - -jpl_call_static_method(byte, Class, MID, Tfps, Ps, R) :- - jCallStaticByteMethod(Class, MID, Tfps, Ps, R). - -jpl_call_static_method(char, Class, MID, Tfps, Ps, R) :- - jCallStaticCharMethod(Class, MID, Tfps, Ps, R). - -jpl_call_static_method(short, Class, MID, Tfps, Ps, R) :- - jCallStaticShortMethod(Class, MID, Tfps, Ps, R). - -jpl_call_static_method(int, Class, MID, Tfps, Ps, R) :- - jCallStaticIntMethod(Class, MID, Tfps, Ps, R). - -jpl_call_static_method(long, Class, MID, Tfps, Ps, R) :- - jCallStaticLongMethod(Class, MID, Tfps, Ps, R). - -jpl_call_static_method(float, Class, MID, Tfps, Ps, R) :- - jCallStaticFloatMethod(Class, MID, Tfps, Ps, R). - -jpl_call_static_method(double, Class, MID, Tfps, Ps, R) :- - jCallStaticDoubleMethod(Class, MID, Tfps, Ps, R). - -jpl_call_static_method(array(_), Class, MID, Tfps, Ps, R) :- - jCallStaticObjectMethod(Class, MID, Tfps, Ps, R). - -jpl_call_static_method(class(_,_), Class, MID, Tfps, Ps, R) :- - jCallStaticObjectMethod(Class, MID, Tfps, Ps, R). - -%------------------------------------------------------------------------------ - -%type jpl_fergus_find_candidate(list(T), T, T, list(T)) - -jpl_fergus_find_candidate([], Candidate, Candidate, []). - -jpl_fergus_find_candidate([X|Xs], Candidate0, Candidate, Rest) :- - ( jpl_fergus_greater(X, Candidate0) - -> Candidate1 = X, - Rest = [Candidate0|Rest1] - ; Candidate1 = Candidate0, - Rest = [X|Rest1] - ), - jpl_fergus_find_candidate(Xs, Candidate1, Candidate, Rest1). - -%------------------------------------------------------------------------------ - -jpl_fergus_greater(z5(_,_,_,_,Tps1), z5(_,_,_,_,Tps2)) :- - jpl_types_fit_types(Tps1, Tps2). -jpl_fergus_greater(z3(_,_,Tps1), z3(_,_,Tps2)) :- - jpl_types_fit_types(Tps1, Tps2). - -%------------------------------------------------------------------------------ - -%type jpl_fergus_is_the_greatest(list(T), T) - -%% jpl_fergus_is_the_greatest(Xs, GreatestX) -% -% Xs is a list of things for which jpl_fergus_greater/2 defines a -% partial ordering; GreatestX is one of those, than which none is -% greater; fails if there is more than one such; this algorithm -% was contributed to c.l.p by Fergus Henderson in response to my -% "there must be a better way" challenge: there was, this is it - -jpl_fergus_is_the_greatest([X|Xs], Greatest) :- - jpl_fergus_find_candidate(Xs, X, Greatest, Rest), - forall( - member(R, Rest), - jpl_fergus_greater(Greatest, R) - ). - -%------------------------------------------------------------------------------ - -%% jpl_get(+X, +Fspec, -V) -% -% X can be: -% * a classname, a descriptor, or an (object or array) type -% (for static fields); -% * a non-array object -% (for static and non-static fields) -% * an array -% (for 'length' pseudo field, or indexed element retrieval), -% but not: -% * a String -% (clashes with class name; anyway, String has no fields to retrieve) -% -% Fspec can be: -% * an atomic field name, -% * or an integral array index (to get an element from an array, -% * or a pair I-J of integers (to get a subrange (slice?) of an -% array) -% -% finally, an attempt will be made to unify V with the retrieved value - -jpl_get(X, Fspec, V) :- - ( jpl_object_to_type(X, Type) - -> Obj = X, - jpl_get_instance( Type, Type, Obj, Fspec, Vx) % pass Type twice for FAI - ; var(X) - -> throw(error(instantiation_error, - context(jpl_get/3, - '1st arg must be bound to an object, classname, descriptor or type'))) - ; jpl_is_type(X) % e.g. class([java,lang],['String']), array(int) - -> Type = X, - ( jpl_type_to_class(Type, ClassObj) - -> jpl_get_static( Type, ClassObj, Fspec, Vx) - ; jpl_type_to_classname( Type, Classname), - throw(error(existence_error(class,Classname), - context(jpl_get/3, - 'the named class cannot be found'))) - ) - ; atom(X) - -> ( jpl_classname_to_type( X, Type) % does this attempt to load the class? - -> ( jpl_type_to_class( Type, ClassObj) - -> jpl_get_static( Type, ClassObj, Fspec, Vx) - ; throw(error(existence_error(class,X), - context(jpl_get/3, - 'the named class cannot be found'))) - ) - ; throw(error(type_error(class_name_or_descriptor,X), - context(jpl_get/3, '1st arg must be an object, classname, descriptor or type'))) - ) - - ; throw(error(domain_error(object_or_class,X), - context(jpl_get/3, - '1st arg must be bound to an object, classname, descriptor or type'))) - ), - ( nonvar(V), - V = {Term} % yucky way of requesting Term->term conversion - -> ( jni_jref_to_term( Vx, TermX) % fails if Rx is not a JRef to a jpl.Term - -> Term = TermX - ; throw(error(type_error, - context(jpl_call/4, 'result is not a jpl.Term instance as required'))) - ) - ; V = Vx - ). - -%------------------------------------------------------------------------------ - -%% jpl_get_static(+Type, +ClassObject, +FieldName, -Value) -% -% ClassObject is an instance of java.lang.Class which represents -% the same class as Type; Value (Vx below) is guaranteed unbound -% on entry, and will, before exit, be unified with the retrieved -% value - -jpl_get_static(Type, ClassObj, Fname, Vx) :- - ( atom(Fname) % assume it's a field name - -> true - ; var(Fname) - -> throw(error(instantiation_error, - context(jpl_get/3, '2nd arg must be bound to an atom naming a public field of the class'))) - ; throw(error(type_error(field_name,Fname), - context(jpl_get/3, '2nd arg must be an atom naming a public field of the class'))) - ), - % get static fields of the denoted class - findall( - z4(I,Mods,FID,Tf), - ( jpl_field_spec(Type, I, Fname, Mods, FID, Tf), - member(static, Mods) - ), - Z4s - ), - ( Z4s = [] - -> throw(error(existence_error(field,Fname), - context(jpl_get/3, - 'the class or object has no public static field with the given name'))) - ; Z4s = [z4(I,_Mods,FID,Tf)] - -> jpl_get_static_field(Tf, ClassObj, FID, Vx) - ; throw(error(existence_error(unique_field,Fname), - context(jpl_get/3, - 'more than one field is found with the given name'))) - ). - -%------------------------------------------------------------------------------ - -% jpl_get_instance(+Type, +Type, +Object, +FieldSpecifier, -Value) :- - -jpl_get_instance(class(_,_), Type, Obj, Fname, Vx) :- - ( atom(Fname) % the usual case - -> true - ; var(Fname) - -> throw(error(instantiation_error, - context(jpl_get/3, '2nd arg must be bound to an atom naming a public field of the class or object'))) - ; throw(error(type_error(field_name,Fname), - context(jpl_get/3, '2nd arg must be an atom naming a public field of the class or object'))) - ), - findall(z4(I,Mods,FID,Tf), - jpl_field_spec(Type, I, Fname, Mods, FID, Tf), - Z4s), - ( Z4s = [] - -> throw(error(existence_error(field,Fname), - context(jpl_get/3, - 'the class or object has no public field with the given name'))) - ; Z4s = [z4(I,Mods,FID,Tf)] - -> ( member(static, Mods) - -> jpl_object_to_class(Obj, ClassObj), - jpl_get_static_field(Tf, ClassObj, FID, Vx) - ; jpl_get_instance_field(Tf, Obj, FID, Vx) - ) - ; throw(error(existence_error(unique_field,Fname), - context(jpl_get/3, - 'more than one field is found with the given name'))) - ). - -jpl_get_instance(array(ElementType), _, Array, Fspec, Vx) :- - ( var(Fspec) - -> throw(error(instantiation_error, - context(jpl_get/3, - 'when 1st arg is an array, 2nd arg must be bound to an index, an index range, or ''length'''))) - ; integer(Fspec) - -> ( Fspec < 0 % lo bound check - -> throw(error(domain_error(array_index,Fspec), - context(jpl_get/3, - 'when 1st arg is an array, integral 2nd arg must be non-negative'))) - ; jGetArrayLength(Array, Len), - Fspec >= Len % hi bound check - -> throw(error(domain_error(array_index,Fspec), - context(jpl_get/3, - 'when 1st arg is an array, integral 2nd arg must not exceed upper bound of array'))) - ; jpl_get_array_element(ElementType, Array, Fspec, Vx) - ) - ; Fspec = N-M % NB should we support e.g. 3-2 -> [] ? - -> ( integer(N), - integer(M) - -> ( N >= 0, - M >= N - -> jGetArrayLength(Array, Len), - ( N >= Len - -> throw(error(domain_error(array_index_range,N-M), - context(jpl_get/3, - 'lower bound of array index range must not exceed upper bound of array'))) - ; M >= Len - -> throw(error(domain_error(array_index_range,N-M), - context(jpl_get/3, - 'upper bound of array index range must not exceed upper bound of array'))) - ; jpl_get_array_elements(ElementType, Array, N, M, Vx) - ) - ; throw(error(domain_error(array_index_range,N-M), - context(jpl_get/3, - 'array index range must be a non-decreasing pair of non-negative integers'))) - ) - ; throw(error(type_error(array_index_range,N-M), - context(jpl_get/3, - 'array index range must be a non-decreasing pair of non-negative integers'))) - ) - ; atom(Fspec) - -> ( Fspec == length % special-case for this solitary array "method" - -> jGetArrayLength(Array, Vx) - ; throw(error(domain_error(array_field_name,Fspec), - context(jpl_get/3, - 'the array has no public field with the given name'))) - ) - ; throw(error(type_error(array_lookup_spec,Fspec), - context(jpl_get/3, - 'when 1st arg is an array, 2nd arg must be an index, an index range, or ''length'''))) - ). - -%------------------------------------------------------------------------------ - -%% jpl_get_array_element(+ElementType, +Array, +Index, -Vc) -% -% Array is (a reference to) an array of ElementType; Vc is -% (unified with a JPL repn of) its Index-th (numbered from 0) -% element Java values are now converted to Prolog terms within -% foreign code -% -% @tbd more of this could be done within foreign code ... - -jpl_get_array_element(Type, Array, Index, Vc) :- - ( ( Type = class(_,_) - ; Type = array(_) - ) - -> jGetObjectArrayElement(Array, Index, Vr) - ; jpl_primitive_type(Type) - -> jni_type_to_xput_code(Type, Xc), - jni_alloc_buffer(Xc, 1, Bp), % one-element buf for a Type - jpl_get_primitive_array_region(Type, Array, Index, 1, Bp), - jni_fetch_buffer_value(Bp, 0, Vr, Xc), % zero-th element - jni_free_buffer(Bp) - ), - Vr = Vc. % redundant since Vc is always (?) unbound at call - -%------------------------------------------------------------------------------ - -%% jpl_get_array_elements(+ElementType, +Array, +N, +M, -Vs) -% -% serves only jpl_get_instance Vs will always be unbound on entry - -jpl_get_array_elements(ElementType, Array, N, M, Vs) :- - ( ( ElementType = class(_,_) - ; ElementType = array(_) - ) - -> jpl_get_object_array_elements(Array, N, M, Vs) - ; jpl_get_primitive_array_elements(ElementType, Array, N, M, Vs) - ). - -%------------------------------------------------------------------------------ - -jpl_get_instance_field(boolean, Obj, FieldID, V) :- - jGetBooleanField(Obj, FieldID, V). -jpl_get_instance_field(byte, Obj, FieldID, V) :- - jGetByteField(Obj, FieldID, V). -jpl_get_instance_field(char, Obj, FieldID, V) :- - jGetCharField(Obj, FieldID, V). -jpl_get_instance_field(short, Obj, FieldID, V) :- - jGetShortField(Obj, FieldID, V). -jpl_get_instance_field(int, Obj, FieldID, V) :- - jGetIntField(Obj, FieldID, V). -jpl_get_instance_field(long, Obj, FieldID, V) :- - jGetLongField(Obj, FieldID, V). -jpl_get_instance_field(float, Obj, FieldID, V) :- - jGetFloatField(Obj, FieldID, V). -jpl_get_instance_field(double, Obj, FieldID, V) :- - jGetDoubleField(Obj, FieldID, V). -jpl_get_instance_field(class(_,_), Obj, FieldID, V) :- - jGetObjectField(Obj, FieldID, V). -jpl_get_instance_field(array(_), Obj, FieldID, V) :- - jGetObjectField(Obj, FieldID, V). - -%------------------------------------------------------------------------------ - -%% jpl_get_object_array_elements(+Array, +LoIndex, +HiIndex, -Vcs) -% -% Array should be a (zero-based) array of some object (array or -% non-array) type; LoIndex is an integer, 0 =< LoIndex < -% length(Array); HiIndex is an integer, LoIndex-1 =< HiIndex < -% length(Array); at call, Vcs will be unbound; at exit, Vcs will -% be a list of (references to) the array's elements -% [LoIndex..HiIndex] inclusive - -jpl_get_object_array_elements(Array, Lo, Hi, Vcs) :- - ( Lo =< Hi - -> Vcs = [Vc|Vcs2], - jGetObjectArrayElement(Array, Lo, Vc), - Next is Lo+1, - jpl_get_object_array_elements(Array, Next, Hi, Vcs2) - ; Vcs = [] - ). - -%------------------------------------------------------------------------------ - -%% jpl_get_primitive_array_elements(+ElementType, +Array, +LoIndex, +HiIndex, -Vcs) -% -% Array should be a (zero-based) Java array of (primitive) -% ElementType; Vcs should be unbound on entry, and on exit will be -% a list of (JPL representations of the values of) the elements -% [LoIndex..HiIndex] inclusive - -jpl_get_primitive_array_elements(ElementType, Array, Lo, Hi, Vcs) :- - Size is Hi-Lo+1, - ( Size == 0 - -> Vcs = [] - ; jni_type_to_xput_code(ElementType, Xc), - jni_alloc_buffer(Xc, Size, Bp), - jpl_get_primitive_array_region(ElementType, Array, Lo, Size, Bp), - jpl_primitive_buffer_to_array(ElementType, Xc, Bp, 0, Size, Vcs), - jni_free_buffer(Bp) - ). - -%------------------------------------------------------------------------------ - -jpl_get_primitive_array_region(boolean, Array, Lo, S, I) :- - jGetBooleanArrayRegion(Array, Lo, S, jbuf(I,boolean)). -jpl_get_primitive_array_region(byte, Array, Lo, S, I) :- - jGetByteArrayRegion(Array, Lo, S, jbuf(I,byte)). -jpl_get_primitive_array_region(char, Array, Lo, S, I) :- - jGetCharArrayRegion(Array, Lo, S, jbuf(I,char)). -jpl_get_primitive_array_region(short, Array, Lo, S, I) :- - jGetShortArrayRegion(Array, Lo, S, jbuf(I,short)). -jpl_get_primitive_array_region(int, Array, Lo, S, I) :- - jGetIntArrayRegion(Array, Lo, S, jbuf(I,int)). -jpl_get_primitive_array_region(long, Array, Lo, S, I) :- - jGetLongArrayRegion(Array, Lo, S, jbuf(I,long)). -jpl_get_primitive_array_region(float, Array, Lo, S, I) :- - jGetFloatArrayRegion(Array, Lo, S, jbuf(I,float)). -jpl_get_primitive_array_region(double, Array, Lo, S, I) :- - jGetDoubleArrayRegion(Array, Lo, S, jbuf(I,double)). - -%------------------------------------------------------------------------------ - -jpl_get_static_field(boolean, Array, FieldID, V) :- - jGetStaticBooleanField(Array, FieldID, V). -jpl_get_static_field(byte, Array, FieldID, V) :- - jGetStaticByteField(Array, FieldID, V). -jpl_get_static_field(char, Array, FieldID, V) :- - jGetStaticCharField(Array, FieldID, V). -jpl_get_static_field(short, Array, FieldID, V) :- - jGetStaticShortField(Array, FieldID, V). -jpl_get_static_field(int, Array, FieldID, V) :- - jGetStaticIntField(Array, FieldID, V). -jpl_get_static_field(long, Array, FieldID, V) :- - jGetStaticLongField(Array, FieldID, V). -jpl_get_static_field(float, Array, FieldID, V) :- - jGetStaticFloatField(Array, FieldID, V). -jpl_get_static_field(double, Array, FieldID, V) :- - jGetStaticDoubleField(Array, FieldID, V). -jpl_get_static_field(class(_,_), Array, FieldID, V) :- - jGetStaticObjectField(Array, FieldID, V). -jpl_get_static_field(array(_), Array, FieldID, V) :- - jGetStaticObjectField(Array, FieldID, V). - -%------------------------------------------------------------------------------ - -%% jpl_new(+X, +Params, -V) -% -% X can be: -% * an atomic classname -% e.g. 'java.lang.String' -% * an atomic descriptor -% e.g. '[I' or 'Ljava.lang.String;' -% * a suitable type -% i.e. any class(_,_) or array(_) -% -% if X is an object (non-array) type or descriptor and Params is a -% list of values or references, then V is the result of an invocation -% of that type's most specifically-typed constructor to whose -% respective formal parameters the actual Params are assignable (and -% assigned) -% -% if X is an array type or descriptor and Params is a list of values -% or references, each of which is (independently) assignable to the -% array element type, then V is a new array of as many elements as -% Params has members, initialised with the respective members of -% Params; -% -% if X is an array type or descriptor and Params is a non-negative -% integer N, then V is a new array of that type, with N elements, each -% initialised to Java's appropriate default value for the type; -% -% If V is {Term} then we attempt to convert a new jpl.Term instance to -% a corresponding term; this is of little obvious use here, but is -% consistent with jpl_call/4 and jpl_get/3 - -jpl_new(X, Params, V) :- - ( var(X) - -> throw(error(instantiation_error, - context(jpl_new/3, - '1st arg must be bound to a classname, descriptor or object type'))) - ; jpl_is_type(X) % NB only class(_,_) or array(_) - -> Type = X - ; atom(X) % e.g. 'java.lang.String', '[L', 'boolean' - -> ( jpl_classname_to_type(X, Type) - -> true - ; throw(error(domain_error(classname,X), - context(jpl_new/3, - 'if 1st arg is an atom, it must be a classname or descriptor'))) - ) - ; throw(error(type_error(instantiable,X), - context(jpl_new/3, - '1st arg must be a classname, descriptor or object type'))) - ), - jpl_new_1(Type, Params, Vx), - ( nonvar(V), - V = {Term} % yucky way of requesting Term->term conversion - -> ( jni_jref_to_term( Vx, TermX) % fails if Rx is not a JRef to a jpl.Term - -> Term = TermX - ; throw(error(type_error, - context(jpl_call/4, 'result is not a jpl.Term instance as required'))) - ) - ; V = Vx - ). - -%------------------------------------------------------------------------------ - -% jpl_new_1(+Tx, +Params, -Vx) :- -% (serves only jpl_new/3) -% -% Tx can be: -% a class(_,_) or array(_) type; -% -% Params must be: -% a proper list of constructor parameters -% -% at exit, Vx is bound to a JPL reference to a new, initialised instance of Tx - -jpl_new_1(class(Ps,Cs), Params, Vx) :- - !, % green (see below) - Tx = class(Ps,Cs), - ( var(Params) - -> throw(error(instantiation_error, - context(jpl_new/3, - '2nd arg must be a proper list of valid parameters for a constructor'))) - ; \+ is_list(Params) - -> throw(error(type_error(list,Params), - context(jpl_new/3, - '2nd arg must be a proper list of valid parameters for a constructor'))) - ; true - ), - length(Params, A), % the "arity" of the required constructor - jpl_type_to_class(Tx, Cx), % throws Java exception if class is not found - N = '', % JNI's constructor naming convention for GetMethodID() - Tr = void, % all constructors have this return "type" - findall( - z3(I,MID,Tfps), - jpl_method_spec(Tx, I, N, A, _Mods, MID, Tr, Tfps), % cached - Z3s - ), - ( Z3s == [] % no constructors which require the given qty of parameters? - -> jpl_type_to_classname( Tx, Cn), - ( jpl_call( Cx, isInterface, [], @(true)) - -> throw(error(type_error(concrete_class,Cn), - context(jpl_new/3, - 'cannot create instance of an interface'))) - ; throw(error(existence_error(constructor,Cn/A), - context(jpl_new/3, - 'no constructor found with the corresponding quantity of parameters'))) - ) - ; ( catch( - jpl_datums_to_types(Params, Taps), % infer actual parameter types - error(type_error(acyclic,Te),context(jpl_datum_to_type/2,Msg)), - throw(error(type_error(acyclic,Te),context(jpl_new/3,Msg))) - ) - -> true - ; throw(error(domain_error(list(jpl_datum),Params), - context(jpl_new/3, - 'one or more of the actual parameters is not a valid representation of any Java value or object'))) - ), - findall( - z3(I,MID,Tfps), % select constructors to which actual parameters are assignable - ( member(z3(I,MID,Tfps), Z3s), - jpl_types_fit_types(Taps, Tfps) % assignability test: actual parameter types "fit" formal parameter types? - ), - Z3sA - ), - ( Z3sA == [] % no type-assignable constructors? - -> ( Z3s = [_] - -> throw(error(existence_error(constructor,Tx/A), - context(jpl_new/3, - 'the actual parameters are not assignable to the formal parameter types of the only constructor which takes this qty of parameters'))) - ; throw(error(type_error(constructor_args,Params), - context(jpl_new/3, - 'the actual parameters are not assignable to the formal parameter types of any of the constructors which take this qty of parameters'))) - ) - ; Z3sA = [z3(I,MID,Tfps)] - -> true - ; jpl_z3s_to_most_specific_z3(Z3sA, z3(I,MID,Tfps)) - -> true - ; throw(error(type_error(constructor_params,Params), - context(jpl_new/3, - 'more than one most-specific matching constructor (shouldn''t happen)'))) - ) - ), - catch( - jNewObject(Cx, MID, Tfps, Params, Vx), - error(java_exception(@(_)), 'java.lang.InstantiationException'), - ( jpl_type_to_classname( Tx, Cn), - throw(error(type_error(concrete_class,Cn), - context(jpl_new/3, - 'cannot create instance of an abstract class'))) - ) - ), - jpl_cache_type_of_ref(Tx, Vx). % since we know it - -jpl_new_1(array(T), Params, Vx) :- - !, - ( var(Params) - -> throw(error(instantiation_error, - context(jpl_new/3, - 'when constructing a new array, 2nd arg must either be a non-negative integer (denoting the required array length) or a proper list of valid element values'))) - ; integer(Params) % integer I -> array[0..I-1] of default values - -> ( Params >= 0 - -> Len is Params - ; throw(error(domain_error(array_length,Params), - context(jpl_new/3, - 'when constructing a new array, if the 2nd arg is an integer (denoting the required array length) then it must be non-negative'))) - ) - ; is_list(Params) % [V1,..VN] -> array[0..N-1] of respective values - -> length(Params, Len) - ), - jpl_new_array(T, Len, Vx), % NB may throw out-of-memory exception - ( nth0(I, Params, Param), % nmember fails silently when Params is integer - jpl_set(Vx, I, Param), - fail - ; true - ), - jpl_cache_type_of_ref(array(T), Vx). % since we know it - -jpl_new_1(T, _Params, _Vx) :- % doomed attempt to create new primitive type instance (formerly a dubious completist feature :-) - jpl_primitive_type(T), - !, - throw(error(domain_error(object_type,T), - context(jpl_new/3, - 'cannot construct an instance of a primitive type'))). - % ( var(Params) - % -> throw(error(instantiation_error, - % context(jpl_new/3, - % 'when constructing a new instance of a primitive type, 2nd arg must be bound (to a representation of a suitable value)'))) - % ; Params == [] - % -> jpl_primitive_type_default_value(T, Vx) - % ; Params = [Param] - % -> jpl_primitive_type_term_to_value(T, Param, Vx) - % ; throw(error(domain_error(constructor_args,Params), - % context(jpl_new/3, - % 'when constructing a new instance of a primitive type, 2nd arg must either be an empty list (indicating that the default value of that type is required) or a list containing exactly one representation of a suitable value)'))) - % ). - -jpl_new_1( T, _, _) :- - throw(error(domain_error(jpl_type,T), - context(jpl_new/3, - '1st arg must denote a known or plausible type'))). - -%------------------------------------------------------------------------------ - -% jpl_new_array(+ElementType, +Length, -NewArray) :- - -jpl_new_array(boolean, Len, A) :- - jNewBooleanArray(Len, A). - -jpl_new_array(byte, Len, A) :- - jNewByteArray(Len, A). - -jpl_new_array(char, Len, A) :- - jNewCharArray(Len, A). - -jpl_new_array(short, Len, A) :- - jNewShortArray(Len, A). - -jpl_new_array(int, Len, A) :- - jNewIntArray(Len, A). - -jpl_new_array(long, Len, A) :- - jNewLongArray(Len, A). - -jpl_new_array(float, Len, A) :- - jNewFloatArray(Len, A). - -jpl_new_array(double, Len, A) :- - jNewDoubleArray(Len, A). - -jpl_new_array(array(T), Len, A) :- - jpl_type_to_class(array(T), C), - jNewObjectArray(Len, C, @(null), A). % initialise each element to null - -jpl_new_array(class(Ps,Cs), Len, A) :- - jpl_type_to_class(class(Ps,Cs), C), - jNewObjectArray(Len, C, @(null), A). - -%------------------------------------------------------------------------------ - -% jpl_set(+X, +Fspec, +V) :- -% basically, sets the Fspec-th field of class or object X to value V -% iff it is assignable -% -% X can be: -% a class instance -% (for static or non-static fields) -% an array -% (for indexed element or subrange assignment) -% a classname, or a class/2 or array/1 type -% (for static fields) -% but not: -% a String (no fields to retrieve) -% -% Fspec can be: -% an atomic field name -% (overloading through shadowing has yet to be handled properly) -% an array index I -% (X must be an array object: V is assigned to X[I]) -% a pair I-J of integers -% (X must be an array object, V must be a list of values: successive members of V are assigned to X[I..J]) -% -% V must be a suitable value or object - -jpl_set(X, Fspec, V) :- - ( jpl_object_to_type(X, Type) % the usual case (test is safe if X is var or rubbish) - -> Obj = X, - catch( - jpl_set_instance(Type, Type, Obj, Fspec, V), % first 'Type' is for FAI - error(type_error(acyclic,Te),context(jpl_datum_to_type/2,Msg)), - throw(error(type_error(acyclic,Te),context(jpl_set/3,Msg))) - ) - ; var(X) - -> throw(error(instantiation_error, - context(jpl_set/3, - '1st arg must be an object, classname, descriptor or type'))) - ; ( atom(X) - -> ( jpl_classname_to_type(X, Type) % it's a classname or descriptor... - -> true - ; throw(error(existence_error(class,X), - context(jpl_set/3, - 'the named class cannot be found'))) - ) - ; ( X = class(_,_) % it's a class type... - ; X = array(_) % ...or an array type - ) - -> Type = X - ), - ( jpl_type_to_class( Type, ClassObj) % ...whose Class object is available - -> true - ; jpl_type_to_classname( Type, Classname), - throw(error(existence_error(class,Classname), - context(jpl_set/3, - 'the class cannot be found'))) - ) - -> catch( - jpl_set_static(Type, ClassObj, Fspec, V), - error(type_error(acyclic,Te),context(jpl_datum_to_type/2,Msg)), - throw(error(type_error(acyclic,Te),context(jpl_set/3,Msg))) - ) - ; throw(error(domain_error(object_or_class,X), - context(jpl_set/3, - '1st arg must be an object, classname, descriptor or type'))) - ). - -%------------------------------------------------------------------------------ - -% jpl_set_instance(+Type, +Type, +ObjectReference, +FieldName, +Value) :- -% ObjectReference is a JPL reference to a Java object -% of the class denoted by Type (which is passed twice for first agument indexing); -% FieldName should name a public, non-final (static or non-static) field of this object, -% but could be anything, and is validated here; -% Value should be assignable to the named field, but could be anything, and is validated here - -jpl_set_instance(class(_,_), Type, Obj, Fname, V) :- % a non-array object - ( atom(Fname) % the usual case - -> true - ; var(Fname) - -> throw(error(instantiation_error, - context(jpl_set/3, - '2nd arg must be bound to the name of a public, non-final field'))) - ; throw(error(type_error(field_name,Fname), - context(jpl_set/3, - '2nd arg must be the name of a public, non-final field'))) - ), - findall( - z4(I,Mods,FID,Tf), - jpl_field_spec(Type, I, Fname, Mods, FID, Tf), % public fields of class denoted by Type - Z4s - ), - ( Z4s = [] - -> throw(error(existence_error(field,Fname), - context(jpl_set/3, - 'no public fields of the object have this name'))) - ; Z4s = [z4(I,Mods,FID,Tf)] - -> ( member(final, Mods) - -> throw(error(permission_error(modify,final_field,Fname), - context(jpl_set/3, - 'cannot assign a value to a final field (actually you could but I''ve decided not to let you)'))) - ; jpl_datum_to_type( V, Tv) - -> ( jpl_type_fits_type( Tv, Tf) - -> ( member(static, Mods) - -> jpl_object_to_class(Obj, ClassObj), - jpl_set_static_field(Tf, ClassObj, FID, V) - ; jpl_set_instance_field(Tf, Obj, FID, V) % oughta be jpl_set_instance_field? - ) - ; jpl_type_to_nicename( Tf, NNf), - throw(error(type_error(NNf,V), - context(jpl_set/3, - 'the value is not assignable to the named field of the class'))) - ) - ; throw(error(type_error(field_value,V), - context(jpl_set/3, - '3rd arg does not represent any Java value or object'))) - ) - ; throw(error(existence_error(field,Fname), % 'existence'? or some other sort of error maybe? - context(jpl_set/3, - 'more than one public field of the object has this name (this should not happen)'))) - ). - - -jpl_set_instance(array(Type), _, Obj, Fspec, V) :- - ( is_list(V) % a list of array element values - -> Vs = V - ; var(V) - -> throw(error(instantiation_error, - context(jpl_set/3, 'when 1st arg is an array, 3rd arg must be bound to a suitable element value or list of values'))) - ; Vs = [V] % a single array element value - ), - length(Vs, Iv), - ( var(Fspec) - -> throw(error(instantiation_error, - context(jpl_set/3, - 'when 1st arg is an array, 2nd arg must be bound to an index or index range'))) - ; integer(Fspec) % single-element assignment - -> ( Fspec < 0 - -> throw(error(domain_error(array_index,Fspec), - context(jpl_set/3, - 'when 1st arg is an array, an integral 2nd arg must be a non-negative index'))) - ; Iv is 1 - -> N is Fspec - ; Iv is 0 - -> throw(error(domain_error(array_element(Fspec),Vs), - context(jpl_set/3, - 'no values for array element assignment: needs one'))) - ; throw(error(domain_error(array_element(Fspec),Vs), - context(jpl_set/3, - 'too many values for array element assignment: needs one'))) - ) - ; Fspec = N-M % element-sequence assignment - -> ( integer(N), - integer(M) - -> ( N >= 0, - Size is (M-N)+1, - Size >= 0 - -> ( Size == Iv - -> true - ; Size < Iv - -> throw(error(domain_error(array_elements(N-M),Vs), - context(jpl_set/3, - 'too few values for array range assignment'))) - ; throw(error(domain_error(array_elements(N-M),Vs), - context(jpl_set/3, - 'too many values for array range assignment'))) - ) - ; throw(error(domain_error(array_index_range,N-M), - context(jpl_set/3, - 'array index range must be a non-decreasing pair of non-negative integers'))) - ) - ; throw(error(type_error(array_index_range,N-M), - context(jpl_set/3, - 'array index range must be a non-decreasing pair of non-negative integers'))) - ) - ; atom(Fspec) - -> ( Fspec == length - -> throw(error(permission_error(modify,final_field,length), - context(jpl_set/3, - 'cannot assign a value to a final field'))) - ; throw(error(existence_error(field,Fspec), - context(jpl_set/3, - 'array has no field with that name'))) - ) - ; throw(error(domain_error(array_index,Fspec), - context(jpl_set/3, - 'when 1st arg is an array object, 2nd arg must be a non-negative index or index range'))) - ), - jpl_set_array(Type, Obj, N, Iv, Vs). - -%------------------------------------------------------------------------------ - -% jpl_set_static(+Type, +ClassObj, +FieldName, +Value) :- -% we can rely on: -% Type being a class/2 type representing some accessible class -% ClassObj being an instance of java.lang.Class which represents the same class as Type -% but FieldName could be anything, so we validate it here, -% look for a suitable (static) field of the target class, -% then call jpl_set_static_field/4 to attempt to assign Value (which could be anything) to it -% -% NB this does not yet handle shadowed fields correctly... - -jpl_set_static(Type, ClassObj, Fname, V) :- - ( atom(Fname) % the usual case - -> true - ; var(Fname) - -> throw(error(instantiation_error, - context(jpl_set/3, - 'when 1st arg denotes a class, 2nd arg must be bound to the name of a public, static, non-final field'))) - ; throw(error(type_error(field_name,Fname), - context(jpl_set/3, - 'when 1st arg denotes a class, 2nd arg must be the name of a public, static, non-final field'))) - ), - findall( % get all static fields of the denoted class - z4(I,Mods,FID,Tf), - ( jpl_field_spec(Type, I, Fname, Mods, FID, Tf), - member(static, Mods) - ), - Z4s - ), - ( Z4s = [] - -> throw(error(existence_error(field,Fname), - context(jpl_set/3, - 'class has no public static fields of this name'))) - ; Z4s = [z4(I,Mods,FID,Tf)] % exactly one synonymous field? - -> ( member(final, Mods) - -> throw(error(permission_error(modify,final_field,Fname), - context(jpl_set/3, - 'cannot assign a value to a final field'))) - ; jpl_datum_to_type(V, Tv) - -> ( jpl_type_fits_type(Tv, Tf) - -> jpl_set_static_field(Tf, ClassObj, FID, V) - ; jpl_type_to_nicename(Tf, NNf), - throw(error(type_error(NNf,V), - context(jpl_set/3, - 'the value is not assignable to the named field of the class'))) - ) - ; throw(error(type_error(field_value,V), - context(jpl_set/3, - '3rd arg does not represent any Java value or object'))) - ) - ; throw(error(existence_error(field,Fname), - context(jpl_set/3, - 'more than one public static field of the class has this name (this should not happen)(?)'))) - ). - -%------------------------------------------------------------------------------ - -%% jpl_set_array(+ElementType, +Array, +Offset, +DatumQty, +Datums) -% -% Datums, of which there are DatumQty, are stashed in successive -% elements of Array which is an array of ElementType starting at -% the Offset-th (numbered from 0) throws -% error(type_error(acyclic,_),context(jpl_datum_to_type/2,_)) - -jpl_set_array(T, A, N, I, Ds) :- - ( jpl_datums_to_types(Ds, Tds) % most specialised types of given values - -> ( jpl_types_fit_type(Tds, T) % all assignable to element type? - -> true - ; throw(error(type_error(array(T),Ds), - context(jpl_set/3, - 'not all values are assignable to the array element type'))) - ) - ; throw(error(type_error(array(T),Ds), - context(jpl_set/3, - 'not all values are convertible to Java values or references'))) - ), - ( ( T = class(_,_) - ; T = array(_) % array elements are objects - ) - -> ( nth0(J, Ds, D), % for each datum - Nd is N+J, % compute array index - ( D = {Tq} % quoted term? - -> jni_term_to_jref(Tq, D2) % convert to a JPL reference to a corresponding jpl.Term object - ; D = D2 - ), - jSetObjectArrayElement(A, Nd, D2), - fail % iterate - ; true - ) - ; jpl_primitive_type(T) % array elements are primitive values - -> jni_type_to_xput_code(T, Xc), - jni_alloc_buffer(Xc, I, Bp), % I-element buf of required primitive type - jpl_set_array_1(Ds, T, 0, Bp), - jpl_set_elements(T, A, N, I, Bp), - jni_free_buffer(Bp) - ; throw(error(system_error(array_element_type,T), - context(jpl_set/3, - 'array element type is unknown (this should not happen)'))) - ). - -%------------------------------------------------------------------------------ - -%% jpl_set_array_1(+Values, +Type, +BufferIndex, +BufferPointer) -% -% successive members of Values are stashed as (primitive) Type -% from the BufferIndex-th element (numbered from 0) onwards of the -% buffer indicated by BufferPointer NB this could be done more -% efficiently (?) within foreign code... - -jpl_set_array_1([], _, _, _). -jpl_set_array_1([V|Vs], Tprim, Ib, Bp) :- - jni_type_to_xput_code(Tprim, Xc), - jni_stash_buffer_value(Bp, Ib, V, Xc), - Ibnext is Ib+1, - jpl_set_array_1(Vs, Tprim, Ibnext, Bp). - -%------------------------------------------------------------------------------ - -jpl_set_elements(boolean, Obj, N, I, Bp) :- - jSetBooleanArrayRegion(Obj, N, I, jbuf(Bp,boolean)). -jpl_set_elements(char, Obj, N, I, Bp) :- - jSetCharArrayRegion(Obj, N, I, jbuf(Bp,char)). -jpl_set_elements(byte, Obj, N, I, Bp) :- - jSetByteArrayRegion(Obj, N, I, jbuf(Bp,byte)). -jpl_set_elements(short, Obj, N, I, Bp) :- - jSetShortArrayRegion(Obj, N, I, jbuf(Bp,short)). -jpl_set_elements(int, Obj, N, I, Bp) :- - jSetIntArrayRegion(Obj, N, I, jbuf(Bp,int)). -jpl_set_elements(long, Obj, N, I, Bp) :- - jSetLongArrayRegion(Obj, N, I, jbuf(Bp,long)). -jpl_set_elements(float, Obj, N, I, Bp) :- - jSetFloatArrayRegion(Obj, N, I, jbuf(Bp,float)). -jpl_set_elements(double, Obj, N, I, Bp) :- - jSetDoubleArrayRegion(Obj, N, I, jbuf(Bp,double)). - -%------------------------------------------------------------------------------ - -%% jpl_set_instance_field(+Type, +Obj, +FieldID, +V) -% -% we can rely on Type, Obj and FieldID being valid, and on V being -% assignable (if V is a quoted term then it is converted here) - -jpl_set_instance_field(boolean, Obj, FieldID, V) :- - jSetBooleanField(Obj, FieldID, V). -jpl_set_instance_field(byte, Obj, FieldID, V) :- - jSetByteField(Obj, FieldID, V). -jpl_set_instance_field(char, Obj, FieldID, V) :- - jSetCharField(Obj, FieldID, V). -jpl_set_instance_field(short, Obj, FieldID, V) :- - jSetShortField(Obj, FieldID, V). -jpl_set_instance_field(int, Obj, FieldID, V) :- - jSetIntField(Obj, FieldID, V). -jpl_set_instance_field(long, Obj, FieldID, V) :- - jSetLongField(Obj, FieldID, V). -jpl_set_instance_field(float, Obj, FieldID, V) :- - jSetFloatField(Obj, FieldID, V). -jpl_set_instance_field(double, Obj, FieldID, V) :- - jSetDoubleField(Obj, FieldID, V). -jpl_set_instance_field(class(_,_), Obj, FieldID, V) :- % also handles byval term assignments - ( V = {T} % quoted term? - -> jni_term_to_jref(T, V2) % convert to a JPL reference to a corresponding jpl.Term object - ; V = V2 - ), - jSetObjectField(Obj, FieldID, V2). -jpl_set_instance_field(array(_), Obj, FieldID, V) :- - jSetObjectField(Obj, FieldID, V). - -%------------------------------------------------------------------------------ - -% jpl_set_static_field(+Type, +ClassObj, +FieldID, +V) :- -% we can rely on Type, ClassObj and FieldID being valid, -% and on V being assignable (if V is a quoted term then it is converted here) - -jpl_set_static_field(boolean, Obj, FieldID, V) :- - jSetStaticBooleanField(Obj, FieldID, V). - -jpl_set_static_field(byte, Obj, FieldID, V) :- - jSetStaticByteField(Obj, FieldID, V). - -jpl_set_static_field(char, Obj, FieldID, V) :- - jSetStaticCharField(Obj, FieldID, V). - -jpl_set_static_field(short, Obj, FieldID, V) :- - jSetStaticShortField(Obj, FieldID, V). - -jpl_set_static_field(int, Obj, FieldID, V) :- - jSetStaticIntField(Obj, FieldID, V). - -jpl_set_static_field(long, Obj, FieldID, V) :- - jSetStaticLongField(Obj, FieldID, V). - -jpl_set_static_field(float, Obj, FieldID, V) :- - jSetStaticFloatField(Obj, FieldID, V). - -jpl_set_static_field(double, Obj, FieldID, V) :- - jSetStaticDoubleField(Obj, FieldID, V). - -jpl_set_static_field(class(_,_), Obj, FieldID, V) :- % also handles byval term assignments - ( V = {T} % quoted term? - -> jni_term_to_jref(T, V2) % convert to a JPL reference to a corresponding jpl.Term object - ; V = V2 - ), - jSetStaticObjectField(Obj, FieldID, V2). - -jpl_set_static_field(array(_), Obj, FieldID, V) :- - jSetStaticObjectField(Obj, FieldID, V). - -%------------------------------------------------------------------------------ - -% jpl_z3s_to_most_specific_z3(+Zs, -Z) :- -% Zs is a list of arity-matching, type-suitable z3(I,MID,Tfps) -% Z is the single most specific element of Zs, -% i.e. that than which no other z3/3 has a more specialised signature; -% fails if there is more than one such - -jpl_z3s_to_most_specific_z3(Zs, Z) :- - jpl_fergus_is_the_greatest(Zs, Z). - -%------------------------------------------------------------------------------ - -% jpl_z5s_to_most_specific_z5(+Zs, -Z) :- -% Zs is a list of arity-matching, type-suitable z5(I,Mods,MID,Tr,Tfps) -% Z is the single most specific element of Zs, -% i.e. that than which no other z5/5 has a more specialised signature -% (this fails if there is more than one such) - -jpl_z5s_to_most_specific_z5(Zs, Z) :- - jpl_fergus_is_the_greatest(Zs, Z). - -%------------------------------------------------------------------------------ - -% jpl_pl_lib_version(-VersionString) :- -% jpl_pl_lib_version(-Major, -Minor, -Patch, -Status) :- - -jpl_pl_lib_version(VersionString) :- - jpl_pl_lib_version(Major, Minor, Patch, Status), - concat_atom([Major,'.',Minor,'.',Patch,'-',Status], VersionString). - - -jpl_pl_lib_version(3, 1, 3, alpha). - -%------------------------------------------------------------------------------ - -% jpl_type_alfa(0'$) --> % presumably not allowed -% "$". % given the "inner class" syntax? - -jpl_type_alfa(0'_) --> - "_", - !. - -jpl_type_alfa(C) --> - [C], { C>=0'a, C=<0'z }, - !. - -jpl_type_alfa(C) --> - [C], { C>=0'A, C=<0'Z }. - -%------------------------------------------------------------------------------ - -jpl_type_alfa_num(C) --> - jpl_type_alfa(C), - !. - -jpl_type_alfa_num(C) --> - [C], { C>=0'0, C=<0'9 }. - -%------------------------------------------------------------------------------ - -jpl_type_array_classname(array(T)) --> - "[", jpl_type_classname_2(T). - -%------------------------------------------------------------------------------ - -jpl_type_array_descriptor(array(T)) --> - "[", jpl_type_descriptor_1(T). - -%------------------------------------------------------------------------------ - -jpl_type_bare_class_descriptor(class(Ps,Cs)) --> - jpl_type_slashed_package_parts(Ps), jpl_type_class_parts(Cs). - -%------------------------------------------------------------------------------ - -jpl_type_bare_classname(class(Ps,Cs)) --> - jpl_type_dotted_package_parts(Ps), jpl_type_class_parts(Cs). - -%------------------------------------------------------------------------------ - -jpl_type_class_descriptor(class(Ps,Cs)) --> - "L", jpl_type_bare_class_descriptor(class(Ps,Cs)), ";". - -%------------------------------------------------------------------------------ - -jpl_type_class_part(N) --> - jpl_type_id(N). - -%------------------------------------------------------------------------------ - -jpl_type_class_parts([C|Cs]) --> - jpl_type_class_part(C), jpl_type_inner_class_parts(Cs). - -%------------------------------------------------------------------------------ - -jpl_type_classname_1(T) --> - jpl_type_bare_classname(T), - !. - -jpl_type_classname_1(T) --> - jpl_type_array_classname(T), - !. - -jpl_type_classname_1(T) --> - jpl_type_primitive(T). - -%------------------------------------------------------------------------------ - -jpl_type_classname_2(T) --> - jpl_type_delimited_classname(T). - -jpl_type_classname_2(T) --> - jpl_type_array_classname(T). - -jpl_type_classname_2(T) --> - jpl_type_primitive(T). - -%------------------------------------------------------------------------------ - -jpl_type_delimited_classname(Class) --> - "L", jpl_type_bare_classname(Class), ";". - -%------------------------------------------------------------------------------ - -jpl_type_descriptor_1(T) --> - jpl_type_primitive(T), - !. - -jpl_type_descriptor_1(T) --> - jpl_type_class_descriptor(T), - !. - -jpl_type_descriptor_1(T) --> - jpl_type_array_descriptor(T), - !. - -jpl_type_descriptor_1(T) --> - jpl_type_method_descriptor(T). - -%------------------------------------------------------------------------------ - -jpl_type_dotted_package_parts([P|Ps]) --> - jpl_type_package_part(P), ".", !, jpl_type_dotted_package_parts(Ps). - -jpl_type_dotted_package_parts([]) --> - []. - -%------------------------------------------------------------------------------ - -jpl_type_findclassname(T) --> - jpl_type_bare_class_descriptor(T). - -jpl_type_findclassname(T) --> - jpl_type_array_descriptor(T). - -%------------------------------------------------------------------------------ - -jpl_type_id(A) --> - { nonvar(A) -> atom_codes(A,[C|Cs]) ; true }, - jpl_type_alfa(C), jpl_type_id_rest(Cs), - { atom_codes(A, [C|Cs]) }. - -%------------------------------------------------------------------------------ - -jpl_type_id_rest([C|Cs]) --> - jpl_type_alfa_num(C), !, jpl_type_id_rest(Cs). - -jpl_type_id_rest([]) --> - []. - -%------------------------------------------------------------------------------ - -jpl_type_id_v2(A) --> % inner class name parts (empirically) - { nonvar(A) -> atom_codes(A,Cs) ; true }, - jpl_type_id_rest(Cs), - { atom_codes(A, Cs) }. - -%------------------------------------------------------------------------------ - -jpl_type_inner_class_part(N) --> - jpl_type_id_v2(N). - -%------------------------------------------------------------------------------ - -jpl_type_inner_class_parts([C|Cs]) --> - "$", jpl_type_inner_class_part(C), !, jpl_type_inner_class_parts(Cs). - -jpl_type_inner_class_parts([]) --> - []. - -%------------------------------------------------------------------------------ - -jpl_type_method_descriptor(method(Ts,T)) --> - "(", jpl_type_method_descriptor_args(Ts), ")", jpl_type_method_descriptor_return(T). - -%------------------------------------------------------------------------------ - -jpl_type_method_descriptor_args([T|Ts]) --> - jpl_type_descriptor_1(T), !, jpl_type_method_descriptor_args(Ts). - -jpl_type_method_descriptor_args([]) --> - []. - -%------------------------------------------------------------------------------ - -jpl_type_method_descriptor_return(T) --> - jpl_type_void(T). - -jpl_type_method_descriptor_return(T) --> - jpl_type_descriptor_1(T). - -%------------------------------------------------------------------------------ - -jpl_type_package_part(N) --> - jpl_type_id(N). - -%------------------------------------------------------------------------------ - -jpl_type_primitive(boolean) --> - "Z", - !. - -jpl_type_primitive(byte) --> - "B", - !. - -jpl_type_primitive(char) --> - "C", - !. - -jpl_type_primitive(short) --> - "S", - !. - -jpl_type_primitive(int) --> - "I", - !. - -jpl_type_primitive(long) --> - "J", - !. - -jpl_type_primitive(float) --> - "F", - !. - -jpl_type_primitive(double) --> - "D". - -%------------------------------------------------------------------------------ - -jpl_type_slashed_package_parts([P|Ps]) --> - jpl_type_package_part(P), "/", !, jpl_type_slashed_package_parts(Ps). - -jpl_type_slashed_package_parts([]) --> - []. - -%------------------------------------------------------------------------------ - -jpl_type_void(void) --> - "V". - -%------------------------------------------------------------------------------ - -%type jCallBooleanMethod(object, method_id, types, datums, boolean) - -% jCallBooleanMethod(+Obj, +MethodID, +Types, +Params, -Rbool) :- - -jCallBooleanMethod(Obj, MethodID, Types, Params, Rbool) :- - jni_params_put(Params, Types, ParamBuf), - jni_func(39, Obj, MethodID, ParamBuf, Rbool). - -%------------------------------------------------------------------------------ - -%type jCallByteMethod(object, method_id, types, datums, byte) - -% jCallByteMethod(+Obj, +MethodID, +Types, +Params, -Rbyte) :- - -jCallByteMethod(Obj, MethodID, Types, Params, Rbyte) :- - jni_params_put(Params, Types, ParamBuf), - jni_func(42, Obj, MethodID, ParamBuf, Rbyte). - -%------------------------------------------------------------------------------ - -%type jCallCharMethod(object, method_id, types, datums, char) - -% jCallCharMethod(+Obj, +MethodID, +Types, +Params, -Rchar) :- - -jCallCharMethod(Obj, MethodID, Types, Params, Rchar) :- - jni_params_put(Params, Types, ParamBuf), - jni_func(45, Obj, MethodID, ParamBuf, Rchar). - -%------------------------------------------------------------------------------ - -%type jCallDoubleMethod(object, method_id, types, datums, double) - -% jCallDoubleMethod(+Obj, +MethodID, +Types, +Params, -Rdouble) :- - -jCallDoubleMethod(Obj, MethodID, Types, Params, Rdouble) :- - jni_params_put(Params, Types, ParamBuf), - jni_func(60, Obj, MethodID, ParamBuf, Rdouble). - -%------------------------------------------------------------------------------ - -%type jCallFloatMethod(object, method_id, types, datums, float) - -% jCallFloatMethod(+Obj, +MethodID, +Types, +Params, -Rfloat) :- - -jCallFloatMethod(Obj, MethodID, Types, Params, Rfloat) :- - jni_params_put(Params, Types, ParamBuf), - jni_func(57, Obj, MethodID, ParamBuf, Rfloat). - -%------------------------------------------------------------------------------ - -%type jCallIntMethod(object, method_id, types, datums, int) - -% jCallIntMethod(+Obj, +MethodID, +Types, +Params, -Rint) :- - -jCallIntMethod(Obj, MethodID, Types, Params, Rint) :- - jni_params_put(Params, Types, ParamBuf), - jni_func(51, Obj, MethodID, ParamBuf, Rint). - -%------------------------------------------------------------------------------ - -%type jCallLongMethod(object, method_id, types, datums, long) - -% jCallLongMethod(+Obj, +MethodID, +Types, +Params, -Rlong) :- - -jCallLongMethod(Obj, MethodID, Types, Params, Rlong) :- - jni_params_put(Params, Types, ParamBuf), - jni_func(54, Obj, MethodID, ParamBuf, Rlong). - -%------------------------------------------------------------------------------ - -%type jCallObjectMethod(object, method_id, types, datums, object) - -% jCallObjectMethod(+Obj, +MethodID, +Types, +Params, -Robj) :- - -jCallObjectMethod(Obj, MethodID, Types, Params, Robj) :- - jni_params_put(Params, Types, ParamBuf), - jni_func(36, Obj, MethodID, ParamBuf, Robj). - -%------------------------------------------------------------------------------ - -%type jCallShortMethod(object, method_id, types, datums, short) - -% jCallShortMethod(+Obj, +MethodID, +Types, +Params, -Rshort) :- - -jCallShortMethod(Obj, MethodID, Types, Params, Rshort) :- - jni_params_put(Params, Types, ParamBuf), - jni_func(48, Obj, MethodID, ParamBuf, Rshort). - -%------------------------------------------------------------------------------ - -%type jCallStaticBooleanMethod(class, types, datums, boolean) - -% jCallStaticBooleanMethod(+Class, +MethodID, +Types, +Params, -Rbool) :- - -jCallStaticBooleanMethod(Class, MethodID, Types, Params, Rbool) :- - jni_params_put(Params, Types, ParamBuf), - jni_func(119, Class, MethodID, ParamBuf, Rbool). - -%------------------------------------------------------------------------------ - -%type jCallStaticByteMethod(class, method_id, types, datums, byte) - -% jCallStaticByteMethod(+Class, +MethodID, +Types, +Params, -Rbyte) :- - -jCallStaticByteMethod(Class, MethodID, Types, Params, Rbyte) :- - jni_params_put(Params, Types, ParamBuf), - jni_func(122, Class, MethodID, ParamBuf, Rbyte). - -%------------------------------------------------------------------------------ - -%type jCallStaticCharMethod(class, method_id, types, datums, char) - -% jCallStaticCharMethod(+Class, +MethodID, +Types, +Params, -Rchar) :- - -jCallStaticCharMethod(Class, MethodID, Types, Params, Rchar) :- - jni_params_put(Params, Types, ParamBuf), - jni_func(125, Class, MethodID, ParamBuf, Rchar). - -%------------------------------------------------------------------------------ - -%type jCallStaticDoubleMethod(class, method_id, types, datums, double) - -% jCallStaticDoubleMethod(+Class, +MethodID, +Types, +Params, -Rdouble) :- - -jCallStaticDoubleMethod(Class, MethodID, Types, Params, Rdouble) :- - jni_params_put(Params, Types, ParamBuf), - jni_func(140, Class, MethodID, ParamBuf, Rdouble). - -%------------------------------------------------------------------------------ - -%type jCallStaticFloatMethod(class, method_id, types, datums, float) - -% jCallStaticFloatMethod(+Class, +MethodID, +Types, +Params, -Rfloat) :- - -jCallStaticFloatMethod(Class, MethodID, Types, Params, Rfloat) :- - jni_params_put(Params, Types, ParamBuf), - jni_func(137, Class, MethodID, ParamBuf, Rfloat). - -%------------------------------------------------------------------------------ - -%type jCallStaticIntMethod(class, method_id, types, datums, int) - -% jCallStaticIntMethod(+Class, +MethodID, +Types, +Params, -Rint) :- - -jCallStaticIntMethod(Class, MethodID, Types, Params, Rint) :- - jni_params_put(Params, Types, ParamBuf), - jni_func(131, Class, MethodID, ParamBuf, Rint). - -%------------------------------------------------------------------------------ - -%type jCallStaticLongMethod(class, method_id, types, datums, long) - -% jCallStaticLongMethod(+Class, +MethodID, +Types, +Params, -Rlong) :- - -jCallStaticLongMethod(Class, MethodID, Types, Params, Rlong) :- - jni_params_put(Params, Types, ParamBuf), - jni_func(134, Class, MethodID, ParamBuf, Rlong). - -%------------------------------------------------------------------------------ - -%type jCallStaticObjectMethod(class, method_id, types, datums, object) - -% jCallStaticObjectMethod(+Class, +MethodID, +Types, +Params, -Robj) :- - -jCallStaticObjectMethod(Class, MethodID, Types, Params, Robj) :- - jni_params_put(Params, Types, ParamBuf), - jni_func(116, Class, MethodID, ParamBuf, Robj). - -%------------------------------------------------------------------------------ - -%type jCallStaticShortMethod(class, method_id, types, datums, short) - -% jCallStaticShortMethod(+Class, +MethodID, +Types, +Params, -Rshort) :- - -jCallStaticShortMethod(Class, MethodID, Types, Params, Rshort) :- - jni_params_put(Params, Types, ParamBuf), - jni_func(128, Class, MethodID, ParamBuf, Rshort). - -%------------------------------------------------------------------------------ - -%type jCallStaticVoidMethod(class, method_id, types, datums) - -% jCallStaticVoidMethod(+Class, +MethodID, +Types, +Params) :- - -jCallStaticVoidMethod(Class, MethodID, Types, Params) :- - jni_params_put(Params, Types, ParamBuf), - jni_void(143, Class, MethodID, ParamBuf). - -%------------------------------------------------------------------------------ - -%type jCallVoidMethod(object, method_id, types, datums) - -% jCallVoidMethod(+Obj, +MethodID, +Types, +Params) :- - -jCallVoidMethod(Obj, MethodID, Types, Params) :- - jni_params_put(Params, Types, ParamBuf), - jni_void(63, Obj, MethodID, ParamBuf). - -%------------------------------------------------------------------------------ - -%type jFindClass(findclassname, class) - -% jFindClass(+ClassName, -Class) :- - -jFindClass(ClassName, Class) :- - jni_func(6, ClassName, Class). - -%------------------------------------------------------------------------------ - -%type jGetArrayLength(array, int) - -% jGetArrayLength(+Array, -Size) :- - -jGetArrayLength(Array, Size) :- - jni_func(171, Array, Size). - -%------------------------------------------------------------------------------ - -%type jGetBooleanArrayRegion(boolean_array, int, int, boolean_buf) - -% jGetBooleanArrayRegion(+Array, +Start, +Len, +Buf) :- - -jGetBooleanArrayRegion(Array, Start, Len, Buf) :- - jni_void(199, Array, Start, Len, Buf). - -%------------------------------------------------------------------------------ - -%type jGetBooleanField(object, field_id, boolean) - -% jGetBooleanField(+Obj, +FieldID, -Rbool) :- - -jGetBooleanField(Obj, FieldID, Rbool) :- - jni_func(96, Obj, FieldID, Rbool). - -%------------------------------------------------------------------------------ - -%type jGetByteArrayRegion(byte_array, int, int, byte_buf) - -% jGetByteArrayRegion(+Array, +Start, +Len, +Buf) :- - -jGetByteArrayRegion(Array, Start, Len, Buf) :- - jni_void(200, Array, Start, Len, Buf). - -%------------------------------------------------------------------------------ - -%type jGetByteField(object, field_id, byte) - -% jGetByteField(+Obj, +FieldID, -Rbyte) :- - -jGetByteField(Obj, FieldID, Rbyte) :- - jni_func(97, Obj, FieldID, Rbyte). - -%------------------------------------------------------------------------------ - -%type jGetCharArrayRegion(char_array, int, int, char_buf) - -% jGetCharArrayRegion(+Array, +Start, +Len, +Buf) :- - -jGetCharArrayRegion(Array, Start, Len, Buf) :- - jni_void(201, Array, Start, Len, Buf). - -%------------------------------------------------------------------------------ - -%type jGetCharField(object, field_id, char) - -% jGetCharField(+Obj, +FieldID, -Rchar) :- - -jGetCharField(Obj, FieldID, Rchar) :- - jni_func(98, Obj, FieldID, Rchar). - -%------------------------------------------------------------------------------ - -%type jGetDoubleArrayRegion(double_array, int, int, double_buf) - -% jGetDoubleArrayRegion(+Array, +Start, +Len, +Buf) :- - -jGetDoubleArrayRegion(Array, Start, Len, Buf) :- - jni_void(206, Array, Start, Len, Buf). - -%------------------------------------------------------------------------------ - -%type jGetDoubleField(object, field_id, double) - -% jGetDoubleField(+Obj, +FieldID, -Rdouble) :- - -jGetDoubleField(Obj, FieldID, Rdouble) :- - jni_func(103, Obj, FieldID, Rdouble). - -%------------------------------------------------------------------------------ - -%type jGetFieldID(class, descriptor, field_id) - -% jGetFieldID(+Class, +Name, +Typedescriptor, -FieldID) :- - -jGetFieldID(Class, Name, Type, FieldID) :- - jpl_type_to_descriptor(Type, TD), - jni_func(94, Class, Name, TD, FieldID). - -%------------------------------------------------------------------------------ - -%type jGetFloatArrayRegion(float_array, int, int, float_buf) - -% jGetFloatArrayRegion(+Array, +Start, +Len, +Buf) :- - -jGetFloatArrayRegion(Array, Start, Len, Buf) :- - jni_void(205, Array, Start, Len, Buf). - -%------------------------------------------------------------------------------ - -%type jGetFloatField(object, field_id, float) - -% jGetFloatField(+Obj, +FieldID, -Rfloat) :- - -jGetFloatField(Obj, FieldID, Rfloat) :- - jni_func(102, Obj, FieldID, Rfloat). - -%------------------------------------------------------------------------------ - -%type jGetIntArrayRegion(int_array, int, int, int_buf) - -% jGetIntArrayRegion(+Array, +Start, +Len, +Buf) :- - -jGetIntArrayRegion(Array, Start, Len, Buf) :- - jni_void(203, Array, Start, Len, Buf). - -%------------------------------------------------------------------------------ - -%type jGetIntField(object, field_id, int) - -% jGetIntField(+Obj, +FieldID, -Rint) :- - -jGetIntField(Obj, FieldID, Rint) :- - jni_func(100, Obj, FieldID, Rint). - -%------------------------------------------------------------------------------ - -%type jGetLongArrayRegion(long_array, int, int, long_buf) - -% jGetLongArrayRegion(+Array, +Start, +Len, +Buf) :- - -jGetLongArrayRegion(Array, Start, Len, Buf) :- - jni_void(204, Array, Start, Len, Buf). - -%------------------------------------------------------------------------------ - -%type jGetLongField(object, field_id, long) - -% jGetLongField(+Obj, +FieldID, -Rlong) :- - -jGetLongField(Obj, FieldID, Rlong) :- - jni_func(101, Obj, FieldID, Rlong). - -%------------------------------------------------------------------------------ - -%type jGetMethodID(class, name, descriptor, method_id) - -% jGetMethodID(+Class, +Name, +TypeDescriptor, -MethodID) :- - -jGetMethodID(Class, Name, Type, MethodID) :- - jpl_type_to_descriptor(Type, TD), - jni_func(33, Class, Name, TD, MethodID). - -%------------------------------------------------------------------------------ - -%type jGetObjectArrayElement(object_array, int, object) - -% jGetObjectArrayElement(+Array, +Index, -Obj) :- - -jGetObjectArrayElement(Array, Index, Obj) :- - jni_func(173, Array, Index, Obj). - -%------------------------------------------------------------------------------ - -%type jGetObjectClass(object, class) - -% jGetObjectClass(+Object, -Class) :- - -jGetObjectClass(Object, Class) :- - jni_func(31, Object, Class). - -%------------------------------------------------------------------------------ - -%type jGetObjectField(object, field_id, object) - -% jGetObjectField(+Obj, +FieldID, -RObj) :- - -jGetObjectField(Obj, FieldID, Robj) :- - jni_func(95, Obj, FieldID, Robj). - -%------------------------------------------------------------------------------ - -%type jGetShortArrayRegion(short_array, int, int, short_buf) - -% jGetShortArrayRegion(+Array, +Start, +Len, +Buf) :- - -jGetShortArrayRegion(Array, Start, Len, Buf) :- - jni_void(202, Array, Start, Len, Buf). - -%------------------------------------------------------------------------------ - -%type jGetShortField(object, field_id, short) - -% jGetShortField(+Obj, +FieldID, -Rshort) :- - -jGetShortField(Obj, FieldID, Rshort) :- - jni_func(99, Obj, FieldID, Rshort). - -%------------------------------------------------------------------------------ - -%type jGetStaticBooleanField(class, field_id, boolean) - -% jGetStaticBooleanField(+Class, +FieldID, -Rbool) :- - -jGetStaticBooleanField(Class, FieldID, Rbool) :- - jni_func(146, Class, FieldID, Rbool). - -%------------------------------------------------------------------------------ - -%type jGetStaticByteField(class, field_id, byte) - -% jGetStaticByteField(+Class, +FieldID, -Rbyte) :- - -jGetStaticByteField(Class, FieldID, Rbyte) :- - jni_func(147, Class, FieldID, Rbyte). - -%------------------------------------------------------------------------------ - -%type jGetStaticCharField(class, field_id, char) - -% jGetStaticCharField(+Class, +FieldID, -Rchar) :- - -jGetStaticCharField(Class, FieldID, Rchar) :- - jni_func(148, Class, FieldID, Rchar). - -%------------------------------------------------------------------------------ - -%type jGetStaticDoubleField(class, field_id, double) - -% jGetStaticDoubleField(+Class, +FieldID, -Rdouble) :- - -jGetStaticDoubleField(Class, FieldID, Rdouble) :- - jni_func(153, Class, FieldID, Rdouble). - -%------------------------------------------------------------------------------ - -%type jGetStaticFieldID(class, name, field_id) - -% jGetStaticFieldID(+Class, +Name, +TypeDescriptor, -FieldID) :- - -jGetStaticFieldID(Class, Name, Type, FieldID) :- - jpl_type_to_descriptor(Type, TD), % cache this? - jni_func(144, Class, Name, TD, FieldID). - -%------------------------------------------------------------------------------ - -%type jGetStaticFloatField(class, field_id, float) - -% jGetStaticFloatField(+Class, +FieldID, -Rfloat) :- - -jGetStaticFloatField(Class, FieldID, Rfloat) :- - jni_func(152, Class, FieldID, Rfloat). - -%------------------------------------------------------------------------------ - -%type jGetStaticIntField(class, field_id, int) - -% jGetStaticIntField(+Class, +FieldID, -Rint) :- - -jGetStaticIntField(Class, FieldID, Rint) :- - jni_func(150, Class, FieldID, Rint). - -%------------------------------------------------------------------------------ - -%type jGetStaticLongField(class, field_id, long) - -% jGetStaticLongField(+Class, +FieldID, -Rlong) :- - -jGetStaticLongField(Class, FieldID, Rlong) :- - jni_func(151, Class, FieldID, Rlong). - -%------------------------------------------------------------------------------ - -%type jGetStaticMethodID(class, name, method_id) - -% jGetStaticMethodID(+Class, +Name, +TypeDescriptor, -MethodID) :- - -jGetStaticMethodID(Class, Name, Type, MethodID) :- - jpl_type_to_descriptor(Type, TD), - jni_func(113, Class, Name, TD, MethodID). - -%------------------------------------------------------------------------------ - -%type jGetStaticObjectField(class, field_id, object) - -% jGetStaticObjectField(+Class, +FieldID, -RObj) :- - -jGetStaticObjectField(Class, FieldID, Robj) :- - jni_func(145, Class, FieldID, Robj). - -%------------------------------------------------------------------------------ - -%type jGetStaticShortField(class, field_id, short) - -% jGetStaticShortField(+Class, +FieldID, -Rshort) :- - -jGetStaticShortField(Class, FieldID, Rshort) :- - jni_func(149, Class, FieldID, Rshort). - -%------------------------------------------------------------------------------ - -%type jGetSuperclass(object, object) - -% jGetSuperclass(+Class1, -Class2) :- - -jGetSuperclass(Class1, Class2) :- - jni_func(10, Class1, Class2). - -%------------------------------------------------------------------------------ - -%type jIsAssignableFrom(object, object) - -% jIsAssignableFrom(+Class1, +Class2) :- - -jIsAssignableFrom(Class1, Class2) :- - jni_func(11, Class1, Class2, @(true)). - -%------------------------------------------------------------------------------ - -%type jNewBooleanArray(int, boolean_array) - -% jNewBooleanArray(+Length, -Array) :- - -jNewBooleanArray(Length, Array) :- - jni_func(175, Length, Array). - -%------------------------------------------------------------------------------ - -%type jNewByteArray(int, byte_array) - -% jNewByteArray(+Length, -Array) :- - -jNewByteArray(Length, Array) :- - jni_func(176, Length, Array). - -%------------------------------------------------------------------------------ - -%type jNewCharArray(int, char_array) - -% jNewCharArray(+Length, -Array) :- - -jNewCharArray(Length, Array) :- - jni_func(177, Length, Array). - -%------------------------------------------------------------------------------ - -%type jNewDoubleArray(int, double_array) - -% jNewDoubleArray(+Length, -Array) :- - -jNewDoubleArray(Length, Array) :- - jni_func(182, Length, Array). - -%------------------------------------------------------------------------------ - -%type jNewFloatArray(int, float_array) - -% jNewFloatArray(+Length, -Array) :- - -jNewFloatArray(Length, Array) :- - jni_func(181, Length, Array). - -%------------------------------------------------------------------------------ - -%type jNewIntArray(int, int_array) - -% jNewIntArray(+Length, -Array) :- - -jNewIntArray(Length, Array) :- - jni_func(179, Length, Array). - -%------------------------------------------------------------------------------ - -%type jNewLongArray(int, long_array) - -% jNewLongArray(+Length, -Array) :- - -jNewLongArray(Length, Array) :- - jni_func(180, Length, Array). - -%------------------------------------------------------------------------------ - -%type jNewObject(class, method_id, types, datums, object) - -% jNewObject(+Class, +MethodID, +Types, +Params, -Obj) :- - -jNewObject(Class, MethodID, Types, Params, Obj) :- - jni_params_put(Params, Types, ParamBuf), - jni_func(30, Class, MethodID, ParamBuf, Obj). - -%------------------------------------------------------------------------------ - -%type jNewObjectArray(int, class, object, object_array) - -% jNewObjectArray(+Len, +Class, +InitVal, -Array) :- - -jNewObjectArray(Len, Class, InitVal, Array) :- - jni_func(172, Len, Class, InitVal, Array). - -%------------------------------------------------------------------------------ - -%type jNewShortArray(int, short_array) - -% jNewShortArray(+Length, -Array) :- - -jNewShortArray(Length, Array) :- - jni_func(178, Length, Array). - -%------------------------------------------------------------------------------ - -%type jSetBooleanArrayRegion(boolean_array, int, int, boolean_buf) - -% jSetBooleanArrayRegion(+Array, +Start, +Len, +Buf) :- - -jSetBooleanArrayRegion(Array, Start, Len, Buf) :- - jni_void(207, Array, Start, Len, Buf). - -%------------------------------------------------------------------------------ - -%type jSetBooleanField(object, field_id, boolean) - -% jSetBooleanField(+Obj, +FieldID, +Rbool) :- - -jSetBooleanField(Obj, FieldID, Rbool) :- - jni_void(105, Obj, FieldID, Rbool). - -%------------------------------------------------------------------------------ - -%type jSetByteArrayRegion(byte_array, int, int, byte_buf) - -% jSetByteArrayRegion(+Array, +Start, +Len, +Buf) :- - -jSetByteArrayRegion(Array, Start, Len, Buf) :- - jni_void(208, Array, Start, Len, Buf). - -%------------------------------------------------------------------------------ - -%type jSetByteField(object, field_id, byte) - -% jSetByteField(+Obj, +FieldID, +Rbyte) :- - -jSetByteField(Obj, FieldID, Rbyte) :- - jni_void(106, Obj, FieldID, Rbyte). - -%------------------------------------------------------------------------------ - -%type jSetCharArrayRegion(char_array, int, int, char_buf) - -% jSetCharArrayRegion(+Array, +Start, +Len, +Buf) :- - -jSetCharArrayRegion(Array, Start, Len, Buf) :- - jni_void(209, Array, Start, Len, Buf). - -%------------------------------------------------------------------------------ - -%type jSetCharField(object, field_id, char) - -% jSetCharField(+Obj, +FieldID, +Rchar) :- - -jSetCharField(Obj, FieldID, Rchar) :- - jni_void(107, Obj, FieldID, Rchar). - -%------------------------------------------------------------------------------ - -%type jSetDoubleArrayRegion(double_array, int, int, double_buf) - -% jSetDoubleArrayRegion(+Array, +Start, +Len, +Buf) :- - -jSetDoubleArrayRegion(Array, Start, Len, Buf) :- - jni_void(214, Array, Start, Len, Buf). - -%------------------------------------------------------------------------------ - -%type jSetDoubleField(object, field_id, double) - -% jSetDoubleField(+Obj, +FieldID, +Rdouble) :- - -jSetDoubleField(Obj, FieldID, Rdouble) :- - jni_void(112, Obj, FieldID, Rdouble). - -%------------------------------------------------------------------------------ - -%type jSetFloatArrayRegion(float_array, int, int, float_buf) - -% jSetFloatArrayRegion(+Array, +Start, +Len, +Buf) :- - -jSetFloatArrayRegion(Array, Start, Len, Buf) :- - jni_void(213, Array, Start, Len, Buf). - -%------------------------------------------------------------------------------ - -%type jSetFloatField(object, field_id, float) - -% jSetFloatField(+Obj, +FieldID, +Rfloat) :- - -jSetFloatField(Obj, FieldID, Rfloat) :- - jni_void(111, Obj, FieldID, Rfloat). - -%------------------------------------------------------------------------------ - -%type jSetIntArrayRegion(int_array, int, int, int_buf) - -% jSetIntArrayRegion(+Array, +Start, +Len, +Buf) :- - -jSetIntArrayRegion(Array, Start, Len, Buf) :- - jni_void(211, Array, Start, Len, Buf). - -%------------------------------------------------------------------------------ - -%type jSetIntField(object, field_id, int) - -% jSetIntField(+Obj, +FieldID, +Rint) :- - -jSetIntField(Obj, FieldID, Rint) :- - jni_void(109, Obj, FieldID, Rint). - -%------------------------------------------------------------------------------ - -%type jSetLongArrayRegion(long_array, int, int, long_buf) - -% jSetLongArrayRegion(+Array, +Start, +Len, +Buf) :- - -jSetLongArrayRegion(Array, Start, Len, Buf) :- - jni_void(212, Array, Start, Len, Buf). - -%------------------------------------------------------------------------------ - -%type jSetLongField(object, field_id, long) - -% jSetLongField(+Obj, +FieldID, +Rlong) :- - -jSetLongField(Obj, FieldID, Rlong) :- - jni_void(110, Obj, FieldID, Rlong). - -%------------------------------------------------------------------------------ - -%type jSetObjectArrayElement(object_array, int, object) - -% jSetObjectArrayElement(+Array, +Index, +Obj) :- - -jSetObjectArrayElement(Array, Index, Obj) :- - jni_void(174, Array, Index, Obj). - -%------------------------------------------------------------------------------ - -%type jSetObjectField(object, field_id, object) - -% jSetObjectField(+Obj, +FieldID, +RObj) :- - -jSetObjectField(Obj, FieldID, Robj) :- - jni_void(104, Obj, FieldID, Robj). - -%------------------------------------------------------------------------------ - -%type jSetShortArrayRegion(short_array, int, int, short_buf) - -% jSetShortArrayRegion(+Array, +Start, +Len, +Buf) :- - -jSetShortArrayRegion(Array, Start, Len, Buf) :- - jni_void(210, Array, Start, Len, Buf). - -%------------------------------------------------------------------------------ - -%type jSetShortField(object, field_id, short) - -% jSetShortField(+Obj, +FieldID, +Rshort) :- - -jSetShortField(Obj, FieldID, Rshort) :- - jni_void(108, Obj, FieldID, Rshort). - -%------------------------------------------------------------------------------ - -%type jSetStaticBooleanField(class, field_id, boolean) - -% jSetStaticBooleanField(+Class, +FieldID, +Rbool) :- - -jSetStaticBooleanField(Class, FieldID, Rbool) :- - jni_void(155, Class, FieldID, Rbool). - -%------------------------------------------------------------------------------ - -%type jSetStaticByteField(class, field_id, byte) - -% jSetStaticByteField(+Class, +FieldID, +Rbyte) :- - -jSetStaticByteField(Class, FieldID, Rbyte) :- - jni_void(156, Class, FieldID, Rbyte). - -%------------------------------------------------------------------------------ - -%type jSetStaticCharField(class, field_id, char) - -% jSetStaticCharField(+Class, +FieldID, +Rchar) :- - -jSetStaticCharField(Class, FieldID, Rchar) :- - jni_void(157, Class, FieldID, Rchar). - -%------------------------------------------------------------------------------ - -%type jSetStaticDoubleField(class, field_id, double) - -% jSetStaticDoubleField(+Class, +FieldID, +Rdouble) :- - -jSetStaticDoubleField(Class, FieldID, Rdouble) :- - jni_void(162, Class, FieldID, Rdouble). - -%------------------------------------------------------------------------------ - -%type jSetStaticFloatField(class, field_id, float) - -% jSetStaticFloatField(+Class, +FieldID, +Rfloat) :- - -jSetStaticFloatField(Class, FieldID, Rfloat) :- - jni_void(161, Class, FieldID, Rfloat). - -%------------------------------------------------------------------------------ - -%type jSetStaticIntField(class, field_id, int) - -% jSetStaticIntField(+Class, +FieldID, +Rint) :- - -jSetStaticIntField(Class, FieldID, Rint) :- - jni_void(159, Class, FieldID, Rint). - -%------------------------------------------------------------------------------ - -%type jSetStaticLongField(class, field_id, long) - -% jSetStaticLongField(+Class, +FieldID, +Rlong) :- - -jSetStaticLongField(Class, FieldID, Rlong) :- - jni_void(160, Class, FieldID, Rlong). - -%------------------------------------------------------------------------------ - -%type jSetStaticObjectField(class, field_id, object) - -% jSetStaticObjectField(+Class, +FieldID, +Robj) :- - -jSetStaticObjectField(Class, FieldID, Robj) :- - jni_void(154, Class, FieldID, Robj). - -%------------------------------------------------------------------------------ - -%type jSetStaticShortField(class, field_id, short) - -% jSetStaticShortField(+Class, +FieldID, +Rshort) :- - -jSetStaticShortField(Class, FieldID, Rshort) :- - jni_void(158, Class, FieldID, Rshort). - -%------------------------------------------------------------------------------ - -% jni_params_put(+Params, +Types, -ParamBuf) :- -% the old form used a static buffer, hence was not re-entrant; -% the new form allocates a buffer of one jvalue per arg, -% puts the (converted) args into respective elements, then returns it -% (the caller is responsible for freeing it) - -jni_params_put(As, Ts, ParamBuf) :- - jni_ensure_jvm, % in case e.g. NewStringUTF() is called - length(As, N), - jni_type_to_xput_code(jvalue, Xc), % Xc will be 15 - jni_alloc_buffer(Xc, N, ParamBuf), - jni_params_put_1(As, 0, Ts, ParamBuf). - -%------------------------------------------------------------------------------ - -% jni_params_put_1(+Params, +N, +JPLTypes, +ParamBuf) :- -% Params is a (full or partial) list of args-not-yet-stashed, -% and Types are their (JPL) types (e.g. 'boolean'); -% N is the arg and buffer index (0+) at which the head of Params is to be stashed; -% the old form used a static buffer and hence was non-reentrant; -% the new form uses a dynamically allocated buffer (which oughta be freed after use) -% -%NB if the (user-provided) actual params were to be unsuitable for conversion -%NB to the method-required types, this would fail silently (without freeing the buffer); -%NB it's not clear whether the overloaded-method-resolution ensures that all args -%NB are convertible - -jni_params_put_1([], _, [], _). - -jni_params_put_1([A|As], N, [Tjni|Ts], ParamBuf) :- % type checking? - ( jni_type_to_xput_code(Tjni, Xc) - -> ( A = {Term} % a quoted general term? - -> jni_term_to_jref( Term, Ax) % convert it to a @(Tag) ref to a new Term instance - ; A = Ax - ), - jni_param_put(N, Xc, Ax, ParamBuf) % foreign - ; fail % oughta raise an exception? - ), - N2 is N+1, - jni_params_put_1(As, N2, Ts, ParamBuf). % stash remaining params (if any) - -%------------------------------------------------------------------------------ - -% jni_type_to_xput_code(+JspType, -JniXputCode) :- -% NB JniXputCode determines widening and casting in foreign code -% NB the codes could be compiled into jni_method_spec_cache etc. -% instead of, or as well as, types (for - small - efficiency gain) - -jni_type_to_xput_code(boolean, 1). % JNI_XPUT_BOOLEAN - -jni_type_to_xput_code(byte, 2). % JNI_XPUT_BYTE - -jni_type_to_xput_code(char, 3). % JNI_XPUT_CHAR - -jni_type_to_xput_code(short, 4). % JNI_XPUT_SHORT - -jni_type_to_xput_code(int, 5). % JNI_XPUT_INT - -jni_type_to_xput_code(long, 6). % JNI_XPUT_LONG - -jni_type_to_xput_code(float, 7). % JNI_XPUT_FLOAT - -jni_type_to_xput_code(double, 8). % JNI_XPUT_DOUBLE - -jni_type_to_xput_code(class(_,_), 12). % JNI_XPUT_REF - -jni_type_to_xput_code(array(_), 12). % JNI_XPUT_REF - -jni_type_to_xput_code(jvalue, 15). % JNI_XPUT_JVALUE - -%------------------------------------------------------------------------------ - -% jpl_class_to_constructor_array(+Class, -MethodArray) :- -% might this be done more efficiently in foreign code? or in Java? - -jpl_class_to_constructor_array(Cx, Ma) :- - jpl_classname_to_class('java.lang.Class', CC), % cacheable? - jGetMethodID( - CC, - getConstructors, - method([],array(class([java,lang,reflect],['Constructor']))), - MID - ), % cacheable? - jCallObjectMethod(Cx, MID, [], [], Ma). - -%------------------------------------------------------------------------------ - -% jpl_class_to_constructors(+Class, -Methods) :- - -jpl_class_to_constructors(Cx, Ms) :- - jpl_class_to_constructor_array(Cx, Ma), - jpl_object_array_to_list(Ma, Ms). - -%------------------------------------------------------------------------------ - -% jpl_class_to_field_array(+Class, -FieldArray) :- - -jpl_class_to_field_array(Cx, Fa) :- - jpl_classname_to_class('java.lang.Class', CC), % cacheable? - jGetMethodID( - CC, - getFields, - method([],array(class([java,lang,reflect],['Field']))), - MID - ), % cacheable? - jCallObjectMethod(Cx, MID, [], [], Fa). - -%------------------------------------------------------------------------------ - -% jpl_class_to_fields(+Class, -Fields) :- -% do this in Java (ditto for methods)? - -jpl_class_to_fields(C, Fs) :- - jpl_class_to_field_array(C, Fa), - jpl_object_array_to_list(Fa, Fs). - -%------------------------------------------------------------------------------ - -% jpl_class_to_method_array(+Class, -MethodArray) :- -% migrate into foreign code for efficiency? - -jpl_class_to_method_array(Cx, Ma) :- - jpl_classname_to_class('java.lang.Class', CC), % cacheable? - jGetMethodID( - CC, - getMethods, - method([],array(class([java,lang,reflect],['Method']))), - MID - ), % cacheable? - jCallObjectMethod(Cx, MID, [], [], Ma). - -%------------------------------------------------------------------------------ - -% jpl_class_to_methods(+Class, -Methods) :- -% also used for constructors -% do this in Java (ditto for fields)? - -jpl_class_to_methods(Cx, Ms) :- - jpl_class_to_method_array(Cx, Ma), - jpl_object_array_to_list(Ma, Ms). - -%------------------------------------------------------------------------------ - -% jpl_constructor_to_modifiers(+Method, -Modifiers) :- -% migrate into foreign code for efficiency? - -jpl_constructor_to_modifiers(X, Ms) :- - jpl_classname_to_class('java.lang.reflect.Constructor', Cx), % cached? - jpl_method_to_modifiers_1(X, Cx, Ms). - -%------------------------------------------------------------------------------ - -% jpl_constructor_to_name(+Method, -Name) :- -% it is a JNI convention that each constructor behaves (at least, -% for reflection), as a method whose name is '' - -jpl_constructor_to_name(_X, ''). - -%------------------------------------------------------------------------------ - -% jpl_constructor_to_parameter_types(+Method, -ParameterTypes) :- -% migrate to foreign code for efficiency? - -jpl_constructor_to_parameter_types(X, Tfps) :- - jpl_classname_to_class('java.lang.reflect.Constructor', Cx), % cached? - jpl_method_to_parameter_types_1(X, Cx, Tfps). - -%------------------------------------------------------------------------------ - -% jpl_constructor_to_return_type(+Method, -Type) :- -% it is a JNI convention that, for the purposes of retrieving a MethodID, -% a constructor has a return type of 'void' - -jpl_constructor_to_return_type(_X, void). - -%------------------------------------------------------------------------------ - -% jpl_field_spec(+Type, -Index, -Name, -Modifiers, -MID, -FieldType) :- -% I'm unsure whether arrays have fields, but if they do, this will handle them correctly - -jpl_field_spec(T, I, N, Mods, MID, Tf) :- - ( jpl_field_spec_is_cached(T) - -> jpl_field_spec_cache(T, I, N, Mods, MID, Tf) - ; jpl_type_to_class(T, C), - jpl_class_to_fields(C, Fs), - ( T = array(_BaseType) % regardless of base type... - -> Tci = array(_) % ...the "cache index" type is this - ; Tci = T - ), - jpl_field_spec_1(C, Tci, Fs), - jpl_assert(jpl_field_spec_is_cached(Tci)), - jpl_field_spec_cache(Tci, I, N, Mods, MID, Tf) - ). - -%------------------------------------------------------------------------------ - -jpl_field_spec_1(C, Tci, Fs) :- - ( nth1(I, Fs, F), - jpl_field_to_name(F, N), - jpl_field_to_modifiers(F, Mods), - jpl_field_to_type(F, Tf), - ( member(static, Mods) - -> jGetStaticFieldID(C, N, Tf, MID) - ; jGetFieldID(C, N, Tf, MID) - ), - jpl_assert(jpl_field_spec_cache(Tci,I,N,Mods,MID,Tf)), - fail - ; true - ). - -%------------------------------------------------------------------------------ - -:- dynamic jpl_field_spec_cache/6. % document this... - -%------------------------------------------------------------------------------ - -:- dynamic jpl_field_spec_is_cached/1. % document this... - -%------------------------------------------------------------------------------ - -%type jpl_field_to_modifiers(object, ordset(modifier)) - -% jpl_field_to_modifiers(+Field, -Modifiers) :- - -jpl_field_to_modifiers(F, Ms) :- - jpl_classname_to_class('java.lang.reflect.Field', Cf), - jpl_method_to_modifiers_1(F, Cf, Ms). - -%------------------------------------------------------------------------------ - -% jpl_field_to_name(+Field, -Name) :- - -jpl_field_to_name(F, N) :- - jpl_classname_to_class('java.lang.reflect.Field', Cf), - jpl_member_to_name_1(F, Cf, N). - -%------------------------------------------------------------------------------ - -%type jpl_field_to_type(object, type) - -% jpl_field_to_type(+Field, -Type) :- - -jpl_field_to_type(F, Tf) :- - jpl_classname_to_class('java.lang.reflect.Field', Cf), - jGetMethodID(Cf, getType, method([],class([java,lang],['Class'])), MID), - jCallObjectMethod(F, MID, [], [], Cr), - jpl_class_to_type(Cr, Tf). - -%------------------------------------------------------------------------------ - -%type jpl_method_spec(type, integer, name, arity, ordset(modifier), method_id, type, list(type)) - -% jpl_method_spec(+Type, -Index, -Name, -Arity, -Modifiers, -MID, -ReturnType, -ParameterTypes) :- -% generates pertinent details of all accessible methods of Type (class/2 or array/1), -% populating or using the cache as appropriate - -jpl_method_spec(T, I, N, A, Mods, MID, Tr, Tfps) :- - ( jpl_method_spec_is_cached(T) - -> jpl_method_spec_cache(T, I, N, A, Mods, MID, Tr, Tfps) - ; jpl_type_to_class(T, C), - jpl_class_to_constructors(C, Xs), - jpl_class_to_methods(C, Ms), - ( T = array(_BaseType) % regardless of base type... - -> Tci = array(_) % ...the "cache index" type is this - ; Tci = T - ), - jpl_method_spec_1(C, Tci, Xs, Ms), - jpl_assert(jpl_method_spec_is_cached(Tci)), - jpl_method_spec_cache(Tci, I, N, A, Mods, MID, Tr, Tfps) - ). - -%------------------------------------------------------------------------------ - -%type jpl_method_spec_1(class, partial_type, list(method), list(method)) - -% jpl_method_spec_1(+ClassObject, +CacheIndexType, +Constructors, +Methods) :- -% if the original type is e.g. array(byte) then CacheIndexType is array(_) else it is that type; - -jpl_method_spec_1(C, Tci, Xs, Ms) :- - ( ( nth1(I, Xs, X), % generate constructors, numbered from 1 - jpl_constructor_to_name(X, N), - jpl_constructor_to_modifiers(X, Mods), - jpl_constructor_to_return_type(X, Tr), - jpl_constructor_to_parameter_types(X, Tfps) - ; length(Xs, J0), - nth1(J, Ms, M), % generate members, continuing numbering - I is J0+J, - jpl_method_to_name(M, N), - jpl_method_to_modifiers(M, Mods), - jpl_method_to_return_type(M, Tr), - jpl_method_to_parameter_types(M, Tfps) - ), - length(Tfps, A), % arity - ( member(static, Mods) - -> jGetStaticMethodID(C, N, method(Tfps,Tr), MID) - ; jGetMethodID(C, N, method(Tfps,Tr), MID) - ), - jpl_assert(jpl_method_spec_cache(Tci,I,N,A,Mods,MID,Tr,Tfps)), - fail - ; true - ). - -%------------------------------------------------------------------------------ - -:- dynamic jpl_method_spec_cache/8. - -%------------------------------------------------------------------------------ - -:- dynamic jpl_method_spec_is_cached/1. - -%------------------------------------------------------------------------------ - -% jpl_method_to_modifiers(+Method, -ModifierSet) :- - -jpl_method_to_modifiers(M, Ms) :- - jpl_classname_to_class('java.lang.reflect.Method', Cm), - jpl_method_to_modifiers_1(M, Cm, Ms). - -%------------------------------------------------------------------------------ - -%type jpl_method_to_modifiers_1(object, object, ordset(modifier)) - -% jpl_method_to_modifiers_1(+Method, +ConstructorClass, -ModifierSet) :- - -jpl_method_to_modifiers_1(XM, Cxm, Ms) :- - jGetMethodID(Cxm, getModifiers, method([],int), MID), - jCallIntMethod(XM, MID, [], [], I), - jpl_modifier_int_to_modifiers(I, Ms). - -%------------------------------------------------------------------------------ - -% jpl_method_to_name(+Method, -Name) :- - -jpl_method_to_name(M, N) :- - jpl_classname_to_class('java.lang.reflect.Method', CM), - jpl_member_to_name_1(M, CM, N). - -%------------------------------------------------------------------------------ - -jpl_member_to_name_1(M, CM, N) :- - jGetMethodID(CM, getName, method([],class([java,lang],['String'])), MID), - jCallObjectMethod(M, MID, [], [], N). - -%------------------------------------------------------------------------------ - -% jpl_method_to_parameter_types(+Method, -Types) :- - -jpl_method_to_parameter_types(M, Tfps) :- - jpl_classname_to_class('java.lang.reflect.Method', Cm), - jpl_method_to_parameter_types_1(M, Cm, Tfps). - -%------------------------------------------------------------------------------ - -% jpl_method_to_parameter_types_1(+XM, +Cxm, -Tfps) :- -% XM is (a JPL ref to) an instance of java.lang.reflect.[Constructor|Method] - -jpl_method_to_parameter_types_1(XM, Cxm, Tfps) :- - jGetMethodID(Cxm, getParameterTypes, method([],array(class([java,lang],['Class']))), MID), - jCallObjectMethod(XM, MID, [], [], Atp), - jpl_object_array_to_list(Atp, Ctps), - jpl_classes_to_types(Ctps, Tfps). - -%------------------------------------------------------------------------------ - -% jpl_method_to_return_type(+Method, -Type) :- - -jpl_method_to_return_type(M, Tr) :- - jpl_classname_to_class('java.lang.reflect.Method', Cm), - jGetMethodID(Cm, getReturnType, method([],class([java,lang],['Class'])), MID), - jCallObjectMethod(M, MID, [], [], Cr), - jpl_class_to_type(Cr, Tr). - -%------------------------------------------------------------------------------ - -jpl_modifier_bit(public, 0x001). -jpl_modifier_bit(private, 0x002). -jpl_modifier_bit(protected, 0x004). -jpl_modifier_bit(static, 0x008). -jpl_modifier_bit(final, 0x010). -jpl_modifier_bit(synchronized, 0x020). -jpl_modifier_bit(volatile, 0x040). -jpl_modifier_bit(transient, 0x080). -jpl_modifier_bit(native, 0x100). -jpl_modifier_bit(interface, 0x200). -jpl_modifier_bit(abstract, 0x400). - -%------------------------------------------------------------------------------ - -%type jpl_modifier_int_to_modifiers(integer, ordset(modifier)) - -% jpl_modifier_int_to_modifiers(+Int, -ModifierSet) :- -% ModifierSet is an ordered (hence canonical) list, -% possibly empty (although I suspect never in practice?), -% of modifier atoms, e.g. [public,static] - -jpl_modifier_int_to_modifiers(I, Ms) :- - setof( - M, % should use e.g. set_of_all/3 - B^(jpl_modifier_bit(M, B), - (B /\ I) =\= 0 - ), - Ms - ). - -%------------------------------------------------------------------------------ - -% jpl_servlet_byref(+Config, +Request, +Response) :- -% this serves the "byref" servlet demo, -% exemplifying one tactic for implementing a servlet in Prolog -% by accepting the Request and Response objects as JPL references -% and accessing their members via JPL as required; -% see also jpl_servlet_byval/3 - -jpl_servlet_byref(Config, Request, Response) :- - jpl_call(Config, getServletContext, [], Context), - - jpl_call(Response, setStatus, [200], _), - jpl_call(Response, setContentType, ['text/html'], _), - jpl_call(Response, getWriter, [], W), - - jpl_call(W, println, ['

    jpl_servlet_byref/3 says:

    '], _),
    -
    -	jpl_call(W, println, ['\nservlet context stuff:'], _),
    -
    -	jpl_call(Context, getInitParameterNames, [], ContextInitParameterNameEnum),
    -	jpl_enumeration_to_list(ContextInitParameterNameEnum, ContextInitParameterNames),
    -	length(ContextInitParameterNames, NContextInitParameterNames),
    -	concat_atom(['\tContext.InitParameters = ',NContextInitParameterNames], NContextInitParameterNamesMsg),
    -	jpl_call(W, println, [NContextInitParameterNamesMsg], _),
    -	(   member(ContextInitParameterName, ContextInitParameterNames),
    -	jpl_call(Context, getInitParameter, [ContextInitParameterName], ContextInitParameter),
    -	concat_atom(['\t\tContext.InitParameter[',ContextInitParameterName,'] = ',ContextInitParameter], ContextInitParameterMsg),
    -	jpl_call(W, println, [ContextInitParameterMsg], _),
    -	fail
    -	;   true
    -	),
    -
    -	jpl_call(Context, getMajorVersion, [], MajorVersion),
    -	concat_atom(['\tContext.MajorVersion = ',MajorVersion], MajorVersionMsg),
    -	jpl_call(W, println, [MajorVersionMsg], _),
    -
    -	jpl_call(Context, getMinorVersion, [], MinorVersion),
    -	concat_atom(['\tContext.MinorVersion = ',MinorVersion], MinorVersionMsg),
    -	jpl_call(W, println, [MinorVersionMsg], _),
    -
    -	jpl_call(Context, getServerInfo, [], ServerInfo),
    -	concat_atom(['\tContext.ServerInfo = ',ServerInfo], ServerInfoMsg),
    -	jpl_call(W, println, [ServerInfoMsg], _),
    -
    -	jpl_call(W, println, ['\nservlet config stuff:'], _),
    -
    -	jpl_call(Config, getServletName, [], ServletName),
    -	(   ServletName == @(null)
    -	->  ServletNameAtom = null
    -	;   ServletNameAtom = ServletName
    -	),
    -	concat_atom(['\tConfig.ServletName = ',ServletNameAtom], ServletNameMsg),
    -	jpl_call(W, println, [ServletNameMsg], _),
    -
    -	jpl_call(Config, getInitParameterNames, [], ConfigInitParameterNameEnum),
    -	jpl_enumeration_to_list(ConfigInitParameterNameEnum, ConfigInitParameterNames),
    -	length(ConfigInitParameterNames, NConfigInitParameterNames),
    -	concat_atom(['\tConfig.InitParameters = ',NConfigInitParameterNames], NConfigInitParameterNamesMsg),
    -	jpl_call(W, println, [NConfigInitParameterNamesMsg], _),
    -	(   member(ConfigInitParameterName, ConfigInitParameterNames),
    -	jpl_call(Config, getInitParameter, [ConfigInitParameterName], ConfigInitParameter),
    -	concat_atom(['\t\tConfig.InitParameter[',ConfigInitParameterName,'] = ',ConfigInitParameter], ConfigInitParameterMsg),
    -	jpl_call(W, println, [ConfigInitParameterMsg], _),
    -	fail
    -	;   true
    -	),
    -
    -	jpl_call(W, println, ['\nrequest stuff:'], _),
    -
    -	jpl_call(Request, getAttributeNames, [], AttributeNameEnum),
    -	jpl_enumeration_to_list(AttributeNameEnum, AttributeNames),
    -	length(AttributeNames, NAttributeNames),
    -	concat_atom(['\tRequest.Attributes = ',NAttributeNames], NAttributeNamesMsg),
    -	jpl_call(W, println, [NAttributeNamesMsg], _),
    -	(   member(AttributeName, AttributeNames),
    -	jpl_call(Request, getAttribute, [AttributeName], Attribute),
    -	jpl_call(Attribute, toString, [], AttributeString),
    -	concat_atom(['\t\tRequest.Attribute[',AttributeName,'] = ',AttributeString], AttributeMsg),
    -	jpl_call(W, println, [AttributeMsg], _),
    -	fail
    -	;   true
    -	),
    -
    -	jpl_call(Request, getCharacterEncoding, [], CharacterEncoding),
    -	(   CharacterEncoding == @(null)
    -	->  CharacterEncodingAtom = ''
    -	;   CharacterEncodingAtom = CharacterEncoding
    -	),
    -	concat_atom(['\tRequest.CharacterEncoding',' = ',CharacterEncodingAtom], CharacterEncodingMsg),
    -	jpl_call(W, println, [CharacterEncodingMsg], _),
    -
    -	jpl_call(Request, getContentLength, [], ContentLength),
    -	concat_atom(['\tRequest.ContentLength',' = ',ContentLength], ContentLengthMsg),
    -	jpl_call(W, println, [ContentLengthMsg], _),
    -
    -	jpl_call(Request, getContentType, [], ContentType),
    -	(   ContentType == @(null)
    -	->  ContentTypeAtom = ''
    -	;   ContentTypeAtom = ContentType
    -	),
    -	concat_atom(['\tRequest.ContentType',' = ',ContentTypeAtom], ContentTypeMsg),
    -	jpl_call(W, println, [ContentTypeMsg], _),
    -
    -	jpl_call(Request, getParameterNames, [], ParameterNameEnum),
    -	jpl_enumeration_to_list(ParameterNameEnum, ParameterNames),
    -	length(ParameterNames, NParameterNames),
    -	concat_atom(['\tRequest.Parameters = ',NParameterNames], NParameterNamesMsg),
    -	jpl_call(W, println, [NParameterNamesMsg], _),
    -	(   member(ParameterName, ParameterNames),
    -	jpl_call(Request, getParameter, [ParameterName], Parameter),
    -	concat_atom(['\t\tRequest.Parameter[',ParameterName,'] = ',Parameter], ParameterMsg),
    -	jpl_call(W, println, [ParameterMsg], _),
    -	fail
    -	;   true
    -	),
    -
    -	jpl_call(Request, getProtocol, [], Protocol),
    -	concat_atom(['\tRequest.Protocol',' = ',Protocol], ProtocolMsg),
    -	jpl_call(W, println, [ProtocolMsg], _),
    -
    -	jpl_call(Request, getRemoteAddr, [], RemoteAddr),
    -	concat_atom(['\tRequest.RemoteAddr',' = ',RemoteAddr], RemoteAddrMsg),
    -	jpl_call(W, println, [RemoteAddrMsg], _),
    -
    -	jpl_call(Request, getRemoteHost, [], RemoteHost),
    -	concat_atom(['\tRequest.RemoteHost',' = ',RemoteHost], RemoteHostMsg),
    -	jpl_call(W, println, [RemoteHostMsg], _),
    -
    -	jpl_call(Request, getScheme, [], Scheme),
    -	concat_atom(['\tRequest.Scheme',' = ',Scheme], SchemeMsg),
    -	jpl_call(W, println, [SchemeMsg], _),
    -
    -	jpl_call(Request, getServerName, [], ServerName),
    -	concat_atom(['\tRequest.ServerName',' = ',ServerName], ServerNameMsg),
    -	jpl_call(W, println, [ServerNameMsg], _),
    -
    -	jpl_call(Request, getServerPort, [], ServerPort),
    -	concat_atom(['\tRequest.ServerPort',' = ',ServerPort], ServerPortMsg),
    -	jpl_call(W, println, [ServerPortMsg], _),
    -
    -	jpl_call(Request, isSecure, [], @(Secure)),
    -	concat_atom(['\tRequest.Secure',' = ',Secure], SecureMsg),
    -	jpl_call(W, println, [SecureMsg], _),
    -
    -	jpl_call(W, println, ['\nHTTP request stuff:'], _),
    -
    -	jpl_call(Request, getAuthType, [], AuthType),
    -	(   AuthType == @(null)
    -	->  AuthTypeAtom = ''
    -	;   AuthTypeAtom = AuthType
    -	),
    -	concat_atom(['\tRequest.AuthType',' = ',AuthTypeAtom], AuthTypeMsg),
    -	jpl_call(W, println, [AuthTypeMsg], _),
    -
    -	jpl_call(Request, getContextPath, [], ContextPath),
    -	(   ContextPath == @(null)
    -	->  ContextPathAtom = ''
    -	;   ContextPathAtom = ContextPath
    -	),
    -	concat_atom(['\tRequest.ContextPath',' = ',ContextPathAtom], ContextPathMsg),
    -	jpl_call(W, println, [ContextPathMsg], _),
    -
    -	jpl_call(Request, getCookies, [], CookieArray),
    -	(   CookieArray == @(null)
    -	->  Cookies = []
    -	;   jpl_array_to_list(CookieArray, Cookies)
    -	),
    -	length(Cookies, NCookies),
    -	concat_atom(['\tRequest.Cookies',' = ',NCookies], NCookiesMsg),
    -	jpl_call(W, println, [NCookiesMsg], _),
    -	(   nth0(NCookie, Cookies, Cookie),
    -	concat_atom(['\t\tRequest.Cookie[',NCookie,']'], CookieMsg),
    -	jpl_call(W, println, [CookieMsg], _),
    -
    -	jpl_call(Cookie, getName, [], CookieName),
    -	concat_atom(['\t\t\tRequest.Cookie.Name = ',CookieName], CookieNameMsg),
    -	jpl_call(W, println, [CookieNameMsg], _),
    -
    -	jpl_call(Cookie, getValue, [], CookieValue),
    -	concat_atom(['\t\t\tRequest.Cookie.Value = ',CookieValue], CookieValueMsg),
    -	jpl_call(W, println, [CookieValueMsg], _),
    -
    -	jpl_call(Cookie, getPath, [], CookiePath),
    -	(   CookiePath == @(null)
    -	->  CookiePathAtom = ''
    -	;   CookiePathAtom = CookiePath
    -	),
    -	concat_atom(['\t\t\tRequest.Cookie.Path = ',CookiePathAtom], CookiePathMsg),
    -	jpl_call(W, println, [CookiePathMsg], _),
    -
    -	jpl_call(Cookie, getComment, [], CookieComment),
    -	(   CookieComment == @(null)
    -	->  CookieCommentAtom = ''
    -	;   CookieCommentAtom = CookieComment
    -	),
    -	concat_atom(['\t\t\tRequest.Cookie.Comment = ',CookieCommentAtom], CookieCommentMsg),
    -	jpl_call(W, println, [CookieCommentMsg], _),
    -
    -	jpl_call(Cookie, getDomain, [], CookieDomain),
    -	(   CookieDomain == @(null)
    -	->  CookieDomainAtom = ''
    -	;   CookieDomainAtom = CookieDomain
    -	),
    -	concat_atom(['\t\t\tRequest.Cookie.Domain = ',CookieDomainAtom], CookieDomainMsg),
    -	jpl_call(W, println, [CookieDomainMsg], _),
    -
    -	jpl_call(Cookie, getMaxAge, [], CookieMaxAge),
    -	concat_atom(['\t\t\tRequest.Cookie.MaxAge = ',CookieMaxAge], CookieMaxAgeMsg),
    -	jpl_call(W, println, [CookieMaxAgeMsg], _),
    -
    -	jpl_call(Cookie, getVersion, [], CookieVersion),
    -	concat_atom(['\t\t\tRequest.Cookie.Version = ',CookieVersion], CookieVersionMsg),
    -	jpl_call(W, println, [CookieVersionMsg], _),
    -
    -	jpl_call(Cookie, getSecure, [], @(CookieSecure)),
    -	concat_atom(['\t\t\tRequest.Cookie.Secure',' = ',CookieSecure], CookieSecureMsg),
    -	jpl_call(W, println, [CookieSecureMsg], _),
    -
    -	fail
    -	;   true
    -	),
    -
    -	jpl_call(W, println, ['
    '], _), - - true. - -%------------------------------------------------------------------------------ - -% jpl_servlet_byval(+MultiMap, -ContentType, -BodyAtom) :- -% this exemplifies an alternative (to jpl_servlet_byref) tactic -% for implementing a servlet in Prolog; -% most Request fields are extracted in Java before this is called, -% and passed in as a multimap (a map, some of whose values are maps) - -jpl_servlet_byval(MM, CT, Ba) :- - CT = 'text/html', - multimap_to_atom(MM, MMa), - concat_atom(['', - '

    jpl_servlet_byval/3 says:

    ',
    -		     MMa,
    -		     '
    ' - ], Ba). - -%------------------------------------------------------------------------------ - -%type jpl_cache_type_of_ref(jpl_type, ref) - -% jpl_cache_type_of_ref(+Type, +Ref) :- -% Type must be a proper (concrete) JPL type; -% Ref must be a proper JPL reference (not void); -% Type is memoed (if policy so dictates) as the type of the referenced object (unless it's null) -% by iref (so as not to disable atom-based GC) -% NB obsolete lemmas must be watched-out-for and removed - -jpl_cache_type_of_ref(T, @(Tag)) :- - ( jpl_assert_policy( jpl_iref_type_cache(_,_), no) - -> true - ; \+ ground(T) % shouldn't happen (implementation error) - -> write('[jpl_cache_type_of_ref/2: arg 1 is not ground]'), nl, % oughta throw an exception - fail - ; \+ atom(Tag) % shouldn't happen (implementation error) - -> write('[jpl_cache_type_of_ref/2: arg 2 is not an atomic-tag ref]'), nl, % oughta throw an exception - fail - ; Tag == null % a null ref? (this is valid) - -> true % silently ignore it - ; jni_tag_to_iref(Tag, Iref) - -> ( jpl_iref_type_cache(Iref, TC) % we expect TC == T - -> ( T == TC - -> true - ; % write('[JPL: found obsolete tag-type lemma...]'), nl, % or keep statistics? (why?) - retractall(jpl_iref_type_cache(Iref,_)), - jpl_assert(jpl_iref_type_cache(Iref,T)) - ) - ; jpl_assert(jpl_iref_type_cache(Iref,T)) - ) - ; write('[jpl_cache_type_of_ref/2: jni_tagatom_to_iref(Tag,_) failed]'), nl, % oughta throw an exception - fail - ). - -%------------------------------------------------------------------------------ - -% jpl_class_tag_type_cache(-Tag, -ClassType) :- -% Tag is the tag part of an @(Tag) reference -% to a JVM instance of java.lang.Class -% which denotes ClassType; -% we index on Tag rather than on Iref so as to keep these objects around -% even after an atom garbage collection -% (if needed once, they are likely to be needed again) - -:- dynamic jpl_class_tag_type_cache/2. - -%------------------------------------------------------------------------------ - -% jpl_class_to_ancestor_classes(+Class, -AncestorClasses) :- -% AncestorClasses will be a list of (JPL references to) instances of java.lang.Class -% denoting the "implements" lineage (?), nearest first -% (the first member denotes the class which Class directly implements, -% the next (if any) denotes the class which *that* class implements, -% and so on to java.lang.Object) - -jpl_class_to_ancestor_classes(C, Cas) :- - ( jpl_class_to_super_class(C, Ca) - -> Cas = [Ca|Cas2], - jpl_class_to_ancestor_classes(Ca, Cas2) - ; Cas = [] - ). - -%------------------------------------------------------------------------------ - -% jpl_class_to_classname(+Class, -ClassName) :- -% Class is a reference to a class object; -% ClassName is its canonical (?) source-syntax (dotted) name, -% e.g. 'java.util.Date' -% not used outside jni_junk and jpl_test (is this (still) true?); -% oughta use the available caches (but their indexing doesn't suit) - -jpl_class_to_classname(C, CN) :- - jpl_call(C, getName, [], CN). - -%------------------------------------------------------------------------------ - -% jpl_class_to_raw_classname(+Class, -ClassName) :- -% hmm, I forget exactly what a "raw" classname is... - -jpl_class_to_raw_classname(Cobj, CN) :- - jpl_classname_to_class('java.lang.Class', CC), % cached? - jGetMethodID(CC, getName, method([],class([java,lang],['String'])), MIDgetName), - jCallObjectMethod(Cobj, MIDgetName, [], [], S), - S = CN. - -%------------------------------------------------------------------------------ - -% jpl_class_to_raw_classname_chars(+Class, -ClassnameChars) :- -% Class is a reference to a class object; -% ClassnameChars is a chars representation of its dotted name, e.g. -% "java.util.Date" - -jpl_class_to_raw_classname_chars(Cobj, CsCN) :- - jpl_class_to_raw_classname(Cobj, CN), - atom_codes(CN, CsCN). - -%------------------------------------------------------------------------------ - -jpl_class_to_super_class(C, Cx) :- - jGetSuperclass(C, Cx), - Cx \== @(null), % as returned when C is java.lang.Object, i.e. no superclass - jpl_cache_type_of_ref(class([java,lang],['Class']), Cx). - -%------------------------------------------------------------------------------ - -% jpl_class_to_type(+ClassObject, -Type) :- -% ClassObject is a reference to a class object of Type -% NB should ensure that, if not found in cache, then cache is updated; -% intriguingly (?), getParameterTypes returns class objects with names -% 'boolean', 'byte' etc. and even 'void' (?!) - -jpl_class_to_type(@(Tag), Type) :- - ( jpl_class_tag_type_cache(Tag, Tx) - -> true - ; jpl_class_to_raw_classname_chars(@(Tag), Cs), % uncached - jpl_classname_chars_to_type(Cs, Tr), - jpl_type_to_canonical_type(Tr, Tx), % map e.g. class([],[byte]) -> byte - jpl_assert(jpl_class_tag_type_cache(Tag,Tx)) - -> true % the elseif goal should be determinate, but just in case... - ), - Type = Tx. - -%------------------------------------------------------------------------------ - -jpl_classes_to_types([], []). - -jpl_classes_to_types([C|Cs], [T|Ts]) :- - jpl_class_to_type(C, T), - jpl_classes_to_types(Cs, Ts). - -%------------------------------------------------------------------------------ - -jpl_classname_chars_to_type(Cs, Type) :- - ( phrase(jpl_type_classname_1(Type), Cs) - -> true - ). - -%------------------------------------------------------------------------------ - -% jpl_classname_to_class(+ClassName, -Class) :- -% ClassName unambiguously represents a class, -% e.g. 'java.lang.String' -% Class is a (canonical) reference to the corresponding class object; -% uses caches where the class is already encountered - -jpl_classname_to_class(N, C) :- - jpl_classname_to_type(N, T), % cached - jpl_type_to_class(T, C). % cached - -%------------------------------------------------------------------------------ - -% jpl_classname_to_type(+Classname, -Type) :- -% Classname is a source-syntax (dotted) class name, -% e.g. 'java.util.Date', '[java.util.Date' or '[L' -% Type is its corresponding JPL type structure, -% e.g. class([java,util],['Date']), array(class([java,util],['Date'])), array(long) -% -%thinks -% by "classname" do I mean "typename"? -% should this throw an exception for unbound CN? is this public API? - -jpl_classname_to_type(CN, T) :- - ( jpl_classname_type_cache(CN, Tx) - -> Tx = T - ; atom_codes(CN, CsCN), - phrase(jpl_type_classname_1(T), CsCN) - -> jpl_assert(jpl_classname_type_cache(CN,T)), - true - ). - -%------------------------------------------------------------------------------ - -% jpl_classname_type_cache( -Classname, -Type) :- -% Classname is the atomic name of Type; -% NB may denote a class which cannot be found - -:- dynamic jpl_classname_type_cache/2. - -%------------------------------------------------------------------------------ - -% jpl_datum_to_type(+Datum, -Type) :- -% Datum must be a proper JPL representation -% of an instance of one (or more) Java types; -% Type is the unique most specialised type of which Datum denotes an instance; -% N.B. 3 is an instance of byte, char, short, int and long, -% of which byte and char are the joint, overlapping most specialised types, -% so this relates 3 to the pseudo subtype 'char_byte'; -% see jpl_type_to_preferred_concrete_type/2 for converting inferred types -% to instantiable types - -jpl_datum_to_type(D, T) :- - ( jpl_value_to_type(D, T) - -> true - ; jpl_ref_to_type(D, T) - -> true - ; nonvar( D), - D = {Term} - -> ( cyclic_term(Term) - -> throw(error(type_error(acyclic,Term), - context(jpl_datum_to_type/2,'must be acyclic'))) - ; atom( Term) - -> T = class([jpl],['Atom']) - ; integer( Term) - -> T = class([jpl],['Integer']) - ; float( Term) - -> T = class([jpl],['Float']) - ; var( Term) - -> T = class([jpl],['Variable']) - ; T = class([jpl],['Compound']) - ) - ). - -%------------------------------------------------------------------------------ - -jpl_datums_to_most_specific_common_ancestor_type([D], T) :- - jpl_datum_to_type(D, T). - -jpl_datums_to_most_specific_common_ancestor_type([D1,D2|Ds], T0) :- - jpl_datum_to_type(D1, T1), - jpl_type_to_ancestor_types(T1, Ts1), - jpl_datums_to_most_specific_common_ancestor_type_1([D2|Ds], [T1|Ts1], [T0|_]). - -%------------------------------------------------------------------------------ - -jpl_datums_to_most_specific_common_ancestor_type_1([], Ts, Ts). - -jpl_datums_to_most_specific_common_ancestor_type_1([D|Ds], Ts1, Ts0) :- - jpl_datum_to_type(D, Tx), - jpl_lineage_types_type_to_common_lineage_types(Ts1, Tx, Ts2), - jpl_datums_to_most_specific_common_ancestor_type_1(Ds, Ts2, Ts0). - -%------------------------------------------------------------------------------ - -% jpl_datums_to_types(+Datums, -Types) :- -% each member of Datums is a JPL value or ref, -% denoting an instance of some Java type, -% and the corresponding member of Types denotes the most specialised type -% of which it is an instance (including some I invented for the overlaps -% between char and short, etc,) - -jpl_datums_to_types([], []). - -jpl_datums_to_types([D|Ds], [T|Ts]) :- - jpl_datum_to_type(D, T), - jpl_datums_to_types(Ds, Ts). - -%------------------------------------------------------------------------------ - -% jpl_false(-X) :- -% X is (by unification) the proper JPL datum which represents the Java boolean value 'false' -% c.f. jpl_is_false/1 - -jpl_false(@(false)). - -%------------------------------------------------------------------------------ - -% jpl_ground_is_type(+X) :- -% X, known to be ground, is (or at least superficially resembles :-) a JPL type - -jpl_ground_is_type(X) :- - jpl_primitive_type(X), - !. - -jpl_ground_is_type(array(X)) :- - jpl_ground_is_type(X). - -jpl_ground_is_type(class(_,_)). - -jpl_ground_is_type(method(_,_)). - -%------------------------------------------------------------------------------ - -:- dynamic jpl_iref_type_cache/2. - -%------------------------------------------------------------------------------ - -% jpl_is_class(?X) :- -% X is a JPL ref to a java.lang.Class object - -jpl_is_class(X) :- - jpl_is_object(X), - jpl_object_to_type(X, class([java,lang],['Class'])). - -%------------------------------------------------------------------------------ - -% jpl_is_false(?X) :- -% X is the proper JPL datum which represents the Java boolean value 'false'; -% whatever, no further instantiation of X occurs - -jpl_is_false(X) :- - X == @(false). - -%------------------------------------------------------------------------------ - -% jpl_is_fieldID(?X) :- -% X is a proper JPL field ID structure (jfieldID/1); -% applications should not be messing with these (?); -% whatever, no further instantiation of X occurs - -jpl_is_fieldID(jfieldID(X)) :- % NB a var arg may get bound... - integer(X). - -%------------------------------------------------------------------------------ - -% jpl_is_methodID(?X) :- -% X is a proper JPL method ID structure (jmethodID/1); -% applications should not be messing with these (?); -% whatever, no further instantiation of X occurs - -jpl_is_methodID(jmethodID(X)) :- % NB a var arg may get bound... - integer(X). - -%------------------------------------------------------------------------------ - -% jpl_is_null(?X) :- -% X is the proper JPL datum which represents Java's 'null' reference; -% whatever, no further instantiation of X occurs - -jpl_is_null(X) :- - X == @(null). - -%------------------------------------------------------------------------------ - -% jpl_is_object(?X) :- -% X is a proper, plausible JPL object reference; -% NB this checks only syntax, not whether the object exists; -% whatever, no further instantiation of X occurs - -jpl_is_object(X) :- - jpl_is_ref(X), % (syntactically, at least...) - X \== @(null). - -%------------------------------------------------------------------------------ - -% jpl_is_object_type(+T) :- -% T is an object (class or array) type, -% not e.g. a primitive, null or void - -jpl_is_object_type(T) :- - \+ var(T), - jpl_non_var_is_object_type(T). - -%------------------------------------------------------------------------------ - -% jpl_is_ref(?T) :- -% the arbitrary term T is a proper, syntactically plausible JPL reference, -% either to a Java object -% (which may not exist, although a jpl_is_current_ref/1 might be useful) -% or to Java's notional but important 'null' non-object; -% whatever, no further instantiation of X occurs; -% NB to distinguish tags from void/false/true, -% could check initial character(s) or length? or adopt strong/weak scheme... - -jpl_is_ref(@(Y)) :- - atom(Y), % presumably a (garbage-collectable) tag - Y \== void, % not a ref - Y \== false, % not a ref - Y \== true. % not a ref - -%------------------------------------------------------------------------------ - -% jpl_is_true(?X) :- -% X is a proper JPL datum, representing the Java boolean value 'true'; -% whatever, no further instantiation of X occurs - -jpl_is_true(X) :- - X == @(true). - -%------------------------------------------------------------------------------ - -% jpl_is_type(+X) :- - -jpl_is_type(X) :- - ground(X), - jpl_ground_is_type(X). - -%------------------------------------------------------------------------------ - -% jpl_is_void(?X) :- -% X is the proper JPL datum which represents the pseudo Java value 'void' -% (which is returned by jpl_call/4 when invoked on void methods); -% NB you can try passing 'void' back to Java, but it won't ever be interested; -% whatever, no further instantiation of X occurs - -jpl_is_void(X) :- - X == @(void). - -%------------------------------------------------------------------------------ - -jpl_lineage_types_type_to_common_lineage_types(Ts, Tx, Ts0) :- - ( append(_, [Tx|Ts2], Ts) - -> [Tx|Ts2] = Ts0 - ; jpl_type_to_super_type(Tx, Tx2) - -> jpl_lineage_types_type_to_common_lineage_types(Ts, Tx2, Ts0) - ). - -%------------------------------------------------------------------------------ - -jpl_non_var_is_object_type(class(_,_)). - -jpl_non_var_is_object_type(array(_)). - -%------------------------------------------------------------------------------ - -% jpl_null(-X) :- -% X is (by unification) the proper JPL datum which represents the Java reference 'null'; -% c.f. jpl_is_null/1 - -jpl_null(@(null)). - -%------------------------------------------------------------------------------ - -% jpl_object_array_to_list(+ArrayObject, -Values) :- -% Values is a list of JPL values (primitive values or object references) -% representing the respective elements of ArrayObject - -jpl_object_array_to_list(A, Vs) :- - jpl_array_to_length(A, N), - jpl_object_array_to_list_1(A, 0, N, Vs). - -%------------------------------------------------------------------------------ - -% jpl_object_array_to_list_1(+A, +I, +N, -Xs) :- - -jpl_object_array_to_list_1(A, I, N, Xs) :- - ( I == N - -> Xs = [] - ; jGetObjectArrayElement(A, I, X), - Xs = [X|Xs2], - J is I+1, - jpl_object_array_to_list_1(A, J, N, Xs2) - ). - -%------------------------------------------------------------------------------ - -% jpl_object_to_class(+Object, -Class) :- -% Object must be a valid object (should this throw an exception otherwise?); -% Class is a (canonical) reference to the (canonical) class object -% which represents the class of Object; -% NB wot's the point of caching the type if we don't look there first? - -jpl_object_to_class(Obj, C) :- - jGetObjectClass(Obj, C), - jpl_cache_type_of_ref(class([java,lang],['Class']), C). - -%------------------------------------------------------------------------------ - -% jpl_object_to_type(+Object, -Type) :- -% Object must be a proper JPL reference to a Java object -% (i.e. a class or array instance, but not null, void or String); -% Type is the JPL type of that object - -jpl_object_to_type(@(Tag), Type) :- - jpl_tag_to_type(Tag, Type). - -%------------------------------------------------------------------------------ - -jpl_object_type_to_super_type(T, Tx) :- - ( ( T = class(_,_) - ; T = array(_) - ) - -> jpl_type_to_class(T, C), - jpl_class_to_super_class(C, Cx), - Cx \== @(null), - jpl_class_to_type(Cx, Tx) - ). - -%------------------------------------------------------------------------------ - -% jpl_primitive_buffer_to_array(+Type, +Xc, +Bp, +I, +Size, -Vcs) :- -% Bp points to a buffer of (sufficient) Type values; -% Vcs will be unbound on entry, -% and on exit will be a list of Size of them, starting at index I -% (the buffer is indexed from zero) - -jpl_primitive_buffer_to_array(T, Xc, Bp, I, Size, [Vc|Vcs]) :- - jni_fetch_buffer_value(Bp, I, Vc, Xc), - Ix is I+1, - ( Ix < Size - -> jpl_primitive_buffer_to_array(T, Xc, Bp, Ix, Size, Vcs) - ; Vcs = [] - ). - -%------------------------------------------------------------------------------ - -jpl_primitive_type(boolean). -jpl_primitive_type(char). -jpl_primitive_type(byte). -jpl_primitive_type(short). -jpl_primitive_type(int). -jpl_primitive_type(long). -jpl_primitive_type(float). -jpl_primitive_type(double). - -%------------------------------------------------------------------------------ - -% jpl_primitive_type_default_value(-Type, -Value) :- -% each element of any array of (primitive) Type created by jpl_new/3, -% or any instance of (primitive) Type created by jpl_new/3, -% should be initialised to Value (to mimic Java semantics) - -jpl_primitive_type_default_value(boolean, @(false)). -jpl_primitive_type_default_value(char, 0). -jpl_primitive_type_default_value(byte, 0). -jpl_primitive_type_default_value(short, 0). -jpl_primitive_type_default_value(int, 0). -jpl_primitive_type_default_value(long, 0). -jpl_primitive_type_default_value(float, 0.0). -jpl_primitive_type_default_value(double, 0.0). - -%------------------------------------------------------------------------------ - -jpl_primitive_type_super_type(T, Tx) :- - ( jpl_type_fits_type_direct_prim(T, Tx) - ; jpl_type_fits_type_direct_xtra(T, Tx) - ). - -%------------------------------------------------------------------------------ - -% jpl_primitive_type_term_to_value(+Type, +Term, -Val) :- -% Term, after widening iff appropriate, represents an instance of Type; -% Val is the instance of Type which it represents (often the same thing); -% currently used only by jpl_new_1 when creating an "instance" -% of a primitive type (which may be misguided completism - you can't -% do that in Java) - -jpl_primitive_type_term_to_value(Type, Term, Val) :- - ( jpl_primitive_type_term_to_value_1(Type, Term, Val) - -> true - ). - -%------------------------------------------------------------------------------ - -% jpl_primitive_type_term_to_value_1(+Type, +RawValue, -WidenedValue) :- -% I'm not worried about structure duplication here -% NB this oughta be done in foreign code... - -jpl_primitive_type_term_to_value_1(boolean, @(false), @(false)). - -jpl_primitive_type_term_to_value_1(boolean, @(true), @(true)). - -jpl_primitive_type_term_to_value_1(char, I, I) :- - integer(I), - I >= 0, - I =< 65535. % (2**16)-1. - -jpl_primitive_type_term_to_value_1(byte, I, I) :- - integer(I), - I >= 128, % -(2**7) - I =< 127. % (2**7)-1 - -jpl_primitive_type_term_to_value_1(short, I, I) :- - integer(I), - I >= -32768, % -(2**15) - I =< 32767. % (2**15)-1 - -jpl_primitive_type_term_to_value_1(int, I, I) :- - integer(I), - I >= -2147483648, % -(2**31) - I =< 2147483647. % (2**31)-1 - -jpl_primitive_type_term_to_value_1(long, I, I) :- - integer(I), - I >= -9223372036854775808, % -(2**63) - I =< 9223372036854775807. % (2**63)-1 - -jpl_primitive_type_term_to_value_1(float, I, F) :- - integer(I), - F is float(I). - -jpl_primitive_type_term_to_value_1(float, F, F) :- - float(F). - -jpl_primitive_type_term_to_value_1(double, I, F) :- - integer(I), - F is float(I). - -jpl_primitive_type_term_to_value_1(double, F, F) :- - float(F). - -%------------------------------------------------------------------------------ - -jpl_primitive_type_to_ancestor_types(T, Ts) :- - ( jpl_primitive_type_super_type(T, Ta) - -> Ts = [Ta|Tas], - jpl_primitive_type_to_ancestor_types(Ta, Tas) - ; Ts = [] - ). - -%------------------------------------------------------------------------------ - -jpl_primitive_type_to_super_type(T, Tx) :- - jpl_primitive_type_super_type(T, Tx). - -%------------------------------------------------------------------------------ - -% jpl_ref_to_type(+Ref, -Type) :- -% Ref must be a proper JPL reference (to an object, null or void); -% Type is its type - -jpl_ref_to_type(@(X), T) :- - ( X == null - -> T = null - ; X == void - -> T = void - ; jpl_tag_to_type(X, T) - ). - -%------------------------------------------------------------------------------ - -% jpl_tag_to_type(+Tag, -Type) :- -% Tag must be an (atomic) object tag; -% Type is its type (either from the cache or by reflection); - -jpl_tag_to_type(Tag, Type) :- - jni_tag_to_iref(Tag, Iref), - ( jpl_iref_type_cache(Iref, T) - -> true % T is Tag's type - ; jpl_object_to_class(@(Tag), Cobj), % else get ref to class obj - jpl_class_to_type(Cobj, T), % get type of class it denotes - jpl_assert(jpl_iref_type_cache(Iref,T)) - ), - Type = T. - -%------------------------------------------------------------------------------ - -% jpl_true(-X) :- -% X is (by unification) the proper JPL datum which represents the Java boolean value 'true'; -%cf jpl_is_true/1 - -jpl_true(@(true)). - -%------------------------------------------------------------------------------ - -% jpl_type_fits_type(+TypeX, +TypeY) :- -% TypeX and TypeY must each be proper JPL types; -% this succeeds iff TypeX is assignable to TypeY - -jpl_type_fits_type(Tx, Ty) :- - ( jpl_type_fits_type_1(Tx, Ty) - -> true - ). - -%------------------------------------------------------------------------------ - -% jpl_type_fits_type_1(+T1, +T2) :- -% it doesn't matter that this leaves choicepoints; it serves only jpl_type_fits_type/2 - -jpl_type_fits_type_1(T, T). - -jpl_type_fits_type_1(class(Ps1,Cs1), class(Ps2,Cs2)) :- - jpl_type_to_class(class(Ps1,Cs1), C1), - jpl_type_to_class(class(Ps2,Cs2), C2), - jIsAssignableFrom(C1, C2). - -jpl_type_fits_type_1(array(T1), class(Ps2,Cs2)) :- - jpl_type_to_class(array(T1), C1), - jpl_type_to_class(class(Ps2,Cs2), C2), - jIsAssignableFrom(C1, C2). - -jpl_type_fits_type_1(array(T1), array(T2)) :- - jpl_type_to_class(array(T1), C1), - jpl_type_to_class(array(T2), C2), - jIsAssignableFrom(C1, C2). - -jpl_type_fits_type_1(null, class(_,_)). - -jpl_type_fits_type_1(null, array(_)). - -jpl_type_fits_type_1(T1, T2) :- - jpl_type_fits_type_xprim(T1, T2). - -%------------------------------------------------------------------------------ - -jpl_type_fits_type_direct_prim(float, double). -jpl_type_fits_type_direct_prim(long, float). -jpl_type_fits_type_direct_prim(int, long). -jpl_type_fits_type_direct_prim(char, int). -jpl_type_fits_type_direct_prim(short, int). -jpl_type_fits_type_direct_prim(byte, short). - -%------------------------------------------------------------------------------ - -jpl_type_fits_type_direct_xprim(Tp, Tq) :- - jpl_type_fits_type_direct_prim(Tp, Tq). - -jpl_type_fits_type_direct_xprim(Tp, Tq) :- - jpl_type_fits_type_direct_xtra(Tp, Tq). - -%------------------------------------------------------------------------------ - -% jpl_type_fits_type_direct_xtra(-PseudoType, -ConcreteType) :- -% this predicate defines the direct subtype-supertype relationships -% which involve the intersection pseudo types char_int, char_short and char_byte - -jpl_type_fits_type_direct_xtra(char_int, int). % char_int is a direct subtype of int -jpl_type_fits_type_direct_xtra(char_int, char). % etc. -jpl_type_fits_type_direct_xtra(char_short, short). -jpl_type_fits_type_direct_xtra(char_short, char). -jpl_type_fits_type_direct_xtra(char_byte, byte). -jpl_type_fits_type_direct_xtra(char_byte, char). - -jpl_type_fits_type_direct_xtra(overlong, float). % 6/Oct/2006 experiment - -%------------------------------------------------------------------------------ - -% jpl_type_fits_type_xprim(-Tp, -T) :- -% indeterminate; -% serves only jpl_type_fits_type_1/2 - -jpl_type_fits_type_xprim(Tp, T) :- - jpl_type_fits_type_direct_xprim(Tp, Tq), - ( Tq = T - ; jpl_type_fits_type_xprim(Tq, T) - ). - -%------------------------------------------------------------------------------ - -% jpl_type_to_ancestor_types(+T, -Tas) :- -% this does not accommodate the assignability of null, -% but that's OK (?) since "type assignability" and "type ancestry" are not equivalent - -jpl_type_to_ancestor_types(T, Tas) :- - ( ( T = class(_,_) - ; T = array(_) - ) - -> jpl_type_to_class(T, C), - jpl_class_to_ancestor_classes(C, Cas), - jpl_classes_to_types(Cas, Tas) - ; jpl_primitive_type_to_ancestor_types(T, Tas) - -> true - ). - -%------------------------------------------------------------------------------ - -% jpl_type_to_canonical_type(+Type, -CanonicalType) :- -% Type must be a type, not necessarily canonical; -% CanonicalType will be equivalent and canonical - -%eg jpl_type_to_canonical_type(class([],[byte]), byte) - -jpl_type_to_canonical_type(array(T), array(Tc)) :- - !, - jpl_type_to_canonical_type(T, Tc). - -jpl_type_to_canonical_type(class([],[void]), void) :- - !. - -jpl_type_to_canonical_type(class([],[N]), N) :- - jpl_primitive_type(N), - !. - -jpl_type_to_canonical_type(class(Ps,Cs), class(Ps,Cs)) :- - !. - -jpl_type_to_canonical_type(void, void) :- - !. - -jpl_type_to_canonical_type(P, P) :- - jpl_primitive_type(P). - -%------------------------------------------------------------------------------ - -% jpl_type_to_class(+Type, -ClassObject) :- -% incomplete types are now never cached (or otherwise passed around); -% jFindClass throws an exception if FCN can't be found - -%nb if this is public API maybe oughta restore the ground(T) check and throw exception - -jpl_type_to_class(T, @(Tag)) :- - % ground(T), % 9/Nov/2004 removed this spurious (?) check - ( jpl_class_tag_type_cache(ClassTag,T) - -> Tag = ClassTag - ; ( jpl_type_to_findclassname(T, FCN) % peculiar syntax for FindClass() - -> jFindClass(FCN, @(ClassTag)), % which caches type of @ClassTag - % jpl_cache_type_of_ref(T, @(ClassTag)) - jpl_cache_type_of_ref(class([java,lang],['Class']), @(ClassTag)) % 9/Nov/2004 bugfix (?) - ), - jpl_assert(jpl_class_tag_type_cache(ClassTag,T)) - ), - Tag = ClassTag. - -%------------------------------------------------------------------------------ - -% jpl_type_to_nicename(+Type, -NiceName) :- -% Type, which is a class or array type (not sure about the others...), -% is denoted by ClassName in dotted syntax - -%nb is this used? is "nicename" well defined and necessary? -%nb this could use caching if indexing were amenable - -%eg jpl_type_to_nicename(class([java,util],['Date']), 'java.util.Date') -%eg jpl_type_to_nicename(boolean, boolean) - -%cf jpl_type_to_classname/2 - -jpl_type_to_nicename(T, NN) :- - ( jpl_primitive_type( T) - -> NN = T - ; ( phrase(jpl_type_classname_1(T), Cs) - -> atom_codes(CNx, Cs), % green commit to first solution - NN = CNx - ) - ). - -%------------------------------------------------------------------------------ - -% jpl_type_to_classname(+Type, -ClassName) :- -% Type, which is a class or array type (not sure about the others...), -% is denoted by ClassName in dotted syntax - -%eg jpl_type_to_classname(class([java,util],['Date']), 'java.util.Date') - -%cf jpl_type_to_nicename/2 - -jpl_type_to_classname(T, CN) :- - ( phrase(jpl_type_classname_1(T), Cs) - -> atom_codes(CNx, Cs), % green commit to first solution - CN = CNx - ). - -%------------------------------------------------------------------------------ - -% jpl_type_to_descriptor(+Type, -Descriptor) :- -% Type (denoting any Java type) -% (can also be a JPL method/2 structure (?!)) -% is represented by Descriptor (JVM internal syntax) -% I'd cache this, but I'd prefer more efficient indexing on types (hashed?) - -jpl_type_to_descriptor(T, D) :- - ( phrase(jpl_type_descriptor_1(T), Cs) - -> atom_codes(Dx, Cs), - D = Dx - ). - -%------------------------------------------------------------------------------ - -% jpl_type_to_findclassname(+Type, -FindClassName) :- -% FindClassName denotes Type (class or array only) -% in the syntax required peculiarly by FindClass() - -jpl_type_to_findclassname(T, FCN) :- - ( phrase(jpl_type_findclassname(T), Cs) - -> atom_codes(FCNx, Cs), - FCN = FCNx - ). - -%------------------------------------------------------------------------------ - -% jpl_type_to_super_type(+Type, -SuperType) :- -% Type oughta be a proper JPL type; -% SuperType is the (at most one) type which it directly implements (if it's a class); -% if Type denotes a class, this works only if that class can be found; -% if Type = array(Type) then I dunno what happens... - -jpl_type_to_super_type(T, Tx) :- - ( jpl_object_type_to_super_type(T, Tx) - -> true - ; jpl_primitive_type_to_super_type(T, Tx) - -> true - ). - -%------------------------------------------------------------------------------ - -% jpl_type_to_preferred_concrete_type( +Type, -ConcreteType) :- -% Type must be a canonical JPL type, -% possibly a pseudo (inferred) type such as char_int or array(char_byte); -% ConcreteType is the preferred concrete (Java-instantiable) type; -% introduced 16/Apr/2005 to fix bug whereby jpl_list_to_array([1,2,3],A) failed -% because the lists's inferred type of array(char_byte) is not Java-instantiable - -jpl_type_to_preferred_concrete_type( T, Tc) :- - ( jpl_type_to_preferred_concrete_type_1( T, TcX) - -> Tc = TcX - ). - -%------------------------------------------------------------------------------ - -jpl_type_to_preferred_concrete_type_1( char_int, int). - -jpl_type_to_preferred_concrete_type_1( char_short, short). - -jpl_type_to_preferred_concrete_type_1( char_byte, byte). - -jpl_type_to_preferred_concrete_type_1( array(T), array(Tc)) :- - jpl_type_to_preferred_concrete_type_1( T, Tc). - -jpl_type_to_preferred_concrete_type_1( T, T). - -%------------------------------------------------------------------------------ - -% jpl_types_fit_type(+Types, +Type) :- -% each member of Types is (independently) (if that means anything) -% assignable to Type -% e.g. for dynamic type check when attempting to assign list of values to array - -jpl_types_fit_type([], _). - -jpl_types_fit_type([T1|T1s], T2) :- - jpl_type_fits_type(T1, T2), - jpl_types_fit_type(T1s, T2). - -%------------------------------------------------------------------------------ - -% jpl_types_fit_types(+Types1, +Types2) :- -% each member type of Types1 "fits" the respective member type of Types2 - -jpl_types_fit_types([], []). - -jpl_types_fit_types([T1|T1s], [T2|T2s]) :- - jpl_type_fits_type(T1, T2), - jpl_types_fit_types(T1s, T2s). - -%------------------------------------------------------------------------------ - -% jpl_value_to_type(+Value, -Type) :- -% Value must be a proper JPL datum other than a ref -% i.e. primitive, String or void; -% it is of (unique most specific) Type, -% which may be one of the pseudo types char_byte, char_short or char_int - -jpl_value_to_type(V, T) :- - ground(V), % critically assumed by jpl_value_to_type_1/2 - ( jpl_value_to_type_1(V, Tv) % 2nd arg must be unbound - -> T = Tv - ). - -%------------------------------------------------------------------------------ - -%% jpl_value_to_type_1(+Value, -Type) is semidet. -% -% Type is the unique most specific JPL type of which Value -% represents an instance; called solely by jpl_value_to_type/2, -% which commits to first solution; -% -% NB some integer values are of JPL-peculiar uniquely most -% specific subtypes, i.e. char_byte, char_short, char_int but all -% are understood by JPL's internal utilities which call this proc -% -% NB we regard float as subtype of double -% -% NB objects and refs always have straightforward types - -jpl_value_to_type_1(@(false), boolean) :- !. -jpl_value_to_type_1(@(true), boolean) :- !. -jpl_value_to_type_1(A, class([java,lang],['String'])) :- % yes it's a "value" - atom(A), !. -jpl_value_to_type_1(I, T) :- - integer(I), !, - ( I >= 0 - -> ( I < 128 - -> T = char_byte - ; I < 32768 -> T = char_short - ; I < 65536 -> T = char_int - ; I < 2147483648 -> T = int - ; I =< 9223372036854775807 -> T = long - ; T = overlong - ) - ; I >= -128 -> T = byte - ; I >= -32768 -> T = short - ; I >= -2147483648 -> T = int - ; I >= -9223372036854775808 -> T = long - ; T = overlong - ). -jpl_value_to_type_1(F, float) :- - float(F). - -%------------------------------------------------------------------------------ - -% jpl_void(-X) :- -% X is (by unification) the proper JPL datum which represents the pseudo Java value 'void'; -% c.f. jpl_is_void/1 - -jpl_void(@(void)). - -%------------------------------------------------------------------------------ - -%type jpl_array_to_length(array, integer) - -% jpl_array_to_length(+ArrayObject, -Length) :- -% must validate ArrayObject before making the JNI call... - -jpl_array_to_length(A, N) :- - ( jpl_ref_to_type(A, array(_)) % can this be done cheaper e.g. in foreign code? - -> jGetArrayLength(A, N) % *must* be array, else undefined (crash?) - ). - -%------------------------------------------------------------------------------ - -%type jpl_array_to_list(array, list(datum)) - -% jpl_array_to_list(+Array, -Elements) :- - -jpl_array_to_list(A, Es) :- - jpl_array_to_length(A, Len), - ( Len > 0 - -> LoBound is 0, - HiBound is Len-1, - jpl_get(A, LoBound-HiBound, Es) - ; Es = [] - ). - -%------------------------------------------------------------------------------ - -%type jpl_datums_to_array(list(datum), array) - -% jpl_datums_to_array(+Ds, -A) :- -% A will be a ref to a new JVM array, -% whose base type is the most specific Java type -% of which each member of Datums is (directly or indirectly) an instance; -% NB this fails (without warning, currently) if: -% Ds is an empty list (no base type can be inferred) -% Ds contains a primitive value and an object or array ref (no common supertype) - -jpl_datums_to_array(Ds, A) :- - ground(Ds), - jpl_datums_to_most_specific_common_ancestor_type(Ds, T), % T may be pseudo e.g. char_byte - jpl_type_to_preferred_concrete_type( T, Tc), % bugfix added 16/Apr/2005 - jpl_new(array(Tc), Ds, A). - -%------------------------------------------------------------------------------ - -%type jpl_enumeration_element(object, datum) - -% jpl_enumeration_element(+Enumeration, -Element) :- -% generates each Element from the Enumeration; -% if the element is a java.lang.String then Element will be an atom; -% if the element is null then Element will (oughta) be null; -% otherwise I reckon it has to be an object ref - -jpl_enumeration_element(En, E) :- - ( jpl_call(En, hasMoreElements, [], @(true)) - -> jpl_call(En, nextElement, [], Ex), - ( E = Ex - ; jpl_enumeration_element(En, E) - ) - ). - -%------------------------------------------------------------------------------ - -%type jpl_enumeration_to_list(object, list(datum)) - -% jpl_enumeration_to_list(+Enumeration, -Elements) :- - -jpl_enumeration_to_list(EN, Es) :- - ( jpl_call(EN, hasMoreElements, [], @(true)) - -> jpl_call(EN, nextElement, [], E), - Es = [E|Es1], - jpl_enumeration_to_list(EN, Es1) - ; Es = [] - ). - -%------------------------------------------------------------------------------ - -%type jpl_hashtable_pair(object, pair(datum,datum)) - -% jpl_hashtable_pair(+HashTable, -KeyValuePair) :- -% generates Key-Value pairs from the given HashTable -% NB String is converted to atom but Integer is presumably returned as an object ref -% (i.e. as elsewhere, no auto unboxing); -%nb this is anachronistic (oughta use the Map interface?) - -jpl_hashtable_pair(HT, K-V) :- - jpl_call(HT, keys, [], Ek), - jpl_enumeration_to_list(Ek, Ks), - member(K, Ks), - jpl_call(HT, get, [K], V). - -%------------------------------------------------------------------------------ - -%type jpl_iterator_element(object, datum) - -% jpl_iterator_element(+Iterator, -Element) :- - -jpl_iterator_element(I, E) :- - ( jpl_call(I, hasNext, [], @(true)) - -> ( jpl_call(I, next, [], E) % surely it's steadfast... - ; jpl_iterator_element(I, E) - ) - ). - -%------------------------------------------------------------------------------ - -%type jpl_list_to_array(list(datum), array) - -% jpl_list_to_array(+Datums, -Array) :- -% Datums is a proper list of JPL datums (values or refs); -% if they have a most specific common supertype, -% Array is an array, of that base type, -% whose respective elements are Datums - -jpl_list_to_array(Ds, A) :- - jpl_datums_to_array(Ds, A). - -% vsc: another hack -%------------------------------------------------------------------------------ - -%type jpl_list_to_array(list(datum), supertype, array) - -% jpl_list_to_array(+Datums, -Array) :- -% Datums is a proper list of JPL datums (values or refs); -% if they have a most specific common supertype, -% Array is an array, of that base type, -% whose respective elements are Datums - -jpl_list_to_array(Ds, Tc, A) :- - jpl_new(array(Tc), Ds, A). - -%------------------------------------------------------------------------------ - -%type jpl_terms_to_array(list(term), array) - -% jpl_terms_to_array(+Terms, -Array) :- -% Terms is a proper list of arbitrary terms; -% Array is an array of jpl.Term, -% whose elements represent the respective members of the list - -jpl_terms_to_array(Ts, A) :- - jpl_terms_to_array_1(Ts, Ts2), - jpl_new( array(class([jpl],['Term'])), Ts2, A). - -%------------------------------------------------------------------------------ - -jpl_terms_to_array_1([], []). - -jpl_terms_to_array_1([T|Ts], [{T}|Ts2]) :- - jpl_terms_to_array_1(Ts, Ts2). - -%------------------------------------------------------------------------------ - -%type jpl_map_element(object, pair(datum,datum)) - -% jpl_map_element(+Map, -KeyValue) :- -% Map must be an instance of any implementation of the java.util.Map interface; -% this generates each Key-Value pair from the Map - -jpl_map_element(M, K-V) :- - jpl_call(M, entrySet, [], ES), - jpl_set_element(ES, E), - jpl_call(E, getKey, [], K), - jpl_call(E, getValue, [], V). - -%------------------------------------------------------------------------------ - -%type jpl_set_element(object, datum) - -% jpl_set_element(+Set, -Element) :- - -jpl_set_element(S, E) :- - jpl_call(S, iterator, [], I), - jpl_iterator_element(I, E). - -%------------------------------------------------------------------------------ - -% is_pair(?T) :- -% I define a half-decent "pair" as having a ground key (any val) - -is_pair(0) :- !, fail. -is_pair(Key-_Val) :- - ground(Key). - -%------------------------------------------------------------------------------ - -is_pairs(0) :- !, fail. -is_pairs([]). -is_pairs([H|T]) :- - is_pair(H), - is_pairs(T). - -%------------------------------------------------------------------------------ - -multimap_to_atom(KVs, A) :- - multimap_to_atom_1(KVs, "", Cz, []), - flatten(Cz, Cs), - atom_codes(A, Cs). - -%------------------------------------------------------------------------------ - -multimap_to_atom_1([], _, Cs, Cs). -multimap_to_atom_1([K-V|KVs], T, Cs1, Cs0) :- - atom_codes(K, CsK), - Cs1 = [T,CsK," = "|Cs2], - ( is_list(V) - -> ( is_pairs(V) - -> V = V2 - ; findall(N-Ve, nth1(N, V, Ve), V2) - ), - T2 = [" ",T], - Cs2 = [10|Cs2a], - multimap_to_atom_1(V2, T2, Cs2a, Cs3) - ; term_to_codes(V, CsV), - Cs2 = [CsV,10|Cs3] - ), - multimap_to_atom_1(KVs, T, Cs3, Cs0). - -%------------------------------------------------------------------------------ - -%% term_to_codes(+Term, ?Codes) -% -% unifies Codes with a printed representation of Term. -% -% @tbd Sort of quoting requirements and use format(codes(Codes), -% ...) - -term_to_codes(Term, Codes) :- - ( atom(Term) - -> Term = A % avoid superfluous quotes - ; system:term_to_atom(Term, A) - ), - atom_codes(A, Codes). - -%------------------------------------------------------------------------------ - - /******************************* - * MESSAGES * - *******************************/ - -:- multifile - prolog:error_message/3. - -prolog:error_message(java_exception(Ex)) --> - ( { jpl_call(Ex, toString, [], Msg) - } - -> [ 'Java exception: ~w'-[Msg] ] - ; [ 'Java exception: ~w'-[Ex] ] - ). - - - /******************************* - * PATHS * - *******************************/ - -:- multifile user:file_search_path/2. -:- dynamic user:file_search_path/2. - -%user:file_search_path(jar, swi(lib)). -%vsc: yap -user:file_search_path(jar, library('')). - -%% add_search_path(+Var, +Value) is det. -% -% Add value to the end of search-path Var. Value is normally a -% directory. Does not change the environment if Dir is already in -% Var. -% -% @param Value Path to add in OS notation. - -add_search_path(Path, Dir) :- - ( getenv(Path, Old) - -> ( current_prolog_flag(windows, true) - -> Sep = (;) - ; Sep = (:) - ), - ( concat_atom(Current, Sep, Old), - memberchk(Dir, Current) - -> true % already present - ; concat_atom([Old, Sep, Dir], New), - setenv(Path, New) - ) - ; setenv(Path, Dir) - ). - -%% search_path_separator(-Sep:atom) -% -% Separator used the the OS in =PATH=, =LD_LIBRARY_PATH=, -% =CLASSPATH=, etc. - -search_path_separator((;)) :- - current_prolog_flag(windows, true), !. -search_path_separator(:). - - /******************************* - * LOAD THE JVM * - *******************************/ - -%% check_java_environment -% -% Verify the Java environment. Preferably we would create, but -% most Unix systems do not allow putenv("LD_LIBRARY_PATH=..." in -% the current process. A suggesting found on the net is to modify -% LD_LIBRARY_PATH right at startup and next execv() yourself, but -% this doesn't work if we want to load Java on demand or if Prolog -% itself is embedded in another application. -% -% So, after reading lots of pages on the web, I decided checking -% the environment and producing a sensible error message is the -% best we can do. -% -% Please not that Java2 doesn't require $CLASSPATH to be set, so -% we do not check for that. - -check_java_environment :- - check_lib(java), - check_lib(jvm). - -check_lib(Name) :- - check_shared_object(Name, File, EnvVar, Absolute), - ( Absolute == (-) - -> ( current_prolog_flag(windows, true) - -> A = '%', Z = '%' - ; A = '$', Z = '' - ), - format(string(Msg), 'Please add directory holding ~w to ~w~w~w', - [ File, A, EnvVar, Z ]), - throw(error(existence_error(library, Name), - context(_, Msg))) - ; true - ). - -%% check_shared_object(+Lib, -File, -EnvVar, -AbsFile) is semidet. -% -% True if AbsFile is existing .so/.dll file for Lib. -% -% @param File Full name of Lib (i.e. libjpl.so or jpl.dll) -% @param EnvVar Search-path for shared objects. - -check_shared_object(Name, File, EnvVar, Absolute) :- - libfile(Name, File), - library_search_path(Path, EnvVar), - ( member(Dir, Path), - concat_atom([Dir, File], /, Absolute), - exists_file(Absolute) - -> true - ; Absolute = (-) - ). - -libfile(Base, File) :- - current_prolog_flag(unix, true), !, - atom_concat(lib, Base, F0), - current_prolog_flag(shared_object_extension, Ext), - file_name_extension(F0, Ext, File). -libfile(Base, File) :- - current_prolog_flag(windows, true), !, - current_prolog_flag(shared_object_extension, Ext), - file_name_extension(Base, Ext, File). - - -%% library_search_path(-Dirs:list, -EnvVar) is det. -% -% Dirs is the list of directories searched for shared -% objects/DLLs. EnvVar is the variable in which the search path os -% stored. - -library_search_path(Path, EnvVar) :- - current_prolog_flag(shared_object_search_path, EnvVar), - search_path_separator(Sep), - ( getenv(EnvVar, Env), - concat_atom(Path, Sep, Env) - -> true - ; Path = [] - ). - - -%% add_jpl_to_classpath -% -% Add jpl.jar to =CLASSPATH= to facilitate callbacks - -add_jpl_to_classpath :- - absolute_file_name(jar('jpl.jar'), - [ access(read) - ], JplJAR), !, - ( getenv('CLASSPATH', Old) - -> true - ; Old = '.' - ), - ( current_prolog_flag(windows, true) - -> Separator = ';' - ; Separator = ':' - ), - concat_atom([JplJAR, Old], Separator, New), - setenv('CLASSPATH', New). - - -%% libjpl(-Spec) is det. -% -% Return the spec for loading the JPL shared object. This shared -% object must be called libjpl.so as the Java System.loadLibrary() -% call used by jpl.jar adds the lib* prefix. - -libjpl(File) :- - ( current_prolog_flag(unix, true), \+ yap % vsc: why? - -> File = foreign(libjpl) - ; File = foreign(jpl) - ). - -%% add_jpl_to_ldpath(+JPL) is det. -% -% Add the directory holding jpl.so to search path for dynamic -% libraries. This is needed for callback from Java. Java appears -% to use its own search and the new value of the variable is -% picked up correctly. - -add_jpl_to_ldpath(JPL) :- - absolute_file_name(JPL, File, [file_type(executable), file_errors(fail)]), !, - file_directory_name(File, Dir), - prolog_to_os_filename(Dir, OsDir), - current_prolog_flag(shared_object_search_path, PathVar), - add_search_path(PathVar, OsDir). -add_jpl_to_ldpath(_). - -%% add_java_to_ldpath is det. -% -% Adds the directories holding jvm.dll and java.dll to the %PATH%. -% This appears to work on Windows. Unfortunately most Unix systems -% appear to inspect the content of LD_LIBRARY_PATH only once. - -add_java_to_ldpath :- - current_prolog_flag(windows, true), !, - add_java_dir(jvm, '\\jre\\bin\\client', Extra1), - add_java_dir(java, '\\jre\\bin', Extra2), - append(Extra1, Extra2, Extra), - ( Extra \== [] - -> print_message(informational, - format('Added ~w to %PATH%', [Extra])), - getenv('PATH', Path0), - concat_atom([Path0|Extra], ';', Path), - setenv('PATH', Path) - ; true - ). -add_java_to_ldpath. - -add_java_dir(DLL, SubPath, Dirs) :- - ( check_shared_object(DLL, _, _Var, Abs), - Abs \== (-) - -> Dirs = [] - ; java_home(JavaHome) - -> atom_concat(JavaHome, SubPath, ClientDir), - Dirs = [ClientDir] - ; Dirs = [] - ). - -%% java_home(-Home) is semidet -% -% Find the home location of Java. -% -% @param Home JAVA home in OS notation - -java_home(Home) :- - getenv('JAVA_HOME', Home), - exists_directory(Home), !. -java_home(Home) :- - current_prolog_flag(windows, true), !, - Key0 = 'HKEY_LOCAL_MACHINE/Software/JavaSoft/Java Development Kit', - win_registry_get_value(Key0, 'CurrentVersion', Version), - concat_atom([Key0, Version], /, Key), - win_registry_get_value(Key, 'JavaHome', Home), - exists_directory(Home), !. -java_home(Home) :- - current_prolog_flag(unix, true), - % vsc: use this info from configure. - (yap -> - jpl_java_home(P), - member(Home, [ P, - '/usr/lib/java', - '/usr/local/lib/java' - ]) - ; - member(Home, [ P, - '/usr/lib/java', - '/usr/local/lib/java' - ]) - ), - exists_directory(Home), !. - -:- dynamic - jvm_ready/0. -:- volatile - jvm_ready/0. - -setup_jvm :- - jvm_ready, !. -setup_jvm :- - add_jpl_to_classpath, - add_java_to_ldpath, - libjpl(JPL), - add_jpl_to_ldpath(JPL), - % vsc: - ( yap -> - catch(load_jpl_lib, E, report_java_setup_problem(E)) - ; - catch(load_foreign_library(JPL), E, report_java_setup_problem(E)) - ), - assert(jvm_ready). - -report_java_setup_problem(E) :- - print_message(error, E), - check_java_environment. - -% vsc: run this from jpl.yap, not after loading jpl.yap -:- yap, setup_jvm. - -:- initialization - setup_jvm. diff --git a/LGPL/JPL/jpl_paths.yap.in b/LGPL/JPL/jpl_paths.yap.in deleted file mode 100644 index 2c0c94f3b..000000000 --- a/LGPL/JPL/jpl_paths.yap.in +++ /dev/null @@ -1,43 +0,0 @@ - -:- ensure_loaded(library(swi)). - -jpl_java_home('@JAVA_HOME@'). - -yap. - -load_jpl_lib :- - jpl_java_home(JavaHome), - fetch_arch(Arch), - gen_jvm_lib(JavaHome,Arch,JPL,JLibs), - load_foreign_files(JPL, JLibs, install), !. - -fetch_arch(Arch) :- - current_prolog_flag(host_type,Name), - atom_codes(Name,Codes), - gen_arch(Codes,Arch). - -gen_arch(L,mac) :- - append(_,[0'd,0'a,0'r,0'w,0'i,0'n|_],L), !. -% handle compilation in 64 bit machine of 32 bit binary. -gen_arch([0'x,0'8,0'6,0'_,0'6,0'4|_],i386) :- yap_flag(max_tagged_integer, 67108863). -gen_arch([0'x,0'8,0'6,0'_,0'6,0'4|_],amd64). -gen_arch([0'i,_,0'8,0'6|_],i386). % take all versions of X86 -gen_arch([0's,0'p,0'a,0'r,0'c|_],sparc). -gen_arch([0'/,0'u,0's,0'r|_],mac). - -gen_jvm_lib(_,mac,[jpl],[]) :- !. -gen_jvm_lib(JavaHome,Arch,[jpl], [JLib]) :- - atom_concat([JavaHome,'/jre/lib/',Arch,'/client/libjvm.so'],JLib), - exists(JLib), !. -gen_jvm_lib(JavaHome,Arch,[jpl], [JLib]) :- - atom_concat([JavaHome,'/jre/lib/',Arch,'/server/libjvm.so'],JLib), - exists(JLib), !. -gen_jvm_lib(JavaHome,Arch,[jpl], [JLib]) :- - atom_concat([JavaHome,'/jre/lib/',Arch,'/classic/libjvm.so'],JLib), - exists(JLib), !. -gen_jvm_lib(JavaHome,Arch,[jpl], [JLib]) :- - atom_concat([JavaHome,'/jre/lib/',Arch,'/libjvm.so'],JLib), - exists(JLib), !. - - - diff --git a/LGPL/JPL/src/Makefile.in b/LGPL/JPL/src/Makefile.in deleted file mode 100644 index 4fb3ae1c8..000000000 --- a/LGPL/JPL/src/Makefile.in +++ /dev/null @@ -1,63 +0,0 @@ -# -# -JAVA_HOME=@JAVA_HOME@ -JVM_INC = @JAVAINCPATH@ -JVM_LIB = @JAVALIBPATH@ -ljava -lverify -ljvm -# -# -# -# default base directory for YAP installation -# (EROOT for architecture-dependent files) -# -prefix = @prefix@ -ROOTDIR = $(prefix) -EROOTDIR = @exec_prefix@ -# -# where the binary should be -# -BINDIR = $(EROOTDIR)/bin -# -# where YAP should look for libraries -# -LIBDIR=$(EROOTDIR)/lib/Yap -# -# -CC=@CC@ -CFLAGS= @CFLAGS@ $(YAP_EXTRAS) $(DEFS) -I$(srcdir) -I../../.. -I$(srcdir)/../../../include $(JVM_INC) -DJAVA_HOME=\"$(JAVA_HOME)\" -# -# -# You shouldn't need to change what follows. -# -INSTALL=@INSTALL@ -INSTALL_DATA=@INSTALL_DATA@ -INSTALL_PROGRAM=@INSTALL_PROGRAM@ -SHELL=/bin/sh -RANLIB=@RANLIB@ -srcdir=@srcdir@ -SHLIB_CFLAGS=@SHLIB_CFLAGS@ -SHLIB_SUFFIX=@SHLIB_SUFFIX@ -#4.1VPATH=@srcdir@:@srcdir@/OPTYap -CWD=$(PWD) -# - -OBJS=jpl.o -SOBJS=jpl@SHLIB_SUFFIX@ - -all: $(SOBJS) - -jpl.o: $(srcdir)/jpl.c $(srcdir)/hacks.c - $(CC) -c $(CFLAGS) $(SHLIB_CFLAGS) $(srcdir)/jpl.c -o jpl.o - - -@DO_SECOND_LD@%@SHLIB_SUFFIX@: %.o -@DO_SECOND_LD@ @SHLIB_LD@ -o $@ $< - -@DO_SECOND_LD@jpl_jni@SHLIB_SUFFIX@: jpl_jni.o jvm.o -@DO_SECOND_LD@ @SHLIB_LD@ -o jpl_jni@SHLIB_SUFFIX@ jpl.o $(JVM_LIB) - -install: all - $(INSTALL_PROGRAM) $(SOBJS) $(DESTDIR)$(LIBDIR) - -clean: - rm -f *.o *~ $(OBJS) $(SOBJS) *.BAK - diff --git a/LGPL/JPL/src/hacks.c b/LGPL/JPL/src/hacks.c deleted file mode 100644 index 99d5b0fc6..000000000 --- a/LGPL/JPL/src/hacks.c +++ /dev/null @@ -1,73 +0,0 @@ -/* - %T jni_SetByteArrayElement(+term, +term, +term) - */ -static foreign_t -jni_SetByteArrayElement( - term_t ta1, // +Arg1 - term_t ta2, // +Arg2 - term_t ta3 // +Arg3 - ) - { - jboolean r; // Prolog exit/fail outcome - jbyteArray p1; - int i2; - jbyte p4; - JNIEnv *env; - atom_t a; /* " */ - functor_t fn; /* temp for conversion macros */ - term_t a1; /* " */ - int i; /* " */ - - if ( !jni_ensure_jvm() ) - { - - - - - return FALSE; - } - r = - JNI_term_to_byte_jarray(ta1,p1) - && JNI_term_to_jint(ta2,i2) - && JNI_term_to_jbyte(ta3,p4) - && ( (*env)->SetByteArrayRegion(env,p1,(jsize)i2,1,&p4) , TRUE ); - - return jni_check_exception(env) && r; - - } - -/* - %T jni_SetByteArrayElement(+term, +term, +term) - */ -static foreign_t -jni_SetDoubleArrayElement( - term_t ta1, // +Arg1 - term_t ta2, // +Arg2 - term_t ta3 // +Arg3 - ) - { - jboolean r; // Prolog exit/fail outcome - void *p1; - jint i2; - jdouble p4; - JNIEnv *env; - atom_t a; /* " */ - functor_t fn; /* temp for conversion macros */ - term_t a1; /* " */ - int i; /* " */ - int64_t i64; - - if ( !jni_ensure_jvm() ) - { - return FALSE; - } - r = - JNI_term_to_double_jarray(ta1,p1) - && JNI_term_to_jint(ta2,i2) - && JNI_term_to_jdouble(ta3,p4) - && ( (*env)->SetDoubleArrayRegion(env,(jdoubleArray)p1,(jsize)i2,1,&p4) , TRUE ); - - return jni_check_exception(env) && r; - - } - diff --git a/LGPL/JPL/src/jpl.c b/LGPL/JPL/src/jpl.c deleted file mode 100644 index c0c19ec57..000000000 --- a/LGPL/JPL/src/jpl.c +++ /dev/null @@ -1,5516 +0,0 @@ -/* $Id: jpl.c,v 1.17 2008-08-12 01:27:22 vsc Exp $ - - Part of JPL -- SWI-Prolog/Java interface - - Author: Paul Singleton, Fred Dushin and Jan Wielemaker - E-mail: paul@jbgb.com - WWW: http://www.swi-prolog.org - Copyright (C): 1985-2004, Paul Singleton - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -*/ - -/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -this source file (jpl.c) combines my Prolog-calls-Java stuff (mostly -prefixed 'JNI' or 'jni' here) with my adaptation of Fred Dushin's -Java-calls-Prolog stuff (mostly prefixed 'JPL' or 'jpl' here) - -recent fixes: - * using PL_get_pointer(), PL_put_pointer() consistently (?) - * replaced all "Class: jpl_fli_PL" by "Class: jpl_fli_Prolog" - -still to do: - * make it completely thread-safe - (both to multiple Prolog (engine-enabled) threads and to multiple Java threads) - * suss JVM 'abort' and 'exit' handling, and 'vfprintf' redirection - * rationalise initialisation; perhaps support startup from C? - -refactoring (trivial): - * initialise a functor_t for jpl_tidy_iref_type_cache/1 - * initialise a functor_t for -/2 - * initialise a module_t for jpl -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ - -/* update this to distinguish releases of this C library: */ -#define JPL_C_LIB_VERSION "3.1.3-alpha" -#define JPL_C_LIB_VERSION_MAJOR 3 -#define JPL_C_LIB_VERSION_MINOR 1 -#define JPL_C_LIB_VERSION_PATCH 3 -#define JPL_C_LIB_VERSION_STATUS "alpha" - -/*#define DEBUG(n, g) ((void)0) */ -#define DEBUG_LEVEL 3 -#define DEBUG(n, g) ( n >= DEBUG_LEVEL ? g : (void)0 ) - -/* disable type-of-ref caching (at least until GC issues are resolved) */ -#define JPL_CACHE_TYPE_OF_REF FALSE - -/*=== includes ===================================================================================== */ - -#ifdef __WINDOWS__ -/* OS-specific header (SWI-Prolog FLI and Java Invocation API both seem to need this): */ -/* but not if we use the .NET 2.0 C compiler */ -#include -#define SIZEOF_WCHAR_T 2 -#endif - -/* SWI-Prolog headers: */ -#include -#include - -/* Java Native Interface and Invocation Interface header: */ -#include - -/* ANSI/ISO C library header (?): */ -#include -#include -#include -#include - -/* POSIX 'pthreads' headers (initially for JPL's Prolog engine pool, useful for locking generally?): */ -#include -#include - - -/*=== JNI constants ================================================================================ */ - -#define JNI_MIN_JCHAR 0 -#define JNI_MAX_JCHAR 65535 - -#define JNI_MIN_JBYTE -128 -#define JNI_MAX_JBYTE 127 - -#define JNI_MIN_JSHORT -32768 -#define JNI_MAX_JSHORT 32767 - - -#define JNI_XPUT_VOID 0 -#define JNI_XPUT_BOOLEAN 1 -#define JNI_XPUT_BYTE 2 -#define JNI_XPUT_CHAR 3 -#define JNI_XPUT_SHORT 4 -#define JNI_XPUT_INT 5 -#define JNI_XPUT_LONG 6 -#define JNI_XPUT_FLOAT 7 -#define JNI_XPUT_DOUBLE 8 -#define JNI_XPUT_FLOAT_TO_DOUBLE 9 -#define JNI_XPUT_LONG_TO_FLOAT 10 -#define JNI_XPUT_LONG_TO_DOUBLE 11 -#define JNI_XPUT_REF 12 -#define JNI_XPUT_ATOM 13 -#define JNI_XPUT_JVALUEP 14 -#define JNI_XPUT_JVALUE 15 - - -/* JNI "hashed refs" constants */ - -#define JNI_HR_LOAD_FACTOR 0.75 - -/* jni_hr_add() return codes: */ -#define JNI_HR_ADD_FAIL -1 -#define JNI_HR_ADD_NEW 0 -#define JNI_HR_ADD_OLD 1 - - -/*=== JPL constants ================================================================================ */ - -/* legit values for jpl_status_jpl_ini and jpl_status_pvm_ini */ -#define JPL_INIT_RAW 101 -#define JPL_INIT_PVM_MAYBE 102 -#define JPL_INIT_OK 103 -#define JPL_INIT_JPL_FAILED 104 -#define JPL_INIT_PVM_FAILED 105 - -#define JPL_MAX_POOL_ENGINES 10 /* max pooled Prolog engines */ -#define JPL_INITIAL_POOL_ENGINES 1 /* initially created ones */ - - -/*=== JNI Prolog<->Java conversion macros ========================================================== */ - -/* JNI (Prolog-calls-Java) conversion macros; mainly used in jni_{func|void}_{0|1|2|3|4}_plc; */ -/* for re-entrancy, ensure that any variables which they use are declared dynamically */ -/* (e.g. or i.e. are local to the host function); */ -/* beware of evaluating *expressions* passed as actual parameters more than once; */ - -#define JNI_term_to_jboolean(T,JB) \ - ( PL_get_functor((T),&fn) \ - && fn==JNI_functor_at_1 \ - ? ( ( a1=PL_new_term_ref(), \ - PL_get_arg(1,(T),a1) \ - ) \ - && PL_get_atom(a1,&a) \ - ? ( a==JNI_atom_false \ - ? ( (JB)=0, TRUE) \ - : ( a==JNI_atom_true \ - ? ( (JB)=1, TRUE) \ - : FALSE \ - ) \ - ) \ - : FALSE \ - ) \ - : FALSE \ - ) - -#define JNI_term_to_jchar(T,J) \ - ( PL_get_integer((T),&i) \ - && i >= JNI_MIN_JCHAR \ - && i <= JNI_MAX_JCHAR \ - && ( (J)=(jchar)i, TRUE) \ - ) - -#define JNI_term_to_jbyte(T,J) \ - ( PL_get_integer((T),&i) \ - && i >= JNI_MIN_JBYTE \ - && i <= JNI_MAX_JBYTE \ - && ( (J)=(jbyte)i, TRUE) \ - ) - -#define JNI_term_to_jshort(T,J) \ - ( PL_get_integer((T),&i) \ - && i >= JNI_MIN_JSHORT \ - && i <= JNI_MAX_JSHORT \ - && ( (J)=(jshort)i, TRUE) \ - ) - -/* JW: jint is always 32-bit! */ - -#define JNI_term_to_jint(T,J) \ - ( PL_get_integer((T),&i) \ - && ((J)=i, TRUE) \ - ) - -#define JNI_term_to_non_neg_jint(T,J) \ - ( PL_get_long((T),&i) \ - && i >= 0 \ - && ( (J)=(jint)i, TRUE) \ - ) - -#define JNI_term_to_jlong(T,J) \ - ( PL_get_int64((T),&i64) \ - && ( (J)=(jlong)i64, TRUE) \ - ) - -#define JNI_term_to_jfloat(T,J) \ - ( PL_get_float((T),&d) \ - ? ( (J)=(jfloat)d, TRUE) \ - : ( PL_get_int64((T),&i64) \ - && ( (J)=(jfloat)i64, TRUE) \ - ) \ - ) - -#define JNI_term_to_jdouble(T,J) \ - ( PL_get_float((T),&(J)) \ - ? TRUE \ - : ( PL_get_int64((T),&i64) \ - && ( (J)=(jdouble)i64, TRUE) \ - ) \ - ) - -#define JNI_term_to_jfieldID(T,J) \ - ( PL_get_functor((T),&fn) \ - && fn==JNI_functor_jfieldID_1 \ - && ( a1=PL_new_term_ref(), \ - PL_get_arg(1,(T),a1) \ - ) \ - && PL_get_pointer(a1,(void**)&(J)) \ - ) - -#define JNI_term_to_jmethodID(T,J) \ - ( PL_get_functor((T),&fn) \ - && fn==JNI_functor_jmethodID_1 \ - && ( a1=PL_new_term_ref(), \ - PL_get_arg(1,(T),a1) \ - ) \ - && PL_get_pointer(a1,(void**)&(J)) \ - ) - -/* converts: */ -/* atom -> String */ -/* @(Tag) -> obj */ -/* @(null) -> NULL */ -/* (else fails) */ -/* */ -#define JNI_term_to_ref(T,J) \ - ( PL_get_atom((T),&a) \ - ? jni_atom_to_String(env,a,(jobject*)&(J)) \ - : PL_get_functor((T),&fn) \ - && fn==JNI_functor_at_1 \ - && ( a1=PL_new_term_ref(), \ - PL_get_arg(1,(T),a1) \ - ) \ - && PL_get_atom(a1,&a) \ - && ( a==JNI_atom_null \ - ? ( (J)=0, TRUE) \ - : jni_tag_to_iref(a,(long*)&(J)) \ - ) \ - ) - -/* converts: */ -/* atom -> String */ -/* @(Tag) -> obj */ -/* (else fails) */ -/* stricter than JNI_term_to_ref(T,J) */ -/* */ -#define JNI_term_to_jobject(T,J) \ - ( JNI_term_to_ref(T,J) \ - && (J) != 0 \ - ) - -/* for now, these specific test-and-convert macros */ -/* are merely mapped to their nearest ancestor... */ - -#define JNI_term_to_jclass(T,J) JNI_term_to_jobject(T,J) - -#define JNI_term_to_throwable_jclass(T,J) JNI_term_to_jobject(T,J) - -#define JNI_term_to_non_array_jclass(T,J) JNI_term_to_jobject(T,J) - -#define JNI_term_to_throwable_jobject(T,J) JNI_term_to_jobject(T,J) - -#define JNI_term_to_jstring(T,J) JNI_term_to_jobject(T,J) - -#define JNI_term_to_jarray(T,J) JNI_term_to_jobject(T,J) - -#define JNI_term_to_object_jarray(T,J) JNI_term_to_jobject(T,J) - -#define JNI_term_to_boolean_jarray(T,J) JNI_term_to_jobject(T,J) - -#define JNI_term_to_byte_jarray(T,J) JNI_term_to_jobject(T,J) - -#define JNI_term_to_char_jarray(T,J) JNI_term_to_jobject(T,J) - -#define JNI_term_to_short_jarray(T,J) JNI_term_to_jobject(T,J) - -#define JNI_term_to_int_jarray(T,J) JNI_term_to_jobject(T,J) - -#define JNI_term_to_long_jarray(T,J) JNI_term_to_jobject(T,J) - -#define JNI_term_to_float_jarray(T,J) JNI_term_to_jobject(T,J) - -#define JNI_term_to_double_jarray(T,J) JNI_term_to_jobject(T,J) - -#define JNI_term_to_jbuf(T,J,TP) \ - ( PL_get_functor((T),&fn) \ - && fn==JNI_functor_jbuf_2 \ - && ( a2=PL_new_term_ref(), \ - PL_get_arg(2,(T),a2) \ - ) \ - && PL_get_atom(a2,&a) \ - && a==(TP) \ - && ( a1=PL_new_term_ref(), \ - PL_get_arg(1,(T),a1) \ - ) \ - && PL_get_pointer(a1,(void**)&(J)) \ - ) - -#define JNI_term_to_charP(T,J) \ - PL_get_atom_chars((T),&(J)) - -#define JNI_term_to_pointer(T,J) \ - PL_get_pointer((T),(void**)&(J)) - - -/* JNI Java-to-Prolog conversion macros: */ - -#define JNI_unify_void(T) \ - PL_unify_term((T), \ - PL_FUNCTOR, JNI_functor_at_1, \ - PL_ATOM, JNI_atom_void \ - ) - -#define JNI_unify_false(T) \ - PL_unify_term((T), \ - PL_FUNCTOR, JNI_functor_at_1, \ - PL_ATOM, JNI_atom_false \ - ) - -#define JNI_unify_true(T) \ - PL_unify_term((T), \ - PL_FUNCTOR, JNI_functor_at_1, \ - PL_ATOM, JNI_atom_true \ - ) - -#define JNI_jboolean_to_term(J,T) \ - ( (J)==0 \ - ? JNI_unify_false((T)) \ - : JNI_unify_true((T)) \ - ) - -#define JNI_jchar_to_term(J,T) \ - PL_unify_integer((T),(int)(J)) - -#define JNI_jbyte_to_term(J,T) \ - PL_unify_integer((T),(int)(J)) - -#define JNI_jshort_to_term(J,T) \ - PL_unify_integer((T),(int)(J)) - -#define JNI_jint_to_term(J,T) \ - PL_unify_integer((T),(int)(J)) - -#define JNI_jlong_to_term(J,T) \ - PL_unify_int64((T),(int64_t)(J)) - -#define JNI_jfloat_to_term(J,T) \ - PL_unify_float((T),(double)(J)) - -#define JNI_jdouble_to_term(J,T) \ - PL_unify_float((T),(double)(J)) - -/* J can be an *expression* parameter to this macro; */ -/* we must evaluate it exactly once; hence we save its value */ -/* in the variable j, which must be dynamic (e.g. local) */ -/* if this macro is to be re-entrant */ -#define JNI_jobject_to_term(J,T) \ - ( ( j=(J), j==NULL ) \ - ? PL_unify_term((T), \ - PL_FUNCTOR, JNI_functor_at_1, \ - PL_ATOM, JNI_atom_null \ - ) \ - : ( (*env)->IsInstanceOf(env,j,str_class) \ - ? jni_String_to_atom(env,j,&a) \ - && PL_unify_term((T), \ - PL_ATOM, a \ - ) \ - : jni_object_to_iref(env,j,&i) \ - && jni_iref_to_tag(i,&a) \ - && PL_unify_term((T), \ - PL_FUNCTOR, JNI_functor_at_1, \ - PL_ATOM, a \ - ) \ - ) \ - ) - -#define JNI_jfieldID_to_term(J,T) \ - PL_unify_term((T), \ - PL_FUNCTOR, JNI_functor_jfieldID_1, \ - PL_POINTER, (void*)(J) \ - ) - -#define JNI_jmethodID_to_term(J,T) \ - PL_unify_term((T), \ - PL_FUNCTOR, JNI_functor_jmethodID_1, \ - PL_POINTER, (void*)(J) \ - ) - -#define JNI_jbuf_to_term(J,T,TP) \ - PL_unify_term((T), \ - PL_FUNCTOR, JNI_functor_jbuf_2, \ - PL_POINTER, (void*)(J), \ - PL_ATOM, (TP) \ - ) - -#define JNI_pointer_to_term(J,T) \ - PL_unify_pointer((T),(void*)(J)) - -#define JNI_charP_to_term(J,T) \ - PL_unify_atom_chars((T),(J)) - - -/*=== JNI initialisation macro (typically succeeds cheaply) ======================================== */ - -#define jni_ensure_jvm() ( ( jvm != NULL \ - || jni_create_default_jvm() \ - ) \ - && (env=jni_env()) != NULL \ - ) - - -/*=== JPL initialisation macros (typically succeed cheaply) ======================================== */ - -/* outcomes: */ -/* fail to find jpl.*, jpl.fli.* classes or to convert init args to String[]: exception, FALSE */ -/* JPL is (newly or already) out of RAW state: TRUE */ -#define jpl_ensure_jpl_init(e) ( jpl_status != JPL_INIT_RAW \ - || jpl_ensure_jpl_init_1(e) \ - ) -/* outcomes: */ -/* JPL or PVM init has already failed: FALSE */ -/* JPL or PVM init fails while being necessarily attempted: exception */ -/* JPL is (newly or already) fully initialised: TRUE */ -#define jpl_ensure_pvm_init(e) ( jpl_status == JPL_INIT_OK \ - || jpl_ensure_pvm_init_1(e) \ - ) - - -/*=== types (structs and typedefs) ================================================================= */ - -typedef struct Hr_Entry HrEntry; /* enables circular definition... */ - -struct Hr_Entry { /* a single interned reference */ - jobject obj; /* a JNI global ref */ - int hash; /* identityHashCode(obj) */ - HrEntry *next; /* next entry in this chain, or NULL */ - }; - -typedef struct Hr_Table HrTable; - -struct Hr_Table { - int count; /* current # entries */ - int threshold; /* rehash on add when count==threshold */ - int length; /* # slots in slot array */ - HrEntry **slots; /* pointer to slot array */ - }; - -typedef long pointer; /* for JPL (I reckon 'int' is enough on 32-bit systems) */ -typedef int bool; /* for JNI/JPL functions returning only TRUE or FALSE */ - - -/*=== JNI constants: sizes of JNI primitive types ================================================== */ - -int size[16] = { /* NB relies on sequence of JNI_XPUT_* defs */ - 0, - sizeof(jboolean), /* size[JNI_XPUT_BOOLEAN] */ - sizeof(jbyte), /* size[JNI_XPUT_BYTE] */ - sizeof(jchar), /* size[JNI_XPUT_CHAR] */ - sizeof(jshort), /* size[JNI_XPUT_SHORT] */ - sizeof(jint), /* size[JNI_XPUT_INT] */ - sizeof(jlong), /* size[JNI_XPUT_LONG] */ - sizeof(jfloat), /* size[JNI_XPUT_FLOAT] */ - sizeof(jdouble), /* size[JNI_XPUT_DOUBLE] */ - 0, /* n/a - JNI_FLOAT_TO_DOUBLE */ - 0, /* n/a - JNI_LONG_TO_FLOAT */ - 0, /* n/a - JNI_LONG_TO_DOUBLE */ - 0, /* n/a - JNI_REF */ - 0, /* n/a - JNI_ATOM */ - 0, /* n/a - JNI_JVALUEP */ - sizeof(jvalue) /* size[JNI_XPUT_JVALUE] */ - }; - - -/*=== JNI "constants", lazily initialised by jni_init() ============================================ */ - -static atom_t JNI_atom_false; /* false */ -static atom_t JNI_atom_true; /* true */ - -static atom_t JNI_atom_boolean; /* boolean */ -static atom_t JNI_atom_char; /* char */ -static atom_t JNI_atom_byte; /* byte */ -static atom_t JNI_atom_short; /* short */ -static atom_t JNI_atom_int; /* int */ -static atom_t JNI_atom_long; /* long */ -static atom_t JNI_atom_float; /* float */ -static atom_t JNI_atom_double; /* double */ - -static atom_t JNI_atom_null; /* null */ -static atom_t JNI_atom_void; /* void */ - -static functor_t JNI_functor_at_1; /* @(_) */ -static functor_t JNI_functor_jbuf_2; /* jbuf(_,_) */ -static functor_t JNI_functor_jlong_2; /* jlong(_,_) */ -static functor_t JNI_functor_jfieldID_1; /* jfieldID(_) */ -static functor_t JNI_functor_jmethodID_1; /* jmethodID(_) */ -static functor_t JNI_functor_error_2; /* error(_, _) */ -static functor_t JNI_functor_java_exception_1; /* java_exception(_) */ -static functor_t JNI_functor_jpl_error_1; /* jpl_error(_) */ - - -/*=== JNI's static JVM references, lazily initialised by jni_init() ================================ */ - -static jclass c_class; /* java.lang.Class (rename to jClass_c ?) */ -static jmethodID c_getName; /* java.lang.Class' getName() (rename to jClassGetName_m ?) */ -static jclass str_class; /* java.lang.String (this duplicates jString_c below) */ -static jclass term_class; /* jpl.Term */ -static jclass termt_class; /* jpl.fli.term_t */ - -static jclass sys_class; /* java.lang.System (rename to jSystem_c ?) */ -static jmethodID sys_ihc; /* java.lang.System's identityHashCode() (rename to jSystemIdentityHashCode_m ?) */ -static jmethodID term_getTerm; /* jpl.Term's getTerm() */ -static jmethodID term_put; /* jpl.Term's put() */ -static jmethodID term_putTerm; /* jpl.Term's static putTerm(Term,term_t) */ - - -/*=== JPL's reusable global class object refs, initialised by jpl_ensure_jpl_init() ================ */ - -static jclass jString_c; -static jclass jJPLException_c; -static jclass jTermT_c; -static jclass jAtomT_c; -static jclass jFunctorT_c; -static jclass jFidT_c; -static jclass jPredicateT_c; -static jclass jQidT_c; -static jclass jModuleT_c; -static jclass jEngineT_c; - -static jclass jLongHolder_c; -static jclass jPointerHolder_c; -static jclass jIntHolder_c; -static jclass jInt64Holder_c; -static jclass jDoubleHolder_c; -static jclass jStringHolder_c; -static jclass jObjectHolder_c; -static jclass jBooleanHolder_c; - - -/*=== JPL's reusable constant field IDs, set before first use by jpl_ensure_jpl_init() ============= */ - -static jfieldID jLongHolderValue_f; -static jfieldID jPointerHolderValue_f; -static jfieldID jIntHolderValue_f; -static jfieldID jInt64HolderValue_f; -static jfieldID jDoubleHolderValue_f; -static jfieldID jStringHolderValue_f; -static jfieldID jObjectHolderValue_f; -static jfieldID jBooleanHolderValue_f; - - -/*=== JPL's default args for PL_initialise() (NB these are not really good enough) ================= */ - -const char *default_args[] = { "pl", - "-g", "true", - "-nosignals", - NULL - }; /* *must* have final NULL */ - - -/*=== JNI global state (initialised by jni_create_jvm_c) =========================================== */ - -static JavaVM *jvm = NULL; /* non-null -> JVM successfully loaded & initialised */ -static char *jvm_ia[2] = {"-Xrs", NULL}; -static char **jvm_dia = jvm_ia; /* default JVM init args (after jpl init, until jvm init) */ -static char **jvm_aia = NULL; /* actual JVM init args (after jvm init) */ - - -/*=== JNI global state (hashed global refs) ======================================================== */ - -static HrTable *hr_table = NULL; /* static handle to allocated-on-demand table */ -static int hr_add_count = 0; /* cumulative total of new refs interned */ -static int hr_old_count = 0; /* cumulative total of old refs reused */ -static int hr_del_count = 0; /* cumulative total of dead refs released */ - - -/*=== JPL global state, initialised by jpl_ensure_jpl_init() or jpl_ensure_jvm_init() ============== */ - -static int jpl_status = JPL_INIT_RAW; /* neither JPL nor PVM initialisation has occurred */ -static jobject pvm_dia = NULL; /* default PVM init args (after jpl init, until pvm init) */ -static jobject pvm_aia = NULL; /* actual PVM init args (after pvm init) */ -static PL_engine_t *engines = NULL; /* handles of the pooled Prolog engines */ -static int engines_allocated = 0; /* size of engines array */ -static pthread_mutex_t engines_mutex = PTHREAD_MUTEX_INITIALIZER; /* for controlling pool access */ -static pthread_cond_t engines_cond = PTHREAD_COND_INITIALIZER; /* for controlling pool access */ - -static pthread_mutex_t jvm_init_mutex = PTHREAD_MUTEX_INITIALIZER; /* for controlling lazy initialisation */ -static pthread_mutex_t pvm_init_mutex = PTHREAD_MUTEX_INITIALIZER; /* for controlling lazy initialisation */ - - -/*=== common functions ============================================================================= */ - -JNIEnv* -jni_env(void) /* economically gets a JNIEnv pointer, valid for this thread */ -{ JNIEnv *env; - - switch( (*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_2) ) - { case JNI_OK: - return env; - case JNI_EDETACHED: - DEBUG(2, Sdprintf( "[JPL: jni_env() calls AttachCurrentThread]\n")); - return (*jvm)->AttachCurrentThread(jvm, (void**)&env, NULL) == 0 ? env : NULL; - default: /* error */ - return NULL; - } -} - - -static char * -jpl_c_lib_version(void) - { - static char v[100]; /* version string */ - static char *vp = NULL; /* set to v at first call */ - - if ( vp != NULL ) /* already set? */ - { - return vp; - } - sprintf( v, "%d.%d.%d-%s", JPL_C_LIB_VERSION_MAJOR, JPL_C_LIB_VERSION_MINOR, JPL_C_LIB_VERSION_PATCH, JPL_C_LIB_VERSION_STATUS); - vp = v; - return vp; - } - - -static foreign_t -jpl_c_lib_version_1_plc( - term_t ta /* -atom: this library's version as an atom, e.g. '3.1.0-alpha' */ - ) - { - - return PL_unify_atom_chars(ta,jpl_c_lib_version()); - } - - -static foreign_t -jpl_c_lib_version_4_plc( - term_t tmajor, /* -integer: major version number */ - term_t tminor, /* -integer: minor version number */ - term_t tpatch, /* -integer: patch version number */ - term_t tstatus /* -atom: status of this version */ - ) - { - - return PL_unify_integer(tmajor,JPL_C_LIB_VERSION_MAJOR) - && PL_unify_integer(tminor,JPL_C_LIB_VERSION_MINOR) - && PL_unify_integer(tpatch,JPL_C_LIB_VERSION_PATCH) - && PL_unify_atom_chars(tstatus,JPL_C_LIB_VERSION_STATUS); - } - - -/*=== JNI function prototypes (to resolve unavoidable forward references) ========================== */ - -static int jni_hr_add(JNIEnv*, jobject, long*); -static int jni_hr_del(JNIEnv*, long); - - -/*=== JNI functions (NB first 6 are cited in macros used subsequently) ============================= */ - -/* this now checks that the atom's name resembles a tag (PS 18/Jun/2004) */ -static bool -jni_tag_to_iref( - atom_t a, - long *iref - ) - { - const char *s = PL_atom_chars(a); - long r; - - if ( strlen(s) == 22 - && s[0] == 'J' - && s[1] == '#' - && isdigit(s[2]) - && isdigit(s[3]) - && isdigit(s[4]) - && isdigit(s[5]) - && isdigit(s[6]) - && isdigit(s[7]) - && isdigit(s[8]) - && isdigit(s[9]) - && isdigit(s[10]) - && isdigit(s[11]) - && isdigit(s[12]) - && isdigit(s[13]) - && isdigit(s[14]) - && isdigit(s[15]) - && isdigit(s[16]) - && isdigit(s[17]) - && isdigit(s[18]) - && isdigit(s[19]) - && isdigit(s[20]) - && isdigit(s[21]) /* s is like 'J#01234567890123456789' */ - && (r=atol(&s[2])) != 0 ) - { - *iref = r; - return 1; - } - else - { - return 0; - } - } - - -static bool -jni_iref_to_tag( - long iref, - atom_t *a - ) - { - char abuf[23]; - - sprintf( abuf, "J#%020lu", iref); /* oughta encapsulate this mapping... */ - *a = PL_new_atom(abuf); - PL_unregister_atom(*a); /* empirically decrement reference count... */ - return TRUE; /* can't fail (?!) */ - } - - -static bool -jni_object_to_iref( - JNIEnv *env, - jobject obj, /* a newly returned JNI local ref */ - long *iref /* gets an integerised, canonical, global equivalent */ - ) - { - int r; /* temp for result code */ - - if ( (r=jni_hr_add(env, obj, iref)) == JNI_HR_ADD_NEW ) - { - hr_add_count++; /* obj was novel, has been added to dict */ - return TRUE; - } - else - if ( r == JNI_HR_ADD_OLD ) - { - hr_old_count++; /* obj was already in dict */ - return TRUE; - } - else - { - return FALSE; /* r == JNI_HR_ADD_FAIL, presumably */ - } - } - - -/* retract all jpl_iref_type_cache(Iref,_) facts */ -static bool -jni_tidy_iref_type_cache( - long iref - ) - { - term_t goal; - - if ( JPL_CACHE_TYPE_OF_REF ) - { - goal = PL_new_term_ref(); - PL_unify_term( goal, - PL_FUNCTOR, PL_new_functor(PL_new_atom("jpl_tidy_iref_type_cache"),1), - PL_INT, iref - ); - return PL_call( - goal, - PL_new_module(PL_new_atom("jpl")) /* "jpl" was erroneously "user" (bugfix 29/3/2005) */ - ); - } - else - { - return TRUE; - } - } - - -/* could merge this into jni_hr_del() ? */ -static bool -jni_free_iref( /* called indirectly from agc hook when a possible iref is unreachable */ - JNIEnv *env, - long iref - ) - { - - if ( jni_hr_del(env,iref) ) /* iref matched a hashedref table entry? (in which case, was deleted) */ - { - if ( !jni_tidy_iref_type_cache(iref) ) - { - DEBUG(0, Sdprintf( "[JPL: jni_tidy_iref_type_cache(%u) failed]\n", iref)); - } - hr_del_count++; - return TRUE; - } - else - { - return FALSE; - } - } - - -/* NB this delivers an atom_t, not a term_t */ -/* returns FALSE if the String arg is NULL */ -static bool - jni_String_to_atom( /* called from JNI_jobject_to_term(J,T) and jpl.fli.Prolog#new_atom() */ - JNIEnv *env, - jobject s, - atom_t *a - ) - { - jsize len = (*env)->GetStringLength(env,s); - const jchar *jcp = (*env)->GetStringChars(env,s,NULL); - - if ( s == NULL ) - { - return FALSE; - } -#if SIZEOF_WCHAR_T == 2 - { - *a = PL_new_atom_wchars(len,jcp); /* easy, huh? (thanks, Jan) */ - } -#else - { - pl_wchar_t *wp; - jsize i; - - if ( (wp=(pl_wchar_t*)malloc(sizeof(pl_wchar_t)*(len))) == NULL) { - (*env)->ReleaseStringChars(env,s,jcp); - return FALSE; - } - for ( i=0 ; iReleaseStringChars(env,s,jcp); - return TRUE; - } - - -/* NB this takes an atom_t, not a term_t */ -static bool - jni_atom_to_String( - JNIEnv *env, - atom_t a, - jobject *s - ) - { - size_t len; - pl_wchar_t *wp; - jchar *jcp; - unsigned char *cp; - unsigned int i; - - if ( (cp=(unsigned char*)PL_atom_nchars(a,&len)) != NULL ) /* got 8-bit chars from trad atom */ - { - jcp = (jchar*)malloc(sizeof(jchar)*len); - for ( i=0 ; iNewString(env,jcp,(jsize)len); - free(jcp); - return TRUE; - } - else if ( (wp=(pl_wchar_t*)PL_atom_wchars(a,&len)) != NULL ) /* got (wide) chars from wide atom */ - { -#if SIZEOF_WCHAR_T == 2 - { - *s = (*env)->NewString(env,wp,(jsize)len); - } -#else - { - jcp = (jchar*)malloc(sizeof(jchar)*len); - for ( i=0 ; iNewString(env,jcp,len); - free(jcp); - } -#endif - return TRUE; - } - else - { - return FALSE; - } - } - - -/* checks that the term_t is a string and delivers a String representation of it */ -static bool - jni_string_to_String( - JNIEnv *env, - term_t t, /* a term which may or may not be a SWIPL string */ - jobject *s - ) - { - size_t len; - pl_wchar_t *wp; - jchar *jcp; - char *cp; - unsigned int i; - - if ( PL_get_nchars(t,&len,&cp,CVT_ATOM) ) /* got 8-bit chars from string? */ - { - jcp = (jchar*)malloc(sizeof(jchar)*len); - for ( i=0 ; iNewString(env,jcp,(jsize)len); - free(jcp); - return TRUE; - } - else if ( PL_get_wchars(t,&len,&wp,CVT_STRING) ) /* got (wide) chars from string? */ - { -#if SIZEOF_WCHAR_T == 2 - { - *s = (*env)->NewString(env,wp,(jsize)len); - } -#else - { - jcp = (jchar*)malloc(sizeof(jchar)*len); - for ( i=0 ; iNewString(env,jcp,len); - free(jcp); - } -#endif - return TRUE; - } - else - { - return FALSE; - } - } - - -/* an FLI wrapper for jni_tag_to_iref() above */ -/* is currently called by jpl_tag_to_type/2, jpl_cache_type_of_object/2 */ -/* jpl_tag_to_type/2 is called by jpl_object_to_type/2, jpl_ref_to_type/2 */ -static foreign_t -jni_tag_to_iref_plc( - term_t tt, /* +atom: a tag */ - term_t ti /* -integer: its corresponding iref */ - ) - { - atom_t a; - long iref; - - return PL_get_atom(tt,&a) - && jni_tag_to_iref(a,&iref) - && PL_unify_integer(ti,iref); - } - - -/* this will be hooked to SWI-Prolog's PL_agc_hook, */ -/* and is called just before each redundant atom is expunged from the dict */ -/* NB need to be able to switch this on and off from Prolog... */ -static bool -jni_atom_freed( - atom_t a - ) - { - const char *cp = PL_atom_chars(a); - long iref; - char cs[23]; /* was 11 until 24/Apr/2007 */ - JNIEnv *env; - - if ((env = jni_env()) == NULL) - return TRUE; /* oughta log an error, at least the first time... */ - if ( jni_tag_to_iref( a, &iref) ) /* check format and convert digits to int if ok */ - { - sprintf( cs, "%020lu", iref); /* reconstruct digits part of tag in cs */ - if ( strcmp(&cp[2],cs) != 0 ) /* original digits != reconstructed digits? */ - { - DEBUG(0, Sdprintf( "[JPL: garbage-collected tag '%s'=%u is bogus (not canonical)]\n", cp, iref)); - } - else - if ( !jni_free_iref(env,iref) ) /* free it (iff it's in the hashedref table) */ - { - DEBUG(0, Sdprintf( "[JPL: garbage-collected tag '%s' is bogus (not in HashedRefs)]\n", cp)); - } - } - else - { - } - return TRUE; /* means "go ahead and expunge the atom" (we do this regardless) */ - } - - -/*=== "hashed ref" (canonical JNI global reference) support ======================================== */ - -static foreign_t -jni_hr_info_plc( /* implements jni_hr_info/4 */ - term_t t1, /* -integer: # object references currently in hash table */ - term_t t2, /* -integer: total # object references so far added */ - term_t t3, /* -integer: total # object references so far found to be already in table */ - term_t t4 /* -integer: total # object references deleted from table (by atom GC) */ - ) - { - return PL_unify_integer(t1,(hr_table==NULL?0:hr_table->count)) /* 0 was -1 (??) */ - && PL_unify_integer(t2,hr_add_count) - && PL_unify_integer(t3,hr_old_count) - && PL_unify_integer(t4,hr_del_count); - } - - -/* unifies t2 with a Prolog term which represents the contents of the hashtable slot */ -static bool -jni_hr_table_slot( - term_t t2, - HrEntry *slot - ) - { - term_t tp = PL_new_term_ref(); - - if ( slot == NULL ) - { - return PL_unify_nil(t2); - } - else - { - return PL_unify_list(t2,tp,t2) - && PL_unify_term(tp, - PL_FUNCTOR, PL_new_functor(PL_new_atom("-"),2), - PL_INT, slot->hash, - PL_LONG, slot->obj - ) - && jni_hr_table_slot(t2,slot->next) - ; - } - } - - -/* unifies t with a list of hash table slot representations */ -static foreign_t -jni_hr_table_plc( - term_t t - ) - { - term_t t1 = PL_copy_term_ref(t); - term_t t2 = PL_new_term_ref(); - int i; - - for ( i=0 ; ilength ; i++ ) - { - if ( !PL_unify_list(t1,t2,t1) || !jni_hr_table_slot(t2,hr_table->slots[i]) ) - { - return FALSE; - } - } - return PL_unify_nil(t1); - } - - -/* an empty table of length is successfully created, where none was before */ -static bool -jni_hr_create( - int length /* required # slots in table */ - ) - { - int i; /* temp for iterative slot initialisation */ - - if ( hr_table != NULL ) - { - return FALSE; /* table already exists (destroy before recreating) */ - } - if ( length <= 0 ) - { - return FALSE; /* unsuitable length */ - } - if ( (hr_table=(HrTable*)malloc(sizeof(HrTable))) == NULL ) - { - return FALSE; /* malloc failed (out of memory, presumably) */ - } - hr_table->length = length; - hr_table->threshold = (int)(hr_table->length*JNI_HR_LOAD_FACTOR); - if ( (hr_table->slots=(HrEntry**)malloc(length*sizeof(HrEntry*))) == NULL ) - { - return FALSE; /* malloc failed: out of memory, presumably */ - } - for ( i=0 ; ilength ; i++ ) - { - hr_table->slots[i] = NULL; - } - hr_table->count = 0; - return TRUE; - } - - -/* an empty table of some default length is successfully created, where none was before */ -static bool -jni_hr_create_default(void) - { - - return jni_hr_create( 101); - } - - -/* ep must point to a chain of zero or more entries; they are freed */ -static void -jni_hr_free_chain_entries( - HrEntry *ep - ) - { - - if ( ep != NULL ) - { - jni_hr_free_chain_entries( ep->next); - free( ep); - } - } - - -/* table t is emptied */ -static void -jni_hr_free_table_chains( - HrTable *t - ) - { - int index; - - for ( index=0 ; index<(t->length) ; index++ ) - { - jni_hr_free_chain_entries( t->slots[index]); - t->slots[index] = NULL; - } - t->count = 0; - } - - -/* all dynamic space used by the pointed-to table is freed */ -static bool -jni_hr_free_table( - HrTable *t - ) - { - - if ( t == NULL ) - { - return FALSE; /* table does not exist */ - } - else - { - jni_hr_free_table_chains( t); - free( t); - return TRUE; - } - } - - -/* the current table is replaced by an equivalent one with more free space */ -static bool -jni_hr_rehash(void) - { - HrTable *t0; /* old table while building new one from it */ - int i; /* for iterating through slots in old table */ - HrEntry *ep1; /* for iterating through all entries in old table */ - HrEntry *ep2; /* an old table entry being relinked into new table */ - int index; /* slot index in new table of entry being transferred */ - - t0 = hr_table; /* temporarily hold onto former table */ - hr_table = NULL; /* precondition for jni_hr_create */ - if ( !jni_hr_create(2*t0->length+1) ) /* new bigger table in its place */ - { - hr_table = t0; /* replace former table for tidiness */ - return FALSE; /* failed to create replacement table during rehash */ - } - for ( i=0 ; ilength ; i++ ) /* for each slot in *former* table */ - { - for ( ep1=t0->slots[i] ; ep1!=NULL ; ) - { /* for each entry in that slot's chain */ - ep2 = ep1; /* grab this entry */ - ep1 = ep1->next; /* advance to next entry or NULL */ - index = (ep2->hash & 0x7fffffff) % hr_table->length; /* new */ - ep2->next = hr_table->slots[index]; /* relink into new array */ - hr_table->slots[index] = ep2; /* " */ - } - t0->slots[i] = NULL; /* tidy old array for generic freeing later */ - } - hr_table->count = t0->count; /* new table's count is old table's count */ - jni_hr_free_table( t0); /* free all space used by old table (NB no entries) */ - return TRUE; - } - - -static bool - jni_hr_hash( /* renamed in v3.0.4 from jni_object_to_hash (it belongs with this hr stuff) */ - JNIEnv *env, - jobject obj, /* MUST BE a valid non-null reference to a Java object */ - int *hash /* gets obj's System.identityHashCode() */ - ) - { - jobject e; /* for possible (but unlikely?) exception */ - - *hash = (*env)->CallStaticIntMethod(env,sys_class,sys_ihc,obj,obj); - return (e=(*env)->ExceptionOccurred(env))==NULL; - } - - -/* returns */ -/* JNI_HR_ADD_NEW -> referenced object is novel */ -/* JNI_HR_ADD_OLD -> referenced object is already known */ -/* JNI_HR_ADD_FAIL -> something went wrong */ -/* and, in *iref, an integerised canonical global ref to the object */ -static int -jni_hr_add( - JNIEnv *env, - jobject lref, /* new JNI local ref from a regular JNI call */ - long *iref /* for integerised canonical global ref */ - ) - { - int hash; /* System.identityHashCode of lref */ - int index; /* lref's slot index, from hash */ - HrEntry *ep; /* temp entry pointer for chain traversal */ - jobject gref; /* iff lref is novel, will hold a global surrogate */ - - if ( hr_table==NULL && !jni_hr_create_default() ) - { - return JNI_HR_ADD_FAIL; /* lazy table creation failed: oughta sort return codes */ - } - if ( !jni_hr_hash(env,lref,&hash) ) /* renamed in v3.0.4 from jni_object_to_hash */ - { - return JNI_HR_ADD_FAIL; /* System.identityHashCode() failed (?) */ - } - index = (hash & 0x7fffffff) % hr_table->length; /* make this a macro? */ - for ( ep=hr_table->slots[index] ; ep!=NULL ; ep=ep->next ) - { - if ( ep->hash==hash ) - { - if ( (*env)->IsSameObject(env,ep->obj,lref) ) - { /* newly referenced object is already interned */ - (*env)->DeleteLocalRef(env,lref); /* free redundant new ref */ - *iref = (long)ep->obj; /* old, equivalent (global) ref */ - return JNI_HR_ADD_OLD; - } - } - } - if ( hr_table->count >= hr_table->threshold ) - { - (void)jni_hr_rehash(); /* oughta check for failure, and return it... */ - return jni_hr_add(env,lref,iref); /* try again with new, larger table */ - } - /* referenced object is novel, and we can add it to table */ - if ( (gref=(*env)->NewGlobalRef(env,lref)) == NULL ) /* derive a global ref */ - { - return JNI_HR_ADD_FAIL; - } - (*env)->DeleteLocalRef(env,lref); /* free redundant (local) ref */ - ep = (HrEntry*)malloc(sizeof(HrEntry)); - ep->hash = hash; - ep->obj = gref; - ep->next = hr_table->slots[index]; /* insert at front of chain */ - hr_table->slots[index] = ep; - hr_table->count++; - *iref = (long)gref; /* pass back the (new) global ref */ - return JNI_HR_ADD_NEW; /* obj was newly interned, under iref as supplied */ - } - - -/* iref corresponded to an entry in the current HashedRef table; */ -/* now that entry is gone, its space is recovered, counts are adjusted etc. */ -/* called only from jni_free_iref() */ -static bool -jni_hr_del( - JNIEnv *env, - long iref /* a possibly spurious canonical global iref */ - ) - { - int index; /* index to a HashedRef table slot */ - HrEntry *ep; /* pointer to a HashedRef table entry */ - HrEntry **epp; /* pointer to ep's handle, in case it needs updating */ - - DEBUG(1, Sdprintf( "[removing possible object reference %u]\n", iref)); - for ( index=0 ; indexlength ; index++ ) /* for each slot */ - { - for ( epp=&(hr_table->slots[index]), ep=*epp ; ep!=NULL ; epp=&(ep->next), ep=*epp ) - { - if ( (long)(ep->obj) == iref ) /* found the sought entry? */ - { - (*env)->DeleteGlobalRef( env, ep->obj); /* free the global object reference */ - *epp = ep->next; /* bypass the entry */ - free( ep); /* free the now-redundant space */ - hr_table->count--; /* adjust table's entry count */ - DEBUG(1, Sdprintf( "[found & removed hashtable entry for object reference %u]\n", iref)); - return TRUE; /* entry found and removed */ - } - } - } - DEBUG(1, Sdprintf("[JPL: failed to find hashtable entry for (presumably bogus) object reference %u]\n", iref)); - return FALSE; - } - - -/*=== JNI initialisation =========================================================================== */ - -/* called once: after successful PVM & JVM creation/discovery, before any JNI calls */ -static int -jni_init() - { - jclass lref; /* temporary local ref, replaced by global */ - JNIEnv *env = jni_env(); /* could pass this in, but this is easier here */ - - if (env == NULL) - return -8; - - /* these initialisations require an active PVM: */ - JNI_atom_false = PL_new_atom( "false"); - JNI_atom_true = PL_new_atom( "true"); - - JNI_atom_boolean = PL_new_atom( "boolean"); - JNI_atom_char = PL_new_atom( "char"); - JNI_atom_byte = PL_new_atom( "byte"); - JNI_atom_short = PL_new_atom( "short"); - JNI_atom_int = PL_new_atom( "int"); - JNI_atom_long = PL_new_atom( "long"); - JNI_atom_float = PL_new_atom( "float"); - JNI_atom_double = PL_new_atom( "double"); - - JNI_atom_null = PL_new_atom( "null"); - JNI_atom_void = PL_new_atom( "void"); /* not yet used properly (?) */ - - JNI_functor_at_1 = PL_new_functor( PL_new_atom("@"), 1); - JNI_functor_jbuf_2 = PL_new_functor( PL_new_atom("jbuf"), 2); - JNI_functor_jlong_2 = PL_new_functor( PL_new_atom("jlong"), 2); - JNI_functor_jfieldID_1 = PL_new_functor( PL_new_atom("jfieldID"), 1); - JNI_functor_jmethodID_1 = PL_new_functor( PL_new_atom("jmethodID"), 1); - - JNI_functor_error_2 = PL_new_functor(PL_new_atom("error"), 2); - JNI_functor_java_exception_1 = PL_new_functor( PL_new_atom("java_exception"), 1); - JNI_functor_jpl_error_1 = PL_new_functor( PL_new_atom("jpl_error"), 1); - - (void)PL_agc_hook( jni_atom_freed); /* link atom GC to object GC (cool:-) */ - - /* these initialisations require an active JVM: */ - return ( (lref=(*env)->FindClass(env,"java/lang/Class")) != NULL - && (c_class=(*env)->NewGlobalRef(env,lref)) != NULL - && ( (*env)->DeleteLocalRef(env,lref), TRUE) - - && (lref=(*env)->FindClass(env,"java/lang/String")) != NULL - && (str_class=(*env)->NewGlobalRef(env,lref)) != NULL - && ( (*env)->DeleteLocalRef(env,lref), TRUE) - && (c_getName=(*env)->GetMethodID(env,c_class,"getName","()Ljava/lang/String;")) != NULL - - && (lref=(*env)->FindClass(env,"java/lang/System")) != NULL - && (sys_class=(*env)->NewGlobalRef(env,lref)) != NULL - && ( (*env)->DeleteLocalRef(env,lref), TRUE) - && (sys_ihc=(*env)->GetStaticMethodID(env,sys_class,"identityHashCode","(Ljava/lang/Object;)I")) != NULL - - && (lref=(*env)->FindClass(env,"jpl/Term")) != NULL - && (term_class=(*env)->NewGlobalRef(env,lref)) != NULL - && ( (*env)->DeleteLocalRef(env,lref), TRUE) - && (term_getTerm=(*env)->GetStaticMethodID(env,term_class,"getTerm","(Ljpl/fli/term_t;)Ljpl/Term;")) != NULL - && (term_put=(*env)->GetMethodID(env,term_class,"put","(Ljpl/fli/term_t;)V")) != NULL - && (term_putTerm=(*env)->GetStaticMethodID(env,term_class,"putTerm","(Ljava/lang/Object;Ljpl/fli/term_t;)V")) != NULL - - && (lref=(*env)->FindClass(env,"jpl/fli/term_t")) != NULL - && (termt_class=(*env)->NewGlobalRef(env,lref)) != NULL - && ( (*env)->DeleteLocalRef(env,lref), TRUE) - - ? 0 - : -7 /* NB #define this? */ - ) - ; - } - - -/*=== JNI exception/error processing/support ======================================================= */ - -/* returns a new error(java_exception(@(tag)),msg) to represent a caught Java exception */ -static term_t - jni_new_java_exception( - atom_t tag, - atom_t msg - ) - { - term_t e = PL_new_term_ref(); - - PL_unify_term(e, - PL_FUNCTOR, JNI_functor_error_2, - PL_FUNCTOR, JNI_functor_java_exception_1, - PL_FUNCTOR, JNI_functor_at_1, - PL_ATOM, tag, - PL_ATOM, msg); - return e; - } - - -/* returns a new error(jpl_error(@(tag)),msg) to represent an exceptional condition raised within JPL */ -static term_t - jni_new_jpl_error( - atom_t tag, - atom_t msg - ) - { - term_t e = PL_new_term_ref(); - - PL_unify_term(e, - PL_FUNCTOR, JNI_functor_error_2, - PL_FUNCTOR, JNI_functor_jpl_error_1, - PL_FUNCTOR, JNI_functor_at_1, - PL_ATOM, tag, - PL_ATOM, msg); - return e; - } - - -/* test for a raised exception; clear and report it if found */ -static bool -jni_check_exception( - JNIEnv *env - ) - { - jobject ej; /* the pending Java exception, if any */ - jobject c; /* its class */ - jobject s; /* its class name as a JVM String, for the report */ - term_t ep; /* a newly created Prolog exception */ - long i; /* temp for an iref denoting a Java exception */ - atom_t tag; /* temp for a tag denoting a Java exception */ - atom_t msg; /* temp for impl-def comment (classname) within error/2 */ - - if ( (ej=(*env)->ExceptionOccurred(env)) == NULL ) - { - return TRUE; - } - else - { - (*env)->ExceptionClear(env); /* clear "exception-pending" state so we can do JNI calls */ - if ( (c=(*env)->GetObjectClass(env,ej)) != NULL ) /* get class of exception */ - { - if ( (s=(*env)->CallObjectMethod(env,c,c_getName)) != NULL ) /* get name of class */ - { - if ( jni_object_to_iref(env,ej,&i) ) - { - if ( jni_iref_to_tag(i,&tag) ) - { - if ( jni_String_to_atom(env,s,&msg) ) - { - ep = jni_new_java_exception(tag,msg); - } - else - { - ep = jni_new_jpl_error(PL_new_atom("FailedToGetUTFCharsOfNameOfClassOfException"),tag); - } - } - else - { - ep = jni_new_jpl_error(PL_new_atom("FailedToConvertExceptionIrefToTagatom"),JNI_atom_null); - } - } - else - { - ep = jni_new_jpl_error(PL_new_atom("FailedToConvertExceptionObjectToIref"),JNI_atom_null); - } - (*env)->DeleteLocalRef(env,s); - } - else - { - ep = jni_new_jpl_error(PL_new_atom("FailedToGetNameOfClassOfException"),JNI_atom_null); - } - (*env)->DeleteLocalRef(env,c); - } - else - { - ep = jni_new_jpl_error(PL_new_atom("FailedToGetClassOfException"),JNI_atom_null); - } - return PL_raise_exception(ep); - } - } - - -/*=== buffer and method param transput ============================================================= */ - -static foreign_t - jni_byte_buf_length_to_codes_plc( - term_t tbb, /* +integer */ - term_t tlen, /* +integer */ - term_t tcs /* -term */ - ) - { - functor_t fn; - term_t a1; - atom_t a; - term_t a2; - jbyte *bb; - int len; - int i; - term_t tl = PL_copy_term_ref( tcs); - term_t ta = PL_new_term_ref(); - void *ptr; - - if ( !( PL_get_functor(tbb,&fn) - && fn==JNI_functor_jbuf_2 - && ( a2=PL_new_term_ref(), - PL_get_arg(2,tbb,a2) - ) - && PL_get_atom(a2,&a) - && a==JNI_atom_byte - && ( a1=PL_new_term_ref(), - PL_get_arg(1,tbb,a1) - ) - && PL_get_pointer(a1,&ptr) - ) - || !PL_get_integer(tlen,&len) - ) - { - return FALSE; - } - bb = ptr; - - for ( i=0 ; i first) */ - term_t txc, /* +integer: transput code, as Prolog integer, appropriate to this param */ - term_t tt, /* +term: param value as datum (value or ref) */ - term_t tjvp /* +pointer: param buffer (allocated just for this call) */ - ) - { - int n; /* got from tn (see above) */ - int xc; /* got from txc (see above) */ - jvalue *jvp; /* got from tjvp (see above) */ - functor_t fn; /* temp for conversion macros */ - term_t a1; /* " */ - atom_t a; /* " */ - int i; /* " */ - int64_t i64; /* " */ - double d; /* " */ - - if ( !PL_get_integer(tn,&n) || - !PL_get_integer(txc,&xc) || - !PL_get_pointer(tjvp,(void*)&jvp) ) - { - return FALSE; - } - - switch ( xc ) - { - case JNI_XPUT_BOOLEAN: - return JNI_term_to_jboolean(tt,jvp[n].z); - - case JNI_XPUT_BYTE: - return JNI_term_to_jbyte(tt,jvp[n].b); - - case JNI_XPUT_CHAR: - return JNI_term_to_jchar(tt,jvp[n].c); - - case JNI_XPUT_SHORT: - return JNI_term_to_jshort(tt,jvp[n].s); - - case JNI_XPUT_INT: - return JNI_term_to_jint(tt,jvp[n].i); - - case JNI_XPUT_LONG: - return JNI_term_to_jlong(tt,jvp[n].j); - - case JNI_XPUT_FLOAT: - return JNI_term_to_jfloat(tt,jvp[n].f); - - case JNI_XPUT_DOUBLE: - return JNI_term_to_jdouble(tt,jvp[n].d); - - case JNI_XPUT_REF: - { JNIEnv *env = jni_env(); - - return env == NULL ? FALSE : JNI_term_to_ref(tt,jvp[n].l); /* this macro needs a valid env */ - } - default: - return FALSE; /* unknown or inappropriate JNI_XPUT_* code */ - } - } - - -/* for completeness, allocates zero-length buffers too, while avoiding malloc() problems */ -static foreign_t -jni_alloc_buffer_plc( - term_t txc, /* +integer: transput code */ - term_t tlen, /* +integer: required length (# items) */ - term_t tbp /* -pointer: PL_POINTER to newly allocated buffer */ - ) - { - int xc; - int len; - void *bp; - - return PL_get_integer(txc,&xc) - && ( ( xc>=JNI_XPUT_BOOLEAN && xc<=JNI_XPUT_DOUBLE ) || xc==JNI_XPUT_JVALUE ) - && PL_get_integer(tlen,&len) - && len >= 0 - && (bp=malloc((len==0?1:len)*size[xc])) != NULL /* avoid (unsafe) malloc(0) */ - && ( PL_unify_pointer(tbp,(void*)bp) - ? TRUE - : ( free(bp), FALSE) - ) - ; - } - - -static foreign_t -jni_free_buffer_plc( - term_t tbp /* +integer: PL_POINTER to redundant buffer */ - ) - { - void *bp; - - return PL_get_pointer(tbp,&bp) - && ( free(bp), TRUE); - } - - -static foreign_t -jni_fetch_buffer_value_plc( - term_t tbp, /* +pointer: PL_POINTER to an active buffer from jni_alloc_buffer/3 */ - term_t ti, /* +integer: index into buffer; 0 <= i < length */ - term_t tv, /* -term: required value (@(false), @(true), integer or float) from buffer */ - term_t txc /* +integer: transput code (one of JNI_XPUT_*) */ - ) - { - void *bp; /* buffer address (trusted to be valid) */ - int i; /* buffer index (trusted to be valid) */ - int xc; /* transput code (range-checked by switch statement) */ - - if ( !PL_get_pointer(tbp,&bp) || !PL_get_integer(ti,&i) || !PL_get_integer(txc,&xc) ) - { - return FALSE; - } - - switch ( xc ) /* primitive type only */ - { - case JNI_XPUT_BOOLEAN: - return JNI_jboolean_to_term(((jboolean*)bp)[i],tv); - - case JNI_XPUT_CHAR: - return PL_unify_integer(tv,((jchar*)bp)[i]); - - case JNI_XPUT_BYTE: - return PL_unify_integer(tv,((jbyte*)bp)[i]); - - case JNI_XPUT_SHORT: - return PL_unify_integer(tv,((jshort*)bp)[i]); - - case JNI_XPUT_INT: - return PL_unify_integer(tv,((jint*)bp)[i]); - - case JNI_XPUT_LONG: - return PL_unify_int64(tv,((jlong*)bp)[i]); - - case JNI_XPUT_FLOAT: - return PL_unify_float(tv,((jfloat*)bp)[i]); - - case JNI_XPUT_DOUBLE: - return PL_unify_float(tv,((jdouble*)bp)[i]); - - default: - return FALSE; - } - } - - -static foreign_t -jni_stash_buffer_value_plc( - term_t tbp, /* +integer: PL_POINTER to buffer */ - term_t ti, /* +integer: index into buffer */ - term_t tv, /* +term: @(false), @(true), integer or float */ - term_t txc /* +integer: transput code (one of JNI_XPUT_*) */ - ) - { - void *bp; - int i; - int idx; - int64_t i64; - int xc; - double d; - functor_t fn; - term_t a1; - atom_t a; - - if ( !PL_get_pointer(tbp,&bp) - || !PL_get_integer(ti,&idx) - || !PL_get_integer(txc,&xc) - ) - { - return FALSE; - } - - switch ( xc ) - { - case JNI_XPUT_BOOLEAN: - return JNI_term_to_jboolean(tv,((jboolean*)bp)[idx]); - - case JNI_XPUT_CHAR: - return JNI_term_to_jchar(tv,((jchar*)bp)[idx]); - - case JNI_XPUT_BYTE: - return JNI_term_to_jbyte(tv,((jbyte*)bp)[idx]); - - case JNI_XPUT_SHORT: - return JNI_term_to_jshort(tv,((jshort*)bp)[idx]); - - case JNI_XPUT_INT: - return JNI_term_to_jint(tv,((jint*)bp)[idx]); - - case JNI_XPUT_LONG: - return JNI_term_to_jlong(tv,((jlong*)bp)[idx]); - - case JNI_XPUT_FLOAT: - return JNI_term_to_jfloat(tv,((jfloat*)bp)[idx]); - - case JNI_XPUT_DOUBLE: - return JNI_term_to_jdouble(tv,((jdouble*)bp)[idx]); - - default: - return FALSE; - } - } - - -/*=== JVM initialisation, startup etc. ============================================================= */ - -static int -jni_get_created_jvm_count() - { - jint n; - - return ( JNI_GetCreatedJavaVMs(NULL,0,&n) == 0 /* what does the '0' arg mean? */ - ? n - : -1 - ) - ; - } - - -#define MAX_JVM_OPTIONS 100 - -static int -jni_create_jvm_c( - char *classpath - ) - { - JavaVMInitArgs vm_args; - /* char cpopt[10000]; */ - char *cpoptp; - JavaVMOption opt[MAX_JVM_OPTIONS]; - int r; - jint n; - int optn = 0; - JNIEnv *env; - - DEBUG(1, Sdprintf( "[creating JVM with 'java.class.path=%s']\n", classpath)); - vm_args.version = JNI_VERSION_1_2; /* "Java 1.2 please" */ - if ( classpath ) - { - cpoptp = (char *)malloc(strlen(classpath)+20); - strcpy( cpoptp, "-Djava.class.path="); /* was cpopt */ - strcat( cpoptp, classpath); /* oughta check length... */ - vm_args.options = opt; - opt[optn].optionString = cpoptp; /* was cpopt */ - optn++; - } - /* opt[optn++].optionString = "-Djava.compiler=NONE"; */ - /* opt[optn].optionString = "exit"; // I don't understand this yet... */ - /* opt[optn++].extraInfo = jvm_exit; // this function has been moved to jpl_extras.c */ - /* opt[optn].optionString = "abort"; // I don't understand this yet... */ - /* opt[optn++].extraInfo = jvm_abort; // this function has been moved to jpl_extras.c */ - /* opt[optn++].optionString = "-Xcheck:jni"; // extra checking of JNI calls */ -#if __YAP_PROLOG__ - opt[optn++].optionString = "-Xmx1512m"; // give java enough space - opt[optn++].optionString = "-Djava.awt.headless=true"; // - // opt[optn++].optionString = "-XstartOnFirstThread"; // -#endif - /* opt[optn++].optionString = "-Xnoclassgc"; // so method/field IDs remain valid (?) */ - /* opt[optn].optionString = "vfprintf"; */ - /* opt[optn++].extraInfo = fprintf; // no O/P, then SEGV */ - /* opt[optn++].extraInfo = xprintf; // one message, then SEGV */ - /* opt[optn++].optionString = "-verbose:jni"; */ - - if ( jvm_dia != NULL ) - { - int i; - - for ( i=0 ; jvm_dia[i]!=NULL ; i++ ) - { - opt[optn++].optionString = jvm_dia[i]; - } - jvm_aia = jvm_dia; - jvm_dia = NULL; - } - - vm_args.nOptions = optn; - /* vm_args.ignoreUnrecognized = TRUE; */ - return - ( JNI_GetCreatedJavaVMs(&jvm,1,&n) == 0 /* what does the '1' arg mean? */ - && n == 1 - /* && (*jvm)->GetEnv(jvm,(void**)&env,JNI_VERSION_1_2) == JNI_OK */ - && (env = jni_env()) != NULL - ? 2 /* success (JVM already available) */ - : ( (r=JNI_CreateJavaVM(&jvm,(void**)&env,&vm_args)) == 0 - ? 0 /* success (JVM created OK) */ - : ( jvm=NULL, r) /* -ve, i.e. some create error */ - ) - ); - } - - -static foreign_t -jni_get_created_jvm_count_plc( - term_t t1 - ) - { - - return PL_unify_integer(t1,jni_get_created_jvm_count()); - } - - -static int -jni_create_jvm( - char *cp - ) - { - int r1; - int r2; - - DEBUG(1, Sdprintf("[JPL: checking for Java VM...]\n")); - return - ( jvm != NULL - ? 1 /* already initialised */ - : ( (r1=jni_create_jvm_c(cp)) < 0 - ? r1 /* err code from JVM-specific routine */ - : ( (r2=jni_init()) < 0 - ? r2 /* err code from jni_init() */ - : ( r1 == 0 /* success code from JVM-specific routine */ - ? ( DEBUG(0, Sdprintf("[JPL: Java VM created]\n")), r1) - : ( DEBUG(0, Sdprintf("[JPL: Java VM found]\n")), r1) - ) - ) - ) - ); - } - - -/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -JW: Note: recent SWI-Prolog set the environment using Win32 API. We can -only get the proper value using the Win32 API; getenv only returns the -value at startup of Prolog. -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ - -int -jni_create_default_jvm(void) - { - int r; -#ifdef __WINDOWS__ - char *cp; - DWORD len; - - if ( (len=GetEnvironmentVariable("CLASSPATH", NULL, 0)) > 0 ) - { cp = malloc(len+1); - - GetEnvironmentVariable("CLASSPATH", cp, len+1); - } else - cp = NULL; -#else - char *cp = getenv("CLASSPATH"); -#endif - - DEBUG(0, Sdprintf("jni_create_default_jvm(): cp=%s\n", cp)); - - if ( (r=jni_create_jvm(cp)) < 0 ) - { - Sdprintf("[JPL: failed to create Java VM (error %d)]\n", r); - } - return r >= 0; /* e.g. 2 -> "JVM already available" */ - } - - -static foreign_t -jni_ensure_jvm_plc(void) - { - JNIEnv *env; /* not used but perhaps initialised by the jni_ensure_jvm() macro */ - - return jni_ensure_jvm(); - } - - - -#if __YAP_PROLOG__ -#include "hacks.c" -#endif - -/* NB after any JNI call which clearly indicates success, */ -/* it is unnecessary to check for an exception */ -/* (potential for slight economy here...) */ -static foreign_t -jni_void_0_plc( /* C identifiers distinguished _0_ etc, Prolog name is overloaded */ - term_t tn /* +integer */ - ) - { - int n; /* JNI function index */ - jboolean r; /* Prolog exit/fail outcome */ - JNIEnv *env; - - if ( !jni_ensure_jvm() /* ought this either succeed or throw a JPL error? */ - || !PL_get_integer(tn,&n) /* ought this either succeed or throw a Prolog type error? */ - ) - { - return FALSE; - } - - switch ( n ) - { - case 17: - r = ( (*env)->ExceptionClear(env) , TRUE ); /* could just return... */ - break; - default: - return FALSE; /* oughta throw exception (design-time error :-) */ - break; - } - - return jni_check_exception(env) && r; - } - - -static foreign_t -jni_void_1_plc( - term_t tn, /* +integer: JNI function index */ - term_t ta1 /* +term: Arg1 */ - ) - { - int n; /* JNI function index */ - /* functor_t fn; // temp for conversion macros */ - /* term_t a1; // " */ - /* term_t a2; // " */ - /* atom_t a; // " */ - /* char *cp; // " */ - /* int i; // " */ - /* int xhi; // " */ - /* int xlo; // " */ - /* jobject j; // " */ - /* jlong jl; // " */ - /* void *p1; // temp for converted (JVM) arg */ - char *c1; /* " */ - /* int i1; // " */ - /* jlong l1; // " */ - /* double d1; // " */ - jboolean r; /* Prolog exit/fail outcome */ - JNIEnv *env; - - if ( !jni_ensure_jvm() - || !PL_get_integer(tn,&n) - ) - { - return FALSE; - } - - switch ( n ) - { - case 18: - r = JNI_term_to_charP(ta1,c1) - && ( (*env)->FatalError(env,(char*)c1) , TRUE ); - break; - default: - return FALSE; /* oughta throw exception (design-time error :-) */ - break; - } - - return jni_check_exception(env) && r; - } - - -static foreign_t -jni_void_2_plc( - term_t tn, /* +integer: JNI function index */ - term_t ta1, /* +term: Arg1 */ - term_t ta2 /* +term: Arg2 */ - ) - { - int n; /* JNI function index */ - /* functor_t fn; // temp for conversion macros */ - /* term_t a1; // " */ - /* term_t a2; // " */ - /* atom_t a; // " */ - /* char *cp; // " */ - /* int i; // " */ - /* int xhi; // " */ - /* int xlo; // " */ - /* jobject j; // " */ - /* jlong jl; // " */ - /* void *p1; // temp for converted (JVM) arg */ - /* void *p2; // " */ - /* char *c1; // " */ - /* char *c2; // " */ - /* int i1; // " */ - /* int i2; // " */ - /* jlong l1; // " */ - /* jlong l2; // " */ - /* double d1; // " */ - /* double d2; // " */ - jboolean r; /* Prolog exit/fail outcome */ - JNIEnv *env; - - if ( !jni_ensure_jvm() - || !PL_get_integer(tn,&n) - ) - { - return FALSE; - } - - switch ( n ) - { - /* case 166: */ - /* r = JNI_term_to_jstring(ta1,p1) */ - /* && JNI_term_to_jbuf(ta2,p2,JNI_atom_char) */ - /* && ( (*env)->ReleaseStringChars(env,(jstring)p1,(jchar*)p2) , TRUE ); */ - /* break; */ - /* case 170: */ - /* r = JNI_term_to_jstring(ta1,p1) */ - /* && JNI_term_to_jbuf(ta2,c2,JNI_atom_byte) */ - /* && ( (*env)->ReleaseStringUTFChars(env,(jstring)p1,(char*)c2) , TRUE ); */ - /* break; */ - default: - return FALSE; /* oughta throw exception (design-time error :-) */ - break; - } - - return jni_check_exception(env) && r; - } - - -static foreign_t -jni_void_3_plc( - term_t tn, /* +integer: JNI function index */ - term_t ta1, /* +term: Arg1 */ - term_t ta2, /* +term: Arg2 */ - term_t ta3 /* +term: Arg3 */ - ) - { - int n; /* JNI function index */ - functor_t fn; /* temp for conversion macros */ - term_t a1; /* " */ - /* term_t a2; // " */ - atom_t a; /* " */ - /* char *cp; // " */ - int i; /* " */ - int64_t i64; /* " */ - double d; /* " */ - /* int xhi; // " */ - /* int xlo; // " */ - /* jobject j; // " */ - /* jlong jl; // " */ - void *p1; /* temp for converted (JVM) arg */ - void *p2; /* " */ - void *p3; /* " */ - /* char *c1; // " */ - /* char *c2; // " */ - /* char *c3; // " */ - /* int i1; // " */ - int i2; /* " */ - int i3; /* " */ - /* jlong l1; // " */ - /* jlong l2; // " */ - jlong l3; /* " */ - /* float f1; // " */ - /* float f2; // " */ - float f3; /* " */ - /* double d1; // " */ - /* double d2; // " */ - double d3; /* " */ - jvalue *jvp = NULL; /* if this is given a buffer, it will be freed after the call */ - jboolean r; /* Prolog exit/fail outcome */ - JNIEnv *env; - - if ( !jni_ensure_jvm() - || !PL_get_integer(tn,&n) - ) - { - return FALSE; - } - - switch ( n ) - { - case 63: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jmethodID(ta2,p2) - && JNI_term_to_pointer(ta3,jvp) - && ( (*env)->CallVoidMethodA(env,(jobject)p1,(jmethodID)p2,jvp) , TRUE ); - break; - case 104: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_term_to_ref(ta3,p3) - && ( (*env)->SetObjectField(env,(jobject)p1,(jfieldID)p2,(jobject)p3) , TRUE ); - break; - case 105: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_term_to_jboolean(ta3,i3) - && ( (*env)->SetBooleanField(env,(jobject)p1,(jfieldID)p2,(jboolean)i3) , TRUE ); - break; - case 106: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_term_to_jbyte(ta3,i3) - && ( (*env)->SetByteField(env,(jobject)p1,(jfieldID)p2,(jbyte)i3) , TRUE ); - break; - case 107: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_term_to_jchar(ta3,i3) - && ( (*env)->SetCharField(env,(jobject)p1,(jfieldID)p2,(jchar)i3) , TRUE ); - break; - case 108: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_term_to_jshort(ta3,i3) - && ( (*env)->SetShortField(env,(jobject)p1,(jfieldID)p2,(jshort)i3) , TRUE ); - break; - case 109: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_term_to_jint(ta3,i3) - && ( (*env)->SetIntField(env,(jobject)p1,(jfieldID)p2,(jint)i3) , TRUE ); - break; - case 110: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_term_to_jlong(ta3,l3) - && ( (*env)->SetLongField(env,(jobject)p1,(jfieldID)p2,(jlong)l3) , TRUE ); - break; - case 111: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_term_to_jfloat(ta3,f3) /* f3 was d3 */ - && ( (*env)->SetFloatField(env,(jobject)p1,(jfieldID)p2,(jfloat)f3) , TRUE ); /* f3 was d3 */ - break; - case 112: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_term_to_jdouble(ta3,d3) - && ( (*env)->SetDoubleField(env,(jobject)p1,(jfieldID)p2,(jdouble)d3) , TRUE ); - break; - case 143: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jmethodID(ta2,p2) - && JNI_term_to_pointer(ta3,jvp) - && ( (*env)->CallStaticVoidMethodA(env,(jclass)p1,(jmethodID)p2,jvp) , TRUE ); - break; - case 154: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_term_to_ref(ta3,p3) - && ( (*env)->SetStaticObjectField(env,(jclass)p1,(jfieldID)p2,(jobject)p3) , TRUE ); - break; - case 155: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_term_to_jboolean(ta3,i3) - && ( (*env)->SetStaticBooleanField(env,(jclass)p1,(jfieldID)p2,(jboolean)i3) , TRUE ); - break; - case 156: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_term_to_jbyte(ta3,i3) - && ( (*env)->SetStaticByteField(env,(jclass)p1,(jfieldID)p2,(jbyte)i3) , TRUE ); - break; - case 157: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_term_to_jchar(ta3,i3) - && ( (*env)->SetStaticCharField(env,(jclass)p1,(jfieldID)p2,(jchar)i3) , TRUE ); - break; - case 158: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_term_to_jshort(ta3,i3) - && ( (*env)->SetStaticShortField(env,(jclass)p1,(jfieldID)p2,(jshort)i3) , TRUE ); - break; - case 159: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_term_to_jint(ta3,i3) - && ( (*env)->SetStaticIntField(env,(jclass)p1,(jfieldID)p2,(jint)i3) , TRUE ); - break; - case 160: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_term_to_jlong(ta3,l3) - && ( (*env)->SetStaticLongField(env,(jclass)p1,(jfieldID)p2,(jlong)l3) , TRUE ); - break; - case 161: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_term_to_jfloat(ta3,f3) /* f3 was d3 */ - && ( (*env)->SetStaticFloatField(env,(jclass)p1,(jfieldID)p2,(jfloat)f3) , TRUE ); /* f3 was d3 */ - break; - case 162: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_term_to_jdouble(ta3,d3) - && ( (*env)->SetStaticDoubleField(env,(jclass)p1,(jfieldID)p2,(jdouble)d3) , TRUE ); - break; - case 174: - r = JNI_term_to_object_jarray(ta1,p1) - && JNI_term_to_jint(ta2,i2) - && JNI_term_to_ref(ta3,p3) - && ( (*env)->SetObjectArrayElement(env,(jobjectArray)p1,(jsize)i2,(jobject)p3) , TRUE ); - break; - /* case 191: */ - /* r = JNI_term_to_boolean_jarray(ta1,p1) */ - /* && JNI_term_to_jbuf(ta2,p2,JNI_atom_boolean) */ - /* && JNI_term_to_jint(ta3,i3) */ - /* && ( (*env)->ReleaseBooleanArrayElements(env,(jbooleanArray)p1,(jboolean*)p2,(jint)i3) , TRUE ); */ - /* break; */ - /* case 192: */ - /* r = JNI_term_to_byte_jarray(ta1,p1) */ - /* && JNI_term_to_jbuf(ta2,p2,JNI_atom_byte) */ - /* && JNI_term_to_jint(ta3,i3) */ - /* && ( (*env)->ReleaseByteArrayElements(env,(jbyteArray)p1,(jbyte*)p2,(jint)i3) , TRUE ); */ - /* break; */ - /* case 193: */ - /* r = JNI_term_to_char_jarray(ta1,p1) */ - /* && JNI_term_to_jbuf(ta2,p2,JNI_atom_char) */ - /* && JNI_term_to_jint(ta3,i3) */ - /* && ( (*env)->ReleaseCharArrayElements(env,(jcharArray)p1,(jchar*)p2,(jint)i3) , TRUE ); */ - /* break; */ - /* case 194: */ - /* r = JNI_term_to_short_jarray(ta1,p1) */ - /* && JNI_term_to_jbuf(ta2,p2,JNI_atom_short) */ - /* && JNI_term_to_jint(ta3,i3) */ - /* && ( (*env)->ReleaseShortArrayElements(env,(jshortArray)p1,(jshort*)p2,(jint)i3) , TRUE ); */ - /* break; */ - /* case 195: */ - /* r = JNI_term_to_int_jarray(ta1,p1) */ - /* && JNI_term_to_jbuf(ta2,p2,JNI_atom_int) */ - /* && JNI_term_to_jint(ta3,i3) */ - /* && ( (*env)->ReleaseIntArrayElements(env,(jintArray)p1,(jint*)p2,(jint)i3) , TRUE ); */ - /* break; */ - /* case 196: */ - /* r = JNI_term_to_long_jarray(ta1,p1) */ - /* && JNI_term_to_jbuf(ta2,p2,JNI_atom_long) */ - /* && JNI_term_to_jint(ta3,i3) */ - /* && ( (*env)->ReleaseLongArrayElements(env,(jlongArray)p1,(jlong*)p2,(jint)i3) , TRUE ); */ - /* break; */ - /* case 197: */ - /* r = JNI_term_to_float_jarray(ta1,p1) */ - /* && JNI_term_to_jbuf(ta2,p2,JNI_atom_float) */ - /* && JNI_term_to_jint(ta3,i3) */ - /* && ( (*env)->ReleaseFloatArrayElements(env,(jfloatArray)p1,(jfloat*)p2,(jint)i3) , TRUE ); */ - /* break; */ - /* case 198: */ - /* r = JNI_term_to_double_jarray(ta1,p1) */ - /* && JNI_term_to_jbuf(ta2,p2,JNI_atom_double) */ - /* && JNI_term_to_jint(ta3,i3) */ - /* && ( (*env)->ReleaseDoubleArrayElements(env,(jdoubleArray)p1,(jdouble*)p2,(jint)i3) , TRUE ); */ - /* break; */ - default: - return FALSE; /* oughta throw exception (design-time error :-) */ - break; - } - - if ( jvp != NULL ) - { - free( jvp); - } - - return jni_check_exception(env) && r; - } - - -static foreign_t -jni_void_4_plc( - term_t tn, /* +integer: JNI function index */ - term_t ta1, /* +term: Arg1 */ - term_t ta2, /* +term: Arg2 */ - term_t ta3, /* +term: Arg3 */ - term_t ta4 /* +term: Arg4 */ - ) - { - int n; /* JNI function index */ - functor_t fn; /* temp for conversion macros */ - term_t a1; /* " */ - term_t a2; /* " */ - atom_t a; /* " */ - /* char *cp; // " */ - int i; /* " */ - /* int xhi; // " */ - /* int xlo; // " */ - /* jobject j; // " */ - /* jlong jl; // " */ - void *p1; /* temp for converted (JVM) arg */ - /* void *p2; // " */ - /* void *p3; // " */ - void *p4; /* " */ - /* char *c1; // " */ - /* char *c2; // " */ - /* char *c3; // " */ - /* char *c4; // " */ - /* int i1; // " */ - int i2; /* " */ - int i3; /* " */ - /* int i4; // " */ - /* jlong l1; // " */ - /* jlong l2; // " */ - /* jlong l3; // " */ - /* jlong l4; // " */ - /* double d1; // " */ - /* double d2; // " */ - /* double d3; // " */ - /* double d4; // " */ - jvalue *jvp = NULL; /* if this is given a buffer, it will be freed after the call */ - jboolean r; /* Prolog exit/fail outcome */ - JNIEnv *env; - - if ( !jni_ensure_jvm() - || !PL_get_integer(tn,&n) - ) - { - return FALSE; - } - - switch ( n ) - { - /* case 93: */ - /* r = JNI_term_to_jobject(ta1,p1) */ - /* && JNI_term_to_jclass(ta2,p2) */ - /* && JNI_term_to_jmethodID(ta3,p3) */ - /* && JNI_term_to_pointer(ta4,jvp) */ - /* && ( (*env)->CallNonvirtualVoidMethodA(env,(jobject)p1,(jclass)p2,(jmethodID)p3,jvp) , TRUE ); */ - /* break; */ - case 199: - r = JNI_term_to_boolean_jarray(ta1,p1) - && JNI_term_to_jint(ta2,i2) - && JNI_term_to_jint(ta3,i3) - && JNI_term_to_jbuf(ta4,p4,JNI_atom_boolean) - && ( (*env)->GetBooleanArrayRegion(env,(jbooleanArray)p1,(jsize)i2,(jsize)i3,(jboolean*)p4) , TRUE ); - break; - case 200: - r = JNI_term_to_byte_jarray(ta1,p1) - && JNI_term_to_jint(ta2,i2) - && JNI_term_to_jint(ta3,i3) - && JNI_term_to_jbuf(ta4,p4,JNI_atom_byte) - && ( (*env)->GetByteArrayRegion(env,(jbyteArray)p1,(jsize)i2,(jsize)i3,(jbyte*)p4) , TRUE ); - break; - case 201: - r = JNI_term_to_char_jarray(ta1,p1) - && JNI_term_to_jint(ta2,i2) - && JNI_term_to_jint(ta3,i3) - && JNI_term_to_jbuf(ta4,p4,JNI_atom_char) - && ( (*env)->GetCharArrayRegion(env,(jcharArray)p1,(jsize)i2,(jsize)i3,(jchar*)p4) , TRUE ); - break; - case 202: - r = JNI_term_to_short_jarray(ta1,p1) - && JNI_term_to_jint(ta2,i2) - && JNI_term_to_jint(ta3,i3) - && JNI_term_to_jbuf(ta4,p4,JNI_atom_short) - && ( (*env)->GetShortArrayRegion(env,(jshortArray)p1,(jsize)i2,(jsize)i3,(jshort*)p4) , TRUE ); - break; - case 203: - r = JNI_term_to_int_jarray(ta1,p1) - && JNI_term_to_jint(ta2,i2) - && JNI_term_to_jint(ta3,i3) - && JNI_term_to_jbuf(ta4,p4,JNI_atom_int) - && ( (*env)->GetIntArrayRegion(env,(jintArray)p1,(jsize)i2,(jsize)i3,(jint*)p4) , TRUE ); - break; - case 204: - r = JNI_term_to_long_jarray(ta1,p1) - && JNI_term_to_jint(ta2,i2) - && JNI_term_to_jint(ta3,i3) - && JNI_term_to_jbuf(ta4,p4,JNI_atom_long) - && ( (*env)->GetLongArrayRegion(env,(jlongArray)p1,(jsize)i2,(jsize)i3,(jlong*)p4) , TRUE ); - break; - case 205: - r = JNI_term_to_float_jarray(ta1,p1) - && JNI_term_to_jint(ta2,i2) - && JNI_term_to_jint(ta3,i3) - && JNI_term_to_jbuf(ta4,p4,JNI_atom_float) - && ( (*env)->GetFloatArrayRegion(env,(jfloatArray)p1,(jsize)i2,(jsize)i3,(jfloat*)p4) , TRUE ); - break; - case 206: - r = JNI_term_to_double_jarray(ta1,p1) - && JNI_term_to_jint(ta2,i2) - && JNI_term_to_jint(ta3,i3) - && JNI_term_to_jbuf(ta4,p4,JNI_atom_double) - && ( (*env)->GetDoubleArrayRegion(env,(jdoubleArray)p1,(jsize)i2,(jsize)i3,(jdouble*)p4) , TRUE ); - break; - case 207: - r = JNI_term_to_boolean_jarray(ta1,p1) - && JNI_term_to_jint(ta2,i2) - && JNI_term_to_jint(ta3,i3) - && JNI_term_to_jbuf(ta4,p4,JNI_atom_boolean) - && ( (*env)->SetBooleanArrayRegion(env,(jbooleanArray)p1,(jsize)i2,(jsize)i3,(jboolean*)p4) , TRUE ); - break; - case 208: - r = JNI_term_to_byte_jarray(ta1,p1) - && JNI_term_to_jint(ta2,i2) - && JNI_term_to_jint(ta3,i3) - && JNI_term_to_jbuf(ta4,p4,JNI_atom_byte) - && ( (*env)->SetByteArrayRegion(env,(jbyteArray)p1,(jsize)i2,(jsize)i3,(jbyte*)p4) , TRUE ); - break; - case 209: - r = JNI_term_to_char_jarray(ta1,p1) - && JNI_term_to_jint(ta2,i2) - && JNI_term_to_jint(ta3,i3) - && JNI_term_to_jbuf(ta4,p4,JNI_atom_char) - && ( (*env)->SetCharArrayRegion(env,(jcharArray)p1,(jsize)i2,(jsize)i3,(jchar*)p4) , TRUE ); - break; - case 210: - r = JNI_term_to_short_jarray(ta1,p1) - && JNI_term_to_jint(ta2,i2) - && JNI_term_to_jint(ta3,i3) - && JNI_term_to_jbuf(ta4,p4,JNI_atom_short) - && ( (*env)->SetShortArrayRegion(env,(jshortArray)p1,(jsize)i2,(jsize)i3,(jshort*)p4) , TRUE ); - break; - case 211: - r = JNI_term_to_int_jarray(ta1,p1) - && JNI_term_to_jint(ta2,i2) - && JNI_term_to_jint(ta3,i3) - && JNI_term_to_jbuf(ta4,p4,JNI_atom_int) - && ( (*env)->SetIntArrayRegion(env,(jintArray)p1,(jsize)i2,(jsize)i3,(jint*)p4) , TRUE ); - break; - case 212: - r = JNI_term_to_long_jarray(ta1,p1) - && JNI_term_to_jint(ta2,i2) - && JNI_term_to_jint(ta3,i3) - && JNI_term_to_jbuf(ta4,p4,JNI_atom_long) - && ( (*env)->SetLongArrayRegion(env,(jlongArray)p1,(jsize)i2,(jsize)i3,(jlong*)p4) , TRUE ); - break; - case 213: - r = JNI_term_to_float_jarray(ta1,p1) - && JNI_term_to_jint(ta2,i2) - && JNI_term_to_jint(ta3,i3) - && JNI_term_to_jbuf(ta4,p4,JNI_atom_float) - && ( (*env)->SetFloatArrayRegion(env,(jfloatArray)p1,(jsize)i2,(jsize)i3,(jfloat*)p4) , TRUE ); - break; - case 214: - r = JNI_term_to_double_jarray(ta1,p1) - && JNI_term_to_jint(ta2,i2) - && JNI_term_to_jint(ta3,i3) - && JNI_term_to_jbuf(ta4,p4,JNI_atom_double) - && ( (*env)->SetDoubleArrayRegion(env,(jdoubleArray)p1,(jsize)i2,(jsize)i3,(jdouble*)p4) , TRUE ); - break; - default: - return FALSE; /* oughta throw exception (design-time error :-) */ - break; - } - - if ( jvp != NULL ) - { - free( jvp); - } - - return jni_check_exception(env) && r; - } - - -static foreign_t -jni_func_0_plc( - term_t tn, /* +integer: JNI function index */ - term_t tr /* -term: Result */ - ) - { - int n; /* JNI function index */ - /* functor_t fn; // temp for conversion macros */ - /* term_t a1; // " */ - /* term_t a2; // " */ - /* atom_t a; // " */ - /* char *cp; // " */ - /* long i; // " */ - /* int xhi; // " */ - /* int xlo; // " */ - /* jobject j; // " */ - /* jlong jl; // " */ - jboolean r; /* Prolog exit/fail outcome */ - JNIEnv *env; - - if ( !jni_ensure_jvm() - || !PL_get_integer(tn,&n) - ) - { - return FALSE; - } - - switch ( n ) - { - /* case 4: */ - /* r = JNI_jint_to_term((*env)->GetVersion(env),tr); */ - /* break; */ - /* case 15: */ - /* r = JNI_jobject_to_term((*env)->ExceptionOccurred(env),tr); */ - /* break; */ - default: - return FALSE; /* oughta throw exception (design-time error :-) */ - break; - } - - return jni_check_exception(env) && r; /* surely NEITHER of these throws an exception! */ - } - - -static foreign_t -jni_func_1_plc( - term_t tn, /* +integer: JNI function index */ - term_t ta1, /* +term: Arg1 */ - term_t tr /* -term: Result */ - ) - { - int n; /* JNI function index */ - functor_t fn; /* temp for conversion macros */ - term_t a1; /* " */ - /* term_t a2; // " */ - atom_t a; /* " */ - /* char *cp; // " */ - long i; /* " */ - /* int xhi; // " */ - /* int xlo; // " */ - jobject j; /* " */ - /* jlong jl; // " */ - void *p1; /* temp for converted (JVM) arg */ - char *c1; /* " */ - int i1; /* " */ - /* jlong l1; // " */ - /* double d1; // " */ - jboolean r; /* Prolog exit/fail outcome */ - JNIEnv *env; - - if ( !jni_ensure_jvm() - || !PL_get_integer(tn,&n) - ) - { - return FALSE; - } - - switch ( n ) - { - case 6: - r = JNI_term_to_charP(ta1,c1) - && JNI_jobject_to_term((*env)->FindClass(env,(char*)c1),tr); /* *NOT* Unicode */ - break; - case 10: - r = JNI_term_to_jclass(ta1,p1) - && JNI_jobject_to_term((*env)->GetSuperclass(env,(jclass)p1),tr); - break; - /* case 13: */ - /* r = JNI_term_to_throwable_jobject(ta1,p1) */ - /* && JNI_jint_to_term((*env)->Throw(env,(jthrowable)p1),tr); */ - /* break; */ - /* case 27: */ - /* r = JNI_term_to_non_array_jclass(ta1,p1) */ - /* && JNI_jobject_to_term((*env)->AllocObject(env,(jclass)p1),tr); */ - /* break; */ - case 31: - r = JNI_term_to_jobject(ta1,p1) - && JNI_jobject_to_term((*env)->GetObjectClass(env,(jobject)p1),tr); - break; - /* case 164: // not used */ - /* r = JNI_term_to_jstring(ta1,p1) */ - /* && JNI_jint_to_term((*env)->GetStringLength(env,(jstring)p1),tr); */ - /* break; */ - /* case 167: // not used */ - /* r = JNI_term_to_charP(ta1,c1) */ - /* && JNI_jobject_to_term((*env)->NewStringUTF(env,(char*)c1),tr); */ - /* break; */ - /* case 168: */ - /* r = JNI_term_to_jstring(ta1,p1) */ - /* && JNI_jint_to_term((*env)->GetStringUTFLength(env,(jstring)p1),tr); */ - /* break; */ - case 171: - r = JNI_term_to_jarray(ta1,p1) - && JNI_jint_to_term((*env)->GetArrayLength(env,(jarray)p1),tr); - break; - case 175: - r = JNI_term_to_non_neg_jint(ta1,i1) - && JNI_jobject_to_term((*env)->NewBooleanArray(env,(jsize)i1),tr); - break; - case 176: - r = JNI_term_to_non_neg_jint(ta1,i1) - && JNI_jobject_to_term((*env)->NewByteArray(env,(jsize)i1),tr); - break; - case 177: - r = JNI_term_to_non_neg_jint(ta1,i1) - && JNI_jobject_to_term((*env)->NewCharArray(env,(jsize)i1),tr); - break; - case 178: - r = JNI_term_to_non_neg_jint(ta1,i1) - && JNI_jobject_to_term((*env)->NewShortArray(env,(jsize)i1),tr); - break; - case 179: - r = JNI_term_to_non_neg_jint(ta1,i1) - && JNI_jobject_to_term((*env)->NewIntArray(env,(jsize)i1),tr); - break; - case 180: - r = JNI_term_to_non_neg_jint(ta1,i1) - && JNI_jobject_to_term((*env)->NewLongArray(env,(jsize)i1),tr); - break; - case 181: - r = JNI_term_to_non_neg_jint(ta1,i1) - && JNI_jobject_to_term((*env)->NewFloatArray(env,(jsize)i1),tr); - break; - case 182: - r = JNI_term_to_non_neg_jint(ta1,i1) - && JNI_jobject_to_term((*env)->NewDoubleArray(env,(jsize)i1),tr); - break; - /* case 217: */ - /* r = JNI_term_to_jobject(ta1,p1) */ - /* && JNI_jint_to_term((*env)->MonitorEnter(env,(jobject)p1),tr); */ - /* break; */ - /* case 218: */ - /* r = JNI_term_to_jobject(ta1,p1) */ - /* && JNI_jint_to_term((*env)->MonitorExit(env,(jobject)p1),tr); */ - /* break; */ - default: - return FALSE; /* oughta throw exception (design-time error :-) */ - break; - } - - return jni_check_exception(env) && r; - } - - -static foreign_t -jni_func_2_plc( - term_t tn, /* +integer: JNI function index */ - term_t ta1, /* +term: Arg1 */ - term_t ta2, /* +term: Arg2 */ - term_t tr /* -term: Result */ - ) - { - int n; /* JNI function index */ - functor_t fn; /* temp for conversion macros */ - term_t a1; /* " */ - /* term_t a2; // " */ - atom_t a; /* " */ - /* char *cp; // " */ - long i; /* " */ - /* int xhi; // " */ - /* int xlo; // " */ - jobject j; /* " */ - /* jlong jl; // " */ - void *p1; /* temp for converted (JVM) arg */ - void *p2; /* " */ - /* char *c1; // " */ - /* char *c2; // " */ - /* int i1; // " */ - int i2; /* " */ - /* jlong l1; // " */ - /* jlong l2; // " */ - /* double d1; // " */ - /* double d2; // " */ - jboolean r; /* Prolog exit/fail outcome */ - JNIEnv *env; - - if ( !jni_ensure_jvm() - || !PL_get_integer(tn,&n) - ) - { - return FALSE; - } - - switch ( n ) - { - case 11: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jclass(ta2,p2) - && JNI_jboolean_to_term((*env)->IsAssignableFrom(env,(jclass)p1,(jclass)p2),tr); - break; - /* case 14: */ - /* r = JNI_term_to_throwable_jclass(ta1,p1) */ - /* && JNI_term_to_charP(ta2,c2) */ - /* && JNI_jint_to_term((*env)->ThrowNew(env,(jclass)p1,(char*)c2),tr); */ - /* break; */ - /* case 24: */ - /* r = JNI_term_to_ref(ta1,p1) */ - /* && JNI_term_to_ref(ta2,p2) */ - /* && JNI_jboolean_to_term((*env)->IsSameObject(env,(jobject)p1,(jobject)p2),tr); */ - /* break; */ - /* case 32: */ - /* r = JNI_term_to_ref(ta1,p1) */ - /* && JNI_term_to_jclass(ta2,p2) */ - /* && JNI_jboolean_to_term((*env)->IsInstanceOf(env,(jobject)p1,(jclass)p2),tr); */ - /* break; */ - case 95: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_jobject_to_term((*env)->GetObjectField(env,(jobject)p1,(jfieldID)p2),tr); - break; - case 96: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_jboolean_to_term((*env)->GetBooleanField(env,(jobject)p1,(jfieldID)p2),tr); - break; - case 97: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_jbyte_to_term((*env)->GetByteField(env,(jobject)p1,(jfieldID)p2),tr); - break; - case 98: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_jchar_to_term((*env)->GetCharField(env,(jobject)p1,(jfieldID)p2),tr); - break; - case 99: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_jshort_to_term((*env)->GetShortField(env,(jobject)p1,(jfieldID)p2),tr); - break; - case 100: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_jint_to_term((*env)->GetIntField(env,(jobject)p1,(jfieldID)p2),tr); - break; - case 101: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_jlong_to_term((*env)->GetLongField(env,(jobject)p1,(jfieldID)p2),tr); - break; - case 102: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_jfloat_to_term((*env)->GetFloatField(env,(jobject)p1,(jfieldID)p2),tr); - break; - case 103: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_jdouble_to_term((*env)->GetDoubleField(env,(jobject)p1,(jfieldID)p2),tr); - break; - case 145: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_jobject_to_term((*env)->GetStaticObjectField(env,(jclass)p1,(jfieldID)p2),tr); - break; - case 146: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_jboolean_to_term((*env)->GetStaticBooleanField(env,(jclass)p1,(jfieldID)p2),tr); - break; - case 147: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_jbyte_to_term((*env)->GetStaticByteField(env,(jclass)p1,(jfieldID)p2),tr); - break; - case 148: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_jchar_to_term((*env)->GetStaticCharField(env,(jclass)p1,(jfieldID)p2),tr); - break; - case 149: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_jshort_to_term((*env)->GetStaticShortField(env,(jclass)p1,(jfieldID)p2),tr); - break; - case 150: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_jint_to_term((*env)->GetStaticIntField(env,(jclass)p1,(jfieldID)p2),tr); - break; - case 151: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_jlong_to_term((*env)->GetStaticLongField(env,(jclass)p1,(jfieldID)p2),tr); - break; - case 152: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_jfloat_to_term((*env)->GetStaticFloatField(env,(jclass)p1,(jfieldID)p2),tr); - break; - case 153: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jfieldID(ta2,p2) - && JNI_jdouble_to_term((*env)->GetStaticDoubleField(env,(jclass)p1,(jfieldID)p2),tr); - break; - /* case 163: */ - /* r = JNI_term_to_charP(ta1,c1) // oughta be _jcharP, i.e. Unicode */ - /* && JNI_term_to_non_neg_jint(ta2,i2) */ - /* && JNI_jobject_to_term((*env)->NewString(env,(jchar*)c1,(jsize)i2),tr); */ - /* break; */ - /* case 165: */ - /* r = JNI_term_to_jstring(ta1,p1) */ - /* && JNI_jbuf_to_term((*env)->GetStringChars(env,(jstring)p1,(jboolean*)&i2),tr,JNI_atom_boolean) */ - /* && JNI_jboolean_to_term(i2,ta2); */ - /* break; */ - /* case 169: */ - /* r = JNI_term_to_jstring(ta1,p1) */ - /* && JNI_jbuf_to_term((*env)->GetStringUTFChars(env,(jstring)p1,(jboolean*)&i2),tr,JNI_atom_byte) */ - /* && JNI_jboolean_to_term(i2,ta2); */ - /* break; */ - case 173: - { int i; /* JW: i is long in this function */ - - i2 = 0; /* JW: make compiler happy */ - r = JNI_term_to_object_jarray(ta1,p1) - && JNI_term_to_jint(ta2,i2); - } - if ( r ) - r = JNI_jobject_to_term((*env)->GetObjectArrayElement(env,(jobjectArray)p1,(jsize)i2),tr); - break; - /* case 183: */ - /* r = JNI_term_to_boolean_jarray(ta1,p1) */ - /* && JNI_jbuf_to_term((*env)->GetBooleanArrayElements(env,(jbooleanArray)p1,(jboolean*)&i2),tr,JNI_atom_boolean) */ - /* && JNI_jboolean_to_term(i2,ta2); */ - /* break; */ - /* case 184: */ - /* r = JNI_term_to_byte_jarray(ta1,p1) */ - /* && JNI_jbuf_to_term((*env)->GetByteArrayElements(env,(jbyteArray)p1,(jboolean*)&i2),tr,JNI_atom_byte) */ - /* && JNI_jboolean_to_term(i2,ta2); */ - /* break; */ - /* case 185: */ - /* r = JNI_term_to_char_jarray(ta1,p1) */ - /* && JNI_jbuf_to_term((*env)->GetCharArrayElements(env,(jcharArray)p1,(jboolean*)&i2),tr,JNI_atom_char) */ - /* && JNI_jboolean_to_term(i2,ta2); */ - /* break; */ - /* case 186: */ - /* r = JNI_term_to_short_jarray(ta1,p1) */ - /* && JNI_jbuf_to_term((*env)->GetShortArrayElements(env,(jshortArray)p1,(jboolean*)&i2),tr,JNI_atom_short) */ - /* && JNI_jboolean_to_term(i2,ta2); */ - /* break; */ - /* case 187: */ - /* r = JNI_term_to_int_jarray(ta1,p1) */ - /* && JNI_jbuf_to_term((*env)->GetIntArrayElements(env,(jintArray)p1,(jboolean*)&i2),tr,JNI_atom_int) */ - /* && JNI_jboolean_to_term(i2,ta2); */ - /* break; */ - /* case 188: */ - /* r = JNI_term_to_long_jarray(ta1,p1) */ - /* && JNI_jbuf_to_term((*env)->GetLongArrayElements(env,(jlongArray)p1,(jboolean*)&i2),tr,JNI_atom_long) */ - /* && JNI_jboolean_to_term(i2,ta2); */ - /* break; */ - /* case 189: */ - /* r = JNI_term_to_float_jarray(ta1,p1) */ - /* && JNI_jbuf_to_term((*env)->GetFloatArrayElements(env,(jfloatArray)p1,(jboolean*)&i2),tr,JNI_atom_float) */ - /* && JNI_jboolean_to_term(i2,ta2); */ - /* break; */ - /* case 190: */ - /* r = JNI_term_to_double_jarray(ta1,p1) */ - /* && JNI_jbuf_to_term((*env)->GetDoubleArrayElements(env,(jdoubleArray)p1,(jboolean*)&i2),tr,JNI_atom_double) */ - /* && JNI_jboolean_to_term(i2,ta2); */ - /* break; */ - default: - return FALSE; /* oughta throw exception (design-time error :-) */ - break; - } - - return jni_check_exception(env) && r; - } - - -static foreign_t -jni_func_3_plc( - term_t tn, /* +integer: JNI function index */ - term_t ta1, /* +term: Arg1 */ - term_t ta2, /* +term: Arg2 */ - term_t ta3, /* +term: Arg3 */ - term_t tr /* -term: Result */ - ) - { - int n; /* JNI function index */ - functor_t fn; /* temp for conversion macros */ - term_t a1; /* " */ - /* term_t a2; // " */ - atom_t a; /* " */ - /* char *cp; // " */ - long i; /* " */ - /* int xhi; // " */ - /* int xlo; // " */ - jobject j; /* " */ - /* jlong jl; // " */ - void *p1; /* temp for converted (JVM) arg */ - void *p2; /* " */ - void *p3; /* " */ - /* char *c1; // " */ - char *c2; /* " */ - char *c3; /* " */ - int i1; /* " */ - /* int i2; // " */ - /* int i3; // " */ - /* jlong l1; // " */ - /* jlong l2; // " */ - /* jlong l3; // " */ - /* double d1; // " */ - /* double d2; // " */ - /* double d3; // " */ - jvalue *jvp = NULL; /* if this is given a buffer, it will be freed after the call */ - jboolean r; /* Prolog exit/fail outcome */ - JNIEnv *env; - - if ( !jni_ensure_jvm() - || !PL_get_integer(tn,&n) - ) - { - return FALSE; - } - - switch ( n ) - { - case 30: - r = JNI_term_to_non_array_jclass(ta1,p1) - && JNI_term_to_jmethodID(ta2,p2) - && JNI_term_to_pointer(ta3,jvp) - && JNI_jobject_to_term((*env)->NewObjectA(env,(jclass)p1,(jmethodID)p2,jvp),tr); - break; - case 33: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_charP(ta2,c2) - && JNI_term_to_charP(ta3,c3) - && JNI_jmethodID_to_term((*env)->GetMethodID(env,(jclass)p1,(char*)c2,(char*)c3),tr); - break; - case 36: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jmethodID(ta2,p2) - && JNI_term_to_pointer(ta3,jvp) - && JNI_jobject_to_term((*env)->CallObjectMethodA(env,(jobject)p1,(jmethodID)p2,jvp),tr); - break; - case 39: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jmethodID(ta2,p2) - && JNI_term_to_pointer(ta3,jvp) - && JNI_jboolean_to_term((*env)->CallBooleanMethodA(env,(jobject)p1,(jmethodID)p2,jvp),tr); - break; - case 42: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jmethodID(ta2,p2) - && JNI_term_to_pointer(ta3,jvp) - && JNI_jbyte_to_term((*env)->CallByteMethodA(env,(jobject)p1,(jmethodID)p2,jvp),tr); - break; - case 45: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jmethodID(ta2,p2) - && JNI_term_to_pointer(ta3,jvp) - && JNI_jchar_to_term((*env)->CallCharMethodA(env,(jobject)p1,(jmethodID)p2,jvp),tr); - break; - case 48: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jmethodID(ta2,p2) - && JNI_term_to_pointer(ta3,jvp) - && JNI_jshort_to_term((*env)->CallShortMethodA(env,(jobject)p1,(jmethodID)p2,jvp),tr); - break; - case 51: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jmethodID(ta2,p2) - && JNI_term_to_pointer(ta3,jvp) - && JNI_jint_to_term((*env)->CallIntMethodA(env,(jobject)p1,(jmethodID)p2,jvp),tr); - break; - case 54: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jmethodID(ta2,p2) - && JNI_term_to_pointer(ta3,jvp) - && JNI_jlong_to_term((*env)->CallLongMethodA(env,(jobject)p1,(jmethodID)p2,jvp),tr); - break; - case 57: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jmethodID(ta2,p2) - && JNI_term_to_pointer(ta3,jvp) - && JNI_jfloat_to_term((*env)->CallFloatMethodA(env,(jobject)p1,(jmethodID)p2,jvp),tr); - break; - case 60: - r = JNI_term_to_jobject(ta1,p1) - && JNI_term_to_jmethodID(ta2,p2) - && JNI_term_to_pointer(ta3,jvp) - && JNI_jdouble_to_term((*env)->CallDoubleMethodA(env,(jobject)p1,(jmethodID)p2,jvp),tr); - break; - case 94: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_charP(ta2,c2) - && JNI_term_to_charP(ta3,c3) - && JNI_jfieldID_to_term((*env)->GetFieldID(env,(jclass)p1,(char*)c2,(char*)c3),tr); - break; - case 113: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_charP(ta2,c2) - && JNI_term_to_charP(ta3,c3) - && JNI_jmethodID_to_term((*env)->GetStaticMethodID(env,(jclass)p1,(char*)c2,(char*)c3),tr); - break; - case 116: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jmethodID(ta2,p2) - && JNI_term_to_pointer(ta3,jvp) - && JNI_jobject_to_term((*env)->CallStaticObjectMethodA(env,(jclass)p1,(jmethodID)p2,jvp),tr); - break; - case 119: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jmethodID(ta2,p2) - && JNI_term_to_pointer(ta3,jvp) - && JNI_jboolean_to_term((*env)->CallStaticBooleanMethodA(env,(jclass)p1,(jmethodID)p2,jvp),tr); - break; - case 122: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jmethodID(ta2,p2) - && JNI_term_to_pointer(ta3,jvp) - && JNI_jbyte_to_term((*env)->CallStaticByteMethodA(env,(jclass)p1,(jmethodID)p2,jvp),tr); - break; - case 125: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jmethodID(ta2,p2) - && JNI_term_to_pointer(ta3,jvp) - && JNI_jchar_to_term((*env)->CallStaticCharMethodA(env,(jclass)p1,(jmethodID)p2,jvp),tr); - break; - case 128: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jmethodID(ta2,p2) - && JNI_term_to_pointer(ta3,jvp) - && JNI_jshort_to_term((*env)->CallStaticShortMethodA(env,(jclass)p1,(jmethodID)p2,jvp),tr); - break; - case 131: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jmethodID(ta2,p2) - && JNI_term_to_pointer(ta3,jvp) - && JNI_jint_to_term((*env)->CallStaticIntMethodA(env,(jclass)p1,(jmethodID)p2,jvp),tr); - break; - case 134: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jmethodID(ta2,p2) - && JNI_term_to_pointer(ta3,jvp) - && JNI_jlong_to_term((*env)->CallStaticLongMethodA(env,(jclass)p1,(jmethodID)p2,jvp),tr); - break; - case 137: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jmethodID(ta2,p2) - && JNI_term_to_pointer(ta3,jvp) - && JNI_jfloat_to_term((*env)->CallStaticFloatMethodA(env,(jclass)p1,(jmethodID)p2,jvp),tr); - break; - case 140: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_jmethodID(ta2,p2) - && JNI_term_to_pointer(ta3,jvp) - && JNI_jdouble_to_term((*env)->CallStaticDoubleMethodA(env,(jclass)p1,(jmethodID)p2,jvp),tr); - break; - case 144: - r = JNI_term_to_jclass(ta1,p1) - && JNI_term_to_charP(ta2,c2) - && JNI_term_to_charP(ta3,c3) - && JNI_jfieldID_to_term((*env)->GetStaticFieldID(env,(jclass)p1,(char*)c2,(char*)c3),tr); - break; - case 172: - r = JNI_term_to_non_neg_jint(ta1,i1) - && JNI_term_to_jclass(ta2,p2) - && JNI_term_to_ref(ta3,p3) - && JNI_jobject_to_term((*env)->NewObjectArray(env,(jsize)i1,(jclass)p2,(jobject)p3),tr); - break; - default: - return FALSE; /* oughta throw exception (design-time error :-) */ - break; - } - - if ( jvp != NULL ) - { - free( jvp); - } - - return jni_check_exception(env) && r; - } - - -static foreign_t -jni_func_4_plc( - term_t tn, /* +integer: JNI function index */ - term_t ta1, /* +term: Arg1 */ - term_t ta2, /* +term: Arg2 */ - term_t ta3, /* +term: Arg3 */ - term_t ta4, /* +term: Arg4 */ - term_t tr /* -term: Result */ - ) - { - int n; /* JNI function index */ - /* functor_t fn; // temp for conversion macros */ - /* term_t a1; // " */ - /* term_t a2; // " */ - /* atom_t a; // " */ - /* char *cp; // " */ - /* long i; // " */ - /* int xhi; // " */ - /* int xlo; // " */ - /* jobject j; // " */ - /* jlong jl; // " */ - /* void *p1; // temp for converted (JVM) arg */ - /* void *p2; // " */ - /* void *p3; // " */ - /* void *p4; // " */ - /* char *c1; // " */ - /* char *c2; // " */ - /* char *c3; // " */ - /* char *c4; // " */ - /* int i1; // " */ - /* int i2; // " */ - /* int i3; // " */ - /* int i4; // " */ - /* jlong l1; // " */ - /* jlong l2; // " */ - /* jlong l3; // " */ - /* jlong l4; // " */ - /* double d1; // " */ - /* double d2; // " */ - /* double d3; // " */ - /* double d4; // " */ - jvalue *jvp = NULL; /* if this is given a buffer, it will be freed after the call */ - jboolean r; /* Prolog exit/fail outcome */ - JNIEnv *env; - - if ( !jni_ensure_jvm() - || !PL_get_integer(tn,&n) - ) - { - return FALSE; - } - - switch ( n ) - { - /* case 5: */ - /* r = JNI_term_to_charP(ta1,c1) */ - /* && JNI_term_to_jobject(ta2,p2) */ - /* && JNI_term_to_jbuf(ta3,p3,JNI_atom_byte) */ - /* && JNI_term_to_jint(ta4,i4) */ - /* && JNI_jobject_to_term((*env)->DefineClass(env,(char*)c1,(jobject)p2,(jbyte*)p3,(jsize)i4),tr); */ - /* break; */ - /* case 66: */ - /* r = JNI_term_to_jclass(ta1,p1) */ - /* && JNI_term_to_jobject(ta2,p2) */ - /* && JNI_term_to_jmethodID(ta3,p3) */ - /* && JNI_term_to_pointer(ta4,jvp) */ - /* && JNI_jobject_to_term((*env)->CallNonvirtualObjectMethodA(env,(jobject)p1,(jclass)p2,(jmethodID)p3,jvp),tr); */ - /* break; */ - /* case 69: */ - /* r = JNI_term_to_jclass(ta1,p1) */ - /* && JNI_term_to_jobject(ta2,p2) */ - /* && JNI_term_to_jmethodID(ta3,p3) */ - /* && JNI_term_to_pointer(ta4,jvp) */ - /* && JNI_jboolean_to_term((*env)->CallNonvirtualBooleanMethodA(env,(jobject)p1,(jclass)p2,(jmethodID)p3,jvp),tr); */ - /* break; */ - /* case 72: */ - /* r = JNI_term_to_jclass(ta1,p1) */ - /* && JNI_term_to_jobject(ta2,p2) */ - /* && JNI_term_to_jmethodID(ta3,p3) */ - /* && JNI_term_to_pointer(ta4,jvp) */ - /* && JNI_jbyte_to_term((*env)->CallNonvirtualByteMethodA(env,(jobject)p1,(jclass)p2,(jmethodID)p3,jvp),tr); */ - /* break; */ - /* case 75: */ - /* r = JNI_term_to_jclass(ta1,p1) */ - /* && JNI_term_to_jobject(ta2,p2) */ - /* && JNI_term_to_jmethodID(ta3,p3) */ - /* && JNI_term_to_pointer(ta4,jvp) */ - /* && JNI_jchar_to_term((*env)->CallNonvirtualCharMethodA(env,(jobject)p1,(jclass)p2,(jmethodID)p3,jvp),tr); */ - /* break; */ - /* case 78: */ - /* r = JNI_term_to_jclass(ta1,p1) */ - /* && JNI_term_to_jobject(ta2,p2) */ - /* && JNI_term_to_jmethodID(ta3,p3) */ - /* && JNI_term_to_pointer(ta4,jvp) */ - /* && JNI_jshort_to_term((*env)->CallNonvirtualShortMethodA(env,(jobject)p1,(jclass)p2,(jmethodID)p3,jvp),tr); */ - /* break; */ - /* case 81: */ - /* r = JNI_term_to_jclass(ta1,p1) */ - /* && JNI_term_to_jobject(ta2,p2) */ - /* && JNI_term_to_jmethodID(ta3,p3) */ - /* && JNI_term_to_pointer(ta4,jvp) */ - /* && JNI_jint_to_term((*env)->CallNonvirtualIntMethodA(env,(jobject)p1,(jclass)p2,(jmethodID)p3,jvp),tr); */ - /* break; */ - /* case 84: */ - /* r = JNI_term_to_jclass(ta1,p1) */ - /* && JNI_term_to_jobject(ta2,p2) */ - /* && JNI_term_to_jmethodID(ta3,p3) */ - /* && JNI_term_to_pointer(ta4,jvp) */ - /* && JNI_jlong_to_term((*env)->CallNonvirtualLongMethodA(env,(jobject)p1,(jclass)p2,(jmethodID)p3,jvp),tr); */ - /* break; */ - /* case 87: */ - /* r = JNI_term_to_jclass(ta1,p1) */ - /* && JNI_term_to_jobject(ta2,p2) */ - /* && JNI_term_to_jmethodID(ta3,p3) */ - /* && JNI_term_to_pointer(ta4,jvp) */ - /* && JNI_jfloat_to_term((*env)->CallNonvirtualFloatMethodA(env,(jobject)p1,(jclass)p2,(jmethodID)p3,jvp),tr); */ - /* break; */ - /* case 90: */ - /* r = JNI_term_to_jclass(ta1,p1) */ - /* && JNI_term_to_jobject(ta2,p2) */ - /* && JNI_term_to_jmethodID(ta3,p3) */ - /* && JNI_term_to_pointer(ta4,jvp) */ - /* && JNI_jdouble_to_term((*env)->CallNonvirtualDoubleMethodA(env,(jobject)p1,(jclass)p2,(jmethodID)p3,jvp),tr); */ - /* break; */ - default: - return FALSE; /* oughta throw exception (design-time error :-) */ - break; - } - - if ( jvp != NULL ) - { - free( jvp); - } - - return jni_check_exception(env) && r; - } - - -/*=== JPL functions ================================================================================ */ - -static int - create_pool_engines(void); - -static int -jpl_num_initial_default_args(void) /* used only once, by jpl_do_jpl_init() */ - { - int i; - - for ( i=0 ; default_args[i]!=NULL ; i++ ) - { - } - return i; - } - - -/* outcomes: */ -/* fail to find jpl.*, jpl.fli.* classes or to convert init args to String[]: exception, FALSE */ -/* all OK: TRUE */ -static bool -jpl_do_jpl_init( /* to be called once only, after PL init, before any JPL calls */ - JNIEnv *env - ) - { - jclass tc; /* temporary class ref */ - jobject ta; /* temporary array ref */ - char *msg; /* error message for exceptions thrown here */ - int i; /* loop counter */ - jobject to; /* temporary (String) object ref */ - - if ( jpl_status != JPL_INIT_RAW ) /* jpl init already attempted? (shouldn't happen) */ - { - DEBUG(1, Sdprintf( "[JPL: jpl_do_jpl_init() called AGAIN (skipping...)]\n")); - return TRUE; - } - - /* prerequisites for setting initial default args into String[] pvm_dia: */ - if ( (tc=(*env)->FindClass(env,"java/lang/String")) == NULL - || (jString_c=(*env)->NewGlobalRef(env,tc)) == NULL - || ( (*env)->DeleteLocalRef(env,tc), FALSE) - - || (ta=(*env)->NewObjectArray(env,jpl_num_initial_default_args(),jString_c,NULL)) == NULL - || (pvm_dia=(*env)->NewGlobalRef(env,ta)) == NULL - || ( (*env)->DeleteLocalRef(env,ta), FALSE) - ) - { - msg = "jpl_do_jpl_init(): failed to find java.lang.String or create String[] pvm_dia"; - goto err; - } - - /* copy the initial default args into String[] pvm_dia: */ - for ( i=0 ; default_args[i]!=NULL ; i++ ) - { - if ( (to=(*env)->NewStringUTF(env,default_args[i])) == NULL ) - { - msg = "jpl_do_jpl_init(): failed to convert an initial default arg to a String"; - goto err; - } - (*env)->SetObjectArrayElement(env,pvm_dia,i,to); /* any errors/exceptions to be handled here? */ - } - - if ( (tc=(*env)->FindClass(env,"jpl/JPLException")) == NULL - || (jJPLException_c=(*env)->NewGlobalRef(env,tc)) == NULL - || ( (*env)->DeleteLocalRef(env,tc), FALSE) - - || (tc=(*env)->FindClass(env,"jpl/fli/term_t")) == NULL - || (jTermT_c=(*env)->NewGlobalRef(env,tc)) == NULL - || ( (*env)->DeleteLocalRef(env,tc), FALSE) - - || (tc=(*env)->FindClass(env,"jpl/fli/atom_t")) == NULL - || (jAtomT_c=(*env)->NewGlobalRef(env,tc)) == NULL - || ( (*env)->DeleteLocalRef(env,tc), FALSE) - - || (tc=(*env)->FindClass(env,"jpl/fli/functor_t")) == NULL - || (jFunctorT_c=(*env)->NewGlobalRef(env,tc)) == NULL - || ( (*env)->DeleteLocalRef(env,tc), FALSE) - - || (tc=(*env)->FindClass(env,"jpl/fli/fid_t")) == NULL - || (jFidT_c=(*env)->NewGlobalRef(env,tc)) == NULL - || ( (*env)->DeleteLocalRef(env,tc), FALSE) - - || (tc=(*env)->FindClass(env,"jpl/fli/predicate_t")) == NULL - || (jPredicateT_c=(*env)->NewGlobalRef(env,tc)) == NULL - || ( (*env)->DeleteLocalRef(env,tc), FALSE) - - || (tc=(*env)->FindClass(env,"jpl/fli/qid_t")) == NULL - || (jQidT_c=(*env)->NewGlobalRef(env,tc)) == NULL - || ( (*env)->DeleteLocalRef(env,tc), FALSE) - - || (tc=(*env)->FindClass(env,"jpl/fli/module_t")) == NULL - || (jModuleT_c=(*env)->NewGlobalRef(env,tc)) == NULL - || ( (*env)->DeleteLocalRef(env,tc), FALSE) - - || (tc=(*env)->FindClass(env,"jpl/fli/engine_t")) == NULL - || (jEngineT_c=(*env)->NewGlobalRef(env,tc)) == NULL - || ( (*env)->DeleteLocalRef(env,tc), FALSE) - - || (tc=(*env)->FindClass(env,"jpl/fli/LongHolder")) == NULL - || (jLongHolder_c=(*env)->NewGlobalRef(env,tc)) == NULL - || ( (*env)->DeleteLocalRef(env,tc), FALSE) - - || (tc=(*env)->FindClass(env,"jpl/fli/PointerHolder")) == NULL - || (jPointerHolder_c=(*env)->NewGlobalRef(env,tc)) == NULL - || ( (*env)->DeleteLocalRef(env,tc), FALSE) - - || (tc=(*env)->FindClass(env,"jpl/fli/IntHolder")) == NULL - || (jIntHolder_c=(*env)->NewGlobalRef(env,tc)) == NULL - || ( (*env)->DeleteLocalRef(env,tc), FALSE) - - || (tc=(*env)->FindClass(env,"jpl/fli/Int64Holder")) == NULL - || (jInt64Holder_c=(*env)->NewGlobalRef(env,tc)) == NULL - || ( (*env)->DeleteLocalRef(env,tc), FALSE) - - || (tc=(*env)->FindClass(env,"jpl/fli/DoubleHolder")) == NULL - || (jDoubleHolder_c=(*env)->NewGlobalRef(env,tc)) == NULL - || ( (*env)->DeleteLocalRef(env,tc), FALSE) - - || (tc=(*env)->FindClass(env,"jpl/fli/StringHolder")) == NULL - || (jStringHolder_c=(*env)->NewGlobalRef(env,tc)) == NULL - || ( (*env)->DeleteLocalRef(env,tc), FALSE) - - || (tc=(*env)->FindClass(env,"jpl/fli/ObjectHolder")) == NULL - || (jObjectHolder_c=(*env)->NewGlobalRef(env,tc)) == NULL - || ( (*env)->DeleteLocalRef(env,tc), FALSE) - - || (tc=(*env)->FindClass(env,"jpl/fli/BooleanHolder")) == NULL - || (jBooleanHolder_c=(*env)->NewGlobalRef(env,tc)) == NULL - || ( (*env)->DeleteLocalRef(env,tc), FALSE) - - || (jLongHolderValue_f=(*env)->GetFieldID(env,jLongHolder_c,"value","J")) == NULL - - || (jPointerHolderValue_f=(*env)->GetFieldID(env,jPointerHolder_c,"value","J")) == NULL - - || (jIntHolderValue_f=(*env)->GetFieldID(env,jIntHolder_c,"value","I")) == NULL - - || (jInt64HolderValue_f=(*env)->GetFieldID(env,jInt64Holder_c,"value","J")) == NULL - - || (jDoubleHolderValue_f=(*env)->GetFieldID(env,jDoubleHolder_c,"value","D")) == NULL - - || (jStringHolderValue_f=(*env)->GetFieldID(env,jStringHolder_c,"value","Ljava/lang/String;")) == NULL - - || (jObjectHolderValue_f=(*env)->GetFieldID(env,jObjectHolder_c,"value","Ljava/lang/Object;")) == NULL - - || (jBooleanHolderValue_f=(*env)->GetFieldID(env,jBooleanHolder_c,"value","Z")) == NULL - ) - { - msg = "jpl_do_jpl_init(): failed to find jpl.* or jpl.fli.* classes"; - goto err; - } - - DEBUG(1, Sdprintf( "[jpl_do_jpl_init() sets jpl_status = JPL_INIT_PVM_MAYBE, returns TRUE]\n")); - jpl_status = JPL_INIT_PVM_MAYBE; - return TRUE; - -err: - jpl_status = JPL_INIT_JPL_FAILED; - (*env)->ThrowNew(env,jJPLException_c,msg); - return FALSE; - } - - -/* prerequisite: */ -/* called only from jpl_test_pvm_init() and jpl_do_pvm_init() */ -/* outcomes: */ -/* error setting up post-PVM-init JPL state: throws exception, sets status = PVM_FAILED, returns FALSE */ -/* OK: sets status = OK, returns TRUE */ -static bool -jpl_post_pvm_init( - JNIEnv *env, - int argc, - char **argv - ) - { - char *msg; - jobject ta; - int i; - - /* Prolog VM is already initialised (by us or by other party) */ - /* retire default init args and set up actual init args: */ - pvm_dia = NULL; /* probably oughta delete (global) ref to former args... */ - if ( (ta=(*env)->NewObjectArray(env,argc,jString_c,NULL)) == NULL - || (pvm_aia=(*env)->NewGlobalRef(env,ta)) == NULL - || ( (*env)->DeleteLocalRef(env,ta), FALSE) - ) - { - msg = "jpl_post_pvm_init(): failed to copy actual init args"; - goto err; - } - for ( i=0 ; iNewStringUTF(env,argv[i]); - if ( to == NULL ) - { - msg = "jpl_post_pvm_init(): failed to convert actual PL init arg to String"; - goto err; - } - (*env)->SetObjectArrayElement(env,pvm_aia,i,to); - } - - if ( create_pool_engines() != 0 ) - { - msg = "jpl_post_pvm_init(): failed to create Prolog engine pool"; - goto err; - } - - jpl_status = JPL_INIT_OK; - return TRUE; - -err: - (*env)->ThrowNew( env, jJPLException_c, msg); - jpl_status = JPL_INIT_PVM_FAILED; - return FALSE; - } - - -/* prerequisite: jpl_status != JPL_INIT_RAW */ -/* outcomes: */ -/* PVM is not (already) initialised -> FALSE */ -/* PVM is (already) initialised -> TRUE */ -/* error setting up post-PVM-init JPL state -> exception */ -static bool -jpl_test_pvm_init( - JNIEnv *env - ) - { - char *msg; - int argc; - char **argv; - /* jobject ta; */ - /* int i; */ - - if ( jpl_status == JPL_INIT_RAW ) - { - msg = "jpl_test_pvm_init(): called while jpl_status == JPL_INIT_RAW"; - goto err; - } - - if ( jpl_status==JPL_INIT_JPL_FAILED || jpl_status==JPL_INIT_PVM_FAILED ) - { - msg = "jpl_test_pvm_init(): initialisation has already failed"; - goto err; - } - - if ( jpl_status == JPL_INIT_OK ) - { - return TRUE; - } - - if ( jpl_status == JPL_INIT_PVM_MAYBE ) - { - /* we test this each time (if not already initialised) in case other foreign code inits the PVM: */ - if ( !PL_is_initialised(&argc,&argv) ) /* PVM not ready? */ - { - /* jpl_status remains = JPL_INIT_PVM_MAYBE */ - DEBUG(1, Sdprintf( "[pl_test_pvm_init(): PL is not yet initialised: returning FALSE]\n")); - return FALSE; /* already-active Prolog VM not found (NB not an exceptional condition) */ - } - else - { - DEBUG(1, Sdprintf( "[pl_test_pvm_init(): PL is already initialised: proceeding to jpl_post_pvm_init()]\n")); - return jpl_post_pvm_init(env,argc,argv); /* TRUE, FALSE or exception */ - } - } - - msg = "jpl_test_pvm_init(): unknown jpl_status value"; - goto err; - -err: - (*env)->ThrowNew( env, jJPLException_c, msg); - jpl_status = JPL_INIT_PVM_FAILED; - return FALSE; - } - - -/* prerequisite: */ -/* jpl_status == JPL_INIT_PVM_MAYBE */ -/* outcomes: */ -/* successful PVM initialisation and subsequent JPL state setup -> TRUE */ -/* any error -> exception */ -static bool -jpl_do_pvm_init( - JNIEnv *env - ) - { - char *msg; - int argc; - char **argv; - int i; - jstring arg; - char *cp; - - /* redundant prerequisites check: */ - if ( jpl_status != JPL_INIT_PVM_MAYBE ) - { - msg = "jpl_do_pvm_init(): called while jpl_status != JPL_INIT_PVM_MAYBE"; - goto err; - } - - /* copy current default init args into suitable form for PL_initialise(): */ - if ( pvm_dia == NULL ) - { - msg = "jpl_do_pvm_init(): pvm_dia == NULL"; - goto err; - } - argc = (*env)->GetArrayLength(env,pvm_dia); - if ( argc <= 0 ) - { - msg = "jpl_do_pvm_init(): there are fewer than 1 default init args"; - goto err; - } - if ( (argv=(char**)malloc((argc+1)*sizeof(char*))) == NULL ) - { - msg = "jpl_do_pvm_init(): malloc() failed for argv"; - goto err; - } - for ( i=0 ; iGetObjectArrayElement(env,pvm_dia,i); - cp = (char*)(*env)->GetStringUTFChars(env,arg,0); - argv[i] = (char*)malloc(strlen(cp)+1); - strcpy( argv[i], cp); - DEBUG(1, Sdprintf( " argv[%d] = %s\n", i, argv[i])); - (*env)->ReleaseStringUTFChars( env, arg, cp); - } - DEBUG(1, Sdprintf( " argv[%d] = NULL\n", argc)); - argv[argc] = NULL; - if ( !PL_initialise(argc,(char**)argv) ) /* NB not (const char**) */ - { - msg = "jpl_do_pvm_init(): PL_initialise() failed"; - goto err; - } - /* *don't* free argv (must exist for lifetime of Prolog VM) */ - - return jpl_post_pvm_init(env,argc,argv); /* TRUE, FALSE or exception */ - -err: - jpl_status = JPL_INIT_PVM_FAILED; - (*env)->ThrowNew( env, jJPLException_c, msg); - return FALSE; - } - - -static bool - jpl_ensure_jpl_init_1( - JNIEnv *env - ) - { - bool r; - - pthread_mutex_lock( &jvm_init_mutex); - r = jpl_do_jpl_init(env); - pthread_mutex_unlock( &jvm_init_mutex); - return r; - } - - -static bool - jpl_ensure_pvm_init_1( - JNIEnv *env - ) - { - bool r; - - pthread_mutex_lock( &pvm_init_mutex); - if ( !jpl_ensure_jpl_init(env) ) - return FALSE; - r = jpl_test_pvm_init(env) || jpl_do_pvm_init(env); - pthread_mutex_unlock( &pvm_init_mutex); - return r; - } - - -/*=== initialisation-related native Java methods of jpl.fli.Prolog ================================= */ - -/* - * Class: jpl_fli_Prolog - * Method: get_default_init_args - * Signature: ()[Ljava/lang/String; - */ -/* if not yet init then return default init args as String[] */ -/* if already init then return NULL */ -/* if already failed to init then throw an exception */ -JNIEXPORT jobject JNICALL -Java_jpl_fli_Prolog_get_1default_1init_1args( - JNIEnv *env, - jclass jProlog - ) - { - char *msg; - - if ( !jpl_ensure_jpl_init(env) ) /* lazily do "local" initialisations iff necessary */ - return FALSE; - - if ( jpl_status==JPL_INIT_JPL_FAILED || jpl_status==JPL_INIT_PVM_FAILED ) - { - msg = "jpl.fli.Prolog.set_default_init_args(): initialisation has already failed"; - goto err; - } - - return ( jpl_test_pvm_init(env) /* if Prolog VM is initialised */ - ? NULL /* then default init args are no longer defined */ - : pvm_dia /* else here they are */ - ) - ; -err: - (*env)->ThrowNew( env, jJPLException_c, msg); - return FALSE; - } - - -/* - * Class: jpl_fli_Prolog - * Method: set_default_init_args - * Signature: ([Ljava/lang/String;)Z - */ -/* if the given jargs are null then throw an exception */ -/* if already failed to init then throw an exception */ -/* if not yet init then set default init args from jargs and return TRUE */ -/* if already init then return FALSE */ -JNIEXPORT jboolean JNICALL -Java_jpl_fli_Prolog_set_1default_1init_1args( - JNIEnv *env, - jclass jProlog, - jobject jargs /* oughta be proper array, perhaps zero-length */ - ) - { - char *msg; - - if ( !jpl_ensure_jpl_init(env) ) /* lazily do "local" initialisations iff necessary */ - return FALSE; - - if ( jargs == NULL ) /* improper call */ - { - msg = "jpl.fli.Prolog.set_default_init_args() called with NULL arg"; - goto err; - } - - if ( jpl_status==JPL_INIT_JPL_FAILED || jpl_status==JPL_INIT_PVM_FAILED ) - { - msg = "jpl.fli.Prolog.set_default_init_args(): initialisation has already failed"; - goto err; - } - - if ( jpl_test_pvm_init(env) ) /* if Prolog VM is initialised */ - { - return FALSE; /* unable to set default init args (too late: PVM is already initialised) */ - } - else - { - pvm_dia = NULL; /* probably oughta delete (global) (?) ref of former args... */ - pvm_dia = (*env)->NewGlobalRef(env,jargs); - return TRUE; /* OK: default init args set to those provided */ - } - -err: - (*env)->ThrowNew( env, jJPLException_c, msg); - return FALSE; - } - - -/* - * Class: jpl_fli_Prolog - * Method: get_actual_init_args - * Signature: ()[Ljava/lang/String; - */ -/* if not yet init then return null */ -/* if already init then return actual init args as String[] */ -/* if already failed to init then throw an exception */ -JNIEXPORT jobject JNICALL -Java_jpl_fli_Prolog_get_1actual_1init_1args( - JNIEnv *env, - jclass jProlog - ) - { - char *msg; - - if ( !jpl_ensure_jpl_init( env) ) /* lazily do "local" initialisations iff necessary */ - return NULL; - - if ( jpl_status==JPL_INIT_JPL_FAILED || jpl_status==JPL_INIT_PVM_FAILED ) - { - msg = "jpl.fli.Prolog.get_actual_init_args(): initialisation has already failed"; - goto err; - } - - return ( jpl_test_pvm_init(env) /* check PL_initialise() and update local state as appropriate */ - ? pvm_aia /* here they are */ - : NULL /* PVM not (yet) initialised */ - ); - -err: - (*env)->ThrowNew( env, jJPLException_c, msg); - return NULL; - } - - -/* - * Class: jpl_fli_Prolog - * Method: initialise - * Signature: ()Z - */ -/* if already init then return FALSE */ -/* if already failed to init then throw an exception */ -/* else attempt to init and if success then return TRUE else throw an exception */ -JNIEXPORT jboolean JNICALL -Java_jpl_fli_Prolog_initialise( - JNIEnv *env, - jclass jProlog - ) - { - char *msg; - - if ( !jpl_ensure_jpl_init( env) ) /* lazily do "local" initialisations iff necessary */ - return FALSE; - - if ( jpl_status==JPL_INIT_JPL_FAILED || jpl_status==JPL_INIT_PVM_FAILED ) - { - msg = "jpl.fli.Prolog.initialise(): initialisation has already failed"; - goto err; - } - - if ( jpl_test_pvm_init(env) ) - { - return FALSE; /* PVM is already initialised */ - } - else - { - jpl_do_pvm_init( env); - return jpl_test_pvm_init(env); - } - -err: - (*env)->ThrowNew( env, jJPLException_c, msg); - return FALSE; - } - - -/* - * Class: jpl_fli_Prolog - * Method: halt - * Signature: (I)V - */ -JNIEXPORT void JNICALL -Java_jpl_fli_Prolog_halt( - JNIEnv *env, - jclass jProlog, - jint jstatus - ) - { - - (void)jpl_ensure_pvm_init(env); - PL_halt( (int)jstatus); - } - - -/*=== JPL utility functions ======================================================================== */ - -/*----------------------------------------------------------------------- - * getLongValue - * - * Retrieves the value in a jpl.fli.LongHolder (or subclass) instance - * - * @param env Java environment - * @param jlong_holder the LongHolder class instance, or null - * @param lv address to write the retrieved (long) value - * @return success? (the LongHolder was not null) - *---------------------------------------------------------------------*/ -static bool -getLongValue( - JNIEnv *env, - jobject jlong_holder, - long *lv - ) - { - - if ( jlong_holder == NULL ) - { - *lv = 0L; - return FALSE; - } - else /* Java compilation ensures it's a jpl.fli.LongHolder instance */ - { - *lv = (long)(*env)->GetLongField(env,jlong_holder,jLongHolderValue_f); - return TRUE; - } - } - - -/*----------------------------------------------------------------------- - * getPointerValue - * - * Retrieves the value in a jpl.fli.PointerHolder instance - * - * @param env Java environment - * @param jpointer_holder the PointerHolder class instance, or null - * @param pv address to write the retrieved (pointer) value - * @return success? (the PointerHolder was not null) - *---------------------------------------------------------------------*/ -static bool -getPointerValue( /* sets pv to jpointer_holder's .value_ (and succeeds), else sets it to NULL (and fails) */ - JNIEnv *env, - jobject jpointer_holder, - pointer *pv - ) - { - - if ( jpointer_holder == NULL ) - { - *pv = (pointer)NULL; - return FALSE; - } - else /* Java compilation ensures it's a jpl.fli.PointerHolder instance */ - { - *pv = (pointer)(*env)->GetLongField(env,jpointer_holder,jPointerHolderValue_f); - return TRUE; - } - } - - -/*----------------------------------------------------------------------- - * setPointerValue - * - * Sets the value in a jpl.fli.Pointer class instance (unless it's null) - * to the supplied value (maybe 0L) - * - * @param env Java environment - * @param jpointer_holder the PointerHolder class instance, or null - * @param pv the new (pointer) value - *---------------------------------------------------------------------*/ -static bool -setPointerValue( - JNIEnv *env, - jobject jpointer_holder, - pointer pv - ) - { - - return jpointer_holder != NULL - && ( (*env)->SetLongField(env,jpointer_holder,jPointerHolderValue_f,(long)pv), - TRUE - ) - ; - } - - -/*----------------------------------------------------------------------- - * setIntValue - * - * Sets the value in a Java IntHolder class instance (unless it's null) - * to the supplied value - * - * @param env Java environment - * @param jint_holder the IntHolder class instance, or null - * @param iv the new (int) value - *---------------------------------------------------------------------*/ -static bool -setIntValue( - JNIEnv *env, - jobject jint_holder, - jint iv - ) - { - - return jint_holder != NULL - && ( (*env)->SetIntField(env,jint_holder,jIntHolderValue_f,iv), - TRUE - ) - ; - } - - -#if 0 -/*----------------------------------------------------------------------- - * setInt64Value - * - * Sets the value in a Java Int64Holder class instance (unless it's null) - * to the supplied value - * - * @param env Java environment - * @param jint64_holder the Int64Holder class instance, or null - * @param iv the new (int64_t) value - *---------------------------------------------------------------------*/ -static bool - setInt64Value( - JNIEnv *env, - jobject jint64_holder, - int64_t i64v - ) - { - - return jint64_holder != NULL - && ( (*env)->SetLongField(env,jint64_holder,jInt64HolderValue_f,i64v), - TRUE - ) - ; - } -#endif - - -/*----------------------------------------------------------------------- - * setLongValue - * - * Sets the value in a Java LongHolder class instance (unless it's null) - * to the supplied value (maybe 0L) - * - * @param env Java environment - * @param jlong_holder the LongHolder class instance, or null - * @param lv the new (long) value - *---------------------------------------------------------------------*/ -static bool -setLongValue( - JNIEnv *env, - jobject jlong_holder, - jlong lv - ) - { - - return jlong_holder != NULL - && ( (*env)->SetLongField(env,jlong_holder,jLongHolderValue_f,lv), - TRUE - ) - ; - } - - -/*----------------------------------------------------------------------- - * setDoubleValue - * - * Sets the value in a Java DoubleHolder class instance (unless it's null) - * to the supplied value - * - * @param env Java environment - * @param jdouble_holder the DoubleHolder class instance, or null - * @param dv the new (double) value - *---------------------------------------------------------------------*/ -static bool -setDoubleValue( - JNIEnv *env, - jobject jdouble_holder, - jdouble dv - ) - { - - return jdouble_holder != NULL - && ( (*env)->SetDoubleField(env,jdouble_holder,jDoubleHolderValue_f,dv), - TRUE - ) - ; - } - - -/*----------------------------------------------------------------------- - * setStringValue - * - * Sets the value in a Java StringHolder class instance (unless it's null) - * to the supplied value (maybe null) - * - * @param env Java environment - * @param jstring_holder the StringHolder class instance, or null - * @param sv the new (jstring) value - *---------------------------------------------------------------------*/ -static bool -setStringValue( - JNIEnv *env, - jobject jstring_holder, - jstring sv - ) - { - - return jstring_holder != NULL - && ( (*env)->SetObjectField(env,jstring_holder,jStringHolderValue_f,sv), - TRUE - ) - ; - } - - -#if 0 -/*----------------------------------------------------------------------- - * setObjectValue - * - * Sets the value in a Java ObjectHolder class instance (unless it's null) - * to the supplied value (maybe null) - * - * @param env Java environment - * @param jobject_holder the ObjectHolder class instance, or null - * @param ref the new (jobject) value - *---------------------------------------------------------------------*/ -static bool -setObjectValue( - JNIEnv *env, - jobject jobject_holder, - jobject ref - ) - { - - return jobject_holder != NULL - && ( (*env)->SetObjectField(env,jobject_holder,jObjectHolderValue_f,ref), - TRUE - ) - ; - } - - -/*----------------------------------------------------------------------- - * setBooleanValue - * - * Sets the .value field of a Java BooleanHolder class instance (unless it's null) - * to the supplied jboolean value - * - * @param env Java environment - * @param jboolean_holder the BooleanHolder class instance, or null - * @param jb the new (jboolean) value - *---------------------------------------------------------------------*/ -static bool -setBooleanValue( - JNIEnv *env, - jobject jboolean_holder, - jboolean jb - ) - { - - return jboolean_holder != NULL - && ( (*env)->SetBooleanField(env,jboolean_holder,jBooleanHolderValue_f,jb), - TRUE - ) - ; - } - - -/*----------------------------------------------------------------------- - * updateAtomValue - * - * Updates the value in a Java atom_t class instance (unless it's null) - * to the supplied value (maybe 0L); unregisters and registers old and new - * atom references as appropriate. NB atom_t extends LongHolder. - * - * @param env Java environment - * @param jatom_holder the atom_t class instance, or null - * @param atom2 the new atom reference - *---------------------------------------------------------------------*/ -static bool -updateAtomValue( - JNIEnv *env, - jobject jatom_holder, - atom_t atom2 /* new value (perhaps 0L (?)) */ - ) - { - atom_t atom1; /* old value (perhaps 0L (?)) */ - - if ( jatom_holder == NULL ) - { - return FALSE; - } - else - { - atom1 = (atom_t)(*env)->GetLongField(env,jatom_holder,jLongHolderValue_f); - if ( atom1 != 0L ) - { - PL_unregister_atom( atom1); - } - (*env)->SetLongField(env,jatom_holder,jLongHolderValue_f,(long)atom2); - if ( atom2 != 0L ) - { - PL_register_atom( atom2); - } - return TRUE; - } - } -#endif - -/*=== Java-wrapped SWI-Prolog FLI functions ======================================================== */ - -static int current_pool_engine_handle(PL_engine_t *e); -static int current_pool_engine(void); - - -/* - * Class: jpl_fli_Prolog - * Method: action_abort - * Signature: ()I - */ -JNIEXPORT int JNICALL - Java_jpl_fli_Prolog_action_1abort( - JNIEnv *env, - jclass jProlog - ) - { - - if ( jpl_ensure_pvm_init(env) ) - { - return PL_action(PL_ACTION_ABORT); - } - else - { - return -2; /* oughta throw exception? */ - } - } - - -/* - * Class: jpl_fli_Prolog - * Method: atom_chars - * Signature: (Ljpl/fli/atom_t;)Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL /* the local ref goes out of scope, */ - Java_jpl_fli_Prolog_atom_1chars( /* but the string itself doesn't */ - JNIEnv *env, - jclass jProlog, - jobject jatom - ) - { - atom_t atom; - jstring lref; - - return ( jpl_ensure_pvm_init(env) - && getLongValue(env,jatom,(long*)&atom) /* checks jatom != null */ - && jni_atom_to_String(env,atom,&lref) - ? lref - : NULL - ) - ; - } - - -/* - * Class: jpl_fli_Prolog - * Method: attach_engine - * Signature: (Ljpl/fli/engine_t;)I - */ -JNIEXPORT int JNICALL - Java_jpl_fli_Prolog_attach_1engine( - JNIEnv *env, - jclass jProlog, - jobject jengine - ) - { - PL_engine_t engine; - int rc; - - if ( !jpl_ensure_pvm_init(env) ) - { - return -2; /* libpl could not be initialised (oughta throw exception) */ - } - - rc = current_pool_engine_handle(&engine); - DEBUG(0, Sdprintf( "attach_engine(): current_engine=%p, thread_self=%d, pool_id=%d\n", engine, PL_thread_self(), rc)); - - if ( !getPointerValue(env,jengine,(pointer*)&engine) ) /* checks jengine isn't null */ - { - return -3; /* null engine holder */ - } - - DEBUG(0, Sdprintf( "attach_engine(): new_engine=%p\n", engine)); - - if ( (rc=PL_set_engine(engine,NULL)) == PL_ENGINE_SET ) - { - return 0; /* OK */ - } - else - { - return -1; /* bad engine status: oughta throw exception */ - } - - } - - -/* - * Class: jpl_fli_Prolog - * Method: close_query - * Signature: (Ljpl/fli/qid_t;)V - */ -JNIEXPORT void JNICALL - Java_jpl_fli_Prolog_close_1query( - JNIEnv *env, - jclass jProlog, - jobject jqid - ) - { - qid_t qid; - - DEBUG(1, Sdprintf( ">close_query(env=%lu,jProlog=%lu,jquid=%u)...\n", (long)env, (long)jProlog, (long)jqid)); - if ( jpl_ensure_pvm_init(env) - && getLongValue(env,jqid,(long*)&qid) /* checks that jqid != NULL */ - ) - { - PL_close_query( qid); /* void */ - DEBUG(1, Sdprintf( " ok: PL_close_query(%lu)\n", (long)qid)); - } - } - - -/* - * Class: jpl_fli_Prolog - * Method: compare - * Signature: (Ljpl/fli/term_t;Ljpl/fli/term_t;)I - */ -JNIEXPORT jint JNICALL /* returns -1, 0 or 1 (or -2 for error) */ - Java_jpl_fli_Prolog_compare( - JNIEnv *env, - jclass jProlog, - jobject jterm1, - jobject jterm2 - ) - { - term_t term1; - term_t term2; - - DEBUG(1, Sdprintf( ">compare(term1=%lu,term2=%lu)\n", (long)jterm1, (long)jterm2)); - if ( jpl_ensure_pvm_init(env) - && getLongValue(env,jterm1,(long*)&term1) /* checks jterm1 isn't null */ - && getLongValue(env,jterm2,(long*)&term2) /* checks jterm2 isn't null */ - ) - { - DEBUG(1, Sdprintf( "> PL_compare( %u, %u)", term1, term2)); - return PL_compare(term1,term2); /* returns -1, 0 or 1 */ - } - else - { - return -2; /* oughta throw an exception... */ - } - } - - -/* - * Class: jpl_fli_Prolog - * Method: cons_functor_v - * Signature: (Ljpl/fli/term_t;Ljpl/fli/functor_t;Ljpl/fli/term_t;)V - */ -JNIEXPORT void JNICALL - Java_jpl_fli_Prolog_cons_1functor_1v( - JNIEnv *env, - jclass jProlog, - jobject jterm, - jobject jfunctor, - jobject jterm0 - ) - { - term_t term; - functor_t functor; - term_t term0; - - if ( jpl_ensure_pvm_init(env) - && getLongValue(env,jterm,(long*)&term) /* checks that jterm isn't null */ - && getLongValue(env,jfunctor,(long*)&functor) /* checks that jfunctor isn't null */ - && getLongValue(env,jterm0,(long*)&term0) /* checks that jterm0 isn't null */ - ) - { - PL_cons_functor_v( term, functor, term0); - } - } - - -/* - * Class: jpl_fli_Prolog - * Method: copy_term_ref - * Signature: (Ljpl/fli/term_t;)Ljpl/fli/term_t; - */ -JNIEXPORT jobject JNICALL - Java_jpl_fli_Prolog_copy_1term_1ref( - JNIEnv *env, - jclass jProlog, - jobject jfrom - ) - { - jobject rval; - term_t term; - term_t term2; - - return ( jpl_ensure_pvm_init(env) - /* && jfrom != NULL // redundant: getLongValue checks this */ - && getLongValue(env,jfrom,(long*)&term) /* SWI RM implies must be non-null */ - && (rval=(*env)->AllocObject(env,jTermT_c)) != NULL - && ( (term2=PL_copy_term_ref(term)) , TRUE ) /* SWI RM -> always succeeds */ - && setLongValue(env,rval,(long)term2) - ? rval - : NULL /* oughta warn of failure? */ - ) - ; - } - - -/* - * Class: jpl_fli_Prolog - * Method: current_engine - * Signature: ()Ljpl/fli/engine_t; - */ -JNIEXPORT jobject JNICALL - Java_jpl_fli_Prolog_current_1engine( - JNIEnv *env, - jclass jProlog - ) - { - PL_engine_t engine; - jobject rval; - - return ( jpl_ensure_pvm_init(env) - && PL_thread_self() != -1 - && ( current_pool_engine_handle(&engine) , TRUE ) - && (rval=(*env)->AllocObject(env,jEngineT_c)) != NULL - && setPointerValue(env,rval,(pointer)engine) - ? rval - : NULL - ) - ; - } - - -/* - * Class: jpl_fli_Prolog - * Method: current_engine_is_pool - * Signature: ()Z - */ -JNIEXPORT jboolean JNICALL - Java_jpl_fli_Prolog_current_1engine_1is_1pool( - JNIEnv *env, - jclass jProlog - ) - { - - if ( jpl_ensure_pvm_init(env) ) - { - return current_pool_engine() >= 0; - } - else - { - return FALSE; /* libpl could not be initialised: oughta throw exception */ - } - } - - -/* - * Class: jpl_fli_Prolog - * Method: exception - * Signature: (Ljpl/fli/qid_t;)Ljpl/fli/term_t; - */ -JNIEXPORT jobject JNICALL - Java_jpl_fli_Prolog_exception( - JNIEnv *env, - jclass jProlog, - jobject jqid - ) - { - qid_t qid; - term_t term; - jobject term_t; /* return value */ - - DEBUG(1, Sdprintf( ">exception(jqid=%lu)\n", (long)jqid)); - return ( jpl_ensure_pvm_init(env) - && ( DEBUG(1, Sdprintf( " ok: jpl_ensure_pvm_init(env)\n")), TRUE ) - /* && jqid != NULL // redundant */ - && ( DEBUG(1, Sdprintf( " ok: jqid != NULL\n")), TRUE ) - && getLongValue(env,jqid,(long*)&qid) /* checks that jqid isn't null */ - && ( DEBUG(1, Sdprintf( " ok: getLongValue(env,jqid,(long*)&qid)\n")), TRUE ) - && ( (term=PL_exception(qid)) , TRUE ) /* we'll build a term_t object regardless */ - && ( DEBUG(1, Sdprintf(" ok: ( (term=PL_exception(qid)), TRUE)\n")), TRUE ) - && (term_t=(*env)->AllocObject(env,jTermT_c)) != NULL - && ( DEBUG(1, Sdprintf( " ok: (term_t=(*env)->AllocObject(env,jTermT_c)) != NULL\n")), TRUE ) - && setLongValue(env,term_t,(long)term) - && ( DEBUG(1, Sdprintf( " ok: setLongValue(env,term_t,(long)term)\n")), TRUE ) - ? ( - DEBUG(1, Sdprintf(" =%lu\n",(long)term_t)), - term_t - ) - : NULL /* oughta diagnose failure? */ - ) - ; - } - - -/* - * Class: jpl_fli_Prolog - * Method: get_arg - * Signature: (ILjpl/fli/term_t;Ljpl/fli/term_t;)Z - */ -JNIEXPORT jboolean JNICALL - Java_jpl_fli_Prolog_get_1arg( - JNIEnv *env, - jclass jProlog, - jint jindex, - jobject jterm, - jobject jarg - ) - { - term_t term; - term_t arg; - - return jpl_ensure_pvm_init(env) - && jarg != NULL /* don't proceed if this holder is null */ - && getLongValue(env,jterm,(long*)&term) /* checks that jterm isn't null */ - && ( arg=PL_new_term_ref() , TRUE ) /* Fred used jarg's original term ref (?) */ - && PL_get_arg(jindex,term,arg) - && setLongValue(env,jarg,(long)arg) - ; - } - - -/* - * Class: jpl_fli_Prolog - * Method: get_atom_chars - * Signature: (Ljpl/fli/term_t;Ljpl/fli/StringHolder;)Z - */ -JNIEXPORT jboolean JNICALL - Java_jpl_fli_Prolog_get_1atom_1chars( - JNIEnv *env, - jclass jProlog, - jobject jterm, - jobject jstring_holder - ) - { - term_t term; - atom_t a; - jstring string; - - return jpl_ensure_pvm_init(env) - && jstring_holder != NULL /* don't call PL_get_atom_chars if this is null */ - && getLongValue(env,jterm,(long*)&term) /* confirms that jterm != NULL */ - && PL_get_atom(term,&a) /* confirms that term is an atom */ - && jni_atom_to_String(env,a,&string) /* Unicode-aware */ - && setStringValue(env,jstring_holder,string) - ; - } - - -/* - * Class: jpl_fli_Prolog - * Method: get_c_lib_version - * Signature: ()Ljava/lang/String; - */ -JNIEXPORT jobject JNICALL - Java_jpl_fli_Prolog_get_1c_1lib_1version( - JNIEnv *env, - jclass jProlog - ) - { - - return (*env)->NewStringUTF(env,JPL_C_LIB_VERSION); /* JPL_C_LIB_VERSION is always Latin-1 */ - } - - -/* - * Class: jpl_fli_Prolog - * Method: get_float - * Signature: (Ljpl/fli/term_t;Ljpl/fli/DoubleHolder;)Z - */ -JNIEXPORT jboolean JNICALL - Java_jpl_fli_Prolog_get_1float( - JNIEnv *env, - jclass jProlog, - jobject jterm, - jobject jdouble_holder - ) - { - term_t term; - double d; - - return jpl_ensure_pvm_init(env) - && jdouble_holder != NULL - && getLongValue(env,jterm,(long*)&term) /* confirms that jterm isn't null */ - && PL_get_float(term,&d) - && setDoubleValue(env,jdouble_holder,d) - ; - } - - -/* - * Class: jpl_fli_Prolog - * Method: get_integer - * Signature: (Ljpl/fli/term_t;Ljpl/fli/Int64Holder;)Z - */ -JNIEXPORT jboolean JNICALL - Java_jpl_fli_Prolog_get_1integer( - JNIEnv *env, - jclass jProlog, - jobject jterm, - jobject jint64_holder - ) - { - term_t term; - int64_t i64; - - return jpl_ensure_pvm_init(env) - && jint64_holder != NULL - && getLongValue(env,jterm,(long*)&term) /* confirms that jterm isn't null */ - && PL_get_int64(term,&i64) - && setLongValue(env,jint64_holder,i64) - ; - } - - -/* - * Class: jpl_fli_Prolog - * Method: get_name_arity - * Signature: (Ljpl/fli/term_t;Ljpl/fli/StringHolder;Ljpl/fli/IntHolder;)Z - */ -JNIEXPORT jboolean JNICALL - Java_jpl_fli_Prolog_get_1name_1arity( - JNIEnv *env, - jclass jProlog, - jobject jterm, - jobject jname_holder, /* we trust this is a StringHolder */ - jobject jarity_holder /* we trust this is an IntHolder */ - ) - { - term_t term; - atom_t atom; - jstring jname; - int arity; - - return jpl_ensure_pvm_init(env) - && jname_holder != NULL /* don't proceed if this holder is null */ - && jarity_holder != NULL /* don't proceed if this holder is null */ - && getLongValue(env,jterm,(long*)&term) /* confirms that jterm isn't null */ - && PL_get_name_arity(term,&atom,&arity) /* proceed to register transient atom ref */ - && jni_atom_to_String(env,atom,&jname) /* Unicode-aware */ - && setStringValue(env,jname_holder,jname) /* stash String ref in holder */ - && setIntValue(env,jarity_holder,arity) /* stash arity value in holder */ - ; - } - - -/* - * Class: jpl_fli_Prolog - * Method: get_string_chars - * Signature: (Ljpl/fli/term_t;Ljpl/fli/StringHolder;)Z - */ -JNIEXPORT jboolean JNICALL - Java_jpl_fli_Prolog_get_1string_1chars( - JNIEnv *env, - jclass jProlog, - jobject jterm, - jobject jstring_holder - ) - { - term_t term; - jstring string; - - return jpl_ensure_pvm_init(env) - && jstring_holder != NULL - && getLongValue(env,jterm,(long*)&term) /* checks that jterm != NULL */ - && jni_string_to_String(env,term,&string) /* */ - && setStringValue(env,jstring_holder,string) /* ...when sent straight back to JVM */ - ; - } - - -/* - * Class: jpl_fli_Prolog - * Method: new_atom - * Signature: (Ljava/lang/String;)Ljpl/fli/atom_t; - */ -JNIEXPORT jobject JNICALL - Java_jpl_fli_Prolog_new_1atom( - JNIEnv *env, - jclass jProlog, - jstring jname - ) - { - atom_t atom; - jobject rval; - - return ( jpl_ensure_pvm_init(env) - && jname != NULL - && jni_String_to_atom(env,jname,&atom) - && (rval=(*env)->AllocObject(env,jAtomT_c)) != NULL /* doesn't call any constructor */ - && setLongValue(env,rval,(long)atom) - ? rval - : NULL /* oughta warn of failure? */ - ) - ; - } - - -/* - * Class: jpl_fli_Prolog - * Method: new_functor - * Signature: (Ljpl/fli/atom_t;I)Ljpl/fli/functor_t; - */ -JNIEXPORT jobject JNICALL - Java_jpl_fli_Prolog_new_1functor( - JNIEnv *env, - jclass jProlog, - jobject jatom, /* read-only */ - jint jarity - ) - { - term_t atom; - functor_t functor; - jobject rval; - - return ( jpl_ensure_pvm_init(env) - && jarity >= 0 - && getLongValue(env,jatom,(long*)&atom) /* checks jatom isn't null */ - && (rval=(*env)->AllocObject(env,jFunctorT_c)) != NULL - && (functor=PL_new_functor(atom,(int)jarity)) != 0L - && setLongValue(env,rval,(long)functor) - ? rval - : NULL /* oughta warn of failure? */ - ) - ; - } - - -/* - * Class: jpl_fli_Prolog - * Method: new_module - * Signature: (Ljpl/fli/atom_t;)Ljpl/fli/module_t; - */ -JNIEXPORT jobject JNICALL - Java_jpl_fli_Prolog_new_1module( - JNIEnv *env, - jclass jProlog, - jobject jatom - ) - { - atom_t atom; - module_t module; - jobject rval; - - return ( jpl_ensure_pvm_init(env) - && getLongValue(env,jatom,(long*)&atom) /* checks that jatom isn't null */ - && ( (module=PL_new_module(atom)) , TRUE ) - && (rval=(*env)->AllocObject(env,jModuleT_c)) != NULL - && setPointerValue(env,rval,(pointer)module) - ? rval - : NULL /* oughta warn of failure? */ - ) - ; - } - - -/* - * Class: jpl_fli_Prolog - * Method: new_term_ref - * Signature: ()Ljpl/fli/term_t; - */ -JNIEXPORT jobject JNICALL - Java_jpl_fli_Prolog_new_1term_1ref( - JNIEnv *env, - jclass jProlog - ) - { - jobject rval; - - return ( jpl_ensure_pvm_init(env) - && (rval=(*env)->AllocObject(env,jTermT_c)) != NULL - && setLongValue(env,rval,(long)PL_new_term_ref()) - ? rval - : NULL - ) - ; - } - - -/* - * Class: jpl_fli_Prolog - * Method: new_term_refs - * Signature: (I)Ljpl/fli/term_t; - */ -JNIEXPORT jobject JNICALL - Java_jpl_fli_Prolog_new_1term_1refs( - JNIEnv *env, - jclass jProlog, - jint jn - ) - { - jobject rval; - term_t trefs; - - DEBUG(1, Sdprintf( ">new_term_refs(env=%lu,jProlog=%lu,jn=%lu)...\n", (long)env, (long)jProlog, (long)jn)); - - return ( jpl_ensure_pvm_init(env) - && jn >= 0 /* I hope PL_new_term_refs(0) is defined [ISSUE] */ - && (rval=(*env)->AllocObject(env,jTermT_c)) != NULL - && ( trefs=PL_new_term_refs((int)jn), TRUE ) - && setLongValue(env,rval,(long)trefs) - && ( DEBUG(1, Sdprintf(" ok: stashed trefs=%ld into new term_t object\n",(long)trefs)), TRUE ) - ? rval - : NULL - ) - ; - } - - -/* - * Class: jpl_fli_Prolog - * Method: next_solution - * Signature: (Ljpl/fli/qid_t;)Z - */ -JNIEXPORT jboolean JNICALL - Java_jpl_fli_Prolog_next_1solution( - JNIEnv *env, - jclass jProlog, - jobject jqid /* read */ - ) - { - qid_t qid; - int rval; /* for boolean return value */ - - DEBUG(1, Sdprintf( ">next_solution(env=%lu,jProlog=%lu,jqid=%lu)...\n", (long)env, (long)jProlog, (long)jqid)); - return jpl_ensure_pvm_init(env) - && getLongValue(env,jqid,(long*)&qid) /* checks that jqid isn't null */ - && ( DEBUG(1, Sdprintf( " ok: getLongValue(env,jqid,(long*)&qid(%lu))\n",(long)qid)), TRUE ) - && ( rval=PL_next_solution(qid), TRUE ) /* can call this until it returns FALSE */ - && ( DEBUG(1, Sdprintf( " ok: PL_next_solution(qid=%lu)=%u\n",(long)qid,rval)), TRUE ) - && ( - DEBUG(1, Sdprintf(" =%lu\n",(long)rval)), - rval - ) - ; - } - - -/* - * Class: jpl_fli_Prolog - * Method: object_to_tag - * Signature: (Ljava/lang/Object;)Ljava/lang/String; - */ -JNIEXPORT jobject JNICALL - Java_jpl_fli_Prolog_object_1to_1tag( - JNIEnv *env, - jclass jProlog, - jobject jobj - ) - { - long iref; - char abuf[23]; - - /* empirically, unless the two 'ensure' macros are called in this order, */ - /* will crash if this is the first native method called */ - - /* Sdprintf("entered object_to_tag...\n"); */ - - if ( !jpl_ensure_pvm_init(env) ) { - /* Sdprintf("jpl_ensure_pvm_init() failed\n"); */ - return NULL; - } - /* Sdprintf("jpl_ensure_pvm_init() ok\n"); */ - - if ( !jni_ensure_jvm() ) { - /* Sdprintf("jni_ensure_jvm() failed\n"); */ - return NULL; - } - /* Sdprintf("jni_ensure_jvm() ok\n"); */ - - if ( jobj!=NULL && jni_object_to_iref(env,jobj,&iref) ) { - /* Sdprintf("jni_object_to_iref() done\n"); */ - sprintf( abuf, "J#%020lu", iref); /* oughta encapsulate this mapping... */ - /* Sdprintf("sprintf() done\n"); */ - return (*env)->NewStringUTF(env,abuf); /* a tag is always Latin-1 */ - } else { - return NULL; - } - } - - -/* - * Class: jpl_fli_Prolog - * Method: open_query - * Signature: (Ljpl/fli/module_t;ILjpl/fli/predicate_t;Ljpl/fli/term_t;)Ljpl/fli/qid_t; - */ -JNIEXPORT jobject JNICALL -Java_jpl_fli_Prolog_open_1query( - JNIEnv *env, - jclass jProlog, - jobject jmodule, /* read */ - jint jflags, /* read */ - jobject jpredicate, /* read */ - jobject jterm0 /* read */ - ) - { - module_t module; - predicate_t predicate; - term_t term0; - qid_t qid; - jobject jqid; /* for returned new QidT object */ - - DEBUG(1, Sdprintf( ">open_query(env=%lu,jProlog=%lu,jmodule=%lu,jflags=%lu,jpredicate=%lu,jterm0=%lu)...\n", - (long)env, (long)jProlog, (long)jmodule, (long)jflags, (long)jpredicate, (long)jterm0)); - return ( jpl_ensure_pvm_init(env) - && ( getPointerValue(env,jmodule,(pointer*)&module) , TRUE ) /* NULL module is OK below... */ - && ( DEBUG(1, Sdprintf(" ok: getPointerValue(env,jmodule=%lu,&(pointer)module=%lu)\n",(long)jmodule,(long)module)), TRUE ) - && getPointerValue(env,jpredicate,(pointer*)&predicate) /* checks that jpredicate != NULL */ - && ( DEBUG(1, Sdprintf(" ok: getPointerValue(env,jpredicate=%lu,&(pointer)predicate=%lu)\n",(long)jpredicate,(long)predicate)), TRUE ) - && getLongValue(env,jterm0,(long*)&term0) /* jterm0!=NULL */ - && ( (qid=PL_open_query(module,jflags,predicate,term0)) , TRUE ) /* NULL module is OK (?) [ISSUE] */ - && ( DEBUG(1, Sdprintf(" ok: PL_open_query(module=%lu,jflags=%u,predicate=%lu,term0=%lu)=%lu\n",(long)module,jflags,(long)predicate,(long)term0,(long)qid)), TRUE ) - && (jqid=(*env)->AllocObject(env,jQidT_c)) != NULL - && ( DEBUG(1, Sdprintf(" ok: AllocObject(env,jQidT_c)=%lu\n",(long)jqid)), TRUE ) - && setLongValue(env,jqid,(long)qid) - && ( DEBUG(1, Sdprintf(" ok: setLongValue(env,%lu,%lu)\n",(long)jqid,(long)qid)), TRUE ) - && ( DEBUG(1, Sdprintf("[open_query module = %s]\n", (module==NULL?"(null)":PL_atom_chars(PL_module_name(module))))), TRUE ) - ? ( - DEBUG(1, Sdprintf(" =%lu\n",(long)jqid)), - jqid - ) - : NULL /* oughta diagnose failure? raise JPL exception? */ - ) - ; - } - - -/* - * Class: jpl_fli_Prolog - * Method: predicate - * Signature: (Ljava/lang/String;ILjava/lang/String;)Ljpl/fli/predicate_t; - */ -JNIEXPORT jobject JNICALL - Java_jpl_fli_Prolog_predicate( - JNIEnv *env, - jclass jProlog, - jstring jname, /* ought not be null */ - jint jarity, /* oughta be >= 0 */ - jstring jmodule /* may be null */ - ) - { - atom_t pname; /* the predicate's name, as an atom */ - atom_t mname; /* the predicate's module's name, as an atom */ - functor_t func; /* the predicate's functor */ - module_t mod; /* the predicate's module */ - predicate_t predicate; - jobject rval; - - DEBUG(1, Sdprintf(">predicate(env=%lu,jProlog=%lu,jname=%lu,jarity=%lu,jmodule=%lu)...\n", - (long)env, (long)jProlog, (long)jname, (long)jarity, (long)jmodule)); - return ( jpl_ensure_pvm_init(env) - && jni_String_to_atom(env,jname,&pname) /* checks that jname isn't NULL */ - && jarity >= 0 - && ( func=PL_new_functor(pname,jarity) , TRUE ) /* "cannot fail" */ - && ( jmodule != NULL - ? jni_String_to_atom(env,jmodule,&mname) /* checks that jmodule isn't NULL */ - : ( mname=(atom_t)NULL , TRUE ) - ) - && ( mod=PL_new_module(mname) , TRUE) - && ( predicate=PL_pred(func,mod) , TRUE ) - && (rval=(*env)->AllocObject(env,jPredicateT_c)) != NULL - && setPointerValue(env,rval,(pointer)predicate) - ? ( - DEBUG(1, Sdprintf("[predicate() module=%s\n",(jmodule==NULL?"(null)":PL_atom_chars(mname)))), - rval - ) - : NULL /* oughta warn of failure? */ - ) - ; - } - - -/* - * Class: jpl_fli_Prolog - * Method: put_float - * Signature: (Ljpl/fli/term_t;D)V - */ -JNIEXPORT void JNICALL /* maybe oughta return jboolean (false iff given object is null) */ - Java_jpl_fli_Prolog_put_1float( - JNIEnv *env, - jclass jProlog, - jobject jterm, - jdouble jf - ) - { - term_t term; - - if ( jpl_ensure_pvm_init(env) - && getLongValue(env,jterm,(long*)&term) /* checks that jterm isn't null */ - ) - { - PL_put_float( term, jf); - } - } - - -/* - * Class: jpl_fli_Prolog - * Method: put_integer - * Signature: (Ljpl/fli/term_t;J)V - */ -JNIEXPORT void JNICALL /* maybe oughta return jboolean (false iff given object is null) */ - Java_jpl_fli_Prolog_put_1integer( - JNIEnv *env, - jclass jProlog, - jobject jterm, - jlong ji /* why jlong? */ - ) - { - term_t term; - - if ( jpl_ensure_pvm_init(env) - && getLongValue(env,jterm,(long*)&term) /* checks that jterm isn't null */ - ) - { - PL_put_integer( term, (int)ji); /* ??? */ - } - } - - -/* - * Class: jpl_fli_Prolog - * Method: put_term - * Signature: (Ljpl/fli/term_t;Ljpl/fli/term_t;)V - */ -JNIEXPORT void JNICALL /* maybe oughta return jboolean (false iff given object is null) */ - Java_jpl_fli_Prolog_put_1term( - JNIEnv *env, - jclass jProlog, - jobject jterm1, - jobject jterm2 - ) - { - term_t term1; - term_t term2; - - if ( jpl_ensure_pvm_init(env) - && getLongValue(env,jterm1,(long*)&term1) /* checks that jterm1 isn't null */ - && getLongValue(env,jterm2,(long*)&term2) /* checks that jterm2 isn't null */ - ) - { - PL_put_term( term1, term2); - } - } - - -/* - * Class: jpl_fli_Prolog - * Method: put_jref - * Signature: (Ljpl/fli/term_t;Ljava/lang/Object;)V - */ -/* added 29/1/2007 PS to support restored but now deprecated jpl.JRef for Rick Moynihan */ -JNIEXPORT void JNICALL - Java_jpl_fli_Prolog_put_1jref( - JNIEnv *env, - jclass jProlog, - jobject jterm, - jobject jref - ) - { - term_t term; - jobject j; // temp for JNI_jobject_to_term(+,-) - atom_t a; // " - long i; // " - - if ( jpl_ensure_pvm_init(env) - && jni_ensure_jvm() - && getLongValue(env,jterm,(long*)&term) // checks that jterm isn't null - ) - { - JNI_jobject_to_term(jref,term); // assumes term is var; OK if jref == null - } - } - - -/* - * Class: jpl_fli_Prolog - * Method: put_variable - * Signature: (Ljpl/fli/term_t;)V - */ -JNIEXPORT void JNICALL /* maybe oughta return jboolean (false iff given object is null) */ - Java_jpl_fli_Prolog_put_1variable( - JNIEnv *env, - jclass jProlog, - jobject jterm - ) - { - term_t term; - - if ( jpl_ensure_pvm_init(env) /* may throw exception but cannot fail */ - && getLongValue(env,jterm,(long*)&term) /* checks that jterm isn't null */ - ) - { - PL_put_variable(term); - } - } - - -/* - * Class: jpl_fli_Prolog - * Method: term_type - * Signature: (Ljpl/fli/term_t;)I - */ -JNIEXPORT jint JNICALL - Java_jpl_fli_Prolog_term_1type( - JNIEnv *env, - jclass jProlog, - jobject jterm - ) - { - term_t term; - - return ( jpl_ensure_pvm_init(env) - && getLongValue(env,jterm,(long*)&term) /* checks jterm isn't null */ - ? PL_term_type(term) - : -1 /* i.e. when jterm is null */ - ) - ; - } - - -/* - * Class: jpl_fli_Prolog - * Method: unregister_atom - * Signature: (Ljpl/fli/atom_t;)V - */ -JNIEXPORT void JNICALL - Java_jpl_fli_Prolog_unregister_1atom( - JNIEnv *env, - jclass jProlog, - jobject jatom - ) - { - atom_t atom; - - DEBUG(1, Sdprintf( ">unregister_atom(env=%lu,jProlog=%lu,jatom=%u)...\n", (long)env, (long)jProlog, (long)jatom)); - - if ( jpl_ensure_pvm_init(env) - && getLongValue(env,jatom,(long*)&atom) /* checks that jatom isn't null */ - ) - { - PL_unregister_atom( atom); /* void */ - DEBUG(1, Sdprintf( " ok: PL_unregister_atom(%lu)\n", (long)atom)); - } - } - - -/*=== JPL's Prolog engine pool and thread management =============================================== */ - -/* - * Class: jpl_fli_Prolog - * Method: thread_self - * Signature: ()I - */ -JNIEXPORT jint JNICALL -Java_jpl_fli_Prolog_thread_1self( - JNIEnv *env, - jclass jProlog - ) - { - - if ( jpl_ensure_pvm_init(env) ) - { - return PL_thread_self(); - } - else - { - return -2; - } - } - - -static int -create_pool_engines() - { - int i; - - DEBUG(1, Sdprintf( "JPL creating engine pool:\n")); - if ( (engines=malloc(sizeof(PL_engine_t)*JPL_MAX_POOL_ENGINES)) == NULL ) - { - return -1; /* malloc failed */ - } - engines_allocated = JPL_MAX_POOL_ENGINES; - memset(engines, 0, sizeof(PL_engine_t)*engines_allocated); - - DEBUG(1, Sdprintf( "JPL stashing default engine as [0]\n")); - PL_set_engine( PL_ENGINE_CURRENT, &engines[0]); - - DEBUG(1, Sdprintf( "JPL detaching default engine\n")); - /* PL_set_engine( NULL, NULL); */ - - for ( i=1 ; iAllocObject(env,jEngineT_c)) != NULL - && setPointerValue(env,rval,(pointer)engines[i]) - ? rval - : NULL - ); - } - if ( rc != PL_ENGINE_INUSE ) - { - DEBUG(1, Sdprintf( "JPL PL_set_engine fails with %d\n", rc)); - pthread_mutex_unlock( &engines_mutex); - return NULL; /* bad engine status: oughta throw exception */ - } - } - - for ( i=0 ; i 0 ) - { DEBUG(1, Sdprintf("JPL releasing engine[%d]=%p\n", i, e)); - PL_set_engine(NULL, NULL); - pthread_cond_signal(&engines_cond); /* alert waiters */ - } - return i; - } - else - { - return -2; - } - } - - -static foreign_t - jni_term_to_jref_plc( - term_t tref1, /* +term: AnyPrologTerm */ - term_t tref2 /* -term: JRef to a jpl.Term instance which represents that term */ - ) - { - jobject term1; - atom_t a; /* " */ - long i; /* " */ - jobject j; /* " */ - JNIEnv *env; - - return jni_ensure_jvm() /* untypically... */ - && jpl_ensure_pvm_init(env) /* ...this requires both inits */ - && (term1=(*env)->AllocObject(env,termt_class)) != NULL - && setLongValue(env,term1,(long)tref1) /* requires jLongHolderValue_f to be initialised */ - && JNI_jobject_to_term((*env)->CallStaticObjectMethod(env,term_class,term_getTerm,term1),tref2) - && jni_check_exception(env); - } - - -/* serves jni_jref_to_term_plc() */ -static bool - jni_jobject_to_term_byval( - JNIEnv *env, - jobject jobj, /* this must be an instance of one of jpl.Term's subclasses */ - term_t term /* a Prolog term, as represented by jobj, is *put* into this term ref */ - ) - { - jobject termt; /* a temporary instance of jpl.fli.term_t (i.e. a "term holder") */ - - return /* jni_ensure_jvm() && jpl_ensure_pvm_init(env) && */ - (termt=(*env)->AllocObject(env,termt_class)) != NULL - && setLongValue(env,termt,(long)term) /* requires jLongHolderValue_f to be initialised */ - && ( (*env)->CallStaticVoidMethod(env,term_class,term_putTerm,jobj,termt) , TRUE ) - && jni_check_exception(env) - ; - } - - -/* if the first arg is a jref i.e. @(Tag) which refers to a jpl.Term instance, */ -/* then the 2nd arg will be matched with a corresponding newly constructed term */ -static foreign_t - jni_jref_to_term_plc( - term_t jref, /* +term: JRef to a jpl.Term instance */ - term_t termIn /* -term: term as represented by the JRef */ - ) - { - functor_t fn; - term_t arg = PL_new_term_ref(); - atom_t a; - long iterm; - jobject jterm; - term_t term = PL_new_term_ref(); /* jni_jobject_to_term_byval() will *put* the constructed term in here */ - JNIEnv *env; - - return jni_ensure_jvm() /* untypically... */ - && jpl_ensure_pvm_init(env) /* ...this requires both inits */ - && PL_get_functor(jref,&fn) - && fn==JNI_functor_at_1 - && PL_get_arg(1,jref,arg) - && PL_get_atom(arg,&a) - && jni_tag_to_iref(a,&iterm) - && (jterm = (jobject)iterm) - && jni_jobject_to_term_byval(env,jterm,term) /* NB a bogus Tag could crash this... */ - && PL_unify( termIn, term) /* attempt to unify the 2nd arg with the newly constructed term */ - ; - } - - -static bool - jni_get_default_jvm_opts_1( - term_t args, - int i, - char **jvm_xia - ) - { - term_t tp = PL_new_term_ref(); - - if ( jvm_xia[i] == NULL ) - { - return PL_unify_nil(args); - } - else - { - return PL_unify_list(args,tp,args) - && PL_unify_term(tp, - PL_ATOM, PL_new_atom(jvm_xia[i]) - ) - && jni_get_default_jvm_opts_1(args,i+1,jvm_xia) - ; - } - } - - -static foreign_t - jni_get_jvm_opts( - term_t args, /* -list(atom): the current default JVM initialisation options */ - char **jvm_xia - ) - { - - if ( jvm_xia==NULL ) - { - return FALSE; - } - else - { - return jni_get_default_jvm_opts_1(args,0,jvm_xia); - } - } - - -static foreign_t - jni_set_default_jvm_opts_plc( - term_t tn, /* +integer: the qty of options */ - term_t args /* +list(atom): the current default JVM initialisation options */ - ) - { - int n; - int i; - term_t head; - term_t list; - char *s; - - if ( jvm_dia == NULL ) - { - return FALSE; /* presumably, JVM is already started, so default options cannot now be set */ - } - if ( !PL_get_integer(tn,&n) ) - { - return FALSE; - } - for ( i=0 ; i<100 ; i++ ) - { - if ( jvm_dia[i] == NULL ) - { - break; - } - else - { - free(jvm_dia[i]); - } - } - if ( n != i ) - { - if ( jvm_dia[0] != NULL ) /* definitely not first time? */ - { - free(jvm_dia); - } - jvm_dia = (char**)malloc((n+1)*sizeof(char**)); - } - head = PL_new_term_ref(); /* variable for the elements */ - list = PL_copy_term_ref(args); /* copy as we need to write */ - i = 0; - while ( PL_get_list(list,head,list) ) - { - if ( PL_get_atom_chars(head,&s) ) - { - jvm_dia[i] = (char*)malloc(strlen(s)+1); - strcpy(jvm_dia[i],s); - } - else - { - return FALSE; - } - i++; - } - jvm_dia[i] = NULL; - return PL_get_nil(list); /* test end for [] */ - } - - -static foreign_t - jni_get_default_jvm_opts_plc( - term_t args /* -list(atom): the current default JVM initialisation options */ - ) - { - - return jni_get_jvm_opts(args,jvm_dia); - } - - -static foreign_t - jni_get_actual_jvm_opts_plc( - term_t args /* -list(atom): the actual JVM initialisation options */ - ) - { - - return jni_get_jvm_opts(args,jvm_aia); - } - -/*=== FLI metadata ================================================================================= */ - -static - PL_extension predspecs[] = - { { "jni_get_created_jvm_count", 1, jni_get_created_jvm_count_plc, 0 }, - { "jni_ensure_jvm", 0, jni_ensure_jvm_plc, 0 }, - { "jni_tag_to_iref", 2, jni_tag_to_iref_plc, 0 }, - { "jni_hr_info", 4, jni_hr_info_plc, 0 }, - { "jni_hr_table", 1, jni_hr_table_plc, 0 }, - { "jni_byte_buf_length_to_codes", 3, jni_byte_buf_length_to_codes_plc, 0 }, - { "jni_param_put", 4, jni_param_put_plc, 0 }, - { "jni_alloc_buffer", 3, jni_alloc_buffer_plc, 0 }, - { "jni_free_buffer", 1, jni_free_buffer_plc, 0 }, - { "jni_fetch_buffer_value", 4, jni_fetch_buffer_value_plc, 0 }, - { "jni_stash_buffer_value", 4, jni_stash_buffer_value_plc, 0 }, - { "jni_void", 1, jni_void_0_plc, 0 }, - { "jni_void", 2, jni_void_1_plc, 0 }, - { "jni_void", 3, jni_void_2_plc, 0 }, - { "jni_void", 4, jni_void_3_plc, 0 }, - { "jni_void", 5, jni_void_4_plc, 0 }, - { "jni_func", 2, jni_func_0_plc, 0 }, - { "jni_func", 3, jni_func_1_plc, 0 }, - { "jni_func", 4, jni_func_2_plc, 0 }, - { "jni_func", 5, jni_func_3_plc, 0 }, - { "jni_func", 6, jni_func_4_plc, 0 }, -#ifdef __YAP_PROLOG__ - { "jni_SetByteArrayElement", 3, jni_SetByteArrayElement, 0 }, - { "jni_SetDoubleArrayElement", 3, jni_SetDoubleArrayElement, 0 }, -#endif - { "jpl_c_lib_version", 1, jpl_c_lib_version_1_plc, 0 }, - { "jpl_c_lib_version", 4, jpl_c_lib_version_4_plc, 0 }, - { "jni_term_to_jref", 2, jni_term_to_jref_plc, 0 }, - { "jni_jref_to_term", 2, jni_jref_to_term_plc, 0 }, - { "jni_get_default_jvm_opts", 1, jni_get_default_jvm_opts_plc, 0 }, - { "jni_set_default_jvm_opts", 2, jni_set_default_jvm_opts_plc, 0 }, - { "jni_get_actual_jvm_opts", 1, jni_get_actual_jvm_opts_plc, 0 }, - { NULL, 0, NULL, 0 } - }; - - -install_t - install(void) - { - PL_register_extensions( predspecs); - } - -/*=== end of jpl.c ================================================================================= */ -