Towards a Software Ecosystem
for Java-Prolog Interoperability
Sergio Castro Kim Mens
RELEASeD LAB
Université catholique de...
2
LogicObjects
Future work/
Conclusions
Content
JPC
Hydra
(or how to build reusable hybrid components)
Tuesday 9 July 13
2
LogicObjects
Future work/
Conclusions
Content
JPC
Hydra
(or how to build reusable hybrid components)
Tuesday 9 July 13
2
LogicObjects
Future work/
Conclusions
Content
JPC
Hydra
(or how to build reusable hybrid components)
Tuesday 9 July 13
2
LogicObjects
Future work/
Conclusions
Content
JPC
Hydra
(or how to build reusable hybrid components)
Tuesday 9 July 13
3
OO
programming
Logic
programming
Appropriate for modelling
real-world concepts
Often enjoy rich software
ecosystems
Decl...
3
OO
programming
Logic
programming
Appropriate for modelling
real-world concepts
Often enjoy rich software
ecosystems
Decl...
3
OO
programming
Logic
programming
Appropriate for modelling
real-world concepts
Often enjoy rich software
ecosystems
Decl...
3
OO
programming
Logic
programming
Appropriate for modelling
real-world concepts
Often enjoy rich software
ecosystems
Decl...
Integration difficult to achieve
• The Prolog community is fragmented.
• Existing Java-Prolog libraries also suffer the sym...
An ecosystem for Java-Prolog
development
• Providing a portable general-purpose library for building Java-
Prolog systems ...
Why Java ?
• None of the authors is a big fan of the Java language itself.
• But we recognise the advantages of the huge J...
• Facilitates the creation of hybrid Java-Prolog applications and
frameworks.
• Not constrained to a specific execution env...
Lessons learned when building
libraries without JPC
• Applications are strongly coupled to a concrete Java-Prolog
library....
JPC features
• A portable abstraction of a Prolog virtual machine.
• A set of utilities for dealing with Java-Prolog inter...
JPC as a portable layer
10
Prolog engines
Bridge libraries
JPC drivers
JPC library
Java-Prolog
applications
Java-Prolog
fr...
11
A Prolog query
browser based on
JPC
Tuesday 9 July 13
12
Tuesday 9 July 13
JPC as a tool for inter-language
conversions
• JPC implements a “new” mechanism for passing artefacts
between Java and Pro...
Common integration techniques
• Shared memory approach (e.g. SOUL).
• Passing object references to Prolog (e.g. JPL).
• Se...
Shared memory approach
• Tight integration/control between Java and Prolog.
• Difficult to implement an efficient Prolog emb...
Passing object references
• The original object reference is preserved.
• Garbage collection may be an issue.
• No control...
Serializing objects
• Object reference is not preserved.
• No configuration required.
• Only works with serializable object...
Mapping objects to terms
• Object reference is difficult to preserve.
• Difficult to support circular relations.
• Requires ...
JPC mapping features
• Helps to modularize context dependent conversions.
• Converters can receive hints on the expected c...
JPC architecture
• Abstraction of a PrologVM.
• Reification of Prolog data types (e.g.Atom, Compound, etc).
• The conversio...
Conversion context components
• The converter manager.
• The type solver.
• The instantiation manager.
21
Tuesday 9 July 13
The converter manager
• Converts between Java-Prolog artefacts.
• Composition of specialized converters (primitive types
c...
public class CollectionConverter<E> extends JpcConverter<Collection<E>, Term> {
@Override
public Collection<E> fromTerm(Te...
public class CollectionConverter<E> extends JpcConverter<Collection<E>, Term> {
@Override
public Collection<E> fromTerm(Te...
public class CollectionConverter<E> extends JpcConverter<Collection<E>, Term> {
@Override
public Collection<E> fromTerm(Te...
public class CollectionConverter<E> extends JpcConverter<Collection<E>, Term> {
@Override
public Collection<E> fromTerm(Te...
public class CollectionConverter<E> extends JpcConverter<Collection<E>, Term> {
@Override
public Collection<E> fromTerm(Te...
public class CollectionConverter<E> extends JpcConverter<Collection<E>, Term> {
@Override
public Collection<E> fromTerm(Te...
A default conversion example
Jpc jpc = ... //the conversion context
Term listTerm = listTerm(new Atom("1"), new Atom("2"))...
A default conversion example
Jpc jpc = ... //the conversion context
Term listTerm = listTerm(new Atom("1"), new Atom("2"))...
A default conversion example
Jpc jpc = ... //the conversion context
Term listTerm = listTerm(new Atom("1"), new Atom("2"))...
A typed conversion example
Term listTerm = listTerm(new Atom("1"), new Atom("2"));
Type type = new TypeToken<ArrayList<Int...
A typed conversion example
Term listTerm = listTerm(new Atom("1"), new Atom("2"));
Type type = new TypeToken<ArrayList<Int...
A custom converter
public class StationConverter extends JpcConverter<Station, Compound> {
	 public static final String ST...
The type solver
• Attempts to infer the best Java type of a Prolog term if no
hint is available.
• Question:What does this...
The instantiation manager
• A customizable mechanism for instantiating abstract classes/
interfaces if required.
• E.g. a ...
LogicObjects
• A portable Java-Prolog linguistic symbiosis framework.
• Currently being migrated to JPC.
• Based on annota...
Symbiosis
30
“The intimate living together of two dissimilar organisms in a
mutually beneficial relationship.” (Merriam-We...
Linguistic symbiosis
• Objects from different worlds must understand each other.
• Invoking routines from another language...
A paradigm leak
“The event of concepts leaking from one programming paradigm
to another”
32
* Gybels, K.
SOUL and Smalltal...
The inhabitants of our two
worlds
33
The OO
world
The logic
world
Tuesday 9 July 13
The inhabitants of our two
worlds
33
Packages
Classes
Objects
Methods
Method invocations
Return values
The OO
world
The lo...
Reducing the gap with Logtalk
34
The OO
world
The logic
world
Tuesday 9 July 13
Reducing the gap with Logtalk
34
The OO
world
The logic
world
Logtalk
An object-oriented layer
Tuesday 9 July 13
Case Study
35
The London underground
from:
Tuesday 9 July 13
Relevant concepts
36
The London underground
Tuesday 9 July 13
Relevant concepts
36
The London underground
stations
Tuesday 9 July 13
Relevant concepts
36
The London underground
stations
lines
Tuesday 9 July 13
Relevant concepts
36
The London underground
stations
linesmetro
Tuesday 9 July 13
Interesting relations
37
line1A C
line2
D
F
B
Tuesday 9 July 13
Interesting relations
37
line1A C
line2
D
F
B
Connected: Directly connected
(e.g., A with B).
connected
Tuesday 9 July 13
Interesting relations
37
line1A C
line2
D
F
B
Connected: Directly connected
(e.g., A with B).
Nearby: At most one intermed...
Interesting relations
37
line1A C
line2
D
F
B
Connected: Directly connected
(e.g., A with B).
Nearby: At most one intermed...
Interesting relations
38
Connected: Directly connected
(e.g., A with B).
Nearby: At most one intermediate
station, in the ...
Which is the best language for
this problem?
• A logic language would let us express our problem using facts
and rules.
• ...
Implementation Strategy
• Develop the program in Prolog.
• Wrap it with an OO layer (Logtalk).
• Program the Java side usi...
A rule based system using Prolog
connected(station(bond_street), station(oxford_circus), line(central)).
connected(station...
A rule based system using Prolog
connected(station(bond_street), station(oxford_circus), line(central)).
connected(station...
A rule based system using Prolog
connected(station(bond_street), station(oxford_circus), line(central)).
connected(station...
Adding an object-oriented layer
with Logtalk
connected(station(bond_street), station(oxford_circus), line(central)).
conne...
Adding an object-oriented layer
with Logtalk
connected(station(bond_street), station(oxford_circus), line(central)).
conne...
Adding an object-oriented layer
with Logtalk
connected(station(bond_street), station(oxford_circus), line(central)).
conne...
Logtalk parametric objects
43
:- object(line(_Name)).
:- public([name/1, connects/2]).
name(Name) :- parameter(1, Name).
....
Logtalk parametric objects
43
:- object(line(_Name)).
:- public([name/1, connects/2]).
name(Name) :- parameter(1, Name).
....
Logtalk parametric objects
43
:- object(line(_Name)).
:- public([name/1, connects/2]).
name(Name) :- parameter(1, Name).
....
Invoking a Logtak method
Messages in Logtalk are expressed with the :: operator.
For example:
line(central)::connects(Stat...
Invoking a Logtak method
Messages in Logtalk are expressed with the :: operator.
For example:
line(central)::connects(Stat...
The Java world
45
public abstract class Line {
	 String name;
public Line(String name) {this.name = name;}
	
	 public abst...
The Java world
45
public abstract class Line {
	 String name;
public Line(String name) {this.name = name;}
	
	 public abst...
Mapping objects from the two
worlds
46
Tuesday 9 July 13
Linguistic symbiosis challenges
• Translating objects to logic terms (and back).
• Mapping OO methods to logic queries.
• ...
48
Translating objects to logic terms
Tuesday 9 July 13
48
Translating objects to logic terms
Tuesday 9 July 13
48
Translating objects to logic terms
Tuesday 9 July 13
public abstract class Metro {...}
@LObject(name = "my_metro")
public abstract class Metro {...}
@LObject(args = {"name"})
...
public abstract class Metro {...}
@LObject(name = "my_metro")
public abstract class Metro {...}
@LObject(args = {"name"})
...
public abstract class Metro {...}
@LObject(name = "my_metro")
public abstract class Metro {...}
@LObject(args = {"name"})
...
public abstract class Metro {...}
@LObject(name = "my_metro")
public abstract class Metro {...}
@LObject(args = {"name"})
...
Mapping Java methods to Logtalk
methods
50
@LObject(args = {"name"})
public abstract class Line {
private String name;
pub...
Mapping Java methods to Logtalk
methods
50
@LObject(args = {"name"})
public abstract class Line {
private String name;
pub...
Mapping Java methods to Logtalk
methods
50
@LObject(args = {"name"})
public abstract class Line {
private String name;
pub...
Mapping Java methods to Logtalk
methods
50
@LObject(args = {"name"})
public abstract class Line {
private String name;
pub...
@LObject(args = {"name"})
public abstract class Station {
@LSolution("IntermediateStations")
@LMethod(name = "reachable", ...
@LObject(args = {"name"})
public abstract class Station {
@LSolution("IntermediateStations")
@LMethod(name = "reachable", ...
@LObject(args = {"name"})
public abstract class Station {
@LSolution("IntermediateStations")
@LMethod(name = "reachable", ...
@LObject(args = {"name"})
public abstract class Station {
@LSolution("IntermediateStations")
@LMethod(name = "reachable", ...
52
Interpreting a query result as a
Java object
The logic solutions
Varx1 x1, Vary1 y1
Varx2 x2, Vary2 y2
Varxn xn, Varyn ...
52
Interpreting a query result as a
Java object
The logic solutions
Varx1 x1, Vary1 y1
Varx2 x2, Vary2 y2
Varxn xn, Varyn ...
53
Interpreting a query result as a
Java object
The logic solutions
Varx1 x1, Vary1 y1
Varx2 x2, Vary2 y2
Varxn xn, Varyn ...
53
Interpreting a query result as a
Java object
The logic solutions The method return value
Varx1 x1, Vary1 y1
Varx2 x2, V...
54
Returning values from one
solution
The logic solutions The method return value
term(Varx1, Vary1)
Varx1 x1, Vary1 y1 te...
54
Returning values from one
solution
The logic solutions The method return value
term(Varx1, Vary1)
Varx1 x1, Vary1 y1 te...
54
Returning values from one
solution
The logic solutions The method return value
term(Varx1, Vary1)
Varx1 x1, Vary1 y1 te...
@LObject(args = {"name"})
public abstract class Station {
@LSolution("IntermediateStations")
@LMethod(name = "reachable", ...
@LObject(args = {"name"})
public abstract class Station {
@LSolution("IntermediateStations")
@LMethod(name = "reachable", ...
@LObject(args = {"name"})
public abstract class Station {
@LSolution("IntermediateStations")
@LMethod(name = "reachable", ...
@LObject(args = {"name"})
public abstract class Station {
@LMethod(name = "reachable", args = {"$1", "LSolution"})
public ...
@LObject(args = {"name"})
public abstract class Station {
@LMethod(name = "reachable", args = {"$1", "LSolution"})
public ...
Managing multiplicity
57
• A logic routine can have many solutions, in Java only one.
• Multiple values can be grouped wit...
58
Returning multiple values from
queries
The logic solutions The method return value
term(Varx1, Vary1)
Varx1 x1, Vary1 y...
58
Returning multiple values from
queries
The logic solutions The method return value
term(Varx1, Vary1)
Varx1 x1, Vary1 y...
@LObject(args = {"name"})
public abstract class Station {
...
@LComposition @LSolution("S")
@LMethod(args = {"S"})
public ...
@LObject(args = {"name"})
public abstract class Station {
...
@LComposition @LSolution("S")
@LMethod(args = {"S"})
public ...
@LObject(args = {"name"})
public abstract class Station {
...
@LComposition @LSolution("S")
@LMethod(args = {"S"})
public ...
@LObject(args = {"name"})
public abstract class Station {
...
@LComposition @LSolution("S")
@LMethod(args = {"S"})
public ...
@LObject(args = {"name"})
public abstract class Station {
...
@LComposition @LSolution("S")
@LMethod(args = {"S"})
public ...
Returning a property of the result
set
60
@LObject(args = {"name"})
public abstract class Line {
private String name;
	
pu...
Returning a property of the result
set
60
@LObject(args = {"name"})
public abstract class Line {
private String name;
	
pu...
Returning a property of the result
set
60
@LObject(args = {"name"})
public abstract class Line {
private String name;
	
pu...
Instantiating a symbiotic object in
Java
Line line = newLogicObject(Line.class, “central”);
System.out.println("Number of ...
Instantiating a symbiotic object in
Java
Line line = newLogicObject(Line.class, “central”);
System.out.println("Number of ...
Instantiating a symbiotic object in
Java
Line line = newLogicObject(Line.class, “central”);
System.out.println("Number of ...
Other features
• Java expressions embedded in logic terms (symbiosis terms).
• Dependency management.
• Integration with p...
Future work
• Finishing a full two-ways linguistic symbiosis framework.
• Supporting more Prolog engines.
• Adding support...
Inspiration from other domains
• Interoperability layer: JDBC.
• Mapping of artefacts using annotations: JAXB.
• Context d...
Conclusions
• We have provided portable and simple solutions for many
issues concerning Java-Prolog interoperability.
• We...
Thanks
66
Tuesday 9 July 13
Upcoming SlideShare
Loading in …5
×

Towards a software ecosystem for java prolog interoperabilty

829 views

Published on

At SATTOSE2013, Sergio Castro presented his work in progress on developing a software ecosystem for Java-Prolog interoperabilty.

Abstract :
The Prolog community, although continuously accomplishing relevant research results on different aspects of Software Engineering, is fragmented by the lack of a common rich ecosystem comparable to the ones surrounding languages belonging to, for example, the object-oriented programming paradigm. This hinders the agile development of new software and often forces practitioners to reimplement existing artifacts, to accommodate them to the requirements of their specific incompatible environments. Some libraries exist that alleviate this problem by providing bridges to object-oriented languages, such as Java, which are surrounded by rich ecosystems. As such, the logic programming community can access and reuse well proven existing frameworks and libraries from these ecosystems. Unfortunately, these bridge libraries themselves suffer from the symptoms of a fragmented community: they provide user and programming interfaces that cannot easily be reused with other libraries that are targeting essentially the same problem. To solve this problem, we have developed a library that decouples the high level programming interface supporting common Java–Prolog operations, from a provider–dependent library interacting with a Prolog engine. A similar approach has been successfully used in the past in the domain of Java-databases interaction (e.g., JDBC). Our library, in addition to proposing a common high level API, provides a simple and modular mechanism for expressing mappings between Prolog and Java artifacts. In addition, we implement a set of reusable components that can be reused independently of the underlying Prolog engine and bridge library.
We illustrate our approach by means of an application allowing a user to visually query geographical data by means of Prolog queries, that can easily change its Prolog engine and bridge library at runtime. We demonstrate how the client code in our example is completely decoupled from any bridge library serving as a port between the two worlds.

Published in: Technology, Education
1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total views
829
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
16
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Towards a software ecosystem for java prolog interoperabilty

  1. 1. Towards a Software Ecosystem for Java-Prolog Interoperability Sergio Castro Kim Mens RELEASeD LAB Université catholique de Louvain Belgium Paulo Moura Center for Research in Advanced Computing Systems INESC TEC Portugal 1 Tuesday 9 July 13
  2. 2. 2 LogicObjects Future work/ Conclusions Content JPC Hydra (or how to build reusable hybrid components) Tuesday 9 July 13
  3. 3. 2 LogicObjects Future work/ Conclusions Content JPC Hydra (or how to build reusable hybrid components) Tuesday 9 July 13
  4. 4. 2 LogicObjects Future work/ Conclusions Content JPC Hydra (or how to build reusable hybrid components) Tuesday 9 July 13
  5. 5. 2 LogicObjects Future work/ Conclusions Content JPC Hydra (or how to build reusable hybrid components) Tuesday 9 July 13
  6. 6. 3 OO programming Logic programming Appropriate for modelling real-world concepts Often enjoy rich software ecosystems Declarative reasoning Tuesday 9 July 13
  7. 7. 3 OO programming Logic programming Appropriate for modelling real-world concepts Often enjoy rich software ecosystems Declarative reasoning Tuesday 9 July 13
  8. 8. 3 OO programming Logic programming Appropriate for modelling real-world concepts Often enjoy rich software ecosystems Declarative reasoning Tuesday 9 July 13
  9. 9. 3 OO programming Logic programming Appropriate for modelling real-world concepts Often enjoy rich software ecosystems Declarative reasoning Tuesday 9 July 13
  10. 10. Integration difficult to achieve • The Prolog community is fragmented. • Existing Java-Prolog libraries also suffer the symptoms of such fragmentation. 4 Tuesday 9 July 13
  11. 11. An ecosystem for Java-Prolog development • Providing a portable general-purpose library for building Java- Prolog systems (JPC). • Providing a framework for linguistic symbiosis between Java & Prolog (LogicObjects). • Providing a set of reusable hybrid components. 5 Tuesday 9 July 13
  12. 12. Why Java ? • None of the authors is a big fan of the Java language itself. • But we recognise the advantages of the huge Java ecosystem. • And the emerging languages running on the JVM • (e.g., Scala, Clojure, JRuby, etc...). 6 Tuesday 9 July 13
  13. 13. • Facilitates the creation of hybrid Java-Prolog applications and frameworks. • Not constrained to a specific execution environment (e.g. an Eclipse plugin). • Compatible with some of the most 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. 7 1https://github.com/sergio-castro/ JPC: Java-Prolog Connectivity Tuesday 9 July 13
  14. 14. Lessons learned when building libraries without JPC • Applications are strongly coupled to a concrete Java-Prolog library. • Conversion concerns tangled with other concerns. • Complex routines dealing with conversion heuristics (high cyclomatic complexity). • Ugly ad-hoc implementation of context-dependent conversions. 8 Tuesday 9 July 13
  15. 15. JPC features • A portable abstraction of a Prolog virtual machine. • A set of utilities for dealing with Java-Prolog inter-language conversions. 9 Tuesday 9 July 13
  16. 16. JPC as a portable layer 10 Prolog engines Bridge libraries JPC drivers JPC library Java-Prolog applications Java-Prolog frameworks (layer coupling denoted by the direction of the arrows) Tuesday 9 July 13
  17. 17. 11 A Prolog query browser based on JPC Tuesday 9 July 13
  18. 18. 12 Tuesday 9 July 13
  19. 19. JPC as a tool for inter-language conversions • JPC implements a “new” mechanism for passing artefacts between Java and Prolog. • Mechanism based on the specification of mappings of such artefacts. 13 Tuesday 9 July 13
  20. 20. Common integration techniques • Shared memory approach (e.g. SOUL). • Passing object references to Prolog (e.g. JPL). • Serializing objects (e.g. InterProlog). • Mapping objects to terms (e.g. JPC, LogicObjects). 14 Tuesday 9 July 13
  21. 21. Shared memory approach • Tight integration/control between Java and Prolog. • Difficult to implement an efficient Prolog embedded in Java. • Often no access to well-proven Prolog libraries. • But easy access to libraries in the host language. 15 Tuesday 9 July 13
  22. 22. Passing object references • The original object reference is preserved. • Garbage collection may be an issue. • No control on the term representation of an object on the Prolog side. 16 Tuesday 9 July 13
  23. 23. Serializing objects • Object reference is not preserved. • No configuration required. • Only works with serializable objects. • Support for circular relations. • No control on the term representation of an object on the Prolog side. 17 Tuesday 9 July 13
  24. 24. Mapping objects to terms • Object reference is difficult to preserve. • Difficult to support circular relations. • Requires explicit mappings (where they cannot be inferred). • Fine control on the (context dependent) term representation of an object. 18 Tuesday 9 July 13
  25. 25. JPC mapping features • Helps to modularize context dependent conversions. • Converters can receive hints on the expected conversion to apply. • Custom conversions can be added at any moment. • Catalog of useful Prolog-Java converters. 19 Tuesday 9 July 13
  26. 26. JPC architecture • Abstraction of a PrologVM. • Reification of Prolog data types (e.g.Atom, Compound, etc). • The conversion context. 20 Tuesday 9 July 13
  27. 27. Conversion context components • The converter manager. • The type solver. • The instantiation manager. 21 Tuesday 9 July 13
  28. 28. The converter manager • Converts between Java-Prolog artefacts. • Composition of specialized converters (primitive types converters, exception converters, multi-valued converters, etc). • Interprets and refines hints from the user. 22 Tuesday 9 July 13
  29. 29. public class CollectionConverter<E> extends JpcConverter<Collection<E>, Term> { @Override public Collection<E> fromTerm(Term listTerm, Type type, Jpc context) { ... } ... } 23 A pre-defined converter example Tuesday 9 July 13
  30. 30. public class CollectionConverter<E> extends JpcConverter<Collection<E>, Term> { @Override public Collection<E> fromTerm(Term listTerm, Type type, Jpc context) { ... } ... } 23 A pre-defined converter example Java type Tuesday 9 July 13
  31. 31. public class CollectionConverter<E> extends JpcConverter<Collection<E>, Term> { @Override public Collection<E> fromTerm(Term listTerm, Type type, Jpc context) { ... } ... } 23 A pre-defined converter example Prolog type Tuesday 9 July 13
  32. 32. public class CollectionConverter<E> extends JpcConverter<Collection<E>, Term> { @Override public Collection<E> fromTerm(Term listTerm, Type type, Jpc context) { ... } ... } 23 A pre-defined converter example term to convert Tuesday 9 July 13
  33. 33. public class CollectionConverter<E> extends JpcConverter<Collection<E>, Term> { @Override public Collection<E> fromTerm(Term listTerm, Type type, Jpc context) { ... } ... } 23 A pre-defined converter example type guiding conversion Tuesday 9 July 13
  34. 34. public class CollectionConverter<E> extends JpcConverter<Collection<E>, Term> { @Override public Collection<E> fromTerm(Term listTerm, Type type, Jpc context) { ... } ... } 23 A pre-defined converter example the conversion context Tuesday 9 July 13
  35. 35. A default conversion example Jpc jpc = ... //the conversion context Term listTerm = listTerm(new Atom("1"), new Atom("2")); //[‘1’, ‘2’] List<String> list = jpc.fromTerm(listTerm); assertEquals("1", list.get(0)); assertEquals("2", list.get(1)); 24 Tuesday 9 July 13
  36. 36. A default conversion example Jpc jpc = ... //the conversion context Term listTerm = listTerm(new Atom("1"), new Atom("2")); //[‘1’, ‘2’] List<String> list = jpc.fromTerm(listTerm); assertEquals("1", list.get(0)); assertEquals("2", list.get(1)); 24 Prolog list of atoms Tuesday 9 July 13
  37. 37. A default conversion example Jpc jpc = ... //the conversion context Term listTerm = listTerm(new Atom("1"), new Atom("2")); //[‘1’, ‘2’] List<String> list = jpc.fromTerm(listTerm); assertEquals("1", list.get(0)); assertEquals("2", list.get(1)); 24 Java list of Strings Tuesday 9 July 13
  38. 38. A typed conversion example Term listTerm = listTerm(new Atom("1"), new Atom("2")); Type type = new TypeToken<ArrayList<Integer>>(){}.getType(); List<Integer> list = jpc.fromTerm(listTerm, type); assertEquals(1, list.get(0)); assertEquals(2, list.get(1)); 25 Tuesday 9 July 13
  39. 39. A typed conversion example Term listTerm = listTerm(new Atom("1"), new Atom("2")); Type type = new TypeToken<ArrayList<Integer>>(){}.getType(); List<Integer> list = jpc.fromTerm(listTerm, type); assertEquals(1, list.get(0)); assertEquals(2, list.get(1)); 25 type guiding conversion Tuesday 9 July 13
  40. 40. A custom converter 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); } } 26 Tuesday 9 July 13
  41. 41. The type solver • Attempts to infer the best Java type of a Prolog term if no hint is available. • Question:What does this term look like ? [A-x,B-y,C-z] • Answer: It may be a map. 27 Tuesday 9 July 13
  42. 42. The instantiation manager • A customizable mechanism for instantiating abstract classes/ interfaces if required. • E.g. a Prolog list term may be mapped to a Java List or a Map. • This manager knows which instance of List and Map to use (if the given hint does not contain such information). 28 Tuesday 9 July 13
  43. 43. LogicObjects • A portable Java-Prolog linguistic symbiosis framework. • Currently being migrated to JPC. • Based on annotations for specifying mappings between Java- Prolog artefacts. • Support for context dependent mappings. 29 Tuesday 9 July 13
  44. 44. Symbiosis 30 “The intimate living together of two dissimilar organisms in a mutually beneficial relationship.” (Merriam-Webster dictionary) Tuesday 9 July 13
  45. 45. Linguistic symbiosis • Objects from different worlds must understand each other. • Invoking routines from another language as if they were defined in their own language. 31 • Easier to achieve if the languages belong to the same paradigm. Tuesday 9 July 13
  46. 46. A paradigm leak “The event of concepts leaking from one programming paradigm to another” 32 * Gybels, K. SOUL and Smalltalk - Just Married: Evolution of the Interaction Between a Logic and an Object-Oriented Language Towards Symbiosis. In Proceedings of the Workshop on Declarative Programming in the Context of Object-Oriented Languages. (2003) * Tuesday 9 July 13
  47. 47. The inhabitants of our two worlds 33 The OO world The logic world Tuesday 9 July 13
  48. 48. The inhabitants of our two worlds 33 Packages Classes Objects Methods Method invocations Return values The OO world The logic world Libraries Modules Terms Clauses Queries Query solutions Tuesday 9 July 13
  49. 49. Reducing the gap with Logtalk 34 The OO world The logic world Tuesday 9 July 13
  50. 50. Reducing the gap with Logtalk 34 The OO world The logic world Logtalk An object-oriented layer Tuesday 9 July 13
  51. 51. Case Study 35 The London underground from: Tuesday 9 July 13
  52. 52. Relevant concepts 36 The London underground Tuesday 9 July 13
  53. 53. Relevant concepts 36 The London underground stations Tuesday 9 July 13
  54. 54. Relevant concepts 36 The London underground stations lines Tuesday 9 July 13
  55. 55. Relevant concepts 36 The London underground stations linesmetro Tuesday 9 July 13
  56. 56. Interesting relations 37 line1A C line2 D F B Tuesday 9 July 13
  57. 57. Interesting relations 37 line1A C line2 D F B Connected: Directly connected (e.g., A with B). connected Tuesday 9 July 13
  58. 58. Interesting relations 37 line1A C line2 D F B Connected: Directly connected (e.g., A with B). Nearby: At most one intermediate station, in the same line (e.g., A with C). nearby Tuesday 9 July 13
  59. 59. Interesting relations 37 line1A C line2 D F B Connected: Directly connected (e.g., A with B). Nearby: At most one intermediate station, in the same line (e.g., A with C). Reachable: Transitively connected (e.g., A with F). reachable Tuesday 9 July 13
  60. 60. Interesting relations 38 Connected: Directly connected (e.g., A with B). Nearby: At most one intermediate station, in the same line (e.g., A with C). Reachable: Transitively connected (e.g., A with F). Easily expressed with logic facts and rules Tuesday 9 July 13
  61. 61. Which is the best language for this problem? • A logic language would let us express our problem using facts and rules. • But we sill want access to a modern OO language to develop a user friendly interface. • Then let’s do it with both through linguistic symbiosis. 39 Tuesday 9 July 13
  62. 62. Implementation Strategy • Develop the program in Prolog. • Wrap it with an OO layer (Logtalk). • Program the Java side using LogicObjects. 40 Tuesday 9 July 13
  63. 63. 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). 41 Tuesday 9 July 13
  64. 64. 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). 41 FACTS Tuesday 9 July 13
  65. 65. 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). 41 RULES Tuesday 9 July 13
  66. 66. 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). 42 :- object(metro). :- end_object. Tuesday 9 July 13
  67. 67. 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). 42 :- object(metro). :- end_object. Tuesday 9 July 13
  68. 68. 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). 42 :- object(metro). :- end_object. :- public([connected/3, nearby/2, reachable/3, line/1]). ACCESS MODIFIERS Tuesday 9 July 13
  69. 69. Logtalk parametric objects 43 :- 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. Tuesday 9 July 13
  70. 70. Logtalk parametric objects 43 :- 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. PARAMETRIC OBJECT Tuesday 9 July 13
  71. 71. Logtalk parametric objects 43 :- 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. PARAMETRIC OBJECT PARAMETRIC OBJECT Tuesday 9 July 13
  72. 72. 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’ 44 Tuesday 9 July 13
  73. 73. 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’ 44 Tuesday 9 July 13
  74. 74. The Java world 45 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 { ... } Tuesday 9 July 13
  75. 75. The Java world 45 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”}) Tuesday 9 July 13
  76. 76. Mapping objects from the two worlds 46 Tuesday 9 July 13
  77. 77. 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. 47 * Some of them presented a bit differently in: D'Hondt, Maja and Gybels, Kris and Jonckers, Viviane. Seamless integration of rule-based knowledge and object- oriented functionality with linguistic symbiosis. In Proceedings of the 2004 ACM symposium on Applied computing, SAC '04, pages 1328{1335, New York, NY, USA, 2004. ACM. * Tuesday 9 July 13
  78. 78. 48 Translating objects to logic terms Tuesday 9 July 13
  79. 79. 48 Translating objects to logic terms Tuesday 9 July 13
  80. 80. 48 Translating objects to logic terms Tuesday 9 July 13
  81. 81. 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(l_name) Translating objects to logic terms 49 Java Logtalk Tuesday 9 July 13
  82. 82. 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(l_name) Translating objects to logic terms 49 Java Logtalk Tuesday 9 July 13
  83. 83. 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(l_name) Translating objects to logic terms 49 Java Logtalk Tuesday 9 July 13
  84. 84. 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(l_name) Translating objects to logic terms 49 Java Logtalk Tuesday 9 July 13
  85. 85. Mapping Java methods to Logtalk methods 50 @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(); } line(l_name)::connects( station(s1_name), station(s2_name)). line(l_name)::connects(_, _). Java Logtalk Tuesday 9 July 13
  86. 86. Mapping Java methods to Logtalk methods 50 @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(); } line(l_name)::connects( station(s1_name), station(s2_name)). line(l_name)::connects(_, _). Java Logtalk Tuesday 9 July 13
  87. 87. Mapping Java methods to Logtalk methods 50 @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(); } line(l_name)::connects( station(s1_name), station(s2_name)). line(l_name)::connects(_, _). Java Logtalk Tuesday 9 July 13
  88. 88. Mapping Java methods to Logtalk methods 50 @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(); } line(l_name)::connects( station(s1_name), station(s2_name)). line(l_name)::connects(_, _). Java Logtalk Tuesday 9 July 13
  89. 89. @LObject(args = {"name"}) public abstract class Station { @LSolution("IntermediateStations") @LMethod(name = "reachable", args = {"$1", "IntermediateStations"}) public abstract List<Station> intermediateStations(Station station); ... } 51 Dealing with unbound variables in method calls Tuesday 9 July 13
  90. 90. @LObject(args = {"name"}) public abstract class Station { @LSolution("IntermediateStations") @LMethod(name = "reachable", args = {"$1", "IntermediateStations"}) public abstract List<Station> intermediateStations(Station station); ... } 51 first Java method parameter (as term) Dealing with unbound variables in method calls Tuesday 9 July 13
  91. 91. @LObject(args = {"name"}) public abstract class Station { @LSolution("IntermediateStations") @LMethod(name = "reachable", args = {"$1", "IntermediateStations"}) public abstract List<Station> intermediateStations(Station station); ... } 51 first Java method parameter (as term) unbound logic variable Dealing with unbound variables in method calls Tuesday 9 July 13
  92. 92. @LObject(args = {"name"}) public abstract class Station { @LSolution("IntermediateStations") @LMethod(name = "reachable", args = {"$1", "IntermediateStations"}) public abstract List<Station> intermediateStations(Station station); ... } 51 station(s1_name)::reachable(station(s2_name), IntermediateStations) Dealing with unbound variables in method calls Tuesday 9 July 13
  93. 93. 52 Interpreting a query result as a Java object The logic solutions Varx1 x1, Vary1 y1 Varx2 x2, Vary2 y2 Varxn xn, Varyn yn Tuesday 9 July 13
  94. 94. 52 Interpreting a query result as a Java object The logic solutions Varx1 x1, Vary1 y1 Varx2 x2, Vary2 y2 Varxn xn, Varyn yn (set of frames binding logic variables to terms) frame 1 frame 2 frame n Tuesday 9 July 13
  95. 95. 53 Interpreting a query result as a Java object The logic solutions Varx1 x1, Vary1 y1 Varx2 x2, Vary2 y2 Varxn xn, Varyn yn (set of frames binding logic variables to terms) Tuesday 9 July 13
  96. 96. 53 Interpreting a query result as a Java object The logic solutions The method return value Varx1 x1, Vary1 y1 Varx2 x2, Vary2 y2 aJavaObject Varxn xn, Varyn yn (set of frames binding logic variables to terms) Tuesday 9 July 13
  97. 97. 54 Returning values from one solution The logic solutions The method return value term(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) Tuesday 9 July 13
  98. 98. 54 Returning values from one solution The logic solutions The method return value term(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 method with @LSolution) Tuesday 9 July 13
  99. 99. 54 Returning values from one solution The logic solutions The method return value term(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 method with @LSolution) (default solution) Tuesday 9 July 13
  100. 100. @LObject(args = {"name"}) public abstract class Station { @LSolution("IntermediateStations") @LMethod(name = "reachable", args = {"$1", "IntermediateStations"}) public abstract List<Station> intermediateStations(Station station); ... } 55 Explicitly specification of return values Tuesday 9 July 13
  101. 101. @LObject(args = {"name"}) public abstract class Station { @LSolution("IntermediateStations") @LMethod(name = "reachable", args = {"$1", "IntermediateStations"}) public abstract List<Station> intermediateStations(Station station); ... } 55 Explicitly specification of return values Tuesday 9 July 13
  102. 102. @LObject(args = {"name"}) public abstract class Station { @LSolution("IntermediateStations") @LMethod(name = "reachable", args = {"$1", "IntermediateStations"}) public abstract List<Station> intermediateStations(Station station); ... } 55 Explicitly specification of return values Tuesday 9 July 13
  103. 103. @LObject(args = {"name"}) public abstract class Station { @LMethod(name = "reachable", args = {"$1", "LSolution"}) public abstract List<Station> intermediateStations(Station station); ... } 56 Implicit specification of return values Tuesday 9 July 13
  104. 104. @LObject(args = {"name"}) public abstract class Station { @LMethod(name = "reachable", args = {"$1", "LSolution"}) public abstract List<Station> intermediateStations(Station station); ... } 56 Implicit specification of return values Tuesday 9 July 13
  105. 105. Managing multiplicity 57 • A logic routine can have many solutions, in Java only one. • Multiple values can be grouped with @LComposition. • The kind of container to return depends on the method signature. • The kind of object in the container is also extracted from the method signature. Tuesday 9 July 13
  106. 106. 58 Returning multiple values from queries The logic solutions The method return value term(Varx1, Vary1) Varx1 x1, Vary1 y1 term(x1, y1) Varx2 x2, Vary2 y2 term(x2, y2) Varxn xn, Varyn yn term(xn, yn) Tuesday 9 July 13
  107. 107. 58 Returning multiple values from queries The logic solutions The method return value term(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) Tuesday 9 July 13
  108. 108. @LObject(args = {"name"}) public abstract class Station { ... @LComposition @LSolution("S") @LMethod(args = {"S"}) public abstract List<Station> nearby(); } 59 station(aName)::nearby(S). LogtalkJava Returning multiple values from queries Tuesday 9 July 13
  109. 109. @LObject(args = {"name"}) public abstract class Station { ... @LComposition @LSolution("S") @LMethod(args = {"S"}) public abstract List<Station> nearby(); } 59 station(aName)::nearby(S). LogtalkJava Returning multiple values from queries Tuesday 9 July 13
  110. 110. @LObject(args = {"name"}) public abstract class Station { ... @LComposition @LSolution("S") @LMethod(args = {"S"}) public abstract List<Station> nearby(); } 59 station(aName)::nearby(S). LogtalkJava Returning multiple values from queries Tuesday 9 July 13
  111. 111. @LObject(args = {"name"}) public abstract class Station { ... @LComposition @LSolution("S") @LMethod(args = {"S"}) public abstract List<Station> nearby(); } 59 container type station(aName)::nearby(S). LogtalkJava Returning multiple values from queries Tuesday 9 July 13
  112. 112. @LObject(args = {"name"}) public abstract class Station { ... @LComposition @LSolution("S") @LMethod(args = {"S"}) public abstract List<Station> nearby(); } 59 each solution type container type station(aName)::nearby(S). LogtalkJava Returning multiple values from queries Tuesday 9 July 13
  113. 113. Returning a property of the result set 60 @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(); } Tuesday 9 July 13
  114. 114. Returning a property of the result set 60 @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 Tuesday 9 July 13
  115. 115. Returning a property of the result set 60 @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 Tuesday 9 July 13
  116. 116. Instantiating a symbiotic object in Java Line line = newLogicObject(Line.class, “central”); System.out.println("Number of segments of line " + line + ": " + line.segments()); 61 Tuesday 9 July 13
  117. 117. Instantiating a symbiotic object in Java Line line = newLogicObject(Line.class, “central”); System.out.println("Number of segments of line " + line + ": " + line.segments()); 61 Tuesday 9 July 13
  118. 118. Instantiating a symbiotic object in Java Line line = newLogicObject(Line.class, “central”); System.out.println("Number of segments of line " + line + ": " + line.segments()); 61 Tuesday 9 July 13
  119. 119. Other features • Java expressions embedded in logic terms (symbiosis terms). • Dependency management. • Integration with plain Prolog (without Logtalk). 62 Tuesday 9 July 13
  120. 120. Future work • Finishing a full two-ways linguistic symbiosis framework. • Supporting more Prolog engines. • Adding support to other kinds of integration techniques (e.g. serialization and objects references). • Continue the development of reusable hybrid components. 63 Tuesday 9 July 13
  121. 121. Inspiration from other domains • Interoperability layer: JDBC. • Mapping of artefacts using annotations: JAXB. • Context dependent conversions: GSON. • Linguistic symbiosis concepts: SOUL. 64 Tuesday 9 July 13
  122. 122. Conclusions • We have provided portable and simple solutions for many issues concerning Java-Prolog interoperability. • We are actively exploring how far we can get in automation/ transparency regarding Java-Prolog linguistic symbiosis. • We are targeting complex heterogeneous realistic scenarios. • We are attempting to provide reusable hybrid components and frameworks that may be helpful to the community. 65 Tuesday 9 July 13
  123. 123. Thanks 66 Tuesday 9 July 13

×