001: /*
002: * <copyright>
003: *
004: * Copyright 2002-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.servicediscovery;
028:
029: import org.cougaar.core.domain.Factory;
030: import org.cougaar.core.mts.MessageAddress;
031: import org.cougaar.planning.ldm.LDMServesPlugin;
032: import org.cougaar.planning.ldm.asset.Asset;
033: import org.cougaar.planning.ldm.plan.AspectType;
034: import org.cougaar.planning.ldm.plan.AspectValue;
035: import org.cougaar.planning.ldm.plan.HasRelationships;
036: import org.cougaar.planning.ldm.plan.Preference;
037: import org.cougaar.planning.ldm.plan.Role;
038: import org.cougaar.planning.ldm.plan.Schedule;
039: import org.cougaar.planning.ldm.plan.ScheduleImpl;
040: import org.cougaar.planning.ldm.plan.ScoringFunction;
041: import org.cougaar.planning.ldm.plan.TimeAspectValue;
042: import org.cougaar.servicediscovery.description.Lineage;
043: import org.cougaar.servicediscovery.description.LineageImpl;
044: import org.cougaar.servicediscovery.description.LineageScheduleElement;
045: import org.cougaar.servicediscovery.description.LineageScheduleElementImpl;
046: import org.cougaar.servicediscovery.description.MMQuery;
047: import org.cougaar.servicediscovery.description.ProviderCapabilities;
048: import org.cougaar.servicediscovery.description.ProviderCapabilitiesImpl;
049: import org.cougaar.servicediscovery.description.ServiceContract;
050: import org.cougaar.servicediscovery.description.ServiceContractImpl;
051: import org.cougaar.servicediscovery.description.ServiceContractRelationship;
052: import org.cougaar.servicediscovery.description.ServiceContractRelationshipImpl;
053: import org.cougaar.servicediscovery.description.ServiceRequest;
054: import org.cougaar.servicediscovery.description.ServiceRequestImpl;
055: import org.cougaar.servicediscovery.transaction.LineageRelay;
056: import org.cougaar.servicediscovery.transaction.MMQueryRequest;
057: import org.cougaar.servicediscovery.transaction.MMQueryRequestImpl;
058: import org.cougaar.servicediscovery.transaction.ServiceContractRelay;
059: import org.cougaar.servicediscovery.transaction.ServiceContractRelayImpl;
060: import org.cougaar.util.MutableTimeSpan;
061: import org.cougaar.util.NewTimeSpan;
062: import org.cougaar.util.TimeSpan;
063: import org.cougaar.util.log.Logger;
064: import org.cougaar.util.log.Logging;
065:
066: import java.util.ArrayList;
067: import java.util.Calendar;
068: import java.util.Collection;
069: import java.util.Iterator;
070: import java.util.List;
071:
072: /**
073: * Service discovery factory Domain package definition.
074: **/
075:
076: public class SDFactory implements Factory {
077: private static final Logger myLogger = Logging
078: .getLogger(SDFactory.class);
079:
080: public static final TimeSpan DEFAULT_TIME_SPAN;
081:
082: static {
083: Calendar calendar = Calendar.getInstance();
084:
085: long DEFAULT_START_TIME = 0l;// 1970 midnight
086:
087: calendar.set(2500, 0, 1, 0, 0, 0);
088: calendar.set(Calendar.MILLISECOND, 0);
089: long DEFAULT_END_TIME = calendar.getTime().getTime();
090:
091: DEFAULT_TIME_SPAN = new MutableTimeSpan();
092: ((NewTimeSpan) DEFAULT_TIME_SPAN).setTimeSpan(
093: DEFAULT_START_TIME, DEFAULT_END_TIME);
094: }
095:
096: private LDMServesPlugin myLDM;
097:
098: public SDFactory(LDMServesPlugin ldm) {
099: myLDM = ldm;
100:
101: /**
102: * Don't currently have service discovery specific assets or property
103: * groups.
104: RootFactory rf = ldm.getFactory();
105: rf.addAssetFactory(new org.cougaar.servicediscovery.asset.AssetFactory());
106: rf.addPropertyGroupFactory(new org.cougaar.servicediscovery.PropertyGroupFactory());
107: */
108: }
109:
110: /** Generate a new MMQueryRequest
111: *@param query - MMQuery to be executed
112: * @return MMQueryRequest
113: **/
114: public MMQueryRequest newMMQueryRequest(MMQuery query) {
115: MMQueryRequest mmRequest = new MMQueryRequestImpl(query);
116: mmRequest.setUID(myLDM.getUIDServer().nextUID());
117: return mmRequest;
118: }
119:
120: /** Generate a new Lineage
121: * @return Lineage
122: **/
123: public Lineage newLineage(int type) {
124: Lineage lineage = null;
125:
126: if (!validLineageType(type)) {
127: myLogger.error("Invalid lineage type: " + type);
128: } else {
129: lineage = new LineageImpl(type);
130: lineage.setUID(myLDM.getUIDServer().nextUID());
131:
132: ScheduleImpl schedule = (ScheduleImpl) newLineageSchedule();
133: ((LineageImpl) lineage).setSchedule(schedule);
134:
135: }
136:
137: return lineage;
138: }
139:
140: /** Generate a new Lineage
141: * @return Lineage
142: **/
143: public Lineage newLineage(int type, List list) {
144: Lineage lineage = newLineage(type);
145:
146: if (lineage != null) {
147: ((LineageImpl) lineage).setList(list);
148: }
149:
150: return lineage;
151: }
152:
153: /** Generate a new Lineage
154: * @return Lineage
155: **/
156: public Lineage newLineage(int type, List list, TimeSpan timeSpan) {
157: Lineage lineage = newLineage(type, list);
158:
159: if (lineage != null) {
160: ScheduleImpl schedule = (ScheduleImpl) lineage
161: .getSchedule();
162: LineageScheduleElement scheduleElement = newLineageScheduleElement(timeSpan);
163: schedule.add(scheduleElement);
164: }
165:
166: return lineage;
167: }
168:
169: /** Copy an existing Lineage - does not create new UID
170: * @return Lineage
171: **/
172: public Lineage copyLineage(Lineage original) {
173: Lineage lineage = new LineageImpl(original);
174: return lineage;
175: }
176:
177: /** Generate a new LineageRelay
178: * @return LineageRelay
179: **/
180: public LineageRelay newLineageRelay(MessageAddress super ior) {
181: LineageRelay lineageRelay = new LineageRelay();
182: lineageRelay.setUID(myLDM.getUIDServer().nextUID());
183: lineageRelay.addTarget(super ior);
184: return lineageRelay;
185: }
186:
187: /**
188: * validate specified lineage type
189: */
190: public static boolean validLineageType(int lineageType) {
191: return Lineage.validType(lineageType);
192: }
193:
194: /** Generate a new Lineage
195: * @return Lineage
196: **/
197: public static Schedule newLineageSchedule() {
198: ScheduleImpl lineageSchedule = new ScheduleImpl();
199:
200: lineageSchedule
201: .setScheduleElementType(Constants.SDScheduleElementType.LINEAGE);
202:
203: return lineageSchedule;
204: }
205:
206: /** Generate a new LineageScheduleElement
207: * @return LineageScheduleElement
208: **/
209: public static LineageScheduleElement newLineageScheduleElement(
210: TimeSpan timeSpan) {
211: return new LineageScheduleElementImpl(timeSpan);
212: }
213:
214: /** Generate a new LineageScheduleElement
215: * @return LineageScheduleElement
216: **/
217: public static LineageScheduleElement newLineageScheduleElement(
218: long startTime, long endTime) {
219: return new LineageScheduleElementImpl(startTime, endTime);
220: }
221:
222: /**
223: * validate specified military echelon
224: */
225: public static boolean validMilitaryEchelon(String echelon) {
226: return Constants.MilitaryEchelon.validMilitaryEchelon(echelon);
227: }
228:
229: /** Generate a new ProviderCapabilities()
230: * @return a ProviderCapabilities
231: **/
232: public ProviderCapabilities newProviderCapabilities(
233: String providerName) {
234: ProviderCapabilities providerCapabilities = new ProviderCapabilitiesImpl(
235: providerName);
236: providerCapabilities.setUID(myLDM.getUIDServer().nextUID());
237: return providerCapabilities;
238: }
239:
240: /** Generate a new ServiceRequest
241: * @return a ServiceRequest
242: **/
243: public ServiceRequest newServiceRequest(Asset client,
244: Role serviceRole, Collection servicePreferences) {
245: ServiceRequest serviceRequest = new ServiceRequestImpl(myLDM
246: .getFactory().cloneInstance(client), serviceRole,
247: servicePreferences);
248: return serviceRequest;
249: }
250:
251: /** Generate a new ServiceContract
252: * @return a ServiceContract
253: **/
254: public ServiceContract newServiceContract(Asset provider,
255: Role serviceRole, Collection servicePreferences) {
256: ServiceContract serviceContract = new ServiceContractImpl(myLDM
257: .getFactory().cloneInstance(provider), serviceRole,
258: servicePreferences);
259: return serviceContract;
260: }
261:
262: /**
263: * revoke a service contract
264: */
265: public static void revokeServiceContract(ServiceContract contract) {
266: ServiceContractImpl revokedContract = (ServiceContractImpl) contract;
267: revokedContract.revoke();
268: }
269:
270: /** Generate a new ServiceContractRelay
271: * @return ServiceContractRelay
272: **/
273: public ServiceContractRelay newServiceContractRelay(
274: MessageAddress provider, ServiceRequest request) {
275: ServiceContractRelayImpl serviceContractRelay = new ServiceContractRelayImpl(
276: request);
277: serviceContractRelay.setUID(myLDM.getUIDServer().nextUID());
278: serviceContractRelay.addTarget(provider);
279: return serviceContractRelay;
280: }
281:
282: /** Generate a new ServiceContractRelationship
283: * @return ServiceContractRelationship
284: **/
285: static public ServiceContractRelationship newServiceContractRelationship(
286: ServiceContractRelay relay, HasRelationships provider,
287: HasRelationships client) {
288: Collection contractPreferences = relay.getServiceContract()
289: .getServicePreferences();
290:
291: TimeSpan timeSpan = getTimeSpanFromPreferences(contractPreferences);
292:
293: return new ServiceContractRelationshipImpl(timeSpan
294: .getStartTime(), timeSpan.getEndTime(), relay
295: .getServiceContract().getServiceRole(), provider,
296: client, relay);
297: }
298:
299: /** Return the value associated with a Preference with the
300: * specified AspectType
301: *
302: * @param preferences Collection of Preferences (will ignore in the Collection
303: * which are not Preferences
304: * @param aspectType int specifying the AspectType of the Preference
305: * @return value, -1 if matching Preference not found.
306: */
307: static public double getPreference(Collection preferences,
308: int aspectType) {
309: double result = -1;
310: Preference preference = null;
311:
312: for (Iterator iterator = preferences.iterator(); iterator
313: .hasNext();) {
314: Object next = iterator.next();
315:
316: if (next instanceof Preference) {
317: Preference testPreference = (Preference) next;
318: if (testPreference.getAspectType() == aspectType) {
319: preference = testPreference;
320: break;
321: }
322: }
323: }
324:
325: if (preference != null) {
326: result = preference.getScoringFunction().getBest()
327: .getValue();
328: }
329: return result;
330: }
331:
332: /**
333: * Return the (long) value associated with a Time Preference with the
334: * specified AspectType, avoiding long->double->long type conversion.
335: *
336: * @param preferences Collection of Preferences (will ignore any in the Collection
337: * which are not Preferences)
338: * @param aspectType int specifying the AspectType of the Preference
339: * @return value, -1 if matching Preference not found.
340: */
341: static public long getTimePreference(Collection preferences,
342: int aspectType) {
343: long result = -1;
344: Preference preference = null;
345:
346: for (Iterator iterator = preferences.iterator(); iterator
347: .hasNext();) {
348: Object next = iterator.next();
349:
350: if (next instanceof Preference) {
351: Preference testPreference = (Preference) next;
352: if (testPreference.getAspectType() == aspectType) {
353: preference = testPreference;
354: break;
355: }
356: }
357: }
358:
359: if (preference != null) {
360: // Because we know this is a time, treat the value as a long
361: result = preference.getScoringFunction().getBest()
362: .getAspectValue().longValue();
363: }
364: return result;
365: }
366:
367: static public Preference findPreference(Collection preferences,
368: int aspectType) {
369: Preference preference = null;
370:
371: for (Iterator iterator = preferences.iterator(); iterator
372: .hasNext();) {
373: Object next = iterator.next();
374:
375: if (next instanceof Preference) {
376: Preference testPreference = (Preference) next;
377: if (testPreference.getAspectType() == aspectType) {
378: preference = testPreference;
379: break;
380: }
381: }
382: }
383:
384: return preference;
385: }
386:
387: public static TimeSpan getTimeSpanFromPreferences(
388: Collection preferences) {
389: long preferenceStart = getTimePreference(preferences,
390: Preference.START_TIME);
391: long preferenceEnd = getTimePreference(preferences,
392: Preference.END_TIME);
393:
394: if ((preferenceEnd == -1) || (preferenceStart == -1)) {
395: myLogger.error("getTimeSpanFromPreferences: "
396: + " unable to handle start and/or end time "
397: + " preferences from " + preferences);
398: return null;
399: }
400:
401: if (preferenceStart >= preferenceEnd) {
402: myLogger
403: .error("getTimeSpanFromPreferences: preferences - "
404: + preferences
405: + " - do specify a valid time span.\n"
406: + "Start - "
407: + preferenceStart
408: + "- >= End - "
409: + preferenceEnd
410: + " Returning null.");
411: return null;
412: }
413:
414: MutableTimeSpan timeSpan = new MutableTimeSpan();
415: timeSpan.setTimeSpan(preferenceStart, preferenceEnd);
416:
417: return timeSpan;
418: }
419:
420: public Collection createTimeSpanPreferences(TimeSpan timeSpan) {
421: ArrayList preferences = new ArrayList(2);
422:
423: AspectValue startTAV = TimeAspectValue.create(
424: AspectType.START_TIME, timeSpan.getStartTime());
425: ScoringFunction startScoreFunc = ScoringFunction
426: .createStrictlyAtValue(startTAV);
427: Preference startPreference = myLDM.getFactory().newPreference(
428: AspectType.START_TIME, startScoreFunc);
429:
430: AspectValue endTAV = TimeAspectValue.create(
431: AspectType.END_TIME, timeSpan.getEndTime());
432: ScoringFunction endScoreFunc = ScoringFunction
433: .createStrictlyAtValue(endTAV);
434: Preference endPreference = myLDM.getFactory().newPreference(
435: AspectType.END_TIME, endScoreFunc);
436:
437: preferences.add(startPreference);
438: preferences.add(endPreference);
439:
440: return preferences;
441: }
442: }
|