1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.plugin.usability2.rules.operator.wrapper; |
---|
16 | |
---|
17 | import java.util.Collection; |
---|
18 | import java.util.LinkedList; |
---|
19 | import java.util.List; |
---|
20 | import java.util.ListIterator; |
---|
21 | import java.util.RandomAccess; |
---|
22 | |
---|
23 | import de.ugoe.cs.autoquest.plugin.usability2.rules.operator.filter.EventTypeFilter; |
---|
24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance; |
---|
25 | |
---|
26 | /** |
---|
27 | * <p> |
---|
28 | * TODO comment |
---|
29 | * </p> |
---|
30 | * |
---|
31 | * @author Konni Hartmann |
---|
32 | */ |
---|
33 | public class InstanceSequenceFinder { |
---|
34 | |
---|
35 | // UtilityClass |
---|
36 | private InstanceSequenceFinder() {} |
---|
37 | |
---|
38 | private static class MatchesFilterComparator implements |
---|
39 | EqualComparator<IEventTaskInstance, EventTypeFilter> |
---|
40 | { |
---|
41 | |
---|
42 | /* |
---|
43 | * (non-Javadoc) |
---|
44 | * |
---|
45 | * @see |
---|
46 | * de.ugoe.cs.autoquest.plugin.usability2.rules.operator.wrapper.EqualComparator#isEqual |
---|
47 | * (java.lang.Object, java.lang.Object) |
---|
48 | */ |
---|
49 | @Override |
---|
50 | public boolean isEqual(IEventTaskInstance source, EventTypeFilter target) { |
---|
51 | return target.match(source.getTask()).isPresent(); |
---|
52 | } |
---|
53 | |
---|
54 | } |
---|
55 | |
---|
56 | public static Collection<List<IEventTaskInstance>> containsSequence(Collection<List<IEventTaskInstance>> sequences, |
---|
57 | List<EventTypeFilter> filters) |
---|
58 | { |
---|
59 | Collection<List<IEventTaskInstance>> result = new LinkedList<List<IEventTaskInstance>>(); |
---|
60 | |
---|
61 | EqualComparator<IEventTaskInstance, EventTypeFilter> comparator = |
---|
62 | new MatchesFilterComparator(); |
---|
63 | |
---|
64 | for (List<IEventTaskInstance> sequence : sequences) { |
---|
65 | if (indexOfSubList(sequence, filters, comparator) == -1) |
---|
66 | continue; |
---|
67 | result.add(sequence); |
---|
68 | } |
---|
69 | |
---|
70 | return result; |
---|
71 | } |
---|
72 | |
---|
73 | private static final int INDEXOFSUBLIST_THRESHOLD = 35; |
---|
74 | |
---|
75 | protected static <S, T> int indexOfSubList(List<S> source, |
---|
76 | List<T> target, |
---|
77 | EqualComparator<S, T> comp) |
---|
78 | { |
---|
79 | int sourceSize = source.size(); |
---|
80 | int targetSize = target.size(); |
---|
81 | int maxCandidate = sourceSize - targetSize; |
---|
82 | |
---|
83 | if (sourceSize < INDEXOFSUBLIST_THRESHOLD || |
---|
84 | (source instanceof RandomAccess && target instanceof RandomAccess)) |
---|
85 | { |
---|
86 | nextCand: for (int candidate = 0; candidate <= maxCandidate; candidate++) { |
---|
87 | for (int i = 0, j = candidate; i < targetSize; i++, j++) |
---|
88 | if (!comp.isEqual(source.get(j), target.get(i))) |
---|
89 | continue nextCand; // Element mismatch, try next cand |
---|
90 | return candidate; // All elements of candidate matched target |
---|
91 | } |
---|
92 | } |
---|
93 | else { // Iterator version of above algorithm |
---|
94 | ListIterator<S> si = source.listIterator(); |
---|
95 | nextCand: for (int candidate = 0; candidate <= maxCandidate; candidate++) { |
---|
96 | ListIterator<T> ti = target.listIterator(); |
---|
97 | for (int i = 0; i < targetSize; i++) { |
---|
98 | if (!comp.isEqual(si.next(), ti.next())) { |
---|
99 | // Back up source iterator to next candidate |
---|
100 | for (int j = 0; j < i; j++) |
---|
101 | si.previous(); |
---|
102 | continue nextCand; |
---|
103 | } |
---|
104 | } |
---|
105 | return candidate; |
---|
106 | } |
---|
107 | } |
---|
108 | return -1; // No candidate matched the target |
---|
109 | } |
---|
110 | } |
---|