01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.mlm.examples;
28:
29: import org.cougaar.glm.ldm.asset.PhysicalAsset;
30: import org.cougaar.planning.ldm.PrototypeProvider;
31: import org.cougaar.planning.ldm.asset.Asset;
32: import org.cougaar.planning.ldm.asset.NewTypeIdentificationPG;
33: import org.cougaar.planning.plugin.legacy.PluginAdapter;
34:
35: /** This plugin knows how to build protypes with typeids
36: * beginning with "NSN/". Actually, it only understands
37: * NSNs in the range of 100-199, which (for our purposes)
38: * are all Widgets, which are of the class PhysicalAsset.
39: **/
40:
41: public class PrototypeProviderPluginExample
42: // no need to inherit any fancy plugin functionality
43: extends PluginAdapter
44: // advertize as a prototypeProvider
45: implements PrototypeProvider {
46:
47: /** This method is called by ldm.getPrototype() methods
48: * after first checking the cache. If the right thing
49: * is not found in the cache, the ldm calls prototype providers
50: * in order until one returns an asset.
51: **/
52: public Asset getPrototype(String aTypeName, Class hint) {
53: // first, see if we can filter based on the hint
54: if (hint != null &&
55: // we only make PhysicalAssets.
56: !(hint.isAssignableFrom(PhysicalAsset.class)))
57: return null;
58:
59: // decide if this is the kind of prototype we can handle.
60: if (aTypeName.startsWith("NSN/")) {
61: Asset proto = makeNSNPrototype(aTypeName);
62: // couldn't handle this typename after all
63: if (proto == null)
64: return null;
65:
66: // let propertyProviders fill in details
67: theLDM.fillProperties(proto);
68: // cache it so we don't get called again on this object
69: theLDM.cachePrototype(aTypeName, proto);
70: // return it
71: return proto;
72: } else {
73: return null;
74: }
75: }
76:
77: private Asset makeNSNPrototype(String typeid) {
78: // one would normally have a database lookup or somesuch.
79: int nsn = Integer.parseInt(typeid.substring(4));
80:
81: Asset proto = null;
82: // is this a Widget?
83: if (nsn >= 100 && nsn <= 199) {
84: proto = theLDMF.createPrototype(typeid, "PhysicalAsset");
85: ((NewTypeIdentificationPG) proto.getTypeIdentificationPG())
86: .setNomenclature("Widget Type " + (nsn - 100));
87: }
88: return proto;
89: }
90:
91: }
|