1 | package de.ugoe.cs.autoquest.tasktrees.alignment.algorithms; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.LinkedList; |
---|
5 | import java.util.Random; |
---|
6 | |
---|
7 | public class NumberSequence { |
---|
8 | private int[] sequence; |
---|
9 | private int id; |
---|
10 | |
---|
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 | |
---|
26 | public NumberSequence(int size) { |
---|
27 | |
---|
28 | sequence = new int[size]; |
---|
29 | } |
---|
30 | |
---|
31 | public int[] getSequence() { |
---|
32 | return sequence; |
---|
33 | } |
---|
34 | |
---|
35 | public void setSequence(int[] sequence) { |
---|
36 | this.sequence = sequence; |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | public void printSequence() { |
---|
42 | for (int i = 0; i < sequence.length; i++) { |
---|
43 | System.out.format("%5d", sequence[i]); |
---|
44 | } |
---|
45 | System.out.println(); |
---|
46 | } |
---|
47 | |
---|
48 | public NumberSequence shuffle() { |
---|
49 | NumberSequence result = new NumberSequence(sequence.length); |
---|
50 | result.setId(getId()); |
---|
51 | result.setSequence(this.sequence); |
---|
52 | Random rgen = new Random(); |
---|
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; |
---|
59 | } |
---|
60 | return result; |
---|
61 | |
---|
62 | } |
---|
63 | |
---|
64 | //Searching occurences of |
---|
65 | public LinkedList<Integer> containsPattern(Match pattern) { |
---|
66 | LinkedList<Integer> result = new LinkedList<Integer>(); |
---|
67 | int i = 0; |
---|
68 | int[] pat1 = pattern.getFirstSequence().getSequence(); |
---|
69 | int[] pat2 = pattern.getSecondSequence().getSequence(); |
---|
70 | notmatched: while (i < sequence.length - pat1.length) { |
---|
71 | if (sequence[i] == pat1[0] || sequence[i] == pat2[0]) { |
---|
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]) { |
---|
85 | i++; |
---|
86 | continue notmatched; |
---|
87 | } |
---|
88 | ipat1++; |
---|
89 | ipat2++; |
---|
90 | } |
---|
91 | result.add(i); |
---|
92 | } |
---|
93 | i++; |
---|
94 | } |
---|
95 | return result; |
---|
96 | } |
---|
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 | } |
---|
108 | |
---|
109 | public int size() { |
---|
110 | return sequence.length; |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | public int getId() { |
---|
115 | return id; |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | public void setId(int id) { |
---|
120 | this.id = id; |
---|
121 | } |
---|
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 | |
---|
137 | } |
---|