Protocols enable the separation between interface and implementation: several objects can implement the same protocol and an object can implement several protocols. There are no pre-defined protocols in Logtalk.
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 <code>.lgt</code> but this is optional and can be set in the configuration files. Compiled source files (by the Logtalk preprocessor) have, by default, a <code>.pl</code> extension. Again, this can be set to match the needs of a particular Prolog compiler in the corresponding configuration file. For example, we may define a protocol named <code>listp</code> and save it in a <code>listp.lgt</code> source file that will be compiled to a <code>listp.pl</code> Prolog file.
Protocol names must be atoms. Objects, categories and protocols share the same name space: we can not have a protocol with the same name as an object or a category.
</p>
<p>
Protocol directives are textually encapsulated by using two Logtalk directives: <atitle="Consult reference manual"href="../refman/directives/protocol1_2.html"><code>protocol/1-2</code></a> and <atitle="Consult reference manual"href="../refman/directives/end_protocol0.html"><code>end_protocol/0</code></a>. The most simple protocol will be one that is self-contained, not depending on any other Logtalk entity:
</p>
<pre>
:- protocol(Protocol).
...
:- end_protocol.
</pre>
<p>
If a protocol extends one or more protocols, then the opening directive will be:
We can find, by backtracking, all defined protocols by using the <atitle="Consult reference manual"href="../refman/builtins/current_protocol1.html"><code>current_protocol/1</code></a> built-in predicate with an uninstantiated variable:
</p>
<pre>
| ?- current_protocol(Protocol).
</pre>
<p>
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 <atitle="Consult reference manual"href="../refman/builtins/create_protocol3.html"><code>create_protocol/3</code></a>:
The first argument, the name of the new protocol (a Prolog atom), should 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.
is equivalent to compiling and loading the protocol:
</p>
<pre>
:- protocol(ppp,
extends(qqq)).
:- dynamic.
:- public(foo/1, bar/1).
:- end_protocol.
</pre>
<p>
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 behaviour.
Dynamic protocols can be abolished using the <atitle="Consult reference manual"href="../refman/builtins/abolish_protocol1.html"><code>abolish_protocol/1</code></a> built-in predicate:
</p>
<pre>
| ?- abolish_protocol(Protocol).
</pre>
<p>
The argument must be an identifier of a defined dynamic protocol, otherwise an error will be thrown.
We can define a goal to be executed as soon as a protocol is (compiled and) loaded to memory with the <atitle="Consult reference manual"href="../refman/directives/initialization1.html"><code>initialization/1</code></a> directive:
</p>
<pre>
:- initialization(Goal).
</pre>
<p>
The argument can be any valid Prolog or Logtalk goal, including a message sending call.
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 <atitle="Consult reference manual"href="../refman/directives/dynamic0.html"><code>dynamic/0</code></a> directive in the protocol source code:
</p>
<pre>
:- dynamic.
</pre>
<p>
Let us just remember that the loss of performance of the dynamic code is usually of considerable importance to the 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 <atitle="Consult reference manual"href="../refman/directives/info1.html"><code>info/1</code></a> directive:
</p>
<pre>
:- info(List).
</pre>
<p>
See the <ahref="documenting.html">Documenting Logtalk programs</a> session for details.
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.
</p>
<p>
The built-in predicates <atitle="Consult reference manual"href="../refman/builtins/extends_protocol2_3.html"><code>extends_protocol/2</code></a> and <atitle="Consult reference manual"href="../refman/builtins/extends_protocol2_3.html"><code>extends_protocol/3</code></a> return all pairs of protocols so that the first one extends the second:
To find which objects or categories implement which protocols we can call the <atitle="Consult reference manual"href="../refman/builtins/implements_protocol2_3.html"><code>implements_protocol/2</code></a> or <atitle="Consult reference manual"href="../refman/builtins/implements_protocol2_3.html"><code>implements_protocol/2</code></a> built-in predicates:
Note that, if we use an uninstantiated variable for the first argument, we will need to use the <atitle="Consult reference manual"href="../refman/builtins/current_object1.html"><code>current_object/1</code></a> or <atitle="Consult reference manual"href="../refman/builtins/current_category1.html"><code>current_category/1</code></a> built-in predicates to identify the kind of entity returned.
We can find the properties of defined protocols by calling the <atitle="Consult reference manual"href="../refman/builtins/protocol_property2.html"><code>protocol_property/2</code></a> built-in predicate:
</p>
<pre>
| ?- protocol_property(Protocol, Property).
</pre>
<p>
A protocol may have the property <code>static</code>, <code>dynamic</code>, or <code>built_in</code>. Dynamic protocols can be abolished in runtime by calling the <atitle="Consult reference manual"href="../refman/builtins/abolish_protocol1.html"><code>abolish_protocol/1</code></a> built-in predicate.
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:
<p><spanclass="bleft"><ahref="http://validator.w3.org/check/referer">XHTML</a> + <ahref="http://jigsaw.w3.org/css-validator/check/referer">CSS</a></span><spanclass="bright">Last updated on: August 5, 2005</span></p>