source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/TaskInstanceTrie.java @ 1256

Last change on this file since 1256 was 1256, checked in by pharms, 11 years ago
  • performance improvement for task tree generation
File size: 6.0 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.tasktrees.temporalrelation;
16
17import java.util.HashMap;
18import java.util.LinkedList;
19import java.util.List;
20import java.util.Map;
21
22import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
23import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession;
24import de.ugoe.cs.autoquest.usageprofiles.SymbolMap;
25import de.ugoe.cs.autoquest.usageprofiles.Trie;
26import de.ugoe.cs.autoquest.usageprofiles.TrieProcessor;
27
28/**
29 * <p>
30 * TODO comment
31 * </p>
32 *
33 * @author Patrick Harms
34 */
35public class TaskInstanceTrie extends Trie<ITaskInstance> {
36
37    /**  */
38    private static final long serialVersionUID = 1L;
39
40    /**
41     * <p>
42     * the task comparator to be used for comparing tasks
43     * </p>
44     */
45    private TaskComparator comparator;
46
47    /**
48     * <p>
49     * TODO: comment
50     * </p>
51     *
52     * @param taskComparator2
53     */
54    public TaskInstanceTrie(TaskComparator taskComparator) {
55        super(taskComparator);
56        this.comparator = taskComparator;
57    }
58
59    /**
60     *
61     */
62    public void trainSessions(List<IUserSession> userSessions, int maxOrder) {
63        if (maxOrder < 1) {
64            return;
65        }
66       
67        SymbolMap<ITaskInstance, List<ITaskInstance>> equalTaskInstancesMap =
68            new SymbolMap<ITaskInstance, List<ITaskInstance>>(comparator);
69       
70        Map<ITaskInstance, List<ITaskInstance>> taskInstancesMap =
71            new HashMap<ITaskInstance, List<ITaskInstance>>();
72       
73        System.out.println("preparing training");
74        for (IUserSession session : userSessions) {
75            for (ITaskInstance taskInstance : session) {
76                List<ITaskInstance> equalTaskInstances =
77                    equalTaskInstancesMap.getValue(taskInstance);
78               
79                if (equalTaskInstances == null) {
80                    equalTaskInstances = new LinkedList<ITaskInstance>();
81                    equalTaskInstancesMap.addSymbol(taskInstance, equalTaskInstances);
82                }
83               
84                equalTaskInstances.add(taskInstance);
85                taskInstancesMap.put(taskInstance, equalTaskInstances);
86            }
87        }
88       
89        System.out.println("performing training");
90        for (IUserSession session : userSessions) {
91            train(session, maxOrder, taskInstancesMap);
92        }     
93       
94        updateKnownSymbols();
95    }
96   
97    /**
98     *
99     */
100    private void train(IUserSession                            userSession,
101                       int                                     maxOrder,
102                       Map<ITaskInstance, List<ITaskInstance>> taskInstancesMap)
103    {
104        List<ITaskInstance> executedTasks = userSession.getExecutedTasks();
105       
106        List<ITaskInstance> subsequence = new LinkedList<ITaskInstance>();
107       
108        int numberOfTrainedSubsequences = 0;
109        int sequenceMaxCount = 0;
110       
111        for (ITaskInstance currentTaskInstance : executedTasks) {
112            int occurrenceCount = taskInstancesMap.get(currentTaskInstance).size();
113           
114            if ((numberOfTrainedSubsequences % 10) == 0) {
115                sequenceMaxCount = getCurrentSequenceMaxCount();
116            }
117           
118            if (occurrenceCount < sequenceMaxCount) {
119                // this task instance does not need to be considered, as it occurs not often enough
120                // to be part of a sequence, that occurs most often. Therefore train all remaining
121                // sequences so far and go on, until the next useful sequence is found.
122               
123                while (subsequence.size() > 1) {
124                    add(subsequence);
125                    subsequence.remove(0);
126                }
127               
128                subsequence.clear();
129            }
130            else {
131                subsequence.add(currentTaskInstance);
132
133                if (subsequence.size() == maxOrder) {
134                    add(subsequence);
135                    subsequence.remove(0);
136
137                    numberOfTrainedSubsequences++;
138                }
139            }
140        }
141       
142        // add shorter remaining subsequences, if any
143        while (subsequence.size() > 1) {
144            add(subsequence);
145            subsequence.remove(0);
146        }
147    }
148
149    /**
150     * <p>
151     * TODO: comment
152     * </p>
153     *
154     * @return
155     */
156    private int getCurrentSequenceMaxCount() {
157        MaxSequenceCountFinder processor = new MaxSequenceCountFinder();
158        super.process(processor);
159        return processor.getMaxCount();
160    }
161
162    /**
163     * @author Patrick Harms
164     */
165    private class MaxSequenceCountFinder implements TrieProcessor<ITaskInstance> {
166       
167        /**
168         *
169         */
170        private int currentCount = 0;
171       
172        /* (non-Javadoc)
173         * @see de.ugoe.cs.autoquest.usageprofiles.TrieProcessor#process(java.util.List, int)
174         */
175        @Override
176        public TrieProcessor.Result process(List<ITaskInstance> foundTask, int count) {
177            if (foundTask.size() == 2) {
178                this.currentCount = Math.max(this.currentCount, count);
179               
180                // do not consider children
181                return TrieProcessor.Result.SKIP_NODE;
182            }
183            else {
184                return TrieProcessor.Result.CONTINUE;
185            }
186        }
187
188        /**
189         *
190         */
191        private int getMaxCount() {
192            return currentCount;
193        }
194
195    }
196   
197}
Note: See TracBrowser for help on using the repository browser.