| 1 | package de.uka.ipd.sdq.tcfmoop.outputtree; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.List; |
| 5 | |
| 6 | /** |
| 7 | * Defines a Node of a tree. |
| 8 | * @author Atanas Dimitrov |
| 9 | */ |
| 10 | public class Node{ |
| 11 | |
| 12 | public enum NodeType{NON_SPECIFIC, MANAGER, TERMINATION_CRITERIA, PARAMETER, PARAMETER_GROUP, EXPRESSION, WARNING}; |
| 13 | |
| 14 | private List<Node> children = new ArrayList<Node>(); |
| 15 | protected Node parent; |
| 16 | private String value; |
| 17 | public NodeType type; |
| 18 | |
| 19 | /** |
| 20 | * Creates a new node |
| 21 | * @param parent The parent of the current node. |
| 22 | * @param value The information that should be saved in the current node |
| 23 | * @param type the type of the node. Can later be used by the representation in the GUI |
| 24 | */ |
| 25 | protected Node(Node parent, String value, NodeType type){ |
| 26 | this.parent = parent; |
| 27 | this.value = value; |
| 28 | this.type = type; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Returns whether the current node has any children nodes |
| 33 | * @return true if the node has children, false otherwise |
| 34 | */ |
| 35 | public boolean hasChildren(){ |
| 36 | return !children.isEmpty(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Returns a list with the children of the node. The list might be empty if |
| 41 | * the node does not have any children. |
| 42 | * @return a list with all children. |
| 43 | */ |
| 44 | public List<Node> getChildren(){ |
| 45 | return this.children; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Returns whether the current node is a root of a tree. |
| 50 | * @return true if the node is root, false otherwise. |
| 51 | */ |
| 52 | public boolean isRoot(){ |
| 53 | return parent == null; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns the parent node of the current node. |
| 58 | * Null might be returned if the node is a root node |
| 59 | * @return the parent of the current node or null |
| 60 | */ |
| 61 | public Node getParent(){ |
| 62 | return this.parent; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns the information saved in the current node. |
| 67 | * @return the information saved in the current string |
| 68 | */ |
| 69 | public String getValue(){ |
| 70 | return this.value; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Add a children node to the current node. |
| 75 | * @param value the information that will be stored in the child node |
| 76 | * @param type the type of the child node |
| 77 | * @return the child node |
| 78 | */ |
| 79 | public Node addChild(String value, NodeType type){ |
| 80 | Node newNode = new Node(this, value, type); |
| 81 | children.add(newNode); |
| 82 | return newNode; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Clear the list of child nodes of the current node. |
| 87 | */ |
| 88 | public void clearChildren(){ |
| 89 | this.children.clear(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Attach a sub tree to the current node |
| 94 | * @param tree the sub tree to be attached |
| 95 | */ |
| 96 | public void attachSubtree(Tree tree){ |
| 97 | this.children.add(tree); |
| 98 | tree.parent = this; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Update the information saved in the current node |
| 103 | * @param newValue the new information to be saved in the node |
| 104 | */ |
| 105 | public void updateValue(String newValue){ |
| 106 | this.value = newValue; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns the type of the node |
| 111 | * @return the type of the node |
| 112 | */ |
| 113 | public NodeType getType(){ |
| 114 | return this.type; |
| 115 | } |
| 116 | |
| 117 | /**] |
| 118 | * {@inheritDoc} |
| 119 | */ |
| 120 | @Override |
| 121 | public String toString() { |
| 122 | return value; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Builds a tree like String representation of the current node and its |
| 127 | * entire subtree by using a tree prefix to simulate tree levels |
| 128 | * @param treePrefix the prefix to use |
| 129 | * @param prefixOccurance occurrence of the prefix on level 0 (root level). Usually 0 |
| 130 | * @return the tree like String representation of the node and its subtree |
| 131 | */ |
| 132 | public String subTreeToString(String treePrefix, int prefixOccurance){ |
| 133 | |
| 134 | StringBuilder prefix = new StringBuilder(); |
| 135 | for(int i = 0; i < prefixOccurance; i++){ |
| 136 | prefix.append(treePrefix); |
| 137 | } |
| 138 | |
| 139 | String eol = System.getProperty( "line.separator" ); |
| 140 | |
| 141 | if(this.hasChildren()){ |
| 142 | StringBuilder sb = new StringBuilder(prefix.toString() + this.toString() + eol); |
| 143 | for(Node n : this.getChildren()){ |
| 144 | sb.append(n.subTreeToString(treePrefix, prefixOccurance + 1)); |
| 145 | } |
| 146 | return sb.toString(); |
| 147 | }else{ |
| 148 | return prefix.toString() + this.toString() + eol; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | } |