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