VisualRepresentationalState.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. package holeg.ui.model;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import java.util.ListIterator;
  7. import java.util.stream.Collectors;
  8. import holeg.model.AbstractCanvasObject;
  9. import holeg.model.Edge;
  10. import holeg.model.GroupNode;
  11. import holeg.model.HolonObject;
  12. import holeg.model.Node;
  13. import holeg.ui.model.ExitCable.ExitCableState;
  14. public class VisualRepresentationalState {
  15. private ArrayList<Supplier> supplierList = new ArrayList<>();
  16. private ArrayList<Passiv> passivList= new ArrayList<>();
  17. private ArrayList<Consumer> consumerList= new ArrayList<>();
  18. private ArrayList<Node> nodeList= new ArrayList<>();
  19. private ArrayList<Edge> cableList= new ArrayList<>();
  20. private ArrayList<DecoratedSwitch> switchList= new ArrayList<>();
  21. private ArrayList<DecoratedGroupNode> groupNodeList= new ArrayList<>();
  22. private ArrayList<ExitCable> exitCableList= new ArrayList<>();
  23. //ForFastAccessIndividualGroupNodes:
  24. private HashMap<GroupNode, DecoratedGroupNode> createdGroupNodes;
  25. public HashMap<HolonObject, DecoratedHolonObject> createdHolonObjects;
  26. public VisualRepresentationalState(DecoratedState stateFromThisTimestep, MinimumModel minimumModel) {
  27. reassignObjects(stateFromThisTimestep, minimumModel);
  28. }
  29. //Getter:
  30. public ArrayList<Supplier> getSupplierList() {
  31. return supplierList;
  32. }
  33. public ArrayList<Passiv> getPassivList() {
  34. return passivList;
  35. }
  36. public ArrayList<Consumer> getConsumerList() {
  37. return consumerList;
  38. }
  39. public ArrayList<Node> getNodeList() {
  40. return nodeList;
  41. }
  42. public ArrayList<Edge> getCableList() {
  43. return cableList;
  44. }
  45. public ArrayList<DecoratedSwitch> getSwitchList() {
  46. return switchList;
  47. }
  48. public ArrayList<DecoratedGroupNode> getGroupNodeList() {
  49. return groupNodeList;
  50. }
  51. //Reassignments:
  52. private void reassignObjects(DecoratedState stateFromThisTimestep, MinimumModel minimumModel) {
  53. //generate CableLookUp
  54. HashMap<Edge, ArrayList<GroupNode>> inGroupEdges = minimumModel.getInGroupEdges();
  55. createdGroupNodes = new HashMap<GroupNode, DecoratedGroupNode>();
  56. createdHolonObjects = new HashMap<HolonObject, DecoratedHolonObject>();
  57. ArrayList<IntermediateCalculationCable> exitCables = new ArrayList<IntermediateCalculationCable>();
  58. //createThem
  59. for(GroupNode groupNode : minimumModel.getUppderNodeList()) {
  60. createdGroupNodes.put(groupNode, new DecoratedGroupNode(groupNode));
  61. }
  62. //unrolling Networks
  63. for(DecoratedNetwork net : stateFromThisTimestep.getNetworkList()) {
  64. for(Consumer con : net.getConsumerList()) {
  65. DecoratedGroupNode groupNodeFromObject = addObject(con.getModel(), consumerList, con);
  66. if(groupNodeFromObject != null) {
  67. addToGroupNode(con, groupNodeFromObject.getConsumerList());
  68. }
  69. this.createdHolonObjects.put(con.getModel(), con);
  70. }
  71. for(Consumer con : net.getConsumerSelfSuppliedList()) {
  72. DecoratedGroupNode groupNodeFromObject = addObject(con.getModel(), consumerList, con);
  73. if(groupNodeFromObject != null) {
  74. addToGroupNode(con, groupNodeFromObject.getConsumerList());
  75. }
  76. this.createdHolonObjects.put(con.getModel(), con);
  77. }
  78. for(Supplier sup : net.getSupplierList()) {
  79. DecoratedGroupNode groupNodeFromObject = addObject(sup.getModel(), supplierList, sup);
  80. if(groupNodeFromObject != null) {
  81. addToGroupNode(sup, groupNodeFromObject.getSupplierList());
  82. }
  83. this.createdHolonObjects.put(sup.getModel(), sup);
  84. }
  85. for(Passiv pas : net.getPassivNoEnergyList()) {
  86. DecoratedGroupNode groupNodeFromObject = addObject(pas.getModel(), passivList, pas);
  87. if(groupNodeFromObject != null) {
  88. addToGroupNode(pas, groupNodeFromObject.getPassivList());
  89. }
  90. this.createdHolonObjects.put(pas.getModel(), pas);
  91. }
  92. for(Edge cable : net.getDecoratedCableList()) {
  93. addCable(cable, inGroupEdges,createdGroupNodes, exitCables);
  94. }
  95. }
  96. for(Edge cable : stateFromThisTimestep.getLeftOverEdges()) {
  97. addCable(cable, inGroupEdges, createdGroupNodes, exitCables);
  98. }
  99. for(Node node : minimumModel.getNodeList()) {
  100. DecoratedGroupNode groupNodeFromObject = addObject(node, nodeList ,node);
  101. if(groupNodeFromObject != null) {
  102. addToGroupNode(node, groupNodeFromObject.getNodeList());
  103. }
  104. }
  105. for(DecoratedSwitch dSwitch: stateFromThisTimestep.getDecoratedSwitches()) {
  106. DecoratedGroupNode groupNodeFromObject = addObject(dSwitch.getModel(), switchList, dSwitch);
  107. if(groupNodeFromObject != null) {
  108. addToGroupNode(dSwitch, groupNodeFromObject.getSwitchList());
  109. }
  110. }
  111. for(DecoratedGroupNode dGroupNode: createdGroupNodes.values()) {
  112. DecoratedGroupNode groupNodeFromObject = addObject(dGroupNode.getModel(), groupNodeList, dGroupNode);
  113. if(groupNodeFromObject != null) {
  114. addToGroupNode(dGroupNode, groupNodeFromObject.getGroupNodeList());
  115. }
  116. }
  117. //Create TreeNodeModel:
  118. HashMap<GroupNode, TreeNode<TreeGroupNodeData>> fastaccess= new HashMap<GroupNode, TreeNode<TreeGroupNodeData>>();
  119. TreeNode<TreeGroupNodeData> root = new TreeNode<TreeGroupNodeData>(null, new ArrayList<TreeNode<TreeGroupNodeData>>(), new TreeGroupNodeData(null, 0));
  120. fastaccess.put(null, root);
  121. for(DecoratedGroupNode dGroupNode: getGroupNodeList()) {
  122. addTreeNode(root, dGroupNode, 1, fastaccess);
  123. }
  124. for(IntermediateCalculationCable cable : exitCables) {
  125. createExitEdgesV2(root,cable , cable.getInsideObject(),cable.getInsideUpperNode(),cable.getOusideObject(),cable.getOutsideUpperNode(), fastaccess);
  126. }
  127. }
  128. private void createExitEdgesV2(TreeNode<TreeGroupNodeData> root, IntermediateCalculationCable cable, AbstractCanvasObject insideObject,
  129. GroupNode insideUpperNode, AbstractCanvasObject ousideObject, GroupNode outsideUpperNode, HashMap<GroupNode, TreeNode<TreeGroupNodeData>> fastaccess) {
  130. //Create Up List
  131. LinkedList<TreeNode<TreeGroupNodeData>> listFromStart = createList(insideUpperNode, fastaccess);
  132. LinkedList<TreeNode<TreeGroupNodeData>> listFromEnd = createList(outsideUpperNode, fastaccess);
  133. LinkedList<TreeNode<TreeGroupNodeData>> common = new LinkedList<TreeNode<TreeGroupNodeData>>(listFromStart);
  134. common.retainAll(listFromEnd);
  135. TreeNode<TreeGroupNodeData> firstCommon = common.getFirst();
  136. LinkedList<TreeNode<TreeGroupNodeData>> resultList = new LinkedList<TreeNode<TreeGroupNodeData>>();
  137. //Add from listFromStart till firstCommon
  138. createresultList(listFromStart, firstCommon, resultList);
  139. //Add firstCommon
  140. resultList.add(firstCommon);
  141. //Add from listFromEnd till firstCommon
  142. createresultList(listFromEnd, firstCommon, resultList);
  143. LinkedList<NodeInfo> infoList = new LinkedList<NodeInfo>();
  144. //Categorize:
  145. ListIterator<TreeNode<TreeGroupNodeData>> iter = resultList.listIterator();
  146. while(iter.hasNext()) {
  147. //categorize
  148. TreeNode<TreeGroupNodeData> actual, next = null, previous = null;
  149. if(iter.hasPrevious()) {
  150. previous =iter.previous();
  151. iter.next();
  152. }
  153. actual = iter.next();
  154. if(iter.hasNext()) {
  155. next =iter.next();
  156. iter.previous();
  157. }
  158. NodeInfo actualInfo = new NodeInfo(actual.getData().groupNode);
  159. if(previous!= null) {
  160. actualInfo.previousGroupNode = previous.getData().groupNode;
  161. if(previous == actual.getParent()) {
  162. actualInfo.previous = Info.Parent;
  163. }else {
  164. actualInfo.previous = Info.Child;
  165. }
  166. }
  167. if(next!= null) {
  168. actualInfo.nextGroupNode = next.getData().groupNode;
  169. if(next == actual.getParent()) {
  170. actualInfo.next = Info.Parent;
  171. }else {
  172. actualInfo.next = Info.Child;
  173. }
  174. }
  175. infoList.add(actualInfo);
  176. }
  177. for(NodeInfo info: infoList) {
  178. DecoratedGroupNode group = this.createdGroupNodes.get(info.groupNode);
  179. ArrayList<ExitCable> mylist;
  180. if(group == null) {
  181. mylist = this.getExitCableList();
  182. }else{
  183. mylist = group.getExitCableList();
  184. }
  185. ExitCableState state =null;
  186. if(info.previous == Info.Nothing) {
  187. if(info.next == Info.Child) {
  188. state = ExitCableState.DOWN;
  189. mylist.add(new ExitCable(state, insideObject, info.nextGroupNode, cable.getEdge()));
  190. }else if(info.next == Info.Parent) {
  191. state = ExitCableState.UP;
  192. mylist.add(new ExitCable(state, insideObject, ousideObject, cable.getEdge()));
  193. }else {
  194. System.out.println("Error in VisualState");
  195. }
  196. }else if(info.previous == Info.Child) {
  197. if(info.next == Info.Child) {
  198. state = ExitCableState.DOWNDOWN;
  199. mylist.add(new ExitCable(state, info.previousGroupNode, info.nextGroupNode, cable.getEdge()));
  200. }else if(info.next == Info.Parent) {
  201. state = ExitCableState.DOWNUP;
  202. mylist.add(new ExitCable(state, info.previousGroupNode, ousideObject, cable.getEdge()));
  203. }else {
  204. state = ExitCableState.DOWN;
  205. mylist.add(new ExitCable(state, info.previousGroupNode, ousideObject, cable.getEdge()));
  206. }
  207. }else {//(info.previous == Info.Parent)
  208. if(info.next == Info.Child) {
  209. state = ExitCableState.DOWNUP;
  210. mylist.add(new ExitCable(state, info.nextGroupNode, insideObject, cable.getEdge()));
  211. }else if(info.next == Info.Parent) {
  212. System.out.println("Error in VisualState");
  213. }else {
  214. state = ExitCableState.UP;
  215. mylist.add(new ExitCable(state, ousideObject, insideObject, cable.getEdge()));
  216. }
  217. }
  218. }
  219. }
  220. private void createresultList(LinkedList<TreeNode<TreeGroupNodeData>> list,
  221. TreeNode<TreeGroupNodeData> firstCommon, LinkedList<TreeNode<TreeGroupNodeData>> resultList) {
  222. for(TreeNode<TreeGroupNodeData> node: list) {
  223. if(node == firstCommon) {
  224. break;
  225. }
  226. resultList.add(node);
  227. }
  228. }
  229. private LinkedList<TreeNode<TreeGroupNodeData>> createList(GroupNode insideUpperNode,
  230. HashMap<GroupNode, TreeNode<TreeGroupNodeData>> fastaccess) {
  231. System.out.println(insideUpperNode);
  232. TreeNode<TreeGroupNodeData> actualNode = fastaccess.get(insideUpperNode);
  233. LinkedList<TreeNode<TreeGroupNodeData>> list = new LinkedList<TreeNode<TreeGroupNodeData>>();
  234. list.add(actualNode);
  235. while(actualNode.getParent() != null) {
  236. actualNode = actualNode.getParent();
  237. list.add(actualNode);
  238. }
  239. return list;
  240. }
  241. private void addCable(Edge cable, HashMap<Edge, ArrayList<GroupNode>> inGroupEdges,
  242. HashMap<GroupNode, DecoratedGroupNode> createdGroupNodes, ArrayList<IntermediateCalculationCable> exitCables) {
  243. boolean isInGroup = false;
  244. if(cable.getA().isInGroupNode()) {
  245. isInGroup = true;
  246. }
  247. if(cable.getB().isInGroupNode()) {
  248. isInGroup = true;
  249. }
  250. if(isInGroup) {
  251. boolean isIntern = cable.getA().getGroupNode() == cable.getB().getGroupNode(); //Case null == null is not possible trough before Filtering MinimumModel#addUpperObjects(CpsUpperNode)
  252. if(isIntern) {
  253. DecoratedGroupNode groupNodeFromBoth = createdGroupNodes.get(cable.getA().getGroupNode());
  254. groupNodeFromBoth.getInternCableList().add(cable);
  255. }else {
  256. if(cable.getA().isInGroupNode()) {
  257. exitCables.add(new IntermediateCalculationCable(cable, cable.getA().getGroupNode(),cable.getB().getGroupNode(), cable.getA(), cable.getB()));
  258. } else if(cable.getB().isInGroupNode()) {
  259. exitCables.add(new IntermediateCalculationCable(cable, cable.getB().getGroupNode(),cable.getA().getGroupNode(), cable.getB(), cable.getA()));
  260. }
  261. }
  262. }else {
  263. cableList.add(cable);
  264. }
  265. }
  266. private <DecoratedObject> void addToGroupNode(DecoratedObject object, ArrayList<DecoratedObject> groupNodeListPar) {
  267. groupNodeListPar.add(object);
  268. }
  269. public int getAmountfOfGroupNodes() {
  270. return groupNodeList.stream().map(groupNode -> groupNode.getAmountOfGroupNodes()).reduce(0, Integer::sum);
  271. }
  272. public float getConsumptionFromConsumer() {
  273. return consumerList.stream().map(con -> con.getEnergyNeededFromNetwork()).reduce(0.f, Float::sum)+
  274. groupNodeList.stream().map(groupNode -> groupNode.getConsumptionFromConsumer()).reduce(0.f, Float::sum);
  275. }
  276. public float getProductionFromSupplier() {
  277. return supplierList.stream().map(sup -> sup.getEnergyToSupplyNetwork()).reduce(0.f, Float::sum)+
  278. groupNodeList.stream().map(groupNode -> groupNode.getProductionFromSupplier()).reduce(0.f, Float::sum);
  279. }
  280. public float getAverageConsumption() {
  281. return getConsumptionFromConsumer() / (float)getAmountfOfGroupNodes();
  282. }
  283. public float getAverageProduction() {
  284. return getProductionFromSupplier() / (float)getAmountfOfGroupNodes();
  285. }
  286. //Generics
  287. private <DecoratedObject> DecoratedGroupNode addObject(AbstractCanvasObject modelOfObject, ArrayList<DecoratedObject> listToAdd, DecoratedObject object) {
  288. if(modelOfObject.getGroupNode() != null) {
  289. return createdGroupNodes.get(modelOfObject.getGroupNode());
  290. }
  291. listToAdd.add(object);
  292. return null;
  293. }
  294. public enum Info{
  295. Nothing, Parent, Child
  296. }
  297. private class NodeInfo{
  298. public GroupNode groupNode;
  299. public Info previous = Info.Nothing;
  300. public Info next = Info.Nothing;
  301. public GroupNode previousGroupNode = null;
  302. public GroupNode nextGroupNode = null;
  303. public NodeInfo(GroupNode groupNode) {
  304. this.groupNode = groupNode;
  305. }
  306. public String toString() {
  307. return "Previuos: " + previous.toString() + "|Next: " + next.toString();
  308. }
  309. }
  310. public HashMap<GroupNode, DecoratedGroupNode> getCreatedGroupNodes() {
  311. return createdGroupNodes;
  312. }
  313. private class TreeNode<T> {
  314. private TreeNode<T> parentNode;
  315. private List<TreeNode<T>> children;
  316. private T data;
  317. public TreeNode( TreeNode<T> parentNode, List<TreeNode<T>> children, T data) {
  318. this.parentNode = parentNode;
  319. this.children = children;
  320. this.data = data;
  321. }
  322. //Methods
  323. public TreeNode<T> getParent(){
  324. return parentNode;
  325. }
  326. public List<TreeNode<T>> getChildren(){
  327. return children;
  328. }
  329. public T getData() {
  330. return data;
  331. }
  332. public String toString() {
  333. return "[" + data.toString() + " Children(" + children.stream().map(Object::toString).collect(Collectors.joining(", ")) + ")]";
  334. }
  335. }
  336. private class TreeGroupNodeData{
  337. public GroupNode groupNode;
  338. public int layer;
  339. public TreeGroupNodeData(GroupNode groupNode, int layer) {
  340. this.groupNode = groupNode;
  341. this.layer = layer;
  342. }
  343. public String toString() {
  344. return "Layer:" + layer;
  345. }
  346. }
  347. private void addTreeNode(TreeNode<TreeGroupNodeData> node, DecoratedGroupNode dGroupNode, int layer, HashMap<GroupNode, TreeNode<TreeGroupNodeData>> fastaccess) {
  348. TreeNode<TreeGroupNodeData> newNode = new TreeNode<TreeGroupNodeData> (node, new ArrayList<TreeNode<TreeGroupNodeData>>() , new TreeGroupNodeData(dGroupNode.getModel(), layer));
  349. node.getChildren().add(newNode);
  350. fastaccess.put(newNode.data.groupNode, newNode);
  351. for(DecoratedGroupNode dGroupNodeIntern: dGroupNode.getGroupNodeList()) {
  352. addTreeNode(newNode, dGroupNodeIntern, layer+1, fastaccess);
  353. }
  354. }
  355. public ArrayList<ExitCable> getExitCableList() {
  356. return exitCableList;
  357. }
  358. }