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 |
|
---|
10 | import de.ugoe.cs.quest.IReplayDecorator;
|
---|
11 | import de.ugoe.cs.quest.eventcore.Event;
|
---|
12 | import de.ugoe.cs.quest.eventcore.IReplayable;
|
---|
13 | import de.ugoe.cs.util.StringTools;
|
---|
14 | import de.ugoe.cs.util.console.Console;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * <p>
|
---|
18 | * This class provides the functionality to generate replay files from sequences
|
---|
19 | * if {@link ReplayableEvent}s.
|
---|
20 | * </p>
|
---|
21 | *
|
---|
22 | * TODO: Figure our how to handle {@link IReplayDecorator}
|
---|
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
|
---|
33 | * decorator is used. 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
|
---|
52 | * generated
|
---|
53 | * @param filename
|
---|
54 | * name and path of the replay file
|
---|
55 | */
|
---|
56 | public void createLogfileMultipleSessions(
|
---|
57 | Collection<List<Event>> sequences, String filename) {
|
---|
58 | OutputStreamWriter writer = openReplayFile(filename);
|
---|
59 | if (writer != null) {
|
---|
60 | try {
|
---|
61 | decorator = sequences.iterator().next().get(0).getReplayDecorator();
|
---|
62 | if (decorator != null) {
|
---|
63 | writer.write(decorator.getHeader());
|
---|
64 | }
|
---|
65 | for (List<Event> actions : sequences) {
|
---|
66 | writeSession(actions, writer);
|
---|
67 | }
|
---|
68 | if (decorator != null) {
|
---|
69 | writer.write(decorator.getFooter());
|
---|
70 | }
|
---|
71 | decorator = null;
|
---|
72 | writer.close();
|
---|
73 | } catch (IOException e) {
|
---|
74 | Console.printerrln("Unable to write replay file " + filename);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * <p>
|
---|
81 | * Creates a replay file that from a single event sequence.
|
---|
82 | * </p>
|
---|
83 | *
|
---|
84 | * @param actions
|
---|
85 | * event sequence from which the sessions are generated
|
---|
86 | * @param filename
|
---|
87 | * name and path of the replay file
|
---|
88 | */
|
---|
89 | public void createLogfileSingleSession(List<Event> actions,
|
---|
90 | String filename) {
|
---|
91 | OutputStreamWriter writer = openReplayFile(filename);
|
---|
92 | if (writer != null) {
|
---|
93 | try {
|
---|
94 | actions.get(0).getReplayDecorator();
|
---|
95 | if (decorator != null) {
|
---|
96 | writer.write(decorator.getHeader());
|
---|
97 | }
|
---|
98 | writeSession(actions, writer);
|
---|
99 | if (decorator != null) {
|
---|
100 | writer.write(decorator.getFooter());
|
---|
101 | }
|
---|
102 | decorator = null;
|
---|
103 | writer.close();
|
---|
104 | } catch (IOException e) {
|
---|
105 | Console.printerrln("Unable to write replay file " + filename);
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * <p>
|
---|
112 | * Helper function that opens the replay file for writing.
|
---|
113 | * </p>
|
---|
114 | *
|
---|
115 | * @param filename
|
---|
116 | * name and path of the replay file
|
---|
117 | * @return {@link OutputStreamWriter} that writes to the replay file
|
---|
118 | */
|
---|
119 | private OutputStreamWriter openReplayFile(String filename) {
|
---|
120 | File file = new File(filename);
|
---|
121 | boolean fileCreated;
|
---|
122 | try {
|
---|
123 | fileCreated = file.createNewFile();
|
---|
124 | if (!fileCreated) {
|
---|
125 | Console.traceln("Created logfile " + filename);
|
---|
126 | } else {
|
---|
127 | Console.traceln("Overwrote existing logfile " + filename);
|
---|
128 | }
|
---|
129 | } catch (IOException e) {
|
---|
130 | Console.printerrln("Unable to create file " + filename);
|
---|
131 | Console.logException(e);
|
---|
132 | }
|
---|
133 | OutputStreamWriter writer = null;
|
---|
134 | try {
|
---|
135 | writer = new OutputStreamWriter(new FileOutputStream(file),
|
---|
136 | "UTF-16");
|
---|
137 | } catch (IOException e) {
|
---|
138 | Console.printerrln("Unable to open file for writing (read-only file):"
|
---|
139 | + filename);
|
---|
140 | Console.logException(e);
|
---|
141 | }
|
---|
142 | return writer;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * <p>
|
---|
147 | * Helper function that adds an event sequence to the replay.
|
---|
148 | * </p>
|
---|
149 | *
|
---|
150 | * @param actions
|
---|
151 | * event sequences to be added
|
---|
152 | * @param writer
|
---|
153 | * {@link OutputStreamWriter} to which the replay is added
|
---|
154 | * @throws IOException
|
---|
155 | * thrown if there is a problem writing to writer
|
---|
156 | */
|
---|
157 | private void writeSession(List<Event> actions,
|
---|
158 | OutputStreamWriter writer) throws IOException {
|
---|
159 | if (decorator != null) {
|
---|
160 | writer.write(decorator.getSessionHeader(sessionId));
|
---|
161 | }
|
---|
162 | for (Event currentAction : actions) {
|
---|
163 |
|
---|
164 | List<? extends IReplayable> replayables = currentAction
|
---|
165 | .getReplayMessages();
|
---|
166 | for (IReplayable replayble : replayables) {
|
---|
167 | writer.write(replayble.getReplay() + StringTools.ENDLINE);
|
---|
168 | writer.flush();
|
---|
169 | }
|
---|
170 | }
|
---|
171 | if (decorator != null) {
|
---|
172 | writer.write(decorator.getSessionFooter(sessionId));
|
---|
173 | }
|
---|
174 | sessionId++;
|
---|
175 | }
|
---|
176 |
|
---|
177 | }
|
---|