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

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

Added automatically created javadoc, still needs to be commented properly though

File size: 5.6 KB
Line 
1/*
2 *
3 */
4package de.ugoe.cs.autoquest.tasktrees.alignment.algorithms;
5
6import java.io.Serializable;
7import java.util.LinkedList;
8import java.util.Random;
9
10// TODO: Auto-generated Javadoc
11/**
12 * The Class NumberSequence.
13 */
14public class NumberSequence implements Serializable {
15       
16        /** The Constant serialVersionUID. */
17        private static final long serialVersionUID = -4604570107534055589L;
18       
19        /** The sequence. */
20        private int[] sequence;
21       
22        /** The id. */
23        private int id;
24
25        /**
26         * Instantiates a new number sequence.
27         *
28         * @param size the size
29         */
30        public NumberSequence(int size) {
31
32                sequence = new int[size];
33        }
34
35        // Searching occurrences of pattern
36        /**
37         * Contains pattern.
38         *
39         * @param pattern the pattern
40         * @return the linked list
41         */
42        public LinkedList<Integer> containsPattern(Match pattern) {
43                final LinkedList<Integer> result = new LinkedList<Integer>();
44                int i = 0;
45                final int[] pat1 = pattern.getFirstSequence().getSequence();
46                final int[] pat2 = pattern.getSecondSequence().getSequence();
47
48                while (i < sequence.length) {
49                        if (matches(i, pat1, pat2, 0, 0, false, false, false, false, false)) {
50                                result.add(i);
51                        }
52                        i++;
53                }
54                return result;
55        }
56
57        /**
58         * Equals.
59         *
60         * @param n the n
61         * @return true, if successful
62         */
63        public boolean equals(NumberSequence n) {
64                final int[] seq = n.getSequence();
65                if (n.size() != this.size()) {
66                        return false;
67                }
68                for (int i = 0; i < n.size(); i++) {
69                        if (seq[i] != this.sequence[i]) {
70                                return false;
71                        }
72                }
73                return true;
74        }
75
76        // Returns the number of times event occurs in this sequence
77        /**
78         * Event count.
79         *
80         * @param event the event
81         * @return the int
82         */
83        public int eventCount(int event) {
84                int count = 0;
85                for (int i = 0; i < sequence.length; i++) {
86                        if (sequence[i] == event) {
87                                count++;
88                        }
89                }
90                return count;
91        }
92
93        /**
94         * Gets the id.
95         *
96         * @return the id
97         */
98        public int getId() {
99                return id;
100        }
101
102        /**
103         * Gets the sequence.
104         *
105         * @return the sequence
106         */
107        public int[] getSequence() {
108                return sequence;
109        }
110
111        // Recursive check if sequence contains pattern at position i
112        /**
113         * Matches.
114         *
115         * @param i the i
116         * @param p1 the p1
117         * @param p2 the p2
118         * @param ip1 the ip1
119         * @param ip2 the ip2
120         * @param jumped1 the jumped1
121         * @param jumped2 the jumped2
122         * @param hadSelection the had selection
123         * @param matchseq1 the matchseq1
124         * @param matchseq2 the matchseq2
125         * @return true, if successful
126         */
127        private boolean matches(int i, int[] p1, int[] p2, int ip1, int ip2,
128                        boolean jumped1, // True if there was a gap in Sequence 1 of the
129                                                                // pattern
130                        boolean jumped2, // True if there was a gap in Sequence 2 of the
131                                                                // pattern
132                        boolean hadSelection, // True if the last match was a selection
133                        boolean matchseq1, boolean matchseq2) {
134
135                if (p1.length == ip1) {
136                        return true;
137                }
138                if (p2.length == ip2) {
139                        return true;
140                }
141                if (i == sequence.length) {
142                        return false;
143                }
144
145                final boolean foundselection = (!(p1[ip1] == p2[ip2]) && !((p1[ip1] == -1) || (p2[ip2] == -1)));
146                final boolean matchInFirstPattern = (p1[ip1] == sequence[i]);
147                final boolean matchInSecondPattern = (p2[ip2] == sequence[i]);
148
149                if (foundselection && hadSelection) {
150                        if ((matchInFirstPattern && matchseq1)
151                                        || (matchInSecondPattern && matchseq2)) {
152                                if (jumped1) {
153                                        return matches(i + 1, p1, p2, ip1 + 1, ip2 + 2, false,
154                                                        false, foundselection, matchInFirstPattern,
155                                                        matchInSecondPattern);
156                                }
157                                if (jumped2) {
158                                        return matches(i + 1, p1, p2, ip1 + 2, ip2 + 1, false,
159                                                        false, foundselection, matchInFirstPattern,
160                                                        matchInSecondPattern);
161                                }
162                                return matches(i + 1, p1, p2, ip1 + 1, ip2 + 1, false, false,
163                                                foundselection, matchInFirstPattern,
164                                                matchInSecondPattern);
165                        } else {
166                                return false;
167                        }
168                }
169
170                if ((matchInFirstPattern || matchInSecondPattern) && jumped1) {
171                        return matches(i + 1, p1, p2, ip1 + 1, ip2 + 2, false, false,
172                                        foundselection, matchInFirstPattern, matchInSecondPattern);
173                }
174                if ((matchInFirstPattern || matchInSecondPattern) && jumped2) {
175                        return matches(i + 1, p1, p2, ip1 + 2, ip2 + 1, false, false,
176                                        foundselection, matchInFirstPattern, matchInSecondPattern);
177                }
178                if (matchInFirstPattern || matchInSecondPattern) {
179                        return matches(i + 1, p1, p2, ip1 + 1, ip2 + 1, false, false,
180                                        foundselection, matchInFirstPattern, matchInSecondPattern);
181                }
182                if (p1[ip1] == -1) {
183                        return matches(i, p1, p2, ip1 + 1, ip2, true, false, false, false,
184                                        false);
185                }
186                if (p2[ip2] == -1) {
187                        return matches(i, p1, p2, ip1, ip2 + 1, false, true, false, false,
188                                        false);
189                }
190
191                return false;
192        }
193
194        /**
195         * Prints the sequence.
196         */
197        public void printSequence() {
198                for (int i = 0; i < sequence.length; i++) {
199                        System.out.format("%5d", sequence[i]);
200                }
201                System.out.println();
202        }
203
204        /**
205         * Sets the id.
206         *
207         * @param id the new id
208         */
209        public void setId(int id) {
210                this.id = id;
211        }
212
213        /**
214         * Sets the sequence.
215         *
216         * @param sequence the new sequence
217         */
218        public void setSequence(int[] sequence) {
219                this.sequence = sequence;
220        }
221
222        /**
223         * Shuffle.
224         *
225         * @return the number sequence
226         */
227        public NumberSequence shuffle() {
228                final NumberSequence result = new NumberSequence(sequence.length);
229                result.setId(getId());
230                result.setSequence(this.sequence);
231                final Random rgen = new Random();
232
233                for (int i = 0; i < result.sequence.length; i++) {
234                        final int randomPosition = rgen.nextInt(result.sequence.length);
235                        final int temp = result.sequence[i];
236                        result.sequence[i] = result.sequence[randomPosition];
237                        result.sequence[randomPosition] = temp;
238                }
239                return result;
240
241        }
242
243        /**
244         * Size.
245         *
246         * @return the int
247         */
248        public int size() {
249                return sequence.length;
250        }
251
252}
Note: See TracBrowser for help on using the repository browser.