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