forked from raoul2000/simpleWorkflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSWyEdConverter.php
295 lines (251 loc) · 10.4 KB
/
SWyEdConverter.php
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
288
289
290
291
292
293
294
295
<?php
/**
* Converts a workflow created with yEd Graph Editor (freeware) and saved in graphml format
* into an array suitable to be used with the simpleWorkflow extension (sW).<br/>
* The conversion is based on the $mapper array, which defines matches between sW attributes
* and properties used by yEd.
* Following sW values are references using a predefined name :
* <ul>
* <li>workflow - initial : is the id of the initial node</li>
* <li>node - id : id of a given node</li>
* <li>node - constraint : PHP expression used as constraint for a node</li>
* <li>node - label : text label for a node</li>
* <li>node - metadata.* : custom metadata value</li>
* <li>edge - task : PHP expression executed when the transition is performed</li>
* </ul>
* yEd Graph Editor node attributes are referenced in two ways : by their attribute name or by
* an xpath expression that applies to the y:ShapeNode element used by yEd to draw each node.
* The later is mainly usefule to extract the node label and possibly, some other informations
* like for instance the background color or text color used to render each node. The former
* is useful to extract built-in yEd attribute and also custom attribute defined by the user.<br/>
*
*/
class SWyEdConverter
{
/**
* @var array mapper between SW Workflow attributes and yEd attributes
*/
public $mapper = array(
'workflow' => array(
'id' => 'Id', // optional
'initial' => 'Initial-node-id', // mandatory
),
'node' => array(
'id' => 'xpath|y:ShapeNode/y:NodeLabel', // mandatory
'constraint' => 'Constraint', // optional
'label' => 'Label', // optional
'metadata.color' => 'xpath|y:ShapeNode/y:NodeLabel/@textColor', // optional
'metadata.bgcolor' => 'xpath|y:ShapeNode/y:Fill/@color', // optional
),
'edge' => array(
'task' => 'Task' // optional
)
);
/////////////////////////////////////////////////////////////////////////////////////////////////////
// private members
private $_xml;
private $_yedProperties;
private $_workflow = array(
'workflow' => array(),
'node' => array()
);
/**
*
* @param string $file name of th graphml (xml) file to process
* @return array the workflow definition
*/
public function convert($file)
{
if(!extension_loaded('domxml')){
throw new SWException('extension domxml not loaded : yEd converter requires domxml extension to process');
}
// reset all
$this->_xml = null;
$this->_yedProperties = array();
$this->_workflow = array(
'workflow' => array(),
'node' => array()
);
$this->_xml = simplexml_load_file($file);
$namespaces = $this->_xml->getNamespaces(true);
$this->_xml->registerXPathNamespace('y', 'http://www.yworks.com/xml/graphml');
$this->_xml->registerXPathNamespace('__empty_ns', $namespaces['']);
// extract yEd custom attributes IDs ///////////////////////////////////////////////////////////////////
//
// for instance : <key attr.name="description" attr.type="string" for="node" id="d7"/>
// is turned into
// array(
// 'node' => array(
// 'description' => 'd7',
// 'nodegraphics' => 'd9',
// etc...
// )
//
$nlKey = $this->_xml->xpath('/__empty_ns:graphml/__empty_ns:key');
foreach($nlKey as $ndKey){
if( $ndKey['attr.name'] != null &&
( $ndKey['for'] == 'node' || $ndKey['for'] == 'edge' || $ndKey['for'] == 'graph'))
{
$for = (string) $ndKey['for'];
$attrName = (string) $ndKey['attr.name'];
if( ! isset($this->_yedProperties[$for]) ) $this->_yedProperties[$for] = array();
$this->_yedProperties[$for][$attrName] = (string) $ndKey['id'];
}
elseif( $ndKey['yfiles.type'] == 'nodegraphics' || $ndKey['yfiles.type'] == 'edgegraphics' )
{
$for = (string )$ndKey['for'];
$fileType = (string) $ndKey['yfiles.type'];
$this->_yedProperties[$for][$fileType] = (string) $ndKey['id'];
}
}
$nodeGraphicId = $this->_yedProperties['node']['nodegraphics'];
// extract workflow properties /////////////////////////////////////////////////////
$nlGraph = $this->_xml->xpath('//__empty_ns:graph');
$yedGraphId = (string) $nlGraph[0]['id'];
foreach($this->mapper['workflow'] as $swAttrName => $yedAttrName)
{
// echo '<li>Extracting attribute sw:'.$swAttrName.' from yed:'.$yedAttrName.'</li>';
// creates the XPath that is applied to the yEd XML file in order to retrieve
// value for simpleWorkflow attribute $swAttrName (and for the current workflow)
if( preg_match('/^xpath\|(.*)$/',$yedAttrName,$matches))
{
$xpath = '//__empty_ns:graph[@id="'.$yedGraphId.'"]/__empty_ns:data[@key="'.$nodeGraphicId.'"]/'.$matches[1];
}
elseif( isset( $this->_yedProperties['graph'][$yedAttrName]))
{
$yedDataKey = $this->_yedProperties['graph'][$yedAttrName];
$xpath = '//__empty_ns:graph[@id="'.$yedGraphId.'"]/__empty_ns:data[@key="'.$yedDataKey.'"]';
}
else{
continue;
}
// // echo '<li>evaluating xpath = '.$xpath.' : ';
$result = $this->_xml->xpath($xpath);
if( count($result) == 1 ){
// // echo 'value found = <u>'.$result[0].'</u>';
$this->_workflow['workflow'][$swAttrName] = trim((string) $result[0]);
}
else {
//$this->_workflow['workflow'][$swAttrName] = null;
// // echo 'WARNING : value not found';
}
// // echo '</li>';
}
// // echo '</ul>';
// // echo '<pre>'.CVarDumper::dumpAsString($this->_workflow['workflow']).'</pre>';
// extract nodes ///////////////////////////////////////////////////////////////////
// // echo '<h2>Extracting Nodes</h2>';
$nlNode = $this->_xml->xpath('//__empty_ns:node');
foreach($nlNode as $ndNode)
{
$yNodeId = (string) $ndNode['id'];
$this->_workflow['node'][$yNodeId] = array();
// // echo 'yEd node Id = '.$yNodeId.'<br/>';
foreach($this->mapper['node'] as $swAttrName => $yedAttrName)
{
// // echo 'maping (sw)'.$swAttrName.' <- (yEd)'.$yedAttrName.'<br/>';
// creates the XPath that is applied to the yEd XML file in order to retrieve
// value for simpleWorkflow attribute $swAttrName (and for the current node)
if( preg_match('/^xpath\|(.*)$/',$yedAttrName,$matches))
{
$relXpath = $matches[1];
$xpath = '//__empty_ns:node[@id="'.$yNodeId.'"]/__empty_ns:data[@key="'.$nodeGraphicId.'"]/'.$relXpath;
}
elseif( isset( $this->_yedProperties['node'][$yedAttrName]))
{
$yedDataKey = $this->_yedProperties['node'][$yedAttrName];
$xpath = '//__empty_ns:node[@id="'.$yNodeId.'"]/__empty_ns:data[@key="'.$yedDataKey.'"]';
}else{
continue;
}
// XPath could be created : evaluate it now and store the returned value as a string
// echo 'evaluating xpath = '.$xpath.'<br/>';
$result = $this->_xml->xpath($xpath);
if( count($result) == 1 ){
// echo 'value found = '.$result[0].'<br/>';
$this->_workflow['node'][$yNodeId][$swAttrName] = trim((string) $result[0]);
}
else {
//$this->_workflow['node'][$yNodeId][$swAttrName] = null;
// echo 'WARNING : value not found<br/>';
}
}
// echo '<pre>'.CVarDumper::dumpAsString($this->_workflow['node'][$yNodeId]).'</pre>';
// echo '<hr/>';
}
// process edges ///////////////////////////////////////////////////////////////////
// echo '<h2>Extracting Edges</h2>';
$nlEdge = $this->_xml->xpath('//__empty_ns:edge');
foreach($nlEdge as $ndEdge)
{
$yedSource = (string) $ndEdge['source'];
$yedTarget = (string) $ndEdge['target'];
$yEdgeId = (string) $ndEdge['id'];
// echo 'processing edge from '.$yedSource.' to '.$yedTarget.'<br/>';
if( isset($this->_workflow['node'][$yedSource]) && isset($this->_workflow['node'][$yedTarget]))
{
// echo 'sw nodes found<br/>';
$swNodeSource = & $this->_workflow['node'][$yedSource];
$swNodeTarget = & $this->_workflow['node'][$yedTarget];
if(!isset($swNodeSource['transition'])){
$swNodeSource['transition'] = array();
}
// is there a task for this transition ? Task is the only attribute
// that can be attached to a transition
$yedAttrName = $this->mapper['edge']['task'];
if( preg_match('/^xpath\|(.*)$/',$yedAttrName,$matches))
{
$relXpath = $matches[1];
$xpath = '//__empty_ns:edge[@id="'.$yEdgeId.'"]/__empty_ns:data[@key="'.$nodeGraphicId.'"]/'.$relXpath;
}
elseif( isset( $this->_yedProperties['edge'][$yedAttrName]))
{
$yedDataKey = $this->_yedProperties['edge'][$yedAttrName];
$xpath = '//__empty_ns:edge[@id="'.$yEdgeId.'"]/__empty_ns:data[@key="'.$yedDataKey.'"]';
}else{
continue;
}
// XPath could be created : evaluate it now and store the returned value as a string
// echo 'evaluating xpath = '.$xpath.'<br/>';
$result = $this->_xml->xpath($xpath);
if( count($result) == 1 ){
// echo 'value found = '.$result[0].'<br/>';
$swNodeSource['transition'][$swNodeTarget['id']] = trim((string) $result[0]);
}
else {
$task = null;
// echo 'WARNING : value not found<br/>';
$swNodeSource['transition'][] = $swNodeTarget['id'];
}
}
}
// echo '<h2>Raw Workflow Array</h2>';
// echo '<pre>'.CVarDumper::dumpAsString($this->_workflow).'</pre>';
// normalize workflow array ///////////////////////////////////////////////////////////////////
// echo '<h2>Normalize Workflow Array</h2>';
if(isset($this->_workflow['workflow']['initial'])){
$this->_workflow['initial'] = $this->_workflow['workflow']['initial'];
}
unset($this->_workflow['workflow']);
foreach ($this->_workflow['node'] as $key => $node)
{
$normalizedNode = array();
foreach ($node as $nodeAttrName => $nodeAttrValue)
{
if( preg_match('/^metadata\.(.*)$/',$nodeAttrName,$matches))
{
if(!isset($normalizedNode['metadata'])){
$normalizedNode['metadata'] = array();
}
// echo 'metadata : '.$mdAttr.' ← '.$nodeAttrValue.'<br/>';
$normalizedNode['metadata'][$matches[1]] = $nodeAttrValue;
}else {
$normalizedNode[$nodeAttrName] = $nodeAttrValue;
}
}
unset($this->_workflow['node'][$key]);
$this->_workflow['node'][] = $normalizedNode;
}
return $this->_workflow;
}
}