1 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 56 public class PathParserImpl implements IPathParser 57 { 58 private static final Logger LOG = Logger.getLogger( PathParserImpl.class ); 59 60 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 throw thre; 103 } 104 catch (ThreadDeath td) 105 { 106 throw td; 108 } 109 catch (TestFailRuntimeException tfre) 110 { 111 } 115 catch (Throwable t) 116 { 117 testErrs.addError( "Uncaught exception", t ); 118 if (t instanceof RuntimeException ) 119 { 120 throw (RuntimeException )t; 121 } 122 t.printStackTrace(); 123 throw new IllegalStateException ( 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 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 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 |