summaryrefslogtreecommitdiff
blob: 677f5413c131c720b12267efab535d2a82747799 (plain)
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
<?php
/*******************************************************************************
*	This file contains the Process Printer for SemanticResultFormats
*   (https://www.mediawiki.org/wiki/Extension:Semantic_Result_Formats)
*
*	Copyright (c) 2008 - 2009 Frank Dengler and Hans-Jörg Happel
*
*   Process Printer is free software: you can redistribute it and/or modify
*   it under the terms of the GNU General Public License as published by
*   the Free Software Foundation, either version 3 of the License, or
*   (at your option) any later version.
*
*   Process Printer is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details.
*
*   You should have received a copy of the GNU General Public License
*   along with Process Printer. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/

if ( !defined( 'MEDIAWIKI' ) ) {
	die( 'Not an entry point.' );
}

/**
 * This is a contribution to Semtantic Result Formats (SRF) which are an
 * extension of Semantic MediaWiki (SMW) which in turn is an extension
 * of MediaWiki
 *
 * SRF defines certain "printers" to render the results of SMW semantic
 * "ASK"-queries. Some of these printers make use of the GraphViz/dot
 * library (which is wrapped by a separate MediaWiki extension).
 *
 * The purpose of this extension, is to render results of ASK-Queries
 * (e.g. Classes with Attributes) as GraphViz-layouted process graphs
 *
 *
 * @author Frank Dengler
 * @author Hans-Jörg Happel
 * @ingroup SemanticResultFormats
 *
 * @note AUTOLOADED
 */

// global variable defining picture path

$srfgPicturePath = "formats/graphviz/images/";



class SRFProcess extends SMWResultPrinter {

	// configuration variables
	protected $m_graphValidation 	= false;
	protected $m_isDebugSet 		= false;
	protected $m_processCategory	= 'Process'; // Category for processes - required for rendering compound nodes

	// internal variables
	protected $m_process;	// process to be rendered

	/**
	 * (non-PHPdoc)
	 * @see SMWResultPrinter::handleParameters()
	 */
	protected function handleParameters( array $params, $outputmode ) {
		parent::handleParameters( $params, $outputmode );

		// init process graph instance
		$this->m_process = new ProcessGraph();

		$this->m_process->setGraphName( trim( $params['graphname'] ) );
		$this->m_process->setGraphSize( trim( $params['graphsize'] ) );
		$this->m_process->setClusterColor( trim( $params['clustercolor'] ) );
		$this->m_process->setRankdir( strtoupper( trim( $params['rankdir'] ) ) );
		$this->m_process->setHighlightNode( trim( $params['highlight'] ) );
		$this->m_process->setHighlightColor( trim( $params['highlightcolor'] ) );
		$this->m_process->setHighlightColor( trim( $params['redlinkcolor'] ) );

		$this->m_process->setShowRoles( $params['showroles'] );
		$this->m_process->setShowStatus( $params['showstatus'] );
		$this->m_process->setShowRessources( $params['showresources'] );
		$this->m_process->setShowDiscussion( $params['showdiscussion'] );
		$this->m_process->setShowRedLinks( $params['showredlinks'] );
		$this->m_process->setShowCompound( $params['showcompound'] );

		$this->m_processCategory = $params['processcat'];
		$this->m_isDebugSet = $params['debug'];
		$this->m_graphValidation = $params['graphvalidation'];
	}

	/**
	 * @see SMWResultPrinter::getParamDefinitions
	 *
	 * @since 1.8
	 *
	 * @param $definitions array of IParamDefinition
	 *
	 * @return array of IParamDefinition|array
	 */
	public function getParamDefinitions( array $definitions ) {
		$params = parent::getParamDefinitions( $definitions );

		$params['graphname'] = array(
			'default' => '',
			'message' => 'srf-paramdesc-graphname',
		);

		$params['rankdir'] = array(
			'default' => 'TB',
			'message' => 'srf-paramdesc-rankdir',
		);

		$params['graphsize'] = array(
			'default' => '',
			'message' => 'srf-paramdesc-graphsize',
		);

		$params['clustercolor'] = array(
			'default' => 'lightgrey',
			'message' => 'srf-paramdesc-clustercolor',
		);

		$params['highlight'] = array(
			'default' => '',
			'message' => 'srf-paramdesc-highlight',
		);

		$params['highlightcolor'] = array(
			'default' => 'blue',
			'message' => 'srf-paramdesc-highlightcolor',
		);

		$params['redlinkcolor'] = array(
			'default' => 'red',
			'message' => 'srf-paramdesc-redlinkcolor',
		);

		$params['processcat'] = array(
			'default' => 'Process',
			'message' => 'srf-paramdesc-processcategory',
		);

		$params['showroles'] = array(
			'type' => 'boolean',
			'default' => false,
			'message' => 'srf-paramdesc-showroles',
		);

		$params['showstatus'] = array(
			'type' => 'boolean',
			'default' => false,
			'message' => 'srf-paramdesc-showstatus',
		);

		$params['showresources'] = array(
			'type' => 'boolean',
			'default' => false,
			'message' => 'srf-paramdesc-showresources',
		);

		$params['showdiscussion'] = array(
			'type' => 'boolean',
			'default' => false,
			'message' => 'srf-paramdesc-showdiscussion',
		);

		$params['showredlinks'] = array(
			'type' => 'boolean',
			'default' => false,
			'message' => 'srf-paramdesc-showredlinks',
		);

		$params['showcompound'] = array(
			'type' => 'boolean',
			'default' => true,
			'message' => 'srf-paramdesc-showcompound',
		);

		$params['debug'] = array(
			'type' => 'boolean',
			'default' => false,
			'message' => 'srf-paramdesc-debug',
		);

		$params['graphvalidation'] = array(
			'type' => 'boolean',
			'default' => false,
			'message' => 'srf-paramdesc-graphvalidation',
		);

		return $params;
	}

	/**
	 *	This method renders the result set provided by SMW according to the printer
	 *
	 *  @param res				SMWQueryResult, result set of the ask query provided by SMW
	 *  @param outputmode		?
	 *  @return				String, rendered HTML output of this printer for the ask-query
	 *
	 */
	protected function getResultText( SMWQueryResult $res, $outputmode ) {
		if ( !is_callable( 'renderGraphviz' ) ) {
			wfWarn( 'The SRF Graph printer needs the GraphViz extension to be installed.' );
			return '';
		}

		global $wgContLang; // content language object

		//
		//	GraphViz settings
		//
		global $wgGraphVizSettings;
		$this->isHTML 		= true;


		//
		//	Iterate all rows in result set
		//

		$row = $res->getNext(); // get initial row (i.e. array of SMWResultArray)

		while ( $row !== false ) {
			/* SMWDataItem */ $subject = $row[0]->getResultSubject(); // get Subject of the Result
			// creates a new node if $val has type wikipage
			if ( $subject->getDIType() == SMWDataItem::TYPE_WIKIPAGE ) {
				$wikiPageValue = new SMWWikiPageValue( '_wpg' );
				$wikiPageValue->setDataItem( $subject );
				$node = $this->m_process->makeNode( $wikiPageValue->getShortWikiText(), $wikiPageValue->getShortWikiText() );
			}

     		//
			//	Iterate all colums of the row (which describe properties of the proces node)
			//

			// FIXME: this does not work with SMW >= 1.6, see
			// https://bugzilla.wikimedia.org/show_bug.cgi?id=35003

			// FIXME: got _a bit_ of redundancy here looks like... :/

			/**
			 * @var SMWResultArray $field
			 */
			foreach ( $row as $field ) {

				// check column title
				$req = $field->getPrintRequest();
				switch ( ( strtolower( $req->getLabel() ) ) ) {

					case strtolower( $wgContLang->getNsText( NS_CATEGORY ) ):
						foreach ( $field->getContent() as $value ) {
							$wikiPageValue = new SMWWikiPageValue( '_wpg' );
							$wikiPageValue->setDataItem( $value );
							$val = $wikiPageValue->getShortWikiText();

							if ( $val == ( $wgContLang->getNsText( NS_CATEGORY ) . ':' . $this->m_processCategory ) ) {
								$node->setAtomic( false );
							}
						}

	 					break;

	 				case "haslabel":
	 					$value = current($field->getContent()); // save only the first

						if (($value !== false)) {
							$wikiPageValue = new SMWWikiPageValue( '_wpg' );
							$wikiPageValue->setDataItem( $value );
							$val = $wikiPageValue->getLongWikiText();

							if ($this->m_process->getUseOtherLabels()) {
								$val = str_replace("&","and",$val);
								$node->setLabel($val);
							}
						}
						break;

					case "hasrole":
						foreach ( $field->getContent() as $value ) {
							$wikiPageValue = new SMWWikiPageValue( $field->getPrintRequest()->getTypeID() );
							$wikiPageValue->setDataItem( $value );
							$val = $wikiPageValue->getShortWikiText();

							$role = $this->m_process->makeRole( $val, $val );
							$node->addRole( $role );
						}
						break;

					case "usesresource":
						foreach ( $field->getContent() as $value ) {
							$wikiPageValue = new SMWWikiPageValue( $field->getPrintRequest()->getTypeID() );
							$wikiPageValue->setDataItem( $value );
							$val = $wikiPageValue->getShortWikiText();

							$xres = $this->m_process->makeRessource( $val, $val );
							$node->addUsedRessource( $xres );
						}
						break;

					case "producesresource":
						foreach ( $field->getContent() as $value ) {
							$wikiPageValue = new SMWWikiPageValue( $field->getPrintRequest()->getTypeID() );
							$wikiPageValue->setDataItem( $value );
							$val = $wikiPageValue->getShortWikiText();

							$xres = $this->m_process->makeRessource( $val, $val );
							$node->addProducedRessource( $xres );
						}
						break;

					case "hassuccessor":

						if ( count( $field->getContent() ) > 1 ) {

							// SplitParallel
							$edge = new SplitParallelEdge();
							$edge->setFrom( $node );
							foreach ( $field->getContent() as $value ) {
								$wikiPageValue = new SMWWikiPageValue( $field->getPrintRequest()->getTypeID() );
								$wikiPageValue->setDataItem( $value );
								$val = $wikiPageValue->getShortWikiText();

								$edge->addTo( $this->m_process->makeNode( $val, $val ) );
							}

						} else {

							// Sequence
							foreach ( $field->getContent() as $value ) {
								$wikiPageValue = new SMWWikiPageValue( $field->getPrintRequest()->getTypeID() );
								$wikiPageValue->setDataItem( $value );
								$val = $wikiPageValue->getShortWikiText();

								$edge = new SequentialEdge();
								$edge->setFrom( $node );
								$edge->setTo( $this->m_process->makeNode( $val, $val ) );
							}
						}

						break;

					case "hasorsuccessor":

						if ( count( $field->getContent() ) > 0 ) {

							// SplitExclusiveOr
							$edge = new SplitExclusiveOrEdge();
							$edge->setFrom( $node );
							foreach ( $field->getContent() as $value ) {
								$wikiPageValue = new SMWWikiPageValue( $field->getPrintRequest()->getTypeID() );
								$wikiPageValue->setDataItem( $value );
								$val = $wikiPageValue->getShortWikiText();

								$edge->addTo( $this->m_process->makeNode( $val, $val ) );
							}
						}

						break;

					case "hascontruesuccessor":

						if ( count( $field->getContent() ) > 0 ) {

							// SplitConditional
							if ( !isset( $cond_edge ) ) {
								$cond_edge = new SplitConditionalOrEdge();
								$cond_edge->setFrom( $node );
							}

							// should be only one
							foreach ( $field->getContent() as $value ) {
								$wikiPageValue = new SMWWikiPageValue( $field->getPrintRequest()->getTypeID() );
								$wikiPageValue->setDataItem( $value );
								$val = $wikiPageValue->getShortWikiText();

								$cond_edge->setToTrue( $this->m_process->makeNode( $val, $val ) );
							}

						}

						break;

					case "hasconfalsesuccessor":

						if ( count( $field->getContent() ) > 0 ) {

					 		// SplitConditional
							if ( !isset( $cond_edge ) ) {
								$cond_edge = new SplitConditionalOrEdge();
								$cond_edge->setFrom( $node );
							}

							// should be only one
							foreach ( $field->getContent() as $value ) {
								$wikiPageValue = new SMWWikiPageValue( $field->getPrintRequest()->getTypeID() );
								$wikiPageValue->setDataItem( $value );
								$val = $wikiPageValue->getShortWikiText();

								$cond_edge->setToFalse( $this->m_process->makeNode( $val, $val ) );
							}
						}

						break;

					case "hascondition":

						if ( count( $field->getContent() ) > 0 ) {

					 		// SplitConditional
							if ( !isset( $cond_edge ) ) {
								$cond_edge = new SplitConditionalOrEdge();
								$cond_edge->setFrom( $node );
							}

							// should be only one
							foreach ( $field->getContent() as $value ) {
								$wikiPageValue = new SMWWikiPageValue( $field->getPrintRequest()->getTypeID() );
								$wikiPageValue->setDataItem( $value );
								$val = $wikiPageValue->getShortWikiText();

								$cond_edge->setConditionText( $val );

							}
						}

						break;

					case "hasstatus":

						// should be only one
						foreach ( $field->getContent() as $value ) {
							$wikiPageValue = new SMWWikiPageValue( $field->getPrintRequest()->getTypeID() );
							$wikiPageValue->setDataItem( $value );
							$val = $wikiPageValue->getShortWikiText();

							$node->setStatus( $val );
						}

						break;

					default:

						// TODO - redundant column in result

	 			}
			}

			// reset row variables
			unset( $node );
			unset( $cond_edge );

		  	$row = $res->getNext();		// switch to next row
		}

		//
		// generate graphInput
		//
		$graphInput = $this->m_process->getGraphVizCode();

		//
		// render graphViz code
		//
		$result = renderGraphviz( $graphInput );

		$debug = '';
		if ( $this->m_isDebugSet ) $debug = '<pre>' . $graphInput . '</pre>';

		return $result . $debug;
	}
}

/**
 * Class representing a process graph
 */
class ProcessGraph {

	// configuration variables
	protected $m_graphName 		= '';
	protected $m_rankdir		= 'TB';
	protected $m_graphSize		= '';
	protected $m_clusterColor	= 'lightgrey';
	protected $m_showStatus		= false;	// should status be rendered?
	protected $m_showRoles		= false;	// should roles be rendered?
	protected $m_showRessources	= false;	// should ressources be rendered?
	protected $m_showDiscussion	= false;	// should discussion be rendered?
	protected $m_highlightNode	= '';		// node to be highlighted
	protected $m_highlightColor = 'blue';	// highlight font color
	protected $m_showRedLinks	= false;	// check and highlight red links?
	protected $m_redLinkColor	= 'red';	// red link font color
	protected $m_showCompound	= true;		// highlight compound nodes (=subprocesses)

	public $m_useHtmlNodes = true;			// Set to false if you do not want to use HTML table nodes

	// instance variables
	protected $m_nodes		= array();	// list of all nodes
	protected $m_startnodes	= array();	// list of start nodes
	protected $m_endnodes	= array();	// list of end nodes
	protected $m_ressources	= array();	// list of ressources
	protected $m_roles		= array();	// list of roles
	protected $m_errors		= array();	// list of errors


	/**
	 * This method should be used for getting new or existing nodes
	 * If a node does not exist yet, it will be created
	 *
	 * @param $id			string, node id
	 * @param $label		string, node label
 	 * @return				Object of type ProcessNode
	 */
	public function makeNode( $id, $label ) {
		// check if node exists
		if ( isset( $this->m_nodes[$id] ) ) {
			// take existing node
			$node = $this->m_nodes[$id];

		} else {
			// create new node

			$node = new ProcessNode();
			$node->setId( $id );
			$node->setLabel( $label );
			$node->setProcess( $this );

			// is actual node name the same like the one to highlight?
			if ( strcasecmp( $id , $this->m_highlightNode ) == 0 ) {
				$node->setFontColor( $this->m_highlightColor );
			}

			// is the node a red link (i.e. corresponding wiki page does not yet exist)?
			if ( $this->m_showRedLinks ) {
				$title = Title::newFromDBkey( $id );
				if ( isset( $title ) && ( !$title->exists() ) ) $node->setFontColor( $this->m_redLinkColor );
			}

			// add new node to process
			$this->m_nodes[$id] = $node;
		}

		return $node;

	}

	public function makeRole( $id, $label ) {
		// check if role exists
		if ( isset( $this->m_roles[$id] ) ) {
			// take existing roles
			$role = $this->m_roles[$id];

		} else {
			$role = new ProcessRole();
			$role->setId( $id );
			$role->setLabel( $label );

			// add new role to process
			$this->m_roles[$id] = $role;
		}

		return $role;

	}

	public function makeRessource( $id, $label ) {
		// check if res exists
		if ( isset( $this->m_ressources[$id] ) ) {
			// take existing res
			$res = $this->m_ressources[$id];

		} else {
			$res = new ProcessRessource();
			$res->setId( $id );
			$res->setLabel( $label );

			// add new res to process
			$this->m_ressources[$id] = $res;

		}

		return $res;

	}

	public function getEndNodes() {
		if ( count( $this->m_endnodes ) == 0 ) {
			foreach ( $this->m_nodes as $node ) {
				if ( count( $node->getSucc() ) == 0 ) $this->m_endnodes[] = $node;
			}
		}

		return $this->m_endnodes;
	}

	public function getStartNodes() {

		if ( count( $this->m_startnodes ) == 0 ) {
			foreach ( $this->m_nodes as $node ) {
				if ( count( $node->getPred() ) == 0 ) {
					$this->m_startnodes[] = $node;
				}
			}
		}

		return $this->m_startnodes;
	}

	public function setShowStatus( $show ) {
		$this->m_showStatus = $show;
	}

	public function getShowStatus() {
		return $this->m_showStatus;
	}

	public function setShowRoles( $show ) {
		$this->m_showRoles = $show;
	}

	public function getShowRoles() {
		return $this->m_showRoles;
	}

	public function setShowCompound( $show ) {
		$this->m_showCompound = $show;
	}

	public function getShowCompound() {
		return $this->m_showCompound;
	}

	public function setShowDiscussion($show){
		$this->m_showDiscussion = $show;
	}

	public function getShowDiscussion(){
		return $this->m_showDiscussion;
	}

	public function setShowRessources( $show ) {
		$this->m_showRessources = $show;
	}

	public function getShowRessources() {
		return $this->m_showRessources;
	}

	public function setGraphName( $name ) {
		$this->m_graphName = $name;
	}

	public function getGraphName() {
		if ( $this->m_graphName == '' ) $this->m_graphName = 'ProcessQueryResult' . rand( 1, 99999 );
		return $this->m_graphName;
	}

	public function setGraphSize( $size ) {
		$this->m_graphSize = $size;
	}

	public function setRankdir( $rankdir ) {
		$this->m_rankdir = $rankdir;
	}

	public function setClusterColor( $color ) {
		$this->m_clusterColor = $color;
	}

	public function setHighlightColor( $color ) {
		$this->m_highlightColor = $color;
	}

	public function setRedLinkColor( $color ) {
		$this->m_redLinkColor = $color;
	}

	public function setShowRedLinks( $show ) {
		$this->m_showRedLinks = $show;
	}

	public function setHighlightNode( $name ) {
		$this->m_highlightNode = $name;
	}

	public function addError( $error ) {
		$this->m_errors[] = $error;
	}

	public function getErrors() {
		return $this->m_errors;
	}

	public function getGraphVizCode() {
		//
		// header
		//
		$res = 'digraph ' . $this->getGraphName() . ' {

	ranksep="0.5";';
		if ( $this->m_graphSize != '' ) $res .= '
	size="' . $this->m_graphSize . '";';
		$res .= '
	rankdir=' . $this->m_rankdir . ';
	';

		//
		// add startnodes
		//
		// TODO I18N
		$res .= '
	{rank=source; "Start";}
		"Start"[shape=box,label="Start",style=filled,color=green];';

		foreach ( $this->getStartNodes() as $node ) {
			$res .= '
		"Start" -> "' . $node->getId() .'":port1:n;';
		}

		$res .= '
		';

		//
		// add endnodes
		//
		// TODO I18N
		$res .= '
	{rank=sink; "End"; }
		"End"[shape=box,label="End",style=filled,color=green];';

		foreach ( $this->getEndNodes() as $node ) {
			$res .= '
		"' . $node->getId() . '":port1:s -> "End";';
		}

		$res .= '

	';

		//
		// add subnodes
		//
		foreach ( $this->m_nodes as $node ) {
			$res .= $node->getGraphVizCode();
		}

		//
		// add final stuff
		//
		$res .=
	'
	}';

		return $res;

	}

}

abstract class ProcessElement {

	// TODO I18N
	private $m_id		 = 'no_id';
	private $m_label	 = 'unlabeled';
	private $m_uid;

	public function getUUID(){
		if (!isset($this->m_uid)){
			$this->m_uid = sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
				mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff),
				mt_rand(0, 0x0fff) | 0x4000,
				mt_rand(0, 0x3fff) | 0x8000,
				mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
		}

		return $this->m_uid;
	}
	public function getId() {
		return $this->m_id;
	}

	public function setId( $id ) {
		$this->m_id = $id;
	}

	public function getLabel() {
		return $this->m_label;
	}

	public function setLabel( $label ) {
		$this->m_label = $label;
	}

}

class ProcessRessource extends ProcessElement {

	private $m_usedby		= array();
	private	$m_producedby	= array();

	public function getProducers() {
		return $this->m_producedby;
	}

	public function getUsers() {
		return $this->m_usedby;
	}

	public function addProducer( $node ) {
		$this->m_producedby[] = $node;
	}

	public function addUser( $node ) {
		$this->m_usedby[] = $node;
	}

}

class ProcessRole extends ProcessElement {

	private $m_nodes	= array();

	public function getNodes() {
		return $this->m_nodes;
	}

	public function addNode( $node ) {
		$this->m_nodes[] = $node;
	}

}

/**
 * Class reperesning a process node
 */
class ProcessNode extends ProcessElement {

	private $m_is_startnode	= false;	// explicit statement if this is a start node
	private $m_is_endnode	= false;	// explicit statement if this is a termination node
	private $m_status;					// status value
	private $m_is_atomic	= true;		// set false if this is a compound node

	private $m_process;					// reference to parent process

	private $m_fontColor = '';			// font color to render

	private $m_usedressources 		= array();	// ressources used by this node
	private $m_producedressources 	= array();	// ressources produces by this node
	private $m_roles				= array();	// roles related to this node

	private $m_edgeout;					// outgoing edge (can be only one)
	private	$m_edgesin 	=	array();	// incoming edges (can be many)

	public function setStatus( $status ) {
		$this->m_status = $status;
	}

	public function getStatus() {
		return $this->m_status;
	}

	public function setFontColor( $color ) {
		$this->m_fontColor = $color;
	}

	public function setProcess( $proc ) {
		$this->m_process =  $proc;
	}

	public function getProcess() {
		return $this->m_process;
	}

	public function getPred() {
		$res = array();

		foreach ( $this->m_edgesin as $edge ) {
			$res = array_merge( $res, $edge->getPred() );
		}

		return $res;
	}

	public function getSucc() {
		$res = array();

		if ( isset( $this->m_edgeout ) ) {
			$res = $this->m_edgeout->getSucc();
		}

		return $res;
	}

	public function setEdgeOut( $edge ) {
		$this->m_edgeout = $edge;
	}

	public function getEdgeOut() {
		return $this->m_edgeout;
	}

	public function addEdgeIn( $edge ) {
		$this->m_edgesin[] = $edge;
	}

	public function getEdgesIn() {
		return $this->m_edgesin;
	}

	public function addRole( $role ) {
		$this->m_roles[] = $role;
		$role->addNode( $this );
	}

	public function getRoles() {
		return $this->m_roles;
	}

	public function addUsedRessource( $res ) {
		$this->m_usedressources[] = $res;
		$res->addUser( $this );
	}

	public function getUsedRessources() {
		return $this->m_usedressources;
	}

	public function addProducedRessource( $res ) {
		$this->m_producedressources[] = $res;
		$res->addProducer( $this );
	}

	public function getProducedRessources() {
		return $this->m_producedressources;
	}

	public function isAtomic() {
		return $this->m_is_atomic;
	}

	public function setAtomic( $atomic ) {
		$this->m_is_atomic = $atomic;
	}

	public function getGraphVizCode() {
		global $IP, $srfgPicturePath, $srfgIP;
		//
		// show node status
		//
		$status = '';
		if ( $this->getProcess()->getShowStatus() ) {

			if ( file_exists( $IP . "/images/p000.png" ) ) {
				$PicturePath = $IP . "/images/";
			} elseif ( file_exists( $srfgIP . "/formats/graphviz/images/p000.png" ) ) {
				$PicturePath = $srfgIP . "/formats/graphviz/images/";
			} else {
				$PicturePath = $IP . $srfgPicturePath;
			}
			// $color = 'grey' . $this->getStatus();
			// $color = 'grey' . rand(1, 100);
			// $status = ',style=filled,color=' . $color;
			if ( $this->getStatus() != '' ) {
				if ( $this->getStatus() < 25 ) {
					$status = ' HREF="[[' . $this->getId() . ']]" TOOLTIP="status '.$this->getStatus().'%"><IMG SRC="' . $PicturePath .'p000.png" /';
				} elseif ( $this->getStatus() < 50 ) {
					$status = ' HREF="[[' . $this->getId() . ']]" TOOLTIP="status '.$this->getStatus().'%"><IMG SRC="' . $PicturePath .'p025.png" /';
				} elseif ( $this->getStatus() < 75 ) {
					$status = ' HREF="[[' . $this->getId() . ']]" TOOLTIP="status '.$this->getStatus().'%"><IMG SRC="' . $PicturePath .'p050.png" /';
				} elseif ( $this->getStatus() < 100 ) {
					$status = ' HREF="[[' . $this->getId() . ']]" TOOLTIP="status '.$this->getStatus().'%"><IMG SRC="' . $PicturePath .'p075.png" /';
				} elseif ( $this->getStatus() == 100 ) {
					$status = ' HREF="[[' . $this->getId() . ']]" TOOLTIP="status '.$this->getStatus().'%"><IMG SRC="' . $PicturePath .'p100.png" /';
				}
			}

		}

	//
		// show discussion page
		//
		$discussion = '';
		if ( $this->getProcess()->getShowDiscussion() ) {

			if ( file_exists( $IP . "/images/discuss_icon.png" ) ) {
				$PicturePath = $IP . "/images/";
			} elseif ( file_exists( $srfgIP . "/formats/graphviz/images/discuss_icon.png" ) ) {
				$PicturePath = $srfgIP . "/formats/graphviz/images/";
			} else {
				$PicturePath = $IP . $srfgPicturePath;
			}
			$discussionTitle = Title::newFromText('Talk:'.$this->getId().'');
			if ($discussionTitle->isKnown()) {
				$discussion = ' HREF="[[Talk:' . $this->getId() . ']]" TOOLTIP="Talk:' . $this->getId() . '"><IMG SRC="' . $PicturePath .'discuss_icon.png" /';
			} else {
				$discussion = ' HREF="[[Talk:' . $this->getId() . ']]" TOOLTIP="Talk:' . $this->getId() . '"><IMG SRC="' . $PicturePath .'discuss_icon_grey.png" /';
			}


		}

		// use highlight color if set (either CURRENTPAGE or REDLINK highlighting - see ProcessGraph::makeNode()
		$high = '';
		if ( $this->m_fontColor !== '' ) {
			$high = ',fontcolor=' . $this->m_fontColor;
		}

		// insert icon for non-atomic nodes (i.e. subprocesses)
		$compound = '<TR><TD ALIGN="LEFT" BORDER="0" WIDTH="20px">';
		if ($this->getProcess()->getShowCompound()){
			if ( file_exists( $IP . "/images/subprocess.png" ) ) {
				$PicturePath = $IP . "/images/";
			} elseif ( file_exists( $srfgIP . "/formats/graphviz/images/subprocess.png" ) ) {
				$PicturePath = $srfgIP . "/formats/graphviz/images/";
			} else {
				$PicturePath = $IP . $srfgPicturePath;
			}
			if (!$this->isAtomic()) $compound = '<TR><TD ALIGN="LEFT" BORDER="0" WIDTH="20px" HREF="[['. $this->getId() . ']]" TOOLTIP="sub process"><IMG SRC="' . $PicturePath .'subprocess.png"/>';
		}



		//
		// render node itself
		//
		if ($this->m_process->m_useHtmlNodes){
			$res =
			'"' . $this->getId() . '" [shape=plaintext,label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">' . $compound . '</TD><TD BORDER="0" WIDTH="80%"></TD><TD ALIGN="RIGHT" BORDER="0" WIDTH="20px"' . $status . '></TD><TD ALIGN="RIGHT" BORDER="0" WIDTH="20px"' . $discussion . '></TD></TR><TR><TD COLSPAN="4" PORT="port1" HREF="[[' . $this->getId() . ']]" TOOLTIP="' . $this->getLabel() .'"><FONT' . $high .'>' . $this->getLabel() . '</FONT></TD> </TR></TABLE>>];
			';
		} else {
			$res =
			'"' . $this->getId() . '"[label="' . $this->getLabel() . '",shape=rect, height=1.5, URL="[[' . $this->getId() . ']]"];
			';
		}

		//
		// render outgoing node
		//
		if ( isset( $this->m_edgeout ) ) $res .= $this->m_edgeout->getGraphVizCode();


		//
		// show cluster for roles and ressources
		//
		$rrcluster = false;
		$rrcode = 'subgraph "cluster_role' . rand( 1, 9999 ) . '" { style=filled;color=lightgrey;';

		// show roles
		if ( $this->getProcess()->getShowRoles() ) {

			foreach ( $this->getRoles() as $role ) {
				$rrcluster = true;
				$rrcode .= '
				"' . $role->getId() . '"[label="' . $role->getLabel() . '",shape=doubleoctagon, color=red, URL="[[' . $role->getId() . ']]"];
				"' . $role->getId() . '" -> "' . $this->getId() . '":port1 [color=red,arrowhead = none,constraint=false];
				';

			}
		}

		if ( $this->getProcess()->getShowRessources() ) {

			foreach ( $this->getUsedRessources() as $xres ) {
				$rrcluster = true;
				$rrcode .= '
			"' . $xres->getId() . '"[label="' . $xres->getLabel() . '",shape=folder, color=blue, URL="[[' . $xres->getId() . ']]"];
			"' . $xres->getId() . '" -> "' . $this->getId() . '":port1 [color=blue,constraint=false];
				';
			}

			foreach ( $this->getProducedRessources() as $xres ) {
				$rrcluster = true;
				$rrcode .= '
			"' . $xres->getId() . '"[label="' . $xres->getLabel() . '",shape=folder, color=blue, URL="[[' . $xres->getId() . ']]"];
			"' . $this->getId() . '":port1 -> "' . $xres->getId() . '" [color=blue,constraint=false];
				';
				}

		}

		if ( $rrcluster ) $res .= $rrcode . '}';

		$res .= '
	';

		return $res;
	}

}


/**
 * Abstract base class for edges in a process graph
 */
abstract class ProcessEdge{

	private $m_id;
	private $m_uid;

	public function getId(){
		if (!isset($this->m_id)){
			$this->m_id = 'edge' . rand(1, 99999);
		}

		return $this->m_id;
	}

	public function getUUID(){
		if (!isset($this->m_uid)){
			$this->m_uid = sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
    	mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff),
    	mt_rand(0, 0x0fff) | 0x4000,
    	mt_rand(0, 0x3fff) | 0x8000,
    	mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
		}

		return $this->m_uid;
	}

	abstract public function getSucc();
	abstract public function getPred();

	abstract public function getGraphVizCode();
}

abstract class SplitEdge extends ProcessEdge{

	protected $m_from;
	protected $m_to 	= array();

	public function setFrom($node){
		$this->m_from = $node;
		$node->setEdgeOut($this);
	}

	public function addTo($node){
		$this->m_to[] = $node;
		$node->addEdgeIn($this);
	}

	public function getPred(){
		return array($this->m_from);
	}

	public function getSucc(){
		return $this->m_to;
	}

}

class SplitConditionalOrEdge extends ProcessEdge{

	protected $m_from;
	protected $m_to_true;
	protected $m_to_false;
	protected $m_con_text = 'empty_condition';

	public function getSucc(){
		return array($this->m_to_false, $this->m_to_true);
	}

	public function getPred(){
		return array($this->m_from);
	}

	public function setFrom($node){
		$this->m_from = $node;
		$node->setEdgeOut($this);
	}

	public function setToFalse($node){
		$this->m_to_false = $node;
		$node->addEdgeIn($this);
	}

	public function setToTrue($node){
		$this->m_to_true = $node;
		$node->addEdgeIn($this);
	}

	public function setConditionText($cond){
		$this->m_con_text = $cond;
	}

	public function getGraphVizCode(){

		$p = $this->m_from;

		if ((!isset($this->m_from)) || (!isset($this->m_to_false)) || (!isset($this->m_to_true))){

			echo "error with SplitConditionalOrEdge"; // TODO
			exit;
		}


		$res =
	'subgraph "clus_' . $this->getId() . '" {
		';

		// cond-Shape
		$con = 'con' .  rand(1, 99999);
		$res .=
		'"'. $con . '"[shape=diamond,label="' . $this->m_con_text . '",style=filled,color=skyblue];
		"' . $p->getId() . '":port1:s -> "'. $con . '";
		';

		// True Succ
		$res .=
		'"' . $this->m_to_true->getId() . '" [URL = "[['. $this->m_to_true->getId() . ']]"];
		';

		$res .=
		'"'. $con .'" -> "' . $this->m_to_true->getId() .'":port1:n [label="true"];
		';

		// False Succ
		$res .=
		'"' . $this->m_to_false->getId() . '" [URL = "[['. $this->m_to_false->getId() . ']]"];
		';

		$res .=
		'"'. $con .'" -> "' . $this->m_to_false->getId() .'":port1:n [label="false"];';


		$res .= '
	}
	';

		return $res;
	}

}

class SplitExclusiveOrEdge extends SplitEdge{

	public function getGraphVizCode(){
		global $srfgShapeStyle;
		$p = $this->getPred();
		$p = $p[0];
		if ($srfgShapeStyle=='') $srfgShapeStyle="box";
		$res =
	'subgraph "clus_' . $this->getId() . '" {
		';

		// add OR-Shape
		$orx = 'or' .  rand(1, 99999);
		$res .=
		'"'. $orx . '"[shape=' . $srfgShapeStyle . ',label="+",style=filled,color=gold];
		"' . $p->getId() . '":port1:s -> "'. $orx . '";
		';

		foreach ($this->getSucc() as $s){
			$res .=
		'"' . $s->getId() . '" [URL="[['. $s->getId() . ']]"];
		';

			$res .=
		'"'. $orx .'" -> "' . $s->getId() .'":port1:n;
		';
		}

		$res .= '
	}
	';

		return $res;
	}

}

class SplitParallelEdge extends SplitEdge{


	public function getGraphVizCode(){
		global $srfgShapeStyle;
		if ($srfgShapeStyle=='') $srfgShapeStyle="box";
		$p = $this->getPred();
		$p = $p[0];

		$res =
	'subgraph "clus_' . $this->getId() . '" {
		';

		// add AND-Shape
		$and = 'and' .  rand(1, 99999);
		$res .=
		'"'. $and . '"[shape=' . $srfgShapeStyle . ',label="||",style=filled,color=palegreen];
		"' . $p->getId() . '":port1:s -> "'. $and . '";
		';

		foreach ($this->getSucc() as $s){
			$res .=
		'"' . $s->getId() . '" [URL = "[['. $s->getId() . ']]"];
		';

			$res .=
		'"'. $and .'" -> "' . $s->getId() .'":port1:n;
		';
		}

		$res .= '
	}
	';

		return $res;
	}

}

class SequentialEdge extends ProcessEdge{

	private $m_from;
	private $m_to;

	public function setFrom($node){
		$this->m_from = $node;
		$node->setEdgeOut($this);
	}

	public function setTo($node){
		$this->m_to = $node;
		$node->addEdgeIn($this);
	}

	public function getPred(){
		return array($this->m_from);
	}

	public function getSucc(){
		return array($this->m_to);
	}

	public function getGraphVizCode(){

		$p = $this->m_from;
		$s = $this->m_to;

		$res =
	'subgraph "clus_' . $this->getId() . '" {
		';

		$res .=
		'"' . $s->getId() . '" [URL = "[['. $s->getId() . ']]"];
		';

		$res .=
		'"'. $p->getId() .'":port1:s -> "' . $s->getId() .'":port1:n;';

		$res .= '
	}
	';

		return $res;
	}

}