[2153] | 1 | // Copyright 2015 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.ArrayList; |
---|
| 19 | import java.util.Collection; |
---|
| 20 | import java.util.HashMap; |
---|
| 21 | import java.util.List; |
---|
| 22 | import java.util.Map; |
---|
| 23 | |
---|
| 24 | import de.ugoe.cs.autoquest.CommandHelpers; |
---|
| 25 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
| 26 | import de.ugoe.cs.util.console.Command; |
---|
| 27 | import de.ugoe.cs.util.console.Console; |
---|
| 28 | import de.ugoe.cs.util.console.GlobalDataContainer; |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * <p> |
---|
| 32 | * TODO comment |
---|
| 33 | * </p> |
---|
| 34 | * |
---|
| 35 | * @author Patrick Harms |
---|
| 36 | */ |
---|
| 37 | public class CMDparseDirSogouQData implements Command { |
---|
| 38 | |
---|
| 39 | /* (non-Javadoc) |
---|
| 40 | * @see de.ugoe.cs.util.console.Command#run(java.util.List) |
---|
| 41 | */ |
---|
| 42 | @Override |
---|
| 43 | public void run(List<Object> parameters) { |
---|
| 44 | String path = null; |
---|
| 45 | String sequencesName = null; |
---|
| 46 | boolean hasTimestamp = false; |
---|
| 47 | boolean ignoreQuery = false; |
---|
| 48 | boolean compareDomainOnly = false; |
---|
| 49 | |
---|
| 50 | try { |
---|
| 51 | for (int i = 0; i < parameters.size(); i++) { |
---|
| 52 | String parameter = (String) parameters.get(i); |
---|
| 53 | if (!parameter.startsWith("-")) { |
---|
| 54 | if (path == null) { |
---|
| 55 | path = parameter; |
---|
| 56 | } |
---|
| 57 | else if (sequencesName == null) { |
---|
| 58 | sequencesName = parameter; |
---|
| 59 | } |
---|
| 60 | else { |
---|
| 61 | throw new IllegalArgumentException("unrecognized parameter: " + parameter); |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | else { |
---|
| 65 | if ("-hasTimestamp".equals(parameter)) { |
---|
| 66 | hasTimestamp = true; |
---|
| 67 | } |
---|
| 68 | else if ("-ignoreQuery".equals(parameter)) { |
---|
| 69 | ignoreQuery = true; |
---|
| 70 | } |
---|
| 71 | else if ("-compareDomainOnly".equals(parameter)) { |
---|
| 72 | compareDomainOnly = true; |
---|
| 73 | } |
---|
| 74 | else { |
---|
| 75 | throw new IllegalArgumentException("unrecognized parameter: " + parameter); |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | catch (IllegalArgumentException e) { |
---|
| 81 | throw e; |
---|
| 82 | } |
---|
| 83 | catch (Exception e) { |
---|
| 84 | throw new IllegalArgumentException("could not process parameters", e); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | if (path == null) { |
---|
| 88 | throw new IllegalArgumentException("no path to directory provided"); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | if (sequencesName == null) { |
---|
| 92 | sequencesName = "sequences"; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | File folder = new File(path); |
---|
| 96 | if (!folder.isDirectory()) { |
---|
| 97 | Console.printerrln(path + " is not a directory"); |
---|
| 98 | return; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | Map<String, List<Event>> userSessions = new HashMap<>(); |
---|
| 102 | traverseFolder(folder, userSessions, hasTimestamp, ignoreQuery, compareDomainOnly); |
---|
| 103 | |
---|
| 104 | Collection<List<Event>> sequences = new ArrayList<>(); |
---|
| 105 | |
---|
| 106 | for (List<Event> session : userSessions.values()) { |
---|
| 107 | if (session.size() > 1) { |
---|
| 108 | sequences.add(session); |
---|
| 109 | } |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | if (GlobalDataContainer.getInstance().addData(sequencesName, sequences)) { |
---|
| 113 | CommandHelpers.dataOverwritten(sequencesName); |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** |
---|
| 118 | * <p> |
---|
| 119 | * TODO: comment |
---|
| 120 | * </p> |
---|
| 121 | * |
---|
| 122 | * @param folder |
---|
| 123 | */ |
---|
| 124 | private void traverseFolder(File folder, Map<String, |
---|
| 125 | List<Event>> allUserSessions, |
---|
| 126 | boolean hasTimestamp, |
---|
| 127 | boolean ignoreQuery, |
---|
| 128 | boolean compareDomainOnly) |
---|
| 129 | { |
---|
| 130 | if (folder.isDirectory()) { |
---|
| 131 | for (File child : folder.listFiles()) { |
---|
| 132 | traverseFolder(child, allUserSessions, hasTimestamp, ignoreQuery, compareDomainOnly); |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | else { |
---|
| 136 | Map<String, List<Event>> userSessions = new SogouQDataFileParser().parseFile |
---|
| 137 | (folder, hasTimestamp, ignoreQuery, compareDomainOnly); |
---|
| 138 | |
---|
| 139 | if (userSessions != null) { |
---|
| 140 | allUserSessions.putAll(userSessions); |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /* (non-Javadoc) |
---|
| 146 | * @see de.ugoe.cs.util.console.Command#help() |
---|
| 147 | */ |
---|
| 148 | @Override |
---|
| 149 | public String help() { |
---|
| 150 | return "parseDirSogouQData <folder> {<sequencesName>} {-hasTimestamp} {-ignoreQuery} " + |
---|
| 151 | "{-compareDomainOnly}"; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | } |
---|