|
@@ -1,4 +1,8 @@
|
|
package de.tu_darmstadt.tk.SmartHomeNetworkSim.evaluation;
|
|
package de.tu_darmstadt.tk.SmartHomeNetworkSim.evaluation;
|
|
|
|
+import java.io.BufferedWriter;
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.FileWriter;
|
|
|
|
+import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.HashSet;
|
|
@@ -72,6 +76,8 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
|
|
*/
|
|
*/
|
|
protected int NUMBER_OF_PACKETS = 200;
|
|
protected int NUMBER_OF_PACKETS = 200;
|
|
|
|
|
|
|
|
+ private String currentScenario = "";
|
|
|
|
+ private int scenarioRun = 0;
|
|
/**
|
|
/**
|
|
* Initializes the different maps
|
|
* Initializes the different maps
|
|
*/
|
|
*/
|
|
@@ -320,6 +326,16 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
|
|
* @param packets packets to be classified
|
|
* @param packets packets to be classified
|
|
*/
|
|
*/
|
|
protected void classify(HashMap<Link, LinkedList<Packet>> packets) {
|
|
protected void classify(HashMap<Link, LinkedList<Packet>> packets) {
|
|
|
|
+ File anomalyResults = new File("results/"+getCurrentScenario() + scenarioRun + ".csv");
|
|
|
|
+ anomalyResults.getParentFile().mkdir();
|
|
|
|
+ BufferedWriter writer = null;
|
|
|
|
+ try {
|
|
|
|
+ writer = new BufferedWriter(new FileWriter(anomalyResults));
|
|
|
|
+ writer.write("PacketRepresentation,anomalyFPorTP,sensorInfo\n");
|
|
|
|
+ } catch (IOException e1) {
|
|
|
|
+ // TODO Auto-generated catch block
|
|
|
|
+ e1.printStackTrace();
|
|
|
|
+ }
|
|
int tp = 0;
|
|
int tp = 0;
|
|
int fp = 0;
|
|
int fp = 0;
|
|
int tn = 0;
|
|
int tn = 0;
|
|
@@ -349,6 +365,16 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
|
|
Instance packet_instance = packet2Instance(l, packet, dataset);
|
|
Instance packet_instance = packet2Instance(l, packet, dataset);
|
|
|
|
|
|
if(packet_instance == null)continue;
|
|
if(packet_instance == null)continue;
|
|
|
|
+ String sensorLabel = "";
|
|
|
|
+ if(packet instanceof MQTTpublishPacket) {
|
|
|
|
+ MQTTpublishPacket mqttPac = (MQTTpublishPacket)packet;
|
|
|
|
+ sensorLabel = ""+mqttPac.getSensorValue();
|
|
|
|
+ if(mqttPac.isBoolean()) {
|
|
|
|
+ sensorLabel = "" + Boolean.parseBoolean(sensorLabel);
|
|
|
|
+ }
|
|
|
|
+ sensorLabel = ","+sensorLabel;
|
|
|
|
+ }
|
|
|
|
+
|
|
try {
|
|
try {
|
|
double dist = classifyInstance(packet_instance, packet);
|
|
double dist = classifyInstance(packet_instance, packet);
|
|
if(dist<=1.0) {
|
|
if(dist<=1.0) {
|
|
@@ -356,14 +382,16 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
|
|
tn++;
|
|
tn++;
|
|
else {
|
|
else {
|
|
fn++;
|
|
fn++;
|
|
-
|
|
|
|
- System.out.println(packet.getTextualRepresentation());
|
|
|
|
|
|
+ //System.out.println(packet.getTextualRepresentation()+",AnomalyNotFound"+sensorLabel);
|
|
}
|
|
}
|
|
}else {
|
|
}else {
|
|
- if(packet.getLabel()==0)
|
|
|
|
|
|
+ if(packet.getLabel()==0) {
|
|
fp++;
|
|
fp++;
|
|
- else
|
|
|
|
|
|
+ writer.write(packet.getTextualRepresentation()+",FP,"+sensorLabel+"\n");
|
|
|
|
+ } else {
|
|
tp++;
|
|
tp++;
|
|
|
|
+ writer.write(packet.getTextualRepresentation()+",TP,"+sensorLabel+"\n");
|
|
|
|
+ }
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
if(packet.getLabel()==0)
|
|
if(packet.getLabel()==0)
|
|
@@ -375,7 +403,8 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
|
|
}
|
|
}
|
|
int n = tp+tn+fp+fn;
|
|
int n = tp+tn+fp+fn;
|
|
if(n!=0) {
|
|
if(n!=0) {
|
|
- System.out.println(getAlgoName()+" Performance: ["+start+"ms, "+end+"ms]");
|
|
|
|
|
|
+ System.out.println(getAlgoName()+" Performance: ["+start+"ms, "+end+"ms] Scenario: " + getCurrentScenario() + scenarioRun);
|
|
|
|
+ scenarioRun++;
|
|
System.out.println("n: "+n);
|
|
System.out.println("n: "+n);
|
|
System.out.println("TP: "+tp);
|
|
System.out.println("TP: "+tp);
|
|
System.out.println("FP: "+fp);
|
|
System.out.println("FP: "+fp);
|
|
@@ -385,6 +414,12 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
|
|
System.out.println("FPR: "+(fp/(fp+tn+0.0)));
|
|
System.out.println("FPR: "+(fp/(fp+tn+0.0)));
|
|
System.out.println("");
|
|
System.out.println("");
|
|
}
|
|
}
|
|
|
|
+ try {
|
|
|
|
+ writer.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ // TODO Auto-generated catch block
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -431,4 +466,19 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public abstract String getAlgoName();
|
|
public abstract String getAlgoName();
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @return the currentScenario
|
|
|
|
+ */
|
|
|
|
+ public String getCurrentScenario() {
|
|
|
|
+ return currentScenario;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @param currentScenario the currentScenario to set
|
|
|
|
+ */
|
|
|
|
+ public void setCurrentScenario(String currentScenario) {
|
|
|
|
+ this.currentScenario = currentScenario;
|
|
|
|
+ this.scenarioRun = 0;
|
|
|
|
+ }
|
|
}
|
|
}
|