root/trunk/manuals/userman/protocols.html

Revision 4625, 12.0 KB (checked in by pmoura, 5 weeks ago)

Corrected a typo in the user manual section on protocols.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?xml version="1.0" encoding="utf-8"?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
6
7<head>
8    <meta http-equiv="content-type" content="application/xml+xhtml; charset=utf-8" />
9    <title>Logtalk user manual: protocols</title>
10    <link rel="stylesheet" href="../screen.css" type="text/css" media="screen"/>
11    <link rel="stylesheet" href="../print.css" type="text/css" media="print"/>
12</head>
13
14<body>
15
16<div class="top-left">Logtalk user manual</div> 
17<div class="top-right">Protocols</div>
18<div class="bottom-left"><span class="page"/></div> 
19<div class="bottom-right"><span class="page"/></div>
20<div class="navtop"><a href="../index.html">Contents</a> &gt; <a href="index.html">User Manual</a> &gt; Protocols</div>
21
22<h1 id="protocols_protocols">Protocols</h1>
23
24<p>
25Protocols 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 <em>interface</em> is used with similar meaning. Logtalk allows predicate declarations of any scope within protocols, contrary to some languages that only allow public declarations.
26</p>
27<p>
28Logtalk defines two built-in protocols, <code>monitoring</code> and <code>expanding</code>, which are described at the end of this section.
29</p>
30
31<h2 id="protocols_defining">Defining a new protocol</h2>
32
33<p>
34We 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. Intermediate Prolog source files (generated by the Logtalk compiler) 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.
35</p>
36<p>
37Protocol 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.
38</p>
39<p>
40Protocol 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:
41</p>
42<pre>:- protocol(Protocol).
43    ...
44:- end_protocol.</pre>
45<p>
46If a protocol extends one or more protocols, then the opening directive will be:
47</p>
48<pre>:- protocol(Protocol,
49    extends(Protocol1, Protocol2, ...)).
50    ...
51:- end_protocol.</pre>
52<p>
53In 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.
54</p>
55
56<h2 id="protocols_finding">Finding defined protocols</h2>
57
58<p>
59We 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:
60</p>
61<pre>| ?- current_protocol(Protocol).</pre>
62<p>
63This predicate can also be used to test if a protocol is defined by calling it with a valid protocol identifier (an atom).
64</p>
65
66<h2 id="protocols_creating">Creating a new protocol in runtime</h2>
67
68<p>
69We 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>:
70</p>
71<pre>| ?- create_protocol(Protocol, Relations, Directives).</pre>
72<p>
73The 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.
74</p>
75<p>
76For instance, the call:
77</p>
78<pre>| ?- create_protocol(ppp, [extends(qqq)], [public(foo/1, bar/1)]).</pre>
79<p>
80is equivalent to compiling and loading the protocol:
81</p>
82<pre>:- protocol(ppp,
83    extends(qqq)).
84
85    :- dynamic.
86
87    :- public(foo/1, bar/1).
88
89:- end_protocol.</pre>
90<p>
91If 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.
92</p>
93
94<h2 id="protocols_abolishing">Abolishing an existing protocol</h2>
95
96<p>
97Dynamic 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:
98</p>
99<pre>| ?- abolish_protocol(Protocol).</pre>
100<p>
101The argument must be an identifier of a defined dynamic protocol, otherwise an error will be thrown.
102</p>
103
104<h2>Protocol directives<a id="protocols_directives"></a></h2>
105
106<p>
107Protocol directives are used to set initialization goals and protocol properties.
108</p>
109
110<h3 id="protocols_initialization">Protocol initialization</h3>
111
112<p>
113We 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:
114</p>
115<pre>:- initialization(Goal).</pre>
116<p>
117The argument can be any valid Prolog or Logtalk goal, including a message sending call.
118</p>
119
120<h3 id="protocols_dynamic">Dynamic protocols</h3>
121
122<p>
123As 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:
124</p>
125<pre>:- dynamic.</pre>
126<p>
127The 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.
128</p>
129
130<h3 id="protocols_documentation">Protocol documentation</h3>
131
132<p>
133A 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:
134</p>
135<pre>:- info(List).</pre>
136<p>
137See the <a href="documenting.html">documenting Logtalk programs</a> session for details.
138</p>
139
140<h2 id="protocols_relationships">Protocol relationships</h2>
141
142<p>
143Logtalk 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.
144</p>
145<p>
146The 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:
147</p>
148<pre>| ?- extends_protocol(Protocol1, Protocol2).</pre>
149<p>
150or, if we want to know the extension scope:
151</p>
152<pre>| ?- extends_protocol(Protocol1, Protocol2, Scope).</pre>
153<p>
154To 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:
155</p>
156<pre>| ?- implements_protocol(ObjectOrCategory, Protocol).</pre>
157<p>
158or, if we want to know the implementation scope:
159</p>
160<pre>| ?- implements_protocol(ObjectOrCategory, Protocol, Scope).</pre>
161<p>
162Note 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.
163</p>
164
165<h2 id="protocols_properties">Protocol properties</h2>
166
167<p>
168We 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:
169</p>
170<pre>| ?- protocol_property(Protocol, Property).</pre>
171<p>
172A 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. Depending on the back-end Prolog compiler, a protocol may have additional properties related to the source file where it is defined.
173</p>
174
175<h2 id="protocols_implementing">Implementing protocols</h2>
176
177<p>
178Any number of objects or categories can implement a protocol. The syntax is very simple:
179</p>
180<pre>:- object(Object,
181    implements(Protocol)).
182    ...
183:- end_object.</pre>
184<p>
185or, in the case of a category:
186</p>
187<pre>:- category(Object,
188    implements(Protocol)).
189    ...
190:- end_category.</pre>
191<p>
192To 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:
193</p>
194<pre>:- object(Object,
195    implements(private::Protocol)).
196    ...
197:- end_object.</pre>
198<p>
199or:
200</p>
201<pre>:- object(Object,
202    implements(protected::Protocol)).
203    ...
204:- end_object.</pre>
205<p>
206Omitting the scope keyword is equivalent to writing:
207</p>
208<pre>:- object(Object,
209    implements(public::Protocol)).
210    ...
211:- end_object.</pre>
212<p>
213The same rules applies to protocols implemented by categories.
214</p>
215
216<h2 id="protocols_built_in">Built-in protocols</h2>
217
218<p>
219Logtalk defines a set of built-in protocols that are always available for any application.
220</p>
221
222<h3 id="protocols_expanding">The built-in protocol <em>expanding</em></h3>
223
224<p>
225Logtalk defines a built-in protocol named <code>expanding</code> that contains declarations for the <code>term_expansion/2</code> and <code>goal_expansion/2</code> predicates. See the <a href="programming.html#programming_flags">description of the <code>hook</code> compiler flag</a> for more details.
226</p>
227
228<h3 id="protocols_monitoring">The built-in protocol <em>monitoring</em></h3>
229
230<p>
231Logtalk defines a built-in protocol named <code>monitoring</code> that contains declarations for the <code>before/3</code> and <code>after/3</code> public event handler predicates. See the <a href="events.html">event-driven programming</a> section for more details.
232</p>
233
234<div class="footer">
235    <div class="copyright">
236        <span>Copyright &copy; <a href="mailto:pmoura@logtalk.org">Paulo Moura</a> &mdash; <a href="http://logtalk.org">Logtalk.org</a></span><br/> 
237        <span>Last updated on: December 1, 2008</span>
238    </div>
239    <div class="navbottom">
240        <span><a href="objects.html">previous</a> | <a href="../glossary.html">glossary</a> | <a href="categories.html">next</a></span><br/>
241        <span><a href="http://validator.w3.org/check/referer">XHTML</a> + <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a></span>
242    </div>
243</div>
244
245</body>
246
247</html>
Note: See TracBrowser for help on using the browser.