| 1 | package de.uka.ipd.sdq.sensorframework.adapter; |
| 2 | |
| 3 | import java.text.DecimalFormat; |
| 4 | import java.text.DecimalFormatSymbols; |
| 5 | import java.util.HashMap; |
| 6 | import java.util.Locale; |
| 7 | import java.util.Map.Entry; |
| 8 | |
| 9 | import de.uka.ipd.sdq.codegen.simudatavisualisation.datatypes.AbstractPie; |
| 10 | import de.uka.ipd.sdq.codegen.simudatavisualisation.datatypes.PieEntity; |
| 11 | import de.uka.ipd.sdq.sensorframework.entities.SensorAndMeasurements; |
| 12 | import de.uka.ipd.sdq.sensorframework.entities.StateSensor; |
| 13 | |
| 14 | /** |
| 15 | * Provides an adapter that allows to convert StateSensors to Pies. |
| 16 | * |
| 17 | * @author Henning Groenda |
| 18 | */ |
| 19 | public abstract class StateSensorToPieAdapter extends DataAdapter { |
| 20 | |
| 21 | /** Factor to display values in percent. */ |
| 22 | private static final int FACTOR_PERCENT = 100; |
| 23 | |
| 24 | /** Information about the StateSensor and the measurements. */ |
| 25 | protected SensorAndMeasurements samInformation; |
| 26 | |
| 27 | /** |
| 28 | * Initializes the adapter with the given StateSensor measurements. |
| 29 | * |
| 30 | * @param samInformation |
| 31 | * Information about the StateSensor and the measurements. |
| 32 | */ |
| 33 | protected StateSensorToPieAdapter(final SensorAndMeasurements samInformation) { |
| 34 | super(); |
| 35 | this.samInformation = samInformation; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * {@inheritDoc} |
| 40 | */ |
| 41 | public Object getAdaptedObject() { |
| 42 | HashMap<String, Double> fractions = new HashMap<String, Double>(); |
| 43 | double sum = calculateFractions(fractions); |
| 44 | return createPie(fractions, sum); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Takes sum of the time in which all states were active and creates a Pie |
| 49 | * from it |
| 50 | * |
| 51 | * @param timeSums |
| 52 | * A map which associates to each state the sum of the time this |
| 53 | * state has been active |
| 54 | * @param sum |
| 55 | * Total sum of times |
| 56 | * @return A pie representation of the data |
| 57 | */ |
| 58 | private AbstractPie createPie(final HashMap<String, Double> timeSums, |
| 59 | final double sum) { |
| 60 | AbstractPie p = instantiatePie(((StateSensor) samInformation.getSensor()) |
| 61 | .getSensorName()); |
| 62 | DecimalFormat df = new DecimalFormat("#0.0", new DecimalFormatSymbols( |
| 63 | Locale.US)); |
| 64 | for (Entry<String, Double> e : timeSums.entrySet()) { |
| 65 | if (e.getValue() > 0.0) { |
| 66 | p |
| 67 | .addEntity(new PieEntity(e.getValue(), e.getKey() |
| 68 | + " (" |
| 69 | + df |
| 70 | .format(e.getValue() * FACTOR_PERCENT |
| 71 | / sum) + "%)")); |
| 72 | } |
| 73 | } |
| 74 | return p; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Calculates the fractions of the pie chart to create. |
| 79 | * |
| 80 | * @param fractions |
| 81 | * the fractions to calculate |
| 82 | * @return the sum of all fractions |
| 83 | */ |
| 84 | protected abstract double calculateFractions( |
| 85 | final HashMap<String, Double> fractions); |
| 86 | |
| 87 | /** |
| 88 | * Instantiates a Pie object. |
| 89 | * |
| 90 | * @param sensorName |
| 91 | * the sensor name |
| 92 | * @return the Pie object |
| 93 | */ |
| 94 | protected abstract AbstractPie instantiatePie(String sensorName); |
| 95 | } |