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.tasktrees.treeifc.IEventTask; |
---|
20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance; |
---|
21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
23 | |
---|
24 | /** |
---|
25 | * <p> |
---|
26 | * This rule identifies two tasks as lexically equal, if they are both event tasks and |
---|
27 | * if their respective event types and targets equal. |
---|
28 | * </p> |
---|
29 | * |
---|
30 | * @author Patrick Harms |
---|
31 | */ |
---|
32 | public class EventTaskComparisonRule implements TaskComparisonRule { |
---|
33 | |
---|
34 | /* (non-Javadoc) |
---|
35 | * @see TaskComparisonRule#isApplicable(ITask, ITask) |
---|
36 | */ |
---|
37 | @Override |
---|
38 | public boolean isApplicable(ITask task1, ITask task2) { |
---|
39 | return (task1 instanceof IEventTask) && (task2 instanceof IEventTask); |
---|
40 | } |
---|
41 | |
---|
42 | /* (non-Javadoc) |
---|
43 | * @see TaskComparisonRule#areLexicallyEqual(ITask, ITask) |
---|
44 | */ |
---|
45 | @Override |
---|
46 | public boolean areLexicallyEqual(ITask task1, ITask task2) { |
---|
47 | Collection<ITaskInstance> taskInstances1 = task1.getInstances(); |
---|
48 | Collection<ITaskInstance> taskInstances2 = task2.getInstances(); |
---|
49 | |
---|
50 | for (ITaskInstance instance1 : taskInstances1) { |
---|
51 | boolean found = false; |
---|
52 | |
---|
53 | for (ITaskInstance instance2 : taskInstances2) { |
---|
54 | if (areLexicallyEqual(instance1, instance2)) { |
---|
55 | found = true; |
---|
56 | break; |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | if (!found) { |
---|
61 | return false; |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | return true; |
---|
66 | } |
---|
67 | |
---|
68 | /* (non-Javadoc) |
---|
69 | * @see TaskComparisonRule#areSyntacticallyEqual(ITask, ITask) |
---|
70 | */ |
---|
71 | @Override |
---|
72 | public boolean areSyntacticallyEqual(ITask task1, ITask task2) { |
---|
73 | return areLexicallyEqual(task1, task2); |
---|
74 | } |
---|
75 | |
---|
76 | /* (non-Javadoc) |
---|
77 | * @see TaskComparisonRule#areSemanticallyEqual(ITask, ITask) |
---|
78 | */ |
---|
79 | @Override |
---|
80 | public boolean areSemanticallyEqual(ITask task1, ITask task2) { |
---|
81 | return areLexicallyEqual(task1, task2); |
---|
82 | } |
---|
83 | |
---|
84 | /* (non-Javadoc) |
---|
85 | * @see TaskComparisonRule#compare(ITask, ITask) |
---|
86 | */ |
---|
87 | @Override |
---|
88 | public TaskEquality compare(ITask task1, ITask task2) { |
---|
89 | if (areLexicallyEqual(task1, task2)) { |
---|
90 | return TaskEquality.LEXICALLY_EQUAL; |
---|
91 | } |
---|
92 | else { |
---|
93 | return TaskEquality.UNEQUAL; |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | /* (non-Javadoc) |
---|
99 | * @see TaskComparisonRule#isApplicable(ITaskInstance, ITaskInstance) |
---|
100 | */ |
---|
101 | @Override |
---|
102 | public boolean isApplicable(ITaskInstance instance1, ITaskInstance instance2) { |
---|
103 | return |
---|
104 | (instance1 instanceof IEventTaskInstance) && (instance2 instanceof IEventTaskInstance); |
---|
105 | } |
---|
106 | |
---|
107 | /* (non-Javadoc) |
---|
108 | * @see TaskComparisonRule#areLexicallyEqual(ITaskInstance, ITaskInstance) |
---|
109 | */ |
---|
110 | @Override |
---|
111 | public boolean areLexicallyEqual(ITaskInstance instance1, ITaskInstance instance2) { |
---|
112 | IEventTaskInstance eventTask1 = (IEventTaskInstance) instance1; |
---|
113 | IEventTaskInstance eventTask2 = (IEventTaskInstance) instance2; |
---|
114 | |
---|
115 | return (eventTask1.getEvent().getType().equals(eventTask2.getEvent().getType()) && |
---|
116 | eventTask1.getEvent().getTarget().equals(eventTask2.getEvent().getTarget())); |
---|
117 | } |
---|
118 | |
---|
119 | /* (non-Javadoc) |
---|
120 | * @see TaskComparisonRule#areSyntacticallyEqual(ITaskInstance, ITaskInstance) |
---|
121 | */ |
---|
122 | @Override |
---|
123 | public boolean areSyntacticallyEqual(ITaskInstance instance1, ITaskInstance instance2) { |
---|
124 | return areLexicallyEqual(instance1, instance2); |
---|
125 | } |
---|
126 | |
---|
127 | /* (non-Javadoc) |
---|
128 | * @see TaskComparisonRule#areSemanticallyEqual(ITaskInstance, ITaskInstance) |
---|
129 | */ |
---|
130 | @Override |
---|
131 | public boolean areSemanticallyEqual(ITaskInstance instance1, ITaskInstance instance2) { |
---|
132 | return areLexicallyEqual(instance1, instance2); |
---|
133 | } |
---|
134 | |
---|
135 | /* (non-Javadoc) |
---|
136 | * @see TaskComparisonRule#compare(ITaskInstance, ITaskInstance) |
---|
137 | */ |
---|
138 | @Override |
---|
139 | public TaskEquality compare(ITaskInstance instance1, ITaskInstance instance2) { |
---|
140 | if (areLexicallyEqual(instance1, instance2)) { |
---|
141 | return TaskEquality.LEXICALLY_EQUAL; |
---|
142 | } |
---|
143 | else { |
---|
144 | return TaskEquality.UNEQUAL; |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | } |
---|