source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/TaskComparator.java @ 1146

Last change on this file since 1146 was 1146, checked in by pharms, 11 years ago
  • complete refactoring of task tree model with a separation of task models and task instances
  • appropriate adaptation of task tree generation process
  • appropriate adaptation of commands and task tree visualization
File size: 6.5 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.taskequality;
16
17import java.util.HashMap;
18
19import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
20import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
21import de.ugoe.cs.autoquest.usageprofiles.SymbolComparator;
22import de.ugoe.cs.util.StopWatch;
23
24/**
25 * <p>
26 * TODO comment
27 * </p>
28 *
29 * @author Patrick Harms
30 */
31public class TaskComparator implements SymbolComparator<ITaskInstance> {
32   
33    /**
34     * <p>
35     * the task equality manager needed for comparing tasks with each other
36     * </p>
37     */
38    private TaskEqualityRuleManager taskEqualityRuleManager;
39
40    /**
41     * <p>
42     * the minimal task equality two identified sublists need to have to consider them as equal
43     * </p>
44     */
45    private TaskEquality minimalNodeEquality;
46
47    /** */
48    private Comparer comparer;
49
50    /** */
51    private Comparer lexicalComparer;
52
53    /** */
54    private StopWatch stopWatch = new StopWatch();
55   
56    /** */
57    private HashMap<Long, Boolean> equalityBuffer = new HashMap<Long, Boolean>();
58
59    /** */
60    private HashMap<Long, Boolean> lexicalEqualityBuffer;
61
62    /**
63     *
64     */
65    public TaskComparator(TaskEqualityRuleManager taskEqualityRuleManager,
66                                  TaskEquality            minimalNodeEquality)
67    {
68        super();
69        this.taskEqualityRuleManager = taskEqualityRuleManager;
70        this.minimalNodeEquality = minimalNodeEquality;
71       
72        if (minimalNodeEquality == TaskEquality.LEXICALLY_EQUAL) {
73            comparer = new LexicalComparer();
74        }
75        else if (minimalNodeEquality == TaskEquality.SYNTACTICALLY_EQUAL) {
76            comparer = new SyntacticalComparer();
77        }
78        else if (minimalNodeEquality == TaskEquality.SEMANTICALLY_EQUAL) {
79            comparer = new SemanticalComparer();
80        }
81        else {
82            comparer = new DefaultComparer();
83        }
84       
85        if (minimalNodeEquality == TaskEquality.LEXICALLY_EQUAL) {
86            lexicalComparer = comparer;
87            lexicalEqualityBuffer = equalityBuffer;
88        }
89        else {
90            lexicalComparer = new LexicalComparer();
91            lexicalEqualityBuffer = new HashMap<Long, Boolean>();
92        }
93    }
94
95    /* (non-Javadoc)
96     * @see de.ugoe.cs.autoquest.tasktrees.temporalrelation.SymbolComparator#equals(java.lang.Object, java.lang.Object)
97     */
98    @Override
99    public boolean equals(ITaskInstance taskInstance1, ITaskInstance taskInstance2) {
100        return equals(taskInstance1.getTask(), taskInstance2.getTask());
101    }       
102
103    /**
104     *
105     */
106    public boolean equals(ITask task1, ITask task2) {
107        //String id = "compare " + taskInstance1.getClass().getSimpleName() + " " +
108        //    taskInstance2.getClass().getSimpleName();
109        //String id = "compare";
110        //stopWatch.start(id);
111       
112        Boolean result;
113       
114        if (task1 != task2) {
115            long key = ((long) System.identityHashCode(task1)) << 32;
116            key += System.identityHashCode(task2);
117           
118            result = equalityBuffer.get(key);
119           
120            if (result == null) {
121                result = comparer.compare(task1, task2);
122                equalityBuffer.put(key, result);
123            }
124        }
125        else {
126            result = true;
127        }
128        //stopWatch.stop(id);
129       
130        /*boolean result2 = taskEqualityRuleManager.areAtLeastEqual(symbol1, symbol2, minimalNodeEquality);
131        if (result != result2) {
132            throw new IllegalStateException("implementation error");
133        }*/
134       
135        return result;
136    }
137
138    /**
139     *
140     */
141    public boolean haveLexicallyEqualTasks(ITaskInstance taskInstance1,
142                                           ITaskInstance taskInstance2)
143    {
144        return areLexicallyEqual(taskInstance1.getTask(), taskInstance2.getTask());
145    }
146       
147
148    /**
149     *
150     */
151    public boolean areLexicallyEqual(ITask task1, ITask task2) {
152        Boolean result;
153       
154        if (task1 != task2) {
155            long key = ((long) System.identityHashCode(task1)) << 32;
156            key += System.identityHashCode(task2);
157           
158            result = lexicalEqualityBuffer.get(key);
159           
160            if (result == null) {
161                result = lexicalComparer.compare(task1, task2);
162                lexicalEqualityBuffer.put(key, result);
163            }
164        }
165        else {
166            result = true;
167        }
168       
169        return result;
170    }
171
172    /**
173     *
174     */
175    StopWatch getStopWatch() {
176        return stopWatch;
177    }
178
179    /**
180     *
181     */
182    TaskEquality getConsideredNodeEquality() {
183        return minimalNodeEquality;
184    }
185
186    /**
187     *
188     */
189    private interface Comparer {
190        /**
191         *
192         */
193        boolean compare(ITask task1, ITask task2);
194    }
195
196    /**
197     *
198     */
199    private class LexicalComparer implements Comparer {
200       
201        /**
202         *
203         */
204        public boolean compare(ITask task1, ITask task2) {
205            return taskEqualityRuleManager.areLexicallyEqual(task1, task2);
206        }
207    }
208
209    /**
210     *
211     */
212    private class SyntacticalComparer implements Comparer {
213       
214        /**
215         *
216         */
217        public boolean compare(ITask task1, ITask task2) {
218            return taskEqualityRuleManager.areSyntacticallyEqual(task1, task2);
219        }
220    }
221
222    /**
223     *
224     */
225    private class SemanticalComparer implements Comparer {
226       
227        /**
228         *
229         */
230        public boolean compare(ITask task1, ITask task2) {
231            return taskEqualityRuleManager.areSemanticallyEqual(task1, task2);
232        }
233    }
234
235    /**
236     *
237     */
238    private class DefaultComparer implements Comparer {
239       
240        /**
241         *
242         */
243        public boolean compare(ITask task1, ITask task2) {
244            return taskEqualityRuleManager.areAtLeastEqual(task1, task2, minimalNodeEquality);
245        }
246    }
247}
Note: See TracBrowser for help on using the repository browser.