|
@@ -1,4 +1,8 @@
|
|
|
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.HashMap;
|
|
|
import java.util.HashSet;
|
|
@@ -72,6 +76,8 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
|
|
|
*/
|
|
|
protected int NUMBER_OF_PACKETS = 200;
|
|
|
|
|
|
+ private String currentScenario = "";
|
|
|
+ private int scenarioRun = 0;
|
|
|
/**
|
|
|
* Initializes the different maps
|
|
|
*/
|
|
@@ -320,6 +326,16 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
|
|
|
* @param packets packets to be classified
|
|
|
*/
|
|
|
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 fp = 0;
|
|
|
int tn = 0;
|
|
@@ -349,6 +365,16 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
|
|
|
Instance packet_instance = packet2Instance(l, packet, dataset);
|
|
|
|
|
|
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 {
|
|
|
double dist = classifyInstance(packet_instance, packet);
|
|
|
if(dist<=1.0) {
|
|
@@ -356,14 +382,16 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
|
|
|
tn++;
|
|
|
else {
|
|
|
fn++;
|
|
|
-
|
|
|
- System.out.println(packet.getTextualRepresentation());
|
|
|
+ //System.out.println(packet.getTextualRepresentation()+",AnomalyNotFound"+sensorLabel);
|
|
|
}
|
|
|
}else {
|
|
|
- if(packet.getLabel()==0)
|
|
|
+ if(packet.getLabel()==0) {
|
|
|
fp++;
|
|
|
- else
|
|
|
+ writer.write(packet.getTextualRepresentation()+",FP,"+sensorLabel+"\n");
|
|
|
+ } else {
|
|
|
tp++;
|
|
|
+ writer.write(packet.getTextualRepresentation()+",TP,"+sensorLabel+"\n");
|
|
|
+ }
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
if(packet.getLabel()==0)
|
|
@@ -375,7 +403,8 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
|
|
|
}
|
|
|
int n = tp+tn+fp+fn;
|
|
|
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("TP: "+tp);
|
|
|
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("");
|
|
|
}
|
|
|
+ try {
|
|
|
+ writer.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -431,4 +466,19 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
|
|
|
* @return
|
|
|
*/
|
|
|
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;
|
|
|
+ }
|
|
|
}
|