| 1 | package de.uka.ipd.sdq.probespec.framework; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.HashMap; |
| 5 | import java.util.List; |
| 6 | import java.util.Map.Entry; |
| 7 | |
| 8 | /** |
| 9 | * The blackboard is a mediator between entities producing samples ( |
| 10 | * {@link ProbeSetSample}s) and entities consuming samples. Producers offer |
| 11 | * their samples by calling {@link #addSample(ProbeSetSample)}. Consumers |
| 12 | * implement the {@link IBlackboardListener} interface and register themselves |
| 13 | * at the blackboard. Afterwards they receive all samples they are interested |
| 14 | * in. |
| 15 | * <p> |
| 16 | * Consumers can express their interest by passing one or more topics while |
| 17 | * registering as observer. When no topics are passed, the consumer gets |
| 18 | * notified of all arriving samples. |
| 19 | * <p> |
| 20 | * Published samples can be stored at the blackboard. When a consumer gets |
| 21 | * notified of a new samples, it has to vote whether the blackboard is supposed |
| 22 | * to keep (store) the sample. A single {@link BlackboardVote#RETAIN}-vote is |
| 23 | * sufficient to store the sample. Only when all consumers vote |
| 24 | * {@link BlackboardVote#DISCARD} the sample gets discarded. |
| 25 | * <p> |
| 26 | * Samples are unique identified by a pair of a ProbeSetID and a |
| 27 | * {@link RequestContext}, encapsulated by a {@link ProbeSetAndRequestContext}. |
| 28 | * Thereby the blackboard can store several samples originating from the same |
| 29 | * ProbeSet, one for each RequestContext. |
| 30 | * |
| 31 | * @author Faber |
| 32 | * @author Philipp Merkle |
| 33 | * |
| 34 | */ |
| 35 | public class SampleBlackboard implements ISampleBlackboard { |
| 36 | |
| 37 | private List<IBlackboardListener> listeners; |
| 38 | |
| 39 | private HashMap<Integer, ArrayList<IBlackboardListener>> topicToListenersMap; |
| 40 | |
| 41 | // stores the samples |
| 42 | // maps RequestContext to map (ProbeSetAndRequestContext -> ProbeSetSample) |
| 43 | private HashMap<RequestContext, HashMap<ProbeSetAndRequestContext, ProbeSetSample>> sampleMap = new HashMap<RequestContext, HashMap<ProbeSetAndRequestContext, ProbeSetSample>>(); |
| 44 | |
| 45 | public SampleBlackboard() { |
| 46 | listeners = new ArrayList<IBlackboardListener>(); |
| 47 | topicToListenersMap = new HashMap<Integer, ArrayList<IBlackboardListener>>(); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | public void addSample(ProbeSetSample pss) { |
| 52 | // notify listeners and obtain deletion vote |
| 53 | BlackboardVote deletionVote = fireSampleArrived(pss); |
| 54 | |
| 55 | // retain sample if at least one RETAIN-vote exists |
| 56 | if (deletionVote.equals(BlackboardVote.RETAIN)) { |
| 57 | RequestContext context = pss.getProbeSetAndRequestContext() |
| 58 | .getCtxID(); |
| 59 | HashMap<ProbeSetAndRequestContext, ProbeSetSample> contextMap = sampleMap |
| 60 | .get(context); |
| 61 | // create hash map for request context, if not done yet |
| 62 | if (contextMap == null) { |
| 63 | contextMap = new HashMap<ProbeSetAndRequestContext, ProbeSetSample>(); |
| 64 | sampleMap.put(context, contextMap); |
| 65 | } |
| 66 | contextMap.put(pss.getProbeSetAndRequestContext(), pss); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | public void deleteSample(ProbeSetAndRequestContext pss) { |
| 72 | sampleMap.get(pss.getCtxID()).remove(pss); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | public void deleteSamplesInRequestContext(RequestContext requestContext) { |
| 77 | // delete samples in child contexts, if there are any |
| 78 | if (requestContext.getChildContexts() != null) { |
| 79 | for (RequestContext child : requestContext.getChildContexts()) { |
| 80 | deleteSamplesInRequestContext(child); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | HashMap<ProbeSetAndRequestContext, ProbeSetSample> contextMap = sampleMap |
| 85 | .get(requestContext); |
| 86 | if (contextMap != null) { |
| 87 | contextMap.clear(); |
| 88 | sampleMap.remove(requestContext); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * {@inheritDoc} |
| 94 | * <p> |
| 95 | * If no ProbeSetSample can be found for the RequestContext and the |
| 96 | * RequestContext has a parent context, the search will be performed for |
| 97 | * that parent context too. This continues recursively until a |
| 98 | * RequestContext is reached that does not have a parent context. |
| 99 | * <p> |
| 100 | * This recursive search is useful for e.g. finding the start ProbeSetSample |
| 101 | * (taken before a fork) for a given end ProbeSetSample (taken within a |
| 102 | * fork). |
| 103 | * |
| 104 | * @param probeSetSampleID |
| 105 | * the encapsulated probeId and RequestContextID |
| 106 | * @return the ProbeSetSample for the probeSetSampleID, if there is any; |
| 107 | * else null. |
| 108 | */ |
| 109 | public ProbeSetSample getSample(ProbeSetAndRequestContext probeSetSampleID) { |
| 110 | // try to find the ProbeSetSample in the specified context |
| 111 | ProbeSetSample sample = obtainSample(probeSetSampleID); |
| 112 | if (sample != null) { |
| 113 | return sample; |
| 114 | } |
| 115 | |
| 116 | // try to find the ProbeSetSample in a parent context |
| 117 | RequestContext ctx = probeSetSampleID.getCtxID().getParentContext(); |
| 118 | Integer probeSetID = probeSetSampleID.getProbeSetID(); |
| 119 | while (ctx != null) { |
| 120 | ProbeSetAndRequestContext pssID = new ProbeSetAndRequestContext( |
| 121 | probeSetID, ctx); |
| 122 | ProbeSetSample pss = obtainSample(pssID); |
| 123 | if (pss != null) { |
| 124 | return pss; |
| 125 | } |
| 126 | ctx = ctx.getParentContext(); |
| 127 | } |
| 128 | return null; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Returns the {@link ProbeSetSample} for the specified |
| 133 | * {@link ProbeSetAndRequestContext}. |
| 134 | * |
| 135 | * @param probeSetSampleID |
| 136 | * @return |
| 137 | */ |
| 138 | private ProbeSetSample obtainSample( |
| 139 | ProbeSetAndRequestContext probeSetSampleID) { |
| 140 | HashMap<ProbeSetAndRequestContext, ProbeSetSample> contextMap = sampleMap |
| 141 | .get(probeSetSampleID.getCtxID()); |
| 142 | if (contextMap != null) { |
| 143 | return contextMap.get(probeSetSampleID); |
| 144 | } |
| 145 | return null; |
| 146 | } |
| 147 | |
| 148 | public int size() { |
| 149 | int i = 0; |
| 150 | for (Entry<RequestContext, HashMap<ProbeSetAndRequestContext, ProbeSetSample>> e : sampleMap |
| 151 | .entrySet()) { |
| 152 | i += e.getValue().size(); |
| 153 | } |
| 154 | return i; |
| 155 | } |
| 156 | |
| 157 | public void addBlackboardListener(IBlackboardListener l, Integer... topics) { |
| 158 | if (topics.length == 0) { |
| 159 | listeners.add(l); |
| 160 | } else { |
| 161 | // add listener for each topic |
| 162 | for (Integer t : topics) { |
| 163 | ArrayList<IBlackboardListener> listeners = topicToListenersMap |
| 164 | .get(t); |
| 165 | if (listeners == null) { |
| 166 | listeners = new ArrayList<IBlackboardListener>(); |
| 167 | topicToListenersMap.put(t, listeners); |
| 168 | } |
| 169 | listeners.add(l); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Notifies all specified listeners of a new {@link ProbeSetSample}. |
| 177 | * |
| 178 | * @param pss |
| 179 | * @param listeners |
| 180 | * the listeners that are to be notified |
| 181 | * @return {@link BlackboardVote#RETAIN} when the sample has to be stored; |
| 182 | * {@link BlackboardVote#DISCARD} else. |
| 183 | */ |
| 184 | private BlackboardVote fireSampleArrived(ProbeSetSample pss, |
| 185 | List<IBlackboardListener> listeners) { |
| 186 | // private BlackboardVote fireSampleArrived(ProbeSetSample pss) { |
| 187 | BlackboardVote deletionVote = BlackboardVote.DISCARD; |
| 188 | for (IBlackboardListener l : listeners) { |
| 189 | if (l.sampleArrived(pss).equals(BlackboardVote.RETAIN)) { |
| 190 | deletionVote = BlackboardVote.RETAIN; |
| 191 | } |
| 192 | } |
| 193 | return deletionVote; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Notifies all listeners of a new {@link ProbeSetSample}. Both listeners |
| 198 | * that are not registered for a specific topic and listeners interested in |
| 199 | * one or more topics get notified of the new ProbeSetSample. |
| 200 | * |
| 201 | * @param pss |
| 202 | * @return {@link BlackboardVote#RETAIN} when the sample has to be stored; |
| 203 | * {@link BlackboardVote#DISCARD} else. |
| 204 | */ |
| 205 | private BlackboardVote fireSampleArrived(ProbeSetSample pss) { |
| 206 | // notify listeners that are not registered for a specific topic |
| 207 | BlackboardVote firstDeletionVote = fireSampleArrived(pss, listeners); |
| 208 | |
| 209 | // notify listeners that are registered for the sample's topic |
| 210 | Integer topic = pss.getProbeSetAndRequestContext().getProbeSetID(); |
| 211 | List<IBlackboardListener> listeners = topicToListenersMap.get(topic); |
| 212 | BlackboardVote secondDeletionVote = null; |
| 213 | if (listeners != null) { |
| 214 | secondDeletionVote = fireSampleArrived(pss, listeners); |
| 215 | } |
| 216 | |
| 217 | if (firstDeletionVote.equals(BlackboardVote.DISCARD) |
| 218 | && (secondDeletionVote == null || secondDeletionVote |
| 219 | .equals(BlackboardVote.DISCARD))) { |
| 220 | return BlackboardVote.DISCARD; |
| 221 | } |
| 222 | return BlackboardVote.RETAIN; |
| 223 | } |
| 224 | |
| 225 | } |