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