1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Rivescript-php |
4
|
|
|
* |
5
|
|
|
* (c) Shea Lewis <[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; |
12
|
|
|
|
13
|
|
|
use Axiom\Collections\Collection; |
14
|
|
|
use Axiom\Rivescript\Cortex\MiniStack\MiniStack; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Memory class |
18
|
|
|
* |
19
|
|
|
* The memory class stores information about different |
20
|
|
|
* parts of the brain (short or long term). |
21
|
|
|
* |
22
|
|
|
* PHP version 7.4 and higher. |
23
|
|
|
* |
24
|
|
|
* @category Core |
25
|
|
|
* @package Cortext |
26
|
|
|
* @author Shea Lewis <[email protected]> |
27
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
28
|
|
|
* @link https://github.com/axiom-labs/rivescript-php |
29
|
|
|
* @since 0.3.0 |
30
|
|
|
*/ |
31
|
|
|
class Memory |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* A collection of person variables. |
35
|
|
|
* |
36
|
|
|
* @var Collection<string, mixed> |
37
|
|
|
*/ |
38
|
|
|
protected Collection $person; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* A collection of short term memory items. |
42
|
|
|
* |
43
|
|
|
* @var Collection<string, mixed> |
44
|
|
|
*/ |
45
|
|
|
protected Collection $shortTerm; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* A collection of substitutes. |
49
|
|
|
* |
50
|
|
|
* @var Collection<string, mixed> |
51
|
|
|
*/ |
52
|
|
|
protected Collection $substitute; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* A collection of variables. |
56
|
|
|
* |
57
|
|
|
* @var Collection<string, mixed> |
58
|
|
|
*/ |
59
|
|
|
protected Collection $variables; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* A collection of user variables. |
63
|
|
|
* |
64
|
|
|
* @var Collection<string, mixed> |
65
|
|
|
*/ |
66
|
|
|
protected Collection $user; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var Collection<string, mixed> |
70
|
|
|
*/ |
71
|
|
|
protected Collection $global; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var Collection<string, mixed> |
75
|
|
|
*/ |
76
|
|
|
protected Collection $local; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var Collection<string, mixed> |
80
|
|
|
*/ |
81
|
|
|
private Collection $arrays; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* A Collection of the latest Input's |
85
|
|
|
* |
86
|
|
|
* @var MiniStack<int, string> |
87
|
|
|
*/ |
88
|
|
|
protected Collection $inputs; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* A Collection of the latest replies. |
92
|
|
|
* |
93
|
|
|
* @var MiniStack<int, string> |
94
|
|
|
*/ |
95
|
|
|
protected Collection $replies; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* A Collection of the tags. |
99
|
|
|
* |
100
|
|
|
* @var Collection<string, \Axiom\Rivescript\Cortex\Tags\Tag> |
101
|
|
|
*/ |
102
|
|
|
protected $tags; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Create a new Memory instance. |
106
|
|
|
*/ |
107
|
|
|
public function __construct() |
108
|
|
|
{ |
109
|
|
|
$this->shortTerm = Collection::make([]); |
110
|
|
|
$this->substitute = Collection::make([]); |
111
|
|
|
$this->variables = Collection::make([]); |
112
|
|
|
$this->global = Collection::make([]); |
113
|
|
|
$this->tags = Collection::make([]); |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* none -- the default, nothing is added when continuation lines are joined together. |
117
|
|
|
* space -- continuation lines are joined by a space character (\s) |
118
|
|
|
* newline -- continuation lines are joined by a line break character (\n) |
119
|
|
|
*/ |
120
|
|
|
$this->local = Collection::make([ |
121
|
|
|
'concat' => 'none' |
122
|
|
|
]); |
123
|
|
|
|
124
|
|
|
$this->arrays = Collection::make([]); |
125
|
|
|
$this->person = Collection::make([]); |
126
|
|
|
$this->user = Collection::make([]); |
127
|
|
|
$this->inputs = new MiniStack(9); |
128
|
|
|
$this->replies = new MiniStack(9); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Stored global variables. |
133
|
|
|
* |
134
|
|
|
* @return Collection<string, mixed> |
135
|
|
|
*/ |
136
|
|
|
public function global(): Collection |
137
|
|
|
{ |
138
|
|
|
return $this->global; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Stored parser variables. |
143
|
|
|
* |
144
|
|
|
* @return Collection<string, mixed> |
145
|
|
|
*/ |
146
|
|
|
public function local(): Collection |
147
|
|
|
{ |
148
|
|
|
return $this->local; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Stored person variables. |
153
|
|
|
* |
154
|
|
|
* @return Collection<string, mixed> |
155
|
|
|
*/ |
156
|
|
|
public function person(): Collection |
157
|
|
|
{ |
158
|
|
|
return $this->person; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Short-term memory cache. |
163
|
|
|
* |
164
|
|
|
* @return Collection<string, mixed> |
165
|
|
|
*/ |
166
|
|
|
public function shortTerm(): Collection |
167
|
|
|
{ |
168
|
|
|
return $this->shortTerm; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Stored substitute variables. |
173
|
|
|
* |
174
|
|
|
* @return Collection<string, mixed> |
175
|
|
|
*/ |
176
|
|
|
public function substitute(): Collection |
177
|
|
|
{ |
178
|
|
|
return $this->substitute; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Stored variables. |
183
|
|
|
* |
184
|
|
|
* @return Collection<string, mixed> |
185
|
|
|
*/ |
186
|
|
|
public function variables(): Collection |
187
|
|
|
{ |
188
|
|
|
return $this->variables; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Stored variables. |
193
|
|
|
* |
194
|
|
|
* @return Collection<string, mixed> |
195
|
|
|
*/ |
196
|
|
|
public function tags(): Collection |
197
|
|
|
{ |
198
|
|
|
return $this->tags; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Return the latest 9 inputs. |
203
|
|
|
* |
204
|
|
|
* @return MiniStack<int, string> |
205
|
|
|
*/ |
206
|
|
|
public function inputs(): MiniStack |
207
|
|
|
{ |
208
|
|
|
return $this->inputs; |
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Return the latest 9 replies. |
213
|
|
|
* |
214
|
|
|
* @return MiniStack<int, string> |
215
|
|
|
*/ |
216
|
|
|
public function replies(): MiniStack |
217
|
|
|
{ |
218
|
|
|
return $this->replies; |
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Stored arrays. |
223
|
|
|
* |
224
|
|
|
* @return Collection<string, mixed> |
225
|
|
|
*/ |
226
|
|
|
public function arrays(): Collection |
227
|
|
|
{ |
228
|
|
|
return $this->arrays; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Stored user data. |
233
|
|
|
* |
234
|
|
|
* @param string $user The user to store information for. |
235
|
|
|
* |
236
|
|
|
* @return Collection<string, mixed> |
237
|
|
|
*/ |
238
|
|
|
public function user(string $user = 'local-user'): Collection |
239
|
|
|
{ |
240
|
|
|
if (!$this->user->has($user)) { |
241
|
|
|
$data = new Collection([]); |
242
|
|
|
|
243
|
|
|
$this->user->put($user, $data); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return $this->user->get($user); |
|
|
|
|
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|