[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 | |
---|
[1660] | 46 | //Recursive check if sequence contains pattern at position i |
---|
[1665] | 47 | private boolean matches(int i, int[] p1, int[] p2 ,int ip1,int ip2,boolean jumped1,boolean jumped2) { |
---|
[1660] | 48 | |
---|
| 49 | if(p1.length==ip1) { |
---|
| 50 | return true; |
---|
| 51 | } |
---|
| 52 | if(p2.length==ip2) { |
---|
| 53 | return true; |
---|
| 54 | } |
---|
| 55 | if(i==sequence.length) { |
---|
| 56 | return false; |
---|
| 57 | } |
---|
[1665] | 58 | if((p1[ip1]==sequence[i]||p2[ip2]==sequence[i]) && jumped1) { |
---|
| 59 | return matches(i+1,p1,p2,ip1+1,ip2+2,false,false); |
---|
| 60 | } |
---|
| 61 | if((p1[ip1]==sequence[i]||p2[ip2]==sequence[i]) && jumped2) { |
---|
| 62 | return matches(i+1,p1,p2,ip1+2,ip2+1,false,false); |
---|
| 63 | } |
---|
[1660] | 64 | if(p1[ip1]==sequence[i]||p2[ip2]==sequence[i]) { |
---|
[1665] | 65 | return matches(i+1,p1,p2,ip1+1,ip2+1,false,false); |
---|
[1660] | 66 | } |
---|
| 67 | if(p1[ip1]==-1) { |
---|
[1665] | 68 | return matches(i,p1,p2,ip1+1,ip2,true,false); |
---|
[1660] | 69 | } |
---|
| 70 | if(p2[ip2]==-1) { |
---|
[1665] | 71 | return matches(i,p1,p2,ip1,ip2+1,false,true); |
---|
[1660] | 72 | } |
---|
[1665] | 73 | |
---|
[1660] | 74 | return false; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | //Searching occurrences of pattern |
---|
[1621] | 78 | public LinkedList<Integer> containsPattern(Match pattern) { |
---|
| 79 | LinkedList<Integer> result = new LinkedList<Integer>(); |
---|
[1618] | 80 | int i = 0; |
---|
[1620] | 81 | int[] pat1 = pattern.getFirstSequence().getSequence(); |
---|
| 82 | int[] pat2 = pattern.getSecondSequence().getSequence(); |
---|
[1657] | 83 | |
---|
[1660] | 84 | while (i < sequence.length ) { |
---|
[1665] | 85 | if(matches(i,pat1,pat2,0,0,false,false)) { |
---|
[1621] | 86 | result.add(i); |
---|
[1618] | 87 | } |
---|
| 88 | i++; |
---|
| 89 | } |
---|
[1621] | 90 | return result; |
---|
[1618] | 91 | } |
---|
[1657] | 92 | |
---|
| 93 | // Returns the number of times event occurs in this sequence |
---|
[1620] | 94 | public int eventCount(int event) { |
---|
| 95 | int count = 0; |
---|
[1657] | 96 | for (int i = 0; i < sequence.length; i++) { |
---|
| 97 | if (sequence[i] == event) { |
---|
[1620] | 98 | count++; |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | return count; |
---|
| 102 | } |
---|
[1618] | 103 | |
---|
[1592] | 104 | public int size() { |
---|
| 105 | return sequence.length; |
---|
| 106 | } |
---|
[1620] | 107 | |
---|
| 108 | public int getId() { |
---|
| 109 | return id; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | public void setId(int id) { |
---|
| 113 | this.id = id; |
---|
| 114 | } |
---|
[1657] | 115 | |
---|
[1621] | 116 | public boolean equals(NumberSequence n) { |
---|
| 117 | int[] seq = n.getSequence(); |
---|
[1657] | 118 | if (n.size() != this.size()) { |
---|
| 119 | return false; |
---|
[1621] | 120 | } |
---|
[1657] | 121 | for (int i = 0; i < n.size(); i++) { |
---|
| 122 | if (seq[i] != this.sequence[i]) { |
---|
[1621] | 123 | return false; |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | return true; |
---|
| 127 | } |
---|
[1657] | 128 | |
---|
[1450] | 129 | } |
---|