001: /*
002: * @(#)AsmblTransitionSet.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Part of the GroboUtils package at:
009: * http://groboutils.sourceforge.net
010: *
011: * Permission is hereby granted, free of charge, to any person obtaining a
012: * copy of this software and associated documentation files (the "Software"),
013: * to deal in the Software without restriction, including without limitation
014: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
015: * and/or sell copies of the Software, and to permit persons to whom the
016: * Software is furnished to do so, subject to the following conditions:
017: *
018: * The above copyright notice and this permission notice shall be included in
019: * all copies or substantial portions of the Software.
020: *
021: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
022: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
024: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
025: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
026: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
027: * DEALINGS IN THE SOFTWARE.
028: */
029: package net.sourceforge.groboutils.mbtf.v1.assembler;
030:
031: import net.sourceforge.groboutils.mbtf.v1.IState;
032: import net.sourceforge.groboutils.mbtf.v1.ITransition;
033: import net.sourceforge.groboutils.mbtf.v1.engine.TransitionImpl;
034:
035: import java.util.Hashtable;
036: import java.util.Enumeration;
037: import java.util.Vector;
038:
039: /**
040: * Contains the set of transitions in their pre-assembled state. Used for
041: * accessing a collection of transitions.
042: *
043: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
044: * @version $Date: 2003/02/10 22:52:25 $
045: * @since June 13, 2002
046: */
047: public class AsmblTransitionSet {
048: private Hashtable nameToTrans = new Hashtable();
049:
050: /**
051: *
052: */
053: public AsmblTransitionSet() {
054: // do nothing
055: }
056:
057: /**
058: *
059: */
060: public void addTransition(AsmblTransition trans) {
061: if (trans != null) {
062: this .nameToTrans.put(trans.getName(), trans);
063: }
064: }
065:
066: /**
067: *
068: */
069: public void addTransitions(AsmblTransition[] trans) {
070: if (trans != null) {
071: for (int i = trans.length; --i >= 0;) {
072: addTransition(trans[i]);
073: }
074: }
075: }
076:
077: /**
078: *
079: */
080: public AsmblTransition getTransition(String name) {
081: return (AsmblTransition) this .nameToTrans.get(name);
082: }
083:
084: /**
085: *
086: */
087: public AsmblTransition[] getTransitions()
088: {
089: AsmblTransition as[] = new AsmblTransition[ this .nameToTrans.size() ];
090: Enumeration enum = this .nameToTrans.elements();
091: for (int i = 0; enum.hasMoreElements(); ++i)
092: {
093: as[i] = (AsmblTransition)enum.nextElement();
094: }
095: return as;
096: }
097:
098: /**
099: * Sets the destination state for every transition. If a transition's
100: * destination state is not in the set, then an IllegalArgumentException
101: * is thrown.
102: */
103: public void setDestinationStates(Hashtable nameToIState) {
104: AsmblTransition[] ats = getTransitions();
105: for (int i = ats.length; --i >= 0;) {
106: IState s = (IState) nameToIState.get(ats[i]
107: .getNextStateName());
108: if (s == null) {
109: throw new IllegalArgumentException("Transition '"
110: + ats[i].getName()
111: + "' expects existing state '"
112: + ats[i].getNextStateName() + "'");
113: }
114: ats[i].setDestinationState(s);
115: }
116: }
117: }
|