KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)PathHistoryImpl.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.IState;
33 import net.sourceforge.groboutils.mbtf.v1.IValidate;
34 import net.sourceforge.groboutils.mbtf.v1.ITransition;
35 import net.sourceforge.groboutils.mbtf.v1.IPathHistory;
36 import net.sourceforge.groboutils.mbtf.v1.IErrors;
37
38 import java.util.Vector JavaDoc;
39
40 import org.apache.log4j.Logger;
41
42
43 /**
44  * Records the states and transition history during a path traversal.
45  * The instance can be cloned for the cases of errors and warnings needing to
46  * store the path history when they occured. The instance should also support
47  * a toString() method for easy debugging and logging.
48  *
49  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
50  * @version $Date: 2003/02/10 22:52:26 $
51  * @since June 12, 2002
52  */

53 public class PathHistoryImpl implements IPathHistory, Cloneable JavaDoc
54 {
55     private static final Logger LOG = Logger.getLogger( PathHistoryImpl.class );
56     
57     private static interface IHistoryPart
58     {
59         public String JavaDoc toString();
60     }
61     
62     private static class StateHistoryPart implements IHistoryPart
63     {
64         private IState state;
65         
66         public StateHistoryPart( IState s )
67         {
68             this.state = s;
69         }
70         
71         public String JavaDoc toString()
72         {
73             if (this.state == null)
74             {
75                 return " finished terminal state.\n";
76             }
77             return " entered state '"+this.state.getName()+"'\n";
78         }
79     }
80     
81     private static class TransHistoryPart implements IHistoryPart
82     {
83         private ITransition trans;
84         
85         public TransHistoryPart( ITransition t )
86         {
87             this.trans = t;
88         }
89         
90         public String JavaDoc toString()
91         {
92             return " followed transition '"+this.trans.getName()+"'\n";
93         }
94     }
95     
96     private static abstract class ValidHistoryPart implements IHistoryPart
97     {
98         protected String JavaDoc err;
99         
100         public ValidHistoryPart( IValidate v, IErrors e )
101         {
102             if (e != null && e.getErrors().length > 0)
103             {
104                 this.err = e.toString();
105             }
106             else
107             {
108                 this.err = "No errors.";
109             }
110         }
111     }
112     
113     private static class StateValidHistoryPart extends ValidHistoryPart
114     {
115         private IState state;
116         
117         public StateValidHistoryPart( IValidate v, IState s, IErrors e )
118         {
119             super( v, e );
120             this.state = s;
121         }
122         
123         public String JavaDoc toString()
124         {
125             return " validated state '" + this.state.getName() + "': " +
126                 this.err + "\n";
127         }
128     }
129     
130     private static class TransValidHistoryPart extends ValidHistoryPart
131     {
132         private ITransition trans;
133         
134         public TransValidHistoryPart( IValidate v, ITransition t, IErrors e )
135         {
136             super( v, e );
137             this.trans = t;
138         }
139         
140         public String JavaDoc toString()
141         {
142             return " validated transition '" + this.trans.getName() + "': " +
143                 this.err + "\n";
144         }
145     }
146     
147     
148     
149     private int errorCount = 0;
150     private Vector JavaDoc history = new Vector JavaDoc();
151     
152     
153     /**
154      * When the current state changes, add the new state here. This should
155      * be called for the start state, and once at the end of every successful
156      * transition.
157      */

158     public void addState( IState s )
159     {
160         LOG.debug( "addState( "+s+" )" );
161         this.history.addElement( new StateHistoryPart( s ) );
162         LOG.debug( " - History is now: "+toString() );
163     }
164     
165     
166     /**
167      * When a transition is about to occur, it should be added here.
168      */

169     public void addTransition( ITransition t )
170     {
171         LOG.debug( "addTransition( "+t+" )" );
172         this.history.addElement( new TransHistoryPart( t ) );
173     }
174     
175     
176     /**
177      * After a validation call, the error results should be added here.
178      */

179     public void addValidate( IValidate val, IState owningState,
180             IErrors errors )
181     {
182         LOG.debug( "addValidate( "+val+", "+owningState+", "+errors+" )" );
183         if (errors != null)
184         {
185             this.errorCount += errors.getErrors().length;
186         }
187         this.history.addElement( new StateValidHistoryPart( val, owningState,
188             errors ) );
189     }
190     
191     
192     /**
193      * After a validation call, the error results should be added here.
194      */

195     public void addValidate( IValidate val, ITransition owningTransition,
196             IErrors errors )
197     {
198         LOG.debug( "addValidate( "+val+", "+owningTransition+", "+errors+" )" );
199         if (errors != null)
200         {
201             this.errorCount += errors.getErrors().length;
202         }
203         this.history.addElement( new TransValidHistoryPart( val,
204             owningTransition, errors ) );
205     }
206     
207     
208     /**
209      * Returns the total number of errors (halts, failures, and errors)
210      * registered during validation and transitions.
211      */

212     public int getErrorCount()
213     {
214         return this.errorCount;
215     }
216     
217     
218     public String JavaDoc toString()
219     {
220         IHistoryPart hp[] = new IHistoryPart[ this.history.size() ];
221         this.history.copyInto( hp );
222         if (hp.length <= 0)
223         {
224             return "[no history]";
225         }
226         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("[Path History:\n");
227         for (int i = 0; i < hp.length; ++i)
228         {
229             sb.append( hp[i] );
230         }
231         sb.append("\n]");
232         return sb.toString();
233     }
234     
235     
236     protected Object JavaDoc clone()
237             throws CloneNotSupportedException JavaDoc
238     {
239         PathHistoryImpl phi = (PathHistoryImpl)super.clone();
240         phi.errorCount = this.errorCount;
241         phi.history = (Vector JavaDoc)this.history.clone();
242         
243         return phi;
244     }
245     
246     
247     public IPathHistory copy()
248     {
249         try
250         {
251             return (IPathHistory)clone();
252         }
253         catch (CloneNotSupportedException JavaDoc e)
254         {
255             throw new IllegalStateException JavaDoc( e.toString() );
256         }
257     }
258 }
259
260
Popular Tags