forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallActivity.php
More file actions
196 lines (181 loc) · 6.99 KB
/
CallActivity.php
File metadata and controls
196 lines (181 loc) · 6.99 KB
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
<?php
namespace ProcessMaker\Models;
use ProcessMaker\Nayra\Bpmn\ActivitySubProcessTrait;
use ProcessMaker\Nayra\Bpmn\Events\ActivityActivatedEvent;
use ProcessMaker\Nayra\Bpmn\Events\ActivityClosedEvent;
use ProcessMaker\Nayra\Bpmn\Events\ActivityCompletedEvent;
use ProcessMaker\Nayra\Contracts\Bpmn\ActivityInterface;
use ProcessMaker\Nayra\Contracts\Bpmn\CallActivityInterface;
use ProcessMaker\Nayra\Contracts\Bpmn\TokenInterface;
use ProcessMaker\Nayra\Contracts\Engine\ExecutionInstanceInterface;
use ProcessMaker\Nayra\Contracts\Bpmn\FlowInterface;
use ProcessMaker\Nayra\Contracts\Bpmn\ErrorInterface;
use ProcessMaker\Nayra\Contracts\Bpmn\CallableElementInterface;
/**
* Call Activity model
*
* @package ProcessMaker\Model
*/
class CallActivity implements CallActivityInterface
{
use ActivitySubProcessTrait {
addToken as addTokenBase;
completeSubprocess as completeSubprocessBase;
catchSubprocessError as catchSubprocessErrorBase;
}
/**
* Configure the activity to go to a FAILING status when activated.
*
*/
protected function initActivity()
{
$this->attachEvent(
ActivityInterface::EVENT_ACTIVITY_ACTIVATED,
function ($self, TokenInterface $token, FlowInterface $sequenceFlow) {
$instance = $this->callSubprocess($token, $sequenceFlow);
$this->getRepository()
->getTokenRepository()
->persistCallActivityActivated($token, $instance, $sequenceFlow);
$this->linkProcesses($token, $instance);
$this->syncronizeInstances($token->getInstance(), $instance);
}
);
}
/**
* Call the subprocess
*
* @return ExecutionInstanceInterface
*/
protected function callSubprocess(TokenInterface $token, FlowInterface $sequenceFlow)
{
$callable = $this->getCalledElement();
// Capability to specify the target start event on the sequence flow to the call activity.
$startId = $sequenceFlow->getProperty('startEvent');
$startEvent = $startId ? $callable->getEngine()->getStorage()->getElementInstanceById($startId) : null;
$dataStore = $callable->getRepository()->createDataStore();
// The entire data model is sent to the target
$data = $token->getInstance()->getDataStore()->getData();
// Add info about parent
$data['_parent'] = [
'process_id' => $token->getInstance()->process_id,
'request_id' => $token->getInstance()->id,
'node_id' => $token->element_id,
];
$configString = $this->getProperty('config');
if ($configString) {
$data['_parent']['config'] = json_decode($configString, true);
}
$dataStore->setData($data);
$instance = $callable->call($dataStore, $startEvent);
return $instance;
}
/**
* Complete the subprocess
*
* @param TokenInterface $token
* @param ExecutionInstanceInterface $closedInstance
* @param ExecutionInstanceInterface $instance
*
* @return CallActivity
*/
protected function completeSubprocess(TokenInterface $token, ExecutionInstanceInterface $closedInstance, ExecutionInstanceInterface $instance)
{
$this->completeSubprocessBase($token);
// Copy data from subprocess to main process
$dataStore = $token->getInstance()->getDataStore();
$data = $closedInstance->getDataStore()->getData();
foreach ($data as $key => $value) {
$dataStore->putData($key, $value);
}
$this->syncronizeInstances($instance, $token->getInstance());
return $this;
}
/**
* Catch a subprocess error
*
* @param TokenInterface $token
* @param ErrorInterface|null $error
* @param ExecutionInstanceInterface $instance
*
* @return CallActivity
*/
protected function catchSubprocessError(TokenInterface $token, ErrorInterface $error = null, ExecutionInstanceInterface $instance)
{
$this->catchSubprocessErrorBase($token, $error);
$this->syncronizeInstances($instance, $token->getInstance());
return $this;
}
/**
* Array map of custom event classes for the bpmn element.
*
* @return array
*/
protected function getBpmnEventClasses()
{
return [
ActivityInterface::EVENT_ACTIVITY_ACTIVATED => ActivityActivatedEvent::class,
ActivityInterface::EVENT_ACTIVITY_COMPLETED => ActivityCompletedEvent::class,
ActivityInterface::EVENT_ACTIVITY_CLOSED => ActivityClosedEvent::class,
];
}
/**
* Get the called element by the activity.
*
* @return \ProcessMaker\Nayra\Contracts\Bpmn\CallableElementInterface
*/
public function getCalledElement()
{
$calledElementRef = $this->getProperty(CallActivityInterface::BPMN_PROPERTY_CALLED_ELEMENT);
$refs = explode('-', $calledElementRef);
if (count($refs) === 1) {
$localBpmn = $this->ownerProcess->getEngine()->getStorage();
return $localBpmn->getElementInstanceById($calledElementRef);
} elseif (count($refs) === 2) {
// Capability to reuse other processes inside a process
$process = Process::findOrFail($refs[1]);
$engine = $this->getProcess()->getEngine();
return isset($engine->currentInstance) ? $engine->currentInstance->getProcess()
: $process->getDefinitions(false, $engine)->getElementInstanceById($refs[0]);
}
}
/**
* Set the called element by the activity.
*
* @param \ProcessMaker\Nayra\Contracts\Bpmn\CallableElementInterface|string $callableElement
*
* @return $this
*/
public function setCalledElement($callableElement)
{
$this->setProperty(CallActivityInterface::BPMN_PROPERTY_CALLED_ELEMENT, $callableElement);
return $this;
}
/**
* Load tokens from array. And Link to the subprocess if exists.
*
* @param ExecutionInstanceInterface $instance
* @param TokenInterface $token
*
* @return $this
*/
public function addToken(ExecutionInstanceInterface $instance, TokenInterface $token)
{
if ($token->getStatus() === ActivityInterface::TOKEN_STATE_ACTIVE && !empty($token->subprocess_request_id)) {
$subprocess = $this->getProcess()->getEngine()->loadProcessRequest($token->subProcessRequest);
$this->linkProcesses($token, $subprocess);
}
return $this->addTokenBase($instance, $token);
}
/**
* Syncronize two process instances
*
* @param ExecutionInstanceInterface $instance
* @param ExecutionInstanceInterface $currentInstance
*/
private function syncronizeInstances(ExecutionInstanceInterface $instance, ExecutionInstanceInterface $currentInstance)
{
if ($instance->process->id !== $currentInstance->process->id) {
$currentInstance->getProcess()->getEngine()->runToNextState();
}
}
}