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