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