[cooja] Reformatted Visualizer and skins code

This commit is contained in:
Enrico Joerns 2014-04-10 19:03:43 +02:00
parent ea979fa027
commit cef3c4079d
4 changed files with 399 additions and 321 deletions

View file

@ -26,7 +26,6 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
package org.contikios.mrm; package org.contikios.mrm;
import java.awt.Color; import java.awt.Color;
@ -51,6 +50,7 @@ import org.contikios.mrm.ChannelModel.TxPair;
@ClassDescription("Radio environment (MRM)") @ClassDescription("Radio environment (MRM)")
@SupportedArguments(radioMediums = {MRM.class}) @SupportedArguments(radioMediums = {MRM.class})
public class MRMVisualizerSkin implements VisualizerSkin { public class MRMVisualizerSkin implements VisualizerSkin {
private static final Logger logger = Logger.getLogger(MRMVisualizerSkin.class); private static final Logger logger = Logger.getLogger(MRMVisualizerSkin.class);
private Simulation simulation = null; private Simulation simulation = null;
@ -77,7 +77,7 @@ public class MRMVisualizerSkin implements VisualizerSkin {
@Override @Override
public Color[] getColorOf(Mote mote) { public Color[] getColorOf(Mote mote) {
if (visualizer.getSelectedMotes().contains(mote)) { if (visualizer.getSelectedMotes().contains(mote)) {
return new Color[] { Color.CYAN }; return new Color[]{Color.CYAN};
} }
return null; return null;
} }
@ -128,35 +128,36 @@ public class MRMVisualizerSkin implements VisualizerSkin {
public Radio getFromRadio() { public Radio getFromRadio() {
return selectedMote.getInterfaces().getRadio(); return selectedMote.getInterfaces().getRadio();
} }
@Override @Override
public Radio getToRadio() { public Radio getToRadio() {
return dRadio; return dRadio;
} }
}; };
double probArr[] = radioMedium.getChannelModel().getProbability( double probArr[] = radioMedium.getChannelModel().getProbability(
txPair, txPair,
Double.NEGATIVE_INFINITY Double.NEGATIVE_INFINITY
); );
double prob = probArr[0]; double prob = probArr[0];
double ss = probArr[1]; double ss = probArr[1];
if (prob == 0.0d) { if (prob == 0.0d) {
continue; continue;
} }
edges++; edges++;
String msg = String.format("%1.1f%%, %1.2fdB", 100.0*prob, ss); String msg = String.format("%1.1f%%, %1.2fdB", 100.0 * prob, ss);
Point pixel = visualizer.transformPositionToPixel(d.getInterfaces().getPosition()); Point pixel = visualizer.transformPositionToPixel(d.getInterfaces().getPosition());
int msgWidth = fm.stringWidth(msg); int msgWidth = fm.stringWidth(msg);
g.setColor(new Color(1-(float)prob, (float)prob, 0.0f)); g.setColor(new Color(1 - (float) prob, (float) prob, 0.0f));
g.drawLine(x, y, pixel.x, pixel.y); g.drawLine(x, y, pixel.x, pixel.y);
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
g.drawString(msg, pixel.x - msgWidth/2, pixel.y + 2*Visualizer.MOTE_RADIUS + 3); g.drawString(msg, pixel.x - msgWidth / 2, pixel.y + 2 * Visualizer.MOTE_RADIUS + 3);
} }
String msg = dests.length + " edges"; String msg = dests.length + " edges";
int msgWidth = fm.stringWidth(msg); int msgWidth = fm.stringWidth(msg);
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
g.drawString(msg, x - msgWidth/2, y + 2*Visualizer.MOTE_RADIUS + 3); g.drawString(msg, x - msgWidth / 2, y + 2 * Visualizer.MOTE_RADIUS + 3);
} }
} }

File diff suppressed because it is too large Load diff

View file

@ -27,7 +27,6 @@
* SUCH DAMAGE. * SUCH DAMAGE.
* *
*/ */
package org.contikios.cooja.plugins.skins; package org.contikios.cooja.plugins.skins;
import java.awt.Color; import java.awt.Color;
@ -53,113 +52,122 @@ import org.contikios.cooja.radiomediums.DirectedGraphMedium;
@ClassDescription("Radio environment (DGRM)") @ClassDescription("Radio environment (DGRM)")
@SupportedArguments(radioMediums = {DirectedGraphMedium.class}) @SupportedArguments(radioMediums = {DirectedGraphMedium.class})
public class DGRMVisualizerSkin implements VisualizerSkin { public class DGRMVisualizerSkin implements VisualizerSkin {
private static final Logger logger = Logger.getLogger(DGRMVisualizerSkin.class);
private Simulation simulation = null; private static final Logger logger = Logger.getLogger(DGRMVisualizerSkin.class);
private Visualizer visualizer = null;
private Simulation simulation = null;
private Visualizer visualizer = null;
@Override @Override
public void setActive(Simulation simulation, Visualizer vis) { public void setActive(Simulation simulation, Visualizer vis) {
if (!(simulation.getRadioMedium() instanceof DirectedGraphMedium)) { if (!(simulation.getRadioMedium() instanceof DirectedGraphMedium)) {
logger.fatal("Cannot activate DGRM skin for unknown radio medium: " + simulation.getRadioMedium()); logger.fatal("Cannot activate DGRM skin for unknown radio medium: " + simulation.getRadioMedium());
return; return;
} }
this.simulation = simulation; this.simulation = simulation;
this.visualizer = vis; this.visualizer = vis;
} }
@Override @Override
public void setInactive() { public void setInactive() {
if (simulation == null) { if (simulation == null) {
/* Skin was never activated */ /* Skin was never activated */
return; return;
} }
} }
@Override @Override
public Color[] getColorOf(Mote mote) { public Color[] getColorOf(Mote mote) {
if (visualizer.getSelectedMotes().contains(mote)) { if (visualizer.getSelectedMotes().contains(mote)) {
return new Color[] { Color.CYAN }; return new Color[]{Color.CYAN};
} }
return null; return null;
} }
@Override @Override
public void paintBeforeMotes(Graphics g) { public void paintBeforeMotes(Graphics g) {
Set<Mote> selectedMotes = visualizer.getSelectedMotes(); Set<Mote> selectedMotes = visualizer.getSelectedMotes();
if (simulation == null || selectedMotes == null) { if (simulation == null || selectedMotes == null) {
return; return;
} }
for (final Mote selectedMote : selectedMotes) { for (final Mote selectedMote : selectedMotes) {
if (selectedMote.getInterfaces().getRadio() == null) { if (selectedMote.getInterfaces().getRadio() == null) {
continue; continue;
} }
/* Paint transmission and interference range for selected mote */ /* Paint transmission and interference range for selected mote */
Position motePos = selectedMote.getInterfaces().getPosition(); Position motePos = selectedMote.getInterfaces().getPosition();
Point pixelCoord = visualizer.transformPositionToPixel(motePos); Point pixelCoord = visualizer.transformPositionToPixel(motePos);
int x = pixelCoord.x; int x = pixelCoord.x;
int y = pixelCoord.y; int y = pixelCoord.y;
Radio selectedRadio = selectedMote.getInterfaces().getRadio(); Radio selectedRadio = selectedMote.getInterfaces().getRadio();
FontMetrics fm = g.getFontMetrics(); FontMetrics fm = g.getFontMetrics();
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
DirectedGraphMedium radioMedium = (DirectedGraphMedium) simulation.getRadioMedium(); DirectedGraphMedium radioMedium = (DirectedGraphMedium) simulation.getRadioMedium();
/* Print transmission success probabilities */ /* Print transmission success probabilities */
DestinationRadio[] dests = radioMedium.getPotentialDestinations(selectedRadio); DestinationRadio[] dests = radioMedium.getPotentialDestinations(selectedRadio);
if (dests == null || dests.length == 0) { if (dests == null || dests.length == 0) {
String msg = "No edges"; String msg = "No edges";
int msgWidth = fm.stringWidth(msg); int msgWidth = fm.stringWidth(msg);
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
g.drawString(msg, x - msgWidth / 2, y + 2 * Visualizer.MOTE_RADIUS + 3); g.drawString(msg, x - msgWidth / 2, y + 2 * Visualizer.MOTE_RADIUS + 3);
continue; continue;
} }
String msg = dests.length + " edges"; String msg = dests.length + " edges";
int msgWidth = fm.stringWidth(msg); int msgWidth = fm.stringWidth(msg);
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
g.drawString(msg, x - msgWidth / 2, y + 2 * Visualizer.MOTE_RADIUS + 3); g.drawString(msg, x - msgWidth / 2, y + 2 * Visualizer.MOTE_RADIUS + 3);
/* Draw LQI/RSSI edges */ /* Draw LQI/RSSI edges */
for (DestinationRadio r : dests) { for (DestinationRadio r : dests) {
double prob = ((DGRMDestinationRadio)r).ratio; double prob = ((DGRMDestinationRadio) r).ratio;
double rssi = ((DGRMDestinationRadio)r).signal; double rssi = ((DGRMDestinationRadio) r).signal;
double pos_rssi = rssi + 100; double pos_rssi = rssi + 100;
int lqi = ((DGRMDestinationRadio)r).lqi; int lqi = ((DGRMDestinationRadio) r).lqi;
float red = (float)(1 - prob*pos_rssi/90*lqi/100); float red = (float) (1 - prob * pos_rssi / 90 * lqi / 100);
if(red > 1) red = 1; if (red > 1) {
if(red < 0) red = 0; red = 1;
float green = (float)(prob*pos_rssi/90*lqi/100); }
if(green > 1) green = 1; if (red < 0) {
if(green < 0) green = 0; red = 0;
if (prob == 0.0d) { }
continue; float green = (float) (prob * pos_rssi / 90 * lqi / 100);
} if (green > 1) {
Position pos = r.radio.getPosition(); green = 1;
Point pixel = visualizer.transformPositionToPixel(pos); }
g.setColor(new Color(red, green, 0.0f)); if (green < 0) {
g.drawString("LQI: " + lqi, (x + pixel.x) / 2, (y + pixel.y) / 2); green = 0;
g.drawString("RSSI: " + rssi, (x + pixel.x) / 2, (y + pixel.y) / 2 + g.getFontMetrics().getHeight()); }
g.drawLine(x, y, pixel.x, pixel.y); if (prob == 0.0d) {
/* Draw success ratio only if single mote selected */ continue;
if (selectedMotes.size() == 1) { }
g.setColor(Color.BLACK); Position pos = r.radio.getPosition();
msg = String.format("%1.1f%%", 100.0 * prob); Point pixel = visualizer.transformPositionToPixel(pos);
msgWidth = fm.stringWidth(msg); g.setColor(new Color(red, green, 0.0f));
g.drawString(msg, pixel.x - msgWidth/2, pixel.y + 2*Visualizer.MOTE_RADIUS + 3); g.drawString("LQI: " + lqi, (x + pixel.x) / 2, (y + pixel.y) / 2);
} g.drawString("RSSI: " + rssi, (x + pixel.x) / 2, (y + pixel.y) / 2 + g.getFontMetrics().getHeight());
} g.drawLine(x, y, pixel.x, pixel.y);
} /* Draw success ratio only if single mote selected */
} if (selectedMotes.size() == 1) {
g.setColor(Color.BLACK);
msg = String.format("%1.1f%%", 100.0 * prob);
msgWidth = fm.stringWidth(msg);
g.drawString(msg, pixel.x - msgWidth / 2, pixel.y + 2 * Visualizer.MOTE_RADIUS + 3);
}
}
}
}
@Override @Override
public void paintAfterMotes(Graphics g) { public void paintAfterMotes(Graphics g) {
} }
@Override @Override
public Visualizer getVisualizer() { public Visualizer getVisualizer() {
return visualizer; return visualizer;
} }
} }

View file

@ -27,7 +27,6 @@
* SUCH DAMAGE. * SUCH DAMAGE.
* *
*/ */
package org.contikios.cooja.plugins.skins; package org.contikios.cooja.plugins.skins;
import java.awt.BorderLayout; import java.awt.BorderLayout;
@ -87,6 +86,7 @@ import org.contikios.cooja.radiomediums.UDGM;
@ClassDescription("Radio environment (UDGM)") @ClassDescription("Radio environment (UDGM)")
@SupportedArguments(radioMediums = {UDGM.class}) @SupportedArguments(radioMediums = {UDGM.class})
public class UDGMVisualizerSkin implements VisualizerSkin { public class UDGMVisualizerSkin implements VisualizerSkin {
private static final Logger logger = Logger.getLogger(UDGMVisualizerSkin.class); private static final Logger logger = Logger.getLogger(UDGMVisualizerSkin.class);
private static final Color COLOR_TX = new Color(0, 255, 0, 100); private static final Color COLOR_TX = new Color(0, 255, 0, 100);
@ -158,8 +158,8 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
txRangeSpinner.addChangeListener(new ChangeListener() { txRangeSpinner.addChangeListener(new ChangeListener() {
@Override @Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
radioMedium.setTxRange(((SpinnerNumberModel) radioMedium.setTxRange(((SpinnerNumberModel) txRangeSpinner.getModel())
txRangeSpinner.getModel()).getNumber().doubleValue()); .getNumber().doubleValue());
visualizer.repaint(); visualizer.repaint();
} }
}); });
@ -167,8 +167,8 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
interferenceRangeSpinner.addChangeListener(new ChangeListener() { interferenceRangeSpinner.addChangeListener(new ChangeListener() {
@Override @Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
radioMedium.setInterferenceRange(((SpinnerNumberModel) radioMedium.setInterferenceRange(((SpinnerNumberModel) interferenceRangeSpinner.getModel())
interferenceRangeSpinner.getModel()).getNumber().doubleValue()); .getNumber().doubleValue());
visualizer.repaint(); visualizer.repaint();
} }
}); });
@ -176,8 +176,8 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
successRatioTxSpinner.addChangeListener(new ChangeListener() { successRatioTxSpinner.addChangeListener(new ChangeListener() {
@Override @Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
radioMedium.SUCCESS_RATIO_TX = ((SpinnerNumberModel) radioMedium.SUCCESS_RATIO_TX = ((SpinnerNumberModel) successRatioTxSpinner.getModel())
successRatioTxSpinner.getModel()).getNumber().doubleValue(); .getNumber().doubleValue();
visualizer.repaint(); visualizer.repaint();
} }
}); });
@ -185,8 +185,8 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
successRatioRxSpinner.addChangeListener(new ChangeListener() { successRatioRxSpinner.addChangeListener(new ChangeListener() {
@Override @Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
radioMedium.SUCCESS_RATIO_RX = ((SpinnerNumberModel) radioMedium.SUCCESS_RATIO_RX = ((SpinnerNumberModel) successRatioRxSpinner.getModel())
successRatioRxSpinner.getModel()).getNumber().doubleValue(); .getNumber().doubleValue();
visualizer.repaint(); visualizer.repaint();
} }
}); });
@ -199,7 +199,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
JPanel main = new JPanel(); JPanel main = new JPanel();
main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS)); main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
main.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); main.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
rangeTX = Box.createHorizontalBox(); rangeTX = Box.createHorizontalBox();
rangeTX.add(new JLabel("TX range:")); rangeTX.add(new JLabel("TX range:"));
rangeTX.add(Box.createHorizontalStrut(5)); rangeTX.add(Box.createHorizontalStrut(5));
@ -221,12 +221,12 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
rangeINT.setVisible(false); rangeINT.setVisible(false);
ratioTX.setVisible(false); ratioTX.setVisible(false);
ratioRX.setVisible(false); ratioRX.setVisible(false);
main.add(rangeTX); main.add(rangeTX);
main.add(rangeINT); main.add(rangeINT);
main.add(ratioTX); main.add(ratioTX);
main.add(ratioRX); main.add(ratioRX);
rrFrame = new JInternalFrame("UDGM", false, true); rrFrame = new JInternalFrame("UDGM", false, true);
rrFrame.setVisible(false); rrFrame.setVisible(false);
rrFrame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); rrFrame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
@ -264,7 +264,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
@Override @Override
public Color[] getColorOf(Mote mote) { public Color[] getColorOf(Mote mote) {
if (visualizer.getSelectedMotes().contains(mote)) { if (visualizer.getSelectedMotes().contains(mote)) {
return new Color[] { Color.CYAN }; return new Color[]{Color.CYAN};
} }
return null; return null;
} }
@ -280,7 +280,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
Area intRangeMaxArea = new Area(); Area intRangeMaxArea = new Area();
Area trxRangeArea = new Area(); Area trxRangeArea = new Area();
Area trxRangeMaxArea = new Area(); Area trxRangeMaxArea = new Area();
for (Mote selectedMote : selectedMotes) { for (Mote selectedMote : selectedMotes) {
if (selectedMote.getInterfaces().getRadio() == null) { if (selectedMote.getInterfaces().getRadio() == null) {
continue; continue;
@ -336,7 +336,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
y - translatedTransmission.y, y - translatedTransmission.y,
2 * translatedTransmission.x, 2 * translatedTransmission.x,
2 * translatedTransmission.y))); 2 * translatedTransmission.y)));
intRangeMaxArea.add(new Area(new Ellipse2D.Double( intRangeMaxArea.add(new Area(new Ellipse2D.Double(
x - translatedInterferenceMax.x, x - translatedInterferenceMax.x,
y - translatedInterferenceMax.y, y - translatedInterferenceMax.y,
@ -349,16 +349,16 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
y - translatedTransmissionMax.y, y - translatedTransmissionMax.y,
2 * translatedTransmissionMax.x, 2 * translatedTransmissionMax.x,
2 * translatedTransmissionMax.y))); 2 * translatedTransmissionMax.y)));
} }
Graphics2D g2d = (Graphics2D) g; Graphics2D g2d = (Graphics2D) g;
g2d.setColor(COLOR_INT); g2d.setColor(COLOR_INT);
g2d.fill(intRangeArea); g2d.fill(intRangeArea);
g.setColor(Color.GRAY); g.setColor(Color.GRAY);
g2d.draw(intRangeMaxArea); g2d.draw(intRangeMaxArea);
g.setColor(COLOR_TX); g.setColor(COLOR_TX);
g2d.fill(trxRangeArea); g2d.fill(trxRangeArea);
g.setColor(Color.GRAY); g.setColor(Color.GRAY);
@ -371,20 +371,20 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
if (selectedMotes.size() == 1) { if (selectedMotes.size() == 1) {
Mote selectedMote = selectedMotes.toArray(new Mote[0])[0]; Mote selectedMote = selectedMotes.toArray(new Mote[0])[0];
Radio selectedRadio = selectedMote.getInterfaces().getRadio(); Radio selectedRadio = selectedMote.getInterfaces().getRadio();
for (Mote m: simulation.getMotes()) { for (Mote m : simulation.getMotes()) {
if (m == selectedMote) { if (m == selectedMote) {
continue; continue;
} }
double prob = double prob
((UDGM) simulation.getRadioMedium()).getSuccessProbability(selectedRadio, m.getInterfaces().getRadio()); = ((UDGM) simulation.getRadioMedium()).getSuccessProbability(selectedRadio, m.getInterfaces().getRadio());
if (prob == 0.0d) { if (prob == 0.0d) {
continue; continue;
} }
String msg = (((int)(1000*prob))/10.0) + "%"; String msg = (((int) (1000 * prob)) / 10.0) + "%";
Position pos = m.getInterfaces().getPosition(); Position pos = m.getInterfaces().getPosition();
Point pixel = visualizer.transformPositionToPixel(pos); Point pixel = visualizer.transformPositionToPixel(pos);
int msgWidth = fm.stringWidth(msg); int msgWidth = fm.stringWidth(msg);
g.drawString(msg, pixel.x - msgWidth/2, pixel.y + 2*Visualizer.MOTE_RADIUS + 3); g.drawString(msg, pixel.x - msgWidth / 2, pixel.y + 2 * Visualizer.MOTE_RADIUS + 3);
} }
} }
@ -395,6 +395,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
} }
public static class RangeMenuAction implements SimulationMenuAction { public static class RangeMenuAction implements SimulationMenuAction {
@Override @Override
public boolean isEnabled(Visualizer visualizer, Simulation simulation) { public boolean isEnabled(Visualizer visualizer, Simulation simulation) {
return true; return true;
@ -420,6 +421,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
}; };
public static class SuccessRatioMenuAction implements SimulationMenuAction { public static class SuccessRatioMenuAction implements SimulationMenuAction {
@Override @Override
public boolean isEnabled(Visualizer visualizer, Simulation simulation) { public boolean isEnabled(Visualizer visualizer, Simulation simulation) {
return true; return true;
@ -433,7 +435,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
@Override @Override
public void doAction(Visualizer visualizer, Simulation simulation) { public void doAction(Visualizer visualizer, Simulation simulation) {
VisualizerSkin[] skins = visualizer.getCurrentSkins(); VisualizerSkin[] skins = visualizer.getCurrentSkins();
for (VisualizerSkin skin: skins) { for (VisualizerSkin skin : skins) {
if (skin instanceof UDGMVisualizerSkin) { if (skin instanceof UDGMVisualizerSkin) {
UDGMVisualizerSkin vskin = ((UDGMVisualizerSkin) skin); UDGMVisualizerSkin vskin = ((UDGMVisualizerSkin) skin);
vskin.ratioTX.setVisible(true); vskin.ratioTX.setVisible(true);