GroupNode.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package classes;
  2. import com.google.gson.annotations.Expose;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.UUID;
  7. import java.util.stream.Collectors;
  8. public class GroupNode extends AbstractCanvasObject {
  9. private ArrayList<AbstractCanvasObject> nodes;
  10. private HashMap<Integer, Integer> nodesIdx;
  11. private String uniqueID;
  12. @Expose
  13. private int leftBorder; //TODO Delete leftBorder, jet is just set to 0
  14. @Expose
  15. private String imgPath = "";
  16. @Expose
  17. private int backgroundMode = 0;
  18. @Expose
  19. private int backgroundWidth = 0;
  20. @Expose
  21. private int backgroundHeight = 0;
  22. public GroupNode(String nodeName) {
  23. super(nodeName);
  24. this.setConnections(new ArrayList<>());
  25. this.setImage("/Images/upper_node.png");
  26. this.setSav("CVS");
  27. this.setId(IdCounter.nextId());
  28. this.setNodes(new ArrayList<>());
  29. this.setNodesIdx(new HashMap<>());
  30. this.uniqueID = "Group Node " + UUID.randomUUID().toString();
  31. }
  32. /**
  33. * @return the nodes
  34. */
  35. public ArrayList<AbstractCanvasObject> getNodes() {
  36. return nodes;
  37. }
  38. /**
  39. * @param nodes
  40. * the nodes to set
  41. */
  42. public void setNodes(ArrayList<AbstractCanvasObject> nodes) {
  43. this.nodes = nodes;
  44. }
  45. /**
  46. * @return the nodesIdx
  47. */
  48. public HashMap<Integer, Integer> getNodesIdx() {
  49. return nodesIdx;
  50. }
  51. /**
  52. * @param nodesIdx
  53. * the nodesIdx to set
  54. */
  55. public void setNodesIdx(HashMap<Integer, Integer> nodesIdx) {
  56. this.nodesIdx = nodesIdx;
  57. }
  58. public ArrayList<AbstractCanvasObject> getNodesAndGroupnodeNodes(){
  59. ArrayList<AbstractCanvasObject> nodes = new ArrayList<AbstractCanvasObject>();
  60. nodes.addAll(getNodes());
  61. for (AbstractCanvasObject temp : getNodes()) {
  62. if(temp instanceof GroupNode) {
  63. nodes.addAll(((GroupNode) temp).getNodesAndGroupnodeNodes());
  64. }
  65. }
  66. return nodes;
  67. }
  68. public ArrayList<HolonObject> getNumHolonObj() {
  69. ArrayList<HolonObject> onlyHolonObj = new ArrayList<HolonObject>();
  70. for (AbstractCanvasObject temp : getNodes()) {
  71. if (temp instanceof HolonObject) {
  72. onlyHolonObj.add((HolonObject) temp);
  73. }
  74. if(temp instanceof GroupNode) {
  75. onlyHolonObj.addAll(((GroupNode) temp).getNumHolonObj());
  76. }
  77. }
  78. return onlyHolonObj;
  79. }
  80. public ArrayList<HolonSwitch> getNumSwitches() {
  81. ArrayList<HolonSwitch> onlySwitsches = new ArrayList<HolonSwitch>();
  82. for (AbstractCanvasObject temp : getNodes()) {
  83. if (temp instanceof HolonSwitch) {
  84. onlySwitsches.add((HolonSwitch) temp);
  85. }
  86. if(temp instanceof GroupNode) {
  87. onlySwitsches.addAll(((GroupNode) temp).getNumSwitches());
  88. }
  89. }
  90. return onlySwitsches;
  91. }
  92. public ArrayList<GroupNode> getNumUpperNodes() {
  93. ArrayList<GroupNode> onlyUpperNodes = new ArrayList<GroupNode>();
  94. for (AbstractCanvasObject temp : getNodes()) {
  95. if (temp instanceof GroupNode) {
  96. onlyUpperNodes.add((GroupNode) temp);
  97. onlyUpperNodes.addAll(((GroupNode) temp).getNumUpperNodes());
  98. }
  99. }
  100. return onlyUpperNodes;
  101. }
  102. public AbstractCanvasObject searchObj(int ID) {
  103. AbstractCanvasObject result = null;
  104. for (AbstractCanvasObject obj : getNodes()) {
  105. if (obj.getId() == ID) {
  106. result = obj;
  107. break;
  108. }
  109. }
  110. return result;
  111. }
  112. /**
  113. * Set the Background Image;
  114. *
  115. * @param imagePath
  116. * Image Path
  117. * @param mode
  118. * Image Mode
  119. * @param width
  120. * Image custom width
  121. * @param height
  122. * Image custom height
  123. */
  124. public void setBackgroundImage(String imagePath, int mode, int width, int height) {
  125. imgPath = imagePath;
  126. backgroundMode = mode;
  127. backgroundWidth = width;
  128. backgroundHeight = height;
  129. }
  130. /**
  131. * Get the Background Image Path.
  132. *
  133. * @return imgPath Path of the Image
  134. */
  135. public String getImagePath() {
  136. return imgPath;
  137. }
  138. /**
  139. * Get the Background mode.
  140. *
  141. * @return mode the mode
  142. */
  143. public int getBackgroundMode() {
  144. return backgroundMode;
  145. }
  146. /**
  147. * Get the Background image Width.
  148. *
  149. * @return mode the width of the Image
  150. */
  151. public int getImageWidht() {
  152. return backgroundWidth;
  153. }
  154. /**
  155. * Get the Background image Height.
  156. *
  157. * @return mode the mode
  158. */
  159. public int getImageHeight() {
  160. return backgroundHeight;
  161. }
  162. /**
  163. * @return the leftBorder
  164. */
  165. public int getLeftBorder() {
  166. return leftBorder;
  167. }
  168. /**
  169. * @param leftBorder the leftBorder to set
  170. */
  171. public void setLeftBorder(int leftBorder) {
  172. this.leftBorder = leftBorder;
  173. }
  174. @Override
  175. public AbstractCanvasObject makeCopy() {
  176. return this;
  177. }
  178. public String getUniqueID() {
  179. if(this.uniqueID == null)
  180. this.uniqueID = "Group Node "+UUID.randomUUID().toString();
  181. return uniqueID;
  182. }
  183. public ArrayList<String> getAllConnectedHolons(ArrayList<AbstractCanvasObject> visited) {
  184. ArrayList<String> conns = new ArrayList<String>();
  185. visited.add(this);
  186. for(Edge e : this.connections) {
  187. AbstractCanvasObject a = e.getA();
  188. AbstractCanvasObject b = e.getB();
  189. if(b.equals(this) && !visited.contains(a)) {
  190. if(a instanceof HolonObject) {
  191. conns.add(((HolonObject) a).holon.getUniqueID());
  192. } else if(a instanceof HolonSwitch) {
  193. conns.addAll(((HolonSwitch)a).getAllConnectedHolons(visited));
  194. } else if(a instanceof Node) {
  195. conns.addAll(((Node) a).getAllConnectedHolons(visited));
  196. } else if(a instanceof GroupNode) {
  197. conns.addAll(((GroupNode) a).getAllConnectedHolons(visited));
  198. }
  199. visited.add(a);
  200. } else if(a.equals(this) && !visited.contains(b)) {
  201. if(b instanceof HolonObject) {
  202. conns.add(((HolonObject) b).holon.getUniqueID());
  203. } else if(b instanceof HolonSwitch) {
  204. conns.addAll(((HolonSwitch)b).getAllConnectedHolons(visited));
  205. } else if(b instanceof Node) {
  206. conns.addAll(((Node) b).getAllConnectedHolons(visited));
  207. } else if(b instanceof GroupNode) {
  208. conns.addAll(((GroupNode) b).getAllConnectedHolons(visited));
  209. }
  210. visited.add(b);
  211. }
  212. }
  213. for(AbstractCanvasObject a : this.nodes) {
  214. if(a instanceof HolonObject) {
  215. conns.add(((HolonObject) a).holon.getUniqueID());
  216. } else if(a instanceof HolonSwitch) {
  217. conns.addAll(((HolonSwitch)a).getAllConnectedHolons(visited));
  218. } else if(a instanceof Node) {
  219. conns.addAll(((Node) a).getAllConnectedHolons(visited));
  220. } else if(a instanceof GroupNode) {
  221. conns.addAll(((GroupNode) a).getAllConnectedHolons(visited));
  222. }
  223. visited.add(a);
  224. }
  225. return conns;
  226. }
  227. public List<AbstractCanvasObject> getAllObjectsInside(){
  228. return this.nodes.stream()
  229. .map(aco -> aco instanceof GroupNode ? ((GroupNode) aco).getAllObjectsInside() : List.of(aco))
  230. .flatMap(List::stream)
  231. .collect(Collectors.toList());
  232. }
  233. }