001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.scxml;
018:
019: import java.io.Serializable;
020: import java.util.Collections;
021: import java.util.HashMap;
022: import java.util.HashSet;
023: import java.util.Map;
024: import java.util.Set;
025:
026: import org.apache.commons.scxml.invoke.Invoker;
027: import org.apache.commons.scxml.invoke.InvokerException;
028: import org.apache.commons.scxml.model.Datamodel;
029: import org.apache.commons.scxml.model.History;
030: import org.apache.commons.scxml.model.TransitionTarget;
031:
032: /**
033: * The <code>SCInstance</code> performs book-keeping functions for
034: * a particular execution of a state chart represented by a
035: * <code>SCXML</code> object.
036: */
037: public class SCInstance implements Serializable {
038:
039: /**
040: * Serial version UID.
041: */
042: private static final long serialVersionUID = 1L;
043:
044: /**
045: * The notification registry.
046: */
047: private NotificationRegistry notificationRegistry;
048:
049: /**
050: * The <code>Map</code> of <code>Context</code>s per
051: * <code>TransitionTarget</code>.
052: */
053: private Map contexts;
054:
055: /**
056: * The <code>Map</code> of last known configurations per
057: * <code>History</code>.
058: */
059: private Map histories;
060:
061: /**
062: * The <code>Invoker</code> classes <code>Map</code>, keyed by
063: * <invoke> target types (specified using "targettype" attribute).
064: */
065: private Map invokerClasses;
066:
067: /**
068: * The <code>Map</code> of active <code>Invoker</code>s, keyed by
069: * (leaf) <code>State</code>s.
070: */
071: private Map invokers;
072:
073: /**
074: * The evaluator for expressions.
075: */
076: private Evaluator evaluator;
077:
078: /**
079: * The root context.
080: */
081: private Context rootContext;
082:
083: /**
084: * The owning state machine executor.
085: */
086: private SCXMLExecutor executor;
087:
088: /**
089: * Constructor.
090: *
091: * @param executor The executor that this instance is attached to.
092: */
093: SCInstance(final SCXMLExecutor executor) {
094: this .notificationRegistry = new NotificationRegistry();
095: this .contexts = Collections.synchronizedMap(new HashMap());
096: this .histories = Collections.synchronizedMap(new HashMap());
097: this .invokerClasses = Collections
098: .synchronizedMap(new HashMap());
099: this .invokers = Collections.synchronizedMap(new HashMap());
100: this .evaluator = null;
101: this .rootContext = null;
102: this .executor = executor;
103: }
104:
105: /**
106: * Get the <code>Evaluator</code>.
107: *
108: * @return The evaluator.
109: */
110: public Evaluator getEvaluator() {
111: return evaluator;
112: }
113:
114: /**
115: * Set the <code>Evaluator</code>.
116: *
117: * @param evaluator The evaluator.
118: */
119: void setEvaluator(final Evaluator evaluator) {
120: this .evaluator = evaluator;
121: }
122:
123: /**
124: * Get the root context.
125: *
126: * @return The root context.
127: */
128: public Context getRootContext() {
129: if (rootContext == null && evaluator != null) {
130: rootContext = evaluator.newContext(null);
131: }
132: return rootContext;
133: }
134:
135: /**
136: * Set the root context.
137: *
138: * @param context The root context.
139: */
140: void setRootContext(final Context context) {
141: this .rootContext = context;
142: }
143:
144: /**
145: * Get the notification registry.
146: *
147: * @return The notification registry.
148: */
149: public NotificationRegistry getNotificationRegistry() {
150: return notificationRegistry;
151: }
152:
153: /**
154: * Set the notification registry.
155: *
156: * @param notifRegistry The notification registry.
157: */
158: void setNotificationRegistry(
159: final NotificationRegistry notifRegistry) {
160: this .notificationRegistry = notifRegistry;
161: }
162:
163: /**
164: * Get the <code>Context</code> for this <code>TransitionTarget</code>.
165: * If one is not available it is created.
166: *
167: * @param transitionTarget The TransitionTarget.
168: * @return The Context.
169: */
170: public Context getContext(final TransitionTarget transitionTarget) {
171: Context context = (Context) contexts.get(transitionTarget);
172: if (context == null) {
173: TransitionTarget parent = transitionTarget.getParent();
174: if (parent == null) {
175: // docroot
176: context = evaluator.newContext(getRootContext());
177: } else {
178: context = evaluator.newContext(getContext(parent));
179: }
180: Datamodel datamodel = transitionTarget.getDatamodel();
181: SCXMLHelper.cloneDatamodel(datamodel, context, evaluator,
182: null);
183: contexts.put(transitionTarget, context);
184: }
185: return context;
186: }
187:
188: /**
189: * Get the <code>Context</code> for this <code>TransitionTarget</code>.
190: * May return <code>null</code>.
191: *
192: * @param transitionTarget The <code>TransitionTarget</code>.
193: * @return The Context.
194: */
195: Context lookupContext(final TransitionTarget transitionTarget) {
196: return (Context) contexts.get(transitionTarget);
197: }
198:
199: /**
200: * Set the <code>Context</code> for this <code>TransitionTarget</code>.
201: *
202: * @param transitionTarget The TransitionTarget.
203: * @param context The Context.
204: */
205: void setContext(final TransitionTarget transitionTarget,
206: final Context context) {
207: contexts.put(transitionTarget, context);
208: }
209:
210: /**
211: * Get the last configuration for this history.
212: *
213: * @param history The history.
214: * @return Returns the lastConfiguration.
215: */
216: public Set getLastConfiguration(final History history) {
217: Set lastConfiguration = (Set) histories.get(history);
218: if (lastConfiguration == null) {
219: lastConfiguration = new HashSet();
220: histories.put(history, lastConfiguration);
221: }
222: return lastConfiguration;
223: }
224:
225: /**
226: * Set the last configuration for this history.
227: *
228: * @param history The history.
229: * @param lc The lastConfiguration to set.
230: */
231: public void setLastConfiguration(final History history, final Set lc) {
232: Set lastConfiguration = getLastConfiguration(history);
233: lastConfiguration.clear();
234: lastConfiguration.addAll(lc);
235: }
236:
237: /**
238: * Check whether we have prior history.
239: *
240: * @param history The history.
241: * @return Whether we have a non-empty last configuration
242: */
243: public boolean isEmpty(final History history) {
244: Set lastConfiguration = (Set) histories.get(history);
245: if (lastConfiguration == null || lastConfiguration.isEmpty()) {
246: return true;
247: }
248: return false;
249: }
250:
251: /**
252: * Resets the history state.
253: *
254: * @param history The history.
255: * @see org.apache.commons.scxml.SCXMLExecutor#reset()
256: */
257: public void reset(final History history) {
258: Set lastConfiguration = (Set) histories.get(history);
259: if (lastConfiguration != null) {
260: lastConfiguration.clear();
261: }
262: }
263:
264: /**
265: * Get the {@link SCXMLExecutor} this instance is attached to.
266: *
267: * @return The SCXMLExecutor this instance is attached to.
268: * @see org.apache.commons.scxml.SCXMLExecutor
269: */
270: public SCXMLExecutor getExecutor() {
271: return executor;
272: }
273:
274: /**
275: * Register an {@link Invoker} class for this target type.
276: *
277: * @param targettype The target type (specified by "targettype"
278: * attribute of <invoke> tag).
279: * @param invokerClass The <code>Invoker</code> <code>Class</code>.
280: */
281: void registerInvokerClass(final String targettype,
282: final Class invokerClass) {
283: invokerClasses.put(targettype, invokerClass);
284: }
285:
286: /**
287: * Remove the {@link Invoker} class registered for this target
288: * type (if there is one registered).
289: *
290: * @param targettype The target type (specified by "targettype"
291: * attribute of <invoke> tag).
292: */
293: void unregisterInvokerClass(final String targettype) {
294: invokerClasses.remove(targettype);
295: }
296:
297: /**
298: * Get the {@link Invoker} for this {@link TransitionTarget}.
299: * May return <code>null</code>. A non-null <code>Invoker</code> will be
300: * returned if and only if the <code>TransitionTarget</code> is
301: * currently active and contains an <invoke> child.
302: *
303: * @param targettype The type of the target being invoked.
304: * @return An {@link Invoker} for the specified type, if an
305: * invoker class is registered against that type,
306: * <code>null</code> otherwise.
307: * @throws InvokerException When a suitable {@link Invoker} cannot
308: * be instantiated.
309: */
310: public Invoker newInvoker(final String targettype)
311: throws InvokerException {
312: Class invokerClass = (Class) invokerClasses.get(targettype);
313: if (invokerClass == null) {
314: throw new InvokerException("No Invoker registered for "
315: + "targettype \"" + targettype + "\"");
316: }
317: Invoker invoker = null;
318: try {
319: invoker = (Invoker) invokerClass.newInstance();
320: } catch (InstantiationException ie) {
321: throw new InvokerException(ie.getMessage(), ie.getCause());
322: } catch (IllegalAccessException iae) {
323: throw new InvokerException(iae.getMessage(), iae.getCause());
324: }
325: return invoker;
326: }
327:
328: /**
329: * Get the {@link Invoker} for this {@link TransitionTarget}.
330: * May return <code>null</code>. A non-null {@link Invoker} will be
331: * returned if and only if the {@link TransitionTarget} is
332: * currently active and contains an <invoke> child.
333: *
334: * @param transitionTarget The <code>TransitionTarget</code>.
335: * @return The Invoker.
336: */
337: public Invoker getInvoker(final TransitionTarget transitionTarget) {
338: return (Invoker) invokers.get(transitionTarget);
339: }
340:
341: /**
342: * Set the {@link Invoker} for this {@link TransitionTarget}.
343: *
344: * @param transitionTarget The TransitionTarget.
345: * @param invoker The Invoker.
346: */
347: public void setInvoker(final TransitionTarget transitionTarget,
348: final Invoker invoker) {
349: invokers.put(transitionTarget, invoker);
350: }
351:
352: /**
353: * Return the Map of {@link Invoker}s currently "active".
354: *
355: * @return The map of invokers.
356: */
357: public Map getInvokers() {
358: return invokers;
359: }
360:
361: }
|