1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the prooph/php-ddd-cargo-sample. |
4
|
|
|
* (c) Alexander Miertsch <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* Date: 01.03.14 - 22:35 |
10
|
|
|
*/ |
11
|
|
|
declare(strict_types = 1); |
12
|
|
|
|
13
|
|
|
namespace Codeliner\GraphTraversalBackend; |
14
|
|
|
|
15
|
|
|
use Codeliner\GraphTraversalBackend\Dto\EdgeDto; |
16
|
|
|
use Codeliner\GraphTraversalBackend\Dto\TransitPathDto; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class GraphTraversalService |
20
|
|
|
* |
21
|
|
|
* The GraphTraversalService takes a RouteSpecification, that describes the start location and the end location of a cargo |
22
|
|
|
* and fetches compatible routes that are described by Itineraries. |
23
|
|
|
* |
24
|
|
|
* @package Application\Service |
25
|
|
|
* @author Alexander Miertsch <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class GraphTraversalService implements GraphTraversalServiceInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $routes = array(); |
33
|
|
|
|
34
|
|
|
public function __construct(array $aRouteList) |
35
|
|
|
{ |
36
|
|
|
$this->routes = $aRouteList; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $fromUnLocode |
41
|
|
|
* @param string $toUnLocode |
42
|
|
|
* @return TransitPathDto[] |
43
|
|
|
*/ |
44
|
|
|
public function findShortestPath($fromUnLocode, $toUnLocode): array |
45
|
|
|
{ |
46
|
|
|
$routes = \array_filter($this->routes, function($route) use ($fromUnLocode, $toUnLocode) { |
47
|
|
|
return $route['origin'] === $fromUnLocode && $route['destination'] === $toUnLocode; |
48
|
|
|
}); |
49
|
|
|
|
50
|
|
|
$transitPaths = array(); |
51
|
|
|
|
52
|
|
|
foreach($routes as $route) { |
53
|
|
|
$transitPaths[] = $this->routeToTransitPath($route); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $transitPaths; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param array $route |
63
|
|
|
* @return TransitPathDto |
64
|
|
|
*/ |
65
|
|
|
private function routeToTransitPath(array $route): TransitPathDto |
66
|
|
|
{ |
67
|
|
|
$edges = array(); |
68
|
|
|
|
69
|
|
|
$loadDay = \rand(1,4); |
70
|
|
|
|
71
|
|
|
$loadTimestamp = strtotime("+$loadDay day"); |
72
|
|
|
|
73
|
|
|
$loadTime = new \DateTime(); |
74
|
|
|
$loadTime->setTimestamp($loadTimestamp); |
75
|
|
|
|
76
|
|
|
$elapsedDays = 0; |
77
|
|
|
|
78
|
|
|
$currentLocation = $route['origin']; |
79
|
|
|
$currentTime = $loadTime; |
80
|
|
|
|
81
|
|
|
if (!empty($route['stops'])) { |
82
|
|
|
foreach($route['stops'] as $unLocode => $duration) { |
83
|
|
|
$elapsedDays += $duration; |
84
|
|
|
|
85
|
|
|
$durationInterval = new \DateInterval('P' . $duration . 'DT' . \rand(1, 12) . 'H' . \rand(1, 60) . 'M'); |
86
|
|
|
|
87
|
|
|
$currentTime->add(new \DateInterval('P1DT' . \rand(1, 12) . 'H' . \rand(1, 60) . 'M')); |
88
|
|
|
|
89
|
|
|
$loadTime = clone $currentTime; |
90
|
|
|
|
91
|
|
|
$currentTime->add($durationInterval); |
92
|
|
|
|
93
|
|
|
$unloadTime = clone $currentTime; |
94
|
|
|
|
95
|
|
|
$edge = new EdgeDto(); |
96
|
|
|
|
97
|
|
|
$edge->setFromUnLocode($currentLocation); |
98
|
|
|
$edge->setToUnLocode($unLocode); |
99
|
|
|
$edge->setFromDate($loadTime->format(\DateTime::ATOM)); |
100
|
|
|
$edge->setToDate($unloadTime->format(\DateTime::ATOM)); |
101
|
|
|
|
102
|
|
|
$edges[] = $edge; |
103
|
|
|
|
104
|
|
|
$currentLocation = $unLocode; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$destinationDuration = $route['duration'] - $elapsedDays; |
109
|
|
|
$durationInterval = new \DateInterval('P' . $destinationDuration . 'DT' . \rand(1, 12) . 'H' . \rand(1, 60) . 'M'); |
110
|
|
|
|
111
|
|
|
$currentTime->add(new \DateInterval('P1DT' . \rand(1, 12) . 'H' . \rand(1, 60) . 'M')); |
112
|
|
|
|
113
|
|
|
$loadTime = clone $currentTime; |
114
|
|
|
|
115
|
|
|
$currentTime->add($durationInterval); |
116
|
|
|
|
117
|
|
|
$unloadTime = clone $currentTime; |
118
|
|
|
|
119
|
|
|
$edge = new EdgeDto(); |
120
|
|
|
|
121
|
|
|
$edge->setFromUnLocode($currentLocation); |
122
|
|
|
$edge->setToUnLocode($route['destination']); |
123
|
|
|
$edge->setFromDate($loadTime->format(\DateTime::ATOM)); |
124
|
|
|
$edge->setToDate($unloadTime->format(\DateTime::ATOM)); |
125
|
|
|
|
126
|
|
|
$edges[] = $edge; |
127
|
|
|
|
128
|
|
|
$transitPath = new TransitPathDto(); |
129
|
|
|
|
130
|
|
|
$transitPath->setEdges($edges); |
131
|
|
|
|
132
|
|
|
return $transitPath; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|