1 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 ; 39 40 import org.apache.log4j.Logger; 41 42 43 53 public class PathHistoryImpl implements IPathHistory, Cloneable 54 { 55 private static final Logger LOG = Logger.getLogger( PathHistoryImpl.class ); 56 57 private static interface IHistoryPart 58 { 59 public String 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 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 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 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 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 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 history = new Vector (); 151 152 153 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 169 public void addTransition( ITransition t ) 170 { 171 LOG.debug( "addTransition( "+t+" )" ); 172 this.history.addElement( new TransHistoryPart( t ) ); 173 } 174 175 176 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 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 212 public int getErrorCount() 213 { 214 return this.errorCount; 215 } 216 217 218 public String 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 sb = new StringBuffer ("[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 clone() 237 throws CloneNotSupportedException 238 { 239 PathHistoryImpl phi = (PathHistoryImpl)super.clone(); 240 phi.errorCount = this.errorCount; 241 phi.history = (Vector )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 e) 254 { 255 throw new IllegalStateException ( e.toString() ); 256 } 257 } 258 } 259 260 | Popular Tags |