NormalDistributionHandler.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.distributionHandler;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import javax.swing.JFrame;
  5. import javax.swing.JLabel;
  6. import javax.swing.JPanel;
  7. import javax.swing.JTextField;
  8. import org.apache.commons.math3.distribution.NormalDistribution;
  9. import org.apache.commons.math3.random.RandomGenerator;
  10. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ProbabilityDistributionHandler;
  11. /**
  12. * Example class representing a Normal Distribution
  13. *
  14. * @author Andreas T. Meyer-Berg
  15. */
  16. public class NormalDistributionHandler implements
  17. ProbabilityDistributionHandler {
  18. /**
  19. * Mean value
  20. */
  21. private double mean;
  22. /**
  23. * Standard Deviation
  24. */
  25. private double sd;
  26. /**
  27. * Normal Distribution
  28. */
  29. private NormalDistribution dist;
  30. /**
  31. *
  32. */
  33. private RandomGenerator gen = null;
  34. /**
  35. * Creates a new distribution which returns the given value
  36. * @param value value to be returned
  37. */
  38. public NormalDistributionHandler(double mean, double sd) {
  39. this.mean = mean;
  40. this.sd = sd;
  41. dist = new NormalDistribution(mean, sd);
  42. }
  43. /**
  44. * Creates a default distribution, which returns 50.
  45. */
  46. public NormalDistributionHandler() {
  47. this.mean = 50.0;
  48. this.sd = 12.0;
  49. dist = new NormalDistribution(mean, sd);
  50. }
  51. @Override
  52. public void setRandomGenerator(RandomGenerator rng) {
  53. gen = rng;
  54. dist = new NormalDistribution(rng, mean, sd);
  55. }
  56. @Override
  57. public long sampleNextValue() {
  58. return (long)Math.round(dist.sample());
  59. }
  60. @Override
  61. public JPanel getConfigurationPanel() {
  62. /**
  63. * JPanel which allows configuration of this Distribution
  64. */
  65. JPanel panel = new JPanel();
  66. panel.setMinimumSize(new Dimension(90, 80));
  67. panel.setLayout(null);
  68. /**
  69. * Label
  70. */
  71. JLabel label = new JLabel("Mean: ");
  72. panel.add(label);
  73. label.setLocation(20, 20);
  74. label.setSize(50,20);
  75. label.setMinimumSize(new Dimension(50, 20));
  76. /**
  77. * Textfield for changing the value
  78. */
  79. JTextField valueConfig = new JTextField(""+mean);
  80. panel.add(valueConfig);
  81. valueConfig.setLocation(80, 20);
  82. valueConfig.setMinimumSize(new Dimension(70, 20));
  83. valueConfig.setSize(70, 20);
  84. valueConfig.addActionListener(a->{
  85. try {
  86. /**
  87. * Update the value
  88. */
  89. mean = Double.parseDouble(valueConfig.getText());
  90. initializeDistribution();
  91. valueConfig.setBackground(Color.WHITE);
  92. } catch (Exception e) {
  93. valueConfig.setBackground(Color.RED);
  94. }
  95. });
  96. /**
  97. * Label
  98. */
  99. JLabel lbSD = new JLabel("SD: ");
  100. panel.add(lbSD);
  101. lbSD.setLocation(20, 50);
  102. lbSD.setSize(50,20);
  103. lbSD.setMinimumSize(new Dimension(50, 20));
  104. /**
  105. * Textfield for changing the value
  106. */
  107. JTextField tfSDvalue = new JTextField(""+sd);
  108. panel.add(tfSDvalue);
  109. tfSDvalue.setLocation(80, 50);
  110. tfSDvalue.setMinimumSize(new Dimension(70, 20));
  111. tfSDvalue.setSize(70, 20);
  112. tfSDvalue.addActionListener(a->{
  113. try {
  114. /**
  115. * Update the value
  116. */
  117. sd = Double.parseDouble(tfSDvalue.getText());
  118. tfSDvalue.setBackground(Color.WHITE);
  119. } catch (Exception e) {
  120. tfSDvalue.setBackground(Color.RED);
  121. }
  122. });
  123. return panel;
  124. }
  125. private void initializeDistribution() {
  126. if(gen==null)
  127. dist = new NormalDistribution(mean, sd);
  128. else
  129. dist = new NormalDistribution(gen, mean, sd);
  130. }
  131. public static void main(String[] args) {
  132. NormalDistributionHandler c = new NormalDistributionHandler(12.0, 5.0);
  133. JFrame test = new JFrame("test");
  134. test.setSize(400, 400);
  135. test.add(c.getConfigurationPanel());
  136. test.setEnabled(true);
  137. test.setVisible(true);
  138. }
  139. @Override
  140. public String getSimpleDescription() {
  141. return "Normal Distribution";
  142. }
  143. }