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