Browse Source

Various Addition

* Fixes Fail on impossible schedules -> simply simulates such events
* Adds Anomaly Detection template
* Adds Normal Distribution Function
* Adds Distribution Configuration User Interface
* Adds Import of Distributions
* Adds Distribution change for ports (only resample after execution)
* Minor Fixes
Andreas T. Meyer-Berg 4 years ago
parent
commit
fdd3aea5d8

+ 1 - 0
README.md

@@ -27,6 +27,7 @@ Gradle downloads these during the build process
 
 * [JUnit4](https://junit.org/junit4/) - Java unit test framework
 * [Pcap4j](https://www.pcap4j.org/) - Java Library used for Pcap File creation (not yet implemented)
+* [Math3](https://commons.apache.org/proper/commons-math/) - Apache Common Maths (for Distribution function)
 
 <!-- PCAP file writing example (as part of the export manager - but probably required packet transformation): https://www.devdungeon.com/content/packet-capturing-java-pcap4j#writing_pcap_file -->
 

+ 20 - 0
examples/UnsupervisedAnomalyDetectionExample.java

@@ -0,0 +1,20 @@
+import java.util.HashMap;
+import java.util.LinkedList;
+
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketSniffer;
+
+/**
+ * Unsupervised Example - maybe Clustering
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class UnsupervisedAnomalyDetectionExample implements PacketSniffer {
+
+	@Override
+	public void processPackets(HashMap<Link, LinkedList<Packet>> packets) {
+		//TODO: feature encoding, clustering weka? 
+	}
+
+}

+ 61 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/ImportController.java

@@ -15,6 +15,7 @@ import javax.tools.ToolProvider;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ProbabilityDistributionHandler;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Protocol;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.configuration.ImportConfiguration;
@@ -293,6 +294,66 @@ public class ImportController {
 		return true;
 	}
 	
+	/**
+	 * Adds new DistributionHandler to the model
+	 * 
+	 * @param distributionHandler DistributionHandler to be added
+	 * @return true if it was added
+	 */
+	public boolean addDistributionHandler(Class<? extends ProbabilityDistributionHandler> distributionHandler) {
+		if (isValidDistributionHandler(distributionHandler))
+			importConf.addDistributionHandlerClass(distributionHandler);
+		else
+			return false;
+		return true;
+		
+	}
+	
+	/**
+	 * Removes DistributionHandler from the model
+	 * 
+	 * @param distributionHandler
+	 *            DistributionHandler to be removed
+	 */
+	public void removeDistributionHandler(Class<? extends ProbabilityDistributionHandler> distributionHandler) {
+		importConf.removeDistributionHandlerClass(distributionHandler);
+	}
+	
+	/**
+	 * Returns the available DistributionHandler of the model
+	 * 
+	 * @return available DistributionHandler
+	 */
+	public LinkedList<Class<? extends ProbabilityDistributionHandler>> getDistributionHandlers() {
+		return importConf.getDistributionHandlerClasses();
+	}
+	
+	/**
+	 * Returns true if it is a Valid DistributionHandler, false if not
+	 * 
+	 * @param distributionHandler
+	 *            DistributionHandler to be checked
+	 * @return true if it is a valid DistributionHandler
+	 */
+	public boolean isValidDistributionHandler(Class<? extends ProbabilityDistributionHandler> distributionHandler) {
+		try {
+			/**
+			 * SmartDevice to be tested
+			 */
+			ProbabilityDistributionHandler p = distributionHandler.newInstance();
+			// Empty constructor required, to create new instance
+			if (p == null)
+				throw new Exception("DistributionHandler required an empty constructor");
+			// Name shall not be null or empty string
+			if (p.getSimpleDescription() == null || p.getSimpleDescription() == "")
+				throw new Exception(
+						"DistributionHandler name shall not be null or empty string.");
+		} catch (Exception e) {
+			return false;
+		}
+		return true;
+	}
+	
 	/**
 	 * Imports the given .java File, compiles it and returns the compiled class
 	 * 

+ 5 - 16
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Port.java

@@ -7,7 +7,7 @@ import org.apache.commons.math3.distribution.AbstractRealDistribution;
 import org.apache.commons.math3.distribution.LaplaceDistribution;
 import org.apache.commons.math3.distribution.NormalDistribution;
 
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.distributionHandler.ConstantValueDistribution;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.distributionHandler.ConstantValueDistributionHandler;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.scheduler.Schedulable;
 
 /**
@@ -94,7 +94,7 @@ public class Port implements Schedulable {
 		status = SENDING;
 		owner = device;
 		connection = null;
-		triggerIntervalStat = new ConstantValueDistribution(new Random().nextInt(1000)+1);
+		triggerIntervalStat = new ConstantValueDistributionHandler(new Random().nextInt(1000)+1);
 		triggerInterval = triggerIntervalStat.sampleNextValue();
 		//setTriggerInterval(new Random().nextInt(1000)+1);
 		lastTrigger = 0;
@@ -114,7 +114,7 @@ public class Port implements Schedulable {
 		status = SENDING;
 		owner = device;
 		connection = null;
-		triggerIntervalStat = new ConstantValueDistribution(triggerInterval);
+		triggerIntervalStat = new ConstantValueDistributionHandler(triggerInterval);
 		this.triggerInterval = triggerIntervalStat.sampleNextValue();
 		lastTrigger = 0;
 		jitter = 0;
@@ -136,7 +136,7 @@ public class Port implements Schedulable {
 		status = SENDING;
 		owner = device;
 		connection = null;
-		triggerIntervalStat = new ConstantValueDistribution(triggerInterval);
+		triggerIntervalStat = new ConstantValueDistributionHandler(triggerInterval);
 		this.triggerInterval = triggerIntervalStat.sampleNextValue();
 		this.lastTrigger = lastTrigger;
 		this.jitter = jitter;
@@ -213,17 +213,6 @@ public class Port implements Schedulable {
 		if(removed)
 			SimulationManager.scheduleEvent(this);
 	}
-	/**
-	 * @param triggerInterval
-	 *            the triggerInterval to set
-	 */
-	@Deprecated
-	private void setTriggerInterval(long triggerInterval) {
-		if(triggerInterval<=0)
-			this.triggerInterval = (long)1;
-		else
-		    this.triggerInterval = triggerInterval;
-	}
 
 	/**
 	 * @return the responseTime
@@ -369,7 +358,7 @@ public class Port implements Schedulable {
 		//TODO: Sample next game
 		triggerInterval = triggerIntervalStat.sampleNextValue();
 		
-		if(status==Port.SENDING)
+		if(status==Port.SENDING && connection.getStatus()==Connection.ACTIVE)
 			SimulationManager.scheduleEvent(this);
 		
 	}

+ 7 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/ProbabilityDistributionHandler.java

@@ -29,4 +29,11 @@ public interface ProbabilityDistributionHandler {
 	 * @return panel for configuration
 	 */
 	public JPanel getConfigurationPanel();
+	
+	/**
+	 * Returns a simple description/name/identifier of the Distribution.
+	 * For example: "Gaussian Distribution"
+	 * @return simple description
+	 */
+	public String getSimpleDescription();
 }

+ 35 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/configuration/ImportConfiguration.java

@@ -7,12 +7,15 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionPerformance;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionPrecision;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PrecisionLink;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ProbabilityDistributionHandler;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Protocol;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.BoolCollectorDevice;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.BoolSensorDevice;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.FloatCollectorDevice;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.FloatSensorDevice;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.distributionHandler.ConstantValueDistributionHandler;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.distributionHandler.NormalDistributionHandler;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.MQTT_protocol;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.Ping_protocol;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleLink;
@@ -42,6 +45,8 @@ public class ImportConfiguration {
 	private LinkedList<Class<? extends SmartDevice>> standardSmartDevices = new LinkedList<Class<? extends SmartDevice>>();
 	private LinkedList<Class<? extends SmartDevice>> importedSmartDevices = new LinkedList<Class<? extends SmartDevice>>();
 	
+	private LinkedList<Class<? extends ProbabilityDistributionHandler>> standardDistributions = new LinkedList<Class<? extends ProbabilityDistributionHandler>>();
+	private LinkedList<Class<? extends ProbabilityDistributionHandler>> importedDistribution = new LinkedList<Class<? extends ProbabilityDistributionHandler>>();
 	/**
 	 * Initializes the configuration and adds the standard classes
 	 */
@@ -63,6 +68,9 @@ public class ImportConfiguration {
 		
 		standardConnections.add(ConnectionPerformance.class);
 		standardConnections.add(ConnectionPrecision.class);
+		
+		standardDistributions.add(ConstantValueDistributionHandler.class);
+		standardDistributions.add(NormalDistributionHandler.class);
 	}
 	
 	
@@ -174,4 +182,31 @@ public class ImportConfiguration {
 		importedSmartDevices.remove(remove);
 	}
 
+	
+	/**
+	 * Returns DistributionHandler Classes of the Model
+	 * @return available DistributionHandler Classes
+	 */
+	public LinkedList<Class<? extends ProbabilityDistributionHandler>> getDistributionHandlerClasses(){
+		LinkedList<Class<? extends ProbabilityDistributionHandler>> export = new LinkedList<Class<? extends ProbabilityDistributionHandler>>();
+		export.addAll(standardDistributions);
+		export.addAll(importedDistribution);
+		return export;
+	}
+	
+	/**
+	 * Adds new DistributionHandler Class to the available DistributionHandler classes
+	 * @param newDistributionHandler new DistributionHandler Class to be added
+	 */
+	public void addDistributionHandlerClass(Class<? extends ProbabilityDistributionHandler> newDistributionHandler){
+		importedDistribution.add(newDistributionHandler);
+	}
+	
+	/**
+	 * Removes DistributionHandler Class from the available DistributionHandler Classes
+	 * @param remove DistributionHandler Class to be removed
+	 */
+	public void removeDistributionHandlerClass(Class<? extends ProbabilityDistributionHandler> remove){
+		importedDistribution.remove(remove);
+	}
 }

+ 23 - 6
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/distributionHandler/ConstantValueDistribution.java → src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/distributionHandler/ConstantValueDistributionHandler.java

@@ -1,5 +1,6 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.distributionHandler;
 
+import java.awt.Color;
 import java.awt.Dimension;
 
 import javax.swing.JFrame;
@@ -12,11 +13,11 @@ import org.apache.commons.math3.random.RandomGenerator;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ProbabilityDistributionHandler;
 
 /**
- * Example class representing 
+ * Example class representing a Distribution, with only one constant value
  *
  * @author Andreas T. Meyer-Berg
  */
-public class ConstantValueDistribution implements
+public class ConstantValueDistributionHandler implements
 		ProbabilityDistributionHandler {
 
 	/**
@@ -28,14 +29,14 @@ public class ConstantValueDistribution implements
 	 * Creates a new distribution which returns the given value
 	 * @param value value to be returned
 	 */
-	public ConstantValueDistribution(long value) {
+	public ConstantValueDistributionHandler(long value) {
 		fixedValue = value;
 	}
 
 	/**
 	 * Creates a default distribution, which returns 50.
 	 */
-	public ConstantValueDistribution() {
+	public ConstantValueDistributionHandler() {
 		fixedValue = 50;
 	}
 	
@@ -62,7 +63,7 @@ public class ConstantValueDistribution implements
 		 * JPanel which allows configuration of this Distribution
 		 */
 		JPanel panel = new JPanel();
-		panel.setSize(300, 300);
+		panel.setMinimumSize(new Dimension(90, 40));
 		panel.setLayout(null);
 		
 		/**
@@ -82,15 +83,31 @@ public class ConstantValueDistribution implements
 		valueConfig.setLocation(80, 20);
 		valueConfig.setMinimumSize(new Dimension(32, 20));
 		valueConfig.setSize(32, 20);
+		valueConfig.addActionListener(a->{
+			try {
+				/**
+				 * Update the value
+				 */
+				fixedValue = Long.parseLong(valueConfig.getText());
+				valueConfig.setBackground(Color.WHITE);
+			} catch (Exception e) {
+				valueConfig.setBackground(Color.RED);
+			}
+		});
 		return panel;
 	}
 
 	public static void main(String[] args) {
-		ConstantValueDistribution c = new ConstantValueDistribution(12);
+		ConstantValueDistributionHandler c = new ConstantValueDistributionHandler(12);
 		JFrame test = new JFrame("test");
 		test.setSize(400, 400);
 		test.add(c.getConfigurationPanel());
 		test.setEnabled(true);
 		test.setVisible(true);
 	}
+
+	@Override
+	public String getSimpleDescription() {
+		return "Constant Value";
+	}
 }

+ 167 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/distributionHandler/NormalDistributionHandler.java

@@ -0,0 +1,167 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.distributionHandler;
+
+import java.awt.Color;
+import java.awt.Dimension;
+
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JTextField;
+
+import org.apache.commons.math3.distribution.NormalDistribution;
+import org.apache.commons.math3.random.RandomGenerator;
+
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ProbabilityDistributionHandler;
+
+/**
+ * Example class representing a Normal Distribution
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class NormalDistributionHandler implements
+		ProbabilityDistributionHandler {
+
+	/**
+	 * Mean value
+	 */
+	private double mean;
+	
+	/**
+	 * Standard Deviation
+	 */
+	private double sd;
+	
+	/**
+	 * Normal Distribution
+	 */
+	private NormalDistribution dist;
+	
+	/**
+	 * 
+	 */
+	private RandomGenerator gen = null;
+	
+	/**
+	 * Creates a new distribution which returns the given value
+	 * @param value value to be returned
+	 */
+	public NormalDistributionHandler(double mean, double sd) {
+		this.mean = mean;
+		this.sd = sd;
+		dist = new NormalDistribution(mean, sd);
+	}
+
+	/**
+	 * Creates a default distribution, which returns 50.
+	 */
+	public NormalDistributionHandler() {
+		this.mean = 50.0;
+		this.sd = 12.0;
+		dist = new NormalDistribution(mean, sd);
+	}
+	
+	@Override
+	public void setRandomGenerator(RandomGenerator rng) {
+		gen = rng;
+		dist = new NormalDistribution(rng, mean, sd);
+	}
+
+	@Override
+	public long sampleNextValue() {
+		return (long)Math.round(dist.sample());
+	}
+
+	@Override
+	public JPanel getConfigurationPanel() {
+		/**
+		 * JPanel which allows configuration of this Distribution
+		 */
+		JPanel panel = new JPanel();
+		panel.setMinimumSize(new Dimension(90, 80));
+		panel.setLayout(null);
+		
+		/**
+		 * Label 
+		 */
+		JLabel label = new JLabel("Mean: ");
+		panel.add(label);
+		label.setLocation(20, 20);
+		label.setSize(50,20);
+		label.setMinimumSize(new Dimension(50, 20));
+
+		/**
+		 * Textfield for changing the value
+		 */
+		JTextField valueConfig = new JTextField(""+mean);
+		panel.add(valueConfig);
+		valueConfig.setLocation(80, 20);
+		valueConfig.setMinimumSize(new Dimension(70, 20));
+		valueConfig.setSize(70, 20);
+		valueConfig.addActionListener(a->{
+			try {
+				/**
+				 * Update the value
+				 */
+				mean = Double.parseDouble(valueConfig.getText());
+				initializeDistribution();
+				valueConfig.setBackground(Color.WHITE);
+			} catch (Exception e) {
+				valueConfig.setBackground(Color.RED);
+			}
+		});
+		
+		
+		
+		/**
+		 * Label 
+		 */
+		JLabel lbSD = new JLabel("SD: ");
+		panel.add(lbSD);
+		lbSD.setLocation(20, 50);
+		lbSD.setSize(50,20);
+		lbSD.setMinimumSize(new Dimension(50, 20));
+
+		/**
+		 * Textfield for changing the value
+		 */
+		JTextField tfSDvalue = new JTextField(""+sd);
+		panel.add(tfSDvalue);
+		tfSDvalue.setLocation(80, 50);
+		tfSDvalue.setMinimumSize(new Dimension(70, 20));
+		tfSDvalue.setSize(70, 20);
+		tfSDvalue.addActionListener(a->{
+			try {
+				/**
+				 * Update the value
+				 */
+				sd = Double.parseDouble(tfSDvalue.getText());
+				tfSDvalue.setBackground(Color.WHITE);
+			} catch (Exception e) {
+				tfSDvalue.setBackground(Color.RED);
+			}
+		});
+		return panel;
+	}
+
+	private void initializeDistribution() {
+		if(gen==null)
+			dist = new NormalDistribution(mean, sd);
+		else
+			dist = new NormalDistribution(gen, mean, sd);
+			
+	}
+
+	public static void main(String[] args) {
+		NormalDistributionHandler c = new NormalDistributionHandler(12.0, 5.0);
+		JFrame test = new JFrame("test");
+		test.setSize(400, 400);
+		test.add(c.getConfigurationPanel());
+		test.setEnabled(true);
+		test.setVisible(true);
+	}
+
+	@Override
+	public String getSimpleDescription() {
+		return "Normal Distribution";
+	}
+}

+ 1 - 3
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/distributionHandler/package-info.java

@@ -1,7 +1,5 @@
 /**
- * 
- */
-/**
+ * Packet for the different Distribution Handler, which allow configuration and storage of different Distributions Functions for Ports. 
  *
  * @author Andreas T. Meyer-Berg
  */

+ 2 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/scheduler/Scheduler.java

@@ -41,6 +41,8 @@ public class Scheduler {
 			
 			System.out.println("Min: "+minimumTimeStep+"  Event: "+event.getEventTime());
 			System.out.println("Could not schedule: "+event.toString());
+			System.out.println("Simulate now ");
+			event.simulateEvent();//TODO: Maybe other solution
 			throw new Error("Fail");
 			
 			//return false;

+ 139 - 8
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/popups/PortDistributionConfigurationPopUp.java

@@ -4,27 +4,63 @@ import javax.swing.JFrame;
 import javax.swing.JPanel;
 
 import java.awt.BorderLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.WindowAdapter;
+import java.util.LinkedList;
 import java.util.Observable;
 import java.util.Observer;
 
 import javax.swing.JComboBox;
 import javax.swing.JLabel;
 import javax.swing.JButton;
+import javax.swing.JOptionPane;
+import javax.swing.JScrollPane;
 import javax.swing.JSplitPane;
 import javax.swing.border.EmptyBorder;
 
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ProbabilityDistributionHandler;
 
 public class PortDistributionConfigurationPopUp extends JFrame implements Observer{
 	
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+
 	private Port port;
 	
 	private JSplitPane splitPane;
 	
-	public PortDistributionConfigurationPopUp(Port port) {
+	private JScrollPane scrollPane;
+	/**
+	 * ComboBox for link selection
+	 */
+	private JComboBox<String> cmbDistribution;
+	
+	/**
+	 * Distributions that can be selected;
+	 */
+	LinkedList<Class<? extends ProbabilityDistributionHandler>> availableDistributions;
+	
+	/**
+	 * Last index which was selected in the combo box
+	 */
+	int lastIndex = -1;
+	
+	boolean mutex = false;
+	
+	private Controller controller;
+	
+	private PortDistributionConfigurationPopUp that = this;
+	
+	public PortDistributionConfigurationPopUp(Port port, Controller controller) {
 		this.port = port;
-		this.setSize(300, 500);
+		this.controller = controller;
+		this.setSize(480, 360);
 		splitPane = new JSplitPane();
 		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
 		splitPane.setBorder(new EmptyBorder(0, 0, 0, 0));
@@ -38,17 +74,78 @@ public class PortDistributionConfigurationPopUp extends JFrame implements Observ
 		lblDescription.setBounds(10, 10, 120, 20);
 		panel.add(lblDescription);
 		
-		JComboBox comboBox = new JComboBox();
-		comboBox.setBounds(120, 10, 190, 20);
-		panel.add(comboBox);
+		cmbDistribution = new JComboBox<String>();
+		cmbDistribution.setBounds(120, 10, 190, 20);
+		panel.add(cmbDistribution);
 		
 		JButton btnImport = new JButton("Import");
 		btnImport.setBounds(320, 10, 100, 20);
 		panel.add(btnImport);
+		btnImport.addActionListener(a -> {
+			ImportPopUp<ProbabilityDistributionHandler> popUp = new ImportPopUp<ProbabilityDistributionHandler>(this, ProbabilityDistributionHandler.class);
+			try {
+				Class<? extends ProbabilityDistributionHandler> imported = popUp.showPopUp();
+				if (imported == null)
+					return;
+				if (controller.getImportController().addDistributionHandler(imported)) {
+					update(null, null);
+				} else {
+					JOptionPane.showMessageDialog(that, "Import failed: Invalid Distribution Handler");
+				}
+			} catch (Exception e1) {
+				JOptionPane.showMessageDialog(that, "Import failed: " + e1.getMessage());
+			}
+		});
 
 		splitPane.setDividerLocation(40);
 		
+		scrollPane = new JScrollPane();
+		splitPane.setRightComponent(scrollPane);
+		
 		update(null,null);
+		
+		this.addWindowListener(new WindowAdapter() {
+			public void windowClosing(java.awt.event.WindowEvent e) {
+				that.controller.removeObserver(that);
+			}
+		});
+		
+		cmbDistribution.addActionListener(new ActionListener() {
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				if (mutex)
+					return;
+
+				/**
+				 * New Distribution
+				 */
+				ProbabilityDistributionHandler newDistribution = null;
+				try {
+					// Create new Instance of the protocol
+					newDistribution = controller.getImportController().getDistributionHandlers()
+							.get(cmbDistribution.getSelectedIndex()).newInstance();
+				} catch (InstantiationException | IllegalAccessException e1) {
+					System.out.println("WARNING: Distribution could not be initialized");
+				}
+				if (newDistribution == null) {
+					cmbDistribution.setSelectedIndex(lastIndex);
+					System.out.println("WARNING: Invalid Distribution Selected - restore last index");
+				} else {
+					/**
+					 * Add new Distribution
+					 */
+					port.setTriggerHandler(newDistribution);
+					/**
+					 * Update Index
+					 */
+					lastIndex = cmbDistribution.getSelectedIndex();
+					
+					update(null, null);
+				}
+			}
+		});
+		
+		this.controller.addObserver(this);
 	}
 
 	@Override
@@ -57,8 +154,42 @@ public class PortDistributionConfigurationPopUp extends JFrame implements Observ
 		ProbabilityDistributionHandler handler = port.getTriggerHandler();
 		
 		if(handler!=null)
-			splitPane.setRightComponent(handler.getConfigurationPanel());
-		else
-			splitPane.setRightComponent(null);
+			scrollPane.setViewportView(handler.getConfigurationPanel());
+		else{
+			scrollPane.setViewportView(null);
+			return;
+		}
+		
+		
+		mutex = true;
+		/**
+		 *  Update Distribution function
+		 */
+		availableDistributions = controller.getImportController().getDistributionHandlers();
+		cmbDistribution.removeAllItems();
+		for (int i = 0; i < availableDistributions.size(); i++)
+			try {
+				cmbDistribution.addItem(availableDistributions.get(i).newInstance().getSimpleDescription());
+			} catch (InstantiationException | IllegalAccessException e1) {
+				System.out.println("Distribution " + i + " is invalid");
+				cmbDistribution.addItem("unknown");
+			}
+		// Set Index to selected Protocol
+		lastIndex = -1;
+		for (int i = 0; i < availableDistributions.size(); i++) {
+			if (handler.getClass().equals(availableDistributions.get(i))) {
+				// Select the right protocol and save last index
+				lastIndex = i;
+			}
+		}
+		cmbDistribution.setSelectedIndex(lastIndex);
+		
+		mutex = false;
+	}
+	
+	public static void main(String[] args) {
+		PortDistributionConfigurationPopUp test = new PortDistributionConfigurationPopUp(null, new Controller(new Model()));
+		test.setEnabled(true);
+		test.setVisible(true);
 	}
 }

+ 7 - 4
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/popups/PortEditorPanel.java

@@ -17,9 +17,7 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionPerformance;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ProbabilityDistributionHandler;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.distributionHandler.ConstantValueDistribution;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.MQTT_protocol;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleLink;
  
@@ -79,13 +77,18 @@ public class PortEditorPanel extends JPanel
      */
     private JTextField tfJitter;
     
+    /**
+     * Controller for notifying other parts of the Framework
+     */
+    private Controller controller;
+    
     /**
      * Creates a new PortEditorPanel, which allows editing of the Ports of toEdit
      * @param toEdit SmartDevice, which ports will be edited
      * @param controller Controller to modify the Model
      */
     public PortEditorPanel(SmartDevice toEdit, Controller controller) {
-    	
+    	this.controller = controller;
     	this.toEdit = toEdit;
         //Create the list of ports
         Port[] ports = new Port[toEdit.getPorts().size()];
@@ -284,7 +287,7 @@ public class PortEditorPanel extends JPanel
 		 */
 		Port toChange = toEdit.getPorts().get(list.getSelectedIndex());
 		
-		PortDistributionConfigurationPopUp popUp = new PortDistributionConfigurationPopUp(toChange);
+		PortDistributionConfigurationPopUp popUp = new PortDistributionConfigurationPopUp(toChange, controller);
 		popUp.setEnabled(true);
 		popUp.setVisible(true);
 		popUp.setLocationRelativeTo(this);

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

@@ -100,7 +100,7 @@ public class SmartDeviceCreationPopUp extends JDialog {
 	 */
 	public SmartDeviceCreationPopUp(SmartDevice deviceToEdit, boolean edit, Controller control) {
 
-		setModal(true);
+		//setModal(true);
 		// setType(Type.POPUP); -> Crashes on Linux
 		this.controller = control;
 		this.maxX = controller.getSettingsController().getWidth();