|
Revision 4601, 1.5 KB
(checked in by pmoura, 7 weeks ago)
|
|
Added svn:mime-type property to source files (set to text/x-logtalk).
|
-
Property svn:mime-type set to
text/x-logtalk
-
Property svn:eol-style set to
native
-
Property svn:keywords set to
Author Date Id Revision
|
| Line | |
|---|
| 1 | /* |
|---|
| 2 | This is a simple example of category composition, i.e. importation of |
|---|
| 3 | categories by other categories in order to provide modified components |
|---|
| 4 | for building objects, using car engines. |
|---|
| 5 | |
|---|
| 6 | The example defines a car engine protocol (enginep), a standard engine |
|---|
| 7 | (classic), and an improved version of it (sport). Both engines are then |
|---|
| 8 | imported in two car models (sedan and coupe). |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | % first we define a protocol for describing the characteristics of an engine: |
|---|
| 13 | |
|---|
| 14 | :- protocol(enginep). |
|---|
| 15 | |
|---|
| 16 | :- public(reference/1). |
|---|
| 17 | :- public(capacity/1). |
|---|
| 18 | :- public(cylinders/1). |
|---|
| 19 | :- public(horsepower_rpm/2). |
|---|
| 20 | :- public(bore_stroke/2). |
|---|
| 21 | :- public(fuel/1). |
|---|
| 22 | |
|---|
| 23 | :- end_protocol. |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | % second, we can define a typical engine as a category, which will be used |
|---|
| 27 | % when "assembling" cars: |
|---|
| 28 | |
|---|
| 29 | :- category(classic, |
|---|
| 30 | implements(enginep)). |
|---|
| 31 | |
|---|
| 32 | reference('M180.940'). |
|---|
| 33 | capacity(2195). |
|---|
| 34 | cylinders(6). |
|---|
| 35 | horsepower_rpm(94, 4800). |
|---|
| 36 | bore_stroke(80, 72.8). |
|---|
| 37 | fuel(gasoline). |
|---|
| 38 | |
|---|
| 39 | :- end_category. |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | % next, we define a souped up version of the previous engine, which differs |
|---|
| 43 | % from the standard one only in its reference and in its horsepower: |
|---|
| 44 | |
|---|
| 45 | :- category(sport, |
|---|
| 46 | extends(classic)). |
|---|
| 47 | |
|---|
| 48 | reference('M180.941'). |
|---|
| 49 | horsepower_rpm(HP, RPM) :- |
|---|
| 50 | ^^horsepower_rpm(ClassicHP, ClassicRPM), |
|---|
| 51 | HP is truncate(ClassicHP*1.23), |
|---|
| 52 | RPM is truncate(ClassicRPM*0.762). |
|---|
| 53 | |
|---|
| 54 | :- end_category. |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | % with engines (and other components), we may start "assembling" some cars: |
|---|
| 58 | |
|---|
| 59 | :- object(sedan, |
|---|
| 60 | imports(classic)). |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | :- end_object. |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | :- object(coupe, |
|---|
| 67 | imports(sport)). |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | :- end_object. |
|---|