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