This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/Logtalk/manuals/userman/protocols.html
pmoura b77427df89 Updated to Logtalk 2.9.1
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@211 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
2001-12-05 23:57:48 +00:00

262 lines
10 KiB
HTML

<!doctype html public "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Protocols</title>
<link rel=stylesheet href="../styles.css" type="text/css">
</head>
<body>
<hr />
<h1><a class="back" title="Return to index" href="index.html#protocols">Protocols</a></h1>
<p>
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.
</p>
<hr />
<h2><a class="back" title="Return to index" name="defining" href="index.html#protocols_defining">Defining a new protocol</a></h2>
<p>
We can define a new protocol in the same way we write Prolog code: by using a text editor. Each protocol (object or category) we define should be contained in its own text file. It is recommended that this text 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 pre-processor) 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.
</p>
<p>
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: <a title="Consult reference manual" href="../refman/directives/protocol1_2.html"><code>protocol/1-2</code></a> and <a title="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:
</p>
<pre>
:- protocol(Protocol,
extends(OtherProtocol)).
...
:- end_protocol.
</pre>
<hr />
<h2><a class="back" title="Return to index" name="finding" href="index.html#protocols_finding">Finding defined protocols</a></h2>
<p>
We can find, by backtracking, all defined protocols by using the <a title="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).
</p>
<hr />
<h2><a class="back" title="Return to index" name="creating" href="index.html#protocols_creating">Creating a new protocol in runtime</a></h2>
<p>
We can create a new (dynamic) protocol in runtime by calling the Logtalk built-in predicate <a title="Consult reference manual" href="../refman/builtins/create_protocol3.html"><code>create_protocol/3</code></a>:
</p>
<pre>
| ?- create_protocol(Protocol, Relations, Directives).
</pre>
<p>
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.
</p>
<p>
For instance, the call:
</p>
<pre>
| ?- create_protocol(ppp, [extends(qqq)], [public(foo/1, bar/1)]).
</pre>
<p>
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.
</p>
<hr />
<h2><a class="back" title="Return to index" name="abolishing" href="index.html#protocols_abolishing">Abolishing an existing protocol</a></h2>
<p>
Dynamic protocols can be abolished using the <a title="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.
</p>
<hr />
<h2><a class="back" title="Return to index" name="directives" href="index.html#protocols_directives">Protocol directives</a></h2>
<p>
Protocol directives are used to set initialization goals and protocol properties.
</p>
<h3><a class="back" title="Return to index" name="initialization" href="index.html#protocols_initialization">Protocol initialization</a></h3>
<p>
We can define a goal to be executed as soon as a protocol is (compiled and) loaded to memory with the <a title="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.
</p>
<h3><a class="back" title="Return to index" name="dynamic" href="index.html#protocols_dynamic">Dynamic protocols</a></h3>
<p>
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 <a title="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.
</p>
<h3><a class="back" title="Return to index" name="documentation" href="index.html#protocols_documentation">Protocol documentation</a></h3>
<p>
A protocol can be documented with arbitrary user-defined information by using the <a title="Consult reference manual" href="../refman/directives/info1.html"><code>info/1</code></a> directive:
</p>
<pre>
:- info(List).
</pre>
<p>
See the <a href="documenting.html">Documenting Logtalk programs</a> session for details.
</p>
<hr />
<h2><a class="back" title="Return to index" name="relationships" href="index.html#protocols_relationships">Protocol relationships</a></h2>
<p>
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 <a title="Consult reference manual" href="../refman/builtins/extends_protocol2_3.html"><code>extends_protocol/2</code></a> and <a title="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:
</p>
<pre>
| ?- extends_protocol(Protocol1, Protocol2).
</pre>
<p>
or, if we want to know the extension scope:
</p>
<pre>
| ?- extends_protocol(Protocol1, Protocol2, Scope).
</pre>
<p>
To find which objects or categories implement which protocols we can call the <a title="Consult reference manual" href="../refman/builtins/implements_protocol2_3.html"><code>implements_protocol/2</code></a> or <a title="Consult reference manual" href="../refman/builtins/implements_protocol2_3.html"><code>implements_protocol/2</code></a> built-in predicates:
</p>
<pre>
| ?- implements_protocol(ObjectOrCategory, Protocol).
</pre>
<p>
or, if we want to know the implementation scope:
</p>
<pre>
| ?- implements_protocol(ObjectOrCategory, Protocol, Scope).
</pre>
<p>
Note that, if we use an uninstantiated variable for the first argument, we will need to use the <a title="Consult reference manual" href="../refman/builtins/current_object1.html"><code>current_object/1</code></a> or <a title="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.
</p>
<hr />
<h2><a class="back" title="Return to index" name="properties" href="index.html#protocols_properties">Protocol properties</a></h2>
<p>
We can find the properties of defined protocols by calling the <a title="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 <a title="Consult reference manual" href="../refman/builtins/abolish_protocol1.html"><code>abolish_protocol/1</code></a> built-in predicate.
</p>
<hr />
<h2><a class="back" title="Return to index" name="implementing" href="index.html#protocols_implementing">Implementing protocols</a></h2>
<p>
Any number of objects or categories can implement a protocol. The syntax is very simple:
</p>
<pre>
:- object(Object,
implements(Protocol).
...
:- end_object.
</pre>
<p>
or, in the case of a category:
</p>
<pre>
:- category(Object,
implements(Protocol).
...
:- end_category.
</pre>
<p>
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>
<pre>
:- object(Object,
implements(private::Protocol).
...
:- end_object.
</pre>
<p>
or:
</p>
<pre>
:- object(Object,
implements(protected::Protocol).
...
:- end_object.
</pre>
<p>
Omitting the scope keyword is equivalent to writing:
</p>
<pre>
:- object(Object,
implements(public::Protocol).
...
:- end_object.
</pre>
<p>
The same rules applies to protocols implemented by categories.
</p>
<hr />
<p class="center">
<strong><a href="objects.html">Previous</a> | <a href="categories.html">Next</a> | <a href="index.html">Table of Contents</a> | <a href="../bibliography.html">Bibliography</a> | <a href="../glossary.html">Glossary</a></strong>
</p>
<p class="center">
Last updated on: April 30, 2001
</p>
<hr />
</body>
</html>