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 |
|
---|
15 | package de.ugoe.cs.autoquest.plugin.genericevents.commands;
|
---|
16 |
|
---|
17 | import java.io.File;
|
---|
18 | import java.util.Arrays;
|
---|
19 | import java.util.Collection;
|
---|
20 | import java.util.HashSet;
|
---|
21 | import java.util.List;
|
---|
22 | import java.util.Set;
|
---|
23 | import java.util.logging.Level;
|
---|
24 |
|
---|
25 | import de.ugoe.cs.autoquest.CommandHelpers;
|
---|
26 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
27 | import de.ugoe.cs.autoquest.eventcore.IHierarchicalEventTargetModel;
|
---|
28 | import de.ugoe.cs.autoquest.plugin.genericevents.GenericEventLogParser;
|
---|
29 | import de.ugoe.cs.util.console.Command;
|
---|
30 | import de.ugoe.cs.util.console.Console;
|
---|
31 | import de.ugoe.cs.util.console.GlobalDataContainer;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * <p>
|
---|
35 | * Command that tries to parse all files in a folder as if they were log files generated by the
|
---|
36 | * generic event monitor. The result is one set of sequences for all files (not one set of
|
---|
37 | * sequences for each file!).
|
---|
38 | * </p>
|
---|
39 | *
|
---|
40 | * @author Patrick Harms
|
---|
41 | * @version 1.0
|
---|
42 | */
|
---|
43 | public class CMDparseDirGenericEvents implements Command {
|
---|
44 |
|
---|
45 | /*
|
---|
46 | * (non-Javadoc)
|
---|
47 | *
|
---|
48 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
49 | */
|
---|
50 | @Override
|
---|
51 | public void run(List<Object> parameters) {
|
---|
52 | String path = null;
|
---|
53 | String sequencesName = null;
|
---|
54 |
|
---|
55 | Set<String> ignoredEvents = new HashSet<>();
|
---|
56 |
|
---|
57 | try {
|
---|
58 | for (int i = 0; i < parameters.size(); i++) {
|
---|
59 | String param = (String) parameters.get(i);
|
---|
60 | if (path == null) {
|
---|
61 | path = param;
|
---|
62 | }
|
---|
63 | else if (sequencesName == null) {
|
---|
64 | sequencesName = param;
|
---|
65 | }
|
---|
66 | else {
|
---|
67 | ignoredEvents.add(param);
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 | catch (Exception e) {
|
---|
72 | throw new IllegalArgumentException("illegal parameters provided: " + e);
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (sequencesName == null) {
|
---|
76 | sequencesName = "sequences";
|
---|
77 | }
|
---|
78 |
|
---|
79 | File folder = new File(path);
|
---|
80 | if (!folder.isDirectory()) {
|
---|
81 | Console.printerrln(path + " is not a directory");
|
---|
82 | return;
|
---|
83 | }
|
---|
84 |
|
---|
85 | GenericEventLogParser parser = new GenericEventLogParser(ignoredEvents);
|
---|
86 |
|
---|
87 | parseFile(folder, parser);
|
---|
88 |
|
---|
89 | Collection<List<Event>> sequences = parser.getSequences();
|
---|
90 |
|
---|
91 | IHierarchicalEventTargetModel<?> targets = parser.getHierarchicalEventTargetModel();
|
---|
92 |
|
---|
93 | if (GlobalDataContainer.getInstance().addData(sequencesName, sequences)) {
|
---|
94 | CommandHelpers.dataOverwritten(sequencesName);
|
---|
95 | }
|
---|
96 |
|
---|
97 | if (GlobalDataContainer.getInstance().addData(sequencesName + "_targets", targets)) {
|
---|
98 | CommandHelpers.dataOverwritten(sequencesName + "_targets");
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * <p>
|
---|
104 | * recursive method for parsing a directory structures
|
---|
105 | * </p>
|
---|
106 | *
|
---|
107 | * @param file the file object to be parsed. If the file is a folder, the method calls itself
|
---|
108 | * for all children
|
---|
109 | * @param parser the parser to use for parsing the files.
|
---|
110 | */
|
---|
111 | private void parseFile(File file, GenericEventLogParser parser) {
|
---|
112 | if (file.isDirectory()) {
|
---|
113 | String[] children = file.list();
|
---|
114 |
|
---|
115 | if (children != null) {
|
---|
116 | Arrays.sort(children);
|
---|
117 |
|
---|
118 | for (String child : children) {
|
---|
119 | File childFile = new File(file, child);
|
---|
120 | parseFile(childFile, parser);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 | else if (file.isFile()) {
|
---|
125 | String source = file.getAbsolutePath();
|
---|
126 | Console.traceln(Level.INFO, "Processing file: " + source);
|
---|
127 |
|
---|
128 | try {
|
---|
129 | parser.parseFile(file);
|
---|
130 | }
|
---|
131 | catch (Exception e) {
|
---|
132 | Console.printerrln("Could not parse " + source + ": " + e.getMessage());
|
---|
133 | e.printStackTrace();
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | /*
|
---|
139 | * (non-Javadoc)
|
---|
140 | *
|
---|
141 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
142 | */
|
---|
143 | @Override
|
---|
144 | public String help() {
|
---|
145 | return "parseDirGenericEvents <directory> [<sequencesName>] [<ignoredEventType>*] [<ignoredEventType.ignoredEventTargetId>*]";
|
---|
146 | }
|
---|
147 |
|
---|
148 | }
|
---|