| 1 | package de.uka.ipd.sdq.simucomframework.variables; |
| 2 | |
| 3 | import java.io.Serializable; |
| 4 | import java.util.Collection; |
| 5 | import java.util.Map.Entry; |
| 6 | |
| 7 | import org.apache.log4j.Logger; |
| 8 | |
| 9 | import de.uka.ipd.sdq.simucomframework.variables.cache.StoExCache; |
| 10 | import de.uka.ipd.sdq.simucomframework.variables.cache.StoExCacheEntry; |
| 11 | import de.uka.ipd.sdq.simucomframework.variables.exceptions.StochasticExpressionEvaluationFailedException; |
| 12 | import de.uka.ipd.sdq.simucomframework.variables.stackframe.SimulatedStack; |
| 13 | import de.uka.ipd.sdq.simucomframework.variables.stackframe.SimulatedStackframe; |
| 14 | import de.uka.ipd.sdq.simucomframework.variables.stoexvisitor.PCMStoExEvaluationVisitor; |
| 15 | import de.uka.ipd.sdq.simucomframework.variables.stoexvisitor.VariableMode; |
| 16 | |
| 17 | /** |
| 18 | * StackContext is the parent of all contexts. Simulated threads carry their |
| 19 | * context with them containing their stack, their resource environment, etc. |
| 20 | * This is the stack part of the story |
| 21 | * @author Steffen Becker |
| 22 | * |
| 23 | */ |
| 24 | public class StackContext implements Serializable { |
| 25 | |
| 26 | /** |
| 27 | * Serial ID |
| 28 | */ |
| 29 | private static final long serialVersionUID = 2031992603442903211L; |
| 30 | |
| 31 | /** |
| 32 | * Mode under which to evaluate StoEx. |
| 33 | */ |
| 34 | private VariableMode mode = VariableMode.EXCEPTION_ON_NOT_FOUND; |
| 35 | |
| 36 | private static Logger logger = |
| 37 | Logger.getLogger(StackContext.class.getName()); |
| 38 | |
| 39 | public StackContext() {} |
| 40 | |
| 41 | /** |
| 42 | * The stack contained in this object. A Stack contains StackFrames |
| 43 | */ |
| 44 | protected SimulatedStack<Object> stack = new SimulatedStack<Object>(); |
| 45 | |
| 46 | /** |
| 47 | * Parse and evaluate the given Stoex |
| 48 | * @param string Stoex to evaluate |
| 49 | * @param expectedType Type expected by the evaluation. It tries |
| 50 | * to case the type when needed. If the type cannot be case, an evaluation |
| 51 | * exception is thrown |
| 52 | * @return The value of the StoEx evaluation |
| 53 | */ |
| 54 | public <T> T evaluate(String string, Class<T> expectedType) { |
| 55 | return (T) StackContext.evaluateStatic(string,expectedType,stack.currentStackFrame(),mode); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Parse and evaluate the given Stoex using the current stackframe on |
| 60 | * top of the stack |
| 61 | * @param string Stoex to evaluate |
| 62 | * @return The value of the StoEx evaluation |
| 63 | */ |
| 64 | public Object evaluate(String string) { |
| 65 | return evaluateStatic(string,stack.currentStackFrame(),mode); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Parse and evaluate the given Stoex |
| 70 | * @param string Stoex to evaluate |
| 71 | * @param currentFrame The Stackframe under which the evaluation is performed |
| 72 | * @return The value of the StoEx evaluation |
| 73 | */ |
| 74 | public Object evaluate(String stoex, SimulatedStackframe<Object> currentFrame) { |
| 75 | logger.debug("About to evaluate "+stoex); |
| 76 | StoExCacheEntry cacheEntry = StoExCache.singleton().getEntry(stoex); |
| 77 | Object result = null; |
| 78 | try { |
| 79 | result = new PCMStoExEvaluationVisitor(cacheEntry,currentFrame,mode,StoExCache.singleton().getProbabilityFunctionFactory()) |
| 80 | .doSwitch(cacheEntry.getParsedExpression()); |
| 81 | } catch (Exception ex) { |
| 82 | throw new StochasticExpressionEvaluationFailedException("Evaluation of expression "+stoex+" failed.",ex); |
| 83 | } |
| 84 | logger.debug("Result "+result); |
| 85 | return result; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Parse and evaluate the given Stoex |
| 90 | * @param string Stoex to evaluate |
| 91 | * @return The value of the StoEx evaluation |
| 92 | */ |
| 93 | public static Object evaluateStatic(String stoex) { |
| 94 | return evaluateStatic(stoex, new SimulatedStackframe<Object>()); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Parse and evaluate the given Stoex |
| 99 | * @param string Stoex to evaluate |
| 100 | * @param currentFrame The Stackframe under which the evaluation is performed |
| 101 | * @return The value of the StoEx evaluation |
| 102 | */ |
| 103 | public static Object evaluateStatic(String stoex, SimulatedStackframe<Object> currentFrame) { |
| 104 | logger.debug("About to evaluate "+stoex); |
| 105 | StoExCacheEntry cacheEntry = StoExCache.singleton().getEntry(stoex); |
| 106 | Object result = null; |
| 107 | try { |
| 108 | result = new PCMStoExEvaluationVisitor(cacheEntry,currentFrame,VariableMode.EXCEPTION_ON_NOT_FOUND,StoExCache.singleton().getProbabilityFunctionFactory()) |
| 109 | .doSwitch(cacheEntry.getParsedExpression()); |
| 110 | } catch (Exception ex) { |
| 111 | throw new StochasticExpressionEvaluationFailedException("Evaluation of expression "+stoex+" failed.",ex); |
| 112 | } |
| 113 | logger.debug("Result "+result); |
| 114 | return result; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Parse and evaluate the given Stoex |
| 119 | * @param string Stoex to evaluate |
| 120 | * @param currentFrame The Stackframe under which the evaluation is performed |
| 121 | * @param mode Evaluation mode to use |
| 122 | * @return The value of the StoEx evaluation |
| 123 | */ |
| 124 | public static Object evaluateStatic(String stoex, SimulatedStackframe<Object> currentFrame, VariableMode mode) { |
| 125 | logger.debug("About to evaluate "+stoex); |
| 126 | StoExCacheEntry cacheEntry = StoExCache.singleton().getEntry(stoex); |
| 127 | Object result = null; |
| 128 | try { |
| 129 | result = new PCMStoExEvaluationVisitor(cacheEntry,currentFrame,mode,StoExCache.singleton().getProbabilityFunctionFactory()) |
| 130 | .doSwitch(cacheEntry.getParsedExpression()); |
| 131 | } catch (Exception ex) { |
| 132 | throw new StochasticExpressionEvaluationFailedException("Evaluation of expression "+stoex+" failed.",ex); |
| 133 | } |
| 134 | logger.debug("Result "+result); |
| 135 | return result; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Parse and evaluate the given Stoex |
| 140 | * @param string Stoex to evaluate |
| 141 | * @param expectedType Type expected by the evaluation. It tries |
| 142 | * to case the type when needed. If the type cannot be case, an evaluation |
| 143 | * exception is thrown |
| 144 | * @return The value of the StoEx evaluation |
| 145 | */ |
| 146 | public static <T> T evaluateStatic(String string, Class<T> expectedType) { |
| 147 | return evaluateStatic(string,expectedType,new SimulatedStackframe<Object>(),VariableMode.EXCEPTION_ON_NOT_FOUND); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Parse and evaluate the given Stoex |
| 152 | * @param string Stoex to evaluate |
| 153 | * @param expectedType Type expected by the evaluation. It tries |
| 154 | * to case the type when needed. If the type cannot be case, an evaluation |
| 155 | * exception is thrown |
| 156 | * @param frame Stackframe to be used in the evaluation |
| 157 | * @return The value of the StoEx evaluation |
| 158 | */ |
| 159 | public static <T> T evaluateStatic(String string, Class<T> expectedType, SimulatedStackframe<Object> frame) { |
| 160 | return evaluateStatic(string,expectedType,frame,VariableMode.EXCEPTION_ON_NOT_FOUND); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Parse and evaluate the given Stoex |
| 165 | * @param string Stoex to evaluate |
| 166 | * @param expectedType Type expected by the evaluation. It tries |
| 167 | * to case the type when needed. If the type cannot be case, an evaluation |
| 168 | * exception is thrown |
| 169 | * @param frame Stackframe to be used in the evaluation |
| 170 | * @param mode Evaluation mode to use |
| 171 | * @return The value of the StoEx evaluation |
| 172 | */ |
| 173 | @SuppressWarnings("unchecked") |
| 174 | public static <T> T evaluateStatic(String string, Class<T> expectedType, SimulatedStackframe<Object> frame,VariableMode mode) { |
| 175 | Object result = evaluateStatic(string,frame,mode); |
| 176 | if (expectedType.isInstance(result)) |
| 177 | return (T) result; |
| 178 | /* Support default (auto-)conversion hierarchy |
| 179 | * char/byte -> short -> int -> long -> float -> double |
| 180 | */ |
| 181 | if (expectedType == Byte.class) { |
| 182 | if (result.getClass() == Character.class) { |
| 183 | return (T) Byte.valueOf((byte) ((Character)result).charValue()); |
| 184 | } |
| 185 | } |
| 186 | if (expectedType == Character.class) { |
| 187 | if (result.getClass() == Byte.class) { |
| 188 | return (T) Character.valueOf((char) ((Byte)result).byteValue()); |
| 189 | } |
| 190 | } |
| 191 | if (expectedType == Short.class) { |
| 192 | if (result.getClass() == Byte.class) { |
| 193 | return (T) Short.valueOf(((Byte)result).shortValue()); |
| 194 | } |
| 195 | if (result.getClass() == Character.class) { |
| 196 | return (T) Short.valueOf(Byte.valueOf((byte)((Character)result).charValue()).shortValue()); |
| 197 | } |
| 198 | } |
| 199 | if (expectedType == Integer.class) { |
| 200 | if (result.getClass() == Byte.class) { |
| 201 | return (T) Integer.valueOf(((Byte)result).intValue()); |
| 202 | } |
| 203 | if (result.getClass() == Short.class) { |
| 204 | return (T) Integer.valueOf(((Short)result).intValue()); |
| 205 | } |
| 206 | if (result.getClass() == Character.class) { |
| 207 | return (T) Integer.valueOf(Byte.valueOf((byte)((Character)result).charValue()).intValue()); |
| 208 | } |
| 209 | } |
| 210 | if (expectedType == Long.class) { |
| 211 | if (result.getClass() == Byte.class) { |
| 212 | return (T) Long.valueOf(((Byte)result).longValue()); |
| 213 | } |
| 214 | if (result.getClass() == Short.class) { |
| 215 | return (T) Long.valueOf(((Short)result).longValue()); |
| 216 | } |
| 217 | if (result.getClass() == Integer.class) { |
| 218 | return (T) Long.valueOf(((Integer)result).longValue()); |
| 219 | } |
| 220 | if (result.getClass() == Character.class) { |
| 221 | return (T) Long.valueOf(Byte.valueOf((byte)((Character)result).charValue()).longValue()); |
| 222 | } |
| 223 | } |
| 224 | if (expectedType == Float.class) { |
| 225 | if (result.getClass() == Byte.class) { |
| 226 | return (T) Float.valueOf(((Byte)result).floatValue()); |
| 227 | } |
| 228 | if (result.getClass() == Short.class) { |
| 229 | return (T) Float.valueOf(((Short)result).floatValue()); |
| 230 | } |
| 231 | if (result.getClass() == Integer.class) { |
| 232 | return (T) Float.valueOf(((Integer)result).floatValue()); |
| 233 | } |
| 234 | if (result.getClass() == Long.class) { |
| 235 | return (T) Float.valueOf(((Long)result).floatValue()); |
| 236 | } |
| 237 | if (result.getClass() == Character.class) { |
| 238 | return (T) Float.valueOf(Byte.valueOf((byte)((Character)result).charValue()).floatValue()); |
| 239 | } |
| 240 | } |
| 241 | if (expectedType == Double.class) { |
| 242 | if (result.getClass() == Byte.class) { |
| 243 | return (T) Double.valueOf(((Byte)result).doubleValue()); |
| 244 | } |
| 245 | if (result.getClass() == Short.class) { |
| 246 | return (T) Double.valueOf(((Short)result).doubleValue()); |
| 247 | } |
| 248 | if (result.getClass() == Integer.class) { |
| 249 | return (T) Double.valueOf(((Integer)result).doubleValue()); |
| 250 | } |
| 251 | if (result.getClass() == Long.class) { |
| 252 | return (T) Double.valueOf(((Long)result).doubleValue()); |
| 253 | } |
| 254 | if (result.getClass() == Float.class) { |
| 255 | return (T) Double.valueOf(((Float)result).doubleValue()); |
| 256 | } |
| 257 | if (result.getClass() == Character.class) { |
| 258 | return (T) Double.valueOf(Byte.valueOf((byte)((Character)result).charValue()).doubleValue()); |
| 259 | } |
| 260 | } |
| 261 | UnsupportedOperationException ex = new UnsupportedOperationException("Evaluation result is of type "+result.getClass().getCanonicalName()+ |
| 262 | " but expected was "+expectedType.getCanonicalName()+ " and no conversion was available..."); |
| 263 | logger.error("Evaluation of an expression resulted in wrong type!",ex); |
| 264 | throw ex; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Evaluate all EvaluationProxies starting with "variable name" and store the |
| 269 | * results in the given stack frame |
| 270 | * @param frame The frame which stores the evaluated proxy results |
| 271 | * @param variablename |
| 272 | */ |
| 273 | public void evaluateInner(SimulatedStackframe<Object> frame, String variablename) { |
| 274 | SimulatedStackframe<Object> topmostFrame = this.getStack().currentStackFrame(); |
| 275 | for(Entry<String,Object> e : (Collection<Entry<String,Object>>)topmostFrame.getContents()) { |
| 276 | if (e.getKey().startsWith(variablename)) { |
| 277 | if (e.getValue() instanceof EvaluationProxy) { |
| 278 | EvaluationProxy proxy = (EvaluationProxy) e.getValue(); |
| 279 | Object result = StackContext.evaluateStatic(proxy.getStoEx(), proxy.getStackFrame(),mode); |
| 280 | frame.addValue(e.getKey(),result); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * @return The current stack |
| 288 | */ |
| 289 | public SimulatedStack<Object> getStack(){ |
| 290 | return stack; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * @return Current evaluation mode |
| 295 | */ |
| 296 | public VariableMode getEvaluationMode() { |
| 297 | return mode; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Set the evaluation method |
| 302 | * @param mode The new evaluation method to set |
| 303 | */ |
| 304 | public void setEvaluationMode(VariableMode mode) { |
| 305 | this.mode = mode; |
| 306 | } |
| 307 | |
| 308 | } |