123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- package ui.controller;
- import java.util.ArrayList;
- import java.util.HashMap;
- import classes.CpsEdge;
- import classes.CpsNode;
- import classes.AbstractCpsObject;
- import classes.HolonElement;
- import classes.HolonObject;
- import classes.HolonSwitch;
- 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;
- private ArrayList<CpsEdge> allConnections;
- private ArrayList<SubNet> subNets;
- private MyCanvas canvas;
- private int timeStep;
- private boolean simMode;
- private HashMap<Integer, Float> tagTable = new HashMap<Integer, Float>();
- /**
- * Constructor.
- *
- * @param m
- * Model
- */
- public SimulationManager(Model m) {
- canvas = null;
- model = m;
- subNets = new ArrayList<SubNet>();
- simMode = model.getIsSimulation();
- }
- /**
- * calculates the flow of the edges and the supply for objects.
- *
- * @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>());
- }
- for (SubNet singleSubNet : subNets) {
- float production = calculateEnergy("prod", singleSubNet, timeStep);
- float consumption = calculateEnergy("cons", singleSubNet, timeStep);
- float minConsumption = calculateMinimumEnergy(singleSubNet, timeStep);
- setFlow(singleSubNet, simMode);
- for (HolonObject hl : singleSubNet.getObjects()) {
- if (!(hl.getState() == 0) && !(hl.getState() == 3)) {
- for (int i = 0; i < hl.getConnections().size(); i++) {
- CpsEdge edge = hl.getConnectedTo().get(i);
- if (edge.getState() && edge.getFlow() > 0 || edge.getCapacity() == -1) {
- // 0 = no energy, 1 = not supplied, 2 = supplied, 3
- // = producer, 4 = partially supplied
- if ((production + consumption) >= 0) {
- hl.setState(2);
- }
- if ((production + consumption) < 0) {
- if ((production + minConsumption) >= 0) {
- hl.setState(4);
- } else if (hl.checkIfPartiallySupplied(timeStep)) {
- hl.setState(4);
- } else {
- hl.setState(1);
- }
- }
- break;
- }
- hl.setState(1);
- }
- if (hl.checkIfPartiallySupplied(timeStep) && !(hl.getState() == 2)) {
- hl.setState(4);
- }
- }
- }
- }
- 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);
- } else {
- setFlowModelation(sN);
- }
- }
- /**
- * set the Flow Modelation.
- *
- * @param sN
- * Subnet
- */
- public void setFlowModelation(SubNet sN) {
- for (HolonObject hl : sN.getObjects()) {
- float energy = hl.getCurrentEnergyAtTimeStep(timeStep);
- if (energy > 0) {
- for (CpsEdge e : sN.getEdges()) {
- e.setFlow(e.getFlow() + energy);
- e.calculateState(simMode);
- }
- }
- }
- }
- /**
- * Set Flow Simulation.
- *
- * @param sN
- * Subnet
- */
- public void setFlowSimulation(SubNet sN) {
- ArrayList<AbstractCpsObject> producers = new ArrayList<AbstractCpsObject>();
- AbstractCpsObject tmp = null;
- tagTable = new HashMap<Integer, Float>();
- for (HolonObject hl : sN.getObjects()) {
- float energy = hl.getCurrentEnergyAtTimeStep(timeStep);
- if (energy > 0) {
- tagTable.put(hl.getID(), energy);
- hl.addTag(hl.getID());
- for (CpsEdge edge : hl.getConnections()) {
- if (edge.getState()) {
- if (edge.getA().getID() == hl.getID()) {
- edge.getB().addTag(hl.getID());
- tmp = edge.getB();
- }
- if (edge.getB().getID() == hl.getID()) {
- edge.getA().addTag(hl.getID());
- tmp = edge.getA();
- }
- edge.setFlow(edge.getFlow() + energy);
- edge.calculateState(true);
- edge.addTag(hl.getID());
- if (edge.getState() && !producers.contains(tmp)) {
- producers.add(tmp);
- }
- }
- }
- }
- }
- 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>();
- AbstractCpsObject tmp = null;
- if (nodes.size() != 0) {
- for (AbstractCpsObject cps : nodes) {
- for (CpsEdge edge : cps.getConnections()) {
- for (Integer tag : cps.getTag()) {
- if (edge.getState() && !(edge.getTags().contains(tag))) {
- edge.setFlow(edge.getFlow() + tagTable.get(tag));
- edge.calculateState(true);
- edge.addTag(tag);
- if (edge.getA().getID() == cps.getID()) {
- tmp = edge.getB();
- }
- if (edge.getB().getID() == cps.getID()) {
- tmp = edge.getA();
- }
- if (tmp.getPseudoTags() != null) {
- pseudoTags.addAll(tmp.getPseudoTags());
- }
- pseudoTags.addAll(cps.getTag());
- tmp.setPseudoTags(mergeLists(tmp.getTag(), pseudoTags));
- if (!edge.getState()) {
- newNodes.remove(edge.getA());
- newNodes.remove(edge.getB());
- if (edge.getA().getID() == cps.getID()) {
- edge.getB().getPseudoTags().removeAll(edge.getTags());
- }
- if (edge.getB().getID() == cps.getID()) {
- edge.getA().getPseudoTags().removeAll(edge.getTags());
- }
- }
- if (edge.getState() && !(newNodes.contains(tmp))) {
- newNodes.add(tmp);
- }
- }
- }
- edge.calculateState(true);
- }
- }
- // printNodes(newNodes);
- setPseudoTags(newNodes);
- setFlowSimRec(newNodes, iter + 1);
- }
- }
- /**
- * 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) {
- System.out.println(node.getID());
- }
- }
- /**
- * Get String.
- *
- * @param tags
- * the tags
- * @return the String
- */
- public String getString(ArrayList<Integer> tags) {
- String result = "";
- for (Integer i : tags) {
- result = result + ", " + i;
- }
- return result;
- }
- /**
- * 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) {
- if (!(result.contains(i))) {
- result.add(i);
- }
- }
- for (Integer j : b) {
- if (!(result.contains(j))) {
- result.add(j);
- }
- }
- return result;
- }
- /**
- * 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();
- for (CpsEdge e : cps.getConnections()) {
- if (!(visitedEdges.contains(e))) {
- e.setFlow(0);
- e.calculateState(simMode);
- e.setTags(new ArrayList<Integer>());
- visitedEdges.add(e);
- if (!(visitedObj.contains(e.getA().getID()))) {
- resetConnections(e.getA(), visitedObj, visitedEdges);
- e.getA().resetTags();
- }
- if (!(visitedObj.contains(e.getB().getID()))) {
- resetConnections(e.getB(), visitedObj, visitedEdges);
- e.getB().resetTags();
- }
- }
- }
- }
- /**
- * calculates the energy of either all producers or consumers.
- *
- * @param type
- * Type
- * @param sN
- * Subnet
- * @param x
- * Integer
- *
- * @return The Energy
- */
- public float calculateEnergy(String type, SubNet sN, int x) {
- float energy = 0;
- for (HolonObject hl : sN.getObjects()) {
- if (type.equals("prod")) {
- if (hl.getCurrentEnergyAtTimeStep(x) > 0) {
- energy = energy + hl.getCurrentEnergyAtTimeStep(x);
- hl.setState(3);
- }
- }
- if (type.equals("cons")) {
- if (hl.getCurrentEnergyAtTimeStep(x) < 0) {
- energy = energy + hl.getCurrentEnergyAtTimeStep(x);
- hl.setState(1);
- }
- }
- if (hl.getCurrentEnergyAtTimeStep(x) == 0) {
- hl.setState(0);
- }
- }
- 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;
- for (HolonObject hl : sN.getObjects()) {
- if (hl.getElements().size() > 0 && hl.getElements().get(0).getTotalEnergyAtTimeStep(x) < 0) {
- minElement = hl.getElements().get(0).getTotalEnergyAtTimeStep(x);
- }
- for (HolonElement he : hl.getElements()) {
- if (minElement < he.getTotalEnergyAtTimeStep(x) && he.getTotalEnergyAtTimeStep(x) < 0) {
- minElement = he.getTotalEnergyAtTimeStep(x);
- }
- }
- min = min + minElement;
- }
- return min;
- }
- /**
- * generates all subNets from all objectsToHandle.
- */
- public void searchForSubNets() {
- subNets = new ArrayList<SubNet>();
- boolean end = false;
- int i = 0;
- AbstractCpsObject cps;
- if (objectsToHandle.size() > 0) {
- while (!end) {
- cps = objectsToHandle.get(i);
- SubNet singleSubNet = new SubNet(new ArrayList<HolonObject>(), new ArrayList<CpsEdge>(),
- new ArrayList<HolonSwitch>());
- singleSubNet = buildSubNet(cps, new ArrayList<Integer>(), singleSubNet);
- if (singleSubNet.getObjects().size() != 0) {
- subNets.add(singleSubNet);
- }
- if (0 == objectsToHandle.size()) {
- end = true;
- }
- }
- }
- }
- /**
- * recursivly generates a subnet of all objects, that one specific object is
- * connected to.
- *
- * @param cps
- * AbstractCpsObject
- * @param visited
- * visited Array of Integer
- * @param sN
- * Subnets
- * @return Subnet
- */
- public SubNet buildSubNet(AbstractCpsObject cps, ArrayList<Integer> visited, SubNet sN) {
- visited.add(cps.getID());
- if (cps instanceof HolonObject) {
- sN.getObjects().add((HolonObject) cps);
- }
- if (cps instanceof HolonSwitch) {
- sN.getSwitches().add((HolonSwitch) cps);
- }
- removeFromToHandle(cps.getID());
- AbstractCpsObject a;
- AbstractCpsObject b;
- for (CpsEdge edge : cps.getConnections()) {
- 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(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)) {
- if (neighbor instanceof HolonSwitch) {
- if (((HolonSwitch) neighbor).getState(timeStep)) {
- return true;
- } else {
- return false;
- }
- }
- } else {
- return false;
- }
- }
- return true;
- }
- /**
- * removes an Object that already has been handled with.
- *
- * @param id the Object ID
- */
- public void removeFromToHandle(int id) {
- for (int i = 0; i < objectsToHandle.size(); i++) {
- if (objectsToHandle.get(i).getID() == id) {
- objectsToHandle.remove(i);
- }
- }
- }
- /**
- * ensures that objectsToHandle only contains HolonObjects.
- */
- public void cleanObjectsToHandle() {
- for (int i = 0; i < objectsToHandle.size(); i++) {
- if (!(objectsToHandle.get(i) instanceof HolonObject)) {
- objectsToHandle.remove(i);
- }
- }
- }
- /**
- * copies the data of an array of Objects.
- *
- * @param toCopy the ArrayList of CpsObjects co Copy
- */
- public void copyObjects(ArrayList<AbstractCpsObject> toCopy) {
- objectsToHandle = new ArrayList<AbstractCpsObject>();
- for (AbstractCpsObject cps : toCopy) {
- objectsToHandle.add(cps);
- }
- }
- /**
- * Prints the Components auf all subnets.
- */
- public void printNet() {
- for (int i = 0; i < subNets.size(); i++) {
- System.out.println("SUBNET NR:" + i);
- System.out.println(" Objects:");
- for (int j = 0; j < subNets.get(i).getObjects().size(); j++) {
- HolonObject hl = subNets.get(i).getObjects().get(j);
- System.out.println(" " + hl.getName() + " " + hl.getID());
- }
- System.out.println(" Edges:");
- for (int j = 0; j < subNets.get(i).getEdges().size(); j++) {
- CpsEdge edge = subNets.get(i).getEdges().get(j);
- System.out.println(" " + edge.getA().getName() + " connected To " + edge.getB().getName());
- }
- System.out.println(" Switches:");
- for (int j = 0; j < subNets.get(i).getSwitches().size(); j++) {
- HolonSwitch sw = subNets.get(i).getSwitches().get(j);
- System.out.println(" " + sw.getName() + " " + sw.getID() + " State:" + sw.getActiveAt()[timeStep]);
- }
- }
- }
- /**
- * Set the Canvas.
- *
- * @param can
- * the Canvas
- */
- public void setCanvas(MyCanvas can) {
- canvas = can;
- }
- /**
- * Reset all Data to the current state of the Model.
- */
- public void reset() {
- copyObjects(model.getObjectsOnCanvas());
- }
- /**
- * Get all Subnets.
- *
- * @return all Subnets
- */
- public ArrayList<SubNet> getSubNets() {
- return subNets;
- }
- }
|