GroupNode.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package holeg.model;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.List;
  5. import java.util.Set;
  6. import java.util.logging.Logger;
  7. import java.util.stream.Collectors;
  8. import java.util.stream.Stream;
  9. import holeg.preferences.ImagePreference;
  10. import holeg.serialize.PostDeserialize;
  11. import holeg.ui.model.GuiSettings;
  12. import holeg.utility.math.vector.Vec2i;
  13. public class GroupNode extends AbstractCanvasObject implements PostDeserialize {
  14. private static final Logger log = Logger.getLogger(AbstractCanvasObject.class.getName());
  15. protected final List<HolonObject> objectList = new ArrayList<>();
  16. protected final List<HolonSwitch> switchList = new ArrayList<>();
  17. protected final List<Node> nodeList = new ArrayList<>();
  18. protected final List<GroupNode> groupNodeList = new ArrayList<>();
  19. public GroupNode(String nodeName) {
  20. super(nodeName);
  21. }
  22. public GroupNode(GroupNode other){
  23. super(other.name);
  24. for (HolonObject obj : other.objectList) {
  25. this.add(new HolonObject(obj));
  26. }
  27. for (HolonSwitch sw : other.switchList) {
  28. this.add(new HolonSwitch(sw));
  29. }
  30. for (Node node : other.nodeList) {
  31. this.add(new Node(node));
  32. }
  33. for (GroupNode groupNode : other.groupNodeList) {
  34. this.add(new GroupNode(groupNode));
  35. }
  36. }
  37. @Override
  38. public AbstractCanvasObject copy() {
  39. return new GroupNode(this);
  40. }
  41. @Override
  42. public String getImagePath() {
  43. return ImagePreference.Canvas.GroupNode;
  44. }
  45. public void addAll(Stream<AbstractCanvasObject> stream) {
  46. stream.forEach(this::add);
  47. }
  48. public void addAll(Collection<AbstractCanvasObject> collection) {
  49. collection.forEach(this::add);
  50. }
  51. public void add(AbstractCanvasObject object) {
  52. if (object instanceof HolonObject hObject) {
  53. objectList.add(hObject);
  54. }else if(object instanceof HolonSwitch hSwitch) {
  55. switchList.add(hSwitch);
  56. }else if(object instanceof Node node) {
  57. nodeList.add(node);
  58. }else if(object instanceof GroupNode groupNode) {
  59. groupNodeList.add(groupNode);
  60. }
  61. object.setGroupNode(this);
  62. }
  63. public void remove(AbstractCanvasObject object) {
  64. if (object instanceof HolonObject hObject) {
  65. objectList.remove(hObject);
  66. }else if(object instanceof HolonSwitch hSwitch) {
  67. switchList.remove(hSwitch);
  68. }else if(object instanceof Node node) {
  69. nodeList.remove(node);
  70. }else if(object instanceof GroupNode groupNode) {
  71. groupNodeList.remove(groupNode);
  72. }
  73. object.setGroupNode(null);
  74. }
  75. public void removeAll(Stream<AbstractCanvasObject> stream){
  76. stream.forEach(this::remove);
  77. }
  78. public void removeAll(Collection<AbstractCanvasObject> collection){
  79. collection.forEach(this::remove);
  80. }
  81. public Stream<AbstractCanvasObject> getObjectsInThisLayer() {
  82. return Stream.of(objectList.stream(), switchList.stream(), nodeList.stream(), groupNodeList.stream())
  83. .flatMap(s -> s);
  84. }
  85. public Stream<AbstractCanvasObject> getAllObjectsRecursive() {
  86. Stream<AbstractCanvasObject> objects = Stream.of(objectList.stream(), switchList.stream(), nodeList.stream())
  87. .flatMap(s -> s);
  88. return Stream.concat(objects,
  89. groupNodeList.stream().flatMap(GroupNode::getObjectsInThisLayer));
  90. }
  91. public Stream<HolonObject> getAllHolonObjectsRecursive() {
  92. return Stream.concat(objectList.stream(),
  93. groupNodeList.stream().flatMap(GroupNode::getAllHolonObjectsRecursive));
  94. }
  95. public Stream<HolonSwitch> getAllSwitchObjectsRecursive() {
  96. return Stream.concat(switchList.stream(),
  97. groupNodeList.stream().flatMap(GroupNode::getAllSwitchObjectsRecursive));
  98. }
  99. public Stream<Node> getAllNodeObjectsRecursive() {
  100. return Stream.concat(nodeList.stream(),
  101. groupNodeList.stream().flatMap(GroupNode::getAllNodeObjectsRecursive));
  102. }
  103. public Stream<GroupNode> getAllGroupNodeObjectsRecursive() {
  104. return Stream.concat(groupNodeList.stream(),
  105. groupNodeList.stream().flatMap(GroupNode::getAllGroupNodeObjectsRecursive));
  106. }
  107. public Stream<HolonObject> getHolonObjects() {
  108. return objectList.stream();
  109. }
  110. public Stream<HolonSwitch> getSwitches() {
  111. return switchList.stream();
  112. }
  113. public Stream<Node> getNodes() {
  114. return nodeList.stream();
  115. }
  116. public Stream<GroupNode> getGroupNodes() {
  117. return groupNodeList.stream();
  118. }
  119. public Stream<HolonElement> getHolonElements(){
  120. return objectList.stream().flatMap(HolonObject::elementsStream);
  121. }
  122. public Stream<HolonElement> getAllHolonElements(){
  123. return Stream.concat(getHolonElements(),
  124. groupNodeList.stream().flatMap(GroupNode::getAllHolonElements));
  125. }
  126. public Stream<Flexibility> getAllFlexibilities() {
  127. return getAllHolonObjectsRecursive().flatMap(hO -> hO.elementsStream().flatMap(ele -> ele.flexList.stream()));
  128. }
  129. public void ungroup(){
  130. getObjectsInThisLayer().forEach(obj -> {
  131. obj.setGroupNode(null);
  132. });
  133. getGroupNode().ifPresent(parent -> {
  134. parent.addAll(getObjectsInThisLayer());
  135. parent.remove(this);
  136. });
  137. clear();
  138. }
  139. public static Vec2i calculateMiddlePosition(Collection<AbstractCanvasObject> objects){
  140. Vec2i middle = new Vec2i();
  141. if(!objects.isEmpty()){
  142. objects.forEach(obj -> middle.addAssign(obj.getPosition()));
  143. middle.divideAssign(objects.size());
  144. }
  145. return middle;
  146. }
  147. public float getTotalConsumption() {
  148. return getAllHolonObjectsRecursive().map(HolonObject::getConsumption).reduce(0.f, Float::sum);
  149. }
  150. public float getTotalProduction() {
  151. return getAllHolonObjectsRecursive().map(HolonObject::getProduction).reduce(0.f, Float::sum);
  152. }
  153. public void clear() {
  154. objectList.clear();
  155. switchList.clear();
  156. nodeList.clear();
  157. groupNodeList.clear();
  158. }
  159. @Override
  160. public void postDeserialize() {
  161. getObjectsInThisLayer().forEach(obj -> obj.setGroupNode(this));
  162. }
  163. @Override
  164. public String toString(){
  165. return "" + objectList + switchList + nodeList +groupNodeList ;
  166. }
  167. }