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.planning.ldm.plan;
028:
029: import java.util.Collection;
030: import java.util.Iterator;
031:
032: import org.cougaar.core.util.UID;
033: import org.cougaar.util.TimeSpan;
034: import org.cougaar.util.TimeSpans;
035: import org.cougaar.util.UnaryPredicate;
036:
037: /**
038: * A RelationshipSchedule is a representation of an object (must implement
039: * HasRelationships) relationships
040: **/
041:
042: public class RelationshipScheduleImpl extends ScheduleImpl implements
043: NewRelationshipSchedule {
044: private HasRelationships myHasRelationships;
045:
046: public RelationshipScheduleImpl() {
047: super ();
048: setScheduleType(ScheduleType.RELATIONSHIP);
049: setScheduleElementType(ScheduleElementType.RELATIONSHIP);
050: }
051:
052: public RelationshipScheduleImpl(HasRelationships hasRelationships) {
053: this ();
054:
055: setHasRelationships(hasRelationships);
056: }
057:
058: public RelationshipScheduleImpl(HasRelationships hasRelationships,
059: Collection relationships) {
060: this (hasRelationships);
061:
062: addAll(relationships);
063: }
064:
065: /** Construct a schedule which has the same elements as the specified
066: * collection. If the specified collection needs to be sorted, it will
067: * be.
068: **/
069: public RelationshipScheduleImpl(RelationshipSchedule schedule) {
070: this (schedule.getHasRelationships(), schedule);
071: }
072:
073: public HasRelationships getHasRelationships() {
074: return myHasRelationships;
075: }
076:
077: public synchronized void setHasRelationships(
078: HasRelationships hasRelationships) {
079: if (!isEmpty()) {
080: throw new IllegalArgumentException(
081: "RelationshipScheduleImpl.setHasRelationships() can only be called on an empty schedule");
082: }
083:
084: myHasRelationships = hasRelationships;
085: }
086:
087: public synchronized boolean isAppropriateScheduleElement(Object o) {
088: if (!super .isAppropriateScheduleElement(o)) {
089: return false;
090: }
091:
092: Relationship relationship = (Relationship) o;
093:
094: if ((myHasRelationships == null)
095: || ((!relationship.getA().equals(myHasRelationships)) && (!relationship
096: .getB().equals(myHasRelationships)))) {
097: return false;
098: }
099:
100: return true;
101: }
102:
103: /** getMatchingRelationships - return all Relationships which pass the
104: * specified UnaryPredicate.
105: *
106: * @param predicate UnaryPredicate to use in screening Relationships
107: * @return a sorted Collection containing all Relationships which
108: * which pass the specified UnaryPredicate
109: **/
110: public synchronized Collection getMatchingRelationships(
111: UnaryPredicate predicate) {
112: return filter(predicate);
113: }
114:
115: /** getMatchingRelationships - return all Relationships which contain the
116: * specified role.
117: *
118: * @param role Role to look for
119: * @return a sorted Collection containing all Relationships which
120: * which match the specified Role
121: **/
122: public synchronized Collection getMatchingRelationships(
123: final Role role) {
124: final RelationshipScheduleImpl schedule = this ;
125: return filter(new UnaryPredicate() {
126: public boolean execute(Object obj) {
127: Relationship relationship = (Relationship) obj;
128: return schedule.getOtherRole(relationship).equals(role);
129: }
130: });
131: }
132:
133: /** getMatchingRelationships - return all Relationships which contain the
134: * specified role and intersect the time.
135: *
136: * @param role Role to look for
137: * @param time long specifying the time
138: * @return a sorted Collection containing all Relationships which
139: * which match the specified Role and intersect the specified time
140: **/
141: public Collection getMatchingRelationships(final Role role,
142: final long time) {
143: final RelationshipScheduleImpl schedule = this ;
144:
145: return filter(new UnaryPredicate() {
146: public boolean execute(Object obj) {
147: Relationship relationship = (Relationship) obj;
148: return ((schedule.getOtherRole(relationship)
149: .equals(role))
150: && (time >= relationship.getStartTime()) && (time < relationship
151: .getEndTime()));
152: }
153: });
154: }
155:
156: /** getMatchingRelationships - return all Relationships which contain the
157: * specified role and overlap the specified time span.
158: *
159: * @param role Role to look for
160: * @param timeSpan TimeSpan
161: * @return a sorted Collection containing all Relationships which
162: * which match the specified Role and overlap the specified time span
163: **/
164: public synchronized Collection getMatchingRelationships(
165: final Role role, final TimeSpan timeSpan) {
166: final RelationshipScheduleImpl schedule = this ;
167: return filter(new UnaryPredicate() {
168: public boolean execute(Object obj) {
169: Relationship relationship = (Relationship) obj;
170: return ((schedule.getOtherRole(relationship)
171: .equals(role))
172: && (relationship.getStartTime() < timeSpan
173: .getEndTime()) && (relationship
174: .getEndTime() > timeSpan.getStartTime()));
175: }
176: });
177: }
178:
179: /** getMatchingRelationships - return all Relationships which contain the
180: * specified role and overlap the time span specified by the start and end
181: * time arguments
182: *
183: * @param role Role to look for
184: * @param startTime long specifying the start of the time span
185: * @param endTime long specifying the end of the time span
186: * @return a sorted Collection containing all Relationships which
187: * which match the specified Role and overlap the specified time span
188: * @deprecated Use getMatchingRelationships(Role role, TimeSpan timeSpan) or
189: * getMatchingRelationships(Role role, long time)
190: **/
191: public synchronized Collection getMatchingRelationships(
192: final Role role, final long startTime, final long endTime) {
193: final TimeSpan timeSpan = TimeSpans.getSpan(startTime, endTime);
194:
195: return getMatchingRelationships(role, timeSpan);
196: }
197:
198: /** getMatchingRelationships - return all Relationships which contain the
199: * specified other object, match the specified role and intersect the time.
200: *
201: * @param role Role to look for
202: * @param otherObject HasRelationships
203: * @param time long specifying the time
204: * @return a sorted Collection containing all Relationships which contain
205: * the specified other object, match the specified role and direct object
206: * flag, and intersect the specified time.
207: **/
208: public Collection getMatchingRelationships(final Role role,
209: final HasRelationships otherObject, final long time) {
210: final RelationshipScheduleImpl schedule = this ;
211: return filter(new UnaryPredicate() {
212: public boolean execute(Object obj) {
213: Relationship relationship = (Relationship) obj;
214: return ((schedule.getOtherRole(relationship)
215: .equals(role))
216: && (schedule.getOther(relationship)
217: .equals(otherObject))
218: && (time >= relationship.getStartTime()) && (time < relationship
219: .getEndTime()));
220: }
221: });
222: }
223:
224: /** getMatchingRelationships - return all Relationships which contain the
225: * specified other object, match the specified role and and overlap the specified time
226: * span.
227: *
228: * @param role Role to look for
229: * @param other HasRelationships
230: * @param timeSpan TimeSpan
231: * @return a sorted Collection containing all Relationships which
232: * which match the specified Role and overlap the specified time span
233: **/
234: public synchronized Collection getMatchingRelationships(
235: final Role role, final HasRelationships other,
236: final TimeSpan timeSpan) {
237: final RelationshipScheduleImpl schedule = this ;
238: return filter(new UnaryPredicate() {
239: public boolean execute(Object obj) {
240: Relationship relationship = (Relationship) obj;
241: return ((schedule.getOtherRole(relationship)
242: .equals(role))
243: && (schedule.getOther(relationship)
244: .equals(other)) && ((relationship
245: .getStartTime() < timeSpan.getEndTime()) && (relationship
246: .getEndTime() > timeSpan.getStartTime())));
247: }
248: });
249: }
250:
251: /** getMatchingRelationships - return all Relationships which match the
252: * specified other object, match the specified role, and overlap the
253: * the time span specified by the start and end time arguments
254: *
255: * @param role Role to look for
256: * @param other HasRelationships
257: * @param startTime long specifying the start of the time span
258: * @param endTime long specifying the end of the time span
259: * @return a sorted Collection containing all Relationships which
260: * which match the specified Role, direct object flag and overlap the
261: * specified time span.
262: * @deprecated Use getMatchingRelationships(Role role, HasRelationships otherObject, TimeSpan timeSpan) or
263: * getMatchingRelationships(Role role, HasRelationships otherObject, long time)
264: **/
265: public synchronized Collection getMatchingRelationships(
266: final Role role, final HasRelationships other,
267: final long startTime, final long endTime) {
268: final TimeSpan timeSpan = TimeSpans.getSpan(startTime, endTime);
269:
270: return getMatchingRelationships(role, other, timeSpan);
271: }
272:
273: /** getMatchingRelationships - return all Relationships which contain the
274: * specified other object and intersect the specified time.
275: *
276: * @param otherObject HasRelationships
277: * @param time long
278: * @return a sorted Collection containing all Relationships which
279: * which contain the specified other HasRelationships and intersect the
280: * specified time span
281: **/
282: public Collection getMatchingRelationships(
283: final HasRelationships otherObject, final long time) {
284: final RelationshipScheduleImpl schedule = this ;
285: return filter(new UnaryPredicate() {
286: public boolean execute(Object obj) {
287: Relationship relationship = (Relationship) obj;
288: return ((schedule.getOther(relationship)
289: .equals(otherObject))
290: && (time >= relationship.getStartTime()) && (time < relationship
291: .getEndTime()));
292: }
293: });
294: }
295:
296: /** getMatchingRelationships - return all Relationships which contain the
297: * specified other object and overlap the specified time span.
298: *
299: * @param other HasRelationships
300: * @param timeSpan TimeSpan
301: * @return a sorted Collection containing all Relationships which
302: * which match the specified Role and overlap the specified time span
303: **/
304: public synchronized Collection getMatchingRelationships(
305: final HasRelationships other, final TimeSpan timeSpan) {
306: final RelationshipScheduleImpl schedule = this ;
307: return filter(new UnaryPredicate() {
308: public boolean execute(Object obj) {
309: Relationship relationship = (Relationship) obj;
310: return ((schedule.getOther(relationship).equals(other)) && ((relationship
311: .getStartTime() < timeSpan.getEndTime()) && (relationship
312: .getEndTime() > timeSpan.getStartTime())));
313: }
314: });
315: }
316:
317: /** getMatchingRelationships - return all Relationships which contain the
318: * specified other object and overlap the time span specified by the start and end
319: * time arguments.
320: *
321: * @param other HasRelationships
322: * @param startTime long specifying the start of the time span
323: * @param endTime long specifying the end of the time span
324: * @return a sorted Collection containing all Relationships which
325: * which match the specified direct object flag and overlap the
326: * specified time span
327: * @deprecated Use getMatchingRelationships(HasRelationships otherObject, TimeSpan timeSpan) or
328: * getMatchingRelationships(HasRelationships otherObject, long time)
329: **/
330: public synchronized Collection getMatchingRelationships(
331: final HasRelationships other, final long startTime,
332: final long endTime) {
333: final TimeSpan timeSpan = TimeSpans.getSpan(startTime, endTime);
334:
335: return getMatchingRelationships(other, timeSpan);
336: }
337:
338: /** getMatchingRelationships - return all Relationships where the role
339: * ends with the specifed suffix and intersects the specified time.
340: *
341: * @param roleSuffix String
342: * @param time long specifying the time
343: * @return a sorted Collection containing all Relationships which
344: * which match the specified role suffix and intersect the specified time
345: **/
346: public synchronized Collection getMatchingRelationships(
347: final String roleSuffix, final long time) {
348: final RelationshipScheduleImpl schedule = this ;
349: return filter(new UnaryPredicate() {
350: public boolean execute(Object obj) {
351: Relationship relationship = (Relationship) obj;
352: return ((schedule.getOtherRole(relationship).getName()
353: .endsWith(roleSuffix))
354: && (time >= relationship.getStartTime()) && (time < relationship
355: .getEndTime()));
356: }
357: });
358: }
359:
360: /** getMatchingRelationships - return all Relationships where the role
361: * ends with the specifed suffix and overlap the specified time span.
362: *
363: * @param roleSuffix String
364: * @param timeSpan TimeSpan
365: * @return a sorted Collection containing all Relationships which
366: * which match the specified role suffix and overlap the specified time span
367: **/
368: public synchronized Collection getMatchingRelationships(
369: final String roleSuffix, final TimeSpan timeSpan) {
370: final RelationshipScheduleImpl schedule = this ;
371: return filter(new UnaryPredicate() {
372: public boolean execute(Object obj) {
373: Relationship relationship = (Relationship) obj;
374: return ((schedule.getOtherRole(relationship).getName()
375: .endsWith(roleSuffix)) && ((relationship
376: .getStartTime() < timeSpan.getEndTime()) && (relationship
377: .getEndTime() > timeSpan.getStartTime())));
378: }
379: });
380: }
381:
382: /** getMatchingRelationships - return all Relationships where the role
383: * ends with the specifed suffix and overlap the time span specified by
384: * the start and end time arguments
385: *
386: * @param roleSuffix String
387: * @param startTime long specifying the start of the time span
388: * @param endTime long specifying the end of the time span
389: * @return a sorted Collection containing all Relationships which
390: * which match the specified role suffix and overlap the
391: * specified time span
392: * @deprecated Use getMatchingRelationships(String roleSuffix, TimeSpan timeSpan) or
393: * getMatchingRelationships(String roleSuffix, long time)
394: **/
395: public synchronized Collection getMatchingRelationships(
396: final String roleSuffix, final long startTime,
397: final long endTime) {
398: final TimeSpan timeSpan = TimeSpans.getSpan(startTime, endTime);
399:
400: return getMatchingRelationships(roleSuffix, timeSpan);
401: }
402:
403: /** getMatchingRelationships - return all Relationships which intersect the
404: * specified time.
405: *
406: * @param time long specifying the time
407: * @return a sorted Collection containing all Relationships which
408: * which intersect the specified time
409: **/
410: public synchronized Collection getMatchingRelationships(
411: final long time) {
412: return filter(new UnaryPredicate() {
413: public boolean execute(Object obj) {
414: Relationship relationship = (Relationship) obj;
415: return ((time >= relationship.getStartTime()) && (time < relationship
416: .getEndTime()));
417: }
418: });
419: }
420:
421: /** getMatchingRelationships - return all Relationships which overlap the
422: * specified time span.
423: *
424: * @param timeSpan TimeSpan
425: * @return a sorted Collection containing all Relationships which
426: * which contain overlap the specified time span
427: **/
428: public synchronized Collection getMatchingRelationships(
429: final TimeSpan timeSpan) {
430: return filter(new UnaryPredicate() {
431: public boolean execute(Object obj) {
432: Relationship relationship = (Relationship) obj;
433: return ((relationship.getStartTime() < timeSpan
434: .getEndTime()) && (relationship.getEndTime() > timeSpan
435: .getStartTime()));
436: }
437: });
438: }
439:
440: /** getMatchingRelationships - return all Relationships which overlap the
441: * time span specified by the start and end time arguments
442: *
443: * @param startTime long specifying the start of the time span
444: * @param endTime long specifying the end of the time span
445: * @return a sorted Collection containing all Relationships which
446: * which match overlap the specified time span
447: * @deprecated Use getMatchingRelationships(TimeSpan timeSpan) or
448: * getMatchingRelationships(long time)
449: **/
450: public synchronized Collection getMatchingRelationships(
451: final long startTime, final long endTime) {
452: final TimeSpan timeSpan = TimeSpans.getSpan(startTime, endTime);
453:
454: return getMatchingRelationships(timeSpan);
455: }
456:
457: /** getMyRole - return role for schedule's HasRelationships in the specified
458: * relationship.
459: *
460: * @param relationship Relationship
461: * @return Role
462: */
463: public Role getMyRole(Relationship relationship) {
464: if (relationship.getA().equals(getHasRelationships())) {
465: return relationship.getRoleA();
466: } else if (relationship.getB().equals(getHasRelationships())) {
467: return relationship.getRoleB();
468: } else {
469: return null;
470: }
471: }
472:
473: /** getMyRole - return role for other HasRelationships in the specified
474: * relationship.
475: *
476: * @param relationship Relationship
477: * @return Role
478: */
479: public Role getOtherRole(Relationship relationship) {
480: if (relationship.getA().equals(getHasRelationships())) {
481: return relationship.getRoleB();
482: } else if (relationship.getB().equals(getHasRelationships())) {
483: return relationship.getRoleA();
484: } else {
485: return null;
486: }
487: }
488:
489: /** getOther - return other (i.e. not schedule's) HasRelationships in the
490: * specified relationship.
491: *
492: * @param relationship Relationship
493: * @return HasRelationships
494: */
495: public HasRelationships getOther(Relationship relationship) {
496: if (relationship.getA().equals(getHasRelationships())) {
497: return relationship.getB();
498: } else if (relationship.getB().equals(getHasRelationships())) {
499: return relationship.getA();
500: } else {
501: return null;
502: }
503: }
504:
505: private static class TestAsset implements
506: org.cougaar.core.util.UniqueObject, HasRelationships {
507: private RelationshipSchedule mySchedule;
508: private org.cougaar.core.util.UID myUID;
509:
510: public TestAsset() {
511: mySchedule = new RelationshipScheduleImpl(this );
512: }
513:
514: public RelationshipSchedule getRelationshipSchedule() {
515: return mySchedule;
516: }
517:
518: public void setRelationshipSchedule(
519: RelationshipSchedule newSchedule) {
520: mySchedule = newSchedule;
521: }
522:
523: public boolean isLocal() {
524: return true;
525: }
526:
527: public void setLocal(boolean flag) {
528: }
529:
530: public boolean isSelf() {
531: return isLocal();
532: }
533:
534: public void setUID(org.cougaar.core.util.UID uid) {
535: myUID = uid;
536: }
537:
538: public org.cougaar.core.util.UID getUID() {
539: return myUID;
540: }
541: }
542:
543: public static void main(String[] args) {
544: int uidNum = 0;
545:
546: TestAsset testAsset0 = new TestAsset();
547: testAsset0.setUID(new UID("testAsset", uidNum));
548: uidNum++;
549:
550: TestAsset testAsset1 = new TestAsset();
551: testAsset1.setUID(new UID("testAsset", uidNum));
552: uidNum++;
553:
554: TestAsset testAsset2 = new TestAsset();
555: testAsset2.setUID(new UID("testAsset", uidNum));
556: uidNum++;
557:
558: Role.create("ParentProvider", "ParentCustomer");
559: Role parent = Role.getRole("ParentProvider");
560: Role child = Role.getRole("ParentCustomer");
561:
562: Role.create("GarbageCustomer", "GarbageProvider");
563:
564: Relationship rel1 = new RelationshipImpl(0, 10, parent,
565: testAsset0, testAsset1);
566: Relationship rel2 = new RelationshipImpl(5, 15, parent,
567: testAsset1, testAsset0);
568: Relationship rel3 = new RelationshipImpl(2, 9, child,
569: testAsset1, testAsset0);
570: Relationship rel4 = new RelationshipImpl(0, 30, Role
571: .getRole("GarbageCustomer"), testAsset0, testAsset1);
572:
573: Relationship testRel = new RelationshipImpl(0, 30, Role
574: .getRole("GarbageCustomer"), testAsset1, testAsset0);
575:
576: RelationshipSchedule schedule = new RelationshipScheduleImpl(
577: testAsset0);
578: schedule.add(rel1);
579: schedule.add(rel2);
580: schedule.add(rel3);
581: schedule.add(rel4);
582:
583: System.out.println(schedule);
584: System.out.println(schedule.iterator());
585: Collection collection = schedule
586: .getMatchingRelationships(parent);
587: Iterator iterator = collection.iterator();
588: System.out.println("Role -" + parent);
589: while (iterator.hasNext()) {
590: System.out.println((Relationship) iterator.next());
591: }
592: ;
593:
594: TimeSpan timeSpan = TimeSpans.getSpan(10, 17);
595: collection = schedule
596: .getMatchingRelationships(parent, timeSpan);
597: iterator = collection.iterator();
598: System.out.println("Role -" + parent + " time span 10 - 17");
599: while (iterator.hasNext()) {
600: System.out.println((Relationship) iterator.next());
601: }
602: ;
603:
604: timeSpan = TimeSpans.getSpan(0, 5);
605: collection = schedule.getMatchingRelationships(child, timeSpan);
606: iterator = collection.iterator();
607: System.out.println("Role -" + child + " time span 0 - 5");
608: while (iterator.hasNext()) {
609: System.out.println((Relationship) iterator.next());
610: }
611: ;
612:
613: timeSpan = TimeSpans.getSpan(10, 17);
614: collection = schedule.getMatchingRelationships("Provider",
615: timeSpan);
616: iterator = collection.iterator();
617: System.out
618: .println("Role Suffix - 'Provider', time span 10 - 17");
619: while (iterator.hasNext()) {
620: System.out.println((Relationship) iterator.next());
621: }
622: ;
623:
624: schedule.add(testRel);
625:
626: }
627:
628: }
|