Browse Source

GroupNode Canvas Information + GroupNodeAlgo Selection

Tom Troppmann 5 years ago
parent
commit
dea94df3e6

+ 73 - 13
src/exampleAlgorithms/PSOAlgotihm.java

@@ -5,6 +5,7 @@ import java.awt.Component;
 import java.awt.Dimension;
 import java.awt.FlowLayout;
 import java.awt.Font;
+import java.awt.image.BufferedImage;
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileOutputStream;
@@ -19,12 +20,14 @@ import java.util.Locale;
 import java.util.stream.Collectors;
 
 import javax.swing.BorderFactory;
+import javax.swing.ImageIcon;
 import javax.swing.JButton;
 import javax.swing.JCheckBox;
 import javax.swing.JFileChooser;
 import javax.swing.JFormattedTextField;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
+import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.JSplitPane;
@@ -34,6 +37,7 @@ import javax.swing.text.NumberFormatter;
 
 import api.Algorithm;
 import classes.AbstractCpsObject;
+import classes.CpsEdge;
 import classes.CpsUpperNode;
 import classes.HolonElement;
 import classes.HolonObject;
@@ -42,17 +46,26 @@ import classes.HolonSwitch;
 import ui.controller.Control;
 import ui.model.Model;
 import ui.model.DecoratedHolonObject.HolonObjectState;
+import ui.model.DecoratedGroupNode;
 import ui.model.DecoratedNetwork;
 import ui.model.DecoratedState;
 
+
+
+
+
 public class PSOAlgotihm implements Algorithm {
 	//Parameter for Algo with default Values:
 	private int swarmSize = 20; 
 	private int maxIterations = 100; 
-	private double limit = 0.3; 
+	private double limit = 0.01; 
 	private double dependency = 2.07; 
 	private int rounds = 20; 
+	
+	//Settings For GroupNode using and plotting
 	private boolean append = false;
+	private boolean useGroupNode = false;
+	private DecoratedGroupNode dGroupNode = null;
 	
 	//Parameter defined by Algo
 	private HashMap<Integer, AccessWrapper> access;
@@ -113,17 +126,7 @@ public class PSOAlgotihm implements Algorithm {
 		JLabel swarmSizeLabel = new JLabel("Swarm Size:");
 		swarmSizeLabel.setBounds(20, 60, 100, 20);
 		parameterPanel.add(swarmSizeLabel);
-		
-		JLabel showDiagnosticsLabel = new JLabel("Append Plott on existing File:");
-		showDiagnosticsLabel.setBounds(200, 60, 170, 20);
-		parameterPanel.add(showDiagnosticsLabel);
-		
-		JLabel cautionLabel = new JLabel(
-				"Caution: High values in the fields of 'Swarm Size' and 'Max. Iteration' may take some time to calculate.");
-		cautionLabel.setFont(new Font("Serif", Font.ITALIC, 12));
-		cautionLabel.setBounds(10, 210, 500, 15);
-		parameterPanel.add(cautionLabel);
-		
+			
 		JLabel maxIterLabel = new JLabel("Max. Iterations:");
 		maxIterLabel.setBounds(20, 85, 100, 20);
 		parameterPanel.add(maxIterLabel);
@@ -140,6 +143,41 @@ public class PSOAlgotihm implements Algorithm {
 		roundsLabel.setBounds(20, 160, 100, 20);
 		parameterPanel.add(roundsLabel);
 		
+		JLabel cautionLabel = new JLabel(
+				"Caution: High values in the fields of 'Swarm Size' and 'Max. Iteration' may take some time to calculate.");
+		cautionLabel.setFont(new Font("Serif", Font.ITALIC, 12));
+
+		JLabel showDiagnosticsLabel = new JLabel("Append Plott on existing File:");
+		showDiagnosticsLabel.setBounds(200, 60, 170, 20);
+		parameterPanel.add(showDiagnosticsLabel);		
+		
+		JPanel borderPanel = new JPanel(null);
+		borderPanel.setBounds(200, 85, 185, 50);
+		borderPanel.setBorder(BorderFactory.createTitledBorder(""));
+		parameterPanel.add(borderPanel);	
+		
+		JLabel showGroupNodeLabel = new JLabel("Use Group Node:");
+		showGroupNodeLabel.setBounds(10, 1, 170, 20);
+		borderPanel.add(showGroupNodeLabel);	
+		
+		JButton selectGroupNodeButton = new JButton("Select GroupNode");
+		selectGroupNodeButton.setEnabled(false);
+		selectGroupNodeButton.setBounds(10, 25, 165, 20);
+		selectGroupNodeButton.addActionListener(actionEvent -> selectGroupNode());
+		borderPanel.add(selectGroupNodeButton);	
+		
+		JCheckBox useGroupNodeCheckBox = new JCheckBox();
+		useGroupNodeCheckBox.setSelected(false);
+		useGroupNodeCheckBox.setBounds(155, 1, 25, 20);
+		useGroupNodeCheckBox.addPropertyChangeListener(propertyChange -> {
+			useGroupNode = useGroupNodeCheckBox.isSelected();
+			selectGroupNodeButton.setEnabled(useGroupNode);
+		});
+		borderPanel.add(useGroupNodeCheckBox);
+		
+		
+		cautionLabel.setBounds(10, 210, 500, 15);
+		parameterPanel.add(cautionLabel);
 		
 		JCheckBox diagnosticsCheckBox = new JCheckBox();
 		diagnosticsCheckBox.setSelected(false);
@@ -147,6 +185,8 @@ public class PSOAlgotihm implements Algorithm {
 		diagnosticsCheckBox.addPropertyChangeListener(propertyChange -> append = diagnosticsCheckBox.isSelected());
 		parameterPanel.add(diagnosticsCheckBox);
 		
+
+		
 		//Integer formatter
 		NumberFormat format = NumberFormat.getIntegerInstance();
 		format.setGroupingUsed(false);
@@ -281,6 +321,15 @@ public class PSOAlgotihm implements Algorithm {
 	private void println(String message) {
 		textArea.append(message  + "\n");
 	}
+	private void selectGroupNode() {
+		Object[] possibilities = control.getSimManager().getActualVisualRepresentationalState().getCreatedGroupNodes().values().stream().map(aCps -> new Handle<DecoratedGroupNode>(aCps)).toArray();
+		@SuppressWarnings("unchecked")
+		Handle<DecoratedGroupNode> selected = (Handle<DecoratedGroupNode>) JOptionPane.showInputDialog(content, "Select GroupNode:", "GroupNode?",  JOptionPane.OK_OPTION,new ImageIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)) , possibilities, "");
+		if(selected != null) {
+			println("Selected: " + selected);
+			dGroupNode = selected.object;
+		}
+	}
 	
 	
 	
@@ -584,7 +633,7 @@ public class PSOAlgotihm implements Algorithm {
 	private List<Boolean> extractPositionAndAccess(Model model) {
 		initialState = new ArrayList<Boolean>(); 
 		access= new HashMap<Integer, AccessWrapper>();
-		rollOutNodes(model.getObjectsOnCanvas(), initialState, model.getCurIteration());
+		rollOutNodes((useGroupNode && (dGroupNode != null))? dGroupNode.getModel().getNodes() :model.getObjectsOnCanvas(), initialState, model.getCurIteration());
 		return initialState;
 	}
 	/**
@@ -804,4 +853,15 @@ public class PSOAlgotihm implements Algorithm {
 			}
 		}
 	
+	
+	private   class  Handle<T>{
+		public T object;
+		Handle(T object){
+			this.object = object;
+		}
+		public String toString() {
+			return object.toString();
+		}
+	}
+	
 }

+ 30 - 0
src/ui/model/DecoratedGroupNode.java

@@ -1,10 +1,14 @@
 package ui.model;
 
 import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collector;
+import java.util.stream.Collectors;
 
 import classes.CpsNode;
 import classes.CpsUpperNode;
 import classes.IntermediateCalculationCable;
+import ui.model.DecoratedHolonObject.HolonObjectState;
 import classes.ExitCable;
 /**
  * For the @VisualRepresentationalState only.
@@ -70,4 +74,30 @@ public class DecoratedGroupNode {
 		return groupNodeList;
 	}
 	
+	//Gather Informations:
+	public int getAmountOfSupplier() {
+		return supplierList.size() + groupNodeList.stream().map(groupNode -> groupNode.getAmountOfSupplier()).reduce(0, (a,b)->a+b);
+	}
+	public int getAmountOfConsumer() {
+		return consumerList.size() + groupNodeList.stream().map(groupNode -> groupNode.getAmountOfConsumer()).reduce(0, (a,b)->a+b);
+	}
+	public int getAmountOfPassiv() {
+		return passivList.size() + groupNodeList.stream().map(groupNode -> groupNode.getAmountOfPassiv()).reduce(0, (a,b)->a+b);
+	}
+	
+	public int getAmountOfConsumerWithState(HolonObjectState state) {
+		return ((int) consumerList.stream().map(con -> con.getState()).filter(rightState -> (rightState == state)).count()) +  groupNodeList.stream().map(groupNode -> groupNode.getAmountOfConsumerWithState(state)).reduce(0, (a,b)->a+b);
+	}
+	
+
+	
+	public String toString() {
+		return 
+				"GroupNode with [Supplier:" + getAmountOfSupplier() 
+				+ ", NotSupplied:" + getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED) 
+				+ ", PartiallySupplied:" + getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED) 
+				+ ", Supplied:" + getAmountOfConsumerWithState(HolonObjectState.SUPPLIED) 
+				+ ", OverSupplied:" + getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED) 
+				+ ", Passiv:"+ getAmountOfPassiv() + "]";
+	}
 }

+ 1 - 1
src/ui/view/AbstractCanvas.java

@@ -97,7 +97,7 @@ public abstract class AbstractCanvas extends JPanel {
 	
 	
 
-	protected class ACpsHandle{
+	class ACpsHandle{
 		public AbstractCpsObject object;
 		ACpsHandle(AbstractCpsObject object){
 			this.object = object;

+ 50 - 26
src/ui/view/MyCanvas.java

@@ -462,35 +462,59 @@ public class MyCanvas extends AbstractCanvas implements MouseListener,
 		g.setFont(new Font("TimesRoman", Font.PLAIN, Math.max((int) (controller.getScale() / 3.5f), 10) )); 
 		g.drawString(currentEnergy + "/" + (unlimited?"\u221E":capacity) , middle.x, middle.y);
 	}
-	private void paintGroupNode(Graphics2D g, DecoratedGroupNode dGroupNode, CpsUpperNode onThisGroupNodeModel) {
-//		for(ExitCable exitCable : dGroupNode.getExitCableList()) {
-//			if(exitCable.getOutsideUpperNode() != onThisGroupNodeModel) {
-//				System.out.println("Check!");
-//				continue;
-//			}
-//			Position start = exitCable.getInsideUpperNode().getPosition();
-//			Position end =  exitCable.getOusideObject().getPosition();
-//			float currentEnergy = exitCable.getCable().getFlowEnergy();
-//			float capacity = exitCable.getModel().getCapacity();
-//			switch(exitCable.getCable().getState()) {
-//			case Burned:
-//				g.setColor(Color.RED);
-//				g.setStroke(new BasicStroke(2));
-//				break;
-//			case Working:
-//				g.setColor(new Color(13, 175, 28));
-//				g.setStroke(new BasicStroke((currentEnergy / capacity* 2f) + 1));
-//				break;
-//			}
-//			g.drawLine(start.x, start.y, end.x, end.y);
-//			Position middle = new Position((start.x + end.x) / 2, (start.y + end.y) / 2);
-//			g.setFont(new Font("TimesRoman", Font.PLAIN, Math.max((int) (controller.getScale() / 3.5f), 10) )); 
-//			g.drawString(currentEnergy + "/" + capacity , middle.x, middle.y);
-//		}
+	private void paintGroupNode(Graphics2D g, DecoratedGroupNode dGroupNode) {
 		Position pos = dGroupNode.getModel().getPosition();
 		g.setColor(Color.lightGray);
 		g.fillRect(pos.x - controller.getScaleDiv2(), pos.y - controller.getScaleDiv2(), controller.getScale(), controller.getScale());
 		drawCanvasObject(g, "/Images/upper_node.png" , pos);
+		paintGroupNodeBar(g, dGroupNode, pos);
+	}
+	private void paintGroupNodeBar(Graphics2D g, DecoratedGroupNode dGroupNode , Position pos) {
+		// +1, -2, -1 little Adjustment for pixel perfect alignment
+		int barWidth = (int) (controller.getScale());
+		int barHeight = (int) (controller.getScale() / 5);
+		g.setColor(Color.WHITE);
+		g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) barWidth, barHeight);
+		float[] percentages = getGroupNodeBarPercentages(dGroupNode);
+		Color[] colors = new Color[6];
+		colors[0] = getStateColor(HolonObjectState.PRODUCER);
+		colors[1] = getStateColor(HolonObjectState.NOT_SUPPLIED);
+		colors[2] = getStateColor(HolonObjectState.PARTIALLY_SUPPLIED);
+		colors[3] = getStateColor(HolonObjectState.SUPPLIED);
+		colors[4] = getStateColor(HolonObjectState.OVER_SUPPLIED);
+		colors[5] = getStateColor(HolonObjectState.NO_ENERGY);
+				
+		for(int i = 5; i>=0; i--) {
+			g.setColor(colors[i]);
+			g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) (barWidth * percentages[i] - 1), barHeight);		
+		}
+//		g.setColor(color);
+//		g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) (barWidth * (percentage < 1 ? percentage : 1.0f) - 1), barHeight);
+		g.setColor(Color.BLACK);
+		g.setStroke(new BasicStroke(1));
+		g.drawRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, barWidth - 1 , barHeight);
+	}
+	/**
+	 * HardCoded Stuff dont try at Home ;)
+	 * @param dGroupNode
+	 * @return
+	 */
+	public float[] getGroupNodeBarPercentages(DecoratedGroupNode dGroupNode) {
+		int[] amountOfObjects = new int[6];
+		amountOfObjects[0] = dGroupNode.getAmountOfSupplier();
+		amountOfObjects[1] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED);
+		amountOfObjects[2] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED);
+		amountOfObjects[3] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.SUPPLIED);
+		amountOfObjects[4] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED);
+		amountOfObjects[5] = dGroupNode.getAmountOfPassiv();
+		int countHolonObjects = amountOfObjects[0] + 	amountOfObjects[1] + amountOfObjects[2] + amountOfObjects[3] + amountOfObjects[4] + amountOfObjects[5];
+		float[] percentages = new float[6];
+		int count = 0;
+		for(int i = 0; i < 6; i++) {
+			count += amountOfObjects[i];
+			percentages[i] = (float)count / (float)countHolonObjects;
+		}
+		return percentages;
 	}
 	private void paintSupplyBar(Graphics2D g, float percentage, Color color, Position pos) {
 		// +1, -2, -1 little Adjustment for pixel perfect alignment
@@ -565,7 +589,7 @@ public class MyCanvas extends AbstractCanvas implements MouseListener,
 			paintCable(g2d, cable, selectedEdges.contains(cable.getModel()));
 		}
 		for(DecoratedGroupNode dGroupNode : visualState.getGroupNodeList()) {
-			paintGroupNode(g2d, dGroupNode, null);
+			paintGroupNode(g2d, dGroupNode);
 		}
 		for(Consumer con: visualState.getConsumerList()) {
 			paintConsumer(g2d, con);					

+ 48 - 0
src/ui/view/UpperNodeCanvas.java

@@ -431,6 +431,54 @@ public class UpperNodeCanvas extends AbstractCanvas implements MouseListener, Mo
 		g.setColor(Color.lightGray);
 		g.fillRect(pos.x - controller.getScaleDiv2(), pos.y - controller.getScaleDiv2(), controller.getScale(), controller.getScale());
 		drawCanvasObject(g, "/Images/upper_node.png" , pos);
+		paintGroupNodeBar(g, dGroupNode, pos);
+	}
+	private void paintGroupNodeBar(Graphics2D g, DecoratedGroupNode dGroupNode , Position pos) {
+		// +1, -2, -1 little Adjustment for pixel perfect alignment
+		int barWidth = (int) (controller.getScale());
+		int barHeight = (int) (controller.getScale() / 5);
+		g.setColor(Color.WHITE);
+		g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) barWidth, barHeight);
+		float[] percentages = getGroupNodeBarPercentages(dGroupNode);
+		Color[] colors = new Color[6];
+		colors[0] = getStateColor(HolonObjectState.PRODUCER);
+		colors[1] = getStateColor(HolonObjectState.NOT_SUPPLIED);
+		colors[2] = getStateColor(HolonObjectState.PARTIALLY_SUPPLIED);
+		colors[3] = getStateColor(HolonObjectState.SUPPLIED);
+		colors[4] = getStateColor(HolonObjectState.OVER_SUPPLIED);
+		colors[5] = getStateColor(HolonObjectState.NO_ENERGY);
+				
+		for(int i = 5; i>=0; i--) {
+			g.setColor(colors[i]);
+			g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) (barWidth * percentages[i] - 1), barHeight);		
+		}
+//		g.setColor(color);
+//		g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) (barWidth * (percentage < 1 ? percentage : 1.0f) - 1), barHeight);
+		g.setColor(Color.BLACK);
+		g.setStroke(new BasicStroke(1));
+		g.drawRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, barWidth - 1 , barHeight);
+	}
+	/**
+	 * HardCoded Stuff dont try at Home ;)
+	 * @param dGroupNode
+	 * @return
+	 */
+	public float[] getGroupNodeBarPercentages(DecoratedGroupNode dGroupNode) {
+		int[] amountOfObjects = new int[6];
+		amountOfObjects[0] = dGroupNode.getAmountOfSupplier();
+		amountOfObjects[1] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED);
+		amountOfObjects[2] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED);
+		amountOfObjects[3] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.SUPPLIED);
+		amountOfObjects[4] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED);
+		amountOfObjects[5] = dGroupNode.getAmountOfPassiv();
+		int countHolonObjects = amountOfObjects[0] + 	amountOfObjects[1] + amountOfObjects[2] + amountOfObjects[3] + amountOfObjects[4] + amountOfObjects[5];
+		float[] percentages = new float[6];
+		int count = 0;
+		for(int i = 0; i < 6; i++) {
+			count += amountOfObjects[i];
+			percentages[i] = (float)count / (float)countHolonObjects;
+		}
+		return percentages;
 	}
 	private void paintExitCable(Graphics2D g, ExitCable eCable) {
 		Position start = eCable.getStart().getPosition();