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