104
A Portable and Extensible Approach for Linguistic Symbiosis between an Object-Oriented and a Logic Programming Language Sergio Castro [email protected] RELEASeD LAB Université catholique de Louvain Belgium 1 LogicObjects Monday 26 August 13

LogicObjects

Embed Size (px)

DESCRIPTION

Logic languages are well suited for declaratively solving computational problems that require knowledge representation and reasoning. Object-oriented programming languages benefit from mature software ecosystems featuring rich libraries and developer tools. Several integration solutions exist that allow a software system to be decomposed into a combination of modules implemented in both a logic and an object-oriented language. Unfortunately, significative amounts of boilerplate code must still be written to accomplish the required interoperability. In addition, such approaches often are not amenable to custom context-dependent reification of objects in the logic world and custom mappings of arbitrary logic terms to objects in the object-oriented world. Furthermore, in the specific case of Prolog-Java integration, existing solutions are often compatible with only a single or a restricted set of Prolog engines and thus suffer from portability issues. To address these problems, we introduce a portable framework, relying on linguistic integration, for transparently and (semi-)automatically enabling communication between routines in these two worlds, as well as a simple mechanism for customising how native artefacts in one language should be reified in the other language. We validate our approach with case studies requiring a seamless integration of declarative programs in Prolog with libraries belonging to the Java ecosystem.

Citation preview

Page 1: LogicObjects

A Portable and Extensible Approach for Linguistic Symbiosis between an Object-Oriented and a Logic

Programming Language

Sergio Castro [email protected]

RELEASeD LABUniversité catholique de Louvain

Belgium

1

LogicObjects

Monday 26 August 13

Page 2: LogicObjects

2

OOprogramming

Logicprogramming

Useful for modellingnon-declarative concepts

Often enjoy rich softwareecosystems

Declarative reasoning &Knowledge representation

Monday 26 August 13

Page 3: LogicObjects

2

OOprogramming

Logicprogramming

Useful for modellingnon-declarative concepts

Often enjoy rich softwareecosystems

Declarative reasoning &Knowledge representation

Monday 26 August 13

Page 4: LogicObjects

2

OOprogramming

Logicprogramming

Useful for modellingnon-declarative concepts

Often enjoy rich softwareecosystems

Declarative reasoning &Knowledge representation

Monday 26 August 13

Page 5: LogicObjects

2

OOprogramming

Logicprogramming

Useful for modellingnon-declarative concepts

Often enjoy rich softwareecosystems

Declarative reasoning &Knowledge representation

Monday 26 August 13

Page 6: LogicObjects

3

Monday 26 August 13

Page 7: LogicObjects

Considerable amount of integration tools and techniques

4

Monday 26 August 13

Page 8: LogicObjects

Considerable amount of integration tools and techniques

4

SOUL

TyRuBaJPL

InterProlog

PDT Connector

TuPrologLogicJava Jiprolog

Kernel-Prolog CIAO Prolog

Jasper

Jekejeke PrologKiev

PrologBeans

PrologCafeJavaLogMINERVA

jProlog

ProvaK-PrologCommonRules Styla

Jinni Prolog

Yajxb

Monday 26 August 13

Page 9: LogicObjects

Research scope

5

“Bidirectional linguistic integration between Prolog and a statically typed, class based OO language (Java)”

Monday 26 August 13

Page 10: LogicObjects

But... why Java ?

• A huge Java ecosystem.

• Emerging languages running on the JVM (e.g. Scala, Clojure, etc.).

• A large user base.6

We recognize the advantages of:

Monday 26 August 13

Page 11: LogicObjects

Outline

• JPC: a portable Java-Prolog interoperability layer.

• LogicObjects: a linguistic integration framework.

• Research status.

• Future work.

• Conclusions.

7

Monday 26 August 13

Page 12: LogicObjects

8

Prolog engines

Bridge libraries

JPC drivers

JPC library

Java-Prolog applications

Java-Prolog frameworks

(layer coupling denoted by the direction of the arrows)

High level architectural overview

Monday 26 August 13

Page 13: LogicObjects

• Attempts to facilitate the creation of hybrid Java-Prolog applications and frameworks.

• Inspired by JPL, InterProlog and Google’s Gson libraries.

• Compatible with some popular open source Prolog engines (XSB,YAP, SWI) and more coming soon.

• Available at the Maven central snapshot repository and GitHub1 (currently) under the LGPL license.

9

1https://github.com/sergio-castro/

JPC: Java-Prolog connectivity

Monday 26 August 13

Page 14: LogicObjects

JPC features

• A portable abstraction of a Prolog virtual machine.

• A set of utilities for dealing with common Java-Prolog interoperability problems.

• High level support for :

•Mapping between Java objects and Prolog terms.

•Dealing with object references and object serialization in Prolog.

10

Monday 26 August 13

Page 15: LogicObjects

11

Converter manager

Instantiation managerType solver

JPC Context

A conversion context as a first order entity

Monday 26 August 13

Page 16: LogicObjects

12

Converter1 Converter2 Converter3

Dispatcher

Converter manager

The converter manager

Monday 26 August 13

Page 17: LogicObjects

12

Converter1 Converter2 Converter3

Dispatcher

(chain of responsibility pattern)

Converter manager

The converter manager

Monday 26 August 13

Page 18: LogicObjects

12

Converter1 Converter2 Converter3

Dispatcher

(chain of responsibility pattern)

Converter manager

The converter manager

primitive types converters,exception converters, multi-valued converters, etc.

Monday 26 August 13

Page 19: LogicObjects

Modularising mappings between Java objects and Prolog terms

public class StationConverter extends JpcConverter<Station, Compound> { public static final String STATION_FUNCTOR = "station"; @Override public Compound toTerm(Station station, Jpc context) { return new Compound(STATION_FUNCTOR, asList(new Atom(station.getName()))); } @Override public Station fromTerm(Compound term, Jpc context) { String stationName = ((Atom)term.arg(1)).getName(); return new StationJpc(stationName); }}

13

Monday 26 August 13

Page 20: LogicObjects

Modularising mappings between Java objects and Prolog terms

public class StationConverter extends JpcConverter<Station, Compound> { public static final String STATION_FUNCTOR = "station"; @Override public Compound toTerm(Station station, Jpc context) { return new Compound(STATION_FUNCTOR, asList(new Atom(station.getName()))); } @Override public Station fromTerm(Compound term, Jpc context) { String stationName = ((Atom)term.arg(1)).getName(); return new StationJpc(stationName); }}

13

Java type

Monday 26 August 13

Page 21: LogicObjects

Modularising mappings between Java objects and Prolog terms

public class StationConverter extends JpcConverter<Station, Compound> { public static final String STATION_FUNCTOR = "station"; @Override public Compound toTerm(Station station, Jpc context) { return new Compound(STATION_FUNCTOR, asList(new Atom(station.getName()))); } @Override public Station fromTerm(Compound term, Jpc context) { String stationName = ((Atom)term.arg(1)).getName(); return new StationJpc(stationName); }}

13

Prolog type

Monday 26 August 13

Page 22: LogicObjects

Modularising mappings between Java objects and Prolog terms

public class StationConverter extends JpcConverter<Station, Compound> { public static final String STATION_FUNCTOR = "station"; @Override public Compound toTerm(Station station, Jpc context) { return new Compound(STATION_FUNCTOR, asList(new Atom(station.getName()))); } @Override public Station fromTerm(Compound term, Jpc context) { String stationName = ((Atom)term.arg(1)).getName(); return new StationJpc(stationName); }}

13

Monday 26 August 13

Page 23: LogicObjects

Modularising mappings between Java objects and Prolog terms

public class StationConverter extends JpcConverter<Station, Compound> { public static final String STATION_FUNCTOR = "station"; @Override public Compound toTerm(Station station, Jpc context) { return new Compound(STATION_FUNCTOR, asList(new Atom(station.getName()))); } @Override public Station fromTerm(Compound term, Jpc context) { String stationName = ((Atom)term.arg(1)).getName(); return new StationJpc(stationName); }}

13

Monday 26 August 13

Page 24: LogicObjects

Configuring a JPC context with a custom converter

Jpc jpcContext = JpcBuilder.create().registerConverter(new StationConverter()).build();

14

Monday 26 August 13

Page 25: LogicObjects

Configuring a JPC context with a custom converter

Jpc jpcContext = JpcBuilder.create().registerConverter(new StationConverter()).build();

14

Default builder (includes pre-defined converters)

Monday 26 August 13

Page 26: LogicObjects

Configuring a JPC context with a custom converter

Jpc jpcContext = JpcBuilder.create().registerConverter(new StationConverter()).build();

14

Default builder (includes pre-defined converters)

Extending JPC with new conversions

Monday 26 August 13

Page 27: LogicObjects

Querying Prolog with JPC

15

public class StationManager {public static final Jpc jpcContext = ... //context includes StationConverter...public Station mainStation() { String stationVarName = "Station"; Term goal = new Compound("main_station", asList(new Variable(stationVarName))); Query query = getPrologEngine().query(goal, jpcContext); return query.<Station>selectObject(stationVarName).oneSolutionOrThrow();}

}

Monday 26 August 13

Page 28: LogicObjects

Querying Prolog with JPC

15

public class StationManager {public static final Jpc jpcContext = ... //context includes StationConverter...public Station mainStation() { String stationVarName = "Station"; Term goal = new Compound("main_station", asList(new Variable(stationVarName))); Query query = getPrologEngine().query(goal, jpcContext); return query.<Station>selectObject(stationVarName).oneSolutionOrThrow();}

}

main_station(Station)

Monday 26 August 13

Page 29: LogicObjects

Querying Prolog with JPC

15

public class StationManager {public static final Jpc jpcContext = ... //context includes StationConverter...public Station mainStation() { String stationVarName = "Station"; Term goal = new Compound("main_station", asList(new Variable(stationVarName))); Query query = getPrologEngine().query(goal, jpcContext); return query.<Station>selectObject(stationVarName).oneSolutionOrThrow();}

}

Monday 26 August 13

Page 30: LogicObjects

Querying Prolog with JPC

15

public class StationManager {public static final Jpc jpcContext = ... //context includes StationConverter...public Station mainStation() { String stationVarName = "Station"; Term goal = new Compound("main_station", asList(new Variable(stationVarName))); Query query = getPrologEngine().query(goal, jpcContext); return query.<Station>selectObject(stationVarName).oneSolutionOrThrow();}

}binding of Station as a Java object

Monday 26 August 13

Page 31: LogicObjects

Dealing with object references@Testpublic void testJRef() {

Object o = new Object();Term jRef = RefManager.jRefTerm(o);assertTrue(o == jpc.fromTerm(jRef));o = null;System.gc();try {

jpc.fromTerm(jRef); fail();

} catch(RuntimeException e) {}}

16

Monday 26 August 13

Page 32: LogicObjects

Dealing with serialized objects

@Testpublic void testSerializedTerm() { String s = "hello"; Term term = SerializedObject.serializedObjectTerm(s); String s2 = jpc.fromTerm(term); assertFalse(s == s2); assertEquals(s, s2);}

17

Monday 26 August 13

Page 33: LogicObjects

18

A Prolog query browser based on

JPC

Monday 26 August 13

Page 34: LogicObjects

19

Monday 26 August 13

Page 35: LogicObjects

JPC as a library for implementing Java-Prolog frameworks

20

Prolog engines

Bridge libraries

JPC drivers

JPC library

Java-Prolog applications

Java-Prolog frameworks

(layer coupling denoted by the direction of the arrows)

Monday 26 August 13

Page 36: LogicObjects

JPC as a library for implementing Java-Prolog frameworks

20

Prolog engines

Bridge libraries

JPC drivers

JPC library

Java-Prolog applications

Java-Prolog frameworks

(layer coupling denoted by the direction of the arrows)

e.g. LogicObjects

Monday 26 August 13

Page 37: LogicObjects

LogicObjects

• A portable Java-Prolog linguistic symbiosis framework.

• Implemented on top of the JPC library.

• Provides obliviousness concerning integration (in common scenarios).

• Based on annotations for customising the integration and minimising programming effort.

21

Monday 26 August 13

Page 38: LogicObjects

Symbiosis

22

“The intimate living together of two dissimilar organisms in a mutually beneficial relationship.” (Merriam-Webster dictionary)

Monday 26 August 13

Page 39: LogicObjects

Linguistic symbiosis

• Objects from different worlds must understand each other.

• Invoking routines from another language as if they were defined in their own language.

23

• Easier to achieve if the languages belong to the same paradigm.

Monday 26 August 13

Page 40: LogicObjects

A paradigm leak

“The event of concepts leaking from one programming paradigm to another”

24

* Brichau, J. et al.Towards linguistic symbiosis of an object-oriented and a logic programming language.

In Proceedings of the Workshop on Multiparadigm Programming with Object-Oriented Languages. (2002)

*

Monday 26 August 13

Page 41: LogicObjects

The inhabitants of our two worlds

25

The OOworld

The logicworld

Monday 26 August 13

Page 42: LogicObjects

The inhabitants of our two worlds

25

ClassesObjects

MessagesMethods

Return valuesPackages The OO

worldThe logic

world

FactsRulesTerms

QueriesQuery solutions

Modules

Monday 26 August 13

Page 43: LogicObjects

Reducing the gap with Logtalk

26

The OOworld

The logicworld

Monday 26 August 13

Page 44: LogicObjects

Reducing the gap with Logtalk

26

The OOworld

The logicworld

LogtalkAn object-oriented layer

Monday 26 August 13

Page 45: LogicObjects

Case study

27

The London underground

from:

Monday 26 August 13

Page 46: LogicObjects

Relevant concepts

28

The London underground

Monday 26 August 13

Page 47: LogicObjects

Relevant concepts

28

The London underground

stations

linesmetro

Monday 26 August 13

Page 48: LogicObjects

A rule based system using Prolog

connected(station(bond_street), station(oxford_circus), line(central)). connected(station(oxford_circus), station(tottenham_court_road), line(central)). ...

nearby(S1,S2) :- connected(S1,S2,_). nearby(S1,S2) :- connected(S1,S3,L), connected(S3,S2,L).

reachable(S1,S2,[]) :- connected(S1,S2,_). reachable(S1,S2,[S3|Ss]) :- connected(S1,S3,_), reachable(S3,S2,Ss).

line(Name) :- setof(L, S1^S2^connected(S1,S2,L), Ls), list::member(line(Name), Ls).

29

Monday 26 August 13

Page 49: LogicObjects

A rule based system using Prolog

connected(station(bond_street), station(oxford_circus), line(central)). connected(station(oxford_circus), station(tottenham_court_road), line(central)). ...

nearby(S1,S2) :- connected(S1,S2,_). nearby(S1,S2) :- connected(S1,S3,L), connected(S3,S2,L).

reachable(S1,S2,[]) :- connected(S1,S2,_). reachable(S1,S2,[S3|Ss]) :- connected(S1,S3,_), reachable(S3,S2,Ss).

line(Name) :- setof(L, S1^S2^connected(S1,S2,L), Ls), list::member(line(Name), Ls).

29

FACTS

Monday 26 August 13

Page 50: LogicObjects

A rule based system using Prolog

connected(station(bond_street), station(oxford_circus), line(central)). connected(station(oxford_circus), station(tottenham_court_road), line(central)). ...

nearby(S1,S2) :- connected(S1,S2,_). nearby(S1,S2) :- connected(S1,S3,L), connected(S3,S2,L).

reachable(S1,S2,[]) :- connected(S1,S2,_). reachable(S1,S2,[S3|Ss]) :- connected(S1,S3,_), reachable(S3,S2,Ss).

line(Name) :- setof(L, S1^S2^connected(S1,S2,L), Ls), list::member(line(Name), Ls).

29

RULES

Monday 26 August 13

Page 51: LogicObjects

Adding an object-oriented layer with Logtalk

connected(station(bond_street), station(oxford_circus), line(central)). connected(station(oxford_circus), station(tottenham_court_road), line(central)). ...

nearby(S1,S2) :- connected(S1,S2,_). nearby(S1,S2) :- connected(S1,S3,L), connected(S3,S2,L).

reachable(S1,S2,[]) :- connected(S1,S2,_). reachable(S1,S2,[S3|Ss]) :- connected(S1,S3,_), reachable(S3,S2,Ss).

line(Name) :- setof(L, S1^S2^connected(S1,S2,L), Ls), list::member(line(Name), Ls).

30

:- object(metro).

:- end_object.

Monday 26 August 13

Page 52: LogicObjects

Adding an object-oriented layer with Logtalk

connected(station(bond_street), station(oxford_circus), line(central)). connected(station(oxford_circus), station(tottenham_court_road), line(central)). ...

nearby(S1,S2) :- connected(S1,S2,_). nearby(S1,S2) :- connected(S1,S3,L), connected(S3,S2,L).

reachable(S1,S2,[]) :- connected(S1,S2,_). reachable(S1,S2,[S3|Ss]) :- connected(S1,S3,_), reachable(S3,S2,Ss).

line(Name) :- setof(L, S1^S2^connected(S1,S2,L), Ls), list::member(line(Name), Ls).

30

:- object(metro).

:- end_object.

Monday 26 August 13

Page 53: LogicObjects

Adding an object-oriented layer with Logtalk

connected(station(bond_street), station(oxford_circus), line(central)). connected(station(oxford_circus), station(tottenham_court_road), line(central)). ...

nearby(S1,S2) :- connected(S1,S2,_). nearby(S1,S2) :- connected(S1,S3,L), connected(S3,S2,L).

reachable(S1,S2,[]) :- connected(S1,S2,_). reachable(S1,S2,[S3|Ss]) :- connected(S1,S3,_), reachable(S3,S2,Ss).

line(Name) :- setof(L, S1^S2^connected(S1,S2,L), Ls), list::member(line(Name), Ls).

30

:- object(metro).

:- end_object.

:- public([connected/3, nearby/2, reachable/3, line/1]). ACCESS MODIFIERS

Monday 26 August 13

Page 54: LogicObjects

Logtalk parametric objects

31

:- object(line(_Name)).

:- public([name/1, connects/2]). name(Name) :- parameter(1, Name). ...

:- end_object.

:- object(station(_Name)).

:- public([name/1, connected/2, nearby/1, reachable/2]). name(Name) :- parameter(1, Name). ...

:- end_object.

Monday 26 August 13

Page 55: LogicObjects

PARAMETRIC OBJECT

Logtalk parametric objects

31

:- object(line(_Name)).

:- public([name/1, connects/2]). name(Name) :- parameter(1, Name). ...

:- end_object.

:- object(station(_Name)).

:- public([name/1, connected/2, nearby/1, reachable/2]). name(Name) :- parameter(1, Name). ...

:- end_object.

Monday 26 August 13

Page 56: LogicObjects

PARAMETRIC OBJECT

PARAMETRIC OBJECT

Logtalk parametric objects

31

:- object(line(_Name)).

:- public([name/1, connects/2]). name(Name) :- parameter(1, Name). ...

:- end_object.

:- object(station(_Name)).

:- public([name/1, connected/2, nearby/1, reachable/2]). name(Name) :- parameter(1, Name). ...

:- end_object.

Monday 26 August 13

Page 57: LogicObjects

Invoking a Logtak method

Messages in Logtalk are expressed with the :: operator.

For example:

line(central)::connects(Station1, Station2)

Answers all the stations connected by the line ‘central’

32

Monday 26 August 13

Page 58: LogicObjects

Invoking a Logtak method

Messages in Logtalk are expressed with the :: operator.

For example:

line(central)::connects(Station1, Station2)

Answers all the stations connected by the line ‘central’

32

Monday 26 August 13

Page 59: LogicObjects

Meta-level artefacts from the two worlds

33

Monday 26 August 13

Page 60: LogicObjects

The Java world

34

public abstract class Line { String name; public Line(String name) {this.name = name;} public abstract boolean connects(Station s1, Station s2); public abstract int segments();}

public abstract class Station { ...}

public abstract class Metro { ...}

Monday 26 August 13

Page 61: LogicObjects

The Java world

34

public abstract class Line { String name; public Line(String name) {this.name = name;} public abstract boolean connects(Station s1, Station s2); public abstract int segments();}

public abstract class Station { ...}

public abstract class Metro { ...}

@LObject(args={“name”})

@LMethod(name={“connects”}, args={“_”, “_”})

@LObject(args={“name”})

Monday 26 August 13

Page 62: LogicObjects

Linguistic symbiosis challenges

• Translating objects to logic terms (and back).

•Mapping OO methods to logic queries.

•Dealing with unbound variables.

• Returning values from queries.

•Managing multiplicity.

35

* Some of them presented a bit differently in:D'Hondt, M. et al.Seamless integration of rule-based knowledge and object-oriented functionality with linguistic symbiosis. In Proceedings of the 2004 ACM symposium on Applied computing.(2004)

*

Monday 26 August 13

Page 63: LogicObjects

public abstract class Metro {...}

@LObject(name = "my_metro")public abstract class Metro {...}

@LObject(args = {"name"})public abstract class Line { private String name; ...}

metro

my_metro

line(lineName)

Translating objects to logic terms

36

Java Prolog

Monday 26 August 13

Page 64: LogicObjects

public abstract class Metro {...}

@LObject(name = "my_metro")public abstract class Metro {...}

@LObject(args = {"name"})public abstract class Line { private String name; ...}

metro

my_metro

line(lineName)

Translating objects to logic terms

36

Java Prolog

Monday 26 August 13

Page 65: LogicObjects

public abstract class Metro {...}

@LObject(name = "my_metro")public abstract class Metro {...}

@LObject(args = {"name"})public abstract class Line { private String name; ...}

metro

my_metro

line(lineName)

Translating objects to logic terms

36

Java Prolog

Monday 26 August 13

Page 66: LogicObjects

public abstract class Metro {...}

@LObject(name = "my_metro")public abstract class Metro {...}

@LObject(args = {"name"})public abstract class Line { private String name; ...}

metro

my_metro

line(lineName)

Translating objects to logic terms

36

Java Prolog

Monday 26 August 13

Page 67: LogicObjects

line(lName)::connects( station(s1Name), station(s2Name)).

line(lName)::connects(_, _).

Mapping Java methods to Logtalk methods

37

@LObject(args = {"name"})public abstract class Line { private String name;

public abstract boolean connects(Station s1, Station s2); @LMethod(name = "connects", args = {"_", "_"}) public abstract int segments();}

Java Prolog

Monday 26 August 13

Page 68: LogicObjects

line(lName)::connects( station(s1Name), station(s2Name)).

line(lName)::connects(_, _).

Mapping Java methods to Logtalk methods

37

@LObject(args = {"name"})public abstract class Line { private String name;

public abstract boolean connects(Station s1, Station s2); @LMethod(name = "connects", args = {"_", "_"}) public abstract int segments();}

Java Prolog

Monday 26 August 13

Page 69: LogicObjects

line(lName)::connects( station(s1Name), station(s2Name)).

line(lName)::connects(_, _).

Mapping Java methods to Logtalk methods

37

@LObject(args = {"name"})public abstract class Line { private String name;

public abstract boolean connects(Station s1, Station s2); @LMethod(name = "connects", args = {"_", "_"}) public abstract int segments();}

Java Prolog

Monday 26 August 13

Page 70: LogicObjects

line(lName)::connects( station(s1Name), station(s2Name)).

line(lName)::connects(_, _).

Mapping Java methods to Logtalk methods

37

@LObject(args = {"name"})public abstract class Line { private String name;

public abstract boolean connects(Station s1, Station s2); @LMethod(name = "connects", args = {"_", "_"}) public abstract int segments();}

Java Prolog

anonymous variables

Monday 26 August 13

Page 71: LogicObjects

38

Interpreting a query result as a Java object

The logic solutions

Varx1 x1, Vary1 y1Varx2 x2, Vary2 y2

Varxn xn, Varyn yn

Monday 26 August 13

Page 72: LogicObjects

38

Interpreting a query result as a Java object

The logic solutions

Varx1 x1, Vary1 y1Varx2 x2, Vary2 y2

Varxn xn, Varyn yn(set of frames binding logic

variables to terms)

frame 1frame 2

frame n

Monday 26 August 13

Page 73: LogicObjects

39

Interpreting a query result as a Java object

The logic solutions

Varx1 x1, Vary1 y1Varx2 x2, Vary2 y2

Varxn xn, Varyn yn(set of frames binding logic

variables to terms)

Monday 26 August 13

Page 74: LogicObjects

39

Interpreting a query result as a Java object

The logic solutions The method return value

Varx1 x1, Vary1 y1Varx2 x2, Vary2 y2

aJavaObject

Varxn xn, Varyn yn(set of frames binding logic

variables to terms)

Monday 26 August 13

Page 75: LogicObjects

40

The logic solutions

Varx1 x1, Vary1 y1Varx2 x2, Vary2 y2

Varxn xn, Varyn yn(set of frames binding logic

variables to terms)

Returning values from one solution

Monday 26 August 13

Page 76: LogicObjects

40

The logic solutions The method return value

Varx1 x1, Vary1 y1Varx2 x2, Vary2 y2

aJavaObject

Varxn xn, Varyn yn(set of frames binding logic

variables to terms)

Returning values from one solution

Monday 26 August 13

Page 77: LogicObjects

41

Returning values from one solution

The logic solutions The method return valueterm(Varx1, Vary1)

Varx1 x1, Vary1 y1Varx2 x2, Vary2 y2

Varxn xn, Varyn yn(set of frames binding logic

variables to terms)

Monday 26 August 13

Page 78: LogicObjects

41

Returning values from one solution

The logic solutions The method return valueterm(Varx1, Vary1)

Varx1 x1, Vary1 y1Varx2 x2, Vary2 y2

Varxn xn, Varyn yn(set of frames binding logic

variables to terms)

(specified in a methodwith @LSolution)

Monday 26 August 13

Page 79: LogicObjects

41

Returning values from one solution

The logic solutions The method return valueterm(Varx1, Vary1)

Varx1 x1, Vary1 y1 term(x1, y1)Varx2 x2, Vary2 y2 term(x2, y2)

Varxn xn, Varyn yn term(xn, yn)(set of frames binding logic

variables to terms)

(specified in a methodwith @LSolution)

(default solution)

Monday 26 August 13

Page 80: LogicObjects

42

Explicit specification of return values

@LObject(args = {"name"})public abstract class Station {

@LSolution("IStations") @LMethod(name = "reachable", args = {"$1", "IStations"}) public abstract List<Station> intermediateStations(Station station); ...

Monday 26 August 13

Page 81: LogicObjects

42

Explicit specification of return values

@LObject(args = {"name"})public abstract class Station {

@LSolution("IStations") @LMethod(name = "reachable", args = {"$1", "IStations"}) public abstract List<Station> intermediateStations(Station station); ...

Monday 26 August 13

Page 82: LogicObjects

• Preprocessing macros and Java exp.

• Convert arguments to terms.

• Convert method to a predicate.

• Convert receiver object to a term.

• Build a query.

• Determine solution heuristic.

• Convert solution terms back to objects.

43

• $1 => station (method parameter).

• (station(a), IStations)

• intermediateStations => reachable/2.

• station(b)

• station(b)::reachable(station(a), IStations)

• Binding of IStations (first solution).

• [station(x), station(y)] => List<Station>

Processing Steps Outcome

@LObject(args = {"name"})public abstract class Station {

@LSolution("IStations") @LMethod(name = "reachable", args = {"$1", "IStations"}) public abstract List<Station> intermediateStations(Station station); ...}

Monday 26 August 13

Page 83: LogicObjects

first Java method parameter

• Preprocessing macros and Java exp.

• Convert arguments to terms.

• Convert method to a predicate.

• Convert receiver object to a term.

• Build a query.

• Determine solution heuristic.

• Convert solution terms back to objects.

43

• $1 => station (method parameter).

• (station(a), IStations)

• intermediateStations => reachable/2.

• station(b)

• station(b)::reachable(station(a), IStations)

• Binding of IStations (first solution).

• [station(x), station(y)] => List<Station>

Processing Steps Outcome

@LObject(args = {"name"})public abstract class Station {

@LSolution("IStations") @LMethod(name = "reachable", args = {"$1", "IStations"}) public abstract List<Station> intermediateStations(Station station); ...}

Monday 26 August 13

Page 84: LogicObjects

first Java method parameter

• Preprocessing macros and Java exp.

• Convert arguments to terms.

• Convert method to a predicate.

• Convert receiver object to a term.

• Build a query.

• Determine solution heuristic.

• Convert solution terms back to objects.

43

• $1 => station (method parameter).

• (station(a), IStations)

• intermediateStations => reachable/2.

• station(b)

• station(b)::reachable(station(a), IStations)

• Binding of IStations (first solution).

• [station(x), station(y)] => List<Station>

Processing Steps Outcome

@LObject(args = {"name"})public abstract class Station {

@LSolution("IStations") @LMethod(name = "reachable", args = {"$1", "IStations"}) public abstract List<Station> intermediateStations(Station station); ...}

unbound Prolog variable

(as term)

Monday 26 August 13

Page 85: LogicObjects

• Preprocessing macros and Java exp.

• Convert arguments to terms.

• Convert method to a predicate.

• Convert receiver object to a term.

• Build a query.

• Determine solution heuristic.

• Convert solution terms back to objects.

43

• $1 => station (method parameter).

• (station(a), IStations)

• intermediateStations => reachable/2.

• station(b)

• station(b)::reachable(station(a), IStations)

• Binding of IStations (first solution).

• [station(x), station(y)] => List<Station>

Processing Steps Outcome

@LObject(args = {"name"})public abstract class Station {

@LSolution("IStations") @LMethod(name = "reachable", args = {"$1", "IStations"}) public abstract List<Station> intermediateStations(Station station); ...}

Monday 26 August 13

Page 86: LogicObjects

• Preprocessing macros and Java exp.

• Convert arguments to terms.

• Convert method to a predicate.

• Convert receiver object to a term.

• Build a query.

• Determine solution heuristic.

• Convert solution terms back to objects.

43

• $1 => station (method parameter).

• (station(a), IStations)

• intermediateStations => reachable/2.

• station(b)

• station(b)::reachable(station(a), IStations)

• Binding of IStations (first solution).

• [station(x), station(y)] => List<Station>

Processing Steps Outcome

@LObject(args = {"name"})public abstract class Station {

@LSolution("IStations") @LMethod(name = "reachable", args = {"$1", "IStations"}) public abstract List<Station> intermediateStations(Station station); ...}

(instance of)

Monday 26 August 13

Page 87: LogicObjects

station(b)::reachable(station(a), IStations)

• Preprocessing macros and Java exp.

• Convert arguments to terms.

• Convert method to a predicate.

• Convert receiver object to a term.

• Build a query.

• Determine solution heuristic.

• Convert solution terms back to objects.

43

• $1 => station (method parameter).

• (station(a), IStations)

• intermediateStations => reachable/2.

• station(b)

• station(b)::reachable(station(a), IStations)

• Binding of IStations (first solution).

• [station(x), station(y)] => List<Station>

Processing Steps Outcome

@LObject(args = {"name"})public abstract class Station {

@LSolution("IStations") @LMethod(name = "reachable", args = {"$1", "IStations"}) public abstract List<Station> intermediateStations(Station station); ...}

Monday 26 August 13

Page 88: LogicObjects

station(b)::reachable(station(a), IStations)

• Preprocessing macros and Java exp.

• Convert arguments to terms.

• Convert method to a predicate.

• Convert receiver object to a term.

• Build a query.

• Determine solution heuristic.

• Convert solution terms back to objects.

43

• $1 => station (method parameter).

• (station(a), IStations)

• intermediateStations => reachable/2.

• station(b)

• station(b)::reachable(station(a), IStations)

• Binding of IStations (first solution).

• [station(x), station(y)] => List<Station>

Processing Steps Outcome

@LObject(args = {"name"})public abstract class Station {

@LSolution("IStations") @LMethod(name = "reachable", args = {"$1", "IStations"}) public abstract List<Station> intermediateStations(Station station); ...}

Monday 26 August 13

Page 89: LogicObjects

station(b)::reachable(station(a), IStations)

• Preprocessing macros and Java exp.

• Convert arguments to terms.

• Convert method to a predicate.

• Convert receiver object to a term.

• Build a query.

• Determine solution heuristic.

• Convert solution terms back to objects.

43

• $1 => station (method parameter).

• (station(a), IStations)

• intermediateStations => reachable/2.

• station(b)

• station(b)::reachable(station(a), IStations)

• Binding of IStations (first solution).

• [station(x), station(y)] => List<Station>

Processing Steps Outcome

@LObject(args = {"name"})public abstract class Station {

@LSolution("IStations") @LMethod(name = "reachable", args = {"$1", "IStations"}) public abstract List<Station> intermediateStations(Station station); ...}

Monday 26 August 13

Page 90: LogicObjects

44

Returning multiple values from queries

The logic solutions The method return valueterm(Varx1, Vary1)

Varx1 x1, Vary1 y1 term(x1, y1)Varx2 x2, Vary2 y2 term(x2, y2)

Varxn xn, Varyn yn term(xn, yn)

Monday 26 August 13

Page 91: LogicObjects

44

Returning multiple values from queries

The logic solutions The method return valueterm(Varx1, Vary1)

Varx1 x1, Vary1 y1 term(x1, y1)Varx2 x2, Vary2 y2 term(x2, y2)

Varxn xn, Varyn yn term(xn, yn)

a composed solution (specified in a methodwith @LComposition)

Monday 26 August 13

Page 92: LogicObjects

45

The logic solutions The method return value(a property of the result set)

Varx1 x1, Vary1 y1Varx2 x2, Vary2 y2

Varxn xn, Varyn yn

Returning a property of the result set

}

Monday 26 August 13

Page 93: LogicObjects

45

The logic solutions The method return value(a property of the result set)

Varx1 x1, Vary1 y1Varx2 x2, Vary2 y2

Varxn xn, Varyn yn

is there a solution ?

Returning a property of the result set

how many solutions ?}

Monday 26 August 13

Page 94: LogicObjects

Returning a property of the result set

46

@LObject(args = {"name"})public abstract class Line { private String name;

public abstract boolean connects(Station s1, Station s2); @LMethod(name = "connects", args = {"_", "_"}) public abstract int segments();}

Monday 26 August 13

Page 95: LogicObjects

Returning a property of the result set

46

@LObject(args = {"name"})public abstract class Line { private String name;

public abstract boolean connects(Station s1, Station s2); @LMethod(name = "connects", args = {"_", "_"}) public abstract int segments();}

should return if logic method succeeds or not

Monday 26 August 13

Page 96: LogicObjects

Returning a property of the result set

46

@LObject(args = {"name"})public abstract class Line { private String name;

public abstract boolean connects(Station s1, Station s2); @LMethod(name = "connects", args = {"_", "_"}) public abstract int segments();}

should return if logic method succeeds or not

should return the number of solutions

Monday 26 August 13

Page 97: LogicObjects

Instantiating a symbiotic object in Java

Line line = newLogicObject(Line.class, “central”);System.out.println("Number of segments of line " + line + ": " + line.segments());

47

Monday 26 August 13

Page 98: LogicObjects

Instantiating a symbiotic object in Java

Line line = newLogicObject(Line.class, “central”);System.out.println("Number of segments of line " + line + ": " + line.segments());

47

Monday 26 August 13

Page 99: LogicObjects

Instantiating a symbiotic object in Java

Line line = newLogicObject(Line.class, “central”);System.out.println("Number of segments of line " + line + ": " + line.segments());

47

Monday 26 August 13

Page 100: LogicObjects

Other features

• Java expressions embedded in logic terms (symbiosis terms).

•Dependency management support.

• Integration with plain Prolog (without Logtalk).

•Not constrained to a specific execution environment (e.g. an Eclipse plugin).

48

Monday 26 August 13

Page 101: LogicObjects

Inspiration from other domains

• Common interoperability layer : JDBC.

•Mapping artefacts using annotations: JAXB.

• Context dependent conversions: GSON.

• Linguistic symbiosis concepts: SOUL.

49

1http://www.oracle.com/technetwork/java/javase/jdbc/2https://jaxb.java.net/3http://code.google.com/p/google-gson/4http://soft.vub.ac.be/SOUL/

1

2

3

4

Monday 26 August 13

Page 102: LogicObjects

implemented

pending

if time allows it

• Integration direction• OO to Prolog• Prolog to OO

• Linguistic symbiosis• Automation• Transparency• Customisation

• Preserving causality• Inter-language reflection

• Code querying• Code transformation• Code generation• Symbiotic inter-language reflection

• Portability

Research status

50

Monday 26 August 13

Page 103: LogicObjects

Future work

• Finishing a full bidirectional linguistic symbiosis framework.

• Exploring the feasibility of our approach for symbiotic inter-language reflection.

• Adding support for more Prolog engines (e.g. an embedded one).

• Continue the development of reusable hybrid components.

51

Monday 26 August 13

Page 104: LogicObjects

Conclusions

•We are actively exploring how far we can get in automation/transparency regarding Java-Prolog linguistic integration.

•We are attempting to provide reusable hybrid components and frameworks that may be helpful to the community.

• And we are trying to do this in a portable way.

52

Monday 26 August 13