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