001: /*
002: * @(#)StateIteratorTask.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.ant;
030:
031: import org.apache.tools.ant.Project;
032: import org.apache.tools.ant.ProjectHelper;
033: import org.apache.tools.ant.Task;
034: import org.apache.tools.ant.TaskAdapter;
035: import org.apache.tools.ant.BuildException;
036: import org.apache.tools.ant.types.Reference;
037: import org.apache.tools.ant.types.DataType;
038: import org.apache.tools.ant.taskdefs.Definer;
039:
040: import java.util.Vector;
041: import java.util.Enumeration;
042:
043: import net.sourceforge.groboutils.mbtf.v1.IValidate;
044: import net.sourceforge.groboutils.mbtf.v1.ISystemFactory;
045: import net.sourceforge.groboutils.mbtf.v1.ISystem;
046: import net.sourceforge.groboutils.mbtf.v1.IErrors;
047: import net.sourceforge.groboutils.mbtf.v1.ITransition;
048: import net.sourceforge.groboutils.mbtf.v1.IState;
049: import net.sourceforge.groboutils.mbtf.v1.IPathGenerator;
050: import net.sourceforge.groboutils.mbtf.v1.IPath;
051: import net.sourceforge.groboutils.mbtf.v1.IPathParser;
052: import net.sourceforge.groboutils.mbtf.v1.TestHaltRuntimeException;
053:
054: import net.sourceforge.groboutils.mbtf.v1.engine.PathParserImpl;
055: import net.sourceforge.groboutils.mbtf.v1.engine.BreadthPathGenerator;
056:
057: import net.sourceforge.groboutils.mbtf.v1.assembler.AsmblState;
058: import net.sourceforge.groboutils.mbtf.v1.assembler.AsmblStateSet;
059: import net.sourceforge.groboutils.mbtf.v1.assembler.AsmblTransition;
060: import net.sourceforge.groboutils.mbtf.v1.assembler.AsmblTransitionSet;
061: import net.sourceforge.groboutils.mbtf.v1.assembler.Assembler;
062:
063: /**
064: * An ant datatype encapsulating the IState instance. This includes an
065: * <tt>execute()</tt> method to allow for this to be defined in a
066: * <typedef> declaration, rather than inside a target as a
067: * <datatype> declaration.
068: *
069: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
070: * @version $Date: 2003/02/10 22:52:25 $
071: * @since June 13, 2002
072: */
073: public class StateIteratorTask extends Task {
074: public static final String DEFAULT_ANT_NAME = "mbtf.stateiterator";
075:
076: private Vector transitions = new Vector();
077: private Vector states = new Vector();
078: private Reference systemFactory;
079: private int maxDepth = 4;
080:
081: public void setSystemFactoryRef(Reference ref) {
082: this .systemFactory = ref;
083: }
084:
085: public ISystemFactory getSystemFactory() throws BuildException {
086: ISystemFactory sys = null;
087: if (this .systemFactory != null) {
088: Object o = this .systemFactory.getReferencedObject(project);
089: if (o != null) {
090: if (o instanceof TaskAdapter) {
091: o = ((TaskAdapter) o).getProxy();
092: }
093: if (!(o instanceof ISystemFactory)) {
094: throw new BuildException(
095: "referenced object not of type ISystemFactory",
096: location);
097: }
098: sys = (ISystemFactory) o;
099: }
100: }
101: return sys;
102: }
103:
104: public void setMaxDepth(int depth) throws BuildException {
105: if (depth <= 0) {
106: throw new BuildException(
107: "invalid value for maximum depth: " + depth,
108: location);
109: }
110: this .maxDepth = depth;
111: }
112:
113: public int getMaxDepth() {
114: return this .maxDepth;
115: }
116:
117: public void addState(StateType state) {
118: if (state != null) {
119: this .states.addElement(state);
120: }
121: }
122:
123: public AsmblStateSet getStates()
124: throws BuildException
125: {
126: Enumeration enum = this .states.elements();
127: AsmblStateSet ass = new AsmblStateSet();
128: while (enum.hasMoreElements())
129: {
130: ass.addState( ((StateType)enum.nextElement()).createState() );
131: }
132:
133: return ass;
134: }
135:
136: public void addTransition(TransitionType trans) {
137: if (trans != null) {
138: this .transitions.addElement(trans);
139: }
140: }
141:
142: public AsmblTransitionSet getTransitions()
143: throws BuildException
144: {
145: Enumeration enum = this .transitions.elements();
146: AsmblTransitionSet ats = new AsmblTransitionSet();
147: while (enum.hasMoreElements())
148: {
149: ats.addTransition( ((TransitionType)enum.nextElement()).
150: createTransition() );
151: }
152: return ats;
153: }
154:
155: public void execute() throws BuildException {
156: IPathGenerator gen = createPathGenerator();
157: IPathParser parser = createPathParser();
158: ISystemFactory sysFactory = getSystemFactory();
159:
160: try {
161: IPath path = gen.getNextPath();
162: while (path.getDepth() <= getMaxDepth()) {
163: ISystem sys = sysFactory.createSystem();
164: if (sys == null) {
165: throw new BuildException(
166: "System factory returned a null factory.",
167: location);
168: }
169: IErrors err = parser.parsePath(path, sys);
170: }
171: } catch (TestHaltRuntimeException thre) {
172: throw new BuildException(thre.getMessage(), thre, location);
173: }
174: }
175:
176: public IPathGenerator createPathGenerator() throws BuildException {
177: try {
178: Assembler asm = new Assembler(getTransitions(), getStates());
179:
180: IState startStates[] = asm.getStartStates();
181: IState finalStates[] = asm.getFinalStates();
182:
183: return new BreadthPathGenerator(startStates, finalStates);
184: } catch (Exception e) {
185: throw new BuildException(e.getMessage(), e);
186: }
187: }
188:
189: protected IPathParser createPathParser() throws BuildException {
190: return new PathParserImpl();
191: }
192:
193: protected void processErrors(IErrors err) {
194: log(err.toString(), Project.MSG_WARN);
195: }
196: }
|