source: trunk/autoquest-plugin-genericevents/src/main/java/de/ugoe/cs/autoquest/plugin/genericevents/commands/CMDparseDirGenericEvents.java @ 2153

Last change on this file since 2153 was 2153, checked in by pharms, 7 years ago
  • Property svn:mime-type set to text/plain
File size: 4.4 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.plugin.genericevents.commands;
16
17import java.io.File;
18import java.util.Arrays;
19import java.util.Collection;
20import java.util.List;
21import java.util.logging.Level;
22
23import de.ugoe.cs.autoquest.CommandHelpers;
24import de.ugoe.cs.autoquest.eventcore.Event;
25import de.ugoe.cs.autoquest.eventcore.IHierarchicalEventTargetModel;
26import de.ugoe.cs.autoquest.plugin.genericevents.GenericEventLogParser;
27import de.ugoe.cs.util.console.Command;
28import de.ugoe.cs.util.console.Console;
29import de.ugoe.cs.util.console.GlobalDataContainer;
30
31/**
32 * <p>
33 * Command that tries to parse all files in a folder as if they were log files generated by the
34 * generic event monitor. The result is one set of sequences for all files (not one set of
35 * sequences for each file!).
36 * </p>
37 *
38 * @author Patrick Harms
39 * @version 1.0
40 */
41public class CMDparseDirGenericEvents implements Command {
42
43    /*
44     * (non-Javadoc)
45     *
46     * @see de.ugoe.cs.util.console.Command#run(java.util.List)
47     */
48    @Override
49    public void run(List<Object> parameters) {
50        String path = null;
51        String sequencesName = null;
52
53        try {
54            for (int i = 0; i < parameters.size(); i++) {
55                String param = (String) parameters.get(i);
56                if (path == null) {
57                    path = param;
58                }
59                else if (sequencesName == null) {
60                    sequencesName = param;
61                }
62            }
63        }
64        catch (Exception e) {
65            throw new IllegalArgumentException("illegal parameters provided: " + e);
66        }
67       
68        if (sequencesName == null) {
69            sequencesName = "sequences";
70        }
71
72        File folder = new File(path);
73        if (!folder.isDirectory()) {
74            Console.printerrln(path + " is not a directory");
75            return;
76        }
77
78        GenericEventLogParser parser = new GenericEventLogParser();
79
80        parseFile(folder, parser);
81
82        Collection<List<Event>> sequences = parser.getSequences();
83
84        IHierarchicalEventTargetModel<?> targets = parser.getHierarchicalEventTargetModel();
85
86        if (GlobalDataContainer.getInstance().addData(sequencesName, sequences)) {
87            CommandHelpers.dataOverwritten(sequencesName);
88        }
89
90        if (GlobalDataContainer.getInstance().addData(sequencesName + "_targets", targets)) {
91            CommandHelpers.dataOverwritten(sequencesName + "_targets");
92        }
93    }
94
95    /**
96     * <p>
97     * recursive method for parsing a directory structures
98     * </p>
99     *
100     * @param file   the file object to be parsed. If the file is a folder, the method calls itself
101     *               for all children
102     * @param parser the parser to use for parsing the files.
103     */
104    private void parseFile(File file, GenericEventLogParser parser) {
105        if (file.isDirectory()) {
106            String[] children = file.list();
107            Arrays.sort(children);
108           
109            for (String child : children) {
110                File childFile = new File(file, child);
111                parseFile(childFile, parser);
112            }
113        }
114        else if (file.isFile()) {
115            String source = file.getAbsolutePath();
116            Console.traceln(Level.INFO, "Processing file: " + source);
117
118            try {
119                parser.parseFile(file);
120            }
121            catch (Exception e) {
122                Console.printerrln("Could not parse " + source + ": " + e.getMessage());
123            }
124        }
125    }
126
127    /*
128     * (non-Javadoc)
129     *
130     * @see de.ugoe.cs.util.console.Command#help()
131     */
132    @Override
133    public String help() {
134        return "parseDirGenericEvents <directory> [<sequencesName>]";
135    }
136
137}
Note: See TracBrowser for help on using the repository browser.