ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilBPMN2Parser.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2016 ILIAS open source, Extended GPL, see docs/LICENSE */
3
12class ilBPMN2Parser
13{
20 public function parseBPMN2XML($bpmn2_xml, $workflow_name = null)
21 {
22 $bpmn2_array = $this->convertXmlToArray($bpmn2_xml);
23 $process = $this->getProcessNodeFromArray($bpmn2_array);
24 $messages = $this->getMessageNodesFromArray($bpmn2_array);
25
26 $workflow_name = $this->determineWorkflowClassName($workflow_name, $bpmn2_array, $process);
27
28 require_once './Services/WorkflowEngine/classes/parser/class.ilWorkflowScaffold.php';
29 $class_object = new ilWorkflowScaffold($bpmn2_array);
30
31 $constructor_method_content = '';
32
33 $class_object->setWorkflowName($workflow_name);
34
35 $hasChildren = (isset($process['children']) && is_array($process['children']) && count($process['children']) > 0);
36 if ($hasChildren) {
37 $stashed_sequence_flows = array(); // There can be no assumption, that the workflow is modeled in sequence,
38 // so we need to stash the connectors to add them after the nodes.
39 $stashed_associations = array(); // There can be no assumption, that the workflow is modeled in sequence,
40 // so we need to stash the connectors to add them after the nodes.
41 $stashed_process_extensions = array(); // It was found that modelers add extensions at process level,
42 // they are stored for possible future use.
43 require_once './Services/WorkflowEngine/classes/parser/elements/class.ilBPMN2ElementLoader.php';
44
45 $loader = new ilBPMN2ElementLoader($bpmn2_array);
46
47 foreach ($process['children'] as $element) {
48 if ($element['name'] == 'ioSpecification') {
49 foreach ($element['children'] as $iospec_element) {
50 $element_object = $loader->load($iospec_element['name']);
51 $constructor_method_content .= $element_object->getPHP($iospec_element, $class_object);
52 }
53
54 continue;
55 }
56
57 if ($element['name'] == 'sequenceFlow') {
58 $stashed_sequence_flows[] = $element;
59 } elseif ($element['name'] == 'association') {
60 $stashed_associations[] = $element;
61 } elseif ($element['name'] == 'extensionElements') {
62 $stashed_process_extensions[] = $element;
63 } else {
64 $element_object = $loader->load($element['name']);
65 $constructor_method_content .= $element_object->getPHP($element, $class_object);
66 }
67 }
68
69 foreach ($stashed_sequence_flows as $element) {
70 $element_object = $loader->load($element['name']);
71 $constructor_method_content .= $element_object->getPHP($element, $class_object);
72 }
73
74 foreach ($stashed_associations as $element) {
75 $element_object = $loader->load($element['name']);
76 $constructor_method_content .= $element_object->getPHP($element, $class_object);
77 }
78 }
79
80 if (count($messages)) {
81 $message_definitions = array();
82 foreach ($messages as $message) {
83 $element_object = $loader->load('messageDefinition');
84 $message_definitions[] = $element_object->getMessageDefinitionArray($message);
85 }
86
87 $code = '
88 public static function getMessageDefinition($id)
89 {
90 $definitions = array(' . implode(',', $message_definitions) . '
91 );
92 return $definitions[$id];
93 }
94 ';
95 $class_object->addAuxilliaryMethod($code);
96 }
97
98 $class_object->setConstructorMethodContent($constructor_method_content);
99 $class_source = '';
100
101 if (strlen($constructor_method_content)) {
102 $class_source .= $class_object->getPHP();
103 }
104
105 return "<?php\n" . $class_source . "\n?>"; // PHP Code
106 }
107
113 public function convertXmlToArray($xml)
114 {
115 require_once './Services/WorkflowEngine/classes/parser/class.ilBPMN2ParserUtils.php';
116 $xml_to_array_parser = new ilBPMN2ParserUtils();
117 $bpmn2 = $xml_to_array_parser->load_string($xml);
118 return $bpmn2;
119 }
120
126 public function getProcessNodeFromArray($bpmn2)
127 {
128 $process = array();
129 foreach ((array) @$bpmn2['children'] as $bpmn2_part) {
130 if ($bpmn2_part['name'] == 'process') {
131 $process = $bpmn2_part;
132 break;
133 }
134 }
135 return $process;
136 }
137
143 public function getMessageNodesFromArray($bpmn2)
144 {
145 $messages = array();
146 foreach ((array) @$bpmn2['children'] as $bpmn2_part) {
147 if ($bpmn2_part['name'] == 'message') {
148 $messages[] = $bpmn2_part;
149 break;
150 }
151 }
152 return $messages;
153 }
154
162 public function determineWorkflowClassName($workflow_name, $bpmn2_array, $process)
163 {
164 $hasChildren = (isset($bpmn2_array['children']) && is_array($bpmn2_array['children']) && count($bpmn2_array['children']) > 0);
165 if (!$workflow_name && !$hasChildren) {
166 $workflow_name = $bpmn2_array['attributes']['id'];
167 }
168
169 if (!$workflow_name) {
170 $workflow_name = $process['attributes']['id'];
171 return $workflow_name;
172 }
173
174 if ($workflow_name) {
175 $workflow_name = substr($workflow_name, 0, strpos($workflow_name, '.'));
176 }
177 return $workflow_name;
178 }
179}
An exception for terminatinating execution or to throw for unit testing.
Class ilBPMN2ElementLoader.
Class ilBPMN2ParserUtils.
Class ilWorkflowScaffold.
$messages
Definition: en.php:5
$code
Definition: example_050.php:99
catch(Exception $e) $message