123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- 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<AbstractCanvasObject> nodes;
- private HashMap<Integer, Integer> 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<AbstractCanvasObject> getNodes() {
- return nodes;
- }
- /**
- * @param nodes
- * the nodes to set
- */
- public void setNodes(ArrayList<AbstractCanvasObject> nodes) {
- this.nodes = nodes;
- }
- /**
- * @return the nodesIdx
- */
- public HashMap<Integer, Integer> getNodesIdx() {
- return nodesIdx;
- }
- /**
- * @param nodesIdx
- * the nodesIdx to set
- */
- public void setNodesIdx(HashMap<Integer, Integer> nodesIdx) {
- this.nodesIdx = nodesIdx;
- }
- public ArrayList<AbstractCanvasObject> getNodesAndGroupnodeNodes(){
- ArrayList<AbstractCanvasObject> nodes = new ArrayList<AbstractCanvasObject>();
- nodes.addAll(getNodes());
- for (AbstractCanvasObject temp : getNodes()) {
- if(temp instanceof GroupNode) {
- nodes.addAll(((GroupNode) temp).getNodesAndGroupnodeNodes());
- }
- }
- return nodes;
- }
-
-
-
- public ArrayList<HolonObject> getNumHolonObj() {
- ArrayList<HolonObject> onlyHolonObj = new ArrayList<HolonObject>();
- 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<HolonSwitch> getNumSwitches() {
- ArrayList<HolonSwitch> onlySwitsches = new ArrayList<HolonSwitch>();
- 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<GroupNode> getNumUpperNodes() {
- ArrayList<GroupNode> onlyUpperNodes = new ArrayList<GroupNode>();
- 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<String> getAllConnectedHolons(ArrayList<AbstractCanvasObject> visited) {
- ArrayList<String> conns = new ArrayList<String>();
-
- 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<AbstractCanvasObject> getAllObjectsInside(){
- return this.nodes.stream()
- .map(aco -> aco instanceof GroupNode ? ((GroupNode) aco).getAllObjectsInside() : List.of(aco))
- .flatMap(List::stream)
- .collect(Collectors.toList());
- }
- }
|