source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/TaskComparator.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: 6.7 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.io.IOException;
18import java.io.ObjectInputStream;
19import java.util.HashMap;
20
21import de.ugoe.cs.autoquest.tasktrees.taskequality.TaskEquality;
22import de.ugoe.cs.autoquest.tasktrees.taskequality.TaskEqualityRuleManager;
23import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
24import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
25import de.ugoe.cs.autoquest.usageprofiles.SymbolComparator;
26
27/**
28 * TODO comment
29 */
30class TaskComparator implements SymbolComparator<ITaskInstance> {
31   
32    /**  */
33    private static final long serialVersionUID = 1L;
34   
35    /** */
36    private static final int MAX_BUFFER_SIZE = 2 * 1024 * 1024;
37
38    /** */
39    private TaskEquality minimalNodeEquality;
40
41    /** */
42    private transient Comparer comparer;
43
44    /** */
45    private transient Comparer lexicalComparer;
46
47    /** */
48    private transient HashMap<Long, Boolean> equalityBuffer = new HashMap<Long, Boolean>();
49
50    /** */
51    private transient HashMap<Long, Boolean> lexicalEqualityBuffer;
52
53    /**
54     *
55     */
56    public TaskComparator(TaskEquality minimalNodeEquality) {
57        this.minimalNodeEquality = minimalNodeEquality;
58        init();
59    }
60
61    /* (non-Javadoc)
62     * @see SymbolComparator#equals(Object, Object)
63     */
64    @Override
65    public boolean equals(ITaskInstance taskInstance1, ITaskInstance taskInstance2) {
66        return equals(taskInstance1.getTask(), taskInstance2.getTask());
67    }       
68
69    /**
70     *
71     */
72    public boolean equals(ITask task1, ITask task2) {
73        Boolean result;
74       
75        if (task1 != task2) {
76            //if ((task1 instanceof IEventTask) && (task2 instanceof IEventTask)) {
77                long key = ((long) System.identityHashCode(task1)) << 32;
78                key += System.identityHashCode(task2);
79           
80                result = equalityBuffer.get(key);
81           
82                if (result == null) {
83                    result = comparer.compare(task1, task2);
84                   
85                    if (equalityBuffer.size() < MAX_BUFFER_SIZE) {
86                        equalityBuffer.put(key, result);
87                    }
88                }
89            /*}
90            else {
91                result = false;
92            }*/
93        }
94        else {
95            result = true;
96        }
97       
98        return result;
99    }
100
101    /**
102     *
103     */
104    public boolean areLexicallyEqual(ITask task1, ITask task2) {
105        Boolean result;
106       
107        if (task1 != task2) {
108            long key = ((long) System.identityHashCode(task1)) << 32;
109            key += System.identityHashCode(task2);
110           
111            result = lexicalEqualityBuffer.get(key);
112           
113            if (result == null) {
114                result = lexicalComparer.compare(task1, task2);
115                if (equalityBuffer.size() < MAX_BUFFER_SIZE) {
116                    lexicalEqualityBuffer.put(key, result);
117                }
118            }
119        }
120        else {
121            result = true;
122        }
123       
124        return result;
125    }
126   
127    /**
128     * <p>
129     * TODO: comment
130     * </p>
131     *
132     */
133    void clearBuffers() {
134        equalityBuffer.clear();
135        init();
136    }
137   
138    /**
139     *
140     */
141    private void init() {
142        if (minimalNodeEquality == TaskEquality.LEXICALLY_EQUAL) {
143            comparer = new LexicalComparer();
144        }
145        else if (minimalNodeEquality == TaskEquality.SYNTACTICALLY_EQUAL) {
146            comparer = new SyntacticalComparer();
147        }
148        else if (minimalNodeEquality == TaskEquality.SEMANTICALLY_EQUAL) {
149            comparer = new SemanticalComparer();
150        }
151        else {
152            comparer = new DefaultComparer(this.minimalNodeEquality);
153        }
154       
155        if (minimalNodeEquality == TaskEquality.LEXICALLY_EQUAL) {
156            lexicalComparer = comparer;
157            lexicalEqualityBuffer = equalityBuffer;
158        }
159        else {
160            lexicalComparer = new LexicalComparer();
161            lexicalEqualityBuffer = new HashMap<Long, Boolean>();
162        }
163    }
164   
165    /**
166     * <p>
167     * deserialize this object and reinitialize the buffers
168     * </p>
169     */
170    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
171        in.defaultReadObject();
172        init();
173    }
174
175
176    /**
177     *
178     */
179    private static interface Comparer {
180       
181        /**
182         *
183         */
184        boolean compare(ITask task1, ITask task2);
185    }
186
187    /**
188     *
189     */
190    private static class LexicalComparer implements Comparer {
191       
192        /**
193         *
194         */
195        public boolean compare(ITask task1, ITask task2) {
196            return TaskEqualityRuleManager.getInstance().areLexicallyEqual(task1, task2);
197        }
198    }
199
200    /**
201     *
202     */
203    private static class SyntacticalComparer implements Comparer {
204       
205        /**
206         *
207         */
208        public boolean compare(ITask task1, ITask task2) {
209            return TaskEqualityRuleManager.getInstance().areSyntacticallyEqual(task1, task2);
210        }
211    }
212
213    /**
214     *
215     */
216    private static class SemanticalComparer implements Comparer {
217       
218        /**
219         *
220         */
221        public boolean compare(ITask task1, ITask task2) {
222            return TaskEqualityRuleManager.getInstance().areSemanticallyEqual(task1, task2);
223        }
224    }
225
226    /**
227     *
228     */
229    private static class DefaultComparer implements Comparer {
230       
231        /**
232         * <p>
233         * the minimal task equality two identified sublists need to have to consider them as equal
234         * </p>
235         */
236        private TaskEquality minimalNodeEquality;
237       
238        /**
239         *
240         */
241        public DefaultComparer(TaskEquality minimalNodeEquality) {
242           this.minimalNodeEquality = minimalNodeEquality;
243        }
244       
245        /**
246         *
247         */
248        public boolean compare(ITask task1, ITask task2) {
249            return TaskEqualityRuleManager.getInstance().areAtLeastEqual
250                (task1, task2, minimalNodeEquality);
251        }
252    }
253
254}
Note: See TracBrowser for help on using the repository browser.