06-JavaProlog

  • Upload
    ha-bui

  • View
    215

  • Download
    0

Embed Size (px)

Citation preview

  • 8/6/2019 06-JavaProlog

    1/26

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    06

    Java-Prolog Integration

    Mirko [email protected]

    Ingegneria Due

    Alma Mater StudiorumUniversita di Bologna a Cesena

    Academic Year 2006/2007

    http://find/http://goback/
  • 8/6/2019 06-JavaProlog

    2/26

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    On Java-Prolog Integration

    Prolog calling Java

    Java calling Prolog

    Prolog libraries written in Java

    Projects in Prolog

    http://find/http://goback/
  • 8/6/2019 06-JavaProlog

    3/26

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    Advantages of Prolog

    Declarative style of programming

    Concise way of expressing algorithms and data structures

    Intrinsic search & re-try mechanisms

    Navigation abilities

    Rapid prototyping

    (More near to model abstract concepts)

    http://find/http://goback/
  • 8/6/2019 06-JavaProlog

    4/26

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    Advantages of Java

    Imperative style of programming

    More easy for the typical programmer More efficient than Prolog

    Wide availability of libraries Graphics, Network, File-System, XML, . . .

    http://find/http://goback/
  • 8/6/2019 06-JavaProlog

    5/26

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    Why Java-Prolog integration

    Mainstream Scenario

    Using Java for everything

    Using Prolog for nothing

    The Proposed Scenario

    Using Java for standard management and low-level aspects

    Using Prolog for data & behaviour of the system model

    Examples

    In a MVC application, the model written in Prolog

    In writing an agent, modelling its mind in Prolog

    Sometimes, even accessing Java from Prolog to exploit theJVM

    O l O J P l I P l ll J J ll P l P l l b J P P l

    http://find/http://goback/
  • 8/6/2019 06-JavaProlog

    6/26

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    public class C{

    public static void main(String[] s){

    javax.swing.JFrame f=new javax.swing.JFrame();

    f.setSize(100,100);

    f.setVisible(true);}

    }

    This is a simple case of a Java class that creates a window on

    the screen using javax.swing library

    O li O J P l I i P l lli J J lli P l P l lib i i i J P j i P l

    http://find/http://goback/
  • 8/6/2019 06-JavaProlog

    7/26

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    createframe(Name,X,Y):-

    java_object(javax.swing.JFrame,[],Obj),

    Obj

  • 8/6/2019 06-JavaProlog

    8/26

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    Predicate java object/3

    Signature

    java object(+ClassName,+ArgumentList,-ObjRef)

    ClassName is the package-qualified name Retrieved through the classpath of the 2P application

    ArgumentList is a list of arguments for the constructor integers, strings, Java objects (see below)

    ObjRef is a Prolog term used as Object reference it is of the kind $obj 2

    It calls the Java construct of ClassName class, passingarguments, and the result is associated to handler ObjRef

    Example

    ?- java_object(Integer,1,X).

    Outline On Java Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    http://find/http://goback/
  • 8/6/2019 06-JavaProlog

    9/26

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    Predicate

  • 8/6/2019 06-JavaProlog

    10/26

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    Predicate returns/2

    Signature

    ObjRef

  • 8/6/2019 06-JavaProlog

    11/26

    Outline On Java Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    Other predicates

    Example

    To read/write the field of an object

    To create/access Java arrays

    To load a class dynamically

    . . .

    Where learning more?

    See document developer-guide.pdf in tuPrologdocumentation

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    http://find/http://goback/
  • 8/6/2019 06-JavaProlog

    12/26

    Outline On Java Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    An example application: handling vectors

    Want to handle vectors of integers in PrologProviding the following predicates

    vector new(-VectorRef)

    vector add(+VectorRef,+Element)

    vector addall(+VectorRef,+OtherVectorRef)

    vector clear(+VectorRef)

    vector contains(+VectorRef,+Element)

    vector lookup(+VectorRef,+Position,-Element)

    vector isempty(+VectorRef,+Position,-Element)

    vector size(+VectorRef,-Size)

    vector print(+VectorRef)

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    http://find/http://goback/
  • 8/6/2019 06-JavaProlog

    13/26

    g g g g g g g j g

    Some examples

    Code

    vector_new(V):-java_object(java.util.Vector,[],V).

    vector_add(V,E):-java_object(java.lang.Integer,[E],I),

    V

  • 8/6/2019 06-JavaProlog

    14/26

    The tuprolog Java library

    The approach..Via an API providing:

    Classes representing an engine, a term, a solution

    Methods to solve goals, ask for other solutions, navigate terms

    alice.tuprolog

    Main Classes

    Prolog: a Prolog Virtual Machine

    Theory: a Prolog DB, as a text or list of clauses

    SolveInfo: the outcome of a goal

    Term: the interface to any term

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    http://find/http://goback/
  • 8/6/2019 06-JavaProlog

    15/26

    Direct Goal Solution

    Working with the Prolog command-lineimport alice.tuprolog.*;

    public class Test1 {

    public static void main(String[] args) throws Exception {

    Prolog engine = new Prolog();// Note: append is a built-in predicate!

    SolveInfo info = engine.solve("append([1],[2,3],X).");

    System.out.println(info.getSolution());

    }

    }Result

    append([1],[2,3],[1,2,3])

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    http://find/http://goback/
  • 8/6/2019 06-JavaProlog

    16/26

    Getting all solutions

    public class Test2 {

    public static void main(String[] args) throws Exception {Prolog engine = new Prolog();

    SolveInfo info = engine.solve("append(X,Y,[1,2,3]).");

    while (info.isSuccess()){

    System.out.println(

    "solution: "+info.getSolution()+

    " - bindings: X/"+info.getTerm("X")+" Y/"+info.getTerm("Y"));

    if (engine.hasOpenAlternatives()){

    info=engine.solveNext();

    } else {

    break;

    } } } }

    solution: append([],[1,2,3],[1,2,3]) - bindings: X/[] Y/[1,2,3]

    solution: append([1],[2,3],[1,2,3]) - bindings: X/[1] Y/[2,3]

    solution: append([1,2],[3],[1,2,3]) - bindings: X/[1,2] Y/[3]

    solution: append([1,2,3],[],[1,2,3]) - bindings: X/[1,2,3] Y/[]

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    http://find/http://goback/
  • 8/6/2019 06-JavaProlog

    17/26

    The structure of Terms

    Term, The root of the hierarchy

    Methods:

    getRenamedCopy: Changes the name of variables

    isAtom: Is this a constant name or number?

    isList: Is this a list?

    unify: Does this unify with that?

    Hierarchy

    Term: The root of the hierarchy Var: A variable (with a name of anonymous) Number: int, float, and so on

    Double, Float, Int, Long

    Struct: a compound term, but also a list

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    http://find/http://goback/
  • 8/6/2019 06-JavaProlog

    18/26

    Creating and Using Terms

    public class Test3 {

    public static void main(String[] args) throws Exception {

    Prolog engine = new Prolog();

    // [1]

    Term list1=engine.toTerm("[1]");// [2,3]

    Term list2=new Struct(new Term[]{new Int(2),new Int(3)});

    // append([1],[2,3],X).

    Term app=new Struct("append",list1,list2,new Var("X"));

    SolveInfo info = engine.solve(app);

    System.out.println(info.getSolution());} }

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    http://find/http://goback/
  • 8/6/2019 06-JavaProlog

    19/26

    Reading Terms

    public class Test4 {

    public static void main(String[] args) throws Exception {

    Prolog engine = new Prolog();

    Term t=new Struct();

    // t will be [0,1,2,...,9,10]

    for (int i=10;i>=0;i--){t=new Struct(new Int(i),t);

    }

    Term app=new Struct("append",t,t,new Var("X"));

    SolveInfo info = engine.solve(app);

    Struct t2=(Struct)info.getTerm("X");

    for(java.util.Iterator i=t2.listIterator();i.hasNext();){System.out.println(""+i.next());

    } } }

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    http://find/http://goback/
  • 8/6/2019 06-JavaProlog

    20/26

    Setting a Theory

    public class Test5 {

    public static void main(String[] args) throws Exception {

    Prolog engine = new Prolog();

    Theory t=new Theory(

    "search(E,[E|_])."+"\n"+

    "search(E,[_|L]):-search(E,L)."+"\n"

    );

    // engine.setTheory(new FileInputStream("file.pl"));

    engine.setTheory(t);

    SolveInfo info = engine.solve("search(1,[1,2,3]).");

    System.out.println(""+info.getSolution());}

    }

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    http://find/http://goback/
  • 8/6/2019 06-JavaProlog

    21/26

    Which applications?

    Each time you want in Java something that is betterimplemented in Prolog

    Example

    Realise permutations over Lists

    Permutation are easy implemented in Prolog by a predicateperm(+L,-L2)

    Build a Java method that takes a LinkedList and returns aIterator, each time invokingsolveNext() and building the LinkedList from the result.

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    http://find/http://goback/
  • 8/6/2019 06-JavaProlog

    22/26

    Prolog Libraries

    What is a Prolog Library?

    It is basically a Prolog DB

    It includes the definition of many predicates

    It can be loaded in different ways:

    From another theory through goal load library/1 In the 2P IDE through the library manager

    Built-in examples

    BasicLibrary: Standard operators, e.g. append. ISOLibrary: Standard operators of ISO standard

    IOLibrary: for Input/Output, e.g. tell, told

    They are all automatically added by 2P IDE

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    http://find/http://goback/
  • 8/6/2019 06-JavaProlog

    23/26

    Building a Library in Java

    It is a very elegant approach

    Write a Java class where each method defines a differentpredicate

    Compile this class, e.g. with name lib.mylibrary

    Load the library with load library(lib.mylibrary)

    Structure of methods

    public boolean (T1 arg1,...,Tn argN)

    public Term (T1 arg1,...,Tn argN)

    where Ti is either Term, Var, etc.

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    http://find/http://goback/
  • 8/6/2019 06-JavaProlog

    24/26

    A Simple Library

    public class TestLibrary extends Library {// builtin functor sum(A,B)

    public Term sum_2(Number arg0, Number arg1){

    float arg0 = arg0.floatValue();

    float arg1 = arg1.floatValue();

    return new Float(arg0+arg1);}

    // builtin predicate println(Message)

    public boolean println_1(Struct arg){

    System.out.println(arg);

    return true;

    }

    }

    test :- N is sum(5,6), println(N).

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    http://find/http://goback/
  • 8/6/2019 06-JavaProlog

    25/26

    Projects in Prolog

    Building Prolog Libraries in Java

    Accessing a DBMS via JDBC

    Parsing an XML document, and transforming it using XSLT

    . . .

    Building Java Applications using Prolog

    A game like chess, where the computer is played by Prolog

    An application where a core is implemented in Prolog

    . . .

    Others related to Parsers and Languages

    . . .

    Outline On Java-Prolog Integration Prolog calling Java Java calling Prolog Prolog libraries written in Java Projects in Prolog

    http://find/http://goback/
  • 8/6/2019 06-JavaProlog

    26/26

    06

    Java-Prolog Integration

    Mirko [email protected]

    Ingegneria Due

    Alma Mater StudiorumUniversita di Bologna a Cesena

    Academic Year 2006/2007

    http://find/http://goback/