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:
016: package org.griphyn.cPlanner.parser.dax;
017:
018: import org.griphyn.cPlanner.classes.ADag;
019: import org.griphyn.cPlanner.classes.DagInfo;
020: import org.griphyn.cPlanner.classes.PCRelation;
021: import org.griphyn.cPlanner.classes.PegasusFile;
022: import org.griphyn.cPlanner.classes.SubInfo;
023:
024: import org.griphyn.cPlanner.common.LogManager;
025: import org.griphyn.cPlanner.common.PegasusProperties;
026:
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.Map;
031: import java.util.Vector;
032:
033: /**
034: * This creates a dag corresponding to one particular partition of the whole
035: * abstract plan. The partition can be as big as the whole abstract graph or can
036: * be as small as a single job. The partitions are determined by the Partitioner.
037: *
038: * @author Karan Vahi
039: * @version $Revision: 314 $
040: */
041: public class DAX2CDAG implements Callback {
042:
043: /**
044: * The DAGInfo object which contains information corresponding to the ADag in
045: * the XML file.
046: */
047: private DagInfo mDagInfo;
048:
049: /**
050: * Contains SubInfo objects. One per submit file.
051: */
052: private Vector mVSubInfo;
053:
054: /**
055: * The mapping of the idrefs of a job to the job name.
056: */
057: private Map mJobMap;
058:
059: /**
060: * The handle to the properties object.
061: */
062: private PegasusProperties mProps;
063:
064: /**
065: * A flag to specify whether the graph has been generated for the partition
066: * or not.
067: */
068: private boolean mDone;
069:
070: /**
071: * The overloaded constructor.
072: *
073: * @param properties the properties passed to the planner.
074: * @param dax the path to the DAX file.
075: */
076: public DAX2CDAG(PegasusProperties properties, String dax) {
077: // mDAXPath = dax;
078: mDagInfo = new DagInfo();
079: mVSubInfo = new Vector();
080: mJobMap = new HashMap();
081: mProps = properties;
082: mDone = false;
083: }
084:
085: /**
086: * Callback when the opening tag was parsed. This contains all
087: * attributes and their raw values within a map. It ends up storing
088: * the attributes with the adag element in the internal memory structure.
089: *
090: * @param attributes is a map of attribute key to attribute value
091: */
092: public void cbDocument(Map attributes) {
093: mDagInfo.count = (String) attributes.get("count");
094: mDagInfo.index = (String) attributes.get("index");
095: mDagInfo.setLabel((String) attributes.get("name"));
096: }
097:
098: /**
099: * Callback for the job from section 2 jobs. These jobs are completely
100: * assembled, but each is passed separately.
101: *
102: * @param job the <code>SubInfo</code> object storing the job information
103: * gotten from parser.
104: */
105: public void cbJob(SubInfo job) {
106: mJobMap.put(job.logicalId, job.jobName);
107: mVSubInfo.add(job);
108: mDagInfo.addNewJob(job);
109:
110: //put the input files in the map
111: Iterator it = job.inputFiles.iterator();
112: String lfn;
113: while (it.hasNext()) {
114: lfn = ((PegasusFile) it.next()).getLFN();
115: mDagInfo.updateLFNMap(lfn, "i");
116: }
117:
118: it = job.outputFiles.iterator();
119: while (it.hasNext()) {
120: lfn = ((PegasusFile) it.next()).getLFN();
121: mDagInfo.updateLFNMap(lfn, "o");
122: }
123:
124: }
125:
126: /**
127: * Callback for child and parent relationships from section 3.
128: *
129: * @param child is the IDREF of the child element.
130: * @param parents is a list of IDREFs of the included parents.
131: */
132: public void cbParents(String child, List parents) {
133: PCRelation relation;
134:
135: child = (String) mJobMap.get(child);
136: String parent;
137:
138: for (Iterator it = parents.iterator(); it.hasNext();) {
139: parent = (String) mJobMap.get((String) it.next());
140: if (parent == null) {
141: //this actually means dax is generated wrong.
142: //probably some one tinkered with it by hand.
143: throw new RuntimeException(
144: "Cannot find parent for job " + child);
145: }
146: mDagInfo.addNewRelation(parent, child);
147: }
148:
149: }
150:
151: /**
152: * Callback when the parsing of the document is done. It sets the flag
153: * that the parsing has been done, that is used to determine whether the
154: * ADag object has been fully generated or not.
155: */
156: public void cbDone() {
157: mDone = true;
158: }
159:
160: /**
161: * Returns an ADag object corresponding to the abstract plan it has generated.
162: * It throws a runtime exception if the method is called before the object
163: * has been created fully.
164: *
165: * @return ADag object containing the abstract plan referred in the dax.
166: */
167: public Object getConstructedObject() {
168: if (!mDone)
169: throw new RuntimeException(
170: "Method called before the abstract dag "
171: + " for the partition was fully generated");
172:
173: return new ADag(mDagInfo, mVSubInfo);
174: }
175: }
|