/
Edge.php
301 lines (266 loc) · 7.63 KB
/
Edge.php
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
<?php declare(strict_types=1);
/*
* This file is part of the Pho package.
*
* (c) Emre Sokullu <emre@phonetworks.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Pho\Lib\Graph;
/**
* Atomic graph entity, Edge
*
* Edges (aka lines or arcs in graph theory) are used to
* represent the relationships between Nodes of a Graph
* therefore it is a fundamental unit of
* which graphs are formed.
*
* Uses Observer Pattern to observe updates from its attribute bags,
* as well as tail nodes.
*
* @author Emre Sokullu <emre@phonetworks.org>
*/
class Edge implements
EntityInterface,
EntityWorkerInterface,
EdgeInterface,
HookableInterface,
\Serializable,
Event\EmitterInterface
{
use SerializableTrait;
use Event\EmitterTrait;
use EntityTrait {
EntityTrait::__construct as ____construct;
EntityTrait::init as __init;
}
use HookableTrait;
/**
* Tail node. Where this originates from.
*
* @var TailNode
*/
protected $tail;
/**
* The ID of the tail node.
*
* @var string
*/
protected $tail_id;
/**
* Head node. Where this is directed towards.
*
* @var HeadNode
*/
protected $head;
/**
* The ID of the head node.
*
* @var string
*/
protected $head_id = "";
/**
* Predicate.
*
* @var PredicateInterface
*/
protected $predicate;
/**
* Predicate's label
*
* @var string
*/
protected $predicate_label;
/**
* Constructor.
*
* @param NodeInterface $tail The node where this edge originates from.
* @param ?NodeInterface $head The node where this edge is targeted at. Default: null.
* @param ?PredicateInterface $predicate The predicate of this edge. Default: null.
*
* @throws Exceptions\DuplicateEdgeException when it's not a multiplicable edge and there's an attempt to create multiple edges between a particular pair of head and tail nodes.
*/
public function __construct(NodeInterface $tail, ?NodeInterface $head = null, ?PredicateInterface $predicate = null)
{
$this->predicate = $this->resolvePredicate($predicate, Predicate::class);
if( !$this->predicate->multiplicable()
&& !is_null($head)
&& $tail->edges()->to($head->id(), get_class($this))->count() != 0
) {
throw new Exceptions\DuplicateEdgeException($tail, $head, get_class($this));
}
$this->____construct();
if(!is_null($head)) {
$this->head = new HeadNode();
$this->head->set($head);
$this->head_id = (string) $head->id();
}
$this->tail = new TailNode();
$this->tail->set($tail);
$this->tail_id = (string) $tail->id();
if(!is_null($head)) {
$this->head->edges()->addIncoming($this);
$this->tail->edges()->addOutgoing($this);
}
$this->predicate_label = (string) $this->predicate;
$this->tail->emit("edge.created", [$this]);
if(!is_null($head)) {
$this->head->emit("edge.connected", [$this]);
}
$this->init();
}
public function init(): void
{
$this->tail()->node()->on("deleting", [$this, "observeTailDestruction"]);
$this->__init();
}
/**
* Resolves the predicate of this class.
*
* The predicate may be given. Or it may be resolved by the name of this class. Or it may be given
* a fallback. As a last resort, it may use the Predicate class available in pho-lib-graph.
*
* @param PredicateInterface|null $predicate Predicate may be given.
* @param string $fallback or the fallback class may be given, asking the method find something more specific if available.
*
* @return PredicateInterface A predicate object.
*/
protected function resolvePredicate(?PredicateInterface $predicate, string $fallback): PredicateInterface
{
$is_a_predicate = function(string $class_name): bool
{
if(!class_exists($class_name))
return false;
$reflector = new \ReflectionClass($class_name);
return $reflector->implementsInterface(PredicateInterface::class);
};
if(!is_null($predicate) && $is_a_predicate(get_class($predicate)) ) {
return $predicate;
}
else {
$predicate_class = get_class($this)."Predicate";
if($is_a_predicate($predicate_class)) {
return new $predicate_class;
}
else if($is_a_predicate($fallback)) {
return new $fallback;
}
else {
return new Predicate();
}
}
}
/**
* {@inheritdoc}
*/
public function connect(NodeInterface $head): void
{
if(!$this->orphan()) {
throw new Exceptions\EdgeAlreadyConnectedException($this, $this->head->node());
}
if( $this->tail->node()->edges()->to($head->id(), get_class($this))->count() != 0 ) {
throw new Exceptions\DuplicateEdgeException($this->tail->node(), $head, get_class($this));
}
$this->head = new HeadNode();
$this->head->set($head);
$this->head_id = (string) $head->id();
$this->head()->edges()->addIncoming($this);
$this->tail()->edges()->addOutgoing($this);
$this->head->emit("edge.connected", [$this]);
}
/**
* {@inheritdoc}
*/
public function head(): NodeInterface
{
if($this->orphan()) {
throw new Exceptions\OrphanEdgeException($this);
}
if(isset($this->head)) {
return $this->head;
}
return $this->hookable();
}
/**
* {@inheritdoc}
*/
public function headID(): ID
{
if($this->orphan()) {
throw new Exceptions\OrphanEdgeException($this);
}
return ID::fromString($this->head_id);
}
/**
* {@inheritdoc}
*/
public function tail(): NodeInterface
{
if(isset($this->tail)) {
return $this->tail;
}
return $this->hookable();
}
/**
* {@inheritdoc}
*/
public function tailID(): ID
{
return ID::fromString($this->tail_id);
}
/**
* {@inheritdoc}
*/
public function predicate(): PredicateInterface
{
if(isset($this->predicate)) {
return $this->predicate;
}
return $this->hookable();
}
/**
* {@inheritdoc}
*/
public function orphan(): bool
{
return empty($this->head_id);
}
/**
* Tail Nodes use this method to update about deletion
*
* @return void
*/
public function observeTailDestruction(): void
{
if($this->predicate()->binding()) {
$this->head()->destroy();
}
$this->destroy();
}
/**
* {@inheritdoc}
*/
public function toArray(): array
{
$array = $this->entityToArray();
$array["tail"] = $this->tail_id;
$array["head"] = $this->head_id;
$array["predicate"] = $this->predicate_label;
return $array;
}
/**
* {@inheritdoc}
*/
public function return(): EntityInterface
{
switch($this->predicate()->role()) {
case Predicate::R_CONSUMER:
return $this->head()->node();
case Predicate::R_REFLECTIVE:
return $this->tail()->node();
default:
return $this;
}
}
}