001: /* */
002: /*
003: * <copyright>
004: *
005: * Copyright 1997-2004 BBNT Solutions, LLC
006: * under sponsorship of the Defense Advanced Research Projects
007: * Agency (DARPA).
008: *
009: * You can redistribute this software and/or modify it under the
010: * terms of the Cougaar Open Source License as published on the
011: * Cougaar Open Source Website (www.cougaar.org).
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
014: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
015: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
016: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
017: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
018: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
019: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
020: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
021: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
022: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
023: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
024: *
025: * </copyright>
026: */
027:
028: package org.cougaar.glm.util;
029:
030: import java.util.Date;
031:
032: import org.cougaar.glm.ldm.plan.GeolocLocation;
033: import org.cougaar.lib.util.UTILPluginException;
034: import org.cougaar.lib.util.UTILPreference;
035: import org.cougaar.planning.ldm.PlanningFactory;
036: import org.cougaar.planning.ldm.plan.AllocationResult;
037: import org.cougaar.planning.ldm.plan.AspectLocation;
038: import org.cougaar.planning.ldm.plan.AspectType;
039: import org.cougaar.planning.ldm.plan.AspectValue;
040: import org.cougaar.planning.ldm.plan.PlanElement;
041: import org.cougaar.planning.ldm.plan.Preference;
042: import org.cougaar.planning.ldm.plan.ScoringFunction;
043: import org.cougaar.planning.ldm.plan.Task;
044: import org.cougaar.util.log.Logger;
045:
046: /**
047: * This class contains preference-related methods.
048: * Can ask basic questions like ready at, early, best, and latest date for
049: * tasks.
050: *
051: * Can also create an various kinds of preferences.
052: */
053:
054: public class GLMPreference extends UTILPreference {
055: private static String myName = "GLMPreference";
056: private static double ONE_DAY = 1000 * 60 * 60 * 24; // millis
057: private static double ONE_OVER_ONE_DAY = 1.0d / ONE_DAY; // millis
058: private static double fiftyYears = ONE_DAY * 365 * 50; // millis
059: private static double boundaryDefaultScore = 0.75;
060:
061: public GLMPreference(Logger l) {
062: super (l);
063: }
064:
065: /**
066: * Make a POD preference.
067: *
068: * Score increases as distance from best location increases...
069: *
070: * This needs work...
071: *
072: * What should we do with the weight of the preference?
073: * Should this be set by a policy object?
074: */
075: public Preference makePODPreference(PlanningFactory ldmf,
076: GeolocLocation loc) {
077: GLMLocationScoringFunction podSF = new GLMLocationScoringFunction(
078: loc, logger);
079: Preference podPref = ldmf.newPreference(AspectType.POD, podSF,
080: 1.0);
081: return podPref;
082: }
083:
084: /**
085: * What should we do with the weight of the preference?
086: * Should this be set by a policy object?
087: *
088: * Note that it uses one day as the slope -- i.e.
089: * a day after the POD date, the pref is exceeded.
090: */
091: public Preference makePODDatePreference(PlanningFactory ldmf,
092: Date bestDate) {
093: if (bestDate == null || bestDate.before(new Date(1000))) {
094: System.err
095: .println("GLMPreference creating bad POD_Date preference: the date is "
096: + bestDate);
097: }
098: AspectValue podAV = AspectValue.newAspectValue(
099: AspectType.POD_DATE, bestDate.getTime());
100: ScoringFunction podSF = ScoringFunction.createPreferredAtValue(
101: podAV, ONE_OVER_ONE_DAY);
102: Preference podPref = ldmf.newPreference(AspectType.POD_DATE,
103: podSF, 1.0);
104: return podPref;
105: }
106:
107: /**
108: * Returns the POD Date from task object, null if POD date not a pref on this task
109: *
110: * @param t - the Task with the pref
111: * @return Date point of departure date for task, null if no POD date pref
112: */
113:
114: public Date getPODDate(Task t) {
115: Preference pod_pref = getPrefWithAspectType(t,
116: AspectType.POD_DATE);
117: if (pod_pref == null)
118: return null;
119:
120: Date pod_date = new Date((long) (pod_pref.getScoringFunction()
121: .getBest().getValue()));
122:
123: return pod_date;
124: }
125:
126: /**
127: * Get reported POD date from plan element
128: */
129: public Date getReportedPODDate(PlanElement pe) {
130: return getReportedPODDate(pe.getReportedResult());
131: }
132:
133: /**
134: * Get reported POD date from allocation result
135: */
136: public Date getReportedPODDate(AllocationResult result) {
137: double value = result.getValue(AspectType.POD_DATE);
138: if (value != NO_ASPECT_VALUE)
139: return new Date((long) value);
140: return null;
141: }
142:
143: /**
144: * Returns the POD location from task object, null if POD not a pref on this task
145: *
146: * @param t - the Task with the pref
147: * @return Date point of departure for task, null if no POD pref
148: */
149:
150: public GeolocLocation getPODLocation(Task t) {
151: Preference pod_pref = getPrefWithAspectType(t, AspectType.POD);
152: if (pod_pref == null)
153: return null;
154:
155: GeolocLocation geoloc = ((GLMLocationScoringFunction) pod_pref
156: .getScoringFunction()).getLocation();
157:
158: return geoloc;
159: }
160:
161: /**
162: * Get reported POD location from plan element
163: */
164: public GeolocLocation getReportedPODLocation(PlanElement pe) {
165: return getReportedPODLocation(pe.getReportedResult());
166: }
167:
168: /**
169: * Get reported POD location from allocation result
170: */
171: public GeolocLocation getReportedPODLocation(AllocationResult result) {
172: if (result == null)
173: return null;
174:
175: Object aspectValue = null;
176: AspectValue[] results = result.getAspectValueResults();
177:
178: try {
179: for (int i = 0; i < results.length; i++) {
180: if (results[i].getAspectType() == AspectType.POD) {
181: aspectValue = results[i];
182: AspectLocation loc = (AspectLocation) aspectValue;
183: return (GeolocLocation) loc.getLocationValue();
184: }
185: }
186: } catch (ClassCastException cce) {
187: throw new UTILPluginException(
188: classname
189: + ".getReportedPODLocation - expecting an AspectLocation as POD aspect value. "
190: + "\nInstead got " + aspectValue.getClass());
191: }
192:
193: return null;
194: }
195:
196: private static final String classname = GLMPreference.class
197: .getName();
198: }
|