[1767] | 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.tasktrees.treeifc; |
---|
| 16 | |
---|
[2030] | 17 | import java.util.Collection; |
---|
| 18 | import java.util.HashMap; |
---|
| 19 | import java.util.HashSet; |
---|
| 20 | import java.util.LinkedList; |
---|
| 21 | import java.util.List; |
---|
| 22 | import java.util.Map; |
---|
| 23 | import java.util.Set; |
---|
| 24 | |
---|
[1767] | 25 | /** |
---|
| 26 | * <p> |
---|
| 27 | * TODO comment |
---|
| 28 | * </p> |
---|
| 29 | * |
---|
| 30 | * @author Patrick Harms |
---|
| 31 | */ |
---|
| 32 | public class TaskTreeUtils { |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | * |
---|
| 36 | */ |
---|
| 37 | public static boolean isChild(ITask potentialChild, ITask potentialParent) { |
---|
| 38 | if (potentialParent instanceof IStructuringTemporalRelationship) { |
---|
| 39 | for (ITask child : ((IStructuringTemporalRelationship) potentialParent).getChildren()) { |
---|
| 40 | if (child.equals(potentialChild) || isChild(potentialChild, child)) { |
---|
| 41 | return true; |
---|
| 42 | } |
---|
| 43 | } |
---|
| 44 | } |
---|
| 45 | else if (potentialParent instanceof IMarkingTemporalRelationship) { |
---|
| 46 | ITask child = ((IMarkingTemporalRelationship) potentialParent).getMarkedTask(); |
---|
| 47 | if (child.equals(potentialChild) || isChild(potentialChild, child)) { |
---|
| 48 | return true; |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | return false; |
---|
| 53 | } |
---|
| 54 | |
---|
[2030] | 55 | /** |
---|
| 56 | * |
---|
| 57 | */ |
---|
[2161] | 58 | public static Set<ISequence> getMostProminentTasks(ITaskModel model) { |
---|
| 59 | return getMostProminentTasks(model, model.getTasks()); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * |
---|
| 64 | */ |
---|
[2030] | 65 | public static Set<ISequence> getMostProminentTasks(ITaskModel model, |
---|
| 66 | Collection<ITask> tasks) |
---|
| 67 | { |
---|
| 68 | List<ISequence> sequences = new LinkedList<>(); |
---|
| 69 | |
---|
| 70 | for (ITask task : tasks) { |
---|
| 71 | if (task instanceof ISequence) { |
---|
| 72 | sequences.add((ISequence) task); |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | return getMostProminentSequences(model, sequences); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | /** |
---|
| 80 | * |
---|
| 81 | */ |
---|
| 82 | public static Set<ISequence> getMostProminentSequences(ITaskModel model, |
---|
| 83 | Collection<ISequence> tasks) |
---|
| 84 | { |
---|
| 85 | Map<Integer, List<ISequence>> sortedSequences = new HashMap<>(); |
---|
| 86 | |
---|
| 87 | int maxCoverage = 0; |
---|
| 88 | |
---|
| 89 | for (ITask task : tasks) { |
---|
| 90 | int coveredEvents = |
---|
| 91 | model.getTaskInfo((ISequence) task).getMeasureValue(TaskMetric.EVENT_COVERAGE); |
---|
| 92 | |
---|
| 93 | List<ISequence> sequencesWithSameCoverage = sortedSequences.get(coveredEvents); |
---|
| 94 | |
---|
| 95 | if (sequencesWithSameCoverage == null) { |
---|
| 96 | sequencesWithSameCoverage = new LinkedList<>(); |
---|
| 97 | sortedSequences.put(coveredEvents, sequencesWithSameCoverage); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | sequencesWithSameCoverage.add((ISequence) task); |
---|
| 101 | |
---|
| 102 | maxCoverage = Math.max(maxCoverage, coveredEvents); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | Set<ISequence> result = new HashSet<>(); |
---|
| 106 | |
---|
| 107 | for (int i = maxCoverage; i > 0; i--) { |
---|
| 108 | List<ISequence> sequencesWithSameCoverage = sortedSequences.get(i); |
---|
| 109 | |
---|
| 110 | if (sequencesWithSameCoverage != null) { |
---|
| 111 | result.addAll(sequencesWithSameCoverage); |
---|
| 112 | |
---|
| 113 | if (result.size() * 5 >= tasks.size()) { |
---|
| 114 | break; |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | return result; |
---|
| 120 | } |
---|
[2161] | 121 | |
---|
| 122 | |
---|
[1767] | 123 | /** |
---|
| 124 | * |
---|
| 125 | */ |
---|
[2161] | 126 | public static int getNoOfEventsCoveredBySequences(Set<ISequence> sequences) { |
---|
| 127 | return getEventsCoveredBySequences(sequences).size(); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | * |
---|
| 132 | */ |
---|
| 133 | public static Set<IEventTaskInstance> getEventsCoveredBySequences(Set<ISequence> sequences) { |
---|
| 134 | final Set<IEventTaskInstance> events = new HashSet<>(); |
---|
| 135 | |
---|
| 136 | for (ISequence task : sequences) { |
---|
| 137 | for (ITaskInstance instance : task.getInstances()) { |
---|
| 138 | instance.accept(new DefaultTaskInstanceTraversingVisitor() { |
---|
| 139 | @Override |
---|
| 140 | public void visit(IEventTaskInstance eventTaskInstance) { |
---|
| 141 | events.add(eventTaskInstance); |
---|
| 142 | } |
---|
| 143 | }); |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | return events; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | /** |
---|
| 151 | * |
---|
| 152 | */ |
---|
[1767] | 153 | private TaskTreeUtils() { /* prevent instantiation */ } |
---|
| 154 | } |
---|