Introducing interprolog
Download
1 / 20

Introducing InterProlog - PowerPoint PPT Presentation


  • 122 Views
  • Uploaded on

Introducing InterProlog. Architecture Functionality and examples From Prolog side From Java side A Java-Prolog-Java event roundtrip Term displayers etc. Under the hood. A Prolog application. A Java application. InterProlog predicates. InterProlog classes. Prolog executable.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Introducing InterProlog' - armand-larson


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Introducing interprolog
Introducing InterProlog

  • Architecture

  • Functionality and examples

    • From Prolog side

    • From Java side

    • A Java-Prolog-Java event roundtrip

    • Term displayers etc.

  • Under the hood


1st interprolog architecture sockets

A Prolog application

A Java application

InterProlog predicates

InterProlog classes

Prolog executable

Java VM

sockets, console redirection

1st InterProlog architecture - sockets

Terms

Objects


Interprolog architecture cont
InterProlog architecture (cont.)

  • Java/Prolog connection

    • Sockets (SubprocessEngine)

      • Java program linked to Prolog through I/O and object sockets, launches “PrologEngines”

        • currently XSB, SWI, YAP

    • JNI (NativeEngine)

      • native methods and built-in predicate for callbacks were added to XSB Prolog 2.5 and later

  • Dynamic Prolog->Java calls using Reflection

  • Serialized objects are parsed/generated by a Definite Clause Grammar :-)



Programming on the prolog side
Programming on the Prolog side

  • I think I’m under a console process

    • The listener window and the first “Hello, World”

      ?- writeln('Hello, world!').

  • javaMessage(Target,Message(Args))

    javaMessage( ‘java.lang.System’-out, println(string(‘hello, world’)) )

    • Cynical’s perspective: yet another RPC

    • Wide open API, as far as Java security allows

    • Variations handle exceptions, return value, cf. docs

    • Full version:

      javaMessage( Target, Result, Exception, MessageName, ArgList, NewArgList )

  • Needed: a generic approach to specifying objects and basic type values, stay tuned…


Object specifications in prolog
Object specifications in Prolog

  • string(Atom) and 'null'

  • ipObjectSpec('InvisibleObject',X,[ID],_)

    • Object X should be the object already existing and registered as (int) ID on the Java side 

  • ipObjectSpec('IPClassObject',X,[C],_)

    •  X is the class object for class with name C 

  • ipObjectSpec('IPClassVariable',X,[C,V],_)

    •  X is the class variable V of class C 

  • ipObjectSpec(boolean,X,[B],_)

    •  X is a boolean basic type for B (which should be 1 or 0); similar ipObjectSpec facts are available for the remaining basic types: byte, small, int, long, float, double, char 

  •  ipObjectSpec(C,X,Variables,_)

    • (Generic case) New object X of class C; C must be the fully qualified class name

    • How to add mine? to be seen in Sudoku example


Hello world in a window
Hello world, in a window

javaMessage('javax.swing.JFrame',W,'JFrame'(string(myTitle))),javaMessage(W,C,getContentPane), javaMessage('javax.swing.JLabel',L, 'JLabel'(string('Hello Prolog, greetings from Swing:-)'))

),javaMessage(C,add(string('Center'),L)),javaMessage(W,pack),

javaMessage(W,show).

  • Note concerning previous slide:

    • javaMessage/3 calls ipObjectSpecification/4 before calling javaMessage/6. A first look at interprolog.P


Programming on the java side basics
Programming on the Java side - basics

  • 1 PrologEngine <-> 1 XSB (or SWI or YAP) engine

    • Implementation restrictions

  • Object[] deterministicGoal( String G, String Ovars, Object[] objectsP, String RVars )

    • object specification subgoals must be included in G to accept objectsP (in Ovars list) and to generate the result (in RVars list)

    • Simpler variantes are available, e.g. not requiring input objects or not returning bindings, cf. Javadoc

  • int registerJavaObject(Object x)

    • Allow Prolog to refer x by an integer key reference

  • interrupt(), shutdown()


Programming on the java side
Programming on the Java side

PrologEngine engine = new SWISubprocessEngine();engine.consultFromJar("test.pl"); /* or consultRelative (to the class location), or consultAbsolute(File),…*/Object[] bindings = engine.deterministicGoal( "descendent_of(X,someAncestor)", "[string(X)]" );if(bindings!=null){// succeeded String X = (String)bindings[0]; System.out.println( "X = " + X); }


Programming on the java side1
Programming on the Java side

Object[] bindings = engine.deterministicGoal(

“X=string(helloWorld), ipObjectSpec(‘java.lang.Integer’,Y,[value=13]”,

”[X,Y]”

);

String hw = (String)bindings[0];

Integer lucky = (Integer)bindings[1];

  • The deterministicGoal variants

  • More examples

    • see packages com.declarativa.interprolog.gui, com.declarativa.interprolog.examples, PrologEngineTest class


Hello world with a roundtrip
“Hello World”, with a roundtrip

CREATING WINDOW FROM PROLOG:

ipPrologEngine(Engine), javaMessage( 'com.declarativa.interprolog.examples.HelloWindow', 'HelloWindow'(Engine))



Prolog term displayers
Prolog Term displayers

  • browseList([one(X),two(Y),1+1+1,X/Y]).

  • browseLiteralInstances(foo('First','Last'),[foo('Miguel','Calejo'),foo('Terry','Swift'),foo('David','Warren'),foo('Robert','Pokorny')]).

  • browseTreeTerm(t(root,[t(child1,[]),t(child2,[]),t(child3,[t(granchild31,[t(grandgrandchild311,[])])])])).


More prolog support stuff
More Prolog support stuff

  • The TermModel class

    • A convenient way to have Prolog term copies on the Java side

  • Building a Java representation of an arbitrary Prolog term…

    • buildTermModel(Term,TermModel)

  • …and vice-versa

    • recoverTermModel(TermModel,Term)

  • How to write a visual term browser

    • Hint: TermModel implements TreeModel


Playing with several prologs
Playing with several Prologs

callAnotherProlog(Engine,G) :- buildTermModel(G,GM), javaMessage(Engine,SM,deterministicGoal(GM)), recoverTermModel(SM,G).

?- javaMessage( 'com.declarativa.interprolog.SWISubprocessEngine', SWI,'SWISubprocessEngine'), callAnotherProlog(SWI, findall(op(P,T,Name), current_op(P,T,Name), SWIops)), findall(op(XSBP,XSBT,XSBO), ( current_op(XSBP,XSBT,XSBO), not(member(op(XSBP,XSBT,XSBO),SWIops) ), XSBonly).


A larger example sudoku puzzle editor and solver
A larger example: Sudoku puzzle editor and solver

  • Java side

    • A Sudoku board in a window (to start, call its constructor)

    • SudokuWindow class with inner classes

      • SudokuBoard extends JTable

      • SudokuModel implements TableModel

      • A few others

      • Menus call Prolog goals

  • Prolog side

    • Known numbers Ni in predicate facts n(X,Y,Ni)

    • resolve(PlacedNumbers1,PlacedNumbersN)

    • Front-end "action" predicates act on current puzzle

      • newPuzzle, openPuzzle(File,Array), savePuzzle(File)

  • How is data passed between Java and Prolog?


Getting object specification predicates from object examples
Getting object specification predicates from object examples

  • Sudoku example uses two object types for communication:

    • int[][] matrix for "bulk assigment" of the whole board model in one step

    • CellValue[] objects, representing a few cells, for "sparse" (a few at a time) assigment

  • Teaching InterProlog about my classes

    • teachMoreObjects(ObjectExamplePair[])


Under the hood java package file structure
“Under the hood”:Java package/file structure

  • com.declarativa.interprolog

    • com.declarativa.interprolog.gui

    • com.declarativa.interprolog.examples

    • com.declarativa.interprolog.util

    • xsb,swi,yap,gnu subdirectories with specific Prolog file

  • com.xsb.interprolog

    • NativeEngine and two related classes


Under the hood
“Under the hood”

  • The serialization grammar

  • About javaMessage

    • javaMessage/2, javaMessage/3, javaMessage/6

    • Returning objects rather than references: getRealJavaObject

  • A special Java object

    • ipPrologEngine(E)

  • The deterministicGoal/javaMessage tandem

  • Prolog peer classes

    • Supporting multiple Prolog systems

    • Walkthrough of ipPrologEngine(E), javaMessage(E,R,getPrologVersion)


Installation
Installation

  • Make sure Java and the Prolog systems work

  • Download and expand interprolog.zip

  • Edit windowsVariables

    • There are other ways to let InterProlog find the Prolog executables

  • Running

    • Use script to launch listener window

    • JUnit test

    • Use script to launch your app using the interprolog.jar


ad