source: trunk/autoquest-ui-core/src/main/java/de/ugoe/cs/autoquest/commands/sequences/CMDgenerateReplayfile.java @ 922

Last change on this file since 922 was 922, checked in by sherbold, 12 years ago
  • renaming of packages from de.ugoe.cs.quest to de.ugoe.cs.autoquest
File size: 6.3 KB
RevLine 
[922]1package de.ugoe.cs.autoquest.commands.sequences;
[1]2
[706]3import java.io.File;
4import java.io.FileOutputStream;
5import java.io.IOException;
6import java.io.OutputStreamWriter;
[203]7import java.util.Collection;
[1]8import java.util.List;
[706]9import java.util.logging.Level;
[1]10
[922]11import de.ugoe.cs.autoquest.CommandHelpers;
12import de.ugoe.cs.autoquest.IReplayDecorator;
13import de.ugoe.cs.autoquest.SequenceInstanceOf;
14import de.ugoe.cs.autoquest.eventcore.Event;
15import de.ugoe.cs.autoquest.eventcore.IReplayable;
[706]16import de.ugoe.cs.util.StringTools;
[1]17import de.ugoe.cs.util.console.Command;
[706]18import de.ugoe.cs.util.console.Console;
[667]19import de.ugoe.cs.util.console.GlobalDataContainer;
[1]20
[171]21/**
[209]22 * <p>
23 * Command to create a replay file from stored sessions.
24 * </p>
25 *
[706]26 * TODO: Add appropriate checks if Events are replayable
27 *
[171]28 * @author Steffen Herbold
29 * @version 1.0
30 */
[1]31public class CMDgenerateReplayfile implements Command {
32
[209]33        /*
34         * (non-Javadoc)
35         *
[171]36         * @see de.ugoe.cs.util.console.Command#help()
37         */
[1]38        @Override
[664]39        public String help() {
40                return "generateReplayfile <filename> <sequences>";
[1]41        }
42
[209]43        /*
44         * (non-Javadoc)
45         *
[171]46         * @see de.ugoe.cs.util.console.Command#run(java.util.List)
47         */
[1]48        @SuppressWarnings("unchecked")
49        @Override
50        public void run(List<Object> parameters) {
[241]51                String filename;
52                String sequencesName;
53                try {
54                        filename = (String) parameters.get(0);
55                        sequencesName = (String) parameters.get(1);
56                } catch (Exception e) {
[766]57                        throw new IllegalArgumentException();
[1]58                }
[209]59
[547]60                Collection<List<Event>> sequences = null;
[209]61                Object dataObject = GlobalDataContainer.getInstance().getData(
62                                sequencesName);
63                if (dataObject == null) {
[240]64                        CommandHelpers.objectNotFoundMessage(sequencesName);
[209]65                        return;
[1]66                }
[209]67                if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) {
[240]68                        CommandHelpers.objectNotType(sequencesName,
69                                        "Collection<List<Event<?>>>");
[209]70                        return;
[1]71                }
[209]72
[547]73                sequences = (Collection<List<Event>>) dataObject;
[706]74                createLogfileMultipleSessions(sequences, filename);
[1]75        }
[706]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;
[1]84
[706]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            }
[1]198}
Note: See TracBrowser for help on using the repository browser.