Browse Source

#76: added stub method for turning on flexible devices, added functionality for HolonElements that turns off active-status if flexibility-status is turned on

I. Dix 7 years ago
parent
commit
c8393d0839
3 changed files with 101 additions and 116 deletions
  1. 49 28
      src/ui/controller/SimulationManager.java
  2. 18 36
      src/ui/view/GUI.java
  3. 34 52
      src/ui/view/UnitGraph.java

+ 49 - 28
src/ui/controller/SimulationManager.java

@@ -29,7 +29,7 @@ public class SimulationManager {
      *
      * @param m Model
      */
-    public SimulationManager(Model m) {
+    SimulationManager(Model m) {
         canvas = null;
         model = m;
         subNets = new ArrayList<>();
@@ -41,7 +41,7 @@ public class SimulationManager {
      *
      * @param x current Iteration
      */
-    public void calculateStateForTimeStep(int x) {
+    void calculateStateForTimeStep(int x) {
         reset();
         timeStep = x;
         searchForSubNets();
@@ -55,7 +55,21 @@ public class SimulationManager {
             float wastedEnergy = production + consumption;
 
             float minConsumption = calculateMinimumEnergy(singleSubNet, timeStep);
+
+            // --------------- use flexible devices ---------------
+            // TODO: add global setting, whether the grid should react with flexible devices or whether it should not
+            if (wastedEnergy > 0) {
+                turnOnFlexibleDevices(singleSubNet, wastedEnergy);
+                // recompute after having examined all flexible devices
+                production = calculateEnergy("prod", singleSubNet, timeStep);
+                consumption = calculateEnergy("cons", singleSubNet, timeStep);
+                wastedEnergy = production + consumption;
+            }
+
+            // --------------- set flow simulation ---------------
             setFlowSimulation(singleSubNet);
+
+            // --------------- visualise graph ---------------
             for (HolonObject hl : singleSubNet.getObjects()) {
                 if (hl.getState() != HolonObject.NO_ENERGY
                         && hl.getState() != HolonObject.PRODUCER) {
@@ -93,12 +107,24 @@ public class SimulationManager {
         flexPane.recalculate();
     }
 
+
+    /**
+     * search for all flexible devices in the network and turn them on, until wasted energy = 0
+     * or all devices have been examined
+     *
+     * @param sN           the subnet
+     * @param wastedEnergy the currently wasted energy
+     */
+    void turnOnFlexibleDevices(SubNet sN, float wastedEnergy) {
+
+    }
+
     /**
      * Set Flow Simulation.
      *
      * @param sN Subnet
      */
-    public void setFlowSimulation(SubNet sN) {
+    private void setFlowSimulation(SubNet sN) {
         ArrayList<AbstractCpsObject> producers = new ArrayList<>();
         AbstractCpsObject tmp = null;
         tagTable = new HashMap<>();
@@ -149,7 +175,7 @@ public class SimulationManager {
      * @param nodes the nodes
      * @param iter  the Iteration
      */
-    public void setFlowSimRec(ArrayList<AbstractCpsObject> nodes, int iter) {
+    private void setFlowSimRec(ArrayList<AbstractCpsObject> nodes, int iter) {
         ArrayList<AbstractCpsObject> newNodes = new ArrayList<>();
         ArrayList<CpsEdge> changedEdges = new ArrayList<>();
         AbstractCpsObject tmp;
@@ -208,7 +234,7 @@ public class SimulationManager {
      *
      * @param nodes Array of AbstractCpsObjects
      */
-    public void setPseudoTags(ArrayList<AbstractCpsObject> nodes, ArrayList<CpsEdge> edges) {
+    private void setPseudoTags(ArrayList<AbstractCpsObject> nodes, ArrayList<CpsEdge> edges) {
         for (AbstractCpsObject node : nodes) {
             node.recalculateTags();
             node.setPseudoTags(new ArrayList<>());
@@ -277,8 +303,8 @@ public class SimulationManager {
      * @param visitedObj   the visited Objects
      * @param visitedEdges the visited Edges
      */
-    public void resetConnections(AbstractCpsObject cps, ArrayList<Integer> visitedObj,
-                                 ArrayList<CpsEdge> visitedEdges) {
+    private void resetConnections(AbstractCpsObject cps, ArrayList<Integer> visitedObj,
+                                  ArrayList<CpsEdge> visitedEdges) {
         visitedObj.add(cps.getId());
         cps.resetTags();
         for (CpsEdge e : cps.getConnections()) {
@@ -307,7 +333,7 @@ public class SimulationManager {
      * @param x    Integer
      * @return The Energy
      */
-    public float calculateEnergy(String type, SubNet sN, int x) {
+    private float calculateEnergy(String type, SubNet sN, int x) {
         float energy = 0;
         for (HolonObject hl : sN.getObjects()) {
             if (type.equals("prod")) {
@@ -336,7 +362,7 @@ public class SimulationManager {
      * @param x  Integer
      * @return the Calculated minimum Energy
      */
-    public float calculateMinimumEnergy(SubNet sN, int x) {
+    private float calculateMinimumEnergy(SubNet sN, int x) {
         float min = 0;
         float minElement = 0;
         for (HolonObject hl : sN.getObjects()) {
@@ -356,7 +382,7 @@ public class SimulationManager {
     /**
      * generates all subNets from all objectsToHandle.
      */
-    public void searchForSubNets() {
+    private void searchForSubNets() {
         subNets = new ArrayList<>();
         brokenEdges.clear();
         boolean end = false;
@@ -387,7 +413,7 @@ public class SimulationManager {
      * @param sN      Subnets
      * @return Subnet
      */
-    public SubNet buildSubNet(AbstractCpsObject cps, ArrayList<Integer> visited, SubNet sN) {
+    private SubNet buildSubNet(AbstractCpsObject cps, ArrayList<Integer> visited, SubNet sN) {
         visited.add(cps.getId());
         if (cps instanceof HolonObject) {
             sN.getObjects().add((HolonObject) cps);
@@ -439,11 +465,9 @@ public class SimulationManager {
      * @param current AbstractCpsObject
      * @return boolean
      */
-    public boolean legitState(AbstractCpsObject current) {
-        if (current instanceof HolonSwitch) {
-            return ((HolonSwitch) current).getState(timeStep);
-        }
-        return true;
+    private boolean legitState(AbstractCpsObject current) {
+        return !(current instanceof HolonSwitch)
+                || ((HolonSwitch) current).getState(timeStep);
     }
 
     /**
@@ -451,7 +475,7 @@ public class SimulationManager {
      *
      * @param id the Object ID
      */
-    public void removeFromToHandle(int id) {
+    private void removeFromToHandle(int id) {
         for (int i = 0; i < objectsToHandle.size(); i++) {
             if (objectsToHandle.get(i).getId() == id) {
                 objectsToHandle.remove(i);
@@ -475,7 +499,7 @@ public class SimulationManager {
      *
      * @param toCopy the ArrayList of CpsObjects co Copy
      */
-    public void copyObjects(ArrayList<AbstractCpsObject> toCopy) {
+    private void copyObjects(ArrayList<AbstractCpsObject> toCopy) {
         for (AbstractCpsObject cps : toCopy) {
             if (cps instanceof CpsUpperNode) {
                 copyObjects(((CpsUpperNode) cps).getNodes());
@@ -529,7 +553,7 @@ public class SimulationManager {
     /**
      * Resets the State of all Edges
      */
-    public void resetEdges() {
+    private void resetEdges() {
         for (CpsEdge e : brokenEdges) {
             e.setWorkingState(true);
         }
@@ -538,7 +562,7 @@ public class SimulationManager {
     /**
      * Resets the State for the whole Simulation Model
      */
-    public void resetSimulation() {
+    void resetSimulation() {
         reset();
         resetEdges();
     }
@@ -555,18 +579,15 @@ public class SimulationManager {
     /**
      * Get broken Edges
      */
-    public ArrayList<CpsEdge> getBrokenEdges() {
-        return brokenEdges;
-    }
+//    public ArrayList<CpsEdge> getBrokenEdges() {
+//        return brokenEdges;
+//    }
 
     /**
      * checks whether a given object is connected to an object inside the upperNode.
      * if yes, the state for the edge is changed in "connected" or "not connected"
-     *
-     * @param cps
-     * @param cUNode
      */
-    public void checkForConnectedStates(AbstractCpsObject cps, CpsUpperNode cUNode, CpsEdge theEdge) {
+    private void checkForConnectedStates(AbstractCpsObject cps, CpsUpperNode cUNode, CpsEdge theEdge) {
         AbstractCpsObject tmp;
         for (CpsEdge edge : cps.getConnections()) {
             if (edge.getA().getId() == cps.getId()) {
@@ -589,7 +610,7 @@ public class SimulationManager {
         return flexPane;
     }
 
-    public void setFlexiblePane(FlexiblePane fp) {
+    void setFlexiblePane(FlexiblePane fp) {
         flexPane = fp;
     }
 

+ 18 - 36
src/ui/view/GUI.java

@@ -33,10 +33,9 @@ import java.util.stream.Collectors;
 /**
  * Graphical User Interface.
  *
- * @param <E> Generic
  * @author Gruppe14
  */
-public class GUI<E> implements CategoryListener {
+public class GUI implements CategoryListener {
 
     private final AlgorithmMenu algorithmMenu;
     private final JMenuBar menuBar = new JMenuBar();
@@ -617,7 +616,6 @@ public class GUI<E> implements CategoryListener {
                 }
                 controller.resetCategorys();
             } catch (Exception e2) {
-                // TODO: handle exception
             }
 
             tree.repaint();
@@ -1036,6 +1034,11 @@ public class GUI<E> implements CategoryListener {
                                     .toString();
                             Boolean bTemp = Boolean.parseBoolean(newBStuff);
                             eleBTemp.setActiveFlex(bTemp);
+
+                            // set inactive, if flexible was clicked
+                            if (bTemp) {
+                                model.getSingleTable().setValueAt(Boolean.FALSE, selectedValueBY, 5);
+                            }
                         } else {
                             // Update of HolonElement
                             HolonElement eleTemp = updCon.getActualHolonElement((HolonObject) updCon.getActualCps(),
@@ -1545,7 +1548,6 @@ public class GUI<E> implements CategoryListener {
                             JOptionPane.showMessageDialog(new JFrame(), selectObjBeforeErase);
                     }
                 } catch (Exception e2) {
-                    // TODO: handle exception
                 }
 
             } else {
@@ -1604,7 +1606,6 @@ public class GUI<E> implements CategoryListener {
                             readStatistics(json);
                         }
                     } catch (IOException | ArchiveException e) {
-                        // TODO Auto-generated catch block
                         e.printStackTrace();
                         JLabel message = new JLabel("The savefile is corrupt and cannot be opened.");
                         JOptionPane.showMessageDialog(null, message, "", JOptionPane.ERROR_MESSAGE);
@@ -1648,7 +1649,6 @@ public class GUI<E> implements CategoryListener {
                     try {
                         controller.saveFile(new File(file).getAbsolutePath());
                     } catch (IOException | ArchiveException e) {
-                        // TODO Auto-generated catch block
                         e.printStackTrace();
                     }
                 }
@@ -1691,20 +1691,8 @@ public class GUI<E> implements CategoryListener {
                 try {
                     controller.loadAutoSave(controller.getUndoSave());
                     canvas.repaint();
-                    ArrayList<HolonElement> tempList = new ArrayList<>();
-                    for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
-                        if (cps instanceof HolonObject) {
-                            for (HolonElement h : ((HolonObject) cps).getElements()) {
-                                tempList.add(h);
-                                unitGraph.repaintWithNewElement(tempList);
-                                unitGraph.fillArrayofValue();
-                                tempList.remove(0);
-                            }
-                        } else if (cps instanceof HolonSwitch) {
-                            unitGraph.repaintWithNewSwitch((HolonSwitch) cps);
-                            unitGraph.fillArrayofBooleans();
-                        }
-                    }
+
+                    repaintGraphAfterUndoRedo();
                     hideScrollGraph();
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
@@ -1723,20 +1711,8 @@ public class GUI<E> implements CategoryListener {
                 try {
                     controller.loadAutoSave(controller.getRedoSave());
                     canvas.repaint();
-                    ArrayList<HolonElement> tempList = new ArrayList<>();
-                    for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
-                        if (cps instanceof HolonObject) {
-                            for (HolonElement h : ((HolonObject) cps).getElements()) {
-                                tempList.add(h);
-                                unitGraph.repaintWithNewElement(tempList);
-                                unitGraph.fillArrayofValue();
-                                tempList.remove(0);
-                            }
-                        } else if (cps instanceof HolonSwitch) {
-                            unitGraph.repaintWithNewSwitch((HolonSwitch) cps);
-                            unitGraph.fillArrayofBooleans();
-                        }
-                    }
+
+                    repaintGraphAfterUndoRedo();
                     hideScrollGraph();
                 } catch (IOException e) {
                     e.printStackTrace();
@@ -1747,6 +1723,7 @@ public class GUI<E> implements CategoryListener {
         timePanel = new TimePanel(model, controller);
         timePanel.setBorder(null);
         ((JSlider) (timePanel.getComponent(1))).addChangeListener(changeEvent -> {
+            System.out.println("Hi");
             int i = model.getCurIteration();
             controller.calculateStateForTimeStep(i);
             unitGraph.repaint();
@@ -1901,7 +1878,6 @@ public class GUI<E> implements CategoryListener {
                                 break;
                         }
                     } catch (Exception e) {
-                        // TODO: handle exception
                     }
 
                 }
@@ -1954,7 +1930,7 @@ public class GUI<E> implements CategoryListener {
      *
      * @return the Frame
      */
-    public JFrame getFrmCyberPhysical() {
+    JFrame getFrmCyberPhysical() {
         return frmCyberPhysical;
     }
 
@@ -2251,6 +2227,12 @@ public class GUI<E> implements CategoryListener {
         }
     }
 
+    private void repaintGraphAfterUndoRedo() {
+        for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
+            unitGraph.repaintGraph(cps);
+        }
+    }
+
     /**
      * Custom Mouse adapter makes contentPane gain focus once mouse leaves this component
      * so copy/paste/cut and "select all" works

+ 34 - 52
src/ui/view/UnitGraph.java

@@ -1,34 +1,18 @@
 package ui.view;
 
-import java.awt.BasicStroke;
-import java.awt.Color;
-import java.awt.Graphics;
-import java.awt.Graphics2D;
-import java.awt.RenderingHints;
-import java.awt.event.ComponentEvent;
-import java.awt.event.ComponentListener;
-import java.awt.event.MouseEvent;
-import java.awt.event.MouseListener;
-import java.awt.event.MouseMotionListener;
+import classes.*;
+import ui.controller.Control;
+import ui.model.Model;
+
+import javax.swing.*;
+import java.awt.*;
+import java.awt.event.*;
 import java.awt.geom.CubicCurve2D;
 import java.awt.geom.GeneralPath;
 import java.awt.geom.Line2D;
 import java.util.ArrayDeque;
 import java.util.ArrayList;
 import java.util.LinkedList;
-import java.awt.Point;
-
-import javax.swing.JPanel;
-
-import classes.AbstractCpsObject;
-import classes.CpsUpperNode;
-import classes.HolonElement;
-import classes.HolonObject;
-import ui.controller.Control;
-import ui.model.Model;
-import classes.HolonSwitch;
-
-import java.awt.Cursor;
 
 /**
  * This Class represents a Graph where the User can model the behavior of
@@ -39,11 +23,10 @@ import java.awt.Cursor;
 public class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, ComponentListener {
 
 	private static final long serialVersionUID = 1L;
-	private float maximum = 0;
-
+    GeneralPath graphCurve = new GeneralPath();
+    private float maximum = 0;
 	// Information shown when a Point is Dragged
 	private String dragInformation = "";
-
 	// Points
 	private Point recSize = new Point(8, 8); // Point Size
 	private Graphics2D g2;
@@ -54,22 +37,16 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 	// Scale for the Graph
 	private double scaleX;
 	private double scaleY;
-
 	private float[] arrayOfFloats = null;
 	private boolean[] arrayOfBooleans = null;
-
 	private double width = -1;
 	private double height = -1;
-
 	private boolean isElement = false;
 	private boolean isSwitch = false;
-
 	private ArrayList<HolonElement> tempElements = new ArrayList<>();
 	private Model model;
 	private Control controller;
 	private Line2D.Double line = null;
-	GeneralPath graphCurve = new GeneralPath();
-
 	private boolean pointDrag = false;
 	private boolean init = true;
 	private Point tempP = null;
@@ -837,31 +814,36 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 	public void update(ArrayList<AbstractCpsObject> obj) {
 
 		ArrayDeque<AbstractCpsObject> queue = new ArrayDeque<>();
-		ArrayList<HolonElement> list = new ArrayList<>();
 
 		AbstractCpsObject u = null;
 		queue.addAll(obj);
 
 		while (!queue.isEmpty()) {
 			u = queue.pop();
-			if (u instanceof HolonObject) {
-				for (HolonElement ele : ((HolonObject) u).getElements()) {
-					list.add(ele);
-					repaintWithNewElement(list);
-					fillArrayofValue();
-					list.remove(0);
-				}
-			} else if (u instanceof HolonSwitch) {
-				repaintWithNewSwitch((HolonSwitch) u);
-				fillArrayofBooleans();
-			}
-		}
-		empty();
 
-		if (u instanceof CpsUpperNode)
-			for (AbstractCpsObject adjacent : ((CpsUpperNode) u).getNodes()) {
-				queue.add(adjacent);
-			}
-
-	}
+            repaintGraph(u);
+        }
+        empty();
+
+        if (u instanceof CpsUpperNode)
+            for (AbstractCpsObject adjacent : ((CpsUpperNode) u).getNodes()) {
+                queue.add(adjacent);
+            }
+    }
+
+    void repaintGraph(AbstractCpsObject u) {
+        ArrayList<HolonElement> list = new ArrayList<>();
+
+        if (u instanceof HolonObject) {
+            for (HolonElement ele : ((HolonObject) u).getElements()) {
+                list.add(ele);
+                repaintWithNewElement(list);
+                fillArrayofValue();
+                list.remove(0);
+            }
+        } else if (u instanceof HolonSwitch) {
+            repaintWithNewSwitch((HolonSwitch) u);
+            fillArrayofBooleans();
+        }
+    }
 }