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 greather) from previous page
- Download JBoss's javassist library
Code
import alice.tuprologx.pj.engine.*;
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());
}
}
}
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
javac -classpath javassist.jar:2p-3.0-alpha.jar PermutationUtility.java
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
java -classpath javassist.jar:2p-3.0-alpha.jar PermutationUtility
Expected Result
If you have done all steps correcly now you should see an output like this:
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]