1 | package de.ugoe.cs.autoquest.plugin.usability2.rules.operator.visitors; |
---|
2 | |
---|
3 | import java.util.Arrays; |
---|
4 | import java.util.Collections; |
---|
5 | import java.util.Iterator; |
---|
6 | import java.util.LinkedList; |
---|
7 | import java.util.List; |
---|
8 | import java.util.Queue; |
---|
9 | |
---|
10 | import de.ugoe.cs.autoquest.plugin.usability2.rules.operator.IFilter; |
---|
11 | import de.ugoe.cs.autoquest.plugin.usability2.rules.operator.IResultTransformer; |
---|
12 | import de.ugoe.cs.autoquest.plugin.usability2.rules.operator.MarkingFilter; |
---|
13 | import de.ugoe.cs.autoquest.plugin.usability2.rules.operator.wrapper.ITaskEntry; |
---|
14 | import de.ugoe.cs.autoquest.plugin.usability2.rules.results.AbstractResult; |
---|
15 | import de.ugoe.cs.autoquest.plugin.usability2.rules.results.IMatch; |
---|
16 | import de.ugoe.cs.autoquest.plugin.usability2.rules.results.IResult; |
---|
17 | import de.ugoe.cs.autoquest.plugin.usability2.rules.results.UnmatchableResult; |
---|
18 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; |
---|
20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
23 | |
---|
24 | public abstract class AbstractBFSContinuableScanner extends MarkingFilter { |
---|
25 | |
---|
26 | public AbstractBFSContinuableScanner(IFilter marked) { |
---|
27 | super(marked); |
---|
28 | } |
---|
29 | |
---|
30 | public AbstractBFSContinuableScanner(IFilter marked, IResultTransformer transformer) { |
---|
31 | super(marked, transformer); |
---|
32 | } |
---|
33 | |
---|
34 | |
---|
35 | private static final List<ITask> EMPTY_LIST = Collections.emptyList(); |
---|
36 | |
---|
37 | private List<? extends ITask> getChildren(ITask task) { |
---|
38 | if (task instanceof ITaskEntry) |
---|
39 | return getChildren((ITaskEntry) task); |
---|
40 | else if (task instanceof ISequence) |
---|
41 | return getChildren((ISequence) task); |
---|
42 | else if (task instanceof IOptional) |
---|
43 | return getChildren((IOptional) task); |
---|
44 | else if (task instanceof IIteration) |
---|
45 | return getChildren((IIteration) task); |
---|
46 | else if (task instanceof ISelection) |
---|
47 | return getChildren((ISelection) task); |
---|
48 | |
---|
49 | return EMPTY_LIST; |
---|
50 | } |
---|
51 | |
---|
52 | private List<? extends ITask> getChildren(ITaskEntry task) { |
---|
53 | return task.getChildren(); |
---|
54 | } |
---|
55 | |
---|
56 | private List<? extends ITask> getChildren(ISelection task) { |
---|
57 | return task.getChildren(); |
---|
58 | } |
---|
59 | |
---|
60 | private List<? extends ITask> getChildren(IOptional task) { |
---|
61 | return Arrays.asList(task.getMarkedTask()); |
---|
62 | } |
---|
63 | |
---|
64 | private List<? extends ITask> getChildren(IIteration task) { |
---|
65 | return Arrays.asList(task.getMarkedTask()); |
---|
66 | } |
---|
67 | |
---|
68 | private List<? extends ITask> getChildren(ISequence task) { |
---|
69 | return task.getChildren(); |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | class VisitorInstance { |
---|
74 | |
---|
75 | Queue<ITask> queue; |
---|
76 | IFilter filter; |
---|
77 | |
---|
78 | VisitorInstance(ITask root, IFilter filter) { |
---|
79 | queue = new LinkedList<ITask>(); |
---|
80 | queue.add(root); |
---|
81 | this.filter = filter; |
---|
82 | } |
---|
83 | |
---|
84 | public IResult getNextMatch() { |
---|
85 | ITask task; |
---|
86 | while( (task = queue.poll()) != null ) { |
---|
87 | visit(task, filter, queue); |
---|
88 | |
---|
89 | IResult r = accept(task, filter); |
---|
90 | if (r.isPresent()) |
---|
91 | return r; |
---|
92 | |
---|
93 | } |
---|
94 | |
---|
95 | return null; |
---|
96 | } |
---|
97 | |
---|
98 | } |
---|
99 | |
---|
100 | protected void visit(ITask task, IFilter filter, Queue<ITask> queue) { |
---|
101 | |
---|
102 | ITask taskType = task; |
---|
103 | |
---|
104 | if (taskType instanceof ITaskEntry) |
---|
105 | taskType = ((ITaskEntry) task).getReference(); |
---|
106 | |
---|
107 | List<? extends ITask> children = getChildren(task); |
---|
108 | |
---|
109 | if (taskType instanceof ISequence) |
---|
110 | visitSequence(children, queue); |
---|
111 | else if (taskType instanceof IOptional) |
---|
112 | visitOptional(children, queue); |
---|
113 | else if (taskType instanceof IIteration) |
---|
114 | visitIteration(children, queue); |
---|
115 | else if (taskType instanceof ISelection) |
---|
116 | visitSelection(children, queue); |
---|
117 | } |
---|
118 | |
---|
119 | protected void visitSelection(List<? extends ITask> children, Queue<ITask> queue) { |
---|
120 | queue.addAll(children); |
---|
121 | } |
---|
122 | |
---|
123 | protected void visitIteration(List<? extends ITask> children, Queue<ITask> queue) { |
---|
124 | queue.addAll(children); |
---|
125 | } |
---|
126 | |
---|
127 | protected void visitOptional(List<? extends ITask> children, Queue<ITask> queue) { |
---|
128 | queue.addAll(children); |
---|
129 | } |
---|
130 | |
---|
131 | protected void visitSequence(List<? extends ITask> children, Queue<ITask> queue) { |
---|
132 | queue.addAll(children); |
---|
133 | } |
---|
134 | |
---|
135 | protected IResult accept(ITask task, IFilter filter) { |
---|
136 | return filter.match(task); |
---|
137 | } |
---|
138 | |
---|
139 | @Override |
---|
140 | protected IResult match(final ITask task, final IFilter filter) { |
---|
141 | final VisitorInstance v = new VisitorInstance(task, filter); |
---|
142 | final IResult match = v.getNextMatch(); |
---|
143 | |
---|
144 | if (match == null) |
---|
145 | return UnmatchableResult.NO_MATCH_FOUND; |
---|
146 | |
---|
147 | return new AbstractResult(true) { |
---|
148 | @Override |
---|
149 | public Iterator<IMatch> iterator() { |
---|
150 | return new Iterator<IMatch>() { |
---|
151 | IMatch current; |
---|
152 | VisitorInstance v = new VisitorInstance(task, filter); |
---|
153 | IResult curResult = v.getNextMatch(); |
---|
154 | Iterator<IMatch> result = null; |
---|
155 | |
---|
156 | @Override |
---|
157 | public void remove() { |
---|
158 | throw new UnsupportedOperationException(); |
---|
159 | } |
---|
160 | |
---|
161 | @Override |
---|
162 | public IMatch next() { |
---|
163 | IMatch tmp = current; |
---|
164 | current = null; |
---|
165 | return tmp; |
---|
166 | } |
---|
167 | |
---|
168 | @Override |
---|
169 | public boolean hasNext() { |
---|
170 | if (current != null) |
---|
171 | return true; |
---|
172 | |
---|
173 | if (curResult == null) |
---|
174 | return false; |
---|
175 | |
---|
176 | do { |
---|
177 | if(result == null) |
---|
178 | result = curResult.iterator(); |
---|
179 | if (result.hasNext()) { |
---|
180 | current = result.next(); |
---|
181 | return true; |
---|
182 | } else |
---|
183 | result = null; |
---|
184 | } while ( (curResult = v.getNextMatch()) != null ); |
---|
185 | return false; |
---|
186 | } |
---|
187 | }; |
---|
188 | } |
---|
189 | }; |
---|
190 | } |
---|
191 | |
---|
192 | } |
---|