TestAlgo.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. package algo;
  2. import java.awt.*;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import javax.swing.*;
  8. import algo.StorageElement.Mode;
  9. import api.AddOn;
  10. import classes.*;
  11. import ui.controller.Control;
  12. public class TestAlgo implements AddOn {
  13. private boolean cancel = false;
  14. // Parameter defined by Algo
  15. private HashMap<Integer, AccessWrapper> access;
  16. private List<Boolean> initialState;
  17. // Gui Part:
  18. private Control control;
  19. private JTextArea textArea;
  20. private JPanel content = new JPanel();
  21. // ProgressBar
  22. private JProgressBar progressBar = new JProgressBar();
  23. private long startTime;
  24. private Thread runThread;
  25. // Blackstart Resistance in Watt
  26. private HolonObject powerPlant;
  27. private TextField blackstartResistanceTextfield;
  28. private TextField blackstartSuccessTimeTextfield;
  29. private TextField powerplantMaxOutputTextfield;
  30. private TextField blackstartStartTimeTextfield;
  31. private TextField simulationDurationTextfield;
  32. private int blackstartSuccessTime;
  33. private int blackstartStartTime;
  34. private int blackstartRunningCounter;
  35. private float powerplantMaxOutput;
  36. private List<HolonObject> renewableProducers;
  37. private List<HolonObject> consumers;
  38. private StorageProductionController SPC;
  39. public static void main(String[] args) {
  40. JFrame newFrame = new JFrame("exampleWindow");
  41. TestAlgo instance = new TestAlgo();
  42. newFrame.setContentPane(instance.getPanel());
  43. newFrame.pack();
  44. newFrame.setVisible(true);
  45. newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  46. }
  47. public TestAlgo() {
  48. content.setLayout(new BorderLayout());
  49. textArea = new JTextArea();
  50. textArea.setEditable(false);
  51. JScrollPane scrollPane = new JScrollPane(textArea);
  52. JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, createOptionPanel(), scrollPane);
  53. splitPane.setResizeWeight(0.0);
  54. content.add(splitPane, BorderLayout.CENTER);
  55. content.setPreferredSize(new Dimension(800, 800));
  56. }
  57. public JPanel createOptionPanel() {
  58. JPanel optionPanel = new JPanel(new BorderLayout());
  59. JScrollPane scrollPane = new JScrollPane(createParameterPanel());
  60. scrollPane.setBorder(BorderFactory.createTitledBorder("Parameter"));
  61. optionPanel.add(scrollPane, BorderLayout.CENTER);
  62. optionPanel.add(createButtonPanel(), BorderLayout.PAGE_END);
  63. return optionPanel;
  64. }
  65. private Component createParameterPanel() {
  66. JPanel parameterPanel = new JPanel(null);
  67. parameterPanel.setPreferredSize(new Dimension(510, 300));
  68. blackstartResistanceTextfield = new TextField("5000000");
  69. blackstartResistanceTextfield.setBounds(10, 10, 170, 20);
  70. parameterPanel.add(blackstartResistanceTextfield);
  71. JLabel blackstartResistanceLabel = new JLabel("Blackstart resistance in Watt");
  72. blackstartResistanceLabel.setBounds(185, 10, 300, 20);
  73. parameterPanel.add(blackstartResistanceLabel);
  74. blackstartSuccessTimeTextfield = new TextField("30");
  75. blackstartSuccessTimeTextfield.setBounds(10, 35, 170, 20);
  76. parameterPanel.add(blackstartSuccessTimeTextfield);
  77. JLabel blackstartSuccessTimeLabel = new JLabel("Time for successfull blackstart in minutes(iterations)");
  78. blackstartSuccessTimeLabel.setBounds(185, 35, 300, 20);
  79. parameterPanel.add(blackstartSuccessTimeLabel);
  80. blackstartStartTimeTextfield = new TextField("15");
  81. blackstartStartTimeTextfield.setBounds(10, 60, 170, 20);
  82. parameterPanel.add(blackstartStartTimeTextfield);
  83. JLabel blackstartStartTimeLabel = new JLabel("Starttime for the blackstart");
  84. blackstartStartTimeLabel.setBounds(185, 60, 300, 20);
  85. parameterPanel.add(blackstartStartTimeLabel);
  86. simulationDurationTextfield = new TextField("500");// TODO:!
  87. simulationDurationTextfield.setBounds(10, 85, 170, 20);
  88. parameterPanel.add(simulationDurationTextfield);
  89. JLabel simulationDurationLabel = new JLabel("How long should the simulation run?");
  90. simulationDurationLabel.setBounds(185, 85, 300, 20);
  91. parameterPanel.add(simulationDurationLabel);
  92. powerplantMaxOutputTextfield = new TextField("40000000");
  93. powerplantMaxOutputTextfield.setBounds(10, 110, 170, 20);
  94. parameterPanel.add(powerplantMaxOutputTextfield);
  95. JLabel powerplantMaxOutputLabel = new JLabel("Powerplant Output afer Blackstart");
  96. powerplantMaxOutputLabel.setBounds(185, 110, 300, 20);
  97. parameterPanel.add(powerplantMaxOutputLabel);
  98. // JButton selectGroupNodeButton = new JButton("Select GroupNode");
  99. // selectGroupNodeButton.setEnabled(false);
  100. // selectGroupNodeButton.setBounds(10, 25, 165, 20);
  101. // selectGroupNodeButton.addActionListener(actionEvent -> selectGroupNode());
  102. // borderPanel.add(selectGroupNodeButton);
  103. // JCheckBox useGroupNodeCheckBox = new JCheckBox();
  104. // useGroupNodeCheckBox.setSelected(false);
  105. // useGroupNodeCheckBox.setBounds(155, 1, 25, 20);
  106. // useGroupNodeCheckBox.addActionListener(actionEvent -> {
  107. // useGroupNode = useGroupNodeCheckBox.isSelected();
  108. // selectGroupNodeButton.setEnabled(useGroupNode);
  109. // });
  110. // borderPanel.add(useGroupNodeCheckBox);
  111. return parameterPanel;
  112. }
  113. public JPanel createButtonPanel() {
  114. JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  115. JButton cancelButton = new JButton("Cancel Run");
  116. cancelButton.addActionListener(actionEvent -> cancel());
  117. buttonPanel.add(cancelButton);
  118. JButton clearButton = new JButton("Clear Console");
  119. clearButton.addActionListener(actionEvent -> clear());
  120. buttonPanel.add(clearButton);
  121. JButton resetButton = new JButton("Reset");
  122. resetButton.setToolTipText("Resets the State to before the Algorithm has runed.");
  123. resetButton.addActionListener(actionEvent -> reset());
  124. buttonPanel.add(resetButton);
  125. JButton runButton = new JButton("Run");
  126. runButton.addActionListener(actionEvent -> {
  127. Runnable task = () -> run();
  128. runThread = new Thread(task);
  129. runThread.start();
  130. });
  131. buttonPanel.add(runButton);
  132. return buttonPanel;
  133. }
  134. private void cancel() {
  135. if (runThread.isAlive()) {
  136. println("");
  137. println("Cancel run.");
  138. cancel = true;
  139. progressBar.setValue(0);
  140. } else {
  141. println("Nothing to cancel.");
  142. }
  143. }
  144. private void run() {
  145. cancel = false;
  146. disableGuiInput(true);
  147. startTimer();
  148. initAlgo();
  149. if (cancel) {
  150. reset();
  151. disableGuiInput(false);
  152. return;
  153. }
  154. printElapsedTime();
  155. disableGuiInput(false);
  156. }
  157. private void reset() {
  158. if (initialState != null) {
  159. println("Resetting..");
  160. resetState();
  161. updateVisual();
  162. } else {
  163. println("No run inistialized.");
  164. }
  165. }
  166. private void disableGuiInput(boolean bool) {
  167. control.guiDisable(bool);
  168. }
  169. @Override
  170. public JPanel getPanel() {
  171. return content;
  172. }
  173. @Override
  174. public void setController(Control control) {
  175. this.control = control;
  176. }
  177. private void clear() {
  178. textArea.setText("");
  179. }
  180. private void print(String message) {
  181. textArea.append(message);
  182. }
  183. private void println(String message) {
  184. textArea.append(message + "\n");
  185. }
  186. private void startTimer() {
  187. startTime = System.currentTimeMillis();
  188. }
  189. private void printElapsedTime() {
  190. long elapsedMilliSeconds = System.currentTimeMillis() - startTime;
  191. println("Execution Time of Algo in Milliseconds:" + elapsedMilliSeconds);
  192. }
  193. ///////////////////////////////////////////////////////////////////////////////////
  194. /**
  195. *
  196. */
  197. private void initAlgo() {
  198. try {
  199. // init
  200. SPC = new StorageProductionController(getStorageElements());// DANGER DONT GIVE NULL
  201. renewableProducers = getRenewableProducers();
  202. consumers = getConsumers();
  203. powerPlant = getPowerPlant();// DANGER DONT GIVE NULL
  204. if(powerPlant == null){
  205. println("No Power Plant in Model");
  206. return;
  207. }
  208. calcResistance();
  209. blackstartRunningCounter = 0;
  210. deactivateBlackstart();
  211. control.getModel().setCurIteration(0);
  212. // prepare model
  213. /////////
  214. setPowerplantProduction(0);
  215. enableAllConsumers();
  216. // StorageElement ele = new StorageElement("Storage", 1, 0, control.getModel());
  217. // ele.setStatusAndSetEnergy(Mode.EMIT, 5000);
  218. //
  219. // System.out.println(""+ele.getEnergyPerElement());
  220. // powerPlant.addElement(ele);
  221. // for (HolonElement elem : powerPlant.getElements()) {
  222. // if(elem instanceof StorageElement) {
  223. // println("boom");
  224. // }
  225. // }
  226. // TODO: prios?
  227. /////////
  228. // Get parameters from textfields
  229. setPowerPlantBlackstartResistance(-Float.parseFloat(blackstartResistanceTextfield.getText()));
  230. blackstartSuccessTime = Integer.parseInt(blackstartSuccessTimeTextfield.getText());
  231. powerplantMaxOutput = Float.parseFloat(powerplantMaxOutputTextfield.getText());
  232. blackstartStartTime = Integer.parseInt(blackstartStartTimeTextfield.getText());
  233. control.getModel().setIterations(Integer.parseInt(simulationDurationTextfield.getText()));
  234. updateVisual();
  235. if (blackstartStartTime + blackstartSuccessTime > control.getModel().getIterations() - 1) {
  236. println("No Time for the blackstart, use working numbers");
  237. } else {
  238. blackstartMain(control.getModel().getCurIteration());
  239. }
  240. } catch (NumberFormatException e) {
  241. println("Worng Input, only numbers please");
  242. }
  243. }
  244. /**
  245. *
  246. * @param curIteration
  247. */
  248. private void blackstartMain(int curIteration) {
  249. // try {
  250. // Thread.sleep(1000);
  251. // } catch (InterruptedException e) {
  252. // // TODO Auto-generated catch block
  253. // e.printStackTrace();
  254. // }
  255. control.getModel().setCurIteration(curIteration);
  256. if (blackstartRunning()) {
  257. if (!blackstartAlgo(curIteration)) {
  258. // blackstart for this iteration was not successfull
  259. deactivateBlackstart();
  260. updateVisual();
  261. println("Simulation of blackstart failed in Iteration: " + curIteration);
  262. } else {
  263. // blackstart for this iteration was successfull
  264. blackstartRunningCounter++;
  265. if (blackstartRunningCounter == blackstartSuccessTime) {
  266. // blackstart was successfull for the needed iterations
  267. deactivateBlackstart();
  268. enableAllConsumers();
  269. updateVisual();
  270. println("Simulation of blackstart Successfull in Iteration: " + curIteration);
  271. } else {
  272. // blackstart not finished yet
  273. updateVisual();
  274. blackstartMain(curIteration + 1);
  275. }
  276. }
  277. } else {
  278. // blackstart has not started yet
  279. if (curIteration == blackstartStartTime) {
  280. activateBlackstart();
  281. disableConsumers();
  282. blackstartMain(curIteration);
  283. } else {
  284. updateVisual();
  285. blackstartMain(curIteration + 1);
  286. }
  287. }
  288. }
  289. /**
  290. * TODO:HOLEG UNTERVERSORGUNG CHECKEN
  291. *
  292. * @param curIteration
  293. * @return true or false depending on whether the blackstart was successful for
  294. * this iteration
  295. */
  296. private boolean blackstartAlgo(int curIteration) {
  297. if (currentRenewableProduction() < getPowerPlantBlackstartResistance()) {
  298. // renewable energy production is not sufficient for the blackstart
  299. if (SPC.currentPossibleStorageProduction() >= getPowerPlantBlackstartResistance()
  300. - currentRenewableProduction()) {// is there currently enough power available from storage?
  301. SPC.enableStorageProduction(getPowerPlantBlackstartResistance() - currentRenewableProduction());
  302. rampUpPowerplant();
  303. enableConsumers(getPowerplantProduction());
  304. return true;
  305. } else {
  306. // blackstart has failed
  307. println("Not enough storage energy available");
  308. return false;
  309. }
  310. } else {
  311. // renewable energy production is sufficient for the blackstart
  312. rampUpPowerplant();
  313. enableConsumers(
  314. currentRenewableProduction() - getPowerPlantBlackstartResistance() + getPowerplantProduction());
  315. return true;
  316. }
  317. }
  318. private HolonObject getPowerPlant() {
  319. for (AbstractCpsObject cps : control.getModel().getObjectsOnCanvas()) {
  320. // geht nur wenn nur ein power plant vorhanden ist
  321. if (cps instanceof HolonObject) {
  322. if (cps.getObjName().equals("Power Plant")) {
  323. return (HolonObject) cps;
  324. }
  325. }
  326. }
  327. return null;
  328. }
  329. private ArrayList<StorageElement> getStorageElements() {
  330. ArrayList<StorageElement> storageElements = new ArrayList<StorageElement>();
  331. for (HolonObject holonObject : control.getModel().getAllHolonObjectsOnCanvas()) {
  332. // if(holonObject.getName().contentEquals("House")) {
  333. // holonObject.addElement(new StorageElement("Storage", 1, 0, control.getModel()));
  334. // }
  335. for (HolonElement ele : holonObject.getElements()) {
  336. if (ele.getEleName().equals("Storage")) {
  337. println(ele.getEleName() + "is : " + ele.getClass());
  338. if (ele instanceof StorageElement) {
  339. println("boomboomboomboom");
  340. }
  341. }
  342. }
  343. }
  344. return storageElements;
  345. }
  346. private void calcResistance() {
  347. if (powerPlant != null) {
  348. // println("Powerplant Energy @"+powerPlant.getEnergyAtTimeStep(1));
  349. // println("" + getPowerplantProduction());
  350. // travers
  351. if (powerPlant.getConnectedTo().size() > 0) {
  352. for (CpsEdge edge : powerPlant.getConnectedTo()) {
  353. // println(edge.toString());
  354. if (powerPlant.getId() == edge.getA().getId()) {
  355. traversEdges(edge.getB(), powerPlant, calcEdgeLength(edge));
  356. } else {
  357. traversEdges(edge.getA(), powerPlant, calcEdgeLength(edge));
  358. }
  359. }
  360. } else {
  361. println("Nothing connected to powerplant");
  362. }
  363. } else {
  364. println("No powerplant connected");
  365. }
  366. }
  367. private void traversEdges(AbstractCpsObject currentObject, AbstractCpsObject last, double distance) {
  368. if (currentObject.getConnectedTo().size() > 1) { // recursive call for all edges
  369. for (CpsEdge edge : currentObject.getConnectedTo()) {
  370. if (currentObject.getId() == edge.getA().getId()) { // look at which way the edge points
  371. if (last.getId() != edge.getB().getId()) {
  372. traversEdges(edge.getB(), currentObject, distance + calcEdgeLength(edge));
  373. }
  374. } else {
  375. if (last.getId() != edge.getA().getId()) {
  376. traversEdges(edge.getA(), currentObject, distance + calcEdgeLength(edge));
  377. }
  378. }
  379. }
  380. } else { // at leaf
  381. // println("Distance to " + currentObject.getId() + ": " + distance);
  382. // ((HolonObject) currentObject).addElement(new StorageElement("Storage", 1, 0, control.getModel()));
  383. for (HolonElement ele : ((HolonObject) currentObject).getElements()) {
  384. if (ele.isProducer()) {
  385. ele.setEnergyPerElement(5000);// TODO: das wollen wir ja so nicht
  386. // println("Energy: " + ele.getEnergyPerElement());
  387. // set how much energy is left after resistance22
  388. ele.setEnergyPerElement(calcEnergyAfterResistance(ele.getEnergyPerElement(), distance));// TODO: das
  389. // wollen
  390. // wir ja so
  391. // auch
  392. // nicht
  393. // println("Energy after resistance: " + ele.getEnergyPerElement());
  394. }
  395. }
  396. }
  397. }
  398. private float calcEnergyAfterResistance(float currentEnergy, double distance) {
  399. // 230v kupfer 30mm durchmesser
  400. int volatage = 230;
  401. int diameter = 30;
  402. // kupfer
  403. double specificMaterialResistance = 0.017;
  404. double blackstartResistance = Math.pow(volatage, 2) / getPowerPlantBlackstartResistance();
  405. double cableResistance = specificMaterialResistance * (distance / (0.25 * Math.PI * Math.pow(diameter, 2)));
  406. return (float) (1 - (cableResistance / (cableResistance + blackstartResistance))) * currentEnergy;
  407. }
  408. private double calcEdgeLength(CpsEdge edge) {
  409. Position aPos = edge.getA().getPosition();
  410. Position bPos = edge.getB().getPosition();
  411. double xDiff = Math.abs(aPos.x - bPos.x);
  412. double yDiff = Math.abs(aPos.y - bPos.y);
  413. return Math.sqrt(Math.pow(xDiff, 2) + Math.pow(yDiff, 2));
  414. }
  415. private void disableConsumers() {
  416. // TODO: disableBatteryLoading? will ich das wirklich?
  417. // SPC.disableStorageProduction();
  418. for (HolonObject consumer : consumers) {
  419. for (HolonElement ele : consumer.getElements()) {
  420. if (ele.isActive() && ele.isConsumer()) {
  421. ele.setActive(false);
  422. }
  423. }
  424. }
  425. }
  426. private void enableConsumers(float energyAvailable) {
  427. println("currenctrenewable: " + currentRenewableProduction());
  428. println("currenctpossiblestorage: " + SPC.currentPossibleStorageProduction());
  429. println("blackstart resi: " + getPowerPlantBlackstartResistance());
  430. println("current pp production: " + getPowerplantProduction());
  431. println("energy available for consumers" + energyAvailable);
  432. // Damit wir immer die gleiche ausgangslage haben //TODO: wirklich?
  433. disableConsumers();
  434. // TODO: das ganze lieber mit prirotaeten
  435. for (HolonObject consumer : consumers) {
  436. for (HolonElement ele : consumer.getElements()) {
  437. if (energyAvailable > 0
  438. && energyAvailable - Math.abs(ele.getEnergyPerElement() * ele.getAmount()) >= 0) {
  439. if (!ele.isActive()) {
  440. ele.setActive(true);
  441. energyAvailable = energyAvailable - Math.abs(ele.getEnergyPerElement() * ele.getAmount());// +
  442. // since
  443. // its
  444. // negative
  445. // System.out.println("Element " + ele.getId() + " was enabled Energy left: "+ energyAvailable);
  446. }
  447. } else {
  448. return;
  449. }
  450. }
  451. }
  452. // if (!SPC.batteriesFull()) {
  453. // // TODO: load unused batteries
  454. // } else {
  455. //
  456. // }
  457. }
  458. private void enableAllConsumers() {
  459. for (HolonObject consumer : consumers) {
  460. for (HolonElement ele : consumer.getElements()) {
  461. if (!ele.isActive() && ele.isConsumer()) {
  462. ele.setActive(true);
  463. }
  464. }
  465. }
  466. }
  467. private float currentRenewableProduction() {
  468. float production = 0;
  469. for (HolonObject house : renewableProducers) {
  470. for (HolonElement ele : house.getElements()) {
  471. if (ele.getEleName().equals("Solar Panels")) {// TODO: hier muss noch mehr dazu
  472. production = production + ele.getEnergyAtTimeStep(control.getModel().getCurIteration());
  473. }
  474. }
  475. }
  476. return production;
  477. }
  478. private void rampUpPowerplant() {
  479. for (HolonElement ele : powerPlant.getElements()) {
  480. if (ele.getEleName().equals("Power")) {
  481. ele.setEnergyPerElement(ele.getEnergyPerElement() + powerplantMaxOutput / blackstartSuccessTime);
  482. }
  483. }
  484. }
  485. private float getPowerplantProduction() {
  486. float totalProduction = 0;
  487. for (HolonElement ele : powerPlant.getElements()) {
  488. if (ele.getEleName().equals("Power")) {
  489. totalProduction = ele.getEnergyPerElement();
  490. }
  491. }
  492. return totalProduction;
  493. }
  494. private float setPowerplantProduction(float power) {
  495. float totalProduction = 0;
  496. for (HolonElement ele : powerPlant.getElements()) {
  497. if (ele.getEleName().equals("Power")) {
  498. ele.setEnergyPerElement(power);
  499. }
  500. }
  501. return totalProduction;
  502. }
  503. private float getPowerPlantBlackstartResistance() {
  504. for (HolonElement ele : powerPlant.getElements()) {
  505. if (ele.getEleName().equals("Blackstart")) {
  506. return -ele.getEnergyPerElement();
  507. }
  508. }
  509. return 0;
  510. }
  511. private void setPowerPlantBlackstartResistance(float resistance) {
  512. for (HolonElement ele : powerPlant.getElements()) {
  513. if (ele.getEleName().equals("Blackstart")) {
  514. ele.setEnergyPerElement(resistance);
  515. }
  516. }
  517. }
  518. private boolean blackstartRunning() {
  519. for (HolonElement ele : powerPlant.getElements()) {
  520. if (ele.getEleName().equals("Blackstart")) {
  521. if (ele.isActive()) {
  522. return true;
  523. } else {
  524. return false;
  525. }
  526. }
  527. }
  528. return false;
  529. }
  530. private void activateBlackstart() {
  531. for (HolonElement ele : powerPlant.getElements()) {
  532. if (ele.getEleName().equals("Blackstart")) {
  533. ele.setActive(true);
  534. }
  535. }
  536. }
  537. private void deactivateBlackstart() {
  538. for (HolonElement ele : powerPlant.getElements()) {
  539. if (ele.getEleName().equals("Blackstart")) {
  540. ele.setActive(false);
  541. }
  542. }
  543. }
  544. private LinkedList<HolonObject> getRenewableProducers() {
  545. LinkedList<HolonObject> list = new LinkedList<HolonObject>();
  546. for (HolonObject holonObject : control.getModel().getAllHolonObjectsOnCanvas()) {
  547. if (holonObject.getObjName().contentEquals("House")) {// TODO: das reicht so nicht da muss noch gecheckt
  548. // werden ob es renewables gibt
  549. list.add(holonObject);
  550. }
  551. }
  552. return list;
  553. }
  554. private LinkedList<HolonObject> getConsumers() {
  555. LinkedList<HolonObject> list = new LinkedList<HolonObject>();
  556. for (HolonObject holonObject : control.getModel().getAllHolonObjectsOnCanvas()) {
  557. if (holonObject.getObjName().contentEquals("House")) {// TODO: das reicht so nicht da muss noch gecheckt
  558. // werden ob es consumer gibt
  559. list.add(holonObject);
  560. }
  561. }
  562. return list;
  563. }
  564. /**
  565. * To let the User See the current state without touching the Canvas.
  566. */
  567. private void updateVisual() {
  568. System.out.println("Start updateVisual in Iteration: " + control.getModel().getCurIteration());
  569. control.calculateStateAndVisualForCurrentTimeStep();
  570. control.updateCanvas();
  571. System.out.println("Finish updateVisual in Iteration: " + control.getModel().getCurIteration());
  572. }
  573. /**
  574. * Sets the Model back to its original State before the LAST run.
  575. */
  576. private void resetState() {
  577. setState(initialState);
  578. }
  579. /**
  580. * Sets the State out of the given position for calculation or to show the user.
  581. *
  582. * @param position
  583. */
  584. private void setState(List<Boolean> position) {
  585. for (int i = 0; i < position.size(); i++) {
  586. access.get(i).setState(position.get(i));
  587. }
  588. }
  589. /**
  590. * A Wrapper Class for Access HolonElement and HolonSwitch in one Element and
  591. * not have to split the List.
  592. */
  593. private class AccessWrapper {
  594. public static final int HOLONELEMENT = 0;
  595. public static final int SWITCH = 1;
  596. private int type;
  597. private HolonSwitch hSwitch;
  598. private HolonElement hElement;
  599. public AccessWrapper(HolonSwitch hSwitch) {
  600. type = SWITCH;
  601. this.hSwitch = hSwitch;
  602. }
  603. public AccessWrapper(HolonElement hElement) {
  604. type = HOLONELEMENT;
  605. this.hElement = hElement;
  606. }
  607. public void setState(boolean state) {
  608. if (type == HOLONELEMENT) {
  609. hElement.setActive(state);
  610. } else {// is switch
  611. hSwitch.setManualMode(true);
  612. hSwitch.setManualState(state);
  613. }
  614. }
  615. public boolean getState(int timeStep) {
  616. return (type == HOLONELEMENT) ? hElement.isActive() : hSwitch.getState(timeStep);
  617. }
  618. public int getType() {
  619. return type;
  620. }
  621. }
  622. private class Handle<T> {
  623. public T object;
  624. Handle(T object) {
  625. this.object = object;
  626. }
  627. public String toString() {
  628. return object.toString();
  629. }
  630. }
  631. }