- Timestamp:
- 09/09/11 06:23:36 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/ReplayGenerator.java
r98 r171 12 12 import de.ugoe.cs.util.console.Console; 13 13 14 /** 15 * <p> 16 * This class provides the functionality to generate replay files from sequences 17 * if {@link ReplayableEvent}s. 18 * </p> 19 * 20 * @author Steffen Herbold 21 * @version 1.0 22 */ 14 23 public class ReplayGenerator { 15 24 25 /** 26 * <p> 27 * {@link IReplayDecorator} to be used. If this field is {@code null}, no 28 * decorator is used. Default: {@code null} 29 * </p> 30 */ 16 31 private IReplayDecorator decorator = null; 17 32 33 /** 34 * <p> 35 * Id of the current session. The starting id is 1. 36 * </p> 37 */ 18 38 int sessionId = 1; 19 20 public void createLogfileMultipleSessions(List<List<ReplayableEvent<?>>> sequences, String filename) { 39 40 /** 41 * <p> 42 * Creates a replay file that contains multiple event sequences. 43 * </p> 44 * 45 * @param sequences 46 * collection of event sequences from which the sessions are 47 * generated 48 * @param filename 49 * name and path of the replay file 50 */ 51 public void createLogfileMultipleSessions( 52 List<List<ReplayableEvent<?>>> sequences, String filename) { 21 53 OutputStreamWriter writer = openReplayFile(filename); 22 if ( writer!=null ) {54 if (writer != null) { 23 55 try { 24 56 decorator = sequences.get(0).get(0).getReplayDecorator(); 25 if ( decorator!=null) {57 if (decorator != null) { 26 58 writer.write(decorator.getHeader()); 27 59 } 28 for ( List<ReplayableEvent<?>> actions : sequences) {60 for (List<ReplayableEvent<?>> actions : sequences) { 29 61 writeSession(actions, writer); 30 62 } 31 if( decorator!=null ) { 32 writer.write(decorator.getFooter()); 33 } 34 decorator = null; 35 writer.close(); 36 } catch (IOException e) { 37 Console.printerrln("Unable to write replay file " + filename); 38 } 39 } 40 } 41 42 public void createLogfileSingleSession(List<ReplayableEvent<?>> actions, String filename) { 43 OutputStreamWriter writer = openReplayFile(filename); 44 if( writer!=null ) { 45 try { 46 actions.get(0).getReplayDecorator(); 47 if( decorator!=null ) { 48 writer.write(decorator.getHeader()); 49 } 50 writeSession(actions, writer); 51 if( decorator!=null ) { 63 if (decorator != null) { 52 64 writer.write(decorator.getFooter()); 53 65 } … … 60 72 } 61 73 74 /** 75 * <p> 76 * Creates a replay file that from a single event sequence. 77 * </p> 78 * 79 * @param actions 80 * event sequence from which the sessions are generated 81 * @param filename 82 * name and path of the replay file 83 */ 84 public void createLogfileSingleSession(List<ReplayableEvent<?>> actions, 85 String filename) { 86 OutputStreamWriter writer = openReplayFile(filename); 87 if (writer != null) { 88 try { 89 actions.get(0).getReplayDecorator(); 90 if (decorator != null) { 91 writer.write(decorator.getHeader()); 92 } 93 writeSession(actions, writer); 94 if (decorator != null) { 95 writer.write(decorator.getFooter()); 96 } 97 decorator = null; 98 writer.close(); 99 } catch (IOException e) { 100 Console.printerrln("Unable to write replay file " + filename); 101 } 102 } 103 } 104 105 /** 106 * <p> 107 * Helper function that opens the replay file for writing. 108 * </p> 109 * 110 * @param filename 111 * name and path of the replay file 112 * @return {@link OutputStreamWriter} that writes to the replay file 113 */ 62 114 private OutputStreamWriter openReplayFile(String filename) { 63 115 File file = new File(filename); … … 65 117 try { 66 118 fileCreated = file.createNewFile(); 67 if ( !fileCreated) {119 if (!fileCreated) { 68 120 Console.traceln("Created logfile " + filename); 69 121 } else { … … 76 128 OutputStreamWriter writer = null; 77 129 try { 78 writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-16"); 130 writer = new OutputStreamWriter(new FileOutputStream(file), 131 "UTF-16"); 79 132 } catch (IOException e) { 80 Console.printerrln("Unable to open file for writing (read-only file):" + filename); 133 Console.printerrln("Unable to open file for writing (read-only file):" 134 + filename); 81 135 Console.printStacktrace(e); 82 136 } 83 137 return writer; 84 138 } 85 86 private void writeSession(List<ReplayableEvent<?>> actions, OutputStreamWriter writer) 87 throws IOException { 88 if( decorator!=null ) { 139 140 /** 141 * <p> 142 * Helper function that adds an event sequence to the replay. 143 * </p> 144 * 145 * @param actions 146 * event sequences to be added 147 * @param writer 148 * {@link OutputStreamWriter} to which the replay is added 149 * @throws IOException 150 * thrown if there is a problem writing to writer 151 */ 152 private void writeSession(List<ReplayableEvent<?>> actions, 153 OutputStreamWriter writer) throws IOException { 154 if (decorator != null) { 89 155 writer.write(decorator.getSessionHeader(sessionId)); 90 156 } 91 for( ReplayableEvent<?> currentAction : actions ) { 92 93 List<? extends IReplayable> replayables = currentAction.getReplayMessages(); 94 for( IReplayable replayble : replayables ) { 95 writer.write(replayble.getReplay()+StringTools.ENDLINE); 157 for (ReplayableEvent<?> currentAction : actions) { 158 159 List<? extends IReplayable> replayables = currentAction 160 .getReplayMessages(); 161 for (IReplayable replayble : replayables) { 162 writer.write(replayble.getReplay() + StringTools.ENDLINE); 96 163 writer.flush(); 97 164 } 98 165 } 99 if ( decorator!=null) {166 if (decorator != null) { 100 167 writer.write(decorator.getSessionFooter(sessionId)); 101 168 }
Note: See TracChangeset
for help on using the changeset viewer.