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

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

Found last bug in containsPattern

File size: 2.8 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        //Recursive check if sequence contains pattern at position i
47        private boolean matches(int i, int[] p1, int[] p2 ,int ip1,int ip2,boolean jumped1,boolean jumped2) {
48               
49                if(p1.length==ip1) {
50                        return true;
51                }
52                if(p2.length==ip2) {
53                        return true;
54                }
55                if(i==sequence.length) {
56                        return false;
57                }
58                if((p1[ip1]==sequence[i]||p2[ip2]==sequence[i]) && jumped1) {
59                        return matches(i+1,p1,p2,ip1+1,ip2+2,false,false);
60                }
61                if((p1[ip1]==sequence[i]||p2[ip2]==sequence[i]) && jumped2) {
62                        return matches(i+1,p1,p2,ip1+2,ip2+1,false,false);
63                }
64                if(p1[ip1]==sequence[i]||p2[ip2]==sequence[i]) {
65                        return matches(i+1,p1,p2,ip1+1,ip2+1,false,false);
66                }
67                if(p1[ip1]==-1) {
68                        return matches(i,p1,p2,ip1+1,ip2,true,false);
69                }
70                if(p2[ip2]==-1) {
71                        return matches(i,p1,p2,ip1,ip2+1,false,true);
72                }
73       
74                return false;
75        }
76       
77        //Searching occurrences of pattern
78        public LinkedList<Integer> containsPattern(Match pattern) {
79                LinkedList<Integer> result = new LinkedList<Integer>();
80                int i = 0;
81                int[] pat1 = pattern.getFirstSequence().getSequence();
82                int[] pat2 = pattern.getSecondSequence().getSequence();
83
84                while (i < sequence.length ) {
85                        if(matches(i,pat1,pat2,0,0,false,false)) {
86                                result.add(i);
87                        }
88                        i++;
89                }
90                return result;
91        }
92
93        // Returns the number of times event occurs in this sequence
94        public int eventCount(int event) {
95                int count = 0;
96                for (int i = 0; i < sequence.length; i++) {
97                        if (sequence[i] == event) {
98                                count++;
99                        }
100                }
101                return count;
102        }
103
104        public int size() {
105                return sequence.length;
106        }
107
108        public int getId() {
109                return id;
110        }
111
112        public void setId(int id) {
113                this.id = id;
114        }
115
116        public boolean equals(NumberSequence n) {
117                int[] seq = n.getSequence();
118                if (n.size() != this.size()) {
119                        return false;
120                }
121                for (int i = 0; i < n.size(); i++) {
122                        if (seq[i] != this.sequence[i]) {
123                                return false;
124                        }
125                }
126                return true;
127        }
128
129}
Note: See TracBrowser for help on using the repository browser.