001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.lib.xml.parser;
028:
029: import java.util.Date;
030:
031: import org.cougaar.lib.util.UTILPreference;
032: import org.cougaar.planning.ldm.PlanningFactory;
033: import org.cougaar.planning.ldm.plan.Preference;
034: import org.cougaar.util.log.Logger;
035: import org.w3c.dom.Node;
036: import org.w3c.dom.NodeList;
037:
038: /**
039: * <pre>
040: * Parses preferences of tasks in test input files.
041: *
042: * Currently handles start time, end time, and cost preferences.
043: * Start time is expected to have a ready at date.
044: * End time is expected to have an early, best, and latest date.
045: * </pre>
046: */
047: public class PreferencesParser {
048:
049: public PreferencesParser(Logger logger) {
050: prefHelper = new UTILPreference(logger);
051: dateParser = new DateParser();
052: }
053:
054: /**
055: * Get the cost preference
056: */
057: public Preference getCost(PlanningFactory ldmf, Node node) {
058: NodeList nlist = node.getChildNodes();
059: int nlength = nlist.getLength();
060: double cost = 0.0;
061:
062: for (int i = 0; i < nlength; i++) {
063: Node child = nlist.item(i);
064: Double d = new Double(child.getNodeValue());
065: cost = d.doubleValue();
066: }
067: return prefHelper.makeCostPreference(ldmf, cost);
068: }
069:
070: /**
071: * Get the quantity preference
072: */
073: public Preference getQuantity(PlanningFactory ldmf, Node node) {
074: NodeList nlist = node.getChildNodes();
075: int nlength = nlist.getLength();
076: long quantity = 0l;
077:
078: for (int i = 0; i < nlength; i++) {
079: Node child = nlist.item(i);
080: Long l = new Long(child.getNodeValue());
081: quantity = l.longValue();
082: }
083: return prefHelper.makeQuantityPreference(ldmf, quantity);
084: }
085:
086: /**
087: * Get the start date preference
088: */
089: public Preference getStartDate(PlanningFactory ldmf, Node node) {
090: NodeList nlist = node.getChildNodes();
091: int nlength = nlist.getLength();
092: Date readyatdate = null;
093:
094: for (int i = 0; i < nlength; i++) {
095: Node child = nlist.item(i);
096: String childname = child.getNodeName();
097:
098: if (child.getNodeType() == Node.ELEMENT_NODE) {
099: if (childname.equals("readyatdate")) {
100: readyatdate = dateParser.getDate(child);
101: }
102: }
103: }
104: return prefHelper.makeStartDatePreference(ldmf, readyatdate);
105: }
106:
107: /**
108: * Get the end date preference
109: */
110: public Preference getEndDate(PlanningFactory ldmf, Node node) {
111: NodeList nlist = node.getChildNodes();
112: int nlength = nlist.getLength();
113: Date earldate = null;
114: Date bestdate = null;
115: Date latedate = null;
116:
117: for (int i = 0; i < nlength; i++) {
118: Node child = nlist.item(i);
119: String childname = child.getNodeName();
120:
121: if (child.getNodeType() == Node.ELEMENT_NODE) {
122: if (childname.equals("earlydate")) {
123: earldate = dateParser.getDate(child);
124: } else if (childname.equals("bestdate")) {
125: bestdate = dateParser.getDate(child);
126: } else if (childname.equals("latedate")) {
127: latedate = dateParser.getDate(child);
128: }
129: }
130: }
131:
132: if (earldate == null || latedate == null)
133: return prefHelper
134: .makeEndDateBelowPreference(ldmf, bestdate);
135: else
136: return prefHelper.makeEndDatePreference(ldmf, earldate,
137: bestdate, latedate);
138: }
139:
140: protected UTILPreference prefHelper;
141: protected DateParser dateParser;
142: }
|