|
@@ -13,6 +13,11 @@ import classes.SubNet;
|
|
|
import ui.model.Model;
|
|
|
import ui.view.MyCanvas;
|
|
|
|
|
|
+
|
|
|
+ * Controller for Simulation.
|
|
|
+ *
|
|
|
+ * @author Gruppe14
|
|
|
+ */
|
|
|
public class SimulationManager {
|
|
|
private Model model;
|
|
|
private ArrayList<AbstractCpsObject> objectsToHandle;
|
|
@@ -23,6 +28,12 @@ public class SimulationManager {
|
|
|
private boolean simMode;
|
|
|
private HashMap<Integer, Float> tagTable = new HashMap<Integer, Float>();
|
|
|
|
|
|
+
|
|
|
+ * Constructor.
|
|
|
+ *
|
|
|
+ * @param m
|
|
|
+ * Model
|
|
|
+ */
|
|
|
public SimulationManager(Model m) {
|
|
|
canvas = null;
|
|
|
model = m;
|
|
@@ -33,14 +44,15 @@ public class SimulationManager {
|
|
|
|
|
|
* calculates the flow of the edges and the supply for objects.
|
|
|
*
|
|
|
- * @param x current Iteration
|
|
|
+ * @param x
|
|
|
+ * current Iteration
|
|
|
*/
|
|
|
public void calculateStateForTimeStep(int x) {
|
|
|
simMode = model.getIsSimulation();
|
|
|
timeStep = x;
|
|
|
searchForSubNets();
|
|
|
for (SubNet singleSubNet : subNets) {
|
|
|
- ResetConnections(singleSubNet.getObjects().get(0), new ArrayList<Integer>(), new ArrayList<CpsEdge>());
|
|
|
+ resetConnections(singleSubNet.getObjects().get(0), new ArrayList<Integer>(), new ArrayList<CpsEdge>());
|
|
|
}
|
|
|
for (SubNet singleSubNet : subNets) {
|
|
|
float production = calculateEnergy("prod", singleSubNet, timeStep);
|
|
@@ -79,6 +91,14 @@ public class SimulationManager {
|
|
|
canvas.repaint();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Sets the Flow.
|
|
|
+ *
|
|
|
+ * @param sN
|
|
|
+ * Subnet
|
|
|
+ * @param simulation
|
|
|
+ * boolean if Simulation is on
|
|
|
+ */
|
|
|
public void setFlow(SubNet sN, boolean simulation) {
|
|
|
if (simulation) {
|
|
|
setFlowSimulation(sN);
|
|
@@ -87,6 +107,12 @@ public class SimulationManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * set the Flow Modelation.
|
|
|
+ *
|
|
|
+ * @param sN
|
|
|
+ * Subnet
|
|
|
+ */
|
|
|
public void setFlowModelation(SubNet sN) {
|
|
|
for (HolonObject hl : sN.getObjects()) {
|
|
|
float energy = hl.getCurrentEnergyAtTimeStep(timeStep);
|
|
@@ -99,6 +125,12 @@ public class SimulationManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Set Flow Simulation.
|
|
|
+ *
|
|
|
+ * @param sN
|
|
|
+ * Subnet
|
|
|
+ */
|
|
|
public void setFlowSimulation(SubNet sN) {
|
|
|
ArrayList<AbstractCpsObject> producers = new ArrayList<AbstractCpsObject>();
|
|
|
AbstractCpsObject tmp = null;
|
|
@@ -131,6 +163,14 @@ public class SimulationManager {
|
|
|
setFlowSimRec(producers, 0);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Set Flow Rimulation Rec.
|
|
|
+ *
|
|
|
+ * @param nodes
|
|
|
+ * the nodes
|
|
|
+ * @param iter
|
|
|
+ * the Iteration
|
|
|
+ */
|
|
|
public void setFlowSimRec(ArrayList<AbstractCpsObject> nodes, int iter) {
|
|
|
ArrayList<AbstractCpsObject> newNodes = new ArrayList<AbstractCpsObject>();
|
|
|
ArrayList<Integer> pseudoTags = new ArrayList<Integer>();
|
|
@@ -179,12 +219,24 @@ public class SimulationManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Set the Pseudo Tags.
|
|
|
+ *
|
|
|
+ * @param nodes
|
|
|
+ * Array of AbstractCpsObjects
|
|
|
+ */
|
|
|
public void setPseudoTags(ArrayList<AbstractCpsObject> nodes) {
|
|
|
for (AbstractCpsObject node : nodes) {
|
|
|
node.setTags(node.getPseudoTags());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Print the nodes.
|
|
|
+ *
|
|
|
+ * @param nodes
|
|
|
+ * Array of AbstractCpsObject
|
|
|
+ */
|
|
|
public void printNodes(ArrayList<AbstractCpsObject> nodes) {
|
|
|
System.out.println("new Nodes:");
|
|
|
for (AbstractCpsObject node : nodes) {
|
|
@@ -192,6 +244,13 @@ public class SimulationManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Get String.
|
|
|
+ *
|
|
|
+ * @param tags
|
|
|
+ * the tags
|
|
|
+ * @return the String
|
|
|
+ */
|
|
|
public String getString(ArrayList<Integer> tags) {
|
|
|
String result = "";
|
|
|
for (Integer i : tags) {
|
|
@@ -200,14 +259,23 @@ public class SimulationManager {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- public ArrayList<Integer> mergeLists(ArrayList<Integer> A, ArrayList<Integer> B) {
|
|
|
+
|
|
|
+ * Merge the Lists.
|
|
|
+ *
|
|
|
+ * @param a
|
|
|
+ * first liust
|
|
|
+ * @param b
|
|
|
+ * second list
|
|
|
+ * @return the Result
|
|
|
+ */
|
|
|
+ public ArrayList<Integer> mergeLists(ArrayList<Integer> a, ArrayList<Integer> b) {
|
|
|
ArrayList<Integer> result = new ArrayList<Integer>();
|
|
|
- for (Integer i : A) {
|
|
|
+ for (Integer i : a) {
|
|
|
if (!(result.contains(i))) {
|
|
|
result.add(i);
|
|
|
}
|
|
|
}
|
|
|
- for (Integer j : B) {
|
|
|
+ for (Integer j : b) {
|
|
|
if (!(result.contains(j))) {
|
|
|
result.add(j);
|
|
|
}
|
|
@@ -215,7 +283,17 @@ public class SimulationManager {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- public void ResetConnections(AbstractCpsObject cps, ArrayList<Integer> visitedObj,
|
|
|
+
|
|
|
+ * Reset the Connection.
|
|
|
+ *
|
|
|
+ * @param cps
|
|
|
+ * CpsObject
|
|
|
+ * @param visitedObj
|
|
|
+ * the visited Objects
|
|
|
+ * @param visitedEdges
|
|
|
+ * the visited Edges
|
|
|
+ */
|
|
|
+ public void resetConnections(AbstractCpsObject cps, ArrayList<Integer> visitedObj,
|
|
|
ArrayList<CpsEdge> visitedEdges) {
|
|
|
visitedObj.add(cps.getID());
|
|
|
cps.resetTags();
|
|
@@ -226,11 +304,11 @@ public class SimulationManager {
|
|
|
e.setTags(new ArrayList<Integer>());
|
|
|
visitedEdges.add(e);
|
|
|
if (!(visitedObj.contains(e.getA().getID()))) {
|
|
|
- ResetConnections(e.getA(), visitedObj, visitedEdges);
|
|
|
+ resetConnections(e.getA(), visitedObj, visitedEdges);
|
|
|
e.getA().resetTags();
|
|
|
}
|
|
|
if (!(visitedObj.contains(e.getB().getID()))) {
|
|
|
- ResetConnections(e.getB(), visitedObj, visitedEdges);
|
|
|
+ resetConnections(e.getB(), visitedObj, visitedEdges);
|
|
|
e.getB().resetTags();
|
|
|
}
|
|
|
}
|
|
@@ -238,11 +316,16 @@ public class SimulationManager {
|
|
|
}
|
|
|
|
|
|
|
|
|
- * calculates the energy of either all producers or consumers
|
|
|
+ * calculates the energy of either all producers or consumers.
|
|
|
*
|
|
|
* @param type
|
|
|
+ * Type
|
|
|
* @param sN
|
|
|
- * @return
|
|
|
+ * Subnet
|
|
|
+ * @param x
|
|
|
+ * Integer
|
|
|
+ *
|
|
|
+ * @return The Energy
|
|
|
*/
|
|
|
public float calculateEnergy(String type, SubNet sN, int x) {
|
|
|
float energy = 0;
|
|
@@ -266,6 +349,15 @@ public class SimulationManager {
|
|
|
return energy;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Calculate the Minimum Energy.
|
|
|
+ *
|
|
|
+ * @param sN
|
|
|
+ * Subnet
|
|
|
+ * @param x
|
|
|
+ * Integer
|
|
|
+ * @return the Calculated minimum Energy
|
|
|
+ */
|
|
|
public float calculateMinimumEnergy(SubNet sN, int x) {
|
|
|
float min = 0;
|
|
|
float minElement = 0;
|
|
@@ -284,7 +376,7 @@ public class SimulationManager {
|
|
|
}
|
|
|
|
|
|
|
|
|
- * generates all subNets from all objectsToHandle
|
|
|
+ * generates all subNets from all objectsToHandle.
|
|
|
*/
|
|
|
public void searchForSubNets() {
|
|
|
subNets = new ArrayList<SubNet>();
|
|
@@ -309,12 +401,15 @@ public class SimulationManager {
|
|
|
|
|
|
|
|
|
* recursivly generates a subnet of all objects, that one specific object is
|
|
|
- * connected to
|
|
|
+ * connected to.
|
|
|
*
|
|
|
* @param cps
|
|
|
+ * AbstractCpsObject
|
|
|
* @param visited
|
|
|
+ * visited Array of Integer
|
|
|
* @param sN
|
|
|
- * @return
|
|
|
+ * Subnets
|
|
|
+ * @return Subnet
|
|
|
*/
|
|
|
public SubNet buildSubNet(AbstractCpsObject cps, ArrayList<Integer> visited, SubNet sN) {
|
|
|
visited.add(cps.getID());
|
|
@@ -325,26 +420,33 @@ public class SimulationManager {
|
|
|
sN.getSwitches().add((HolonSwitch) cps);
|
|
|
}
|
|
|
removeFromToHandle(cps.getID());
|
|
|
- AbstractCpsObject A;
|
|
|
- AbstractCpsObject B;
|
|
|
+ AbstractCpsObject a;
|
|
|
+ AbstractCpsObject b;
|
|
|
for (CpsEdge edge : cps.getConnections()) {
|
|
|
- A = edge.getA();
|
|
|
- B = edge.getB();
|
|
|
+ a = edge.getA();
|
|
|
+ b = edge.getB();
|
|
|
if (!(cps instanceof HolonSwitch)) {
|
|
|
if (!(sN.getEdges().contains(edge))) {
|
|
|
sN.getEdges().add(edge);
|
|
|
}
|
|
|
}
|
|
|
- if (!visited.contains(A.getID()) && legitState(A, cps)) {
|
|
|
- sN = buildSubNet(A, visited, sN);
|
|
|
+ if (!visited.contains(a.getID()) && legitState(a, cps)) {
|
|
|
+ sN = buildSubNet(a, visited, sN);
|
|
|
}
|
|
|
- if (!visited.contains(B.getID()) && legitState(B, cps)) {
|
|
|
- sN = buildSubNet(B, visited, sN);
|
|
|
+ if (!visited.contains(b.getID()) && legitState(b, cps)) {
|
|
|
+ sN = buildSubNet(b, visited, sN);
|
|
|
}
|
|
|
}
|
|
|
return sN;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * is the Switch in a legitimate State.
|
|
|
+ *
|
|
|
+ * @param neighbor AbstractCpsObject
|
|
|
+ * @param current AbstractCpsObject
|
|
|
+ * @return boolean
|
|
|
+ */
|
|
|
public boolean legitState(AbstractCpsObject neighbor, AbstractCpsObject current) {
|
|
|
if (current instanceof HolonSwitch) {
|
|
|
if (((HolonSwitch) current).getState(timeStep)) {
|
|
@@ -363,9 +465,9 @@ public class SimulationManager {
|
|
|
}
|
|
|
|
|
|
|
|
|
- * removes an Object that already has been handled with
|
|
|
+ * removes an Object that already has been handled with.
|
|
|
*
|
|
|
- * @param id
|
|
|
+ * @param id the Object ID
|
|
|
*/
|
|
|
public void removeFromToHandle(int id) {
|
|
|
for (int i = 0; i < objectsToHandle.size(); i++) {
|
|
@@ -376,7 +478,7 @@ public class SimulationManager {
|
|
|
}
|
|
|
|
|
|
|
|
|
- * ensures that objectsToHandle only contains HolonObjects
|
|
|
+ * ensures that objectsToHandle only contains HolonObjects.
|
|
|
*/
|
|
|
public void cleanObjectsToHandle() {
|
|
|
for (int i = 0; i < objectsToHandle.size(); i++) {
|
|
@@ -387,9 +489,9 @@ public class SimulationManager {
|
|
|
}
|
|
|
|
|
|
|
|
|
- * copies the data of an array of Objects
|
|
|
+ * copies the data of an array of Objects.
|
|
|
*
|
|
|
- * @param toCopy
|
|
|
+ * @param toCopy the ArrayList of CpsObjects co Copy
|
|
|
*/
|
|
|
public void copyObjects(ArrayList<AbstractCpsObject> toCopy) {
|
|
|
objectsToHandle = new ArrayList<AbstractCpsObject>();
|
|
@@ -399,7 +501,7 @@ public class SimulationManager {
|
|
|
}
|
|
|
|
|
|
|
|
|
- * Prints the Components auf all subnets
|
|
|
+ * Prints the Components auf all subnets.
|
|
|
*/
|
|
|
public void printNet() {
|
|
|
for (int i = 0; i < subNets.size(); i++) {
|