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