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;
028:
029: import org.cougaar.core.blackboard.Blackboard;
030: import org.cougaar.core.blackboard.CollectionSubscription;
031: import org.cougaar.core.blackboard.UniqueObjectSet;
032: import org.cougaar.core.domain.XPlan;
033: import org.cougaar.core.util.UID;
034: import org.cougaar.planning.ldm.asset.Asset;
035: import org.cougaar.planning.ldm.asset.AssetSet;
036: import org.cougaar.planning.ldm.plan.PlanElement;
037: import org.cougaar.planning.ldm.plan.PlanElementSet;
038: import org.cougaar.planning.ldm.plan.Task;
039: import org.cougaar.planning.ldm.plan.Workflow;
040: import org.cougaar.util.UnaryPredicate;
041:
042: /**
043: * Implementation of "planning" LogPlan.
044: */
045: public class LogPlanImpl implements LogPlan, XPlan {
046: private Blackboard blackboard;
047:
048: private static final UnaryPredicate planElementP = new PlanElementPredicate();
049:
050: private static class PlanElementPredicate implements UnaryPredicate {
051: public boolean execute(Object o) {
052: return (o instanceof PlanElement);
053: }
054: }
055:
056: /** is this a task object? **/
057: private static final UnaryPredicate taskP = new TaskPredicate();
058:
059: private static class TaskPredicate implements UnaryPredicate {
060: public boolean execute(Object o) {
061: return (o instanceof Task);
062: }
063: }
064:
065: /** is this an asset? **/
066: private static final UnaryPredicate assetP = new AssetPredicate();
067:
068: private static class AssetPredicate implements UnaryPredicate {
069: public boolean execute(Object o) {
070: return (o instanceof Asset);
071: }
072: }
073:
074: /**
075: * Private container for PlanElements only. Supports fast lookup of
076: * Task->PlanElement.
077: **/
078: PlanElementSet planElementSet = new PlanElementSet();
079: private CollectionSubscription planElementCollection;
080:
081: UniqueObjectSet taskSet = new UniqueObjectSet();
082: private CollectionSubscription taskCollection;
083:
084: AssetSet assetSet = new AssetSet();
085: private CollectionSubscription assetCollection;
086:
087: public void setupSubscriptions(Blackboard blackboard) {
088: this .blackboard = blackboard;
089: planElementCollection = new CollectionSubscription(
090: planElementP, planElementSet);
091: blackboard.subscribe(planElementCollection);
092:
093: taskCollection = new CollectionSubscription(taskP, taskSet);
094: blackboard.subscribe(taskCollection);
095:
096: assetCollection = new CollectionSubscription(assetP, assetSet);
097: blackboard.subscribe(assetCollection);
098: }
099:
100: public PlanElement findPlanElement(Task task) {
101: return planElementSet.findPlanElement(task);
102: }
103:
104: /** @deprecated Use findPlanElement(UID uid) instead. **/
105: public PlanElement findPlanElement(String id) {
106: return planElementSet.findPlanElement(UID.toUID(id));
107: }
108:
109: public PlanElement findPlanElement(UID uid) {
110: return planElementSet.findPlanElement(uid);
111: }
112:
113: public Task findTask(Task task) {
114: return (Task) taskSet.findUniqueObject(task.getUID());
115: }
116:
117: /** @deprecated Use findTask(UID uid) instead. **/
118: public Task findTask(String id) {
119: return findTask(UID.toUID(id));
120: }
121:
122: public Task findTask(UID uid) {
123: return (Task) taskSet.findUniqueObject(uid);
124: }
125:
126: public Asset findAsset(Asset asset) {
127: return assetSet.findAsset(asset);
128: }
129:
130: public Asset findAsset(String id) {
131: return assetSet.findAsset(id);
132: }
133:
134: /** Counters for different types of logplan objects for metrics **/
135: private int planelemCnt = 0;
136: private int workflowCnt = 0;
137: private int taskCnt = 0;
138: private int assetCnt = 0;
139:
140: // Accessors for metrics counts
141: public int getLogPlanCount() {
142: return assetCnt + taskCnt + workflowCnt + planelemCnt;
143: }
144:
145: public int getAssetCount() {
146: return assetSet.size();
147: }
148:
149: public int getTaskCount() {
150: return taskSet.size();
151: }
152:
153: public int getPlanElementCount() {
154: return planElementSet.size();
155: }
156:
157: private static final UnaryPredicate workflowPredicate = new WorkflowPredicate();
158:
159: private static class WorkflowPredicate implements UnaryPredicate {
160: public boolean execute(Object o) {
161: return (o instanceof Workflow);
162: }
163: }
164:
165: public int getWorkflowCount() {
166: // no subscription for workflows?
167: return blackboard.countBlackboard(workflowPredicate);
168: }
169:
170: // Increment counts by given amount
171: public void incAssetCount(int inc) {
172: assetCnt += inc;
173: }
174:
175: public void incTaskCount(int inc) {
176: taskCnt += inc;
177: }
178:
179: public void incPlanElementCount(int inc) {
180: planelemCnt += inc;
181: }
182:
183: public void incWorkflowCount(int inc) {
184: workflowCnt += inc;
185: }
186: }
|