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 | |
---|
15 | package de.ugoe.cs.autoquest.tasktrees.taskequality; |
---|
16 | |
---|
17 | import java.util.Collection; |
---|
18 | |
---|
19 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
20 | import de.ugoe.cs.autoquest.eventcore.gui.Scroll; |
---|
21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance; |
---|
23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
25 | |
---|
26 | /** |
---|
27 | * <p> |
---|
28 | * This comparison rule compares two events if they represent inefficient actions. Which |
---|
29 | * events are considered inefficient actions is checked in the method |
---|
30 | * {@link #isInefficientAction(Event)}. Two events are considered lexically equal, if |
---|
31 | * both represent an inefficient action. There is no further distinction so far. |
---|
32 | * </p> |
---|
33 | * |
---|
34 | * @author Patrick Harms |
---|
35 | */ |
---|
36 | public class InefficientActionsComparisonRule implements TaskComparisonRule { |
---|
37 | |
---|
38 | /* (non-Javadoc) |
---|
39 | * @see TaskComparisonRule#isApplicable(ITask, ITask) |
---|
40 | */ |
---|
41 | @Override |
---|
42 | public boolean isApplicable(ITask task1, ITask task2) { |
---|
43 | if ((task1 instanceof IEventTask) && (task2 instanceof IEventTask)) { |
---|
44 | IEventTaskInstance instance1 = |
---|
45 | (IEventTaskInstance) task1.getInstances().iterator().next(); |
---|
46 | |
---|
47 | IEventTaskInstance instance2 = |
---|
48 | (IEventTaskInstance) task2.getInstances().iterator().next(); |
---|
49 | |
---|
50 | return isApplicable(instance1, instance2); |
---|
51 | } |
---|
52 | else { |
---|
53 | return false; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | /* (non-Javadoc) |
---|
58 | * @see TaskComparisonRule#areLexicallyEqual(ITask, ITask) |
---|
59 | */ |
---|
60 | @Override |
---|
61 | public boolean areLexicallyEqual(ITask task1, ITask task2) { |
---|
62 | Collection<ITaskInstance> taskInstances1 = task1.getInstances(); |
---|
63 | Collection<ITaskInstance> taskInstances2 = task2.getInstances(); |
---|
64 | |
---|
65 | for (ITaskInstance instance1 : taskInstances1) { |
---|
66 | boolean found = false; |
---|
67 | |
---|
68 | for (ITaskInstance instance2 : taskInstances2) { |
---|
69 | if (areLexicallyEqual(instance1, instance2)) { |
---|
70 | found = true; |
---|
71 | break; |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | if (!found) { |
---|
76 | return false; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | return true; |
---|
81 | } |
---|
82 | |
---|
83 | /* (non-Javadoc) |
---|
84 | * @see TaskComparisonRule#areSyntacticallyEqual(ITask, ITask) |
---|
85 | */ |
---|
86 | @Override |
---|
87 | public boolean areSyntacticallyEqual(ITask task1, ITask task2) { |
---|
88 | return areLexicallyEqual(task1, task2); |
---|
89 | } |
---|
90 | |
---|
91 | /* (non-Javadoc) |
---|
92 | * @see TaskComparisonRule#areSemanticallyEqual(ITask, ITask) |
---|
93 | */ |
---|
94 | @Override |
---|
95 | public boolean areSemanticallyEqual(ITask task1, ITask task2) { |
---|
96 | return areLexicallyEqual(task1, task2); |
---|
97 | } |
---|
98 | |
---|
99 | /* (non-Javadoc) |
---|
100 | * @see TaskComparisonRule#compare(ITask, ITask) |
---|
101 | */ |
---|
102 | @Override |
---|
103 | public TaskEquality compare(ITask task1, ITask task2) { |
---|
104 | if (areLexicallyEqual(task1, task2)) { |
---|
105 | return TaskEquality.LEXICALLY_EQUAL; |
---|
106 | } |
---|
107 | else { |
---|
108 | return TaskEquality.UNEQUAL; |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | /* (non-Javadoc) |
---|
114 | * @see TaskComparisonRule#isApplicable(ITaskInstance, ITaskInstance) |
---|
115 | */ |
---|
116 | @Override |
---|
117 | public boolean isApplicable(ITaskInstance instance1, ITaskInstance instance2) { |
---|
118 | return |
---|
119 | (instance1 instanceof IEventTaskInstance) && |
---|
120 | (instance2 instanceof IEventTaskInstance) && |
---|
121 | (isInefficientAction(((IEventTaskInstance) instance1).getEvent())) && |
---|
122 | (isInefficientAction(((IEventTaskInstance) instance2).getEvent())); |
---|
123 | } |
---|
124 | |
---|
125 | /* (non-Javadoc) |
---|
126 | * @see TaskComparisonRule#areLexicallyEqual(ITaskInstance, ITaskInstance) |
---|
127 | */ |
---|
128 | @Override |
---|
129 | public boolean areLexicallyEqual(ITaskInstance instance1, ITaskInstance instance2) { |
---|
130 | return |
---|
131 | (isInefficientAction(((IEventTaskInstance) instance1).getEvent())) && |
---|
132 | (isInefficientAction(((IEventTaskInstance) instance2).getEvent())); |
---|
133 | } |
---|
134 | |
---|
135 | /* (non-Javadoc) |
---|
136 | * @see TaskComparisonRule#areSyntacticallyEqual(ITaskInstance, ITaskInstance) |
---|
137 | */ |
---|
138 | @Override |
---|
139 | public boolean areSyntacticallyEqual(ITaskInstance instance1, ITaskInstance instance2) { |
---|
140 | return areLexicallyEqual(instance1, instance2); |
---|
141 | } |
---|
142 | |
---|
143 | /* (non-Javadoc) |
---|
144 | * @see TaskComparisonRule#areSemanticallyEqual(ITaskInstance, ITaskInstance) |
---|
145 | */ |
---|
146 | @Override |
---|
147 | public boolean areSemanticallyEqual(ITaskInstance instance1, ITaskInstance instance2) { |
---|
148 | return areLexicallyEqual(instance1, instance2); |
---|
149 | } |
---|
150 | |
---|
151 | /* (non-Javadoc) |
---|
152 | * @see TaskComparisonRule#compare(ITaskInstance, ITaskInstance) |
---|
153 | */ |
---|
154 | @Override |
---|
155 | public TaskEquality compare(ITaskInstance instance1, ITaskInstance instance2) { |
---|
156 | if (areLexicallyEqual(instance1, instance2)) { |
---|
157 | return TaskEquality.LEXICALLY_EQUAL; |
---|
158 | } |
---|
159 | else { |
---|
160 | return TaskEquality.UNEQUAL; |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * <p> |
---|
166 | * This method checks, if an event represents an inefficient action. This is currently the case |
---|
167 | * if the event type is a {@link Scroll} or if its string representation is either "headRotated" |
---|
168 | * or "headMoved". |
---|
169 | * </p> |
---|
170 | * |
---|
171 | * @param event the event to check if it is an inefficient action |
---|
172 | * |
---|
173 | * @return as described |
---|
174 | */ |
---|
175 | private boolean isInefficientAction(Event event) { |
---|
176 | return (event.getType() instanceof Scroll) || |
---|
177 | ("headRotated".equals(event.getType().toString())) || |
---|
178 | ("headMoved".equals(event.getType().toString())); |
---|
179 | } |
---|
180 | } |
---|