towards a software ecosystem for java prolog interoperabilty
DESCRIPTION
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.TRANSCRIPT
Towards a Software Ecosystemfor Java-Prolog Interoperability
Sergio Castro Kim Mens RELEASeD LAB
Université catholique de Louvain Belgium
Paulo MouraCenter for Research in Advanced Computing Systems
INESC TECPortugal
1
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
2
LogicObjects
Future work/Conclusions
Content
JPC
Hydra(or how to build reusable hybrid components)
Tuesday 9 July 13
3
OOprogramming
Logicprogramming
Appropriate for modellingreal-world concepts
Often enjoy rich softwareecosystems
Declarative reasoning
Tuesday 9 July 13
3
OOprogramming
Logicprogramming
Appropriate for modellingreal-world concepts
Often enjoy rich softwareecosystems
Declarative reasoning
Tuesday 9 July 13
3
OOprogramming
Logicprogramming
Appropriate for modellingreal-world concepts
Often enjoy rich softwareecosystems
Declarative reasoning
Tuesday 9 July 13
3
OOprogramming
Logicprogramming
Appropriate for modellingreal-world concepts
Often enjoy rich softwareecosystems
Declarative reasoning
Tuesday 9 July 13
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
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
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
• 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
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
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
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
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 Prolog.
• Mechanism based on the specification of mappings of such artefacts.
13
Tuesday 9 July 13
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
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
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
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
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
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
JPC architecture
• Abstraction of a Prolog VM.
• Reification of Prolog data types (e.g. Atom, Compound, etc).
• The conversion context.
20
Tuesday 9 July 13
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 converters, exception converters, multi-valued converters, etc).
• Interprets and refines hints from the user.
22
Tuesday 9 July 13
public class CollectionConverter<E> extends JpcConverter<Collection<E>, Term> {@Overridepublic Collection<E> fromTerm(Term listTerm, Type type, Jpc context) { ...}...
}
23
A pre-defined converter example
Tuesday 9 July 13
public class CollectionConverter<E> extends JpcConverter<Collection<E>, Term> {@Overridepublic Collection<E> fromTerm(Term listTerm, Type type, Jpc context) { ...}...
}
23
A pre-defined converter example
Java type
Tuesday 9 July 13
public class CollectionConverter<E> extends JpcConverter<Collection<E>, Term> {@Overridepublic Collection<E> fromTerm(Term listTerm, Type type, Jpc context) { ...}...
}
23
A pre-defined converter example
Prolog type
Tuesday 9 July 13
public class CollectionConverter<E> extends JpcConverter<Collection<E>, Term> {@Overridepublic Collection<E> fromTerm(Term listTerm, Type type, Jpc context) { ...}...
}
23
A pre-defined converter example
term to convert
Tuesday 9 July 13
public class CollectionConverter<E> extends JpcConverter<Collection<E>, Term> {@Overridepublic Collection<E> fromTerm(Term listTerm, Type type, Jpc context) { ...}...
}
23
A pre-defined converter example
type guiding conversion
Tuesday 9 July 13
public class CollectionConverter<E> extends JpcConverter<Collection<E>, Term> {@Overridepublic Collection<E> fromTerm(Term listTerm, Type type, Jpc context) { ...}...
}
23
A pre-defined converter example
the conversion context
Tuesday 9 July 13
A default conversion example
Jpc jpc = ... //the conversion contextTerm 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
A default conversion example
Jpc jpc = ... //the conversion contextTerm 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
A default conversion example
Jpc jpc = ... //the conversion contextTerm 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
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
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
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
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
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
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
Symbiosis
30
“The intimate living together of two dissimilar organisms in a mutually beneficial relationship.” (Merriam-Webster dictionary)
Tuesday 9 July 13
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
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
The inhabitants of our two worlds
33
The OOworld
The logicworld
Tuesday 9 July 13
The inhabitants of our two worlds
33
PackagesClassesObjectsMethods
Method invocationsReturn values
The OOworld
The logicworld
LibrariesModulesTerms
ClausesQueries
Query solutions
Tuesday 9 July 13
Reducing the gap with Logtalk
34
The OOworld
The logicworld
Tuesday 9 July 13
Reducing the gap with Logtalk
34
The OOworld
The logicworld
LogtalkAn 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 intermediate station, in the same line (e.g., A with C).
nearby
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 intermediate station, in the same line (e.g., A with C).
Reachable: Transitively connected (e.g., A with F).
reachable
Tuesday 9 July 13
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
• 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
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 Line { private String name; ...}
metro
my_metro
line(l_name)
Translating objects to logic terms
49
Java Logtalk
Tuesday 9 July 13
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
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
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
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
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
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
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
@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
@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
@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
@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
52
Interpreting a query result as a Java object
The logic solutions
Varx1 x1, Vary1 y1Varx2 x2, Vary2 y2
Varxn xn, Varyn yn
Tuesday 9 July 13
52
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
Tuesday 9 July 13
53
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)
Tuesday 9 July 13
53
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)
Tuesday 9 July 13
54
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)
Tuesday 9 July 13
54
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)
Tuesday 9 July 13
54
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)
Tuesday 9 July 13
@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
@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
@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
@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
@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
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
58
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)
Tuesday 9 July 13
58
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)
Tuesday 9 July 13
@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
@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
@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
@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
@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
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
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
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
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
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
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
Other features
• Java expressions embedded in logic terms (symbiosis terms).
• Dependency management.
• Integration with plain Prolog (without Logtalk).
62
Tuesday 9 July 13
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
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
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
Thanks
66
Tuesday 9 July 13