001: /*
002: * @(#)Assembler.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.IAction;
033: import net.sourceforge.groboutils.mbtf.v1.IValidate;
034: import net.sourceforge.groboutils.mbtf.v1.ITransition;
035:
036: import net.sourceforge.groboutils.mbtf.v1.engine.StateImpl;
037: import net.sourceforge.groboutils.mbtf.v1.engine.TransitionImpl;
038:
039: import java.util.Vector;
040: import java.util.Hashtable;
041: import java.util.Enumeration;
042:
043: /**
044: * Helper class which transforms the AsmblState and AsmblTransition instances
045: * into a coherent IState machine.
046: *
047: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
048: * @version $Date: 2003/02/10 22:52:25 $
049: * @since June 13, 2002
050: */
051: public class Assembler {
052: private Hashtable nameToIState;
053: private Vector startStates;
054: private Vector finalStates;
055:
056: /**
057: *
058: */
059: public Assembler(AsmblTransitionSet trans, AsmblStateSet states) {
060: if (trans == null || states == null) {
061: throw new IllegalArgumentException("no null args");
062: }
063: parseStates(trans, states);
064: }
065:
066: /**
067: * Returns a list of all States passed in to the constructor.
068: */
069: public IState[] getStates()
070: {
071: IState s[] = new IState[ this .nameToIState.size() ];
072: Enumeration enum = this .nameToIState.elements();
073: for (int i = 0; enum.hasMoreElements(); ++i)
074: {
075: s[i] = (IState)enum.nextElement();
076: }
077: return s;
078: }
079:
080: /**
081: *
082: */
083: public IState[] getStartStates() {
084: IState s[] = new IState[this .startStates.size()];
085: this .startStates.copyInto(s);
086: return s;
087: }
088:
089: /**
090: *
091: */
092: public IState[] getFinalStates() {
093: IState s[] = new IState[this .finalStates.size()];
094: this .finalStates.copyInto(s);
095: return s;
096: }
097:
098: //-------------------------------------------------------------------------
099:
100: protected void parseStates(AsmblTransitionSet trans,
101: AsmblStateSet stateSet) {
102: populateIStates(trans, stateSet.getStates());
103:
104: // Now load all the transitions destination states correctly
105: trans.setDestinationStates(this .nameToIState);
106: }
107:
108: protected void populateIStates(AsmblTransitionSet trans,
109: AsmblState[] as) {
110: this .nameToIState = new Hashtable();
111: this .startStates = new Vector();
112: this .finalStates = new Vector();
113:
114: for (int i = as.length; --i >= 0;) {
115: addAsmblState(as[i], as[i].createState(trans));
116: }
117: }
118:
119: protected void addAsmblState(AsmblState as, IState s) {
120: this .nameToIState.put(s.getName(), s);
121:
122: if (as.isStartState()) {
123: this .startStates.addElement(s);
124: }
125:
126: if (as.isFinalState()) {
127: this .finalStates.addElement(s);
128: }
129: }
130:
131: //-------------------------------------------------------------------------
132:
133: protected IState getState(String name) {
134: return (IState) this.nameToIState.get(name);
135: }
136:
137: }
|