source: branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/NumberSequence.java @ 1733

Last change on this file since 1733 was 1733, checked in by rkrimmel, 10 years ago

Used Eclipse code cleanup

File size: 4.4 KB
Line 
1package de.ugoe.cs.autoquest.tasktrees.alignment.algorithms;
2
3import java.io.Serializable;
4import java.util.LinkedList;
5import java.util.Random;
6
7public class NumberSequence implements Serializable {
8        /**
9         *
10         */
11        private static final long serialVersionUID = -4604570107534055589L;
12        private int[] sequence;
13        private int id;
14
15        public NumberSequence(int size) {
16
17                sequence = new int[size];
18        }
19
20        // Searching occurrences of pattern
21        public LinkedList<Integer> containsPattern(Match pattern) {
22                final LinkedList<Integer> result = new LinkedList<Integer>();
23                int i = 0;
24                final int[] pat1 = pattern.getFirstSequence().getSequence();
25                final int[] pat2 = pattern.getSecondSequence().getSequence();
26
27                while (i < sequence.length) {
28                        if (matches(i, pat1, pat2, 0, 0, false, false, false, false, false)) {
29                                result.add(i);
30                        }
31                        i++;
32                }
33                return result;
34        }
35
36        public boolean equals(NumberSequence n) {
37                final int[] seq = n.getSequence();
38                if (n.size() != this.size()) {
39                        return false;
40                }
41                for (int i = 0; i < n.size(); i++) {
42                        if (seq[i] != this.sequence[i]) {
43                                return false;
44                        }
45                }
46                return true;
47        }
48
49        // Returns the number of times event occurs in this sequence
50        public int eventCount(int event) {
51                int count = 0;
52                for (int i = 0; i < sequence.length; i++) {
53                        if (sequence[i] == event) {
54                                count++;
55                        }
56                }
57                return count;
58        }
59
60        public int getId() {
61                return id;
62        }
63
64        public int[] getSequence() {
65                return sequence;
66        }
67
68        // Recursive check if sequence contains pattern at position i
69        private boolean matches(int i, int[] p1, int[] p2, int ip1, int ip2,
70                        boolean jumped1, // True if there was a gap in Sequence 1 of the
71                                                                // pattern
72                        boolean jumped2, // True if there was a gap in Sequence 2 of the
73                                                                // pattern
74                        boolean hadSelection, // True if the last match was a selection
75                        boolean matchseq1, boolean matchseq2) {
76
77                if (p1.length == ip1) {
78                        return true;
79                }
80                if (p2.length == ip2) {
81                        return true;
82                }
83                if (i == sequence.length) {
84                        return false;
85                }
86
87                final boolean foundselection = (!(p1[ip1] == p2[ip2]) && !((p1[ip1] == -1) || (p2[ip2] == -1)));
88                final boolean matchInFirstPattern = (p1[ip1] == sequence[i]);
89                final boolean matchInSecondPattern = (p2[ip2] == sequence[i]);
90
91                if (foundselection && hadSelection) {
92                        if ((matchInFirstPattern && matchseq1)
93                                        || (matchInSecondPattern && matchseq2)) {
94                                if (jumped1) {
95                                        return matches(i + 1, p1, p2, ip1 + 1, ip2 + 2, false,
96                                                        false, foundselection, matchInFirstPattern,
97                                                        matchInSecondPattern);
98                                }
99                                if (jumped2) {
100                                        return matches(i + 1, p1, p2, ip1 + 2, ip2 + 1, false,
101                                                        false, foundselection, matchInFirstPattern,
102                                                        matchInSecondPattern);
103                                }
104                                return matches(i + 1, p1, p2, ip1 + 1, ip2 + 1, false, false,
105                                                foundselection, matchInFirstPattern,
106                                                matchInSecondPattern);
107                        } else {
108                                return false;
109                        }
110                }
111
112                if ((matchInFirstPattern || matchInSecondPattern) && jumped1) {
113                        return matches(i + 1, p1, p2, ip1 + 1, ip2 + 2, false, false,
114                                        foundselection, matchInFirstPattern, matchInSecondPattern);
115                }
116                if ((matchInFirstPattern || matchInSecondPattern) && jumped2) {
117                        return matches(i + 1, p1, p2, ip1 + 2, ip2 + 1, false, false,
118                                        foundselection, matchInFirstPattern, matchInSecondPattern);
119                }
120                if (matchInFirstPattern || matchInSecondPattern) {
121                        return matches(i + 1, p1, p2, ip1 + 1, ip2 + 1, false, false,
122                                        foundselection, matchInFirstPattern, matchInSecondPattern);
123                }
124                if (p1[ip1] == -1) {
125                        return matches(i, p1, p2, ip1 + 1, ip2, true, false, false, false,
126                                        false);
127                }
128                if (p2[ip2] == -1) {
129                        return matches(i, p1, p2, ip1, ip2 + 1, false, true, false, false,
130                                        false);
131                }
132
133                return false;
134        }
135
136        public void printSequence() {
137                for (int i = 0; i < sequence.length; i++) {
138                        System.out.format("%5d", sequence[i]);
139                }
140                System.out.println();
141        }
142
143        public void setId(int id) {
144                this.id = id;
145        }
146
147        public void setSequence(int[] sequence) {
148                this.sequence = sequence;
149        }
150
151        public NumberSequence shuffle() {
152                final NumberSequence result = new NumberSequence(sequence.length);
153                result.setId(getId());
154                result.setSequence(this.sequence);
155                final Random rgen = new Random();
156
157                for (int i = 0; i < result.sequence.length; i++) {
158                        final int randomPosition = rgen.nextInt(result.sequence.length);
159                        final int temp = result.sequence[i];
160                        result.sequence[i] = result.sequence[randomPosition];
161                        result.sequence[randomPosition] = temp;
162                }
163                return result;
164
165        }
166
167        public int size() {
168                return sequence.length;
169        }
170
171}
Note: See TracBrowser for help on using the repository browser.