001: /*
002: * This file or a portion of this file is licensed under the terms of
003: * the Globus Toolkit Public License, found in file GTPL, or at
004: * http://www.globus.org/toolkit/download/license.html. This notice must
005: * appear in redistributions of this file, with or without modification.
006: *
007: * Redistributions of this Software, with or without modification, must
008: * reproduce the GTPL in: (1) the Software, or (2) the Documentation or
009: * some other similar material which is provided with the Software (if
010: * any).
011: *
012: * Copyright 1999-2004 University of Chicago and The University of
013: * Southern California. All rights reserved.
014: */
015: package org.griphyn.cPlanner.partitioner;
016:
017: import org.griphyn.cPlanner.classes.Data;
018:
019: import java.util.Iterator;
020: import java.util.List;
021:
022: /**
023: * Data class that allows us to construct information about the nodes
024: * in the abstract graph. Contains for each node the references to it's
025: * parents and children.
026: *
027: * @author Karan Vahi
028: * @version $Revision: 50 $
029: */
030: public class GraphNode extends Data {
031:
032: //the constants for the color of the nodes
033: public static final int WHITE_COLOR = 0;
034: public static final int GRAY_COLOR = 1;
035: public static final int BLACK_COLOR = 2;
036:
037: /**
038: * The logical id of the job as identified in the dax.
039: */
040: private String mLogicalID;
041:
042: /**
043: * The logical name of the node as identified in the dax.
044: */
045: private String mLogicalName;
046:
047: /**
048: * The depth of the node from the root or any arbitary node.
049: */
050: private int mDepth;
051:
052: /**
053: * The color the node is colored.
054: */
055: private int mColor;
056:
057: /**
058: * The list of parents of the job/node in the abstract graph. Each element
059: * of the list is a <code>GraphNode</code> object.
060: */
061: private List mParents;
062:
063: /**
064: * The list of children of the job/node in the abstract graph. Each element
065: * of the list is a <code>GraphNode</code> object.
066: */
067: private List mChildren;
068:
069: /**
070: * A Bag of objects that maybe associated with the node.
071: *
072: * @see Bag
073: */
074: private Bag mBag;
075:
076: /**
077: * The default constructor.
078: */
079: public GraphNode() {
080: mLogicalID = new String();
081: mParents = new java.util.LinkedList();
082: mChildren = null;
083: mDepth = -1;
084: mLogicalName = new String();
085: mColor = this .WHITE_COLOR;
086: mBag = null;
087: }
088:
089: /**
090: * The overloaded constructor.
091: *
092: * @param id the logical id of the node.
093: * @param name the name of the node.
094: */
095: public GraphNode(String id, String name) {
096: mLogicalID = id;
097: mParents = new java.util.LinkedList();
098: mChildren = new java.util.LinkedList();
099: mDepth = -1;
100: mLogicalName = name;
101: mColor = this .WHITE_COLOR;
102: }
103:
104: /**
105: * Sets the bag of objects associated with the node.
106: */
107: public void setBag(Bag bag) {
108: mBag = bag;
109: }
110:
111: /**
112: * It adds the parents to the node. It ends up overwriting all the existing
113: * parents if some already exist.
114: */
115: public void setParents(List parents) {
116: mParents = parents;
117: }
118:
119: /**
120: * It sets the children to the node. It ends up overwriting all the existing
121: * parents if some already exist.
122: */
123: public void setChildren(List children) {
124: mChildren = children;
125: }
126:
127: /**
128: * Sets the depth associated with the node.
129: */
130: public void setDepth(int depth) {
131: mDepth = depth;
132: }
133:
134: /**
135: * Returns the bag of objects associated with the node.
136: *
137: * @return the bag or null if no bag associated
138: */
139: public Bag getBag() {
140: return mBag;
141: }
142:
143: /**
144: * Returns a reference to the parents of the node.
145: */
146: public List getParents() {
147: return mParents;
148: }
149:
150: /**
151: * Returns a reference to the parents of the node.
152: */
153: public List getChildren() {
154: return mChildren;
155: }
156:
157: /**
158: * Adds a child to end of the child list.
159: */
160: public void addChild(GraphNode child) {
161: mChildren.add(child);
162: }
163:
164: /**
165: * Returns the logical id of the graph node.
166: */
167: public String getID() {
168: return mLogicalID;
169: }
170:
171: /**
172: * Returns the logical name of the graph node.
173: */
174: public String getName() {
175: return mLogicalName;
176: }
177:
178: /**
179: * Returns the depth of the node in the graph.
180: */
181: public int getDepth() {
182: return mDepth;
183: }
184:
185: /**
186: * Returns if the color of the node is as specified.
187: *
188: * @param color color that node should be.
189: */
190: public boolean isColor(int color) {
191: return (mColor == color) ? true : false;
192: }
193:
194: /**
195: * Sets the color of the node to the color specified
196: *
197: * @param color color that node should be.
198: */
199: public void setColor(int color) {
200: mColor = color;
201: }
202:
203: /**
204: * Returns if all the parents of that node have the color that is specified.
205: *
206: * @param color the color of the node.
207: *
208: * @return true if there are no parents or all parents are of the color.
209: * false in all other cases.
210: */
211: public boolean parentsColored(int color) {
212: boolean colored = true;
213: GraphNode par;
214: if (mParents == null) {
215: return colored;
216: }
217:
218: Iterator it = mParents.iterator();
219: while (it.hasNext() && colored) {
220: par = (GraphNode) it.next();
221: colored = par.isColor(color);
222: }
223:
224: return colored;
225: }
226:
227: /**
228: * The textual representation of the graph node.
229: */
230: public String toString() {
231: StringBuffer sb = new StringBuffer();
232: Iterator it;
233:
234: sb.append("ID->").append(mLogicalID).append(" name->").append(
235: mLogicalName).append(" parents->{");
236: if (mParents != null) {
237: it = mParents.iterator();
238: while (it.hasNext()) {
239: sb.append(((GraphNode) it.next()).getID()).append(',');
240: }
241:
242: }
243: sb.append("} children->{");
244: it = mChildren.iterator();
245: while (it.hasNext()) {
246: sb.append(((GraphNode) it.next()).getID()).append(',');
247: }
248: sb.append("}");
249: sb.append(" Bag-{").append(getBag()).append("}");
250: return sb.toString();
251: }
252:
253: /**
254: * Returns a copy of the object.
255: */
256: public Object clone() {
257: return new java.lang.CloneNotSupportedException(
258: "Clone() not implemented in GraphNode");
259: }
260: }
|