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.core.qos.frame;
028:
029: import java.util.Collection;
030: import java.util.Iterator;
031: import java.util.StringTokenizer;
032:
033: import org.cougaar.core.component.ServiceBroker;
034: import org.cougaar.core.qos.ca.ConnectionSpec;
035: import org.cougaar.core.qos.ca.CoordinationArtifact;
036: import org.cougaar.core.qos.ca.FacetImpl;
037: import org.cougaar.core.qos.ca.FactAssertion;
038: import org.cougaar.core.qos.ca.FactRevision;
039: import org.cougaar.core.qos.ca.RolePlayer;
040: import org.cougaar.core.service.BlackboardService;
041: import org.cougaar.core.service.LoggingService;
042: import org.cougaar.core.util.UID;
043:
044: /**
045: * This class is the abstract implementation of the "Producer" facet
046: * of the Frame CoordinationArtifact. That is, it creates or modifies
047: * {@link Frame}s, given facts.
048: *
049: * @see FrameCoordinationArtifactProvider
050: */
051: abstract public class FactToFrameFacetImpl extends FacetImpl {
052:
053: private LoggingService log;
054: private ServiceBroker sb;
055: private FrameSet frameSet;
056: private String[] xml_filenames;
057: private String set_name;
058: private boolean use_existing_frameset;
059:
060: protected FactToFrameFacetImpl(CoordinationArtifact owner,
061: ServiceBroker sb, ConnectionSpec spec, RolePlayer player) {
062: super (owner, sb, spec, player);
063: log = (LoggingService) sb.getService(this ,
064: LoggingService.class, null);
065: this .sb = sb;
066: String files = spec.ca_parameters.getProperty("files");
067: set_name = spec.ca_parameters.getProperty("frame-set");
068: String use_existing = spec.ca_parameters.getProperty(
069: "use-existing", "false");
070: use_existing_frameset = use_existing.equalsIgnoreCase("true");
071: if (files == null || set_name == null) {
072: throw new RuntimeException(
073: "No frame-sets and no frame-set-files!");
074: }
075:
076: StringTokenizer tk = new StringTokenizer(files, ",");
077: xml_filenames = new String[tk.countTokens()];
078: int i = 0;
079: while (tk.hasMoreTokens()) {
080: String fname = tk.nextToken();
081: if (log.isInfoEnabled())
082: log.info("FrameSet " + set_name + " file " + fname);
083: xml_filenames[i++] = fname;
084: }
085:
086: linkPlayer();
087: }
088:
089: abstract protected boolean isNewFrame(Object fact);
090:
091: abstract protected boolean isModifiedFrame(Object fact);
092:
093: abstract protected DataFrame getFrame(Object fact);
094:
095: abstract protected Object getModifications(Object fact);
096:
097: public void setupSubscriptions(BlackboardService bbs) {
098: }
099:
100: public void execute(BlackboardService bbs) {
101: }
102:
103: // Make the FrameSet available to subclasses
104: protected FrameSet getFrameSet() {
105: return frameSet;
106: }
107:
108: private synchronized void ensureFrameSet(BlackboardService bbs) {
109: if (frameSet != null)
110: return;
111:
112: FrameSetService fss = (FrameSetService) sb.getService(this ,
113: FrameSetService.class, null);
114: if (use_existing_frameset) {
115: frameSet = fss.findFrameSet(set_name, null);
116: if (frameSet != null) {
117: FrameSetParser fsp = new FrameSetParser(sb, bbs);
118: for (int i = 0; i < xml_filenames.length; i++)
119: fsp.parseFrameSetFile(set_name, xml_filenames[i],
120: frameSet);
121: }
122: } else {
123: frameSet = fss.loadFrameSet(set_name, xml_filenames, sb,
124: bbs);
125: }
126: }
127:
128: private void processNewFrame(Object fact) {
129: DataFrame frame = getFrame(fact);
130: frame.addToFrameSet(frameSet);
131: }
132:
133: private void handleChange(Frame.Change change) {
134: UID uid = change.getFrameUID();
135: Frame frame = frameSet.findFrame(uid);
136: if (frame == null) {
137: if (log.isInfoEnabled())
138: log.info("No match for uid " + uid);
139: return;
140: }
141:
142: String attr = change.getSlotName();
143: Object val = change.getValue();
144: if (log.isInfoEnabled())
145: log.info("Changing " + attr + " to " + val);
146: frame.setValue(attr, val);
147: }
148:
149: private void processModifiedFrame(Object fact) {
150: Object mods = getModifications(fact);
151:
152: if (mods instanceof Collection) {
153: Collection changes = (Collection) mods;
154: if (log.isInfoEnabled())
155: log.info("Processing " + changes.size() + " changes");
156:
157: Iterator itr = changes.iterator();
158: while (itr.hasNext()) {
159: Frame.Change change = (Frame.Change) itr.next();
160: handleChange(change);
161: }
162: } else if (mods instanceof Frame.Change) {
163: Frame.Change change = (Frame.Change) mods;
164: handleChange(change);
165: } else {
166: if (log.isWarnEnabled())
167: log
168: .warn("Frame Change "
169: + mods
170: + " is neither a Collection nor a Frame.Change");
171: }
172:
173: }
174:
175: public void processFactBase(BlackboardService bbs) {
176: if (!factsHaveChanged())
177: return;
178: ensureFrameSet(bbs);
179: if (frameSet == null)
180: return;
181: for (FactRevision frev = nextFact(); frev != null; frev = nextFact()) {
182: if (log.isDebugEnabled())
183: log.debug("Processing fact " + frev.getFact());
184: if (frev instanceof FactAssertion) {
185: Object fact = frev.getFact();
186: if (isNewFrame(fact)) {
187: processNewFrame(fact);
188: } else if (isModifiedFrame(fact)) {
189: processModifiedFrame(fact);
190: } else {
191: // subclass is using it for some other purpose
192: }
193: } else {
194: // no retractions yet
195: }
196: }
197: frameSet.processQueue();
198: }
199:
200: }
|