/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ids.IA;
import ids.Server.DiagnosticManagment;
import ids.Server.Server;
import ids.Server.TableOfEvents;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
/**
*
* @author Peet
*/
public class IA extends Thread {
Server server;
TableOfEvents tableOfEvents;
InternalTableVerifier currentSituation;
long startingTime;
BayesNetwork bayesNetwork;
Diagnostic currentDiagnostic;
Solution currentSolution;
private final String Status_OK = "OK";
private final String Status_Warning = "Warning";
private final String Status_Danger = "Danger";
String status;
DiagnosticManagment diagnosticMan;
public IA(Server s) throws ParserConfigurationException, SAXException, IOException {
this.server = s;
this.status = Status_OK;
this.tableOfEvents = new TableOfEvents();
this.currentSituation = new InternalTableVerifier();
this.bayesNetwork = new BayesNetwork();
this.bayesNetwork.initNetwork();
this.diagnosticMan = new DiagnosticManagment(this.server);
this.diagnosticMan.setLocation(700, 0);
for (Diagnostic d : this.bayesNetwork.diagnostics) {
this.server.tableWeight.add(d.name, 1);
}
}
public void getCurrentEventTable() throws InterruptedException {
this.startingTime = System.currentTimeMillis();
try {
synchronized (this) {
wait(5000);
}
} catch (InterruptedException ex) {
Logger.getLogger(IA.class.getName()).log(Level.SEVERE, null, ex);
}
this.tableOfEvents = this.server.getCopyTable();
this.currentSituation.updateTable(this.tableOfEvents);
}
public void analyse() {
//Initialise currentDiagnostic
for (Diagnostic d : this.bayesNetwork.diagnostics) {
if (d.name.equals("No_Problem")) {
this.currentDiagnostic = d;
}
}
//Do stuff to analyse and find the currentDiagnostic
float currentDiagnosticProbability;
float maxCurrentProb = 0;
float currentProb;
Vector<Symptom> symptoms;
for (Diagnostic d : this.bayesNetwork.diagnostics) {
currentDiagnosticProbability = 0;
currentProb = 0;
symptoms = new Vector<Symptom>();
float symptomProb = 0;
for (Symptom s : d.symptoms) {
if (this.currentSituation.isPresent(s)) {
symptomProb = 0;
symptoms.add(s);
symptomProb += (1 / s.getProbability()) * this.currentSituation.getFrequency(s.name);
}
}
currentDiagnosticProbability = d.probability.getPobability(symptoms) * this.server.tableWeight.getWeight(d.name);
currentProb = symptomProb + currentDiagnosticProbability;
if (currentProb > maxCurrentProb) {
maxCurrentProb = currentProb;
this.currentDiagnostic = d;
}
}
//Find the best solution
float maxCurrentSolutionProb = 0;
float currentSolutionProb;
for (Solution s : this.currentDiagnostic.solution) {
currentSolutionProb = maxCurrentProb * s.probability.probabilities.elementAt(1).probability;
if (currentSolutionProb > maxCurrentSolutionProb) {
maxCurrentSolutionProb = currentSolutionProb;
this.currentSolution = s;
}
}
//Set the status "color"
if (this.currentDiagnostic.name.equals("No_Problem")) {
this.status = Status_OK;
} else {
if (this.currentSolution.name.equals("Alert_1") || this.currentSolution.name.equals("Alert_2")
|| this.currentSolution.name.equals("Block_Address") || this.currentSolution.name.equals("Test_Connection")
|| this.currentSolution.name.equals("Block_Page")) {
this.status = Status_Warning;
} else if (this.currentSolution.name.equals("Stop_Server") || this.currentSolution.name.equals("Maintenance_Page")
|| this.currentSolution.name.equals("Alert_3")) {
this.status = Status_Danger;
} else {
this.status = Status_OK;
}
}
}
private void isItRightDiagnostic() {
this.diagnosticMan.setVisible(true);
}
public void run() {
this.server.setStatus(this.status, "No_Problem", "Nothing");
while (true) {
try {
getCurrentEventTable();
} catch (InterruptedException ex) {
Logger.getLogger(IA.class.getName()).log(Level.SEVERE, null, ex);
}
analyse();
if (this.currentDiagnostic != null && this.currentSolution != null) {
this.server.setStatus(this.status, this.currentDiagnostic.name, this.currentSolution.name);
} else if (this.currentDiagnostic != null && this.currentSolution == null) {
this.server.setStatus(this.status, this.currentDiagnostic.name, "Nothing");
}
isItRightDiagnostic();
}
}
}