source: branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/NumberSequence.java @ 1619

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

Detecting matches in other matches

File size: 1.7 KB
RevLine 
[1553]1package de.ugoe.cs.autoquest.tasktrees.alignment.algorithms;
[1450]2
[1618]3import java.util.ArrayList;
[1568]4import java.util.Random;
[1450]5
[1618]6public class NumberSequence {
[1450]7        private int[] sequence;
8        private int signature;
[1618]9
[1450]10        public NumberSequence(int size) {
[1618]11
[1450]12                sequence = new int[size];
13        }
[1618]14
[1450]15        public int[] getSequence() {
16                return sequence;
17        }
[1618]18
[1450]19        public void setSequence(int[] sequence) {
20                this.sequence = sequence;
21        }
[1618]22
[1450]23        public int getSignature() {
24                return signature;
25        }
[1618]26
[1450]27        public void setSignature(int signature) {
28                this.signature = signature;
29        }
[1618]30
31        public void printSequence() {
[1558]32                for (int i = 0; i < sequence.length; i++) {
[1559]33                        System.out.format("%5d", sequence[i]);
[1558]34                }
35                System.out.println();
36        }
[1618]37
38        public NumberSequence shuffle() {
[1568]39                NumberSequence result = new NumberSequence(sequence.length);
40                result.setSequence(this.sequence);
41                Random rgen = new Random();
[1618]42
43                for (int i = 0; i < result.sequence.length; i++) {
44                        int randomPosition = rgen.nextInt(result.sequence.length);
45                        int temp = result.sequence[i];
46                        result.sequence[i] = result.sequence[randomPosition];
47                        result.sequence[randomPosition] = temp;
[1568]48                }
49                return result;
[1618]50
[1568]51        }
[1450]52
[1618]53        // TODO: This can be done faster
54        public int containsPattern(ArrayList<NumberSequence> pattern) {
55                int i = 0;
56                int count = 0;
57                int[] pat1 = pattern.get(0).getSequence();
58                int[] pat2 = pattern.get(1).getSequence();
59                notmatched: while (i < sequence.length - pat1.length) {
60                        if (sequence[i] == pat1[0] || sequence[i] == pat2[0]) {
61                                for (int j = 0; j < pat1.length; j++) {
62                                        if (sequence[i + j] != pat1[j]
63                                                        && sequence[i + j] != pat2[j]) {
[1619]64                                                i++;
[1618]65                                                continue notmatched;
66                                        }
67                                }
68                                count++;
69                        }
70                        i++;
71                }
72                return count;
73        }
74
[1592]75        public int size() {
76                return sequence.length;
77        }
[1450]78}
Note: See TracBrowser for help on using the repository browser.