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.mlm.debug.ui;
028:
029: import org.cougaar.glm.ldm.plan.AlpineAspectType;
030: import org.cougaar.planning.ldm.plan.AspectType;
031: import org.cougaar.planning.ldm.plan.Constraint;
032: import org.cougaar.planning.ldm.plan.ConstraintEvent;
033: import org.cougaar.planning.ldm.plan.Task;
034:
035: /** A tree node for a Constraint.
036: Overrides the UITreeNode loadChildren, toString and isLeaf
037: methods to dynamically display the Constraint which has no children.
038: */
039:
040: public class UIConstraintNode extends UITreeNode {
041: private static int msecsPerSecond = 1000;
042: private static int msecsPerMinute = 1000 * 60;
043: private static int msecsPerHour = 1000 * 60 * 60;
044: private static int msecsPerDay = 1000 * 60 * 60 * 24;
045: Constraint constraint;
046:
047: /** Create a tree node for the constraint.
048: @param constraint the constraint for which to create a tree node
049: */
050: public UIConstraintNode(Constraint constraint) {
051: super (constraint);
052: this .constraint = constraint;
053: }
054:
055: /** The constraint is a leaf.
056: @return true
057: */
058: public boolean isLeaf() {
059: return true;
060: }
061:
062: /** This shouldn't be called because this is a leaf.
063: */
064:
065: public void loadChildren() {
066: System.out
067: .println("Warning: UIConstraintNode:loadChildren called, but constraint is a leaf.");
068: }
069:
070: /** Return representation of a Constraint in a tree.
071: <constrained event> <constrained task> <offset of constraint>
072: <constraint order> <constraining event> <constraining task>
073: @return for example: initiate Task1 5 msecs after complete Task2
074: */
075:
076: public String toString() {
077: String constrainingEventS = getConstraintEventDescription(constraint
078: .getConstrainingEventObject());
079: String constrainedEventS = getConstraintEventDescription(constraint
080: .getConstrainedEventObject());
081: String constraintOrderS = getConstraintOrderDescription(constraint);
082: String constraintOffsetS = "";
083: double constraintOffset = constraint.getOffsetOfConstraint();
084: if (constraintOffset > 0.0) {
085: constraintOffsetS = " + "
086: + getOffsetDescription(constraintOffset, constraint
087: .getConstrainedAspect());
088: }
089: if (constraintOffset < 0.0) {
090: constraintOffsetS = " - "
091: + getOffsetDescription(-constraintOffset,
092: constraint.getConstrainedAspect());
093: }
094: return constrainedEventS + constraintOffsetS + constraintOrderS
095: + constrainingEventS;
096: }
097:
098: private String getOffsetDescription(double offset, int aspect) {
099: switch (aspect) {
100: case AspectType.START_TIME:
101: case AspectType.END_TIME:
102: return getTimeDescription((long) offset);
103: default:
104: return Double.toString(offset);
105: }
106: }
107:
108: private String getConstraintEventDescription(ConstraintEvent event) {
109: Task task = event.getTask();
110: int aspectType = event.getAspectType();
111: if (task == null) { // Event is absolute
112: if (aspectType == AspectType.START_TIME
113: || aspectType == AspectType.START_TIME) {
114: return new java.util.Date((long) event.getValue())
115: .toString();
116: } else {
117: return Double.toString(event.getValue());
118: }
119: } else {
120: return (getConstraintAspectDescription(aspectType) + " of " + UITask
121: .getDescription(task));
122: }
123: }
124:
125: /** Takes time in msecs; returns as days, hours, minutes, etc. */
126:
127: private String getTimeDescription(long time) {
128: long days = time / msecsPerDay;
129: time = time % msecsPerDay;
130: long hours = time / msecsPerHour;
131: time = time % msecsPerHour;
132: long minutes = time / msecsPerMinute;
133: time = time % msecsPerMinute;
134: long seconds = time / msecsPerSecond;
135: time = time % msecsPerSecond;
136: long msecs = time % msecsPerSecond;
137: String s = "";
138: if (days != 0)
139: s = days + " days ";
140: if (hours != 0)
141: s = s + hours + " hours ";
142: if (minutes != 0)
143: s = s + minutes + " minutes ";
144: if (seconds != 0)
145: s = s + seconds + " seconds ";
146: if (msecs != 0)
147: s = s + msecs + " msecs";
148: return s.trim();
149: }
150:
151: private String getConstraintOrderDescription(Constraint constraint) {
152: int aspectType = constraint.getConstrainedAspect();
153: if (aspectType == AspectType.START_TIME
154: || aspectType == AspectType.START_TIME) {
155: return getConstraintTimeOrderDescription(constraint
156: .getConstraintOrder());
157: } else {
158: return getConstraintValueOrderDescription(constraint
159: .getConstraintOrder());
160: }
161: }
162:
163: private String getConstraintTimeOrderDescription(int constraintOrder) {
164: if (constraintOrder == constraint.COINCIDENT)
165: return "coincident with";
166: if (constraintOrder == constraint.BEFORE)
167: return "before";
168: if (constraintOrder == constraint.AFTER)
169: return "after";
170: return "unknown";
171: }
172:
173: private String getConstraintValueOrderDescription(
174: int constraintOrder) {
175: if (constraintOrder == constraint.EQUALTO)
176: return "equal to";
177: if (constraintOrder == constraint.LESSTHAN)
178: return "less than";
179: if (constraintOrder == constraint.GREATERTHAN)
180: return "greater than";
181: return "unknown";
182: }
183:
184: private String getConstraintAspectDescription(int constraintAspect) {
185: if (constraintAspect < 0
186: || constraintAspect > AlpineAspectType.LAST_ALPINE_ASPECT) {
187: return "Aspect " + constraintAspect;
188: }
189: return AlpineAspectType.aspectTypeToString(constraintAspect);
190: }
191:
192: }
|