KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > mbtf > v1 > engine > PathParserImpl


1 /*
2  * @(#)PathParserImpl.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Part of the GroboUtils package at:
9  * http://groboutils.sourceforge.net
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  */

29 package net.sourceforge.groboutils.mbtf.v1.engine;
30
31
32 import net.sourceforge.groboutils.mbtf.v1.IPath;
33 import net.sourceforge.groboutils.mbtf.v1.IState;
34 import net.sourceforge.groboutils.mbtf.v1.IAction;
35 import net.sourceforge.groboutils.mbtf.v1.ISystem;
36 import net.sourceforge.groboutils.mbtf.v1.IErrors;
37 import net.sourceforge.groboutils.mbtf.v1.IValidate;
38 import net.sourceforge.groboutils.mbtf.v1.ITransition;
39 import net.sourceforge.groboutils.mbtf.v1.IPathParser;
40 import net.sourceforge.groboutils.mbtf.v1.IPathHistory;
41 import net.sourceforge.groboutils.mbtf.v1.IPathIterator;
42 import net.sourceforge.groboutils.mbtf.v1.TestHaltRuntimeException;
43 import net.sourceforge.groboutils.mbtf.v1.TestFailRuntimeException;
44
45 import org.apache.log4j.Logger;
46
47
48 /**
49  * Knows how to parse an IPath instance, and correctly follow the transitions
50  * and record errors.
51  *
52  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
53  * @version $Date: 2003/02/10 22:52:26 $
54  * @since June 12, 2002
55  */

56 public class PathParserImpl implements IPathParser
57 {
58     private static final Logger LOG = Logger.getLogger( PathParserImpl.class );
59     
60     /**
61      * @exception TestHaltRuntimeException if the path validation reports
62      * a <tt>halt()</tt> to <tt>errors</tt>.
63      */

64     public IErrors parsePath( IPath path, ISystem sys )
65     {
66         ErrorsImpl ei = createErrors();
67         PathHistoryImpl phi = createPathHistory();
68         
69         IState currentState = path.getStartState();
70         IPathIterator pi = path.iterator();
71         while (currentState != null
72                 && !ei.isHaltPath())
73         {
74             ErrorsImpl testErrs = createErrors();
75             try
76             {
77                 testErrs.setCurrentPathHistory( phi );
78                 LOG.debug( "Entering state "+currentState );
79                 phi.addState( currentState );
80                 LOG.debug( "Validating state "+currentState );
81                 validate( currentState, testErrs, phi, sys );
82                 currentState = null;
83                 if (!testErrs.isHaltPath() && pi.hasNext())
84                 {
85                     ITransition trans = pi.nextTransition();
86                     phi.addTransition( trans );
87                     LOG.debug( "Vaidatling transition "+trans );
88                     validate( trans, testErrs, phi, sys );
89                     if (!testErrs.isHaltPath())
90                     {
91                         IAction act = trans.getAction();
92                         LOG.debug( "Start Performing action "+act );
93                         act.performAction( sys, testErrs );
94                         LOG.debug( "End Performing action "+act );
95                         currentState = trans.getDestinationState();
96                     }
97                 }
98             }
99             catch (TestHaltRuntimeException thre)
100             {
101                 // should have already been caught
102
throw thre;
103             }
104             catch (ThreadDeath JavaDoc td)
105             {
106                 // never catch these. never.
107
throw td;
108             }
109             catch (TestFailRuntimeException tfre)
110             {
111                 // The error was already added to the error set.
112
// This exception ends this path, but the exception is
113
// not propigated.
114
}
115             catch (Throwable JavaDoc t)
116             {
117                 testErrs.addError( "Uncaught exception", t );
118                 if (t instanceof RuntimeException JavaDoc)
119                 {
120                     throw (RuntimeException JavaDoc)t;
121                 }
122                 t.printStackTrace();
123                 throw new IllegalStateException JavaDoc( t.toString() );
124             }
125             finally
126             {
127                 ei.addErrors( testErrs );
128             }
129         }
130         
131         return ei;
132     }
133     
134     
135     protected void validate( IState state, ErrorsImpl ei, PathHistoryImpl phi,
136             ISystem sys )
137             throws CloneNotSupportedException JavaDoc
138     {
139         IValidate[] v = state.getValidates();
140         if (v.length <= 0)
141         {
142             ei.setCurrentPathHistory( phi );
143             ei.addError( "No validations for state '"+state.getName()+"'" );
144             return;
145         }
146         for (int i = 0; i < v.length && !ei.isHaltPath(); ++i)
147         {
148             ei.setCurrentPathHistory( phi );
149             v[i].validate( sys, ei );
150             phi.addValidate( v[i], state, ei );
151             ei.setCurrentPathHistory( phi );
152         }
153     }
154     
155     
156     protected void validate( ITransition trans, ErrorsImpl ei,
157             PathHistoryImpl phi, ISystem sys )
158             throws CloneNotSupportedException JavaDoc
159     {
160         IValidate[] v = trans.getValidates();
161         if (v.length <= 0)
162         {
163             ei.setCurrentPathHistory( phi );
164             ei.addError( "No validations for transition '"+
165                 trans.getName()+"'" );
166             return;
167         }
168         for (int i = 0; i < v.length && !ei.isHaltPath(); ++i)
169         {
170             ei.setCurrentPathHistory( (IPathHistory)phi.clone() );
171             v[i].validate( sys, ei );
172             phi.addValidate( v[i], trans, ei );
173             ei.setCurrentPathHistory( (IPathHistory)phi.clone() );
174         }
175     }
176     
177     
178     protected ErrorsImpl createErrors()
179     {
180         return new ErrorsImpl();
181     }
182     
183     
184     protected PathHistoryImpl createPathHistory()
185     {
186         return new PathHistoryImpl();
187     }
188 }
189
190
Popular Tags