1 | package de.uka.ipd.sdq.simucomframework.variables.functions; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.HashMap; |
5 | |
6 | import de.uka.ipd.sdq.probfunction.math.IPDFFactory; |
7 | import de.uka.ipd.sdq.probfunction.math.IRandomGenerator; |
8 | |
9 | import de.uka.ipd.sdq.simucomframework.variables.exceptions.FunctionParametersNotAcceptedException; |
10 | import de.uka.ipd.sdq.simucomframework.variables.exceptions.FunctionUnknownException; |
11 | import de.uka.ipd.sdq.stoex.analyser.probfunction.ProbfunctionHelper; |
12 | |
13 | /** |
14 | * Function library of functions available in stoex in simucom |
15 | * @author Steffen Becker |
16 | * |
17 | */ |
18 | public class FunctionLib { |
19 | |
20 | private HashMap<String, IFunction> myFunctions = new HashMap<String, IFunction>(); |
21 | private IRandomGenerator randomStream; |
22 | |
23 | /** |
24 | * Initialise the function library. |
25 | * |
26 | */ |
27 | public FunctionLib(IRandomGenerator randomStream, IPDFFactory factory) { |
28 | this.randomStream = randomStream; |
29 | addStdFunctionsToLib(randomStream, factory); |
30 | } |
31 | |
32 | /** |
33 | * Add the functions available in simucoms standard library |
34 | */ |
35 | private void addStdFunctionsToLib(IRandomGenerator randomGen, IPDFFactory factory) { |
36 | myFunctions.put("Norm",new NormDistFunction(randomGen, factory)); |
37 | myFunctions.put("Exp",new ExpDistFunction(randomGen, factory)); |
38 | myFunctions.put("Pois",new PoissonDistFunction(randomGen, factory)); |
39 | myFunctions.put("UniDouble",new UniDoubleDistFunction(randomGen, factory)); |
40 | myFunctions.put("UniInt",new UniIntDistFunction(randomGen, factory)); |
41 | myFunctions.put("Trunc",new TruncFunction()); |
42 | myFunctions.put(RoundFunction.ROUND_FUNCTION_NAME,new RoundFunction()); |
43 | myFunctions.put(ProbfunctionHelper.LOGNORM,new LogNormDistFunction(randomGen, factory)); |
44 | myFunctions.put(ProbfunctionHelper.LOGNORM2,new LogNormDistFunctionFromMoments(randomGen, factory)); |
45 | myFunctions.put(ProbfunctionHelper.GAMMA,new GammaDistFunction(randomGen, factory)); |
46 | myFunctions.put(ProbfunctionHelper.GAMMA2,new GammaDistFunctionFromMoments(randomGen, factory)); |
47 | myFunctions.put(MinFunction.MIN_FUNCTION_NAME, new MinFunction()); |
48 | myFunctions.put(MaxFunction.MAX_FUNCTION_NAME, new MaxFunction()); |
49 | myFunctions.put(MinDeviationFunction.MIN_DEVIATION_FUNCTION_NAME, new MinDeviationFunction()); |
50 | myFunctions.put(MaxDeviationFunction.MAX_DEVIATION_FUNCTION_NAME, new MaxDeviationFunction()); |
51 | } |
52 | |
53 | /** |
54 | * Add a function to this function library. |
55 | * The passed function should use the instance of |
56 | * SimuComDefaultRandomNumberGenerator returned by |
57 | * {@link #getRandomStream()}. |
58 | * |
59 | * @param id Name of the function to add. It is the ID in the stoex |
60 | * @param f The function object used during evaluation |
61 | */ |
62 | public void addFunction(String id, IFunction f) { |
63 | myFunctions.put(id, f); |
64 | } |
65 | |
66 | public IRandomGenerator getRandomStream(){ |
67 | return randomStream; |
68 | } |
69 | |
70 | /** |
71 | * Evaluate the function with the given ID using the given parameters, |
72 | * e.g. f(10,29) |
73 | * @param functionID ID of the function to evaluate |
74 | * @param parameterValues Parameter set to use in evaluation |
75 | * @return Value of the function evaluated using the given parameters |
76 | */ |
77 | public Object evaluate(String functionID, ArrayList<Object> parameterValues) { |
78 | if (!myFunctions.containsKey(functionID)) |
79 | throw new FunctionUnknownException("Function "+functionID+" is unknown! Evaluation aborted"); |
80 | IFunction f = myFunctions.get(functionID); |
81 | if (!f.checkParameters(parameterValues)) |
82 | throw new FunctionParametersNotAcceptedException("Parameters passed to function "+functionID+" do not match function definition!"); |
83 | return f.evaluate(parameterValues); |
84 | } |
85 | } |