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

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