1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
<?php /** * PHP_UML (PHP/MOF program elements classes) * * PHP version 5 * * @category PHP * @package PHP_UML * @subpackage Metamodel * @author Baptiste Autin <ohlesbeauxjours@yahoo.fr> * @license http://www.gnu.org/licenses/lgpl.html LGPL License 3 * @version SVN: $Revision: 175 $ * @link http://pear.php.net/package/PHP_UML * @link http://www.omg.org/mof/ * @since $Date: 2011-09-15 17:07:58 +0200 (jeu., 15 sept. 2011) $ * */
/** * A superstructure is a set composed of a UML model, a physical model, * and some stereotypes associated to the UML model. * * @category PHP * @package PHP_UML * @subpackage Metamodel * @author Baptiste Autin <ohlesbeauxjours@yahoo.fr> * @license http://www.gnu.org/licenses/lgpl.html LGPL License 3 */ class PHP_UML_Metamodel_Superstructure { const META_INTERFACE = 'PHP_UML_Metamodel_Interface'; const META_CLASS = 'PHP_UML_Metamodel_Class'; const META_DATATYPE = 'PHP_UML_Metamodel_Datatype'; const META_OPERATION = 'PHP_UML_Metamodel_Operation'; const META_PROPERTY = 'PHP_UML_Metamodel_Property';
const PHP_PROFILE_NAME = 'php'; const PHP_STEREOTYPE_DOCBLOCK = 'docblock'; /** * The root package for a logical UML model * * @var PHP_UML_Metamodel_Package */ public $packages;
/** * The root package for a deployment (physical) model * * @var PHP_UML_Metamodel_Package */ public $deploymentPackages;
/** * Stack of all stereotypes * TODO: when real stereotypes will be implemented, deleting that array, and * reading the stereotypes from the $packages instead * * @var array */ public $stereotypes = array();
/** * Constructor * */ public function __construct() { }
/** * Adds the internal PHP metatypes, metaclasses, metainterfaces... * */ public function addInternalPhpTypes() { foreach (PHP_UML_Metamodel_Enumeration::$datatypes as $d) { $type = new PHP_UML_Metamodel_Datatype; $type->id = PHP_UML_SimpleUID::getUID(); $type->name = $d;
$this->addRootType($type, 'Internal PHP type.'); } foreach (PHP_UML_Metamodel_Enumeration::$interfaces as $i) { $type = new PHP_UML_Metamodel_Interface; $type->id = PHP_UML_SimpleUID::getUID(); $type->name = $i;
$this->addRootType($type, 'Internal PHP interface.'); } foreach (PHP_UML_Metamodel_Enumeration::$classes as $c) { $type = new PHP_UML_Metamodel_Class; $type->id = PHP_UML_SimpleUID::getUID(); $type->name = $c;
$this->addRootType($type, 'Internal PHP class.'); } } private function addRootType(PHP_UML_Metamodel_NamedElement $type, $desc) { if (!PHP_UML_Metamodel_Helper::searchTypeIntoPackage($this->packages, $type->name)) { $this->packages->ownedType[] = $type; $this->addDocTags($type, $desc); } } /** * Creates a stereotype and the necessary Tag objects for a given element * * @param PHP_UML_Metamodel_NamedElement &$element The element to document * @param string $desc A textual description * @param array $docs An array containing docblocks */ public function addDocTags(PHP_UML_Metamodel_NamedElement &$element, $desc, array $docs = array()) { $stereotype = $this->createStereotype($element, self::PHP_PROFILE_NAME, self::PHP_STEREOTYPE_DOCBLOCK);
if ($desc != '') { $tag = new PHP_UML_Metamodel_Tag; $tag->id = PHP_UML_SimpleUID::getUID();; $tag->name = 'description'; $tag->value = $desc;
$stereotype->ownedAttribute[] = $tag; } foreach ($docs as $doc) { $tag = new PHP_UML_Metamodel_Tag; $tag->id = PHP_UML_SimpleUID::getUID();; $tag->name = $doc[1]; $tag->value = trim(implode(' ', array_slice($doc, 2)));
$stereotype->ownedAttribute[] = $tag; } $element->description = $stereotype; } /** * Recursively replaces all the "named types" by a proper "reference" to a * typed element. This impacts: * - the extended classes and implemented classes (through their * EMOF-"superClass" and "implements" relations) * - the function parameters (through their EMOF-"type" attribute) * - the properties in classes (through their EMOF-"type" attribute) * * @param PHP_UML_Metamodel_Package &$ns Package to resolve the elements of * @param array &$_oDef Default packages to look for * orphaned elements */ private function resolveReferences(PHP_UML_Metamodel_Package &$ns, array &$_oDef) { if (!is_null($ns->nestedPackage)) { foreach ($ns->nestedPackage as $key => &$pkg) { $this->resolveReferences($pkg, $_oDef); } } if (!is_null($ns->ownedType)) foreach ($ns->ownedType as &$elt) { if (isset($elt->superClass) && !is_null($elt->superClass)) { foreach ($elt->superClass as &$className) { $this->resolveType($ns, $className, $_oDef, $elt); } } if (isset($elt->implements) && !is_null($elt->implements)) { foreach ($elt->implements as &$className) { $this->resolveType($ns, $className, $_oDef, $elt); } } if (isset($elt->ownedOperation)) { foreach ($elt->ownedOperation as &$function) { foreach ($function->ownedParameter as &$parameter) { $this->resolveType($ns, $parameter->type, $_oDef, $elt); } } } if (isset($elt->ownedAttribute)) { foreach ($elt->ownedAttribute as &$attribute) { $this->resolveType($ns, $attribute->type, $_oDef, $elt); } } } if (isset($ns->ownedOperation)) { foreach ($ns->ownedOperation as &$function) { foreach ($function->ownedParameter as &$parameter) { $this->resolveType($ns, $parameter->type, $_oDef, $function); } } } if (isset($ns->ownedAttribute)) { foreach ($ns->ownedAttribute as &$attribute) { $this->resolveType($ns, $attribute->type, $_oDef, $attribute); } } }
/** * Retrieves the stereotype (named $name) associated to the element $element * If not found, returns null. * * @param PHP_UML_Metamodel_NamedElement $element Related object * @param string $profileName Profile name * @param string $stereotypeName Stereotype name * * @return PHP_UML_Metamodel_Stereotype */ public function getStereotype(PHP_UML_Metamodel_NamedElement $element, $profileName, $stereotypeName) { foreach ($this->stereotypes->getIterator() as $s) { if ($s->element == $element && $s->name == $stereotypeName && $s->profile == $profileName) { return $s; } } return null; }
/** * Creates a stereotype in a given profile, and binds it to a given element * Returns the stereotype that was created * * @param PHP_UML_Metamodel_NamedElement &$element The element * @param string $profileName The profile name * @param string $stereotypeName The stereotype name * * @return PHP_UML_Metamodel_Stereotype */ public function createStereotype(PHP_UML_Metamodel_NamedElement &$element, $profileName, $stereotypeName) { $stereotype = new PHP_UML_Metamodel_Stereotype;
$stereotype->profile = $profileName; $stereotype->name = $stereotypeName; $stereotype->element = $element; $this->stereotypes[] = $stereotype; return $stereotype; } /** * Finalizes the main object structure that the Parser has built. * Launches the resolution of the references for all stacks from the root pkg * * Every reference (a temporary string) is replaced by a PHP reference * to the corresponding type (that is, a class or a datatype) * Must be run once the model is complete (= once PHP parsing is done) * * @param bool $noEmptyPkg True to force removal of empty packages * @param array $defPkg Array of PHP_UML_Metamodel_Package where to look into, * in order to resolve the orphaned elements. * By default, it will look in the root package. This is, * by the way, where the PHP datatypes are. */ public function finalizeAll($noEmptyPkg = true, $defPkg = array()) { if ($noEmptyPkg) PHP_UML_Metamodel_Helper::deleteEmptyPackages($this->packages);
$resolver = new PHP_UML_Metamodel_TypeResolverByName(); $resolver->package = $this->packages; if (empty($defPkg)) $defPkg = array($this->packages); else $defPkg[] = &$this->packages; $resolver->resolveReferences($this->packages, $defPkg); } /** * Initialize the structure before use (we just instantiate the top objects in * the logical and deployment models) * * @param string $modelName Model name */ public function initModel($modelName = 'default') { $this->packages = new PHP_UML_Metamodel_Package; $this->packages->name = $modelName; $this->packages->id = PHP_UML_SimpleUID::getUID(); $this->addInternalPhpTypes();
$this->deploymentPackages = new PHP_UML_Metamodel_Package; $this->deploymentPackages->name = 'Deployment View'; $this->deploymentPackages->id = PHP_UML_SimpleUID::getUID(); } } ?>
|