[1] | 1 | package de.ugoe.cs.eventbench;
|
---|
| 2 |
|
---|
| 3 | import java.io.File;
|
---|
[44] | 4 | import java.io.FileOutputStream;
|
---|
[1] | 5 | import java.io.IOException;
|
---|
[44] | 6 | import java.io.OutputStreamWriter;
|
---|
[1] | 7 | import java.util.List;
|
---|
| 8 |
|
---|
| 9 | import de.ugoe.cs.eventbench.data.IReplayable;
|
---|
| 10 | import de.ugoe.cs.eventbench.data.ReplayableEvent;
|
---|
| 11 | import de.ugoe.cs.util.StringTools;
|
---|
| 12 | import de.ugoe.cs.util.console.Console;
|
---|
| 13 |
|
---|
[171] | 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 | */
|
---|
[1] | 23 | public class ReplayGenerator {
|
---|
[171] | 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 | */
|
---|
[58] | 31 | private IReplayDecorator decorator = null;
|
---|
[171] | 32 |
|
---|
| 33 | /**
|
---|
| 34 | * <p>
|
---|
| 35 | * Id of the current session. The starting id is 1.
|
---|
| 36 | * </p>
|
---|
| 37 | */
|
---|
[98] | 38 | int sessionId = 1;
|
---|
[171] | 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) {
|
---|
[44] | 53 | OutputStreamWriter writer = openReplayFile(filename);
|
---|
[171] | 54 | if (writer != null) {
|
---|
[1] | 55 | try {
|
---|
[58] | 56 | decorator = sequences.get(0).get(0).getReplayDecorator();
|
---|
[171] | 57 | if (decorator != null) {
|
---|
[58] | 58 | writer.write(decorator.getHeader());
|
---|
| 59 | }
|
---|
[171] | 60 | for (List<ReplayableEvent<?>> actions : sequences) {
|
---|
[1] | 61 | writeSession(actions, writer);
|
---|
| 62 | }
|
---|
[171] | 63 | if (decorator != null) {
|
---|
[58] | 64 | writer.write(decorator.getFooter());
|
---|
| 65 | }
|
---|
| 66 | decorator = null;
|
---|
[1] | 67 | writer.close();
|
---|
| 68 | } catch (IOException e) {
|
---|
| 69 | Console.printerrln("Unable to write replay file " + filename);
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
[171] | 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) {
|
---|
[44] | 86 | OutputStreamWriter writer = openReplayFile(filename);
|
---|
[171] | 87 | if (writer != null) {
|
---|
[1] | 88 | try {
|
---|
[58] | 89 | actions.get(0).getReplayDecorator();
|
---|
[171] | 90 | if (decorator != null) {
|
---|
[58] | 91 | writer.write(decorator.getHeader());
|
---|
| 92 | }
|
---|
[1] | 93 | writeSession(actions, writer);
|
---|
[171] | 94 | if (decorator != null) {
|
---|
[58] | 95 | writer.write(decorator.getFooter());
|
---|
| 96 | }
|
---|
| 97 | decorator = null;
|
---|
[1] | 98 | writer.close();
|
---|
| 99 | } catch (IOException e) {
|
---|
| 100 | Console.printerrln("Unable to write replay file " + filename);
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[171] | 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 | */
|
---|
[44] | 114 | private OutputStreamWriter openReplayFile(String filename) {
|
---|
[1] | 115 | File file = new File(filename);
|
---|
| 116 | boolean fileCreated;
|
---|
| 117 | try {
|
---|
| 118 | fileCreated = file.createNewFile();
|
---|
[171] | 119 | if (!fileCreated) {
|
---|
[1] | 120 | Console.traceln("Created logfile " + filename);
|
---|
| 121 | } else {
|
---|
| 122 | Console.traceln("Overwrote existing logfile " + filename);
|
---|
| 123 | }
|
---|
| 124 | } catch (IOException e) {
|
---|
| 125 | Console.printerrln("Unable to create file " + filename);
|
---|
| 126 | Console.printStacktrace(e);
|
---|
| 127 | }
|
---|
[44] | 128 | OutputStreamWriter writer = null;
|
---|
[1] | 129 | try {
|
---|
[171] | 130 | writer = new OutputStreamWriter(new FileOutputStream(file),
|
---|
| 131 | "UTF-16");
|
---|
[1] | 132 | } catch (IOException e) {
|
---|
[171] | 133 | Console.printerrln("Unable to open file for writing (read-only file):"
|
---|
| 134 | + filename);
|
---|
[1] | 135 | Console.printStacktrace(e);
|
---|
| 136 | }
|
---|
| 137 | return writer;
|
---|
| 138 | }
|
---|
[171] | 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) {
|
---|
[98] | 155 | writer.write(decorator.getSessionHeader(sessionId));
|
---|
[58] | 156 | }
|
---|
[171] | 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);
|
---|
[58] | 163 | writer.flush();
|
---|
[1] | 164 | }
|
---|
| 165 | }
|
---|
[171] | 166 | if (decorator != null) {
|
---|
[98] | 167 | writer.write(decorator.getSessionFooter(sessionId));
|
---|
[58] | 168 | }
|
---|
[98] | 169 | sessionId++;
|
---|
[1] | 170 | }
|
---|
| 171 |
|
---|
| 172 | }
|
---|