Browse Source

Fixes Example Classifiers

Andreas T. Meyer-Berg 4 years ago
parent
commit
3e9b086993

+ 11 - 13
examples/UnsupervisedAnomalyDetectionExample.java

@@ -96,17 +96,6 @@ public class UnsupervisedAnomalyDetectionExample implements PacketSniffer {
 	
 	@Override
 	public void processPackets(HashMap<Link, LinkedList<Packet>> packets) {
-		if(!packets.entrySet().isEmpty() && packets.entrySet().iterator().next().getValue().getFirst().getTimestamp()>10000) {
-			training = false;
-			// Build Clusterer
-			try {
-				finishDataCollection();
-			} catch (Exception e) {
-				System.out.println("Clustering failed");
-				e.printStackTrace();
-			}
-		}
-		
 		if(training)
 			try {
 				training(packets);
@@ -299,11 +288,20 @@ public class UnsupervisedAnomalyDetectionExample implements PacketSniffer {
 	
 	@Override
 	public void setMode(boolean testing) {
-		mode = testing;
+		training = !testing;
+		if(testing) {
+			// Build Clusterer
+			try {
+				finishDataCollection();
+			} catch (Exception e) {
+				System.out.println("Clustering failed");
+				e.printStackTrace();
+			}
+		}
 	}
 	
 	@Override
 	public boolean getMode() {
-		return mode;
+		return !training;
 	}
 }

+ 11 - 14
examples/UnsupervisedAnomalyDetectionExample2.java

@@ -13,7 +13,6 @@ import weka.core.Attribute;
 import weka.core.DenseInstance;
 import weka.core.Instance;
 import weka.core.Instances;
-import weka.core.NominalAttributeInfo;
 
 /**
  * Unsupervised Example - maybe Clustering
@@ -98,17 +97,6 @@ public class UnsupervisedAnomalyDetectionExample2 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()>10000) {
-			training = false;
-			// Build Clusterer
-			try {
-				finishDataCollection();
-			} catch (Exception e) {
-				System.out.println("Clustering failed");
-				e.printStackTrace();
-			}
-		}
-		
 		if(training)
 			try {
 				training(packets);
@@ -336,11 +324,20 @@ public class UnsupervisedAnomalyDetectionExample2 implements PacketSniffer {
 	
 	@Override
 	public void setMode(boolean testing) {
-		mode = testing;
+		training = !testing;
+		if(testing) {
+			// Build Clusterer
+			try {
+				finishDataCollection();
+			} catch (Exception e) {
+				System.out.println("Clustering failed");
+				e.printStackTrace();
+			}
+		}
 	}
 	
 	@Override
 	public boolean getMode() {
-		return mode;
+		return !training;
 	}
 }