Protocols enable the separation between interface and implementation: several objects can implement the same protocol and an object can implement several protocols. Protocols may contain only predicate declarations. In some languages the term interface is used with similar meaning. Logtalk allows predicate declarations of any scope within protocols, contrary to some languages that only allow public declarations.
Logtalk defines three built-in protocols, monitoring
, expanding
, and forwarding
, which are described at the end of this section.
We can define a new object in the same way we write Prolog code: by using a text editor. Logtalk source files may contain one or more objects, categories, or protocols. If you prefer to define each entity in its own source file, it is recommended that the file be named after the protocol. By default, all Logtalk source files use the extension .lgt
but this is optional and can be set in the adapter files. Intermediate Prolog source files (generated by the Logtalk compiler) have, by default, a _lgt
suffix and a .pl
extension. Again, this can be set to match the needs of a particular Prolog compiler in the corresponding adapter file. For example, we may define a protocol named listp
and save it in a listp.lgt
source file that will be compiled to a listp_lgt.pl
Prolog file.
Protocol names must be atoms. Objects, categories and protocols share the same name space: we cannot have a protocol with the same name as an object or a category.
Protocol directives are textually encapsulated by using two Logtalk directives: protocol/1-2
and end_protocol/0
. The most simple protocol will be one that is self-contained, not depending on any other Logtalk entity:
:- protocol(Protocol). ... :- end_protocol.
If a protocol extends one or more protocols, then the opening directive will be:
:- protocol(Protocol, extends(Protocol1, Protocol2, ...)). ... :- end_protocol.
In order to maximize protocol reuse, all predicates specified in a protocol should relate to the same functionality. Therefore, the only recommended use of protocol extension is when you need both a minimal protocol and an extended version of the same protocol with additional, useful predicates.
We can find, by backtracking, all defined protocols by using the current_protocol/1
built-in predicate with a non-instantiated variable:
| ?- current_protocol(Protocol).
This predicate can also be used to test if a protocol is defined by calling it with a valid protocol identifier (an atom).
We can create a new (dynamic) protocol in runtime by calling the Logtalk built-in predicate create_protocol/3
:
| ?- create_protocol(Protocol, Relations, Directives).
The first argument should be either a variable or the name of the new protocol (a Prolog atom, which must not match an existing entity name). The remaining two arguments correspond to the relations described in the opening protocol directive and to the protocol directives.
For instance, the call:
| ?- create_protocol(ppp, [extends(qqq)], [public([foo/1, bar/1])]).
is equivalent to compiling and loading the protocol:
:- protocol(ppp, extends(qqq)). :- dynamic. :- public([foo/1, bar/1]). :- end_protocol.
If we need to create a lot of (dynamic) protocols at runtime, then is best to define a metaclass or a prototype with a predicate that will call this built-in predicate in order to provide more sophisticated behavior.
Dynamic protocols can be abolished using the abolish_protocol/1
built-in predicate:
| ?- abolish_protocol(Protocol).
The argument must be an identifier of a defined dynamic protocol, otherwise an error will be thrown.
Protocol directives are used to define protocol properties and documentation.
As usually happens with Prolog code, a protocol can be either static or dynamic. A protocol created during the execution of a program is always dynamic. A protocol defined in a file can be either dynamic or static. Dynamic protocols are declared by using the dynamic/0
directive in the protocol source code:
:- dynamic.
The directive must precede any predicate directives. Please be aware that using dynamic code results in a performance hit when compared to static code. We should only use dynamic protocols when these need to be abolished during program execution.
A protocol can be documented with arbitrary user-defined information by using the info/1
directive:
:- info(List).
See the documenting Logtalk programs section for details.
The include/1
directive can be used to load the contents of a file into a protocol. See the objects section for an example of using this directive.
Logtalk provides two sets of built-in predicates that enable us to query the system about the possible relationships that a protocol have with other entities.
The built-in predicates extends_protocol/2
and extends_protocol/3
return all pairs of protocols so that the first one extends the second:
| ?- extends_protocol(Protocol1, Protocol2).
or, if we want to know the extension scope:
| ?- extends_protocol(Protocol1, Protocol2, Scope).
To find which objects or categories implement which protocols we can call the implements_protocol/2
or implements_protocol/2
built-in predicates:
| ?- implements_protocol(ObjectOrCategory, Protocol).
or, if we want to know the implementation scope:
| ?- implements_protocol(ObjectOrCategory, Protocol, Scope).
Note that, if we use a non-instantiated variable for the first argument, we will need to use the current_object/1
or current_category/1
built-in predicates to identify the kind of entity returned.
We can find the properties of defined protocols by calling the protocol_property/2
built-in predicate:
| ?- protocol_property(Protocol, Property).
A protocol may have the property static
, dynamic
, or built_in
. Dynamic protocols can be abolished in runtime by calling the abolish_protocol/1
built-in predicate. Depending on the back-end Prolog compiler, a protocol may have additional properties related to the source file where it is defined.
The following protocol properties are supported:
static
dynamic
abolish_category/1
built-in predicate)built_in
source_data
file(Path)
file(Basename, Directory)
lines(BeginLine, EndLine)
public(Predicates)
protected(Predicates)
private(Predicates)
declares(Predicate, Properties)
alias(Predicate, Properties)
for(Original)
, from(Entity)
, non_terminal(NonTerminal)
, and line_count(Line)
with Line
being the begin line of the alias directive)
Some of the properties such as line numbers are only available when the protocol is defined in a source file compiled with the source_data
flag turned on.
Any number of objects or categories can implement a protocol. The syntax is very simple:
:- object(Object, implements(Protocol)). ... :- end_object.
or, in the case of a category:
:- category(Object, implements(Protocol)). ... :- end_category.
To make all public predicates declared via an implemented protocol protected or to make all public and protected predicates private we prefix the protocol's name with the corresponding keyword. For instance:
:- object(Object, implements(private::Protocol)). ... :- end_object.
or:
:- object(Object, implements(protected::Protocol)). ... :- end_object.
Omitting the scope keyword is equivalent to writing:
:- object(Object, implements(public::Protocol)). ... :- end_object.
The same rules applies to protocols implemented by categories.
Logtalk defines a set of built-in protocols that are always available for any application.
Logtalk defines a built-in protocol named expanding
that contains declarations for the term_expansion/2
and goal_expansion/2
predicates. See the description of the hook
compiler flag for more details.
Logtalk defines a built-in protocol named monitoring
that contains declarations for the before/3
and after/3
public event handler predicates. See the event-driven programming section for more details.
Logtalk defines a built-in protocol named forwarding
that contains a declaration for the forward/1
user-defined message forwarding handler, which is automatically called (if defined) by the runtime for any message that the receiving object does not understand. See also the []/1 control construct.