source: trunk/autoquest-plugin-html/src/main/java/de/ugoe/cs/autoquest/plugin/html/commands/CMDcondenseHTMLListStructures.java @ 2215

Last change on this file since 2215 was 2146, checked in by pharms, 7 years ago
  • refactored GUI model so that hierarchical event target structures can also be used and created by plugins not being strictly for GUIs
  • Property svn:mime-type set to text/plain
File size: 10.6 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.html.commands;
16
17import java.util.ArrayList;
18import java.util.HashMap;
19import java.util.List;
20import java.util.Map;
21import java.util.logging.Level;
22
23import de.ugoe.cs.autoquest.CommandHelpers;
24import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel;
25import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
26import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLDocument;
27import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLPageElement;
28import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLServer;
29import de.ugoe.cs.util.console.Command;
30import de.ugoe.cs.util.console.Console;
31import de.ugoe.cs.util.console.GlobalDataContainer;
32
33/**
34 * <p>
35 * TODO document
36 * </p>
37 *
38 * @author Patrick Harms
39 * @version 1.0
40 */
41public class CMDcondenseHTMLListStructures 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 sequencesName;
51
52        try {
53            sequencesName = (String) parameters.get(0);
54        }
55        catch (Exception e) {
56            throw new IllegalArgumentException("illegal parameters provided: " + e);
57        }
58
59        Object dataObject = GlobalDataContainer.getInstance().getData(sequencesName + "_targets");
60        if (dataObject == null) {
61            CommandHelpers.objectNotFoundMessage(sequencesName + "_targets");
62            return;
63        }
64        if (!(dataObject instanceof GUIModel)) {
65            CommandHelpers.objectNotType(sequencesName, "GUIModel");
66            return;
67        }
68
69        GUIModel model = (GUIModel) dataObject;
70       
71        for (IGUIElement root : model.getRootElements()) {
72            if (root instanceof HTMLServer) {
73                try {
74                    condenseListStructures((HTMLServer) root, model);
75                }
76                catch (Exception e) {
77                    Console.printerrln
78                        ("problems while condensing list structures on pages of server " + root);
79                    Console.logException(e);
80                    break;
81                }
82            }
83        }
84    }
85
86    /**
87     * <p>
88     *
89     * </p>
90     *
91     * @param server the server of which all documents shall be condensed
92     * @param model  the GUI model in which the server is referenced
93     */
94    private void condenseListStructures(HTMLServer server, GUIModel model) {
95        Console.traceln(Level.INFO, "condensing list structures in documents of " + server);
96       
97        for (IGUIElement document : model.getChildren(server)) {
98            if (document instanceof HTMLDocument) {
99                condenseListStructures((HTMLDocument) document, model, "");
100            }
101            else {
102                Console.traceln(Level.WARNING, "child " + document + " of server " + server +
103                                " is no document");
104            }
105        }
106       
107        Console.traceln(Level.INFO, "condensed list structures in documents of " + server);
108    }
109
110    /**
111     * <p>
112     * TODO: comment
113     * </p>
114     *
115     * @param document
116     * @param model
117     * @param string
118     */
119    private void condenseListStructures(HTMLDocument document, GUIModel model, String indent) {
120        for (IGUIElement pageElement : model.getChildren(document)) {
121            if (pageElement instanceof HTMLPageElement) {
122                condenseListStructures((HTMLPageElement) pageElement, model, indent + "  ");
123            }
124            else {
125                Console.traceln(Level.WARNING, "child " + pageElement + " of document " + document +
126                                " is no page element");
127            }
128        }
129    }
130
131    /**
132     * <p>
133     * TODO: comment
134     * </p>
135     *
136     * @param pageElement
137     * @param model
138     * @param string
139     */
140    private List<List<HTMLPageElement>> condenseListStructures(HTMLPageElement pageElement,
141                                                               GUIModel        model,
142                                                               String          indent)
143    {
144        List<IGUIElement> children = model.getChildren(pageElement);
145       
146        if ((children == null) || (children.size() == 0)) {
147            // no children, return the element itself as the single entry of a list
148            List<List<HTMLPageElement>> result = new ArrayList<>();
149            result.add(new ArrayList<HTMLPageElement>());
150            result.get(0).add(pageElement);
151            return result;
152        }
153       
154        // traverse the children for structure information
155        @SuppressWarnings("unchecked")
156        List<List<HTMLPageElement>>[] childPaths = new List[children.size()];
157        int index = 0;
158        for (IGUIElement childElement : children) {
159            if (childElement instanceof HTMLPageElement) {
160                childPaths[index++] =
161                    condenseListStructures((HTMLPageElement) childElement, model, indent + "  ");
162            }
163            else {
164                Console.traceln(Level.WARNING, "child " + childElement + " of " + pageElement +
165                                " is no page element");
166            }
167        }
168       
169        // check for equally structured direct sibling children and group them. For this, their
170        // child paths need to be equal ignoring the HTML ids. So check for this.
171        Map<Integer, List<Integer>> equallyStructured = new HashMap<>();
172        int indexToCompareTo = 0;
173        equallyStructured.put(indexToCompareTo, new ArrayList<Integer>());
174       
175        SEARCH:
176        for (int i = 1; i < childPaths.length; i++) {
177            // compare any with the previous that needs to be compared with.
178            if (childPaths[indexToCompareTo].size() != childPaths[i].size()) {
179                // no equally structured children, try next.
180                indexToCompareTo = i;
181                equallyStructured.put(indexToCompareTo, new ArrayList<Integer>());
182                continue SEARCH;
183            }
184           
185            // compare individual child paths, first for their length
186            for (int j = 0; j < childPaths[indexToCompareTo].size(); j++) {
187                List<HTMLPageElement> firstPath = childPaths[indexToCompareTo].get(j);
188                List<HTMLPageElement> secondPath = childPaths[i].get(j);
189                if (firstPath.size() != secondPath.size()) {
190                    // no equally structured children, try next.
191                    indexToCompareTo = i;
192                    equallyStructured.put(indexToCompareTo, new ArrayList<Integer>());
193                    continue SEARCH;
194                }
195           
196                // compare individual path elements
197                for (int k = 0; k < firstPath.size(); k++) {
198                    if (!firstPath.get(k).getTagName().equals(secondPath.get(k).getTagName())) {
199                        // no equally structured children, try next.
200                        indexToCompareTo = i;
201                        equallyStructured.put(indexToCompareTo, new ArrayList<Integer>());
202                        continue SEARCH;
203                    }
204                }
205            }
206               
207            // found a match
208            equallyStructured.get(indexToCompareTo).add(i);
209        }
210       
211        for (Map.Entry<Integer, List<Integer>> entry : equallyStructured.entrySet()) {
212            if ((entry.getValue().size() > 1)) {
213                entry.getValue().add(0, entry.getKey());
214               
215                // check if the equal one have a minimum depth of 3
216                boolean hasMinDepth = false;
217                for (Integer indexesOfEqualChildren : entry.getValue()) {
218                    for (List<HTMLPageElement> path : childPaths[indexesOfEqualChildren]) {
219                        if (path.size() >= 3) {
220                            hasMinDepth = true;
221                            break;
222                        }
223                    }
224                }
225               
226                if (!hasMinDepth) {
227                    continue;
228                }
229               
230                // found equally structured children --> dump them
231                System.out.print("tata: " + pageElement.getView() + "  ");
232                String log = "";
233               
234                IGUIElement elem = pageElement;
235                while ((elem != null) && (elem instanceof HTMLPageElement)) {
236                    log = elem + "/" + log;
237                    elem = elem.getParent();
238                }
239               
240                System.out.println(log);
241               
242                for (int i = 0; i < entry.getValue().size(); i++) {
243                    System.out.println("  child " + (i + 1));
244                    for (int j = 0; j < childPaths[entry.getValue().get(i)].size(); j++) {
245                        System.out.print("    ");
246                        List<HTMLPageElement> path = childPaths[entry.getValue().get(i)].get(j);
247                        for (int k = 0; k < path.size(); k++) {
248                            System.out.print(path.get(k));
249                            System.out.print('/');
250                        }
251                        System.out.println();
252                    }
253                }
254            }
255        }
256       
257        List<List<HTMLPageElement>> result = new ArrayList<>();
258        for (List<List<HTMLPageElement>> childStructure : childPaths) {
259            for (List<HTMLPageElement> childPath : childStructure) {
260                childPath.add(0, pageElement);
261                result.add(childPath);
262            }
263        }
264       
265        return result;
266    }
267       
268
269    /*
270     * (non-Javadoc)
271     *
272     * @see de.ugoe.cs.util.console.Command#help()
273     */
274    @Override
275    public String help() {
276        return "condenseHTMLListStructures <sequence>";
277    }
278}
Note: See TracBrowser for help on using the repository browser.