1 | package de.uka.ipd.sdq.simucomframework.variables.cache; |
2 | |
3 | import java.util.HashMap; |
4 | |
5 | import de.uka.ipd.sdq.probfunction.math.IProbabilityFunctionFactory; |
6 | import de.uka.ipd.sdq.probfunction.math.IRandomGenerator; |
7 | |
8 | /** |
9 | * A cache for Stoex. This saves the time to parse the stoex again. |
10 | * Caches also the {@link IRandomGenerator} which may contain a fixed seed. |
11 | * |
12 | * @author Steffen Becker |
13 | * |
14 | */ |
15 | public class StoExCache { |
16 | |
17 | /** |
18 | * Hashmap mapping stoex strings on StoEx AST incl. Type Inferer and ProbfunctionCache |
19 | */ |
20 | private final HashMap<String,StoExCacheEntry> cache = new HashMap<String,StoExCacheEntry>(); |
21 | |
22 | // TODO: This is no real singleton anymore, as each simulation can have its own seed. |
23 | // Franz' version of adding the random number generator to the cache does not work in |
24 | // concurrent runs as well as in Protocom |
25 | private static StoExCache stoexSingleton = null; |
26 | private IProbabilityFunctionFactory probFunctionFactory = null; |
27 | |
28 | |
29 | |
30 | /** |
31 | * Only protected because the cost model should have its own instance of the StoExCache |
32 | * so that it can be resetted independently of the main singleton instance. |
33 | * @param randomGenerator |
34 | */ |
35 | protected StoExCache(IProbabilityFunctionFactory probFunctionFactory) { |
36 | this.probFunctionFactory = probFunctionFactory; |
37 | } |
38 | |
39 | public static void initialiseStoExCache(IProbabilityFunctionFactory probFunctionFactory){ |
40 | assert probFunctionFactory != null; |
41 | stoexSingleton = new StoExCache(probFunctionFactory); |
42 | } |
43 | |
44 | public static StoExCache singleton() { |
45 | assert stoexSingleton != null; |
46 | return stoexSingleton; |
47 | } |
48 | |
49 | /** |
50 | * Get a cached stoex parse tree and visitors. If entry is not in the cache it |
51 | * is created and added automatically. |
52 | * @param spec The stoex to search for in the cache |
53 | * @return The StoExCacheEntry containing the static information on the stoex |
54 | */ |
55 | public synchronized StoExCacheEntry getEntry(String spec) { |
56 | assert probFunctionFactory.getRandomGenerator() != null; |
57 | if (!cache.containsKey(spec)){ |
58 | cache.put(spec, new StoExCacheEntry(spec)); |
59 | } |
60 | return cache.get(spec); |
61 | } |
62 | |
63 | public IRandomGenerator getRandomGenerator(){ |
64 | return this.probFunctionFactory.getRandomGenerator(); |
65 | } |
66 | |
67 | public IProbabilityFunctionFactory getProbabilityFunctionFactory(){ |
68 | return this.probFunctionFactory; |
69 | } |
70 | } |