-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.php
121 lines (94 loc) · 2.72 KB
/
main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env php
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
include 'util.php';
include 'solver.php';
include 'reader.php';
class CliLogger implements Logger
{
private $threshold;
public function __construct($threshold)
{
$this->threshold = $threshold;
}
public function write($format, $arguments, $level)
{
if ($level >= $this->threshold)
printf("DEBUG: %s\n\n", vsprintf($format, $arguments));
}
}
function usage($path)
{
echo "Usage: $path knowledge.xml [goal]";
exit;
}
function main($argc, $argv)
{
if ($argc < 2 || $argc > 3)
usage($argv[0]);
// Als '-vN' is meegegeven tijdens het starten, ga in verbose mode
if (preg_match('/^-v(\d?)$/', $argv[1], $match))
{
$logger = new CliLogger($match[1] ? intval($match[1]) : LOG_LEVEL_INFO);
$argc--;
array_shift($argv);
}
else
$logger = null;
// Reader voor de XML-bestanden
$reader = new KnowledgeBaseReader();
// Parse een xml-bestand (het eerste argument) tot knowledge base
$state = $reader->parse($argv[1]);
// Start de solver, dat ding dat kan infereren
$solver = new Solver($logger);
// leid alle goals in de knowledge base af.
$goals = $state->goals;
// Begin met de doelen die we hebben op de goal stack te zetten
foreach($goals as $goal)
{
$state->goalStack->push($goal->name);
// Also push any answer values that are variables as goals to be solved.
foreach ($goal->answers as $answer)
if (KnowledgeState::is_variable($answer->value))
$state->goalStack->push(KnowledgeState::variable_name($answer->value));
}
// Zo lang we nog vragen kunnen stellen, stel ze
while (($question = $solver->solveAll($state)) instanceof AskedQuestion)
{
$answer = cli_ask($question);
if ($answer instanceof Option)
$state->apply($answer->consequences,
Yes::because("User answered '{$answer->description}' to '{$question->description}'"));
}
// Geen vragen meer, print de gevonden oplossingen.
foreach ($goals as $goal)
{
printf("%s: %s\n",
$goal->description,
$state->substitute_variables($goal->answer($state)->description));
}
}
/**
* Stelt een vraag op de terminal, en blijf net zo lang wachten totdat
* we een zinnig antwoord krijgen.
*
* @return Option
*/
function cli_ask(Question $question)
{
echo $question->description . "\n";
for ($i = 0; $i < count($question->options); ++$i)
printf("%2d) %s\n", $i + 1, $question->options[$i]->description);
if ($question->skippable)
printf("%2d) weet ik niet\n", ++$i);
do {
$response = fgetc(STDIN);
$choice = @intval(trim($response));
if ($choice > 0 && $choice <= count($question->options))
return $question->options[$choice - 1];
if ($question->skippable && $choice == $i)
return null;
} while (true);
}
main($argc, $argv);