[1326] | 1 | |
---|
| 2 | package de.ugoe.cs.autoquest.plugin.usability2.statistics; |
---|
| 3 | |
---|
| 4 | import java.util.ArrayList; |
---|
| 5 | import java.util.Collection; |
---|
| 6 | import java.util.LinkedList; |
---|
| 7 | import java.util.List; |
---|
| 8 | import java.util.Map; |
---|
| 9 | import java.util.NoSuchElementException; |
---|
| 10 | |
---|
| 11 | import org.apache.commons.collections15.map.HashedMap; |
---|
| 12 | |
---|
| 13 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance; |
---|
| 14 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance; |
---|
| 15 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance; |
---|
| 16 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance; |
---|
| 17 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
| 18 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
| 19 | |
---|
| 20 | public class StatisticNode { |
---|
| 21 | |
---|
| 22 | private static class StatisticData { |
---|
| 23 | StatisticData parent = null; |
---|
| 24 | ITaskInstance instance = null; |
---|
| 25 | |
---|
| 26 | int getCount(int index) { |
---|
| 27 | return 1; |
---|
| 28 | } |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | private static class StatisticDataVarying extends StatisticData { |
---|
| 32 | int[] counts; |
---|
| 33 | |
---|
| 34 | @Override |
---|
| 35 | int getCount(int index) { |
---|
| 36 | return counts[index]; |
---|
| 37 | } |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | private Map<ITaskInstance, StatisticData> map; |
---|
| 41 | |
---|
| 42 | private final ITask root; |
---|
| 43 | |
---|
| 44 | public StatisticNode(ITask root) { |
---|
| 45 | this.root = root; |
---|
| 46 | map = new HashedMap<ITaskInstance, StatisticData>(); |
---|
| 47 | analyse(); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | private void analyse() { |
---|
| 51 | Collection<ITaskInstance> instances = root.getInstances(); |
---|
| 52 | analyseNode(instances, null); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | private void analyseNode(Collection<ITaskInstance> nodes, StatisticData parent) { |
---|
| 56 | if (nodes == null) |
---|
| 57 | return; |
---|
| 58 | |
---|
| 59 | for (ITaskInstance node : nodes) { |
---|
| 60 | analyseNode(node, parent); |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | public ITaskInstance findCommonParent(ITaskInstance i1, ITaskInstance i2) { |
---|
| 65 | if (i1 == i2) |
---|
| 66 | return i1; |
---|
| 67 | |
---|
| 68 | StatisticData d1 = map.get(i1); |
---|
| 69 | StatisticData d2 = map.get(i2); |
---|
| 70 | |
---|
| 71 | if (d1 == null || d2 == null) |
---|
| 72 | return null; |
---|
| 73 | |
---|
| 74 | LinkedList<StatisticData> list1 = new LinkedList<StatisticData>(); |
---|
| 75 | while ((d1 = d1.parent) != null) { |
---|
| 76 | list1.push(d1); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | LinkedList<StatisticData> list2 = new LinkedList<StatisticData>(); |
---|
| 80 | while ((d2 = d2.parent) != null) { |
---|
| 81 | list2.push(d2); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | ITaskInstance result = null; |
---|
| 85 | |
---|
| 86 | try { |
---|
| 87 | while ((d1 = list1.pop()) == (d2 = list2.pop())) { |
---|
| 88 | result = d1.instance; |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | catch (NoSuchElementException e) { |
---|
| 92 | // ignore |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | return result; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | public Collection<ITaskInstance> findAllInstances(ITask task) { |
---|
| 99 | List<ITaskInstance> matches = new ArrayList<ITaskInstance>(); |
---|
| 100 | |
---|
| 101 | for (ITaskInstance instance : map.keySet()) { |
---|
| 102 | if (instance.getTask().equals(task)) |
---|
| 103 | matches.add(instance); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | return matches; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | private void analyseNode(ITaskInstance node, StatisticData parent) { |
---|
| 110 | if (node == null) |
---|
| 111 | return; |
---|
| 112 | |
---|
| 113 | StatisticData data = new StatisticData(); |
---|
| 114 | data.parent = parent; |
---|
| 115 | data.instance = node; |
---|
| 116 | map.put(node, data); |
---|
| 117 | |
---|
| 118 | if (node instanceof IOptionalInstance) { |
---|
| 119 | ITaskInstance child = ((IOptionalInstance) node).getChild(); |
---|
| 120 | analyseNode(child, parent); |
---|
| 121 | } |
---|
| 122 | else if (node instanceof ISelectionInstance) { |
---|
| 123 | ITaskInstance child = ((ISelectionInstance) node).getChild(); |
---|
| 124 | analyseNode(child, parent); |
---|
| 125 | } |
---|
| 126 | else if (node instanceof IIterationInstance) { |
---|
| 127 | List<ITaskInstance> children = ((IIterationInstance) node).getChildren(); |
---|
| 128 | analyseNode(children, parent); |
---|
| 129 | } |
---|
| 130 | else if (node instanceof ISequenceInstance) { |
---|
| 131 | List<ITaskInstance> children = ((ISequenceInstance) node).getChildren(); |
---|
| 132 | analyseNode(children, parent); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | } |
---|
| 136 | } |
---|