package classes; import com.google.gson.annotations.Expose; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.UUID; import java.util.stream.Collectors; public class GroupNode extends AbstractCanvasObject { private ArrayList nodes; private HashMap nodesIdx; private String uniqueID; @Expose private int leftBorder; //TODO Delete leftBorder, jet is just set to 0 @Expose private String imgPath = ""; @Expose private int backgroundMode = 0; @Expose private int backgroundWidth = 0; @Expose private int backgroundHeight = 0; public GroupNode(String nodeName) { super(nodeName); this.setConnections(new ArrayList<>()); this.setImage("/Images/upper_node.png"); this.setSav("CVS"); this.setId(IdCounter.nextId()); this.setNodes(new ArrayList<>()); this.setNodesIdx(new HashMap<>()); this.uniqueID = "Group Node " + UUID.randomUUID().toString(); } /** * @return the nodes */ public ArrayList getNodes() { return nodes; } /** * @param nodes * the nodes to set */ public void setNodes(ArrayList nodes) { this.nodes = nodes; } /** * @return the nodesIdx */ public HashMap getNodesIdx() { return nodesIdx; } /** * @param nodesIdx * the nodesIdx to set */ public void setNodesIdx(HashMap nodesIdx) { this.nodesIdx = nodesIdx; } public ArrayList getNodesAndGroupnodeNodes(){ ArrayList nodes = new ArrayList(); nodes.addAll(getNodes()); for (AbstractCanvasObject temp : getNodes()) { if(temp instanceof GroupNode) { nodes.addAll(((GroupNode) temp).getNodesAndGroupnodeNodes()); } } return nodes; } public ArrayList getNumHolonObj() { ArrayList onlyHolonObj = new ArrayList(); for (AbstractCanvasObject temp : getNodes()) { if (temp instanceof HolonObject) { onlyHolonObj.add((HolonObject) temp); } if(temp instanceof GroupNode) { onlyHolonObj.addAll(((GroupNode) temp).getNumHolonObj()); } } return onlyHolonObj; } public ArrayList getNumSwitches() { ArrayList onlySwitsches = new ArrayList(); for (AbstractCanvasObject temp : getNodes()) { if (temp instanceof HolonSwitch) { onlySwitsches.add((HolonSwitch) temp); } if(temp instanceof GroupNode) { onlySwitsches.addAll(((GroupNode) temp).getNumSwitches()); } } return onlySwitsches; } public ArrayList getNumUpperNodes() { ArrayList onlyUpperNodes = new ArrayList(); for (AbstractCanvasObject temp : getNodes()) { if (temp instanceof GroupNode) { onlyUpperNodes.add((GroupNode) temp); onlyUpperNodes.addAll(((GroupNode) temp).getNumUpperNodes()); } } return onlyUpperNodes; } public AbstractCanvasObject searchObj(int ID) { AbstractCanvasObject result = null; for (AbstractCanvasObject obj : getNodes()) { if (obj.getId() == ID) { result = obj; break; } } return result; } /** * Set the Background Image; * * @param imagePath * Image Path * @param mode * Image Mode * @param width * Image custom width * @param height * Image custom height */ public void setBackgroundImage(String imagePath, int mode, int width, int height) { imgPath = imagePath; backgroundMode = mode; backgroundWidth = width; backgroundHeight = height; } /** * Get the Background Image Path. * * @return imgPath Path of the Image */ public String getImagePath() { return imgPath; } /** * Get the Background mode. * * @return mode the mode */ public int getBackgroundMode() { return backgroundMode; } /** * Get the Background image Width. * * @return mode the width of the Image */ public int getImageWidht() { return backgroundWidth; } /** * Get the Background image Height. * * @return mode the mode */ public int getImageHeight() { return backgroundHeight; } /** * @return the leftBorder */ public int getLeftBorder() { return leftBorder; } /** * @param leftBorder the leftBorder to set */ public void setLeftBorder(int leftBorder) { this.leftBorder = leftBorder; } @Override public AbstractCanvasObject makeCopy() { return this; } public String getUniqueID() { if(this.uniqueID == null) this.uniqueID = "Group Node "+UUID.randomUUID().toString(); return uniqueID; } public ArrayList getAllConnectedHolons(ArrayList visited) { ArrayList conns = new ArrayList(); visited.add(this); for(Edge e : this.connections) { AbstractCanvasObject a = e.getA(); AbstractCanvasObject b = e.getB(); if(b.equals(this) && !visited.contains(a)) { if(a instanceof HolonObject) { conns.add(((HolonObject) a).holon.getUniqueID()); } else if(a instanceof HolonSwitch) { conns.addAll(((HolonSwitch)a).getAllConnectedHolons(visited)); } else if(a instanceof Node) { conns.addAll(((Node) a).getAllConnectedHolons(visited)); } else if(a instanceof GroupNode) { conns.addAll(((GroupNode) a).getAllConnectedHolons(visited)); } visited.add(a); } else if(a.equals(this) && !visited.contains(b)) { if(b instanceof HolonObject) { conns.add(((HolonObject) b).holon.getUniqueID()); } else if(b instanceof HolonSwitch) { conns.addAll(((HolonSwitch)b).getAllConnectedHolons(visited)); } else if(b instanceof Node) { conns.addAll(((Node) b).getAllConnectedHolons(visited)); } else if(b instanceof GroupNode) { conns.addAll(((GroupNode) b).getAllConnectedHolons(visited)); } visited.add(b); } } for(AbstractCanvasObject a : this.nodes) { if(a instanceof HolonObject) { conns.add(((HolonObject) a).holon.getUniqueID()); } else if(a instanceof HolonSwitch) { conns.addAll(((HolonSwitch)a).getAllConnectedHolons(visited)); } else if(a instanceof Node) { conns.addAll(((Node) a).getAllConnectedHolons(visited)); } else if(a instanceof GroupNode) { conns.addAll(((GroupNode) a).getAllConnectedHolons(visited)); } visited.add(a); } return conns; } public List getAllObjectsInside(){ return this.nodes.stream() .map(aco -> aco instanceof GroupNode ? ((GroupNode) aco).getAllObjectsInside() : List.of(aco)) .flatMap(List::stream) .collect(Collectors.toList()); } }