1 | package de.ugoe.cs.autoquest.tasktrees.alignment.algorithms; |
---|
2 | |
---|
3 | import java.util.LinkedList; |
---|
4 | import java.util.Random; |
---|
5 | |
---|
6 | public class NumberSequence { |
---|
7 | private int[] sequence; |
---|
8 | private int id; |
---|
9 | |
---|
10 | |
---|
11 | public NumberSequence(int size) { |
---|
12 | |
---|
13 | sequence = new int[size]; |
---|
14 | } |
---|
15 | |
---|
16 | public int[] getSequence() { |
---|
17 | return sequence; |
---|
18 | } |
---|
19 | |
---|
20 | public void setSequence(int[] sequence) { |
---|
21 | this.sequence = sequence; |
---|
22 | } |
---|
23 | |
---|
24 | public void printSequence() { |
---|
25 | for (int i = 0; i < sequence.length; i++) { |
---|
26 | System.out.format("%5d", sequence[i]); |
---|
27 | } |
---|
28 | System.out.println(); |
---|
29 | } |
---|
30 | |
---|
31 | public NumberSequence shuffle() { |
---|
32 | NumberSequence result = new NumberSequence(sequence.length); |
---|
33 | result.setId(getId()); |
---|
34 | result.setSequence(this.sequence); |
---|
35 | Random rgen = new Random(); |
---|
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; |
---|
42 | } |
---|
43 | return result; |
---|
44 | |
---|
45 | } |
---|
46 | |
---|
47 | //Searching occurrences of |
---|
48 | public LinkedList<Integer> containsPattern(Match pattern) { |
---|
49 | LinkedList<Integer> result = new LinkedList<Integer>(); |
---|
50 | int i = 0; |
---|
51 | int[] pat1 = pattern.getFirstSequence().getSequence(); |
---|
52 | int[] pat2 = pattern.getSecondSequence().getSequence(); |
---|
53 | notmatched: while (i < sequence.length - pat1.length) { |
---|
54 | if (sequence[i] == pat1[0] || sequence[i] == pat2[0]) { |
---|
55 | int ipat1 =0; |
---|
56 | int ipat2 =0; |
---|
57 | int optcount1 = 0; |
---|
58 | int optcount2 = 0; |
---|
59 | while(ipat1 < pat1.length && ipat2<pat2.length) { |
---|
60 | if(pat1[ipat1]==-1) { |
---|
61 | ipat1++; |
---|
62 | optcount1++; |
---|
63 | continue; |
---|
64 | } |
---|
65 | if(pat2[ipat2]==-1) { |
---|
66 | ipat2++; |
---|
67 | optcount2++; |
---|
68 | continue; |
---|
69 | } |
---|
70 | if (sequence[i + ipat1-optcount1] != pat1[ipat1] |
---|
71 | && sequence[i + ipat2-optcount2] != pat2[ipat2]) { |
---|
72 | i++; |
---|
73 | //System.out.println(sequence[i+ipat1] + " != " + pat1[ipat1] + " || " + sequence[i+ipat2] + " != " + pat2[ipat2]); |
---|
74 | continue notmatched; |
---|
75 | } |
---|
76 | ipat1++; |
---|
77 | ipat2++; |
---|
78 | } |
---|
79 | result.add(i); |
---|
80 | } |
---|
81 | i++; |
---|
82 | } |
---|
83 | return result; |
---|
84 | } |
---|
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 | } |
---|
96 | |
---|
97 | public int size() { |
---|
98 | return sequence.length; |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | public int getId() { |
---|
103 | return id; |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | public void setId(int id) { |
---|
108 | this.id = id; |
---|
109 | } |
---|
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 | |
---|
125 | } |
---|