O InterProlog e a Declarativa Miguel Calejo Declarativa http://www ...

skatechildrenΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 4 χρόνια και 6 μέρες)

151 εμφανίσεις

1

Introducing InterProlog


Architecture


Functionality and examples


From Prolog side


From Java side


A Java
-
Prolog
-
Java event roundtrip


Term displayers etc.


Under the hood


2

1st InterProlog architecture
-

sockets

sockets,

console redirection

Java VM

InterProlog


classes

Prolog

executable

InterProlog

predicates

A Java


application

A Prolog


application

Objects

Terms

3

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 :
-
)

4

Yet another use for DCGs :
-
)


Stream bytes
InterProlog
Object
Grammar
object(class(...),...data)
JAVA
Serialization
API
Object network
in memory
Prolog
Term
5

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…

6

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

7

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

8

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()

9

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);

}


10

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

11

“Hello World”, with a roundtrip

CREATING WINDOW FROM PROLOG:

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

12

Control flow in “Hello World”

a JButton
a
Prol ogEventBroker
acti onPerformed()
a Prol ogEngi ne
determi ni sti cGoal ("greetat(BUTTONID)")
a Call backHandler
a JTextFi el d
setText("Hel lo Worl d!")
greetat(T) :-
javaMessage(T,stri ng('Hel l o World!'))
13

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','Pokorn
y')]).


browseTreeTerm(t(root,[t(child1,[]),t(child2,[]),t(child3,[t(granchild31,[t(grandgr
andchild311,[])])])])).

14

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



15

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?

16

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[])


17

“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

18

“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)


19

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