1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Stratadox\GraphpFinder; |
4
|
|
|
|
5
|
|
|
use Fhaculty\Graph\Edge\Base as Edge; |
6
|
|
|
use Fhaculty\Graph\Exception\OutOfBoundsException; |
7
|
|
|
use Fhaculty\Graph\Graph; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Stratadox\Pathfinder\Graph\Ids; |
10
|
|
|
use Stratadox\Pathfinder\Labels; |
11
|
|
|
use Stratadox\Pathfinder\Network; |
12
|
|
|
|
13
|
|
|
final class AdaptedNetwork implements Network |
14
|
|
|
{ |
15
|
|
|
private $graph; |
16
|
|
|
|
17
|
|
|
private function __construct(Graph $graph) |
18
|
|
|
{ |
19
|
|
|
$this->graph = $graph; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function from(Graph $graph): Network |
23
|
|
|
{ |
24
|
|
|
return new self($graph); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function all(): Labels |
28
|
|
|
{ |
29
|
|
|
return Ids::consistingOf(...$this->graph->getVertices()->getIds()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function neighboursOf(string $node): Labels |
33
|
|
|
{ |
34
|
|
|
$neighbours = []; |
35
|
|
|
try { |
36
|
|
|
$start = $this->graph->getVertices()->getVertexId($node); |
37
|
|
|
/** @var Edge $edge */ |
38
|
|
|
foreach ($start->getEdgesOut() as $edge) { |
39
|
|
|
$neighbours[] = (string) $edge->getVertexToFrom($start)->getId(); |
40
|
|
|
} |
41
|
|
|
} catch (OutOfBoundsException $e) { |
42
|
|
|
// no node, no neighbours |
43
|
|
|
} |
44
|
|
|
return Ids::consistingOf(...$neighbours); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function areNeighbours(string $source, string $neighbour): bool |
48
|
|
|
{ |
49
|
|
|
try { |
50
|
|
|
$start = $this->graph->getVertices()->getVertexId($source); |
51
|
|
|
} catch (OutOfBoundsException $e) { |
52
|
|
|
// no source node, no neighbours |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
/** @var Edge $edge */ |
56
|
|
|
foreach ($start->getEdgesOut() as $edge) { |
57
|
|
|
if ((string) $edge->getVertexToFrom($start)->getId() === $neighbour) { |
58
|
|
|
return true; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function has(string $node): bool |
65
|
|
|
{ |
66
|
|
|
return $this->graph->hasVertex($node); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function movementCostBetween( |
70
|
|
|
string $source, |
71
|
|
|
string $neighbour |
72
|
|
|
): float { |
73
|
|
|
try { |
74
|
|
|
$start = $this->graph->getVertices()->getVertexId($source); |
75
|
|
|
} catch (OutOfBoundsException $e) { |
76
|
|
|
throw new InvalidArgumentException("Vertex $source not found."); |
77
|
|
|
} |
78
|
|
|
/** @var Edge $edge */ |
79
|
|
|
foreach ($start->getEdgesOut() as $edge) { |
80
|
|
|
if ((string) $edge->getVertexToFrom($start)->getId() === $neighbour) { |
81
|
|
|
return (float) $edge->getWeight(); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
throw new InvalidArgumentException( |
85
|
|
|
"Vertices $source and $neighbour are not neighbours." |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function hasNegativeEdgeCosts(): bool |
90
|
|
|
{ |
91
|
|
|
/** @var Edge $edge */ |
92
|
|
|
foreach ($this->graph->getEdges() as $edge) { |
93
|
|
|
if ($edge->getWeight() < 0) { |
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|