tuProlog--------java实习的prolog,可以好的java开发,,但是 唯一的不足是效率问题。有纯界面及eclipse插件
-
Install SWI-prolog Version 6.0.2 (or above) for your platform.
-
Windows: Add the “bin” directory of your SWI-Prolog installation to the system PATH.
-
Linux: Download the SWI-Prolog sources and see the README.linux file for further instructions. Make sure that all dependencies are loaded correctly (see http://www.swi-prolog.org/build/LinuxDistro.txt).
-
prolog java linux: 下载swi prolog 里面有为windos的bin文件夹下的
.dll和为linux的 .so 添加到变量classpath中,
下载jpl,添加jpl.jar 到classpath,,,,,,,可能jpl现在就直接在swi-prolog里面 lib下面
用java调用jpl开发plogog,可链接数据库 版本可能得注意
6.0.2的版本可能会有问题
http://www.swi-prolog.org/build/Debian.html
Using Prolog from Java
Swi-Prolog and Java on Linux (Fedora/Ubuntu/SuSE..)
I have had a lot of problems with getting the Swi-Prolog – Java binding to work. So here is my guide on how to do it.
Steps for installing swi-prolog withe JPL (Java Prolog binding) on Fedora 19.
1.
Install dependencies with this command:
Fedora, RHEL, CentOS:
sudo
yum
install
\
autoconf \
chrpath \
libunwind \
freetype-devel \
gmp-devel \
java-1.6.0-openjdk-devel \
jpackage-utils \
libICE-devel \
libjpeg-devel \
libSM-devel \
libX11-devel \
libXaw-devel \
libXext-devel \
libXft-devel \
libXinerama-devel \
libXmu-devel \
libXpm-devel \
libXrender-devel \
libXt-devel \
ncurses-devel \
openssl-devel \
pkgconfig \
readline-devel \
unixODBC-devel \
zlib-devel \
uuid-devel \
libarchive-devel
|
Ubuntu and Debian:
sudo
apt-get
install
\
build-essential autoconf curl chrpath \
ncurses-dev libreadline-dev libunwind8-dev \
libgmp-dev \
libxext-dev libice-dev libjpeg-dev libxinerama-dev libxft-dev \
libxpm-dev libxt-dev pkg-config \
libssl-dev \
unixodbc-dev \
openjdk-7-jdk junit \
zlib1g-dev libarchive-dev \
libossp-uuid-dev
|
可能要去掉 libunwind8-dev 加
libunwind7-dev
2.
Download Swi-Prolog source from her. 或直接http://ftp.at.debian.org/debian/pool/main/s/swi-prolog/swi-prolog_5.10.4-5_amd64.deb
3.
Untar the source (I untared to /usr/local/src/).
4.
Go into the untared prolog directory. I did
cd
/usr/local/src/pl-6
.4.1
|
Then configure:
.
/configure
|
Be sure you don’t have any errors her. It the configuration succiede, to make and install.
make
make
install
|
Now you can check that Prolog works, by:
swipl
|
If so, exit prolog (Crl-D).
5.
Go into the packages/ folder in the source directory:
cd
packages
|
(for me this is /usr/local/src/pl-6.4.1/packages).
Run this:
.
/configure
make
make
install
|
Now check that the jpl.jar file was built. For me this file is placed in /usr/local/pl-6.2.6/lib/swipl-6.2.6/lib/jpl.jar.
It might be in some slightly different location for you, run this to check you have it:
find
/ -name jpl.jar
|
6.
Set the LD_LIBRARY_PATH variable, in your ~/.bashrc file add this:
export
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:
/usr/local/lib/swipl-/lib//
|
to find the exact path go to your your swipl-/lib and check what folder are present. To find this location you could also do find / -name libswipl.so and add the path to the file (not the one from the source).
I did:
export
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:
/usr/local/lib/swipl-6
.4.1
/lib/x86_64-linux/
|
7.
Now we are done
To use it add the jpl.jar (we talked about in the end of point 5.) to your project (or link to it if your not using a ide).
8. Test
create these files
foo.pl:
1
2
|
foo(a).
foo(b).
|
MainJPL.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import
jpl.Query;
public
class
MainJPL {
public
static
void
main(String[] args) {
Query q;
q =
new
Query(
"consult('foo.pl')"
);
System.err.println(q.hasSolution());
q =
new
Query(
"foo(a)"
);
System.err.println(q.hasSolution());
q =
new
Query(
"foo(b)"
);
System.err.println(q.hasSolution());
q =
new
Query(
"foo(c)"
);
System.err.println(q.hasSolution());
q =
new
Query(
"foo(X)"
);
System.err.println(q.hasSolution());
while
(q.hasMoreElements()) {
System.err.println(q.nextElement());
}
}
}
|
in your IDE, make sure your working directory (in the run configuration) is set to the directory with the files (not a parent directory).
When you run you should get this output:
% foo.pl compiled 0,00 sec, 3 clauses
true
true
true
false
true
{X=a}
{X=b}
|
All done
Troubleshoot
Make sure to use Oracle java, Open JDK won’t work :/
If your getting some error like this while running the test:
Exception in thread
"main"
java.lang.UnsatisfiedLinkError: no jpl in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:
1738
)
at java.lang.Runtime.loadLibrary0(Runtime.java:
823
)
at java.lang.System.loadLibrary(System.java:
1028
)
at jpl.JPL.loadNativeLibrary(JPL.java:
100
)
at jpl.fli.Prolog.(Prolog.java:
85
)
at jpl.Query.open(Query.java:
286
)
at jpl.Util.textToTerm(Util.java:
162
)
at jpl.Query.(Query.java:
198
)
at MainJPL.main(MainJPL.java:
9
)
|
then you are using the wrong LD_LIBRARY_PATH, or in some way it is not set before you run your IDE. Check your path is correct, from terminal run
echo
$LD_LIBRARY_PATH
|
to check it is correctly set, then try running your IDE form terminal. If it is not set just run the export-command from step 6. in the terminal and then run your IDE.
If your geting somthin like this, when runing the test:
Exception in thread
"main"
java.lang.UnsatisfiedLinkError: /usr/local/lib/swipl-
6.4
.
1
/lib/x86_64-linux/libjpl.so: libjsig.so: cannot open shared object file: Ingen slik fil eller filkatalog
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:
1957
)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:
1882
)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:
1872
)
at java.lang.Runtime.loadLibrary0(Runtime.java:
849
)
at java.lang.System.loadLibrary(System.java:
1087
)
at jpl.JPL.loadNativeLibrary(JPL.java:
100
)
at jpl.fli.Prolog.(Prolog.java:
85
)
at jpl.Query.open(Query.java:
286
)
at jpl.Util.textToTerm(Util.java:
162
)
at jpl.Query.(Query.java:
198
)
at mars.explorer.MainJPL.main(MainJPL.java:
9
)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
57
)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
43
)
at java.lang.reflect.Method.invoke(Method.java:
606
)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:
120
)
|
then you should try to run it with another version of Java and make sure your not using Open JDK for this.