|
@@ -0,0 +1,53 @@
|
|
|
|
+package classifier;
|
|
|
|
+
|
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
|
|
|
|
+import weka.clusterers.HierarchicalClusterer;
|
|
|
|
+import weka.core.EuclideanDistance;
|
|
|
|
+import weka.core.Instance;
|
|
|
|
+import weka.core.Instances;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Hierarchical Clustering Approach
|
|
|
|
+ * @author Andreas T. Meyer-Berg
|
|
|
|
+ */
|
|
|
|
+public class HierarchicalClustering extends BasicPacketClassifier {
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Hierarchical cluster which is used
|
|
|
|
+ */
|
|
|
|
+ private HierarchicalClusterer clusterer;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Initialize the clusterer
|
|
|
|
+ */
|
|
|
|
+ public HierarchicalClustering() {
|
|
|
|
+ clusterer = new HierarchicalClusterer();
|
|
|
|
+ clusterer.setDistanceFunction(new EuclideanDistance());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void trainModel(Instances instances) {
|
|
|
|
+ try {
|
|
|
|
+ clusterer.buildClusterer(instances);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ // TODO Auto-generated catch block
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void classifyInstance(Instance instance, Packet origin) {
|
|
|
|
+ try {
|
|
|
|
+ clusterer.clusterInstance(instance);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ System.out.println("Anomaly Detected on Packet: "+origin.getTextualRepresentation());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public long getClassificationStart() {
|
|
|
|
+ return 10000;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|