P@J
Although object-oriented languages are nowadays the mainstream for application development, several research contexts suggest that a multi-paradigm approach is worth pursuing. In particular, a declarative, logic-based paradigm could fruitfully add functionalities related to intelligence, adaptivity, and conciseness in expressing algorithms. The aim of the P@J project is to provide a framework for enhancing interoperability between Java and Prolog, based on the 2P open-source Prolog engine for Java.
P@J achieves smoother language-interoperability through two stacked layers:
- an API layer for automated mapping of Java types into Prolog types (and viceversa) and seamless exploitation of the Generic Collections Framework;
- an annotation layer, that aims at truly extending Java programming with the ability of specifying Prolog-based declarative implementations of Java methods, relying on Java annotations.
P@J Technology
- click for downloading the latest version of P@J
- Java Annotation Framework for tuProlog
Java Annotation Framework for tuProlog
- Check Integrating Java and Prolog through Generic Methods and Type Inference for more information on this feature
Example: Permutation
Requirements
- Download tuProlog (version 2.3.0 or greater) from the tuProlog page
- Download JBoss's javassist library
Code
import alice.tuprologx.pj.model.*;
import alice.tuprologx.pj.annotations.*;
public abstract class PermutationUtility {
@PrologMethod ( clauses = {
"permutation([],[]).",
"permutation(U,[X|V]):-remove(U,X,Z),permutation(Z,V).",
"remove([X|T],X,T).",
"remove([X|U],E,[X|V]):-remove(U,E,V)."
})
public abstract <$X extends List<Int>, $Y extends List<Int>> Iterable<$Y> permutation($X l);
public static void main(String[] args) throws Exception{
PermutationUtility pu = PJ.newInstance(PermutationUtility.class);
java.util.Collection<Integer> l=java.util.Arrays.<Integer>asList(new Integer[]{1,2,3});
for (List<Int> p : pu.permutation(new List<Int>(l))) {
System.out.println(p.toJava());
}
}
}
Compilation
- To compile the sample code you should add all required libraries in the classpath
- In this example we assume the libraries are in the same folder of sample code
- Use the command below to execute
Execution
- To execute the sample code you should add all required libraries in the classpath
- In the same way as for compilation, we assume the libraries are in the same folder of sample code
- Use the command below to execute
Expected Result
If you have done all steps correctly now you should see an output like this:
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]