Changeset 1733 for branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/NumberSequence.java
- Timestamp:
- 09/05/14 19:33:12 (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
r1717 r1733 5 5 import java.util.Random; 6 6 7 public class NumberSequence implements Serializable {7 public class NumberSequence implements Serializable { 8 8 /** 9 9 * … … 18 18 } 19 19 20 public int[] getSequence() { 21 return sequence; 22 } 20 // Searching occurrences of pattern 21 public LinkedList<Integer> containsPattern(Match pattern) { 22 final LinkedList<Integer> result = new LinkedList<Integer>(); 23 int i = 0; 24 final int[] pat1 = pattern.getFirstSequence().getSequence(); 25 final int[] pat2 = pattern.getSecondSequence().getSequence(); 23 26 24 public void setSequence(int[] sequence) { 25 this.sequence = sequence; 26 } 27 28 public void printSequence() { 29 for (int i = 0; i < sequence.length; i++) { 30 System.out.format("%5d", sequence[i]); 31 } 32 System.out.println(); 33 } 34 35 public NumberSequence shuffle() { 36 NumberSequence result = new NumberSequence(sequence.length); 37 result.setId(getId()); 38 result.setSequence(this.sequence); 39 Random rgen = new Random(); 40 41 for (int i = 0; i < result.sequence.length; i++) { 42 int randomPosition = rgen.nextInt(result.sequence.length); 43 int temp = result.sequence[i]; 44 result.sequence[i] = result.sequence[randomPosition]; 45 result.sequence[randomPosition] = temp; 46 } 47 return result; 48 49 } 50 51 //Recursive check if sequence contains pattern at position i 52 private boolean matches(int i, 53 int[] p1, 54 int[] p2 , 55 int ip1, 56 int ip2, 57 boolean jumped1, //True if there was a gap in Sequence 1 of the pattern 58 boolean jumped2, //True if there was a gap in Sequence 2 of the pattern 59 boolean hadSelection, //True if the last match was a selection 60 boolean matchseq1, 61 boolean matchseq2) { 62 63 if(p1.length==ip1) { 64 return true; 65 } 66 if(p2.length==ip2) { 67 return true; 68 } 69 if(i==sequence.length) { 70 return false; 71 } 72 73 boolean foundselection=(!(p1[ip1] == p2[ip2])&&!(p1[ip1]==-1||p2[ip2]==-1)); 74 boolean matchInFirstPattern = (p1[ip1]==sequence[i]); 75 boolean matchInSecondPattern = (p2[ip2]==sequence[i]); 76 77 if(foundselection && hadSelection) { 78 if((matchInFirstPattern && matchseq1) || (matchInSecondPattern && matchseq2)){ 79 if(jumped1) { 80 return matches(i+1,p1,p2,ip1+1,ip2+2,false,false,foundselection,matchInFirstPattern,matchInSecondPattern); 81 } 82 if(jumped2) { 83 return matches(i+1,p1,p2,ip1+2,ip2+1,false,false,foundselection,matchInFirstPattern,matchInSecondPattern); 84 } 85 return matches(i+1,p1,p2,ip1+1,ip2+1,false,false,foundselection,matchInFirstPattern,matchInSecondPattern); 86 } 87 else { 88 return false; 89 } 90 } 91 92 if((matchInFirstPattern||matchInSecondPattern) && jumped1) { 93 return matches(i+1,p1,p2,ip1+1,ip2+2,false,false,foundselection,matchInFirstPattern,matchInSecondPattern); 94 } 95 if((matchInFirstPattern||matchInSecondPattern) && jumped2) { 96 return matches(i+1,p1,p2,ip1+2,ip2+1,false,false,foundselection,matchInFirstPattern,matchInSecondPattern); 97 } 98 if(matchInFirstPattern||matchInSecondPattern) { 99 return matches(i+1,p1,p2,ip1+1,ip2+1,false,false,foundselection,matchInFirstPattern,matchInSecondPattern); 100 } 101 if(p1[ip1]==-1) { 102 return matches(i,p1,p2,ip1+1,ip2,true,false,false,false,false); 103 } 104 if(p2[ip2]==-1) { 105 return matches(i,p1,p2,ip1,ip2+1,false,true,false,false,false); 106 } 107 108 return false; 109 } 110 111 //Searching occurrences of pattern 112 public LinkedList<Integer> containsPattern(Match pattern) { 113 LinkedList<Integer> result = new LinkedList<Integer>(); 114 int i = 0; 115 int[] pat1 = pattern.getFirstSequence().getSequence(); 116 int[] pat2 = pattern.getSecondSequence().getSequence(); 117 118 while (i < sequence.length ) { 119 if(matches(i,pat1,pat2,0,0,false,false,false,false,false)) { 27 while (i < sequence.length) { 28 if (matches(i, pat1, pat2, 0, 0, false, false, false, false, false)) { 120 29 result.add(i); 121 30 } … … 123 32 } 124 33 return result; 34 } 35 36 public boolean equals(NumberSequence n) { 37 final int[] seq = n.getSequence(); 38 if (n.size() != this.size()) { 39 return false; 40 } 41 for (int i = 0; i < n.size(); i++) { 42 if (seq[i] != this.sequence[i]) { 43 return false; 44 } 45 } 46 return true; 125 47 } 126 48 … … 136 58 } 137 59 138 public int size() {139 return sequence.length;60 public int getId() { 61 return id; 140 62 } 141 63 142 public int getId() { 143 return id; 64 public int[] getSequence() { 65 return sequence; 66 } 67 68 // Recursive check if sequence contains pattern at position i 69 private boolean matches(int i, int[] p1, int[] p2, int ip1, int ip2, 70 boolean jumped1, // True if there was a gap in Sequence 1 of the 71 // pattern 72 boolean jumped2, // True if there was a gap in Sequence 2 of the 73 // pattern 74 boolean hadSelection, // True if the last match was a selection 75 boolean matchseq1, boolean matchseq2) { 76 77 if (p1.length == ip1) { 78 return true; 79 } 80 if (p2.length == ip2) { 81 return true; 82 } 83 if (i == sequence.length) { 84 return false; 85 } 86 87 final boolean foundselection = (!(p1[ip1] == p2[ip2]) && !((p1[ip1] == -1) || (p2[ip2] == -1))); 88 final boolean matchInFirstPattern = (p1[ip1] == sequence[i]); 89 final boolean matchInSecondPattern = (p2[ip2] == sequence[i]); 90 91 if (foundselection && hadSelection) { 92 if ((matchInFirstPattern && matchseq1) 93 || (matchInSecondPattern && matchseq2)) { 94 if (jumped1) { 95 return matches(i + 1, p1, p2, ip1 + 1, ip2 + 2, false, 96 false, foundselection, matchInFirstPattern, 97 matchInSecondPattern); 98 } 99 if (jumped2) { 100 return matches(i + 1, p1, p2, ip1 + 2, ip2 + 1, false, 101 false, foundselection, matchInFirstPattern, 102 matchInSecondPattern); 103 } 104 return matches(i + 1, p1, p2, ip1 + 1, ip2 + 1, false, false, 105 foundselection, matchInFirstPattern, 106 matchInSecondPattern); 107 } else { 108 return false; 109 } 110 } 111 112 if ((matchInFirstPattern || matchInSecondPattern) && jumped1) { 113 return matches(i + 1, p1, p2, ip1 + 1, ip2 + 2, false, false, 114 foundselection, matchInFirstPattern, matchInSecondPattern); 115 } 116 if ((matchInFirstPattern || matchInSecondPattern) && jumped2) { 117 return matches(i + 1, p1, p2, ip1 + 2, ip2 + 1, false, false, 118 foundselection, matchInFirstPattern, matchInSecondPattern); 119 } 120 if (matchInFirstPattern || matchInSecondPattern) { 121 return matches(i + 1, p1, p2, ip1 + 1, ip2 + 1, false, false, 122 foundselection, matchInFirstPattern, matchInSecondPattern); 123 } 124 if (p1[ip1] == -1) { 125 return matches(i, p1, p2, ip1 + 1, ip2, true, false, false, false, 126 false); 127 } 128 if (p2[ip2] == -1) { 129 return matches(i, p1, p2, ip1, ip2 + 1, false, true, false, false, 130 false); 131 } 132 133 return false; 134 } 135 136 public void printSequence() { 137 for (int i = 0; i < sequence.length; i++) { 138 System.out.format("%5d", sequence[i]); 139 } 140 System.out.println(); 144 141 } 145 142 … … 148 145 } 149 146 150 public boolean equals(NumberSequence n) { 151 int[] seq = n.getSequence(); 152 if (n.size() != this.size()) { 153 return false; 147 public void setSequence(int[] sequence) { 148 this.sequence = sequence; 149 } 150 151 public NumberSequence shuffle() { 152 final NumberSequence result = new NumberSequence(sequence.length); 153 result.setId(getId()); 154 result.setSequence(this.sequence); 155 final Random rgen = new Random(); 156 157 for (int i = 0; i < result.sequence.length; i++) { 158 final int randomPosition = rgen.nextInt(result.sequence.length); 159 final int temp = result.sequence[i]; 160 result.sequence[i] = result.sequence[randomPosition]; 161 result.sequence[randomPosition] = temp; 154 162 } 155 for (int i = 0; i < n.size(); i++) {156 if (seq[i] != this.sequence[i]) { 157 return false;158 } 159 }160 return true;163 return result; 164 165 } 166 167 public int size() { 168 return sequence.length; 161 169 } 162 170
Note: See TracChangeset
for help on using the changeset viewer.