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