Browse Source

Adds Basis for Distribution Sampling, Import Math framework

Andreas T. Meyer-Berg 4 years ago
parent
commit
7d307f326a

+ 49 - 47
build.gradle

@@ -1,47 +1,49 @@
-/*
- * This file was generated by the Gradle 'init' task.
- *
- * This is a general purpose Gradle build.
- * Learn how to create Gradle builds at https://guides.gradle.org/creating-new-gradle-builds/
- */
-
-apply plugin: 'java'
-apply plugin: 'eclipse'
-apply plugin: 'application'
-
-group = groupName
-version = versionNumber
-
-//application allows the Task 'run' which runs the main class
-mainClassName = "de.tu_darmstadt.tk.SmartHomeNetworkSim.Main"
-
-repositories {
-	jcenter()
-}
-
-//Creates the Jar
-jar {
-    manifest {
-        attributes 'Main-Class': 'de.tu_darmstadt.tk.SmartHomeNetworkSim.Main'
-    }
-}
-
-test {
-	ignoreFailures = true
-}
-
-//Runs the created Jar
-task runIt(dependsOn:jar) {
-    doLast {
-        javaexec { 
-            main="-jar"; args jar.archivePath
-        }
-    }
-}
-
-dependencies {
-	compile 'org.pcap4j:pcap4j-core:1.+'
-	compile 'org.pcap4j:pcap4j-packetfactory-static:1.+'
-
-	testCompile 'junit:junit:4.12'
-}
+/*
+ * This file was generated by the Gradle 'init' task.
+ *
+ * This is a general purpose Gradle build.
+ * Learn how to create Gradle builds at https://guides.gradle.org/creating-new-gradle-builds/
+ */
+
+apply plugin: 'java'
+apply plugin: 'eclipse'
+apply plugin: 'application'
+
+group = groupName
+version = versionNumber
+
+//application allows the Task 'run' which runs the main class
+mainClassName = "de.tu_darmstadt.tk.SmartHomeNetworkSim.Main"
+
+repositories {
+	jcenter()
+}
+
+//Creates the Jar
+jar {
+    manifest {
+        attributes 'Main-Class': 'de.tu_darmstadt.tk.SmartHomeNetworkSim.Main'
+    }
+}
+
+test {
+	ignoreFailures = true
+}
+
+//Runs the created Jar
+task runIt(dependsOn:jar) {
+    doLast {
+        javaexec { 
+            main="-jar"; args jar.archivePath
+        }
+    }
+}
+
+dependencies {
+	compile 'org.pcap4j:pcap4j-core:1.+'
+	compile 'org.pcap4j:pcap4j-packetfactory-static:1.+'
+	// https://mvnrepository.com/artifact/org.apache.commons/commons-math3
+	compile group: 'org.apache.commons', name: 'commons-math3', version: '3.6.1'
+	
+	testCompile 'junit:junit:4.12'
+}

+ 64 - 10
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Port.java

@@ -3,6 +3,11 @@ package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
 import java.util.Collection;
 import java.util.Random;
 
+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.scheduler.Schedulable;
 
 /**
@@ -43,12 +48,6 @@ public class Port implements Schedulable {
 	 * Connection this Port listens to
 	 */
 	private Connection connection;
-	
-	/**
-	 * Constant time interval, which triggers new traffic every
-	 * {@code triggerInterval} milliseconds.
-	 */
-	private long triggerInterval;
 
 	/**
 	 * Last timestep traffic was triggered
@@ -75,6 +74,17 @@ public class Port implements Schedulable {
 	 */
 	private short portNumber;
 
+	/**
+	 * Constant time interval, which triggers new traffic every
+	 * {@code triggerInterval} milliseconds.
+	 */
+	private long triggerInterval;
+
+	/**
+	 * Probability Distribution for calculating the next Trigger
+	 */
+	private ProbabilityDistributionHandler triggerIntervalStat;
+	
 	/**
 	 * Creates a new Port for the given Device with the specified PortNumber
 	 * @param device SmartDevice this port listens on
@@ -84,7 +94,9 @@ public class Port implements Schedulable {
 		status = SENDING;
 		owner = device;
 		connection = null;
-		setTriggerInterval(new Random().nextInt(1000)+1);
+		triggerIntervalStat = new ConstantValueDistribution(new Random().nextInt(1000)+1);
+		triggerInterval = triggerIntervalStat.sampleNextValue();
+		//setTriggerInterval(new Random().nextInt(1000)+1);
 		lastTrigger = 0;
 		jitter = (short)(new Random().nextInt(5)+1);
 		currentJitter = (short)Math.round(Math.random()*jitter);
@@ -102,7 +114,8 @@ public class Port implements Schedulable {
 		status = SENDING;
 		owner = device;
 		connection = null;
-		this.triggerInterval = triggerInterval;
+		triggerIntervalStat = new ConstantValueDistribution(triggerInterval);
+		this.triggerInterval = triggerIntervalStat.sampleNextValue();
 		lastTrigger = 0;
 		jitter = 0;
 		currentJitter = (short)Math.round(Math.random()*jitter);
@@ -123,7 +136,8 @@ public class Port implements Schedulable {
 		status = SENDING;
 		owner = device;
 		connection = null;
-		this.triggerInterval = triggerInterval;
+		triggerIntervalStat = new ConstantValueDistribution(triggerInterval);
+		this.triggerInterval = triggerIntervalStat.sampleNextValue();
 		this.lastTrigger = lastTrigger;
 		this.jitter = jitter;
 		currentJitter = (short)Math.round(Math.random()*jitter);
@@ -163,17 +177,48 @@ public class Port implements Schedulable {
 	}
 
 	/**
+	 * TriggerInterval is sampled from distribution after previous simulation
 	 * @return the triggerInterval
 	 */
 	public long getTriggerInterval() {
 		return triggerInterval;
 	}
 
+	/**
+	 * Returns the Probability Handler for the trigger interval, which allows configuration of the Distribution
+	 * @return Distribution Handler for configuration
+	 */
+	public ProbabilityDistributionHandler getTriggerHandler(){
+		return triggerIntervalStat;
+	}
+	
+	/**
+	 * Set a new Probability Distribution Handler to sample the trigger interval
+	 * @param triggerHandler Sets the Probability Distribution to generate the trigger interval
+	 */
+	public void setTriggerHandler(ProbabilityDistributionHandler triggerHandler){
+		this.triggerIntervalStat = triggerHandler;
+		boolean removed = SimulationManager.removeEvent(this);
+		this.triggerInterval = triggerHandler.sampleNextValue();
+		if(removed)
+			SimulationManager.scheduleEvent(this);
+	}
+	
+	/**
+	 * Samples a new Value for the TriggerInterval
+	 */
+	public void resampleTriggerInterval(){
+		boolean removed = SimulationManager.removeEvent(this);
+		this.triggerInterval = triggerIntervalStat.sampleNextValue();
+		if(removed)
+			SimulationManager.scheduleEvent(this);
+	}
 	/**
 	 * @param triggerInterval
 	 *            the triggerInterval to set
 	 */
-	public void setTriggerInterval(long triggerInterval) {
+	@Deprecated
+	private void setTriggerInterval(long triggerInterval) {
 		if(triggerInterval<=0)
 			this.triggerInterval = (long)1;
 		else
@@ -321,8 +366,17 @@ public class Port implements Schedulable {
 		
 		connection.getLink().addPackets(packets);
 		
+		//TODO: Sample next game
+		triggerInterval = triggerIntervalStat.sampleNextValue();
+		
 		if(status==Port.SENDING)
 			SimulationManager.scheduleEvent(this);
 		
 	}
+	
+	public static void main(String[] args) {
+		AbstractRealDistribution dist = new LaplaceDistribution(30, 50);
+		for(int i = 0; i<100; i++)
+			System.out.println(dist.sample());
+	}
 }

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

@@ -0,0 +1,32 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
+
+import javax.swing.JPanel;
+
+import org.apache.commons.math3.random.RandomGenerator;
+
+/**
+ * Allows selection and configuration of such a class in view. Also Import & visualization of the example panel.
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public interface ProbabilityDistributionHandler {
+
+	/**
+	 * Set the random generator which should be used
+	 * @param rng random generator
+	 */
+	public void setRandomGenerator(RandomGenerator rng);
+
+	/**
+	 * Next value from the distribution
+	 * @return next sample value
+	 */
+	public long sampleNextValue();
+	
+	/**
+	 * Returns a configuration panel for the distribution.
+	 * Should be set enabled, visible and embedded by the calling methods.
+	 * @return panel for configuration
+	 */
+	public JPanel getConfigurationPanel();
+}

+ 96 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/distributionHandler/ConstantValueDistribution.java

@@ -0,0 +1,96 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.distributionHandler;
+
+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.random.RandomGenerator;
+
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ProbabilityDistributionHandler;
+
+/**
+ * Example class representing 
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class ConstantValueDistribution implements
+		ProbabilityDistributionHandler {
+
+	/**
+	 * Fixed value this distribution returns
+	 */
+	private long fixedValue;
+	
+	/**
+	 * Creates a new distribution which returns the given value
+	 * @param value value to be returned
+	 */
+	public ConstantValueDistribution(long value) {
+		fixedValue = value;
+	}
+
+	/**
+	 * Creates a default distribution, which returns 50.
+	 */
+	public ConstantValueDistribution() {
+		fixedValue = 50;
+	}
+	
+	@Override
+	public void setRandomGenerator(RandomGenerator rng) {
+		// Fixed Value requires no randomness
+	}
+
+	@Override
+	public long sampleNextValue() {
+		return fixedValue;
+	}
+
+	/**
+	 * Sets the sample value to the given one
+	 * @param value new fixed value
+	 */
+	public void setValue(long value){
+		this.fixedValue = value;
+	}
+	@Override
+	public JPanel getConfigurationPanel() {
+		/**
+		 * JPanel which allows configuration of this Distribution
+		 */
+		JPanel panel = new JPanel();
+		panel.setSize(300, 300);
+		panel.setLayout(null);
+		
+		/**
+		 * Label 
+		 */
+		JLabel label = new JLabel("Value: ");
+		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(""+fixedValue);
+		panel.add(valueConfig);
+		valueConfig.setLocation(80, 20);
+		valueConfig.setMinimumSize(new Dimension(32, 20));
+		valueConfig.setSize(32, 20);
+		return panel;
+	}
+
+	public static void main(String[] args) {
+		ConstantValueDistribution c = new ConstantValueDistribution(12);
+		JFrame test = new JFrame("test");
+		test.setSize(400, 400);
+		test.add(c.getConfigurationPanel());
+		test.setEnabled(true);
+		test.setVisible(true);
+	}
+}

+ 8 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/distributionHandler/package-info.java

@@ -0,0 +1,8 @@
+/**
+ * 
+ */
+/**
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.distributionHandler;

+ 10 - 21
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/popups/MenuBarNetworkExamples.java

@@ -1,6 +1,5 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups;
 
-import java.awt.print.Paper;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedList;
@@ -249,13 +248,11 @@ public class MenuBarNetworkExamples extends JMenu{
 		B.addLink(link);
 		C.addLink(link);
 		
-		Port a = new Port(A, (short) 1);
+		Port a = new Port(A, (short) 1, 68);
 		a.setLastTrigger(0);
-		a.setTriggerInterval(68);
 		a.setStatus(Port.SENDING);
 		A.addPort(a);
-		Port b = new Port(B, (short) 2);
-		b.setTriggerInterval(102);
+		Port b = new Port(B, (short) 2, 102);
 		b.setStatus(Port.SENDING);
 		B.addPort(b);
 		
@@ -285,9 +282,8 @@ public class MenuBarNetworkExamples extends JMenu{
 		broker.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
 		networkController.addSmartDevice(broker);
 		
-		Port brokerPort = new Port(broker, (short) 0);
+		Port brokerPort = new Port(broker, (short) 0, 987);
 		brokerPort.setLastTrigger(0);
-		brokerPort.setTriggerInterval(987);
 		brokerPort.setStatus(Port.OPEN);		
 		broker.addPort(brokerPort);
 		
@@ -316,9 +312,8 @@ public class MenuBarNetworkExamples extends JMenu{
 			link.addDevice(A);
 			A.addLink(link);
 			
-			aP = new Port(A,((short) (3*i+1)));
+			aP = new Port(A,((short) (3*i+1)),100+(int)(Math.random()*900));
 			aP.setLastTrigger(0);
-			aP.setTriggerInterval(100+(int)(Math.random()*900));
 			aP.setJitter((short) (Math.random()*50));
 			aP.setStatus(Port.SENDING);
 			aP.setConnection(con);
@@ -337,9 +332,8 @@ public class MenuBarNetworkExamples extends JMenu{
 			link.addDevice(A1);
 			A1.addLink(link);
 			
-			a1P = new Port(A1,((short) (3*i+1)));
+			a1P = new Port(A1,((short) (3*i+1)),100+(int)(Math.random()*900));
 			a1P.setLastTrigger(0);
-			a1P.setTriggerInterval(100+(int)(Math.random()*900));
 			a1P.setJitter((short) (Math.random()*50));
 			a1P.setStatus(Port.SENDING);
 			a1P.setConnection(con);
@@ -358,9 +352,8 @@ public class MenuBarNetworkExamples extends JMenu{
 			link.addDevice(A2);
 			A2.addLink(link);
 			
-			a2P = new Port(A2,((short) (3*i+1)));
+			a2P = new Port(A2,((short) (3*i+1)),100+(int)(Math.random()*900));
 			a2P.setLastTrigger(0);
-			a2P.setTriggerInterval(100+(int)(Math.random()*900));
 			a2P.setJitter((short) (Math.random()*50));
 			a2P.setStatus(Port.SENDING);
 			a2P.setConnection(con);
@@ -380,9 +373,8 @@ public class MenuBarNetworkExamples extends JMenu{
 			link.addDevice(B);
 			B.addLink(link);
 			
-			bP = new Port(B,((short) (3*i+2)));
+			bP = new Port(B,((short) (3*i+2)),10+(int)(Math.random()*190));
 			bP.setLastTrigger(0);
-			bP.setTriggerInterval(10+(int)(Math.random()*190));
 			bP.setJitter((short) (Math.random()*50));
 			bP.setStatus(Port.SENDING);
 			bP.setConnection(con);
@@ -399,9 +391,8 @@ public class MenuBarNetworkExamples extends JMenu{
 			link.addDevice(C);
 			C.addLink(link);
 			
-			cP = new Port(C,((short) (3*i+1)));
+			cP = new Port(C,((short) (3*i+1)), 50+(int)(Math.random()*450));
 			cP.setLastTrigger(0);
-			cP.setTriggerInterval(50+(int)(Math.random()*450));
 			cP.setJitter((short) (Math.random()*50));
 			cP.setStatus(Port.SENDING);
 			cP.setConnection(con);
@@ -445,8 +436,7 @@ public class MenuBarNetworkExamples extends JMenu{
 		inetAcces.setName("Cloud  Access");
 		networkController.addConnectionToLink(inetAcces, wifi);
 		inetAcces.setProtocol(new SimpleProtocol());
-		Port pRouter = new Port(router, (short)80, 1000L);
-		pRouter.setTriggerInterval(-500L);
+		Port pRouter = new Port(router, (short)80, 500L);
 		networkController.addDeviceToConnectionAndProtocol(pRouter, inetAcces, 0);
 		Port pZigBee = new Port(zigBeeRouter, (short)80, 1000L);
 		networkController.addDeviceToConnectionAndProtocol(pRouter, inetAcces, 0);
@@ -477,10 +467,9 @@ public class MenuBarNetworkExamples extends JMenu{
 		floatSensor.setFSmin(15.0f);
 		floatSensor.setFSmax(32.0f);
 		networkController.addSmartDevice(floatSensor);
-		Port pFloatSensor = new Port(floatSensor, (short)1883);
+		Port pFloatSensor = new Port(floatSensor, (short)1883, 15000);
 		pFloatSensor.setStatus(Port.SENDING);
 		pFloatSensor.setLastTrigger(-357L);
-		pFloatSensor.setTriggerInterval(15000);//15sek
 		networkController.addDeviceToConnectionAndProtocol(pFloatSensor, mqtt,1);
 		
 		

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

@@ -17,7 +17,9 @@ 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;
  
@@ -375,7 +377,13 @@ public class PortEditorPanel extends JPanel
 		//Edit trigger Interval
 		if(tfTriggerInterval.getText()!=""+toChange.getTriggerInterval()){
 			try {
-				toChange.setTriggerInterval(Long.parseLong(tfTriggerInterval.getText()));
+				//TODO: Advanced Triggers
+				ProbabilityDistributionHandler dist = toChange.getTriggerHandler();
+				if(dist instanceof ConstantValueDistribution){
+					((ConstantValueDistribution)dist).setValue(Long.parseLong(tfTriggerInterval.getText()));
+					toChange.resampleTriggerInterval();
+				}
+					//toChange.setTriggerInterval(Long.parseLong(tfTriggerInterval.getText()));
 				tfTriggerInterval.setBackground(Color.WHITE);
 			} catch (Exception e) {
 				tfTriggerInterval.setBackground(Color.RED);