1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JMGQ\AStar\Benchmark\Result; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Style\StyleInterface; |
6
|
|
|
|
7
|
|
|
class ResultPrinter |
8
|
|
|
{ |
9
|
|
|
private $output; |
10
|
|
|
|
11
|
7 |
|
public function __construct(StyleInterface $output) |
12
|
|
|
{ |
13
|
7 |
|
$this->output = $output; |
14
|
7 |
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param AggregatedResult[] $results |
18
|
|
|
*/ |
19
|
7 |
|
public function display(array $results) |
20
|
|
|
{ |
21
|
7 |
|
$tableRows = array(); |
22
|
|
|
|
23
|
7 |
|
$orderedResults = $this->orderResults($results); |
24
|
|
|
|
25
|
7 |
|
foreach ($orderedResults as $result) { |
26
|
6 |
|
$size = $result->getSize() . 'x' . $result->getSize(); |
27
|
6 |
|
$averageDuration = $result->getAverageDuration() . 'ms'; |
28
|
6 |
|
$minimumDuration = $result->getMinimumDuration() . 'ms'; |
29
|
6 |
|
$maximumDuration = $result->getMaximumDuration() . 'ms'; |
30
|
6 |
|
$solutionFound = $this->formatSolutionFound($result); |
31
|
|
|
|
32
|
6 |
|
$tableRows[] = array($size, $averageDuration, $minimumDuration, $maximumDuration, $solutionFound); |
33
|
|
|
} |
34
|
|
|
|
35
|
7 |
|
$tableHeaders = array('Size', 'Avg Duration', 'Min Duration', 'Max Duration', 'Solved?'); |
36
|
|
|
|
37
|
7 |
|
$this->output->table($tableHeaders, $tableRows); |
38
|
7 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param AggregatedResult[] $results |
42
|
|
|
* @return AggregatedResult[] |
43
|
|
|
*/ |
44
|
|
|
private function orderResults(array $results) |
45
|
|
|
{ |
46
|
|
|
// Suppressing the errors thrown by usort due to a PHP5 bug: |
47
|
|
|
// https://bugs.php.net/bug.php?id=50688 |
48
|
|
|
// https://stackoverflow.com/q/3235387 |
49
|
7 |
|
@usort($results, function ($a, $b) { |
|
|
|
|
50
|
1 |
|
return $a->getSize() > $b->getSize(); |
51
|
7 |
|
}); |
52
|
|
|
|
53
|
7 |
|
return $results; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param AggregatedResult $result |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
6 |
|
private function formatSolutionFound($result) |
61
|
|
|
{ |
62
|
6 |
|
$allResultsAreSolved = $result->getNumberOfSolutions() === $result->getNumberOfTerrains(); |
63
|
6 |
|
if ($allResultsAreSolved) { |
64
|
4 |
|
return 'Yes'; |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
$allResultsAreUnsolved = $result->getNumberOfSolutions() === 0; |
68
|
2 |
|
if ($allResultsAreUnsolved) { |
69
|
1 |
|
return 'No'; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
return 'Sometimes'; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: