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

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