1 | /* |
---|
2 | * |
---|
3 | */ |
---|
4 | |
---|
5 | package de.ugoe.cs.autoquest.tasktrees.alignment.algorithms; |
---|
6 | |
---|
7 | import java.io.Serializable; |
---|
8 | import java.util.LinkedList; |
---|
9 | import java.util.Random; |
---|
10 | |
---|
11 | // TODO: Auto-generated Javadoc |
---|
12 | /** |
---|
13 | * The Class NumberSequence. |
---|
14 | */ |
---|
15 | public class NumberSequence implements Serializable { |
---|
16 | |
---|
17 | /** The Constant serialVersionUID. */ |
---|
18 | private static final long serialVersionUID = -4604570107534055589L; |
---|
19 | |
---|
20 | /** The sequence. */ |
---|
21 | private int[] sequence; |
---|
22 | |
---|
23 | /** The id. */ |
---|
24 | private int id; |
---|
25 | |
---|
26 | /** |
---|
27 | * Instantiates a new number sequence. |
---|
28 | * |
---|
29 | * @param size |
---|
30 | * the size |
---|
31 | */ |
---|
32 | public NumberSequence(int size) { |
---|
33 | |
---|
34 | sequence = new int[size]; |
---|
35 | } |
---|
36 | |
---|
37 | // Searching occurrences of pattern |
---|
38 | /** |
---|
39 | * Contains pattern. |
---|
40 | * |
---|
41 | * @param pattern |
---|
42 | * the pattern |
---|
43 | * @return the linked list |
---|
44 | */ |
---|
45 | public LinkedList<Integer> containsPattern(Match pattern) { |
---|
46 | final LinkedList<Integer> result = new LinkedList<Integer>(); |
---|
47 | int i = 0; |
---|
48 | final int[] pat1 = pattern.getFirstSequence().getSequence(); |
---|
49 | final int[] pat2 = pattern.getSecondSequence().getSequence(); |
---|
50 | |
---|
51 | while (i < sequence.length) { |
---|
52 | if (matches(i, pat1, pat2, 0, 0, false, false, false, false, false)) { |
---|
53 | result.add(i); |
---|
54 | } |
---|
55 | i++; |
---|
56 | } |
---|
57 | return result; |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * Equals. |
---|
62 | * |
---|
63 | * @param n |
---|
64 | * the n |
---|
65 | * @return true, if successful |
---|
66 | */ |
---|
67 | public boolean equals(NumberSequence n) { |
---|
68 | final int[] seq = n.getSequence(); |
---|
69 | if (n.size() != this.size()) { |
---|
70 | return false; |
---|
71 | } |
---|
72 | for (int i = 0; i < n.size(); i++) { |
---|
73 | if (seq[i] != this.sequence[i]) { |
---|
74 | return false; |
---|
75 | } |
---|
76 | } |
---|
77 | return true; |
---|
78 | } |
---|
79 | |
---|
80 | // Returns the number of times event occurs in this sequence |
---|
81 | /** |
---|
82 | * Event count. |
---|
83 | * |
---|
84 | * @param event |
---|
85 | * the event |
---|
86 | * @return the int |
---|
87 | */ |
---|
88 | public int eventCount(int event) { |
---|
89 | int count = 0; |
---|
90 | for (int i = 0; i < sequence.length; i++) { |
---|
91 | if (sequence[i] == event) { |
---|
92 | count++; |
---|
93 | } |
---|
94 | } |
---|
95 | return count; |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * Gets the id. |
---|
100 | * |
---|
101 | * @return the id |
---|
102 | */ |
---|
103 | public int getId() { |
---|
104 | return id; |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Gets the sequence. |
---|
109 | * |
---|
110 | * @return the sequence |
---|
111 | */ |
---|
112 | public int[] getSequence() { |
---|
113 | return sequence; |
---|
114 | } |
---|
115 | |
---|
116 | // Recursive check if sequence contains pattern at position i |
---|
117 | /** |
---|
118 | * Matches. |
---|
119 | * |
---|
120 | * @param i |
---|
121 | * the position in the sequence |
---|
122 | * @param p1 |
---|
123 | * the p1 |
---|
124 | * @param p2 |
---|
125 | * the p2 |
---|
126 | * @param ip1 |
---|
127 | * the ip1 |
---|
128 | * @param ip2 |
---|
129 | * the ip2 |
---|
130 | * @param jumped1 |
---|
131 | * the jumped1 |
---|
132 | * @param jumped2 |
---|
133 | * the jumped2 |
---|
134 | * @param hadSelection |
---|
135 | * the had selection |
---|
136 | * @param matchseq1 |
---|
137 | * the matchseq1 |
---|
138 | * @param matchseq2 |
---|
139 | * the matchseq2 |
---|
140 | * @return true, if successful |
---|
141 | */ |
---|
142 | private boolean matches(int i, int[] p1, int[] p2, int ip1, int ip2, boolean jumped1, // True if |
---|
143 | // there |
---|
144 | // was a |
---|
145 | // gap in |
---|
146 | // Sequence |
---|
147 | // 1 of |
---|
148 | // the |
---|
149 | // pattern |
---|
150 | boolean jumped2, // True if there was a gap in Sequence 2 of the |
---|
151 | // pattern |
---|
152 | boolean hadSelection, // True if the last match was a selection |
---|
153 | boolean matchseq1, |
---|
154 | boolean matchseq2) |
---|
155 | { |
---|
156 | |
---|
157 | if (p1.length == ip1) { |
---|
158 | return true; |
---|
159 | } |
---|
160 | if (p2.length == ip2) { |
---|
161 | return true; |
---|
162 | } |
---|
163 | if (i == sequence.length) { |
---|
164 | return false; |
---|
165 | } |
---|
166 | |
---|
167 | final boolean foundselection = |
---|
168 | (!(p1[ip1] == p2[ip2]) && !((p1[ip1] == -1) || (p2[ip2] == -1))); |
---|
169 | final boolean matchInFirstPattern = (p1[ip1] == sequence[i]); |
---|
170 | final boolean matchInSecondPattern = (p2[ip2] == sequence[i]); |
---|
171 | |
---|
172 | if (foundselection && hadSelection) { |
---|
173 | if ((matchInFirstPattern && matchseq1) || (matchInSecondPattern && matchseq2)) { |
---|
174 | if (jumped1) { |
---|
175 | return matches(i + 1, p1, p2, ip1 + 1, ip2 + 2, false, false, foundselection, |
---|
176 | matchInFirstPattern, matchInSecondPattern); |
---|
177 | } |
---|
178 | if (jumped2) { |
---|
179 | return matches(i + 1, p1, p2, ip1 + 2, ip2 + 1, false, false, foundselection, |
---|
180 | matchInFirstPattern, matchInSecondPattern); |
---|
181 | } |
---|
182 | return matches(i + 1, p1, p2, ip1 + 1, ip2 + 1, false, false, foundselection, |
---|
183 | matchInFirstPattern, matchInSecondPattern); |
---|
184 | } |
---|
185 | else { |
---|
186 | return false; |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | if ((matchInFirstPattern || matchInSecondPattern) && jumped1) { |
---|
191 | return matches(i + 1, p1, p2, ip1 + 1, ip2 + 2, false, false, foundselection, |
---|
192 | matchInFirstPattern, matchInSecondPattern); |
---|
193 | } |
---|
194 | if ((matchInFirstPattern || matchInSecondPattern) && jumped2) { |
---|
195 | return matches(i + 1, p1, p2, ip1 + 2, ip2 + 1, false, false, foundselection, |
---|
196 | matchInFirstPattern, matchInSecondPattern); |
---|
197 | |
---|
198 | } |
---|
199 | if (matchInFirstPattern || matchInSecondPattern) { |
---|
200 | return matches(i + 1, p1, p2, ip1 + 1, ip2 + 1, false, false, foundselection, |
---|
201 | matchInFirstPattern, matchInSecondPattern); |
---|
202 | } |
---|
203 | if (p1[ip1] == -1) { |
---|
204 | return matches(i, p1, p2, ip1 + 1, ip2, true, false, false, false, false); |
---|
205 | } |
---|
206 | if (p2[ip2] == -1) { |
---|
207 | return matches(i, p1, p2, ip1, ip2 + 1, false, true, false, false, false); |
---|
208 | } |
---|
209 | |
---|
210 | return false; |
---|
211 | } |
---|
212 | |
---|
213 | /** |
---|
214 | * Prints the sequence. |
---|
215 | */ |
---|
216 | public void printSequence() { |
---|
217 | for (int i = 0; i < sequence.length; i++) { |
---|
218 | System.out.format("%5d", sequence[i]); |
---|
219 | } |
---|
220 | System.out.println(); |
---|
221 | } |
---|
222 | |
---|
223 | /** |
---|
224 | * Sets the id. |
---|
225 | * |
---|
226 | * @param id |
---|
227 | * the new id |
---|
228 | */ |
---|
229 | public void setId(int id) { |
---|
230 | this.id = id; |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * Sets the sequence. |
---|
235 | * |
---|
236 | * @param sequence |
---|
237 | * the new sequence |
---|
238 | */ |
---|
239 | public void setSequence(int[] sequence) { |
---|
240 | this.sequence = sequence; |
---|
241 | } |
---|
242 | |
---|
243 | /** |
---|
244 | * Shuffle. |
---|
245 | * |
---|
246 | * @return the number sequence |
---|
247 | */ |
---|
248 | public NumberSequence shuffle() { |
---|
249 | final NumberSequence result = new NumberSequence(sequence.length); |
---|
250 | result.setId(getId()); |
---|
251 | result.setSequence(this.sequence); |
---|
252 | final Random rgen = new Random(); |
---|
253 | |
---|
254 | for (int i = 0; i < result.sequence.length; i++) { |
---|
255 | final int randomPosition = rgen.nextInt(result.sequence.length); |
---|
256 | final int temp = result.sequence[i]; |
---|
257 | result.sequence[i] = result.sequence[randomPosition]; |
---|
258 | result.sequence[randomPosition] = temp; |
---|
259 | } |
---|
260 | return result; |
---|
261 | |
---|
262 | } |
---|
263 | |
---|
264 | /** |
---|
265 | * Size. |
---|
266 | * |
---|
267 | * @return the int |
---|
268 | */ |
---|
269 | public int size() { |
---|
270 | return sequence.length; |
---|
271 | } |
---|
272 | |
---|
273 | } |
---|