/// <summary> /// Creates the parameter value associationg according to actual parameters /// </summary> /// <param name="context">The interpretation context</param> /// <param name="callable">The callable</param> /// <param name="log">Indicates whether errors should be logged</param> /// <param name="explain"></param> /// <returns></returns> public Dictionary <Actual, IValue> AssignParameterValues(InterpretationContext context, ICallable callable, bool log, ExplanationPart explain) { // Compute the unnamed actual parameter values Dictionary <Actual, IValue> retVal = new Dictionary <Actual, IValue>(); if (callable.FormalParameters.Count == NamedActualParameters.Count + ActualParameters.Count) { int i = 0; foreach (Expression expression in ActualParameters) { Parameter parameter = (Parameter)callable.FormalParameters[i]; ExplanationPart subExplanation = ExplanationPart.CreateSubExplanation(explain, parameter); IValue val = expression.GetValue(context, subExplanation); if (val != null) { Actual actual = parameter.CreateActual(); val = val.RightSide(actual, false, false); retVal.Add(actual, val); } else { AddError("Cannot evaluate value for parameter " + i + " (" + expression + ") of function " + callable.Name, RuleChecksEnum.ExecutionFailed); throw new Exception("Evaluation of parameters failed: Cannot evaluate value for parameter " + i + " (" + expression + ") of function " + callable.Name); } ExplanationPart.SetNamable(subExplanation, val); i = i + 1; } foreach (KeyValuePair <Designator, Expression> pair in NamedActualParameters) { Parameter parameter = callable.GetFormalParameter(pair.Key.Image); ExplanationPart subExplanation = ExplanationPart.CreateSubExplanation(explain, parameter); IValue val = pair.Value.GetValue(context, subExplanation); if (val != null) { Actual actual = parameter.CreateActual(); val = val.RightSide(actual, false, false); actual.Value = val; retVal.Add(actual, val); } else { AddError("Cannot evaluate value for parameter " + pair.Key + " of function " + callable.Name, RuleChecksEnum.ExecutionFailed); throw new Exception("Evaluation of parameters failed: cannot evaluate value for parameter " + pair.Key + " of function " + callable.Name); } ExplanationPart.SetNamable(subExplanation, val); } } return(retVal); }
/// <summary> /// Provides the value associated to this Expression /// </summary> /// <param name="context">The context on which the value must be found</param> /// <param name="explain">The explanation to fill, if any</param> /// <returns></returns> protected internal override IValue GetValue(InterpretationContext context, ExplanationPart explain) { ExplanationPart subPart = ExplanationPart.CreateSubExplanation(explain, BoundVariable); BoundVariable.Value = BindingExpression.GetValue(context, explain); ExplanationPart.SetNamable(subPart, BoundVariable.Value); int token = context.LocalScope.PushContext(); context.LocalScope.SetVariable(BoundVariable); IValue retVal = Expression.GetValue(context, explain); context.LocalScope.PopContext(token); return(retVal); }
/// <summary> /// Provides the value associated to this Expression /// </summary> /// <param name="context">The context on which the value must be found</param> /// <param name="explain">The explanation to fill, if any</param> /// <returns></returns> protected internal override IValue GetValue(InterpretationContext context, ExplanationPart explain) { IValue retVal = null; ExplanationPart subExplanation = ExplanationPart.CreateSubExplanation(explain, this); try { if (Parameters.Count == 1) { int token = context.LocalScope.PushContext(); context.LocalScope.SetGraphParameter(Parameters[0]); Graph graph = CreateGraph(context, Parameters[0], subExplanation); context.LocalScope.PopContext(token); if (graph != null) { retVal = graph.Function; } } else if (Parameters.Count == 2) { int token = context.LocalScope.PushContext(); context.LocalScope.SetSurfaceParameters(Parameters[0], Parameters[1]); Surface surface = CreateSurface(context, Parameters[0], Parameters[1], subExplanation); context.LocalScope.PopContext(token); if (surface != null) { retVal = surface.Function; } } } catch (Exception) { /// TODO Ugly hack, because functions & function types are merged. /// This provides an empty function as the type of this retVal = GetExpressionType() as IValue; } finally { ExplanationPart.SetNamable(subExplanation, retVal); } return(retVal); }
/// <summary> /// Provides the value associated to this Expression /// </summary> /// <param name="context">The context on which the value must be found</param> /// <param name="explain">The explanation to fill, if any</param> /// <returns></returns> protected internal override IValue GetValue(InterpretationContext context, ExplanationPart explain) { ExplanationPart stabilizeExpressionExplanation = ExplanationPart.CreateSubExplanation(explain, this); IterationsList = new List <IValue>(); LastIteration.Value = InitialValue.GetValue(context, explain); int i = 0; bool stop = false; while (!stop) { i = i + 1; ExplanationPart iterationExplanation = ExplanationPart.CreateSubExplanation(stabilizeExpressionExplanation, "Iteration " + i); ExplanationPart iteratorValueExplanation = ExplanationPart.CreateSubExplanation(iterationExplanation, "Iteration expression value = "); int token = context.LocalScope.PushContext(); context.LocalScope.SetVariable(LastIteration); CurrentIteration.Value = Expression.GetValue(context, iteratorValueExplanation); ExplanationPart.SetNamable(iteratorValueExplanation, CurrentIteration.Value); ExplanationPart stopValueExplanation = ExplanationPart.CreateSubExplanation(iterationExplanation, "Stop expression value = "); context.LocalScope.SetVariable(CurrentIteration); BoolValue stopCondition = Condition.GetValue(context, stopValueExplanation) as BoolValue; ExplanationPart.SetNamable(stopValueExplanation, stopCondition); if (stopCondition != null) { stop = stopCondition.Val; } else { AddError("Cannot evaluate condition " + Condition, RuleChecksEnum.ExecutionFailed); stop = true; } if (!stop && IterationsList.Exists(x => x.LiteralName == CurrentIteration.Value.LiteralName)) { // Cycle found !!! IterationsList.Add(CurrentIteration.Value); string cycleReport = "Execution cycled: "; bool keepvalues = false; foreach (IValue value in IterationsList) { if (keepvalues) { cycleReport += ", " + value.LiteralName; } else if (value.LiteralName == CurrentIteration.Value.LiteralName) { keepvalues = true; cycleReport += value.LiteralName; } } ExplanationPart.CreateSubExplanation(stabilizeExpressionExplanation, cycleReport); CurrentIteration.Value = EfsSystem.Instance.EmptyValue; stop = true; } else { IterationsList.Add(CurrentIteration.Value); } context.LocalScope.PopContext(token); LastIteration.Value = CurrentIteration.Value; } ExplanationPart.SetNamable(stabilizeExpressionExplanation, CurrentIteration.Value); return(CurrentIteration.Value); }
/// <summary> /// Provides the value associated to this Expression /// </summary> /// <param name="context">The context on which the value must be found</param> /// <param name="explain">The explanation to fill, if any</param> /// <returns></returns> protected internal override IValue GetValue(InterpretationContext context, ExplanationPart explain) { IValue retVal = null; ExplanationPart subExplanation = ExplanationPart.CreateSubExplanation(explain, this); Function function = GetFunction(context, explain); if (function != null) { long start = Environment.TickCount; Dictionary <Actual, IValue> parameterValues = AssignParameterValues(context, function, true, subExplanation); List <Parameter> parameters = GetPlaceHolders(function, parameterValues); if (parameters == null) { retVal = function.Evaluate(context, parameterValues, subExplanation); if (retVal == null) { AddErrorAndExplain( "Call " + function.Name + " ( " + ParameterValues(parameterValues) + " ) returned nothing", subExplanation); } } else if (parameters.Count == 1) // graph { int token = context.LocalScope.PushContext(); context.LocalScope.SetGraphParameter(parameters[0]); Graph graph = function.CreateGraphForParameter(context, parameters[0], subExplanation); context.LocalScope.PopContext(token); if (graph != null) { retVal = graph.Function; } else { AddError("Cannot create graph on Call " + function.Name + " ( " + ParameterValues(parameterValues) + " )", RuleChecksEnum.ExecutionFailed); } } else // surface { Surface surface = function.CreateSurfaceForParameters(context, parameters[0], parameters[1], subExplanation); if (surface != null) { retVal = surface.Function; } else { AddError("Cannot create surface on Call " + function.Name + " ( " + ParameterValues(parameterValues) + " )", RuleChecksEnum.ExecutionFailed); } } long stop = Environment.TickCount; long span = (stop - start); function.ExecutionTimeInMilli += span; function.ExecutionCount += 1; ExplanationPart.SetNamable(subExplanation, retVal); } else { AddErrorAndExplain("Cannot find function for " + ToString(), subExplanation); } return(retVal); }