[1553] | 1 | package de.ugoe.cs.autoquest.tasktrees.alignment.algorithms; |
---|
[1450] | 2 | |
---|
[1621] | 3 | import java.util.LinkedList; |
---|
[1568] | 4 | import java.util.Random; |
---|
[1450] | 5 | |
---|
[1618] | 6 | public class NumberSequence { |
---|
[1450] | 7 | private int[] sequence; |
---|
[1620] | 8 | private int id; |
---|
[1618] | 9 | |
---|
[1450] | 10 | public NumberSequence(int size) { |
---|
[1618] | 11 | |
---|
[1450] | 12 | sequence = new int[size]; |
---|
| 13 | } |
---|
[1657] | 14 | |
---|
[1450] | 15 | public int[] getSequence() { |
---|
| 16 | return sequence; |
---|
| 17 | } |
---|
[1618] | 18 | |
---|
[1450] | 19 | public void setSequence(int[] sequence) { |
---|
| 20 | this.sequence = sequence; |
---|
| 21 | } |
---|
[1618] | 22 | |
---|
| 23 | public void printSequence() { |
---|
[1558] | 24 | for (int i = 0; i < sequence.length; i++) { |
---|
[1559] | 25 | System.out.format("%5d", sequence[i]); |
---|
[1558] | 26 | } |
---|
| 27 | System.out.println(); |
---|
| 28 | } |
---|
[1618] | 29 | |
---|
| 30 | public NumberSequence shuffle() { |
---|
[1568] | 31 | NumberSequence result = new NumberSequence(sequence.length); |
---|
[1620] | 32 | result.setId(getId()); |
---|
[1568] | 33 | result.setSequence(this.sequence); |
---|
| 34 | Random rgen = new Random(); |
---|
[1618] | 35 | |
---|
| 36 | for (int i = 0; i < result.sequence.length; i++) { |
---|
| 37 | int randomPosition = rgen.nextInt(result.sequence.length); |
---|
| 38 | int temp = result.sequence[i]; |
---|
| 39 | result.sequence[i] = result.sequence[randomPosition]; |
---|
| 40 | result.sequence[randomPosition] = temp; |
---|
[1568] | 41 | } |
---|
| 42 | return result; |
---|
[1618] | 43 | |
---|
[1568] | 44 | } |
---|
[1450] | 45 | |
---|
[1657] | 46 | // Searching occurrences of |
---|
[1621] | 47 | public LinkedList<Integer> containsPattern(Match pattern) { |
---|
| 48 | LinkedList<Integer> result = new LinkedList<Integer>(); |
---|
[1618] | 49 | int i = 0; |
---|
[1620] | 50 | int[] pat1 = pattern.getFirstSequence().getSequence(); |
---|
| 51 | int[] pat2 = pattern.getSecondSequence().getSequence(); |
---|
[1657] | 52 | notmatched: while (i <= sequence.length - pat1.length) { |
---|
[1618] | 53 | if (sequence[i] == pat1[0] || sequence[i] == pat2[0]) { |
---|
[1657] | 54 | int ipat1 = 0; |
---|
| 55 | int ipat2 = 0; |
---|
[1650] | 56 | int optcount1 = 0; |
---|
| 57 | int optcount2 = 0; |
---|
[1657] | 58 | while (ipat1 < pat1.length |
---|
| 59 | && ipat2 < pat2.length) { |
---|
| 60 | |
---|
| 61 | if (pat1[ipat1] == -1) { |
---|
| 62 | ipat1++; |
---|
| 63 | optcount1++; |
---|
| 64 | continue; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | if (pat2[ipat2] == -1) { |
---|
| 68 | ipat2++; |
---|
| 69 | optcount2++; |
---|
| 70 | continue; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | if (sequence[i + ipat1 - optcount1] != pat1[ipat1] |
---|
| 75 | && sequence[i + ipat2 - optcount2] != pat2[ipat2]) { |
---|
[1619] | 76 | i++; |
---|
[1657] | 77 | // System.out.println(sequence[i+ipat1-optcount1] + |
---|
| 78 | // " != " + pat1[ipat1] + " || " + |
---|
| 79 | // sequence[i+ipat2-optcount2] + " != " + pat2[ipat2]); |
---|
[1618] | 80 | continue notmatched; |
---|
| 81 | } |
---|
[1657] | 82 | ipat1++; |
---|
| 83 | ipat2++; |
---|
[1618] | 84 | } |
---|
[1657] | 85 | |
---|
[1621] | 86 | result.add(i); |
---|
[1657] | 87 | System.out.println("FOUND MATCH AT: " + i); |
---|
| 88 | this.printSequence(); |
---|
| 89 | pattern.getFirstSequence().printSequence(); |
---|
| 90 | pattern.getSecondSequence().printSequence(); |
---|
[1618] | 91 | } |
---|
| 92 | i++; |
---|
| 93 | } |
---|
[1621] | 94 | return result; |
---|
[1618] | 95 | } |
---|
[1657] | 96 | |
---|
| 97 | // Returns the number of times event occurs in this sequence |
---|
[1620] | 98 | public int eventCount(int event) { |
---|
| 99 | int count = 0; |
---|
[1657] | 100 | for (int i = 0; i < sequence.length; i++) { |
---|
| 101 | if (sequence[i] == event) { |
---|
[1620] | 102 | count++; |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | return count; |
---|
| 106 | } |
---|
[1618] | 107 | |
---|
[1592] | 108 | public int size() { |
---|
| 109 | return sequence.length; |
---|
| 110 | } |
---|
[1620] | 111 | |
---|
| 112 | public int getId() { |
---|
| 113 | return id; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | public void setId(int id) { |
---|
| 117 | this.id = id; |
---|
| 118 | } |
---|
[1657] | 119 | |
---|
[1621] | 120 | public boolean equals(NumberSequence n) { |
---|
| 121 | int[] seq = n.getSequence(); |
---|
[1657] | 122 | if (n.size() != this.size()) { |
---|
| 123 | return false; |
---|
[1621] | 124 | } |
---|
[1657] | 125 | for (int i = 0; i < n.size(); i++) { |
---|
| 126 | if (seq[i] != this.sequence[i]) { |
---|
[1621] | 127 | return false; |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | return true; |
---|
| 131 | } |
---|
[1657] | 132 | |
---|
[1450] | 133 | } |
---|