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

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

About to fix bug in containsPattern

File size: 2.9 KB
Line 
1package de.ugoe.cs.autoquest.tasktrees.alignment.algorithms;
2
3import java.util.LinkedList;
4import java.util.Random;
5
6public class NumberSequence {
7        private int[] sequence;
8        private int id;
9
10        public NumberSequence(int size) {
11
12                sequence = new int[size];
13        }
14
15        public int[] getSequence() {
16                return sequence;
17        }
18
19        public void setSequence(int[] sequence) {
20                this.sequence = sequence;
21        }
22
23        public void printSequence() {
24                for (int i = 0; i < sequence.length; i++) {
25                        System.out.format("%5d", sequence[i]);
26                }
27                System.out.println();
28        }
29
30        public NumberSequence shuffle() {
31                NumberSequence result = new NumberSequence(sequence.length);
32                result.setId(getId());
33                result.setSequence(this.sequence);
34                Random rgen = new Random();
35
36                for (int i = 0; i < result.sequence.length; i++) {
37                        int randomPosition = rgen.nextInt(result.sequence.length);
38                        int temp = result.sequence[i];
39                        result.sequence[i] = result.sequence[randomPosition];
40                        result.sequence[randomPosition] = temp;
41                }
42                return result;
43
44        }
45
46        // Searching occurrences of
47        public LinkedList<Integer> containsPattern(Match pattern) {
48                LinkedList<Integer> result = new LinkedList<Integer>();
49                int i = 0;
50                int[] pat1 = pattern.getFirstSequence().getSequence();
51                int[] pat2 = pattern.getSecondSequence().getSequence();
52                notmatched: while (i <= sequence.length - pat1.length) {
53                        if (sequence[i] == pat1[0] || sequence[i] == pat2[0]) {
54                                int ipat1 = 0;
55                                int ipat2 = 0;
56                                int optcount1 = 0;
57                                int optcount2 = 0;
58                                while (ipat1 < pat1.length
59                                                && ipat2 < pat2.length) {
60                                       
61                                                if (pat1[ipat1] == -1) {
62                                                        ipat1++;
63                                                        optcount1++;
64                                                        continue;
65                                                }
66                                       
67                                                if (pat2[ipat2] == -1) {
68                                                        ipat2++;
69                                                        optcount2++;
70                                                        continue;
71                                                }
72                                       
73
74                                        if (sequence[i + ipat1 - optcount1] != pat1[ipat1]
75                                                        && sequence[i + ipat2 - optcount2] != pat2[ipat2]) {
76                                                i++;
77                                                // System.out.println(sequence[i+ipat1-optcount1] +
78                                                // " != " + pat1[ipat1] + " || " +
79                                                // sequence[i+ipat2-optcount2] + " != " + pat2[ipat2]);
80                                                continue notmatched;
81                                        }
82                                                ipat1++;
83                                                ipat2++;
84                                }
85                               
86                                result.add(i);
87                                System.out.println("FOUND MATCH AT: " + i);
88                                this.printSequence();
89                                pattern.getFirstSequence().printSequence();
90                                pattern.getSecondSequence().printSequence();
91                        }
92                        i++;
93                }
94                return result;
95        }
96
97        // Returns the number of times event occurs in this sequence
98        public int eventCount(int event) {
99                int count = 0;
100                for (int i = 0; i < sequence.length; i++) {
101                        if (sequence[i] == event) {
102                                count++;
103                        }
104                }
105                return count;
106        }
107
108        public int size() {
109                return sequence.length;
110        }
111
112        public int getId() {
113                return id;
114        }
115
116        public void setId(int id) {
117                this.id = id;
118        }
119
120        public boolean equals(NumberSequence n) {
121                int[] seq = n.getSequence();
122                if (n.size() != this.size()) {
123                        return false;
124                }
125                for (int i = 0; i < n.size(); i++) {
126                        if (seq[i] != this.sequence[i]) {
127                                return false;
128                        }
129                }
130                return true;
131        }
132
133}
Note: See TracBrowser for help on using the repository browser.