1
|
|
|
<?php |
2
|
|
|
namespace Algorithms\GraphTools; |
3
|
|
|
|
4
|
|
|
class Creator |
5
|
|
|
{ |
6
|
|
|
private $points = array(); |
7
|
|
|
private $labels = array(); |
8
|
|
|
|
9
|
|
|
private static function checkPointID($point) |
10
|
|
|
{ |
11
|
|
|
if (is_int($point) && ($point > 0)) { |
12
|
|
|
return 'int'; |
13
|
|
|
} else { |
14
|
|
|
return 'string'; |
15
|
|
|
} |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
private function randNewID() |
19
|
|
|
{ |
20
|
|
|
do { |
21
|
|
|
$rand = mt_rand(); |
22
|
|
|
} while (isset($this->points[$rand])); |
23
|
|
|
|
24
|
|
|
return $rand; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
private function pointNotExists($point_id) |
28
|
|
|
{ |
29
|
|
|
if (!isset($this->points[$point_id])) { |
30
|
|
|
return true; |
31
|
|
|
} else { |
32
|
|
|
throw new CreatorException('Point was added earlier'); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
private function labelNotExists($label) |
37
|
|
|
{ |
38
|
|
|
if ($label != null) { |
39
|
|
|
if (!isset($this->labels[$label])) { |
40
|
|
|
return true; |
41
|
|
|
} else { |
42
|
|
|
throw new CreatorException('Label exists! Must be unique'); |
43
|
|
|
} |
44
|
|
|
} else { |
45
|
|
|
return true; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function createPointFromID($point_id, $label = null) |
50
|
|
|
{ |
51
|
|
|
if ($this->pointNotExists($point_id) && $this->labelNotExists($label)) { |
52
|
|
|
if (empty($label)) { |
53
|
|
|
$new_point = Point::create($point_id); |
54
|
|
|
} else { |
55
|
|
|
$this->labels[$label] = $point_id; |
56
|
|
|
$new_point = Point::create($point_id, $label); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->points[$point_id] = $new_point; |
60
|
|
|
|
61
|
|
|
return $new_point; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function addPoint($point) |
66
|
|
|
{ |
67
|
|
|
if (Point::checkPoint($point)) { |
68
|
|
|
return $this->addPointObject($point); |
69
|
|
|
} elseif (self::checkPointID($point) == 'int') { |
70
|
|
|
return self::createPointFromID($point); |
71
|
|
|
} else { |
72
|
|
|
return self::createPointFromID($this->randNewID(), $point); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function addPointObject($object) |
77
|
|
|
{ |
78
|
|
|
$this->createPointFromID($object->getID(), $object->getLabel()); |
79
|
|
|
|
80
|
|
|
return $object; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getPoint($point_id) |
84
|
|
|
{ |
85
|
|
|
try { |
86
|
|
|
return $this->getPointOrFail($point_id); |
87
|
|
|
} catch (PointException $e) { |
88
|
|
|
return NULL; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getPointOrFail($point_id) |
93
|
|
|
{ |
94
|
|
|
if (isset($this->points[$point_id])) { |
95
|
|
|
return $this->points[$point_id]; |
96
|
|
|
} elseif (isset($this->labels[$point_id])) { |
97
|
|
|
return $this->points[$this->labels[$point_id]]; |
98
|
|
|
} else { |
99
|
|
|
throw new PointException('Point not exists'); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function createConnections() |
104
|
|
|
{ |
105
|
|
|
$relations = array(); |
106
|
|
|
|
107
|
|
|
foreach ($this->points as $point_id => $point) { |
108
|
|
|
$relations[$point_id] = $point->getDinstances(); |
109
|
|
|
|
110
|
|
|
foreach ($relations[$point_id] as $array_to_analyze) { |
111
|
|
|
if (!isset($relations[$array_to_analyze[0]])) { |
112
|
|
|
$relations[$array_to_analyze[0]] = array(); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $relations; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|