|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of Rivescript-php |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Johnny Mast <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Axiom\Rivescript\Cortex\ResponseQueue; |
|
12
|
|
|
|
|
13
|
|
|
use Axiom\Collections\Collection; |
|
14
|
|
|
use Axiom\Rivescript\Cortex\Node; |
|
15
|
|
|
use Axiom\Rivescript\Traits\Tags; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* ResponseQueue class |
|
19
|
|
|
* |
|
20
|
|
|
* The ResponseQueue releases the responses in order of sending them |
|
21
|
|
|
* back to the user. |
|
22
|
|
|
* |
|
23
|
|
|
* PHP version 7.4 and higher. |
|
24
|
|
|
* |
|
25
|
|
|
* @category Core |
|
26
|
|
|
* @package Cortext\ResponseQueue |
|
27
|
|
|
* @author Johnny Mast <[email protected]> |
|
28
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
|
29
|
|
|
* @link https://github.com/axiom-labs/rivescript-php |
|
30
|
|
|
* @since 0.4.0 |
|
31
|
|
|
*/ |
|
32
|
|
|
class ResponseQueue extends Collection |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
use Tags; |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* A container with responses. |
|
39
|
|
|
* |
|
40
|
|
|
* @var Collection<ResponseQueueItem> |
|
41
|
|
|
*/ |
|
42
|
|
|
protected Collection $responses; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Store the local interpreter options |
|
46
|
|
|
* for this instance in time (since they can change). |
|
47
|
|
|
* |
|
48
|
|
|
* @var array<string, string> |
|
49
|
|
|
*/ |
|
50
|
|
|
protected array $options = []; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The trigger string this ResponseQueue belongs to. |
|
54
|
|
|
* |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected string $trigger = ""; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* ResponseQueue constructor. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $trigger the trigger this queue belongs to. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __construct(string $trigger = "") |
|
65
|
|
|
{ |
|
66
|
|
|
parent::__construct(); |
|
67
|
|
|
|
|
68
|
|
|
$this->responses = new Collection([]); |
|
69
|
|
|
$this->trigger = $trigger; |
|
70
|
|
|
|
|
71
|
|
|
$this->options = synapse()->memory->local()->all(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Attach a response to the queue. |
|
76
|
|
|
* |
|
77
|
|
|
* @param Node $node The node contains information about the command. |
|
78
|
|
|
* |
|
79
|
|
|
* @return void |
|
80
|
|
|
*/ |
|
81
|
|
|
public function attach(Node $node): void |
|
82
|
|
|
{ |
|
83
|
|
|
$type = $this->determineResponseType($node->source()); |
|
84
|
|
|
$queueItem = new ResponseQueueItem($node->command(), $type, 0, $this->options); |
|
85
|
|
|
$this->responses->put($node->value(), $queueItem); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Sort the responses by order. |
|
90
|
|
|
* |
|
91
|
|
|
* @param Collection<ResponseQueueItem> $responses The array containing the resources. |
|
92
|
|
|
* |
|
93
|
|
|
* @return Collection<ResponseQueueItem> |
|
94
|
|
|
*/ |
|
95
|
|
|
private function sortResponses(Collection $responses): Collection |
|
96
|
|
|
{ |
|
97
|
|
|
return $responses->sort( |
|
98
|
|
|
function ($current, $previous) { |
|
99
|
|
|
return ($current->order < $previous->order) ? -1 : 1; |
|
100
|
|
|
} |
|
101
|
|
|
)->reverse(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Check if a response is allowed to be returned by the bot or not. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $response The response to validate. |
|
108
|
|
|
* @param ResponseQueueItem $item The ResponseQueueItem. |
|
109
|
|
|
* |
|
110
|
|
|
* @return false|mixed |
|
111
|
|
|
*/ |
|
112
|
|
|
private function validateResponse(string $response, ResponseQueueItem $item) |
|
113
|
|
|
{ |
|
114
|
|
|
$response = $this->parseTags($response); |
|
115
|
|
|
$responses = synapse()->responses; |
|
116
|
|
|
|
|
117
|
|
|
foreach ($responses as $class) { |
|
118
|
|
|
if (class_exists("\\Axiom\\Rivescript\\Cortex\\Responses\\{$class}")) { |
|
119
|
|
|
$class = "\\Axiom\\Rivescript\\Cortex\\Responses\\{$class}"; |
|
120
|
|
|
$class = new $class($response, $item); |
|
121
|
|
|
|
|
122
|
|
|
$result = $class->parse(); |
|
123
|
|
|
|
|
124
|
|
|
if ($result !== false) { |
|
125
|
|
|
return $result; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return false; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Merge the ^ continue responses to the last - response. |
|
135
|
|
|
* |
|
136
|
|
|
* @param Collection<ResponseQueueItem> $responses The array containing the responses. |
|
137
|
|
|
* |
|
138
|
|
|
* @return Collection<ResponseQueueItem> |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function concatContinues(Collection $responses): Collection |
|
141
|
|
|
{ |
|
142
|
|
|
$lastData = $responses->first(); |
|
143
|
|
|
$lastResponse = ""; |
|
144
|
|
|
|
|
145
|
|
|
$continues = Collection::make($responses->all()); |
|
146
|
|
|
$continues->each( |
|
147
|
|
|
function (ResponseQueueItem $data, $response) use (&$lastData, &$lastResponse, &$continues) { |
|
148
|
|
|
|
|
149
|
|
|
if ($data->type === 'continue') { |
|
150
|
|
|
$continues->remove($lastResponse); |
|
151
|
|
|
$continues->remove($response); |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* none -- the default, nothing is added when continuation lines are joined together. |
|
155
|
|
|
* space -- continuation lines are joined by a space character (\s) |
|
156
|
|
|
* newline -- continuation lines are joined by a line break character (\n) |
|
157
|
|
|
*/ |
|
158
|
|
|
$options = $lastData->options; |
|
159
|
|
|
$method = $options['concat']; |
|
160
|
|
|
|
|
161
|
|
|
switch ($method) { |
|
162
|
|
|
case 'space': |
|
163
|
|
|
$lastResponse .= " {$response}"; |
|
164
|
|
|
break; |
|
165
|
|
|
|
|
166
|
|
|
case 'newline': |
|
167
|
|
|
$lastResponse .= "\n{$response}"; |
|
168
|
|
|
break; |
|
169
|
|
|
|
|
170
|
|
|
case 'none': |
|
171
|
|
|
default: |
|
172
|
|
|
$lastResponse .= $response; |
|
173
|
|
|
break; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
$continues->put($lastResponse, $lastData); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
if ($data->command !== '^') { |
|
180
|
|
|
$lastData = $data; |
|
181
|
|
|
$lastResponse = $response; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
); |
|
185
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
return $continues; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Determine the order of responses by type. |
|
192
|
|
|
* |
|
193
|
|
|
* @param Collection<ResponseQueueItem> $responses The responses to inspect. |
|
194
|
|
|
* |
|
195
|
|
|
* @return Collection<ResponseQueueItem> |
|
196
|
|
|
*/ |
|
197
|
|
|
private function determineResponseOrder(Collection $responses): Collection |
|
198
|
|
|
{ |
|
199
|
|
|
return $responses->each( |
|
200
|
|
|
function (ResponseQueueItem $data, $response) use ($responses) { |
|
201
|
|
|
if (isset($data->type)) { |
|
202
|
|
|
switch ($data->type) { |
|
203
|
|
|
case 'condition': |
|
204
|
|
|
$data->order += 3000000; |
|
205
|
|
|
break; |
|
206
|
|
|
case 'weighted': |
|
207
|
|
|
case 'atomic': |
|
208
|
|
|
$data->order += 1000000; |
|
209
|
|
|
break; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
$responses->put($response, $data); |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Determine the response type. |
|
220
|
|
|
* |
|
221
|
|
|
* @param string $response |
|
222
|
|
|
* |
|
223
|
|
|
* @return string |
|
224
|
|
|
*/ |
|
225
|
|
|
public function determineResponseType(string $response): string |
|
226
|
|
|
{ |
|
227
|
|
|
$wildcards = [ |
|
228
|
|
|
'weighted' => '{weight=(.+?)}', |
|
229
|
|
|
'condition' => '/^\*/', |
|
230
|
|
|
'continue' => '/^\^/', |
|
231
|
|
|
'atomic' => '/-/', |
|
232
|
|
|
]; |
|
233
|
|
|
|
|
234
|
|
|
foreach ($wildcards as $type => $pattern) { |
|
235
|
|
|
if (@preg_match_all($pattern, $response, $matches)) { |
|
236
|
|
|
return $type; |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
return 'atomic'; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Process the Response Queue. |
|
245
|
|
|
* |
|
246
|
|
|
* @return false|int|string|null |
|
247
|
|
|
*/ |
|
248
|
|
|
public function process() |
|
249
|
|
|
{ |
|
250
|
|
|
$sortedResponses = $this->determineResponseOrder($this->responses); |
|
251
|
|
|
|
|
252
|
|
|
$validResponses = new Collection([]); |
|
253
|
|
|
foreach ($sortedResponses as $response => $item) { |
|
254
|
|
|
$result = $this->validateResponse($response, $item); |
|
255
|
|
|
|
|
256
|
|
|
if ($result !== false) { |
|
257
|
|
|
$validResponses->put($result, $item); |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
$validResponses = $this->concatContinues($validResponses); |
|
262
|
|
|
$validResponses = $this->sortResponses($validResponses); |
|
263
|
|
|
|
|
264
|
|
|
if ($validResponses->count() > 0) { |
|
265
|
|
|
return $validResponses->keys()->first(); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
return false; |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
|