EMMA Coverage Report (generated Sun Feb 05 10:43:15 CET 2012)
[all classes][de.uka.ipd.sdq.pcm.cost.helper]

COVERAGE SUMMARY FOR SOURCE FILE [CostUtil.java]

nameclass, %method, %block, %line, %
CostUtil.java0%   (0/2)0%   (0/12)0%   (0/207)0%   (0/61)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CostStoExCache0%   (0/1)0%   (0/1)0%   (0/4)0%   (0/2)
CostStoExCache (IProbabilityFunctionFactory): void 0%   (0/1)0%   (0/4)0%   (0/2)
     
class CostUtil0%   (0/1)0%   (0/11)0%   (0/203)0%   (0/59)
<static initializer> 0%   (0/1)0%   (0/6)0%   (0/4)
CostUtil (): void 0%   (0/1)0%   (0/24)0%   (0/6)
getDoubleFromSpecification (String): double 0%   (0/1)0%   (0/11)0%   (0/4)
getInitialCost (VariableProcessingResourceCost): double 0%   (0/1)0%   (0/13)0%   (0/5)
getInstance (): CostUtil 0%   (0/1)0%   (0/8)0%   (0/3)
getNumberOfReplicas (VariableProcessingResourceCost): int 0%   (0/1)0%   (0/9)0%   (0/3)
getOperatingCost (VariableProcessingResourceCost): double 0%   (0/1)0%   (0/13)0%   (0/5)
getProcessingRate (VariableProcessingResourceCost): double 0%   (0/1)0%   (0/21)0%   (0/5)
resetCache (): void 0%   (0/1)0%   (0/8)0%   (0/2)
solveFunctionExpression (VariableProcessingResourceCost, ScalarFunction): double 0%   (0/1)0%   (0/72)0%   (0/17)
toDoubleOrZero (Object): double 0%   (0/1)0%   (0/18)0%   (0/5)

1package de.uka.ipd.sdq.pcm.cost.helper;
2 
3import org.apache.log4j.Logger;
4 
5import de.uka.ipd.sdq.pcm.cost.ScalarFunction;
6import de.uka.ipd.sdq.pcm.cost.VariableProcessingResourceCost;
7import de.uka.ipd.sdq.probfunction.math.IProbabilityFunctionFactory;
8import de.uka.ipd.sdq.probfunction.math.IRandomGenerator;
9import de.uka.ipd.sdq.probfunction.math.impl.ProbabilityFunctionFactoryImpl;
10import de.uka.ipd.sdq.simucomframework.SimuComDefaultRandomNumberGenerator;
11import de.uka.ipd.sdq.simucomframework.variables.StackContext;
12import de.uka.ipd.sdq.simucomframework.variables.cache.StoExCache;
13import de.uka.ipd.sdq.simucomframework.variables.cache.StoExCacheEntry;
14import de.uka.ipd.sdq.simucomframework.variables.stackframe.SimulatedStackframe;
15import de.uka.ipd.sdq.simucomframework.variables.stoexvisitor.PCMStoExEvaluationVisitor;
16import de.uka.ipd.sdq.simucomframework.variables.stoexvisitor.VariableMode;
17import de.uka.ipd.sdq.stoex.Expression;
18 
19public 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 
151class CostStoExCache extends StoExCache {
152        
153        protected CostStoExCache(IProbabilityFunctionFactory factory){
154                super(factory);
155        }
156        
157}

[all classes][de.uka.ipd.sdq.pcm.cost.helper]
EMMA 2.0.9414 (unsupported private build) (C) Vladimir Roubtsov