Browse Source

Adds back-end for testing/training/disabled mode

Andreas T. Meyer-Berg 4 years ago
parent
commit
ee46b62f44

+ 15 - 0
examples/CountingMetric.java

@@ -20,6 +20,11 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimplePa
  */
 public class CountingMetric implements PacketSniffer {
 
+	/**
+	 * Mode of the algorithm, True = testing
+	 */
+	private boolean mode = false;
+	
 	@Override
 	public void processPackets(HashMap<Link, LinkedList<Packet>> packets) {
 		System.out.println("Counting Metric: ");
@@ -133,4 +138,14 @@ public class CountingMetric implements PacketSniffer {
 		
 		
 	}
+	
+	@Override
+	public void setMode(boolean testing) {
+		mode = testing;
+	}
+	
+	@Override
+	public boolean getMode() {
+		return mode;
+	}
 }

+ 12 - 0
examples/UnsupervisedAnomalyDetectionExample.java

@@ -294,4 +294,16 @@ public class UnsupervisedAnomalyDetectionExample implements PacketSniffer {
 			}
 		}
 	}
+	
+
+	
+	@Override
+	public void setMode(boolean testing) {
+		mode = testing;
+	}
+	
+	@Override
+	public boolean getMode() {
+		return mode;
+	}
 }

+ 12 - 0
examples/UnsupervisedAnomalyDetectionExample2.java

@@ -331,4 +331,16 @@ public class UnsupervisedAnomalyDetectionExample2 implements PacketSniffer {
 			}
 		}
 	}
+	
+
+	
+	@Override
+	public void setMode(boolean testing) {
+		mode = testing;
+	}
+	
+	@Override
+	public boolean getMode() {
+		return mode;
+	}
 }

+ 18 - 11
examples/classifier/BasicPacketClassifier.java

@@ -84,17 +84,6 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
 	
 	@Override
 	public void processPackets(HashMap<Link, LinkedList<Packet>> packets) {
-		if(training && !packets.entrySet().isEmpty() && !packets.entrySet().iterator().next().getValue().isEmpty() && packets.entrySet().iterator().next().getValue().getFirst().getTimestamp()>getClassificationStart()) {
-			training = false;
-			// Build Clusterer
-			try {
-				finishDataCollection();
-			} catch (Exception e) {
-				System.out.println("Clustering failed");
-				e.printStackTrace();
-			}
-		}
-		
 		if(training)
 			try {
 				training(packets);
@@ -322,4 +311,22 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
 	 * @return timestep of the testing begin.
 	 */
 	public abstract long getClassificationStart();
+	
+	@Override
+	public void setMode(boolean testing) {
+		training = !testing;
+		if(testing) {
+			try {
+				finishDataCollection();
+			} catch (Exception e) {
+				System.out.println("Clustering failed");
+				e.printStackTrace();
+			}	
+		}
+	}
+	
+	@Override
+	public boolean getMode() {
+		return !training;
+	}
 }

+ 46 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/PacketCollector.java

@@ -37,6 +37,16 @@ public class PacketCollector {
 	 */
 	private PacketSniffer packetAlgorithm = null;
 	
+	/**
+	 * Mode of the Collector, whether it should train or test the packetSniffer algorithm
+	 */
+	private boolean mode = false;
+	
+	/**
+	 * True if it should collect packages
+	 */
+	private boolean active = true;
+	
 	/**
 	 * Creates a PacketCollector without assigning and algorithm to process the packets
 	 */
@@ -180,4 +190,40 @@ public class PacketCollector {
 	public void setPacketAlgorithm(PacketSniffer packetAlgorithm) {
 		this.packetAlgorithm = packetAlgorithm;
 	}
+
+	/**
+	 * Returns training or testing mode of the algorithm.
+	 * @return true if testing or false if training the algorithm.
+	 */
+	public boolean getMode() {
+		if(packetAlgorithm!=null)
+			mode = packetAlgorithm.getMode();
+		return mode;
+	}
+
+	/**
+	 * Sets the training or testing mode
+	 * @param mode true if testing or false if training the algorithm.
+	 */
+	public void setMode(boolean mode) {
+		this.mode = mode;
+		if(packetAlgorithm != null)
+			packetAlgorithm.setMode(mode);
+	}
+
+	/**
+	 * Whether the algorithm should run
+	 * @return true if algorithm will be executed
+	 */
+	public boolean isActive() {
+		return active;
+	}
+
+	/**
+	 * Set to true, if it should run
+	 * @param active true if it should be active
+	 */
+	public void setActive(boolean active) {
+		this.active = active;
+	}
 }

+ 16 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/PacketSniffer.java

@@ -22,5 +22,21 @@ public interface PacketSniffer {
 	 *            grouped by each link.
 	 */
 	public void processPackets(HashMap<Link, LinkedList<Packet>> packets);
+	
+	/**
+	 * Set the mode of the algorithm, whether it should be training or testing 
+	 * with the given packets
+	 * @param testing <code>false</code> if it should be training, <code>true</code> if
+	 * it should be testing
+	 */
+	public void setMode(boolean testing);
+	
+	/**
+	 * Returns the mode of the algorithm, whether it should be training or testing 
+	 * with the given packets.<br>
+	 * <code>false</code>: if it should be training<br> 
+	 * <code>true</code>:  if it should be testing
+	 */
+	public boolean getMode();
 
 }

+ 15 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/simpleImplementation/CountingMetric.java

@@ -22,6 +22,11 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimplePa
  */
 public class CountingMetric implements PacketSniffer {
 
+	/**
+	 * Mode of the algorithm, True = testing
+	 */
+	private boolean mode = false;
+	
 	@Override
 	public void processPackets(HashMap<Link, LinkedList<Packet>> packets) {
 		System.out.println("Counting Metric: ");
@@ -135,4 +140,14 @@ public class CountingMetric implements PacketSniffer {
 		
 		
 	}
+	
+	@Override
+	public void setMode(boolean testing) {
+		mode = testing;
+	}
+	
+	@Override
+	public boolean getMode() {
+		return mode;
+	}
 }

+ 17 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/simpleImplementation/SimplePacketSniffer.java

@@ -16,6 +16,11 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketSniffer;
  */
 public class SimplePacketSniffer implements PacketSniffer {
 
+	/**
+	 * True if in testing mode;
+	 */
+	private boolean testing = true;
+	
 	@Override
 	public void processPackets(HashMap<Link, LinkedList<Packet>> packets) {
 		System.out.println("PacketSniffer: ");
@@ -31,4 +36,16 @@ public class SimplePacketSniffer implements PacketSniffer {
 
 	}
 
+	@Override
+	public void setMode(boolean testing) {
+		this.testing = testing;
+	}
+
+	@Override
+	public boolean getMode() {
+		return this.testing;
+	}
+	
+	
+
 }