source: trunk/autoquest-plugin-usability2/src/main/java/de/ugoe/cs/autoquest/plugin/usability2/rules/operator/wrapper/InstanceUtility.java @ 1326

Last change on this file since 1326 was 1326, checked in by khartmann, 10 years ago

Moved alexanders code into a new plugin project.
First commit of my experimental code (needs a lot of cleanup).

File size: 7.6 KB
Line 
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
15package de.ugoe.cs.autoquest.plugin.usability2.rules.operator.wrapper;
16
17import java.util.Collection;
18import java.util.List;
19import java.util.Map;
20import java.util.Map.Entry;
21
22import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
23import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
24import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance;
25import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
26import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance;
27import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
28import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance;
29import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
30import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance;
31import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
32import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
33
34/**
35 * <p>
36 * Utility Class that does the instance based setup for a Node of a TaskWrapper Tree.
37 * </p>
38 *
39 * @author Konni Hartmann
40 */
41public class InstanceUtility {
42
43    /**
44     * <p>
45     * Generate lists of filtered instances. For a set of root task instances generate a list containing
46     * all instances of the current task that are children of the said root instances.
47     * The root instances are either taken from the parent task or if the current task is the root
48     * task are all instances of the task.
49     * </p>
50     *
51     * @param task
52     */
53    public static void setupInstances(ITaskWrapper task) {
54        ITaskWrapper parent = task.getParent();
55        if(parent == null) {
56            // If task is root of this tree use all instances and exit
57            Collection<ITaskInstance> instances = task.getInstances();
58            for (ITaskInstance instance : instances) {
59                task.addFilteredInstance(instance, instance);
60            }
61            return;           
62        }
63       
64        ITask reference = task.getParent().getReference();
65        while (reference instanceof ITaskEntry) {
66            reference = ((ITaskEntry) reference).getReference();
67        }
68       
69        if (reference instanceof IEventTask) {
70            event(task);
71        }
72        else if (reference instanceof IIteration) {
73            iteration(task);
74        }
75        else if (reference instanceof IOptional) {
76            optional(task);
77        }
78        else if (reference instanceof ISelection) {
79            selection(task);
80        }
81        else if (reference instanceof ISequence) {
82            sequence(task);
83        }
84    }
85
86    public static void setupSequence(TaskWrapper task, int id) {
87        Map<ITaskInstance, List<ITaskInstance>> instances = task.getParent().getFilteredInstances();
88
89       
90        for (Entry<ITaskInstance, List<ITaskInstance>> entry : instances.entrySet()) {
91            ITaskInstance filter = entry.getKey();
92            @SuppressWarnings("unchecked")
93            List<ISequenceInstance> instanceList = (List<ISequenceInstance>)(List<?>) entry.getValue();
94
95            for (ISequenceInstance sequence : instanceList) {
96
97                if (id >= sequence.size()) {
98                    System.err.printf("\nNo #%d in sequence: %s\n", id, sequence);
99                    continue;
100                }
101
102                ITaskInstance instance = sequence.get(id);               
103                task.addFilteredInstance(filter, instance);
104            }
105        }       
106    }
107
108    private static void sequence(ITaskWrapper task) {
109//        Map<ITaskInstance, List<ITaskInstance>> instances = task.getParent().getFilteredInstances();
110//       
111//        for (Entry<ITaskInstance, List<ITaskInstance>> entry : instances.entrySet()) {
112//            ITaskInstance filter = entry.getKey();
113//            List<ISequenceInstance> instanceList = (List<ISequenceInstance>)(List<?>) entry.getValue();
114//           
115//            for (ISequenceInstance sequence : instanceList) {
116//                List<ITaskInstance> sequenceInstances = sequence.getChildren();
117//
118//                //TODO: Should be possible to index the current task!
119//                for (ITaskInstance instance : sequenceInstances) {
120//                    if (instance.getTask().equals(task.getReference()))
121//                    {
122//                        task.addFilteredInstance(filter, instance);
123//                        break;
124//                    }
125//                }                   
126//            }
127//        }
128        throw new RuntimeException("parent of task is a sequence without telling us the position this node occupies");       
129    }
130
131    private static void selection(ITaskWrapper task) {
132        Map<ITaskInstance, List<ITaskInstance>> instances = task.getParent().getFilteredInstances();
133       
134        for (Entry<ITaskInstance, List<ITaskInstance>> entry : instances.entrySet()) {
135            ITaskInstance filter = entry.getKey();
136            @SuppressWarnings("unchecked")
137            List<ISelectionInstance> instanceList = (List<ISelectionInstance>)(List<?>) entry.getValue();
138           
139            for (ISelectionInstance selection : instanceList) {
140                ITaskInstance instance = selection.getChild();
141               
142                //TODO: Should be possible to name the selected task!
143                if(instance != null && instance.getTask().equals(task.getReference()))
144                    task.addFilteredInstance(filter, instance);
145            }
146        }
147    }
148
149    private static void iteration(ITaskWrapper task) {
150        Map<ITaskInstance, List<ITaskInstance>> instances = task.getParent().getFilteredInstances();
151       
152        for (Entry<ITaskInstance, List<ITaskInstance>> entry : instances.entrySet()) {
153            ITaskInstance filter = entry.getKey();
154            @SuppressWarnings("unchecked")
155            List<IIterationInstance> instanceList = (List<IIterationInstance>)(List<?>) entry.getValue();
156           
157            for (IIterationInstance iteration : instanceList) {
158                List<ITaskInstance> iterationInstances = iteration.getChildren();
159               
160                for (ITaskInstance instance : iterationInstances) {
161                    task.addFilteredInstance(filter, instance);                   
162                }
163            }
164        }
165    }
166
167    private static void optional(ITaskWrapper task) {
168        Map<ITaskInstance, List<ITaskInstance>> instances = task.getParent().getFilteredInstances();
169       
170        for (Entry<ITaskInstance, List<ITaskInstance>> entry : instances.entrySet()) {
171            ITaskInstance filter = entry.getKey();
172            @SuppressWarnings("unchecked")
173            List<IOptionalInstance> instanceList = (List<IOptionalInstance>)(List<?>) entry.getValue();
174           
175            for (IOptionalInstance optional : instanceList) {
176                ITaskInstance instance = optional.getChild();
177                if(instance != null)
178                    task.addFilteredInstance(filter, instance);
179            }
180        }
181    }
182
183    private static void event(ITaskWrapper task) {
184        throw new RuntimeException("a parent of a task can never be an event (these are leafs!)");
185    }
186
187}
Note: See TracBrowser for help on using the repository browser.