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

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