Archive for the ‘Programare Logica’ Category

h1

SWI Prolog & JAVA

mai 31, 2008

Pentru tema4 la Programare Logica :

Să se creeze o interfaţă grafică în Java (sau alt limbaj, la alegere) care să permită aceleaşi operaţiuni cu cele descrise la tema 2.

Erori:
Exception in thread “main” java.lang.UnsatisfiedLinkError: no jpl in java.library.path

1) Se instaleaza SWI Prolog: download

2) In directorul unde s-a instalat ( de obicei C:\Program Files\pl) in directorul lib se gaseste jar-ul JPL. Acesta trebuie inclus ca librarie la compilare si la rulare. Daca se foloseste NetBeans in partea stanga se da click dreapta in proiect pe Libraries si Add Jar/Folder si cautati JPL-ul.

3) Da-ti click dreapta pe MyComputer -> Proprieties -> Advanced ->  Enviroment Variables -> New ( user variables)

La Name se pune: PATH

La Value se pune: calea unde este jpl.dll, de obicei “C:\Program Files\pl\bin”

4) Restart calculator

5) Se creeaza interfata – exemplu de assert si query:

a) Creem un fisier prolog de exemplu test.pl (daca folosim NetBeans creem acest fisier in director sursa.. acolo unde avem src,nbproject,test,etc.)
b) Creem un fisier java

import jpl.JPL;
import jpl.*;
import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.awt.event.*;
import jpl.Integer;
import jpl.Compound;
import jpl.Atom;
import jpl.Term;
import jpl.Query;

public class Main {

public static void main(String[] args) {
JPL.init();
Term consult_arg[] = {new Atom(“test.pl”)};

// Verificam daca programul Prolog se proceseaza corect
Query consult_query = new Query(“consult”, consult_arg);
boolean consulted = consult_query.query();
if (!consulted) {
System.err.println(“Consult failed”);
System.exit(1);
}

//Creem dinamic o fapta : sarpe(2,3)
Term primul = new Integer(2);
Term doi = new Integer(3);
Term pair = new Compound(“sarpe”, new Term[]{primul, doi});

System.out.println(pair);
Query assert_query = new Query(“assert”, pair);
assert_query.oneSolution();

//Interogam
Query q2 = new Query(“sarpe(X,Y)”);
System.out.println(“querying sarpe( X, Y )”);

while (q2.hasMoreSolutions()) {
java.util.Hashtable solution = q2.nextSolution();
System.out.println(“X=” + solution.get(“X”) + “,Y=” + solution.get(“Y”));

}
}
}
5)Compilam si Rulam. Obtinem:

//Pentru assert
sarpe(2, 3)

//Pentru query
querying sarpe( X, Y )
X=2,Y=3