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