root/trunk/manuals/userman/categories.html

Revision 4621, 18.2 KB (checked in by pmoura, 6 weeks ago)

Improved navigation bar in the XHTML manual pages.

  • 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: categories</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">Categories</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; Categories</div>
21
22<h1 id="categories_categories">Categories</h1>
23
24<p>
25Categories provide a way to encapsulate a set of related predicate declarations and definitions that do not represent an object and that only make sense when composed with other predicates. Categories may also be used to break a complex object in functional units. A category can be imported by several objects (without code duplication), including objects participating in prototype or class-based hierarchies. This concept of categories shares some ideas with Smalltalk-80 functional categories <a href="../bibliography.html#Goldberg83">[Goldberg 83]</a>, Flavors mix-ins <a href="../bibliography.html#Moon86">[Moon 86]</a> (without necessarily implying multi-inheritance) and Objective-C categories <a href="../bibliography.html#Cox86">[Cox 86]</a>.
26</p>
27<p>
28There are no pre-defined categories in Logtalk.
29</p>
30
31<h2 id="categories_defining">Defining a new category</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 category. 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 instance, we may define a category named <code>documenting</code> and save it in a <code>documenting.lgt</code> source file that will be compiled to a <code>documenting.pl</code> Prolog file.
35</p>
36<p>
37Category names must be atoms. Objects, categories, and protocols share the same name space: we cannot have a category with the same name as an object or a protocol.
38</p>
39<p>
40Category code (directives and predicates) is textually encapsulated by using two Logtalk directives: <a title="Consult reference manual" href="../refman/directives/category1_3.html"><code>category/1-3</code></a> and <a title="Consult reference manual" href="../refman/directives/end_category0.html"><code>end_category/0</code></a>. The most simple category will be one that is self-contained, not depending on any other Logtalk entity:
41</p>
42<pre>:- category(Category).
43    ...
44:- end_category.</pre>
45<p>
46If a category implements one or more protocols then the opening directive will be:
47</p>
48<pre>:- category(Category,
49    implements(Protocol1, Protocol2, ...)).
50    ...
51:- end_category.</pre>
52<p>
53A category may be defined as a composition of other categories by writing:
54</p>
55<pre>:- category(Category,
56    extends(Category1, Category2, ...)).
57    ...
58:- end_category.</pre>
59<p>
60This feature should only be used when extending a category without breaking its functional cohesion (for example, when a modified version of a category is needed for importing on several unrelated objects). The preferred way of composing several categories is by importing them into an object. When a category overrides a predicate defined in an extended category, the overridden definition can still be used by using the <a title="Consult reference manual" href="../refman/directives/alias3.html"><code>alias/3</code></a> predicate directive.
61</p>
62<p>
63A category may explicitly complement one or more existing objects, thus providing functionality similar to Objective-C categories:
64</p>
65<pre>:- category(Category,
66    complements(Object1, Object2, ....)).
67    ...
68:- end_category.</pre>
69<p>
70This allows us to add new functionality to existing objects without requiring access or modifications to their source code. Common uses are adding logging or debugging predicates to a set of objects. Complementing categories may also be used to define aliases for complemented object predicates. Complemented objects need to be compiled with the <code>complements</code> compiler option switched on.
71</p>
72<p>
73When you want to use static binding with a <em>complemented</em> object, be sure to compile and load the <em>complementing</em> category before the object (otherwise the new functionality added by the category will be ignored by the object clients).
74</p>
75<p>
76Categories cannot inherit from objects. In addition, categories cannot contain clauses for dynamic predicates. This restriction applies because a category can be imported by several objects and because we cannot use the database handling built-in methods with categories (messages can only be sent to objects). However, categories may contain declarations for dynamic predicates and they can contain predicates which handle dynamic predicates. For example:
77</p>
78<pre>:- category(attributes).
79
80    :- public(attribute/2).
81    :- public(set_attribute/2).
82    :- public(del_attribute/2).
83
84    :- private(attribute_/2).
85    :- dynamic(attribute_/2).
86
87    attribute(Attribute, Value) :-
88        ::attribute_(Attribute, Value).
89
90    set_attribute(Attribute, Value) :-
91        ::retractall(attribute_(Attribute, _)),
92        ::assertz(attribute_(Attribute, Value)).
93
94    del_attribute(Attribute, Value) :-
95        ::retract(attribute_(Attribute, Value)).
96
97:- end_category.</pre>
98<p>
99Each object importing this category will have its own <code>attribute_/2</code> private, dynamic predicate. The predicates <code>attribute/2</code>, <code>set_attribute/2</code>, and <code>del_attribute/2</code> always access and modify the dynamic predicate contained in the object receiving the corresponding messages.
100</p>
101
102<h2 id="categories_finding">Finding defined categories</h2>
103
104<p>
105We can find, by backtracking, all defined categories by using the <a title="Consult reference manual" href="../refman/builtins/current_category1.html"><code>current_category/1</code></a> Logtalk built-in predicate with an uninstantiated variable:
106</p>
107<pre>| ?- current_category(Category).</pre>
108<p>
109This predicate can also be used to test if a category is defined by calling it with a valid category identifier (an atom or a compound term).
110</p>
111
112<h2 id="categories_creating">Creating a new category in runtime</h2>
113
114<p>
115A category can be dynamically created at runtime by using the <a title="Consult reference manual" href="../refman/builtins/create_category4.html"><code>create_category/4</code></a> built-in predicate:
116</p>
117<pre>| ?- create_category(Category, Relations, Directives, Clauses).</pre>
118<p>
119The first argument should be either a variable or the name of the new category (a Prolog atom, which must not match with an existing entity name). The remaining three arguments correspond to the relations described in the opening category directive and to the category code contents (directives and clauses).
120</p>
121<p>
122For instance, the call:
123</p>
124<pre>| ?- create_category(ccc,
125                     [implements(ppp)],
126                     [private(bar/1)],
127                     [(foo(X):-bar(X)), bar(1), bar(2)]).</pre>
128<p>
129is equivalent to compiling and loading the category:
130</p>
131<pre>:- category(ccc,
132    implements(ppp)).
133
134    :- dynamic.
135
136    :- private(bar/1).
137
138    foo(X) :-
139        bar(X).
140
141    bar(1).
142    bar(2).
143
144:- end_category.</pre>
145<p>
146If we need to create a lot of (dynamic) categories at runtime, then is best to to define a metaclass or a prototype with a predicate that will call this built-in predicate in order to provide more sophisticated behavior.
147</p>
148
149<h2 id="categories_abolishing">Abolishing an existing category</h2>
150
151<p>
152Dynamic categories can be abolished using the <a title="Consult reference manual" href="../refman/builtins/abolish_category1.html"><code>abolish_category/1</code></a> built-in predicate:
153</p>
154<pre>| ?- abolish_category(Category).</pre>
155<p>
156The argument must be an identifier of a defined dynamic category, otherwise an error will be thrown.
157</p>
158
159<h2 id="categories_directives">Category directives</h2>
160
161<p>
162Category directives are used to set initialization goals and category properties and to document a category dependencies on other Logtalk entities.
163</p>
164
165<h3 id="categories_initialization">Category initialization</h3>
166
167<p>
168We can define a goal to be executed as soon as a category is (compiled and) loaded to memory with the <a title="Consult reference manual" href="../refman/directives/initialization1.html"><code>initialization/1</code></a> directive:
169</p>
170<pre>:- initialization(Goal).</pre>
171<p>
172The argument can be any valid Prolog or Logtalk goal, including a message sending call.
173</p>
174
175<h3 id="categories_dynamic">Dynamic categories</h3>
176
177<p>
178As usually happens with Prolog code, a category can be either static or dynamic. A category created during the execution of a program is always dynamic. A category defined in a file can be either dynamic or static. Dynamic categories are declared by using the <a title="Consult reference manual" href="../refman/directives/dynamic0.html"><code>dynamic/0</code></a> directive in the category source code:
179</p>
180<pre>:- dynamic.</pre>
181<p>
182The directive must precede any predicate directives or clauses. Please be aware that using dynamic code results in a performance hit when compared to static code. We should only use dynamic categories when these need to be abolished during program execution.
183</p>
184
185<h3 id="categories_dependencies">Category dependencies</h3>
186
187<p>
188Besides the relations declared in the category opening directive, the predicate definitions contained in the category may imply other dependencies. This can be documented by using the <a title="Consult reference manual" href="../refman/directives/calls1.html"><code>calls/1</code></a> and the <a title="Consult reference manual" href="../refman/directives/uses1.html"><code>uses/1</code></a> directives.
189</p>
190<p>
191The <code>calls/1</code> directive can be used when a predicate definition sends a message that is declared in a specific protocol:
192</p>
193<pre>:- calls(Protocol).</pre>
194<p>
195If a predicate definition sends a message to a specific object, this dependence can be declared with the <code>uses/1</code> directive:
196</p>
197<pre>:- uses(Object).</pre>
198<p>
199These two directives can be used by the Logtalk runtime to ensure that all needed entities are loaded when running an application.
200</p>
201
202<h3 id="categories_documentation">Category documentation</h3>
203
204<p>
205A category 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:
206</p>
207<pre>:- info(List).</pre>
208<p>
209See the <a href="documenting.html">documenting Logtalk programs</a> session for details.
210</p>
211
212<h2 id="categories_relationships">Category relationships</h2>
213
214<p>
215Logtalk provides two sets of built-in predicates that enable us to query the system about the possible relationships that a category can have with other entities.
216</p>
217<p>
218The built-in predicates <a title="Consult reference manual" href="../refman/builtins/implements_protocol2_3.html"><code>implements_protocol/2</code></a> and <a title="Consult reference manual" href="../refman/builtins/implements_protocol2_3.html"><code>implements_protocol/3</code></a> find which categories implements which protocols:
219</p>
220<pre>| ?- implements_protocol(Category, Protocol).</pre>
221<p>
222or, if we want to know the implementation scope:
223</p>
224<pre>| ?- implements_protocol(Category, Protocol, Scope).</pre>
225<p>
226Note 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_category1.html"><code>current_category/1</code></a> built-in predicate to ensure that the returned entity is a category and not an object.
227</p>
228<p>
229To find which objects import which categories we can use the
230<a title="Consult reference manual" href="../refman/builtins/imports_category2_3.html"><code>imports_category/2</code></a> or <a title="Consult reference manual" href="../refman/builtins/imports_category2_3.html"><code>imports_category/3</code></a> built-in predicates:
231</p>
232<pre>| ?- imports_category(Object, Category).</pre>
233<p>
234or, if we want to know the importation scope:
235</p>
236<pre>| ?- imports_category(Object, Category, Scope).</pre>
237<p>
238Note that a category may be imported by several objects.
239</p>
240<p>
241To find which categories extend other categories we can use the
242<a title="Consult reference manual" href="../refman/builtins/extends_category2_3.html"><code>extends_category/2</code></a> or <a title="Consult reference manual" href="../refman/builtins/extends_category2_3.html"><code>extends_category/3</code></a> built-in predicates:
243</p>
244<pre>| ?- extends_category(Category1, Category2).</pre>
245<p>
246or, if we want to know the extension scope:
247</p>
248<pre>| ?- extends_category(Category1, Category2, Scope).</pre>
249<p>
250Note that a category may be extended by several categories.
251</p>
252<p>
253To find which categories explicitly complement existing objects we can use the
254<a title="Consult reference manual" href="../refman/builtins/complements_object2.html"><code>complements_object/2</code></a> built-in predicate:
255</p>
256<pre>| ?- complements_object(Category, Object).</pre>
257<p>
258Note that a category may explicitly complement several objects.
259</p>
260
261<h2 id="categories_properties">Category properties</h2>
262
263<p>
264We can find the properties of defined categories by calling the built-in predicate <a title="Consult reference manual" href="../refman/builtins/category_property2.html"><code>category_property/2</code></a>:
265</p>
266<pre>| ?- category_property(Category, Property).</pre>
267<p>
268A category may have the property <code>static</code>, <code>dynamic</code>, or <code>built_in</code>. Dynamic categories can be abolished in runtime by calling the <a title="Consult reference manual" href="../refman/builtins/abolish_category1.html"><code>abolish_category/1</code></a> built-in predicate. A category may also have the property <code>synchronized</code>, which is related to multi-threading programming. Depending on the back-end Prolog compiler, a category may have additional properties related to the source file where it is defined.
269</p>
270
271<h2 id="categories_importing">Importing categories</h2>
272
273<p>
274Any number of objects can import a category. In addition, an object may import any number of categories. The syntax is very simple:
275</p>
276<pre>:- object(Object,
277    imports(Category1, Category2, ...)).
278    ...
279:- end_object.</pre>
280<p>
281To make all public predicates imported via a category protected or to make all public and protected predicates private we prefix the category's name with the corresponding keyword:
282</p>
283<pre>:- object(Object,
284    imports(private::Category)).
285    ...
286:- end_object.</pre>
287<p>
288or:
289</p>
290<pre>:- object(Object,
291    imports(protected::Category)).
292    ...
293:- end_object.</pre>
294<p>
295Omitting the scope keyword is equivalent to writing:
296</p>
297<pre>:- object(Object,
298    imports(public::Category)).
299    ...
300:- end_object.</pre>
301
302<h2 id="categories_predicates">Using category predicates</h2>
303
304<p>
305Category predicates can be called from within an object using either the message sending mechanisms or a direct call. Consider the following category:
306</p>
307<pre>:- category(output).
308
309    :- public(out/1).
310
311    out(X) :-
312        writeq(X), nl.
313
314:- end_category.</pre>
315<p>
316Using the message sending mechanisms, the predicate <code>out/1</code> can be called from within an object importing the category by simply sending a message to <em>self</em>. For example:
317</p>
318<pre>:- object(worker,
319    imports(output)).
320
321    ...
322    do(Task) :-
323        execute(Task, Result),
324        ::out(Result).
325    ...
326
327:- end_object.</pre>
328<p>
329This is the recommended way of calling category predicates as it supports the specialization/redefinition of the category predicate in a descendant object (as the predicate declaration and definition lookups will start from <em>self</em>). Messages to <em>self</em> usually imply the use of dynamic binding as the actual object that will receive the message is only known at runtime. This translates to a small performance penalty when compared with calls to local object predicates.
330</p>
331<p>
332It is also possible to perform direct calls to predicates from imported categories without using the message sending mechanisms with the <a title="Consult reference manual" href="../refman/control/direct1.html"><code>:/1</code></a> control construct. For example:
333</p>
334<pre>:- object(worker,
335    imports(output)).
336
337    ...
338    do(Task) :-
339        execute(Task, Result),
340        :out(Result).
341    ...
342
343:- end_object.</pre>
344<p>
345This alternative should only be used when the user knows a priori that the category predicates will not be specialized or redefined by descendant objects of the object importing the category. Its advantage is that, whenever possible, the Logtalk compiler will optimize the calls by using static binding (requires separate loading of the categories using the <code>reload(skip)</code> compiler flag). However, when static binding is not possible due to the lack of sufficient information at compilation time, the performance may be worse than calling the category predicate using a message to <em>self</em> due to the lack of a caching mechanism.
346</p>
347
348<div class="footer">
349    <div class="copyright">
350        <span>Copyright &copy; <a href="mailto:pmoura@logtalk.org">Paulo Moura</a> &mdash; <a href="http://logtalk.org">Logtalk.org</a></span><br/> 
351        <span>Last updated on: October 20, 2008</span>
352    </div>
353    <div class="navbottom">
354        <span><a href="protocols.html">previous</a> | <a href="../glossary.html">glossary</a> | <a href="predicates.html">next</a></span><br/>
355        <span><a href="http://validator.w3.org/check/referer">XHTML</a> + <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a></span>
356    </div>
357</div>
358
359</body>
360
361</html>
Note: See TracBrowser for help on using the browser.