Browse Source

Adds an InformationPanel

Tom Troppmann 4 năm trước cách đây
mục cha
commit
d48a8a3c29
1 tập tin đã thay đổi với 257 bổ sung0 xóa
  1. 257 0
      src/exampleAlgorithms/InformationPanel.java

+ 257 - 0
src/exampleAlgorithms/InformationPanel.java

@@ -0,0 +1,257 @@
+package exampleAlgorithms;
+
+import java.awt.BorderLayout;
+import java.awt.Dimension;
+import java.awt.GridBagConstraints;
+import java.awt.GridBagLayout;
+import java.awt.GridLayout;
+import java.awt.Insets;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
+import javax.swing.JButton;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JSeparator;
+import javax.swing.JTextField;
+import javax.swing.SpringLayout;
+
+import api.AddOn;
+import classes.Flexibility;
+import classes.HolonElement.Priority;
+import ui.controller.Control;
+import ui.controller.FlexManager.FlexState;
+import ui.controller.FlexManager.FlexWrapper;
+import ui.model.DecoratedHolonObject.HolonObjectState;
+import ui.model.DecoratedState;
+
+public class InformationPanel implements AddOn {
+	Control control;
+	
+	private JPanel content = new JPanel();
+	
+	
+	
+	//Fields
+	private int amountHolonObjects;
+	private int amountSwitch;
+	private int amountConsumer;
+		private int	amountUnderSupplied;
+		private int amountPatiallySupplied;
+		private int amountFullySupplied;
+		private int amountOversupllied;
+		private int amountSupplier;
+	
+	private int amountHolonElements;
+		private int amountActiveHolonElements;
+		private int amountInactiveHolonElements;
+	private int amountFlexibilities;
+		private int amountEssential;
+		private int amountHigh;
+		private int amountMedium;
+		private int amountLow;
+
+		private int amountConsumingFlexibilities;
+		private int amountProducingFlexibilities;
+	private int amountPassiv;
+	
+	List<Entry> entryList = new ArrayList<Entry>();
+
+	private int currentEntryVerticalPosition = 0;
+
+
+
+
+	
+	
+	public InformationPanel(){
+		content.setLayout(new BorderLayout());
+		content.add(createMiddlePanel(), BorderLayout.CENTER);
+		JButton button = new JButton("calculate");
+		button.addActionListener(action -> updateEntrys());
+		content.add(button, BorderLayout.SOUTH);
+	}
+	
+	public static void main(String[] args)
+	{
+	      JFrame newFrame = new JFrame("exampleWindow");
+	      InformationPanel instance = new InformationPanel();
+	      newFrame.setContentPane(instance.getPanel());
+	      newFrame.pack();
+	      newFrame.setVisible(true);
+	      newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+	}
+	
+	
+	private JPanel createMiddlePanel() {
+		JPanel middle = new JPanel();
+		middle.setLayout(new GridBagLayout());
+		
+		int size = 60;
+		middle.setPreferredSize(new Dimension(16*size, 9*size));
+		
+
+		entryList.add(new Entry(() -> Integer.toString(amountHolonObjects), "amountHolonObjects", middle));
+		entryList.add(new Entry(() -> Integer.toString(amountConsumer) + addPercentage(amountConsumer, amountHolonObjects), "\tamountConsumer", middle));
+		entryList.add(new Entry(() -> Integer.toString(amountUnderSupplied) + addPercentage(amountUnderSupplied, amountConsumer), "\t\tamountUnderSupplied", middle));
+		entryList.add(new Entry(() -> Integer.toString(amountPatiallySupplied) + addPercentage(amountPatiallySupplied, amountConsumer), "\t\tamountPatiallySupplied", middle));
+		entryList.add(new Entry(() -> Integer.toString(amountFullySupplied) + addPercentage(amountFullySupplied, amountConsumer), "\t\tamountFullySupplied", middle));
+		entryList.add(new Entry(() -> Integer.toString(amountOversupllied) + addPercentage(amountOversupllied, amountConsumer), "\t\tamountOversupllied", middle));
+		entryList.add(new Entry(() -> Integer.toString(amountSupplier) + addPercentage(amountSupplier, amountHolonObjects), "\tamountSupplier", middle));
+		entryList.add(new Entry(() -> Integer.toString(amountPassiv) + addPercentage(amountPassiv, amountHolonObjects), "\tamountPassiv", middle));
+		addSeperator(middle);
+		entryList.add(new Entry(() -> Integer.toString(amountSwitch), "amountSwitch (not a HolonObject)", middle));
+		addSeperator(middle);
+		entryList.add(new Entry(() -> Integer.toString(amountHolonElements), "amountHolonElements", middle));
+		entryList.add(new Entry(() -> Integer.toString(amountActiveHolonElements) + addPercentage(amountActiveHolonElements, amountHolonElements), "\tamountActiveHolonElements", middle));
+		entryList.add(new Entry(() -> Integer.toString(amountInactiveHolonElements) + addPercentage(amountInactiveHolonElements, amountHolonElements), "\tamountInactiveHolonElements", middle));
+		
+		addSeperator(middle);
+		entryList.add(new Entry(() -> Integer.toString(amountFlexibilities), "amountFlexibilities", middle));
+		entryList.add(new Entry(() -> Integer.toString(amountConsumingFlexibilities) + addPercentage(amountConsumingFlexibilities, amountFlexibilities), "\tamountConsumingFlexibilities", middle));
+		entryList.add(new Entry(() -> Integer.toString(amountProducingFlexibilities) + addPercentage(amountProducingFlexibilities, amountFlexibilities), "\tamountProducingFlexibilities", middle));
+		addSeperator(middle);
+		entryList.add(new Entry(() -> Integer.toString(amountFlexibilities), "amountFlexibilities", middle));
+		entryList.add(new Entry(() -> Integer.toString(amountLow) + addPercentage(amountLow, amountFlexibilities), "\tamountLow", middle));
+		entryList.add(new Entry(() -> Integer.toString(amountMedium) + addPercentage(amountMedium, amountFlexibilities), "\tamountMedium", middle));
+		entryList.add(new Entry(() -> Integer.toString(amountHigh) + addPercentage(amountHigh, amountFlexibilities), "\tamountHigh", middle));
+		entryList.add(new Entry(() -> Integer.toString(amountEssential) + addPercentage(amountEssential, amountFlexibilities), "\tamountEssential", middle));
+		
+		
+		
+		return middle;
+	}
+
+	String addPercentage(int amountActual, int amountMaximum) {
+		return (amountMaximum > 0) ? "                " + String.format (Locale.US, "%.2f", ((float) amountActual) / ((float) amountMaximum) * 100) + "%" : "";
+	}
+	
+	
+	
+	private void addSeperator(JPanel middle) {
+		GridBagConstraints c = new GridBagConstraints();
+		c.fill = GridBagConstraints.HORIZONTAL;
+		c.gridwidth = 2;
+		c.weighty = 0.5;
+		c.weightx = 0.5;
+		c.gridx = 0;
+		c.gridy = this.currentEntryVerticalPosition++;
+		middle.add(new JSeparator(JSeparator.HORIZONTAL), c);
+	}
+
+	private GridBagConstraints createGbConstrain(int x, int y) {
+		GridBagConstraints c = new GridBagConstraints();
+		c.fill = GridBagConstraints.HORIZONTAL;
+		c.insets = new Insets(3,6, 3, 6);  
+		c.weighty = 0.5;
+		c.weightx = 0.5;
+		c.gridx = x;
+		c.gridy = y;
+		return c;
+	}
+	
+	
+	void updateEntrys() {
+		calculateValues();
+		entryList.forEach(entry -> entry.update());
+	}
+	
+	
+	void calculateValues(){
+		DecoratedState dState = control.getSimManager().getActualDecorState();
+		amountConsumer = dState.getNetworkList().stream().map(net -> net.getAmountOfConsumer()).reduce(0, Integer::sum);
+		this.amountSwitch = dState.getDecoratedSwitches().size();
+		this.amountUnderSupplied = dState.getNetworkList().stream().map(net -> net.getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED)).reduce(0, Integer::sum);
+		this.amountPatiallySupplied = dState.getNetworkList().stream().map(net -> net.getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED)).reduce(0, Integer::sum);
+		this.amountFullySupplied = dState.getNetworkList().stream().map(net -> net.getAmountOfConsumerWithState(HolonObjectState.SUPPLIED)).reduce(0, Integer::sum);
+		this.amountOversupllied = dState.getNetworkList().stream().map(net -> net.getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED)).reduce(0, Integer::sum);
+		amountSupplier = dState.getNetworkList().stream().map(net -> net.getAmountOfSupplier()).reduce(0, Integer::sum);
+		amountPassiv = dState.getNetworkList().stream().map(net -> net.getAmountOfPassiv()).reduce(0, Integer::sum);
+		
+		this.amountHolonObjects = amountConsumer + amountSupplier;
+		int elements = 0;
+		elements += dState.getNetworkList().stream().map(net -> net.getConsumerList().stream().map(con->con.getModel().getNumberOfElements()).reduce(0, Integer::sum) ).reduce(0, Integer::sum);
+		elements += dState.getNetworkList().stream().map(net -> net.getSupplierList().stream().map(con->con.getModel().getNumberOfElements()).reduce(0, Integer::sum) ).reduce(0, Integer::sum);
+		elements += dState.getNetworkList().stream().map(net -> net.getConsumerSelfSuppliedList().stream().map(con->con.getModel().getNumberOfElements()).reduce(0, Integer::sum) ).reduce(0, Integer::sum);
+		elements += dState.getNetworkList().stream().map(net -> net.getPassivNoEnergyList().stream().map(con->con.getModel().getNumberOfElements()).reduce(0, Integer::sum) ).reduce(0, Integer::sum);
+		this.amountHolonElements = elements;
+		int activeElements = 0;
+		activeElements += dState.getNetworkList().stream().map(net -> net.getConsumerList().stream().map(con->con.getModel().getNumberOfActiveElements()).reduce(0, Integer::sum) ).reduce(0, Integer::sum);
+		activeElements += dState.getNetworkList().stream().map(net -> net.getSupplierList().stream().map(con->con.getModel().getNumberOfActiveElements()).reduce(0, Integer::sum) ).reduce(0, Integer::sum);
+		activeElements += dState.getNetworkList().stream().map(net -> net.getConsumerSelfSuppliedList().stream().map(con->con.getModel().getNumberOfActiveElements()).reduce(0, Integer::sum) ).reduce(0, Integer::sum);
+		activeElements += dState.getNetworkList().stream().map(net -> net.getPassivNoEnergyList().stream().map(con->con.getModel().getNumberOfActiveElements()).reduce(0, Integer::sum) ).reduce(0, Integer::sum);
+		this.amountActiveHolonElements = activeElements;
+		this.amountInactiveHolonElements = amountHolonElements - amountActiveHolonElements;
+		
+		
+		
+		
+		
+		List<Flexibility> flexList = control.getSimManager().getActualFlexManager().getAllFlexWrapper().stream().filter(flexwrapper -> flexwrapper.getFlex().offered).map(flex -> flex.getFlex()).collect(Collectors.toList());
+		amountEssential = (int)flexList.stream().filter(flex -> flex.getElement().getPriority() == Priority.Essential).count();
+		amountHigh = (int)flexList.stream().filter(flex -> flex.getElement().getPriority() == Priority.High).count();
+		amountMedium = (int)flexList.stream().filter(flex -> flex.getElement().getPriority() == Priority.Medium).count();
+		amountLow = (int)flexList.stream().filter(flex -> flex.getElement().getPriority() == Priority.Low).count();
+		this.amountFlexibilities = amountEssential + amountHigh + amountMedium + amountLow;
+		int cost = 0;
+		int consumingFlex = 0;
+		float consumingFlexEnergy = 0.0f;
+		int producingFlex = 0;
+		float producingFlexEnergy = 0.0f;
+		int maxCooldown = 0;
+		for(Flexibility flex :flexList) {
+			cost += flex.cost;
+			float energy = flex.bringtmir();
+			if(energy < 0) {
+				consumingFlex++;
+				consumingFlexEnergy += -energy;
+			}else {
+				producingFlex++;
+				producingFlexEnergy += energy;
+			}
+		}
+		this.amountConsumingFlexibilities = consumingFlex;
+		this.amountProducingFlexibilities = producingFlex;
+		
+	}
+	
+	
+	@Override
+	public JPanel getPanel() {
+		return content;
+	}
+
+	@Override
+	public void setController(Control control) {
+		this.control = control;
+		if(control != null)  updateEntrys();
+	}
+	
+	private class Entry{
+		public Entry( Supplier<String> getter, String label, JPanel panel) {
+			this.getter = getter;
+			//RegisterToPanel
+			label = label.replaceAll("\t", "                   ");
+			JLabel jlabel = new JLabel(label);
+			panel.add(jlabel, createGbConstrain(0, currentEntryVerticalPosition));
+			tf = new JTextField(getter.get());
+			tf.setEditable(false);
+			panel.add(tf, createGbConstrain(1, currentEntryVerticalPosition));
+			currentEntryVerticalPosition++;
+		}
+		
+		private JTextField tf; //the Textfield to update
+		private Supplier<String> getter; //Getter for the field
+		public void update() {
+			tf.setText(getter.get());
+		}
+		
+		
+		
+		
+	}
+}