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

Last change on this file since 1285 was 1285, checked in by pharms, 11 years ago
  • improved performance of task instance trie generation by using different symbol management strategies while creating the trie. This performance improvement is significant and allows to detect tasks now in a much faster manner.
File size: 7.1 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.ITask;
23import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
24import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession;
25import de.ugoe.cs.autoquest.usageprofiles.SymbolMap;
26import de.ugoe.cs.autoquest.usageprofiles.Trie;
27import de.ugoe.cs.autoquest.usageprofiles.TrieProcessor;
28
29/**
30 * <p>
31 * TODO comment
32 * </p>
33 *
34 * @author Patrick Harms
35 */
36class TaskInstanceTrie extends Trie<ITaskInstance> {
37
38    /**  */
39    private static final long serialVersionUID = 1L;
40
41    /**
42     * <p>
43     * the task comparator to be used for comparing tasks
44     * </p>
45     */
46    private TaskHandlingStrategy taskStrategy;
47   
48    /**
49     * <p>
50     * TODO: comment
51     * </p>
52     *
53     * @param taskComparator2
54     */
55    public TaskInstanceTrie(TaskHandlingStrategy taskStrategy) {
56        super(taskStrategy);
57        this.taskStrategy = taskStrategy;
58    }
59
60    /**
61     *
62     */
63    public void trainSessions(List<IUserSession> userSessions, int maxOrder) {
64        if (maxOrder < 1) {
65            return;
66        }
67       
68        SymbolMap<ITaskInstance, Counter> equalTaskInstancesMap =
69            taskStrategy.createSymbolMap();
70       
71        Map<ITask, Counter> instanceCountMap = new HashMap<ITask, Counter>();
72       
73        System.out.println("preparing training");
74        int noOfTaskInstances = 0;
75        for (IUserSession session : userSessions) {
76            for (ITaskInstance taskInstance : session) {
77                Counter counter = equalTaskInstancesMap.getValue(taskInstance);
78               
79                if (counter == null) {
80                    counter = new Counter();
81                    equalTaskInstancesMap.addSymbol(taskInstance, counter);
82                }
83               
84                counter.count++;
85                instanceCountMap.put(taskInstance.getTask(), counter);
86               
87                noOfTaskInstances++;
88            }
89        }
90       
91        System.out.println("performing training of " + noOfTaskInstances + " task instances");
92        Counter processedTaskInstances = new Counter();
93        int counterRecheckAt = noOfTaskInstances / 10; // recheck the maximum count after each
94                                                       // 10% of processed task instances
95        for (IUserSession session : userSessions) {
96            train(session, maxOrder, instanceCountMap, processedTaskInstances, counterRecheckAt);
97        }     
98       
99        updateKnownSymbols();
100    }
101   
102    /* (non-Javadoc)
103     * @see de.ugoe.cs.autoquest.usageprofiles.Trie#equals(java.lang.Object)
104     */
105    @Override
106    public boolean equals(Object other) {
107        if (this == other) {
108            return true;
109        }
110        else if (other instanceof TaskInstanceTrie) {
111            return super.equals(other);
112        }
113        else {
114            return false;
115        }
116    }
117
118    /* (non-Javadoc)
119     * @see de.ugoe.cs.autoquest.usageprofiles.Trie#hashCode()
120     */
121    @Override
122    public int hashCode() {
123        return super.hashCode();
124    }
125
126    /**
127     *
128     */
129    private void train(IUserSession        userSession,
130                       int                 maxOrder,
131                       Map<ITask, Counter> taskInstanceCountMap,
132                       Counter             processedTaskInstances,
133                       int                 counterRecheckAt)
134    {
135        List<ITaskInstance> executedTasks = userSession.getExecutedTasks();
136       
137        List<ITaskInstance> subsequence = new LinkedList<ITaskInstance>();
138       
139        int sequenceMaxCount = 0;
140       
141        for (ITaskInstance currentTaskInstance : executedTasks) {
142           
143            int occurrenceCount = taskInstanceCountMap.get(currentTaskInstance.getTask()).count;
144           
145            if (processedTaskInstances.count >= counterRecheckAt) {
146                sequenceMaxCount = getCurrentSequenceMaxCount();
147                processedTaskInstances.count = 0;
148            }
149           
150            if (occurrenceCount < sequenceMaxCount) {
151                // this task instance does not need to be considered, as it occurs not often enough
152                // to be part of a sequence, that occurs most often. Therefore train all remaining
153                // sequences so far and go on, until the next useful sequence is found.
154               
155                while (subsequence.size() > 1) {
156                    add(subsequence);
157                    subsequence.remove(0);
158                }
159               
160                subsequence.clear();
161            }
162            else {
163                subsequence.add(currentTaskInstance);
164
165                if (subsequence.size() == maxOrder) {
166                    add(subsequence);
167                    subsequence.remove(0);
168                }
169            }
170           
171            processedTaskInstances.count++;
172        }
173       
174        // add shorter remaining subsequences, if any
175        while (subsequence.size() > 1) {
176            add(subsequence);
177            subsequence.remove(0);
178        }
179    }
180
181    /**
182     * <p>
183     * TODO: comment
184     * </p>
185     *
186     * @return
187     */
188    private int getCurrentSequenceMaxCount() {
189        MaxSequenceCountFinder processor = new MaxSequenceCountFinder();
190        super.process(processor);
191        return processor.getMaxCount();
192    }
193
194    /**
195     * @author Patrick Harms
196     */
197    private static class MaxSequenceCountFinder implements TrieProcessor<ITaskInstance> {
198       
199        /**
200         *
201         */
202        private int currentCount = 0;
203       
204        /* (non-Javadoc)
205         * @see de.ugoe.cs.autoquest.usageprofiles.TrieProcessor#process(java.util.List, int)
206         */
207        @Override
208        public TrieProcessor.Result process(List<ITaskInstance> foundTask, int count) {
209            if (foundTask.size() == 2) {
210                this.currentCount = Math.max(this.currentCount, count);
211               
212                // do not consider children
213                return TrieProcessor.Result.SKIP_NODE;
214            }
215            else {
216                return TrieProcessor.Result.CONTINUE;
217            }
218        }
219
220        /**
221         *
222         */
223        private int getMaxCount() {
224            return currentCount;
225        }
226
227    }
228   
229    /**
230     * @author Patrick Harms
231     */
232    private static class Counter {
233        int count = 0;
234    }
235       
236}
Note: See TracBrowser for help on using the repository browser.