VisualRepresentationalState.java 15 KB

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