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