|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Scientist; |
|
4
|
|
|
|
|
5
|
|
|
use Scientist\Matchers\Matcher; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Intern |
|
9
|
|
|
* |
|
10
|
|
|
* Interns do all the hard work, naturally. They'll execute an experiment within |
|
11
|
|
|
* the Laboratory, and record the results. |
|
12
|
|
|
* |
|
13
|
|
|
* @package \Scientist |
|
14
|
|
|
*/ |
|
15
|
|
|
class Intern |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var bool */ |
|
18
|
|
|
protected $overreact = false; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Exceptions will be thrown from experiments |
|
22
|
|
|
*/ |
|
23
|
1 |
|
public function overreact() |
|
24
|
|
|
{ |
|
25
|
1 |
|
$this->overreact = true; |
|
26
|
1 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Run an experiment, and retrieve the result. |
|
30
|
|
|
* |
|
31
|
|
|
* @param \Scientist\Experiment $experiment |
|
32
|
|
|
* |
|
33
|
|
|
* @return \Scientist\Report |
|
34
|
|
|
*/ |
|
35
|
12 |
|
public function run(Experiment $experiment) |
|
36
|
|
|
{ |
|
37
|
12 |
|
$control = $this->runControl($experiment); |
|
38
|
11 |
|
$trials = $this->runTrials($experiment); |
|
39
|
|
|
|
|
40
|
10 |
|
$this->determineMatches($experiment->getMatcher(), $control, $trials); |
|
41
|
|
|
|
|
42
|
10 |
|
return new Report($experiment->getName(), $control, $trials); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Run the control callback, and record its execution state. |
|
47
|
|
|
* |
|
48
|
|
|
* @param \Scientist\Experiment $experiment |
|
49
|
|
|
* |
|
50
|
|
|
* @return \Scientist\Result |
|
51
|
|
|
*/ |
|
52
|
12 |
|
protected function runControl(Experiment $experiment) |
|
53
|
|
|
{ |
|
54
|
12 |
|
return (new Machine($experiment->getControl(), $experiment->getParams()))->execute(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Run trial callbacks and record their execution state. |
|
59
|
|
|
* |
|
60
|
|
|
* @param \Scientist\Experiment $experiment |
|
61
|
|
|
* |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
11 |
|
protected function runTrials(Experiment $experiment) |
|
65
|
|
|
{ |
|
66
|
11 |
|
$executions = []; |
|
67
|
|
|
|
|
68
|
11 |
|
foreach ($experiment->getTrials() as $name => $trial) { |
|
69
|
10 |
|
$executions[$name] = (new Machine( |
|
70
|
10 |
|
$trial, |
|
71
|
10 |
|
$experiment->getParams(), |
|
72
|
10 |
|
(! $this->overreact) |
|
73
|
10 |
|
))->execute(); |
|
74
|
10 |
|
} |
|
75
|
|
|
|
|
76
|
10 |
|
return $executions; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Determine whether trial results match the control. |
|
81
|
|
|
* |
|
82
|
|
|
* @param \Scientist\Matchers\Matcher $matcher |
|
83
|
|
|
* @param \Scientist\Result $control |
|
84
|
|
|
* @param array $trials |
|
85
|
|
|
*/ |
|
86
|
10 |
|
protected function determineMatches(Matcher $matcher, Result $control, array $trials = []) |
|
87
|
|
|
{ |
|
88
|
10 |
|
foreach ($trials as $trial) { |
|
89
|
9 |
|
if ($matcher->match($control->getValue(), $trial->getValue())) { |
|
90
|
3 |
|
$trial->setMatch(true); |
|
91
|
3 |
|
} |
|
92
|
10 |
|
} |
|
93
|
10 |
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|