001: /*
002: * <copyright>
003: *
004: * Copyright 2000-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.tools.csmart.runtime.plugin;
028:
029: import org.cougaar.core.blackboard.Subscription;
030: import org.cougaar.core.component.ServiceRevokedEvent;
031: import org.cougaar.core.component.ServiceRevokedListener;
032: import org.cougaar.core.plugin.ComponentPlugin;
033: import org.cougaar.core.service.BlackboardService;
034: import org.cougaar.core.service.DomainService;
035: import org.cougaar.core.service.LoggingService;
036: import org.cougaar.core.service.UIDService;
037: import org.cougaar.core.util.UID;
038: import org.cougaar.planning.ldm.PlanningFactory;
039: import org.cougaar.util.StateModelException;
040: import org.cougaar.util.UnaryPredicate;
041:
042: /**
043: * CSMART Base Plugin. Provide convenience methods.<br>
044: * Get a hook to the <code>PlanningFactory</code>
045: */
046: public abstract class CSMARTPlugin extends ComponentPlugin {
047:
048: //
049: // CSMART hooks
050: //
051:
052: private DomainService domainService = null;
053: private UIDService uidService = null;
054: protected LoggingService log = null;
055:
056: /** Root LDM Factory */
057: protected PlanningFactory theLDMF;
058:
059: /** Identifier of Plugin */
060: private UID id = null;
061:
062: //
063: // constructor
064: //
065: public CSMARTPlugin() {
066: }
067:
068: /**
069: * Get the UID for this Plugin (same as its PluginAsset)
070: * Only to be called after the Plugin has been loaded
071: *
072: * @return an <code>UID</code> value
073: */
074: public UID getUID() {
075: return this .id;
076: }
077:
078: //
079: // load extensions
080: //
081:
082: /** Watch for who we are plugging into **/
083: public void load() throws StateModelException {
084: // FIXME!!! What about ThreadingChoice??
085: super .load();
086: uidService = (UIDService) getServiceBroker().getService(this ,
087: UIDService.class, null);
088: domainService = (DomainService) getServiceBroker().getService(
089: this , DomainService.class, null);
090: log = (LoggingService) getServiceBroker().getService(this ,
091: LoggingService.class, null);
092:
093: if (log == null) {
094: // Print to STDERR?
095: log = LoggingService.NULL;
096: }
097:
098: // Also get the Root factory
099: this .theLDMF = ((domainService != null) ? ((PlanningFactory) domainService
100: .getFactory("planning"))
101: : null);
102:
103: // Give each Plugin a UID to uniquely identify it
104: this .id = uidService.nextUID();
105:
106: } // end of load()
107:
108: // A couple convenience functions to provide backwards compatibility
109: protected Subscription subscribe(UnaryPredicate pred) {
110: return getBlackboardService().subscribe(pred);
111: }
112:
113: //
114: // Timer related functions
115: //
116:
117: //
118: // LogPlan changes publishing
119: //
120:
121: // A convenience backwards-compatibility method
122: protected final void publishAdd(Object o) {
123: getBlackboardService().publishAdd(o);
124: }
125:
126: protected final void publishAdd(Object o, UnaryPredicate pred) {
127: if ((pred == null) || (pred.execute(o))) {
128: getBlackboardService().publishAdd(o);
129: } else {
130: // pred cancelled the add
131: }
132: }
133:
134: // Convenience backwards-compatibility
135: protected final void publishChange(Object o) {
136: getBlackboardService().publishChange(o);
137: }
138:
139: /**
140: * @see #publishAdd(Object,UnaryPredicate)
141: */
142: protected final void publishChange(Object o, UnaryPredicate pred) {
143: if ((pred == null) || (pred.execute(o))) {
144: getBlackboardService().publishChange(o);
145: } else {
146: // pred cancelled the change
147: }
148: }
149:
150: // Convenience
151: protected final void publishRemove(Object o) {
152: getBlackboardService().publishRemove(o);
153: }
154:
155: /**
156: * @see #publishAdd(Object,UnaryPredicate)
157: */
158: protected final void publishRemove(Object o, UnaryPredicate pred) {
159: if ((pred == null) || (pred.execute(o))) {
160: getBlackboardService().publishRemove(o);
161: } else {
162: // pred cancelled the remove
163: }
164: }
165:
166: /** Called during initialization to set up subscriptions.
167: * More precisely, called in the plugin's Thread of execution
168: * inside of a transaction before execute will ever be called.
169: **/
170: protected abstract void setupSubscriptions();
171:
172: /**
173: * Called inside of an open transaction whenever the plugin was
174: * explicitly told to run or when there are changes to any of
175: * our subscriptions.
176: **/
177: protected abstract void execute();
178:
179: } // end of CSMARTPlugin.java
|