| 1 | package de.uka.ipd.sdq.pcm.cost.helper; |
| 2 | |
| 3 | import org.apache.log4j.Logger; |
| 4 | |
| 5 | import de.uka.ipd.sdq.pcm.cost.ScalarFunction; |
| 6 | import de.uka.ipd.sdq.pcm.cost.VariableProcessingResourceCost; |
| 7 | import de.uka.ipd.sdq.probfunction.math.IProbabilityFunctionFactory; |
| 8 | import de.uka.ipd.sdq.probfunction.math.IRandomGenerator; |
| 9 | import de.uka.ipd.sdq.probfunction.math.impl.ProbabilityFunctionFactoryImpl; |
| 10 | import de.uka.ipd.sdq.simucomframework.SimuComDefaultRandomNumberGenerator; |
| 11 | import de.uka.ipd.sdq.simucomframework.variables.StackContext; |
| 12 | import de.uka.ipd.sdq.simucomframework.variables.cache.StoExCache; |
| 13 | import de.uka.ipd.sdq.simucomframework.variables.cache.StoExCacheEntry; |
| 14 | import de.uka.ipd.sdq.simucomframework.variables.stackframe.SimulatedStackframe; |
| 15 | import de.uka.ipd.sdq.simucomframework.variables.stoexvisitor.PCMStoExEvaluationVisitor; |
| 16 | import de.uka.ipd.sdq.simucomframework.variables.stoexvisitor.VariableMode; |
| 17 | import de.uka.ipd.sdq.stoex.Expression; |
| 18 | |
| 19 | public class CostUtil { |
| 20 | |
| 21 | /** Logger for log4j. */ |
| 22 | private static Logger logger = |
| 23 | Logger.getLogger("de.uka.ipd.sdq.pcm.cost.helper.CostUtil"); |
| 24 | |
| 25 | private static final String PROCESSING_RATE_VARIABLE = "procRate.VALUE"; |
| 26 | private static final String NUMBER_OF_CORES_VARIABLE = "numberOfReplicas.VALUE"; |
| 27 | |
| 28 | private CostStoExCache stoExCache; |
| 29 | |
| 30 | private IRandomGenerator randomGenerator; |
| 31 | private IProbabilityFunctionFactory probFunctionFactory; |
| 32 | |
| 33 | private static CostUtil singletonInstance = null; |
| 34 | |
| 35 | public static CostUtil getInstance(){ |
| 36 | if (singletonInstance == null){ |
| 37 | singletonInstance = new CostUtil(); |
| 38 | } |
| 39 | return singletonInstance; |
| 40 | } |
| 41 | |
| 42 | private CostUtil(){ |
| 43 | this.randomGenerator = new SimuComDefaultRandomNumberGenerator(null); |
| 44 | probFunctionFactory = ProbabilityFunctionFactoryImpl.getInstance(); |
| 45 | probFunctionFactory.setRandomGenerator(this.randomGenerator); |
| 46 | this.stoExCache = new CostStoExCache(probFunctionFactory); |
| 47 | } |
| 48 | |
| 49 | public double getDoubleFromSpecification(String specification) { |
| 50 | // TODO Auto-generated method stub |
| 51 | if (StoExCache.singleton() == null) |
| 52 | { |
| 53 | StoExCache.initialiseStoExCache(probFunctionFactory); |
| 54 | } |
| 55 | Object rate = StackContext.evaluateStatic(specification); |
| 56 | // cannot use the following direct access to the solving visitor, as it also requires an initialised StoExCache. |
| 57 | //StoExCacheEntry entry = new StoExCacheEntry(specification,null); |
| 58 | //Object rate = new PCMStoExEvaluationVisitor(specification,new SimulatedStackframe<Object>(),VariableMode.EXCEPTION_ON_NOT_FOUND) |
| 59 | // .doSwitch(entry.getParsedExpression()); |
| 60 | return toDoubleOrZero(rate); |
| 61 | } |
| 62 | |
| 63 | private static double toDoubleOrZero(Object number) { |
| 64 | if (Double.class.isInstance(number)){ |
| 65 | return (Double)number; |
| 66 | } else if (Integer.class.isInstance(number)){ |
| 67 | return ((Integer)number).doubleValue(); |
| 68 | } |
| 69 | return 0.0; |
| 70 | } |
| 71 | |
| 72 | public double getOperatingCost( |
| 73 | VariableProcessingResourceCost varCost) { |
| 74 | double functionValue = solveFunctionExpression( |
| 75 | varCost, |
| 76 | varCost.getProcessingRateOperatingFunction()); |
| 77 | double cost = varCost.getFixedOperatingCost() + functionValue ; |
| 78 | return cost; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | private double solveFunctionExpression( |
| 83 | VariableProcessingResourceCost varCost, |
| 84 | ScalarFunction scalarFunction) { |
| 85 | if (scalarFunction == null || scalarFunction.getSpecification() == ""){ |
| 86 | return 0.0; |
| 87 | } |
| 88 | double processingRate = getProcessingRate(varCost); |
| 89 | int numberOfCores = getNumberOfReplicas(varCost); |
| 90 | String specification = scalarFunction.getSpecification(); |
| 91 | SimulatedStackframe<Object> stackframe = new SimulatedStackframe<Object>(); |
| 92 | stackframe.addValue(PROCESSING_RATE_VARIABLE, processingRate); |
| 93 | stackframe.addValue(NUMBER_OF_CORES_VARIABLE, numberOfCores); |
| 94 | |
| 95 | try { |
| 96 | //Term parsedExpression = scalarFunction.getTerm(); |
| 97 | //ExpressionInferTypeVisitor typeInferer = StoExCache.singleton().getEntry(specification).getTypeInferer(); |
| 98 | //typeInferer.getTypeAnnotation().put(arg0, arg1) |
| 99 | |
| 100 | StoExCacheEntry stoExEntry = this.stoExCache.getEntry(specification); |
| 101 | Expression parsedExpression = stoExEntry.getParsedExpression(); |
| 102 | |
| 103 | PCMStoExEvaluationVisitor visitor = new PCMStoExEvaluationVisitor(stoExEntry,stackframe,VariableMode.RETURN_DEFAULT_ON_NOT_FOUND,probFunctionFactory); |
| 104 | Object number = visitor.doSwitch(parsedExpression); |
| 105 | return toDoubleOrZero(number); |
| 106 | } catch (RuntimeException e){ |
| 107 | logger.warn("Error when evaluating processing rate cost function: "+e.getMessage()); |
| 108 | e.printStackTrace(); |
| 109 | return 0; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | |
| 114 | public double getInitialCost( |
| 115 | VariableProcessingResourceCost varCost) { |
| 116 | double functionValue = solveFunctionExpression( |
| 117 | varCost, |
| 118 | varCost.getProcessingRateInitialFunction()); |
| 119 | double cost = varCost.getFixedInitialCost() + functionValue ; |
| 120 | return cost; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * <!-- begin-user-doc --> |
| 125 | * <!-- end-user-doc --> |
| 126 | * @generated not |
| 127 | */ |
| 128 | private double getProcessingRate(VariableProcessingResourceCost varCost) { |
| 129 | //TODO: what about longs and shorts and stuff here? |
| 130 | if ( varCost.getProcessingresourcespecification() != null |
| 131 | && varCost.getProcessingresourcespecification().getProcessingRate_ProcessingResourceSpecification() != null |
| 132 | && varCost.getProcessingresourcespecification().getProcessingRate_ProcessingResourceSpecification().getSpecification() != null) { |
| 133 | return getDoubleFromSpecification(varCost.getProcessingresourcespecification().getProcessingRate_ProcessingResourceSpecification().getSpecification()); |
| 134 | } |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | private int getNumberOfReplicas(VariableProcessingResourceCost varCost){ |
| 139 | if (varCost.getProcessingresourcespecification() != null){ |
| 140 | return varCost.getProcessingresourcespecification().getNumberOfReplicas(); |
| 141 | } |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | public void resetCache(){ |
| 146 | this.stoExCache = new CostStoExCache(probFunctionFactory); |
| 147 | } |
| 148 | |
| 149 | } |
| 150 | |
| 151 | class CostStoExCache extends StoExCache { |
| 152 | |
| 153 | protected CostStoExCache(IProbabilityFunctionFactory factory){ |
| 154 | super(factory); |
| 155 | } |
| 156 | |
| 157 | } |