|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* ****************************************************************************** |
|
5
|
|
|
* Copyright 2011-2017 DANTE Ltd. and GÉANT on behalf of the GN3, GN3+, GN4-1 |
|
6
|
|
|
* and GN4-2 consortia |
|
7
|
|
|
* |
|
8
|
|
|
* License: see the web/copyright.php file in the file structure |
|
9
|
|
|
* ****************************************************************************** |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace core\diag; |
|
13
|
|
|
|
|
14
|
|
|
use \Exception; |
|
15
|
|
|
|
|
16
|
|
|
require_once(dirname(dirname(__DIR__)) . "/config/_config.php"); |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
*/ |
|
20
|
|
|
class Sociopath extends AbstractTest { |
|
21
|
|
|
|
|
22
|
|
|
private $qaArray; |
|
23
|
|
|
private $previousQuestions; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* |
|
27
|
|
|
* @param array $previousGuess list of suspect elements and their current occurence factor (entries of sorts [ INFRA_DEVICE => 0.6, ... ] |
|
28
|
|
|
* @param array $alreadyAsked questions which were already asked before |
|
29
|
|
|
* |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct($previousGuess, $alreadyAsked = []) { |
|
32
|
|
|
// here is an array with yes/no answers per failure category, and the factors by which a Yes modifies the score; No's modify it by 1/factor; |
|
33
|
|
|
// the order in this array is important: lower numbered questions will be asked first. So make sure you have high-quality questions in the beginning. |
|
34
|
|
|
// to be clear: "Yes" answers are elsewhere in the class the TRUE case; No's are FALSE, a possible "Don't know or N/A" is NULL |
|
35
|
|
|
$this->qaArray = [ |
|
36
|
|
|
0 => ["AREA" => Telepath::INFRA_DEVICE, "TXT" => _("Did the device previously work at other hotspots?"), "FACTOR" => 0.33], |
|
37
|
|
|
1 => ["AREA" => Telepath::INFRA_DEVICE, "TXT" => _("Did you recently change the configuration on your device?"), "FACTOR" => 3], |
|
38
|
|
|
2 => ["AREA" => Telepath::INFRA_DEVICE, "TXT" => _("Do your other devices still work?"), "FACTOR" => 0.33], |
|
39
|
|
|
3 => ["AREA" => Telepath::INFRA_SP_80211, "TXT" => _("Does the connection get better when you move around?"), "FACTOR" => 3], |
|
40
|
|
|
4 => ["AREA" => Telepath::INFRA_SP_LAN, "TXT" => _("Do you see errors stating something similar to 'Unable to get IP address'?"), "FACTOR" => 3], |
|
41
|
|
|
]; |
|
42
|
|
|
// stow away the current state of guesswork |
|
43
|
|
|
$this->previousQuestions = $alreadyAsked; |
|
44
|
|
|
$this->possibleFailureReasons = $previousGuess; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function revaluate($questionNumber, $answer) { |
|
48
|
|
|
$questionDetails = $this->qaArray[$questionNumber]; |
|
49
|
|
View Code Duplication |
if ($answer === TRUE) { |
|
|
|
|
|
|
50
|
|
|
$this->possibleFailureReasons[$questionDetails['AREA']] = $this->possibleFailureReasons[$questionDetails['AREA']] * $questionDetails["FACTOR"]; |
|
51
|
|
|
} |
|
52
|
|
View Code Duplication |
if ($answer === FALSE) { |
|
|
|
|
|
|
53
|
|
|
$this->possibleFailureReasons[$questionDetails['AREA']] = $this->possibleFailureReasons[$questionDetails['AREA']] / $questionDetails["FACTOR"]; |
|
54
|
|
|
} |
|
55
|
|
|
$this->normaliseResultSet(); |
|
56
|
|
|
$this->previousQuestions[] = $questionNumber; |
|
57
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function questionOracle() { |
|
61
|
|
|
reset($this->possibleFailureReasons); |
|
62
|
|
|
$highestCategory = key($this->possibleFailureReasons); |
|
63
|
|
|
next($this->possibleFailureReasons); |
|
64
|
|
|
$nextCategory = key($this->possibleFailureReasons); |
|
65
|
|
|
if ($this->possibleFailureReasons[$highestCategory] != $this->possibleFailureReasons[$nextCategory]) { |
|
66
|
|
|
$nextCategory = $highestCategory; |
|
67
|
|
|
} |
|
68
|
|
|
// if both are identical, take any of the questions in the pool of both |
|
69
|
|
|
foreach ($this->qaArray as $questionNumber => $questionDetails) { |
|
70
|
|
|
// if we find a question we didn't ask before AND it is related to our currently high-scoring problem area, ask it |
|
71
|
|
|
if (!in_array($questionNumber, $this->previousQuestions) && ( $questionDetails["AREA"] == $highestCategory || $questionDetails["AREA"] == $nextCategory) ) { |
|
72
|
|
|
return json_encode(["NEXTEXISTS" => TRUE, "NUMBER" => $questionNumber, "TEXT" => $questionDetails["TXT"]]); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
// if we got here, we ran out of questions. Return that fact |
|
76
|
|
|
return json_encode(["NEXTEXISTS" => FALSE]); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function getCurrentGuessState() { |
|
80
|
|
|
return json_encode([ "SUSPECTS" => $this->possibleFailureReasons, "PREVIOUSQUESTIONS" => $this->previousQuestions ]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.