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: /*
028: * File: DMPIClusterStartup.java
029: * Heavily modified from original to be an LDMPlugin
030: */
031:
032: package org.cougaar.glm.plugin.completion;
033:
034: import org.cougaar.core.blackboard.CollectionSubscription;
035: import org.cougaar.glm.ldm.Constants;
036: import org.cougaar.glm.ldm.oplan.Oplan;
037: import org.cougaar.mlm.plugin.organization.GLSInitServlet;
038: import org.cougaar.planning.ldm.plan.Task;
039: import org.cougaar.planning.plugin.completion.CompletionSocietyPlugin;
040: import org.cougaar.util.UnaryPredicate;
041:
042: public class GLMCompletionSocietyPlugin extends CompletionSocietyPlugin {
043: private static final long DEFAULT_SEND_OPLAN_DELAY = 60000L;
044: private static final long DEFAULT_PUBLISH_GLS_DELAY = 10000L;
045: private static final long DEFAULT_WAIT_GLS_DELAY = 1000L;
046: private static final String forRoot = "ForRoot";
047: private long SEND_OPLAN_DELAY = DEFAULT_SEND_OPLAN_DELAY;
048: private long PUBLISH_GLS_DELAY = DEFAULT_PUBLISH_GLS_DELAY;
049: private long WAIT_GLS_DELAY = DEFAULT_WAIT_GLS_DELAY;
050: private long timeout;
051: private CollectionSubscription oplanSubscription;
052: private CollectionSubscription glsSubscription;
053: private static UnaryPredicate oplanPredicate = new UnaryPredicate() {
054: public boolean execute(Object o) {
055: return (o instanceof Oplan);
056: }
057: };
058:
059: /**
060: * This predicate selects for root tasks injected by the GLSGUIInitPlugin
061: **/
062: private UnaryPredicate glsPredicate = new UnaryPredicate() {
063: public boolean execute(Object o) {
064: if (!(o instanceof Task))
065: return false;
066: Task task = (Task) o;
067: if (!task.getVerb().equals(Constants.Verb.GETLOGSUPPORT))
068: return false;
069: if (!task.getSource().equals(getAgentIdentifier()))
070: return false;
071: if (!task.getDestination().equals(getAgentIdentifier()))
072: return false;
073: return (task.getPrepositionalPhrase(forRoot) != null);
074: }
075: };
076: private CompletionAction sendOplanAction = new CompletionAction() {
077: public boolean checkCompletion(boolean haveLaggard) {
078: if (haveLaggard) {
079: timeout = now + SEND_OPLAN_DELAY;
080: } else if (now > timeout) {
081: sendOplan();
082: timeout = now + PUBLISH_GLS_DELAY;
083: return true;
084: }
085: return false;
086: }
087:
088: public String toString() {
089: return "CompletionAction(sendOplan)";
090: }
091: };
092: private CompletionAction publishGLSAction = new CompletionAction() {
093: public boolean checkCompletion(boolean haveLaggard) {
094: if (haveLaggard) {
095: timeout = now + WAIT_GLS_DELAY;
096: } else if (now > timeout) {
097: publishGLS();
098: return true;
099: }
100: return false;
101: }
102:
103: public String toString() {
104: return "CompletionAction(publishGLS)";
105: }
106: };
107: private CompletionAction waitGLSAction = new CompletionAction() {
108: public boolean checkCompletion(boolean haveLaggard) {
109: if (haveLaggard) {
110: timeout = now + PUBLISH_GLS_DELAY;
111: } else if (now > timeout) {
112: return !blackboard.query(glsPredicate).isEmpty();
113: }
114: return false;
115: }
116:
117: public String toString() {
118: return "CompletionAction(waitGLS)";
119: }
120: };
121: private CompletionAction[] myCompletionActions = {
122: // sendOplanAction,
123: // publishGLSAction
124: waitGLSAction };
125:
126: public void setupSubscriptions() {
127: oplanSubscription = (CollectionSubscription) blackboard
128: .subscribe(oplanPredicate, false);
129: glsSubscription = (CollectionSubscription) blackboard
130: .subscribe(glsPredicate, false);
131: super .setupSubscriptions();
132: }
133:
134: protected CompletionAction[] getCompletionActions() {
135: return myCompletionActions;
136: }
137:
138: private void sendOplan() {
139: GLSInitServlet.Request req = new GLSInitServlet.Request(
140: GLSInitServlet.GETOPINFO);
141: blackboard.publishAdd(req);
142: }
143:
144: private void publishGLS() {
145: GLSInitServlet.Request req = new GLSInitServlet.Request(
146: GLSInitServlet.PUBLISHGLS);
147: blackboard.publishAdd(req);
148: }
149: }
|