Sfoglia il codice sorgente

Adds CSV Export V1

Andreas T. Meyer-Berg 3 anni fa
parent
commit
d58919f07f

+ 123 - 122
.gitignore

@@ -1,122 +1,123 @@
-# Created by https://www.gitignore.io/api/java,gradle,eclipse
-# Edit at https://www.gitignore.io/?templates=java,gradle,eclipse
-
-### Eclipse ###
-
-.metadata
-bin/
-tmp/
-*.tmp
-*.bak
-*.swp
-*~.nib
-local.properties
-.settings/
-.loadpath
-.recommenders
-
-# External tool builders
-.externalToolBuilders/
-
-# Locally stored "Eclipse launch configurations"
-*.launch
-
-# PyDev specific (Python IDE for Eclipse)
-*.pydevproject
-
-# CDT-specific (C/C++ Development Tooling)
-.cproject
-
-# CDT- autotools
-.autotools
-
-# Java annotation processor (APT)
-.factorypath
-
-# PDT-specific (PHP Development Tools)
-.buildpath
-
-# sbteclipse plugin
-.target
-
-# Tern plugin
-.tern-project
-
-# TeXlipse plugin
-.texlipse
-
-# STS (Spring Tool Suite)
-.springBeans
-
-# Code Recommenders
-.recommenders/
-
-# Annotation Processing
-.apt_generated/
-
-# Scala IDE specific (Scala & Java development for Eclipse)
-.cache-main
-.scala_dependencies
-.worksheet
-
-### Eclipse Patch ###
-# Eclipse Core
-.project
-
-# JDT-specific (Eclipse Java Development Tools)
-.classpath
-
-# Annotation Processing
-.apt_generated
-
-.sts4-cache/
-
-### Java ###
-# Compiled class file
-*.class
-
-# Log file
-*.log
-
-# BlueJ files
-*.ctxt
-
-# Mobile Tools for Java (J2ME)
-.mtj.tmp/
-
-# Package Files #
-*.jar
-*.war
-*.nar
-*.ear
-*.zip
-*.tar.gz
-*.rar
-
-# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
-hs_err_pid*
-
-### Gradle ###
-.gradle
-/build/
-
-# Ignore Gradle GUI config
-gradle-app.setting
-
-# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
-!gradle-wrapper.jar
-# Avoid if
-!/libs/*.jar
-!/libs_src/*.jar
-
-# Cache of project
-.gradletasknamecache
-
-# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898
-# gradle/wrapper/gradle-wrapper.properties
-
-### Gradle Patch ###
-**/build/
-
-# End of https://www.gitignore.io/api/java,gradle,eclipse
-/MQTT_protocolSN.java
+# Created by https://www.gitignore.io/api/java,gradle,eclipse
+# Edit at https://www.gitignore.io/?templates=java,gradle,eclipse
+
+### Eclipse ###
+
+.metadata
+bin/
+tmp/
+*.tmp
+*.bak
+*.swp
+*~.nib
+local.properties
+.settings/
+.loadpath
+.recommenders
+
+# External tool builders
+.externalToolBuilders/
+
+# Locally stored "Eclipse launch configurations"
+*.launch
+
+# PyDev specific (Python IDE for Eclipse)
+*.pydevproject
+
+# CDT-specific (C/C++ Development Tooling)
+.cproject
+
+# CDT- autotools
+.autotools
+
+# Java annotation processor (APT)
+.factorypath
+
+# PDT-specific (PHP Development Tools)
+.buildpath
+
+# sbteclipse plugin
+.target
+
+# Tern plugin
+.tern-project
+
+# TeXlipse plugin
+.texlipse
+
+# STS (Spring Tool Suite)
+.springBeans
+
+# Code Recommenders
+.recommenders/
+
+# Annotation Processing
+.apt_generated/
+
+# Scala IDE specific (Scala & Java development for Eclipse)
+.cache-main
+.scala_dependencies
+.worksheet
+
+### Eclipse Patch ###
+# Eclipse Core
+.project
+
+# JDT-specific (Eclipse Java Development Tools)
+.classpath
+
+# Annotation Processing
+.apt_generated
+
+.sts4-cache/
+
+### Java ###
+# Compiled class file
+*.class
+
+# Log file
+*.log
+
+# BlueJ files
+*.ctxt
+
+# Mobile Tools for Java (J2ME)
+.mtj.tmp/
+
+# Package Files #
+*.jar
+*.war
+*.nar
+*.ear
+*.zip
+*.tar.gz
+*.rar
+
+# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
+hs_err_pid*
+
+### Gradle ###
+.gradle
+/build/
+
+# Ignore Gradle GUI config
+gradle-app.setting
+
+# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
+!gradle-wrapper.jar
+# Avoid if
+!/libs/*.jar
+!/libs_src/*.jar
+
+# Cache of project
+.gradletasknamecache
+
+# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898
+# gradle/wrapper/gradle-wrapper.properties
+
+### Gradle Patch ###
+**/build/
+
+# End of https://www.gitignore.io/api/java,gradle,eclipse
+/MQTT_protocolSN.java
+/results/

+ 2 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/Main.java

@@ -33,5 +33,7 @@ public class Main {
 		model = new Model();
 		controller = new Controller(model);
 	    view = new MainFrame(controller);
+	    view.menu.mnExamples.createSWCExample(true);
+	    System.exit(0);
 	}
 }

+ 1 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/devices/SmartLightSensor.java

@@ -6,7 +6,7 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.scheduler.Schedulable;
 
 public class SmartLightSensor extends BoolSensorDevice implements Schedulable, SensorLabel {
 
-	long eventTime = -1000;
+	long eventTime = 0;
 	long updateInterval = 10000;
 	RoomStatus roomStatus;
 	

+ 1 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/devices/SmartTemperatureSensor.java

@@ -9,7 +9,7 @@ public class SmartTemperatureSensor extends FloatSensorDevice implements SensorL
 
 	
 	public SmartTemperatureSensor(String name, RoomStatus room) {
-		super(name);
+		super(name, 1000);
 		roomStatus = room;
 	}
 	

+ 55 - 5
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/evaluation/BasicPacketClassifier.java

@@ -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;
+	}
 }

+ 2 - 2
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/evaluation/SWCKMeansClustering.java

@@ -23,7 +23,7 @@ public class SWCKMeansClustering extends BasicPacketClassifier {
 	 */ 
 	//17 works fine
 	//34 found value anomalies
-	protected int NUMBER_OF_CLUSTERS = 8;
+	protected int NUMBER_OF_CLUSTERS = 25;
 	protected double[] stdv = new double[NUMBER_OF_CLUSTERS];
 	/**
 	 * Initializes the k means clusterer
@@ -75,7 +75,7 @@ public class SWCKMeansClustering extends BasicPacketClassifier {
 			e.printStackTrace();
 		}
 	}
-	private boolean test = true;
+	private boolean test = false;
 	
 	@Override
 	public double classifyInstance(Instance instance, Packet origin) throws Exception {

+ 1 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/MainFrame.java

@@ -25,7 +25,7 @@ public class MainFrame extends JFrame {
 	/**
 	 * MenuBar of the GUI
 	 */
-	private MenuBar menu;
+	public MenuBar menu;
 	/**
 	 * Panel which visualizes the devices, connections and links
 	 */

+ 1 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/MenuBar.java

@@ -74,7 +74,7 @@ public class MenuBar extends JMenuBar {
 	/**
 	 * JMenu for examples
 	 */
-	private JMenu mnExamples;
+	public MenuBarNetworkExamples mnExamples;
 	
 	/**
 	 * JMenu for Examample Anomalies

+ 4 - 2
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/menuBar/MenuBarNetworkExamples.java

@@ -924,7 +924,7 @@ public class MenuBarNetworkExamples extends JMenu{
 			/**
 			 * Instances of the packet Sniffers
 			 */
-			PacketSniffer snifferKNN = null; //, snifferEM = null, snifferHC = null;
+			SWCKMeansClustering snifferKNN = null; //, snifferEM = null, snifferHC = null;
 			/*
 			 * Import Example PacketSniffer algorithms
 			 */
@@ -1043,7 +1043,7 @@ public class MenuBarNetworkExamples extends JMenu{
 			/*
 			 * Simulate/Test 1 hour without anomalies
 			 */
-
+			snifferKNN.setCurrentScenario("NoAnomalies");
 			System.out.println("Test w/0 anomalies:");//TODO
 			for(int i=24; i<26; i++)
 				sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
@@ -1057,6 +1057,7 @@ public class MenuBarNetworkExamples extends JMenu{
 			 * Light Anomaly 1h
 			 * Light != Sensor
 			 */
+			snifferKNN.setCurrentScenario("LightAnomalies");
 			System.out.println("Light Anomaly:");
 			smartLight.setTrueStatus(smartLight.getBSval());
 			smartLight.setLabel((short)-1);
@@ -1093,6 +1094,7 @@ public class MenuBarNetworkExamples extends JMenu{
 			/**
 			 * Temperature Anomaly 2h
 			 */
+			snifferKNN.setCurrentScenario("TemperatureAnomalies");
 			System.out.println("Temperature Anomaly:");
 			heaterDevice.setFSval(21f);
 			heaterDevice.setTrueTemperature(34f);