modules/helpers/pluginGen/src/main/java/org/rhq/helpers/ui/ManagedHive.java | 176 ++++++++-- 1 file changed, 152 insertions(+), 24 deletions(-)
New commits: commit 663bff29da20c4b6edd53ba15c02e02e3b6fc2fc Author: Simeon Pinder spinder@fulliautomatix.conchfritter.com Date: Wed Sep 19 18:42:00 2012 -0400
Clean up numerous things about swarm behavior and updates.
diff --git a/modules/helpers/pluginGen/src/main/java/org/rhq/helpers/ui/ManagedHive.java b/modules/helpers/pluginGen/src/main/java/org/rhq/helpers/ui/ManagedHive.java index 6ef28ad..141538f 100644 --- a/modules/helpers/pluginGen/src/main/java/org/rhq/helpers/ui/ManagedHive.java +++ b/modules/helpers/pluginGen/src/main/java/org/rhq/helpers/ui/ManagedHive.java @@ -76,6 +76,11 @@ public class ManagedHive extends JFrame { //initial hive setup for (int i = 0; i < basePopulation; i++) { addBee(); + try { + Thread.sleep(3); + } catch (InterruptedException e1) { + e1.printStackTrace(); + } } }
@@ -84,13 +89,17 @@ public class ManagedHive extends JFrame { /******************* UI Logic & Components **************************/ private int space = 7;//horizontal spacing between components protected static int basePopulation = 50;//resident hive population - private int swarmTime = 10000;//ms. + //swarm time should be (2 or 3)* 30s to allow RHQ to clearly collect angry status. + protected static int swarmTime = 60 * 1000;//ms. protected static Hive hiveComponent; protected static Random generator = new Random(System.currentTimeMillis()); protected static int beeWidth = 15; protected static int beeHeight = 15; protected static JTextField currentPopulation; protected static ManagedHive CONTROLLER = null; + protected static Runnable angryTimer = null; + protected static int angryPackSize = 50; + protected static JLabel mood = null;
/** Responsible for putting together the layout components. * @@ -117,7 +126,7 @@ public class ManagedHive extends JFrame {
{ //monitor row shows current state of the hive - JLabel currentPopulationLabel = new JLabel("Bee count"); + JLabel currentPopulationLabel = new JLabel("Current Bee count"); monitorRow.add(currentPopulationLabel); monitorRow.add(Box.createHorizontalStrut(space)); currentPopulation = new JTextField("" + basePopulation); @@ -133,7 +142,7 @@ public class ManagedHive extends JFrame { monitorRow.add(maxPopulation); monitorRow.add(Box.createHorizontalStrut(space));//spacer
- JLabel mood = new JLabel(); + mood = new JLabel(); mood.setOpaque(true); mood.setBackground(Color.green); mood.setText("Calm"); @@ -146,9 +155,28 @@ public class ManagedHive extends JFrame { shake.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - for (int i = 0; i < 10; i++) { + for (int i = 0; i < angryPackSize; i++) { addBee(); } + //kick the hive into angry mode and set angry timer + if (angryTimer == null) { + angryTimer = new SwarmTimer(); + Thread t = new Thread(angryTimer); + t.start(); + //speed up the bees. + BeeFlight.setDelay(2); + //update the ui to reflect hive mood. + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + ManagedHive.mood.setText("Angry!"); + ManagedHive.mood.setBackground(Color.red); + } + }); + } else {//reset angry timer + SwarmTimer swarmResponseManager = (SwarmTimer) angryTimer; + swarmResponseManager.setExpireTime(swarmTime); + } } }); interactionRow.add(shake); @@ -188,7 +216,6 @@ public class ManagedHive extends JFrame { * Adds a bouncing ball to the canvas and starts a thread to make it bounce */ public static void addBee() { - // Bee b = new Bee(); Bee b = null; //tweak the start position int newX = generator.nextInt(BeeFlight.delta); @@ -335,7 +362,17 @@ class BeeFlight implements Runnable {
public static final int STEPS = 10000;
- public static final int DELAY = 5; + public static int DELAY = 5; + + public static int getDelay() { + return DELAY; + } + + public static void setDelay(int delay) { + if ((delay >= 2) || (delay <= 6)) {//2 <delay <= 6 > + DELAY = delay; + }//otherwise ignore + }
public static int delta = 300;
@@ -360,3 +397,43 @@ class BeeFlight implements Runnable { } } } + +//swarm anger timer +class SwarmTimer implements Runnable { + private static int timeToLive; + + public SwarmTimer() { + timeToLive = ManagedHive.swarmTime;//default to 1 minute + } + + @Override + public void run() { + try { + while (timeToLive > 0) { + Thread.sleep(1000);//sleep for a second + timeToLive = timeToLive - 1000; + } + //reset visual hive state flags + //update the fields + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + ManagedHive.mood.setText("Calm"); + ManagedHive.mood.setBackground(Color.green); + //speed up the bees. + BeeFlight.setDelay(5); + } + }); + //null out angrySwarm + ManagedHive.angryTimer = null; + } catch (InterruptedException e) { + } + } + + public void setExpireTime(int swarmTime) { + //only accept swarm times less than 10 mins and greater then 1 min(s). + if ((swarmTime >= 1000 * 60) || (swarmTime <= 1000 * 60 * 10)) { + timeToLive = swarmTime; + }//otherwise ignore. + } +}
commit 77c9f18472969610c0faeae09f3b3f530015ba16 Author: Simeon Pinder spinder@fulliautomatix.conchfritter.com Date: Wed Sep 19 16:39:10 2012 -0400
Fix issue with concurrent modification of population.
diff --git a/modules/helpers/pluginGen/src/main/java/org/rhq/helpers/ui/ManagedHive.java b/modules/helpers/pluginGen/src/main/java/org/rhq/helpers/ui/ManagedHive.java index 76de0bf..6ef28ad 100644 --- a/modules/helpers/pluginGen/src/main/java/org/rhq/helpers/ui/ManagedHive.java +++ b/modules/helpers/pluginGen/src/main/java/org/rhq/helpers/ui/ManagedHive.java @@ -83,14 +83,14 @@ public class ManagedHive extends JFrame {
/******************* UI Logic & Components **************************/ private int space = 7;//horizontal spacing between components - protected static int basePopulation = 50; + protected static int basePopulation = 50;//resident hive population private int swarmTime = 10000;//ms. protected static Hive hiveComponent; protected static Random generator = new Random(System.currentTimeMillis()); protected static int beeWidth = 15; protected static int beeHeight = 15; protected static JTextField currentPopulation; - protected static ManagedHive THE_HIVE = null; + protected static ManagedHive CONTROLLER = null;
/** Responsible for putting together the layout components. * @@ -146,7 +146,6 @@ public class ManagedHive extends JFrame { shake.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - // addBee(); for (int i = 0; i < 10; i++) { addBee(); } @@ -172,7 +171,7 @@ public class ManagedHive extends JFrame { setVisible(true);
//assigned shared reference. - THE_HIVE = this; + CONTROLLER = this; }
private void buildCenterPanel(final JPanel center) { @@ -211,34 +210,41 @@ class Hive extends JComponent { }
public void add(Bee b) { - population.add(b); + synchronized (population) { + population.add(b); + } }
public void removeBee() { - if (population.size() > 0){ - population.remove(0); - } - //if population falls below basePopulation level then add another bee - if (population.size() < ManagedHive.THE_HIVE.basePopulation) { - int delta = ManagedHive.THE_HIVE.basePopulation - population.size(); - for (int i = 0; i <= delta; i++) {//replenish - ManagedHive.THE_HIVE.addBee(); + synchronized (population) { + if (population.size() > 0) { + population.remove(0); + } + //if population falls below basePopulation level then add another bee + if (population.size() < ManagedHive.CONTROLLER.basePopulation) { + int delta = ManagedHive.CONTROLLER.basePopulation - population.size(); + for (int i = 0; i <= delta; i++) {//replenish + ManagedHive.CONTROLLER.addBee(); + } } } }
public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; - for (Bee b : population) { - g2.fill(b.getShape()); - } - //update the fields - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - ManagedHive.currentPopulation.setText(ManagedHive.hiveComponent.getCurrentPopulation() + ""); + synchronized (population) { + + for (Bee b : population) { + g2.fill(b.getShape()); } - }); + //update the fields + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + ManagedHive.currentPopulation.setText(ManagedHive.hiveComponent.getCurrentPopulation() + ""); + } + }); + } } }
commit 8d0b8e9a566fcf9c9e04169dfff4ac1f056dd684 Author: Simeon Pinder spinder@fulliautomatix.conchfritter.com Date: Wed Sep 19 14:18:38 2012 -0400
modify to keep base population above a certain value. -Still has concurrent modification error to fix.
diff --git a/modules/helpers/pluginGen/src/main/java/org/rhq/helpers/ui/ManagedHive.java b/modules/helpers/pluginGen/src/main/java/org/rhq/helpers/ui/ManagedHive.java index 8dde091..76de0bf 100644 --- a/modules/helpers/pluginGen/src/main/java/org/rhq/helpers/ui/ManagedHive.java +++ b/modules/helpers/pluginGen/src/main/java/org/rhq/helpers/ui/ManagedHive.java @@ -74,21 +74,23 @@ public class ManagedHive extends JFrame { //create ui layout initializeUi(); //initial hive setup - // initializeHive(); + for (int i = 0; i < basePopulation; i++) { + addBee(); + } }
/******************* Management capabilities **************************/
/******************* UI Logic & Components **************************/ - // private JTextField hiveDirectory; private int space = 7;//horizontal spacing between components - private int initialPopulation = 50; + protected static int basePopulation = 50; private int swarmTime = 10000;//ms. protected static Hive hiveComponent; protected static Random generator = new Random(System.currentTimeMillis()); protected static int beeWidth = 15; protected static int beeHeight = 15; protected static JTextField currentPopulation; + protected static ManagedHive THE_HIVE = null;
/** Responsible for putting together the layout components. * @@ -118,7 +120,7 @@ public class ManagedHive extends JFrame { JLabel currentPopulationLabel = new JLabel("Bee count"); monitorRow.add(currentPopulationLabel); monitorRow.add(Box.createHorizontalStrut(space)); - currentPopulation = new JTextField("" + initialPopulation); + currentPopulation = new JTextField("" + basePopulation); currentPopulation.setEditable(false); monitorRow.add(currentPopulation); monitorRow.add(Box.createHorizontalStrut(space)); @@ -168,6 +170,9 @@ public class ManagedHive extends JFrame { } }); setVisible(true); + + //assigned shared reference. + THE_HIVE = this; }
private void buildCenterPanel(final JPanel center) { @@ -183,7 +188,7 @@ public class ManagedHive extends JFrame { /** * Adds a bouncing ball to the canvas and starts a thread to make it bounce */ - public void addBee() { + public static void addBee() { // Bee b = new Bee(); Bee b = null; //tweak the start position @@ -210,8 +215,16 @@ class Hive extends JComponent { }
public void removeBee() { - if (population.size() > 0) + if (population.size() > 0){ population.remove(0); + } + //if population falls below basePopulation level then add another bee + if (population.size() < ManagedHive.THE_HIVE.basePopulation) { + int delta = ManagedHive.THE_HIVE.basePopulation - population.size(); + for (int i = 0; i <= delta; i++) {//replenish + ManagedHive.THE_HIVE.addBee(); + } + } }
public void paintComponent(Graphics g) {
commit a042a122caa0997f86672c4674f42b61d6e5a75f Author: Simeon Pinder spinder@fulliautomatix.conchfritter.com Date: Wed Sep 19 12:17:26 2012 -0400
update monitoring fields in real time with bee population.
diff --git a/modules/helpers/pluginGen/src/main/java/org/rhq/helpers/ui/ManagedHive.java b/modules/helpers/pluginGen/src/main/java/org/rhq/helpers/ui/ManagedHive.java index 0919231..8dde091 100644 --- a/modules/helpers/pluginGen/src/main/java/org/rhq/helpers/ui/ManagedHive.java +++ b/modules/helpers/pluginGen/src/main/java/org/rhq/helpers/ui/ManagedHive.java @@ -30,8 +30,8 @@ import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; -import java.util.ArrayList; import java.util.Random; +import java.util.Vector;
import javax.swing.Box; import javax.swing.BoxLayout; @@ -41,6 +41,7 @@ import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; +import javax.swing.SwingUtilities; import javax.swing.border.LineBorder;
/** Is a basic ui that generates a very simple managed @@ -83,7 +84,11 @@ public class ManagedHive extends JFrame { private int space = 7;//horizontal spacing between components private int initialPopulation = 50; private int swarmTime = 10000;//ms. - private Hive hiveComponent; + protected static Hive hiveComponent; + protected static Random generator = new Random(System.currentTimeMillis()); + protected static int beeWidth = 15; + protected static int beeHeight = 15; + protected static JTextField currentPopulation;
/** Responsible for putting together the layout components. * @@ -113,7 +118,7 @@ public class ManagedHive extends JFrame { JLabel currentPopulationLabel = new JLabel("Bee count"); monitorRow.add(currentPopulationLabel); monitorRow.add(Box.createHorizontalStrut(space)); - JTextField currentPopulation = new JTextField("" + initialPopulation); + currentPopulation = new JTextField("" + initialPopulation); currentPopulation.setEditable(false); monitorRow.add(currentPopulation); monitorRow.add(Box.createHorizontalStrut(space)); @@ -139,7 +144,10 @@ public class ManagedHive extends JFrame { shake.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - addBee(); + // addBee(); + for (int i = 0; i < 10; i++) { + addBee(); + } } }); interactionRow.add(shake); @@ -179,13 +187,10 @@ public class ManagedHive extends JFrame { // Bee b = new Bee(); Bee b = null; //tweak the start position - Random generator = new Random(System.currentTimeMillis()); int newX = generator.nextInt(BeeFlight.delta); int newY = generator.nextInt(BeeFlight.delta); b = new Bee(newX, newY); - // comp.add(b); hiveComponent.add(b); - // Runnable r = new BeeFlight(b, comp); Runnable r = new BeeFlight(b, hiveComponent); Thread t = new Thread(r); t.start(); @@ -194,17 +199,33 @@ public class ManagedHive extends JFrame {
class Hive extends JComponent { //entire hive population. - private static ArrayList<Bee> population = new ArrayList<Bee>(); + private static Vector<Bee> population = new Vector<Bee>(); + + public int getCurrentPopulation() { + return population.size(); + }
public void add(Bee b) { population.add(b); }
+ public void removeBee() { + if (population.size() > 0) + population.remove(0); + } + public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; for (Bee b : population) { g2.fill(b.getShape()); } + //update the fields + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + ManagedHive.currentPopulation.setText(ManagedHive.hiveComponent.getCurrentPopulation() + ""); + } + }); } }
@@ -214,8 +235,8 @@ class Hive extends JComponent { class Bee {
//properties of typical cartesion component. - private int xWidth = 15; - private int yWidth = 15; + private int xWidth = ManagedHive.beeWidth; + private int yWidth = ManagedHive.beeHeight;
//cartesion components private double x = 0; @@ -234,14 +255,25 @@ class Bee { * Defines the shape of the bee at each call. */ public Ellipse2D getShape() { - //todo: randomly change dimensions to simulate busy - return new Ellipse2D.Double(x, y, xWidth, yWidth); + //randomly change dimensions to simulate flying bee + int nextX = ManagedHive.generator.nextInt(ManagedHive.beeWidth); + int nextY = ManagedHive.generator.nextInt(ManagedHive.beeHeight); + if (nextX < 1) + nextX = 1; + if (nextY < 1) + nextY = 1; + if ((x > 0) && (y > 0)) { + return new Ellipse2D.Double(x, y, nextX, nextY); + } else { + return new Ellipse2D.Double(x, y, 0, 0); + } }
//return to invisible public void clear() { xWidth = 0; xWidth = 0; + ManagedHive.hiveComponent.removeBee(); }
/**