/// <summary> /// Creates the graph for the unbound parameters provided /// </summary> /// <param name="context"></param> /// <param name="expression"></param> /// <param name="function"></param> /// <param name="unbound"></param> /// <returns></returns> private Graph createGraphForUnbound(InterpretationContext context, Expression expression, Function function, List <Parameter> unbound) { Graph retVal = null; if (unbound.Count == 0) { if (function != null && function.FormalParameters.Count > 0) { retVal = function.createGraph(context, (Parameter)function.FormalParameters[0]); } else { retVal = Graph.createGraph(expression.GetValue(context), null); } } else { if (function == null) { retVal = expression.createGraph(context, unbound[0]); } else { retVal = function.createGraph(context, unbound[0]); } } return(retVal); }
/// <summary> /// Creates the result as a surface /// </summary> /// <param name="context"></param> /// <param name="leftFunction"></param> /// <param name="unboundLeft"></param> /// <param name="rightFunction"></param> /// <param name="unboundRight"></param> /// <returns></returns> private ICallable createGraphResult(InterpretationContext context, Functions.Function leftFunction, List <Parameter> unboundLeft, Functions.Function rightFunction, List <Parameter> unboundRight) { ICallable retVal = null; Functions.Graph leftGraph = createGraphForUnbound(context, Left, leftFunction, unboundLeft); if (leftGraph != null) { Functions.Graph rightGraph = createGraphForUnbound(context, Right, rightFunction, unboundRight); if (rightGraph != null) { retVal = combineGraph(leftGraph, rightGraph).Function; } else { AddError("Cannot create graph for " + Right); } } else { AddError("Cannot create graph for " + Left); } return(retVal); }
/// <summary> /// Provides the value associated to this Expression /// </summary> /// <param name="context">The context on which the value must be found</param> /// <returns></returns> public override Values.IValue GetValue(InterpretationContext context) { LastIteration.Value = InitialValue.GetValue(context); bool stop = false; while (!stop) { int token = context.LocalScope.PushContext(); context.LocalScope.setVariable(LastIteration); CurrentIteration.Value = Expression.GetValue(context); context.LocalScope.setVariable(CurrentIteration); Values.BoolValue stopCondition = Condition.GetValue(context) as Values.BoolValue; if (stopCondition != null) { stop = stopCondition.Val; } else { AddError("Cannot evaluate condition " + Condition.ToString()); stop = true; } context.LocalScope.PopContext(token); LastIteration.Value = CurrentIteration.Value; } return(CurrentIteration.Value); }
/// <summary> /// Provides the value of the function /// </summary> /// <param name="context"></param> /// <param name="actuals">the actual parameters values</param> /// <param name="explain"></param> /// <returns>The value for the function application</returns> public override IValue Evaluate(InterpretationContext context, Dictionary<Actual, IValue> actuals, ExplanationPart explain) { IValue retVal = null; int token = context.LocalScope.PushContext(); AssignParameters(context, actuals); ListValue value = context.FindOnStack(Collection) as ListValue; if (value != null) { Collection collectionType = value.Type as Collection; if (collectionType != null && collectionType.Type != null) { Type elementType = collectionType.Type; if (value.Val.Count >= collectionType.getMaxSize()) { AddError("Cannot allocate element in list : list full"); } else { retVal = elementType.DefaultValue; value.Val.Add(retVal); } } } context.LocalScope.PopContext(token); return retVal; }
/// <summary> /// Provides all the steps used to get the value of the expression /// </summary> /// <returns></returns> public ExplanationPart Explain() { ExplanationPart retVal = new ExplanationPart(Root); currentExplanation = retVal; try { explain = true; InterpretationContext context = new InterpretationContext(); Values.IValue value = GetValue(context); if (value != null) { retVal.Message = ToString() + " = " + explainNamable(value); } else { retVal.Message = "Cannot evaluate value for " + ToString(); } } finally { explain = false; } return(retVal); }
/// <summary> /// Constructor /// </summary> /// <param name="context"></param> /// <param name="statement"></param> /// <param name="variable"></param> /// <param name="explanation"></param> public InsertInListChange(InterpretationContext context, InsertStatement statement, IVariable variable, ExplanationPart explanation) : base(variable, null, null) { Context = context; Statement = statement; Explanation = explanation; }
/// <summary> /// Provides the callable that is called by this expression /// </summary> /// <param name="context"></param> /// <param name="explain"></param> /// <returns></returns> public override ICallable GetCalled(InterpretationContext context, ExplanationPart explain) { ICallable retVal; Call calledFunction = Called as Call; if (calledFunction != null) { retVal = Called.GetValue(context, explain) as ICallable; } else { retVal = Called.GetCalled(context, explain); if (retVal == null) { Type type = Called.GetExpressionType(); if (type != null) { retVal = type.CastFunction; } if (retVal == null) { retVal = Called.GetValue(context, explain) as ICallable; } } } return(retVal); }
/// <summary> /// Constructor /// </summary> /// <param name="other">Copies the other interpretation context contents</param> public InterpretationContext(InterpretationContext other) { LocalScope = other.LocalScope; Instance = other.Instance; UseDefaultValue = other.UseDefaultValue; HasSideEffects = other.HasSideEffects; }
/// <summary> /// Creates the graph associated to this expression, when the given parameter ranges over the X axis /// </summary> /// <param name="context">The interpretation context</param> /// <param name="parameter">The parameters of *the enclosing function* for which the graph should be created</param> /// <returns></returns> public override Functions.Graph createGraph(InterpretationContext context, Parameter parameter) { Functions.Graph retVal = base.createGraph(context, parameter); if (Term != null) { retVal = Functions.Graph.createGraph(GetValue(context), parameter); } else if (Expression != null) { if (UnaryOp == null) { retVal = Expression.createGraph(context, parameter); } else if (UnaryOp == MINUS) { retVal = Expression.createGraph(context, parameter); retVal.Negate(); } else { throw new Exception("Cannot create graph where NOT operator is defined"); } } return(retVal); }
/// <summary> /// Constructor /// </summary> /// <param name="other">Copies the other interpretation context contents</param> /// <param name="instance">The evaluation instance</param> public InterpretationContext(InterpretationContext other, INamable instance) { LocalScope = other.LocalScope; Instance = instance; UseDefaultValue = true; HasSideEffects = other.HasSideEffects; }
/// <summary> /// Provides the surface of this function if it has been statically defined /// </summary> /// <param name="context">the context used to create the surface</param> /// <param name="xParam">The X axis of this surface</param> /// <param name="yParam">The Y axis of this surface</param> /// <param name="explain"></param> /// <returns>The surface which corresponds to this expression</returns> public override Surface CreateSurface(InterpretationContext context, Parameter xParam, Parameter yParam, ExplanationPart explain) { Surface retVal = base.CreateSurface(context, xParam, yParam, explain); if (Term != null) { retVal = Surface.createSurface(xParam, yParam, GetValue(context, explain), explain); } else if (Expression != null) { if (UnaryOp == null) { retVal = Expression.CreateSurface(context, xParam, yParam, explain); } else { if (UnaryOp == Minus) { retVal = Expression.CreateSurface(context, xParam, yParam, explain); retVal.Negate(); } else { AddError("Cannot create surface with unary op " + UnaryOp, RuleChecksEnum.ExecutionFailed); } } } retVal.XParameter = xParam; retVal.YParameter = yParam; return(retVal); }
/// <summary> /// Provides the value associated to this Expression /// </summary> /// <param name="instance">The instance on which the value is computed</param> /// <param name="localScope">The local scope used to compute the value of this expression</param> /// <param name="globalFind">Indicates that the search should be performed globally</param> /// <returns></returns> public override Values.IValue GetValue(InterpretationContext context) { Values.StructureValue retVal = null; Types.Structure structureType = Structure.GetExpressionType() as Types.Structure; if (structureType != null) { retVal = new Values.StructureValue(structureType, Root); foreach (KeyValuePair <string, Expression> pair in Associations) { Values.IValue val = pair.Value.GetValue(new InterpretationContext(context)); Variables.Variable var = (Variables.Variable)Generated.acceptor.getFactory().createVariable(); var.Name = pair.Key; var.Value = val; var.Enclosing = retVal; retVal.set(var); } } else { AddError("Cannot determine structure type for " + ToString()); } return(retVal); }
/// <summary> /// Provides the value associated to this Expression /// </summary> /// <param name="context">The context on which the value must be found</param> /// <returns></returns> public override Values.IValue GetValue(InterpretationContext context) { INamable retVal = Ref as Values.IValue; if (retVal == null) { InterpretationContext ctxt = new InterpretationContext(context); for (int i = 0; i < Arguments.Count; i++) { if (retVal != null) { ctxt.Instance = retVal; } retVal = Arguments[i].GetValue(ctxt); if (retVal == EFSSystem.EmptyValue) { break; } } } if (retVal == null) { AddError(ToString() + " does not refer to a value"); } return(retVal as Values.IValue); }
/// <summary> /// Provides the value of the prefix of the expression /// </summary> /// <param name="context">The context on which the value must be found</param> /// <param name="elementCount">The number of elements to consider</param> /// <returns></returns> public INamable GetPrefixValue(InterpretationContext context, int elementCount) { INamable retVal = null; InterpretationContext ctxt = new InterpretationContext(context); for (int i = 0; i < elementCount; i++) { if (retVal != null) { ctxt.Instance = retVal; } retVal = Arguments[i].GetValue(ctxt); if (retVal == null) { retVal = Arguments[i].Ref; } if (retVal == EFSSystem.EmptyValue) { break; } } if (retVal == null) { AddError(ToString() + " prefix does not refer to a value"); } return(retVal); }
/// <summary> /// Provides the callable that is called by this expression /// </summary> /// <param name="namable"></param> /// <returns></returns> public override ICallable getCalled(InterpretationContext context) { ICallable retVal = null; Call calledFunction = Called as Call; if (calledFunction != null) { retVal = Called.GetValue(context) as ICallable; } else { retVal = Called.getCalled(context); if (retVal == null) { Types.Range range = Called.GetExpressionType() as Types.Range; if (range != null) { retVal = range.CastFunction; } if (retVal == null) { retVal = Called.GetValue(context) as ICallable; } } } return(retVal); }
/// <summary> /// Provides the value of the prefix of the expression /// </summary> /// <param name="context">The context on which the value must be found</param> /// <param name="elementCount">The number of elements to consider</param> /// <param name="explain"></param> /// <returns></returns> public INamable GetPrefixValue(InterpretationContext context, int elementCount, ExplanationPart explain) { INamable retVal = null; InterpretationContext ctxt = new InterpretationContext(context); for (int i = 0; i < elementCount; i++) { if (retVal != null) { ctxt.Instance = retVal; } retVal = Arguments[i].GetValue(ctxt, explain); if (retVal == null) { retVal = Arguments[i].Ref; } if (retVal == EfsSystem.Instance.EmptyValue) { break; } } if (retVal == null) { AddError(ToString() + " prefix does not refer to a value", RuleChecksEnum.ExecutionFailed); } return(retVal); }
/// <summary> /// Creates the graph associated to this expression, when the given parameter ranges over the X axis /// </summary> /// <param name="context">The interpretation context</param> /// <param name="parameter">The parameters of *the enclosing function* for which the graph should be created</param> /// <param name="explain"></param> /// <returns></returns> public override Graph CreateGraph(InterpretationContext context, Parameter parameter, ExplanationPart explain) { Graph retVal = base.CreateGraph(context, parameter, explain); if (Term != null) { retVal = Graph.createGraph(GetValue(context, explain), parameter, explain); } else if (Expression != null) { if (UnaryOp == null) { retVal = Expression.CreateGraph(context, parameter, explain); } else if (UnaryOp == Minus) { retVal = Expression.CreateGraph(context, parameter, explain); retVal.Negate(); } else { throw new Exception("Cannot create graph where NOT operator is defined"); } } return(retVal); }
/// <summary> /// Provides the variable designated by this designator according to the interpretation context /// </summary> /// <param name="context"></param> /// <returns></returns> public IVariable GetVariable(InterpretationContext context) { INamable reference = GetReference(context); IVariable retVal = reference as IVariable; 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; if (Term != null) { retVal = Term.GetValue(context, explain); } else { if (Not.Equals(UnaryOp)) { BoolValue b = Expression.GetValue(context, explain) as BoolValue; if (b != null) { if (b.Val) { retVal = EfsSystem.Instance.BoolType.False; } else { retVal = EfsSystem.Instance.BoolType.True; } } else { AddError("Expression " + Expression + " does not evaluate to boolean", RuleChecksEnum.SemanticAnalysisError); } } else if (Minus.Equals(UnaryOp)) { IValue val = Expression.GetValue(context, explain); IntValue intValue = val as IntValue; if (intValue != null) { retVal = new IntValue(intValue.Type, -intValue.Val); } else { DoubleValue doubleValue = val as DoubleValue; if (doubleValue != null) { retVal = new DoubleValue(doubleValue.Type, -doubleValue.Val); } } if (retVal == null) { AddError("Cannot negate value for " + Expression, RuleChecksEnum.SemanticAnalysisError); } } else { retVal = Expression.GetValue(context, explain); } } return(retVal); }
/// <summary> /// Creates the graph associated to this expression, when the given parameter ranges over the X axis /// </summary> /// <param name="context">The interpretation context</param> /// <param name="parameter">The parameters of *the enclosing function* for which the graph should be created</param> /// <returns></returns> public override Functions.Graph createGraph(InterpretationContext context, Parameter parameter) { Functions.Graph retVal = base.createGraph(context, parameter); Functions.PredefinedFunctions.Cast cast = Called.Ref as Functions.PredefinedFunctions.Cast; if (cast != null) { // In case of cast, just take the graph of the enclosed expression Parameter param = (Parameter)cast.FormalParameters[0]; retVal = cast.createGraphForParameter(context, param); } Function calledFunction = Called.Ref as Function; Dictionary <Parameter, Expression> parameterAssociation = null; if (calledFunction == null) { calledFunction = Called.GetValue(context) as Function; parameterAssociation = createParameterAssociation(calledFunction); } else { parameterAssociation = ParameterAssociation; } Parameter Xaxis = null; foreach (KeyValuePair <Parameter, Expression> pair in parameterAssociation) { if (pair.Value.Ref == parameter) { if (Xaxis == null) { Xaxis = pair.Key; } else { Root.AddError("Cannot evaluate graph for function call " + ToString() + " which has more than 1 parameter used as X axis"); Xaxis = null; break; } } } int token = context.LocalScope.PushContext(); calledFunction.AssignParameters(context, AssignParameterValues(context, calledFunction, false)); if (Xaxis != null) { retVal = calledFunction.createGraphForParameter(context, Xaxis); } else { retVal = Function.createGraphForValue(GetValue(context)); } 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> /// <returns></returns> public override Values.IValue GetValue(InterpretationContext context) { Values.IValue retVal = null; if (Term != null) { retVal = Term.GetValue(context); } else { if (NOT.CompareTo(UnaryOp) == 0) { Values.BoolValue b = Expression.GetValue(context) as Values.BoolValue; if (b != null) { if (b.Val) { retVal = EFSSystem.BoolType.False; } else { retVal = EFSSystem.BoolType.True; } } else { AddError("Expression " + Expression.ToString() + " does not evaluate to boolean"); } } else if (MINUS.CompareTo(UnaryOp) == 0) { Values.IValue val = Expression.GetValue(context); Values.IntValue intValue = val as Values.IntValue; if (intValue != null) { retVal = new Values.IntValue(intValue.Type, -intValue.Val); } else { Values.DoubleValue doubleValue = val as Values.DoubleValue; if (doubleValue != null) { retVal = new Values.DoubleValue(doubleValue.Type, -doubleValue.Val); } } if (retVal == null) { AddError("Cannot negate value for " + Expression.ToString()); } } else { retVal = Expression.GetValue(context); } } return(retVal); }
/// <summary> /// Provides the surface of this function if it has been statically defined /// </summary> /// <param name="context">the context used to create the surface</param> /// <param name="xParam">The X axis of this surface</param> /// <param name="yParam">The Y axis of this surface</param> /// <param name="explain"></param> /// <returns>The surface which corresponds to this expression</returns> public override Surface CreateSurface(InterpretationContext context, Parameter xParam, Parameter yParam, ExplanationPart explain) { Surface retVal = base.CreateSurface(context, xParam, yParam, explain); Function function = GetFunction(context, explain); Cast cast = function as Cast; if (cast != null) { // In case of cast, just take the surface of the enclosed expression Expression actual = ActualParameters[0]; retVal = actual.CreateSurface(context, xParam, yParam, explain); } else { if (function == null) { function = Called.GetCalled(context, explain) as Function; } if (function != null) { Parameter xAxis; Parameter yAxis; SelectXandYAxis(xParam, yParam, function, out xAxis, out yAxis); if (xAxis != null || yAxis != null) { int token = context.LocalScope.PushContext(); function.AssignParameters(context, AssignParameterValues(context, function, true, explain)); retVal = function.CreateSurfaceForParameters(context, xAxis, yAxis, explain); context.LocalScope.PopContext(token); } else { IValue value = GetValue(context, explain); if (value != null) { retVal = Surface.createSurface(value, xParam, yParam); } else { throw new Exception("Cannot create surface for expression"); } } } else { AddError("Cannot determine called function", RuleChecksEnum.SemanticAnalysisError); } } retVal.XParameter = xParam; retVal.YParameter = yParam; return(retVal); }
public Variables.IVariable GetVariable(InterpretationContext context) { Variables.IVariable retVal = null; INamable reference = getReference(context); retVal = reference as Variables.IVariable; return(retVal); }
/// <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 callable that is called by this expression /// </summary> /// <param name="context"></param> /// <param name="explain"></param> /// <returns></returns> public override ICallable GetCalled(InterpretationContext context, ExplanationPart explain) { ICallable retVal = Called; if (retVal == null) { AddError("Cannot evaluate call to " + ToString(), RuleChecksEnum.ExecutionFailed); } return(retVal); }
/// <summary> /// Provides the callable that is called by this expression /// </summary> /// <param name="namable"></param> /// <returns></returns> public override ICallable getCalled(InterpretationContext context) { ICallable retVal = Called; if (retVal == null) { AddError("Cannot evaluate call to " + ToString()); } return(retVal); }
/// <summary> /// Creates the graph associated to this expression, when the given parameter ranges over the X axis /// </summary> /// <param name="context">The interpretation context</param> /// <param name="parameter">The parameters of *the enclosing function* for which the graph should be created</param> /// <param name="explain"></param> /// <returns></returns> public override Graph CreateGraph(InterpretationContext context, Parameter parameter, ExplanationPart explain) { Graph retVal = Graph.createGraph(GetValue(context, explain), parameter, explain); if (retVal == null) { throw new Exception("Cannot create graph for " + ToString()); } return(retVal); }
/// <summary> /// Provides the variable referenced by this expression, if any /// </summary> /// <param name="context">The context on which the variable must be found</param> /// <returns></returns> public IVariable GetVariable(InterpretationContext context) { IVariable retVal = null; if (Designator != null) { retVal = Designator.GetVariable(context); } return(retVal); }
/// <summary> /// Provides the element called by this term, if any /// </summary> /// <param name="context">The context on which the variable must be found</param> /// <param name="explain"></param> /// <returns></returns> public ICallable GetCalled(InterpretationContext context, ExplanationPart explain) { ICallable retVal = null; if (Designator != null) { retVal = Designator.GetCalled(context); } return(retVal); }
/// <summary> /// Provides the element called by this term, if any /// </summary> /// <param name="context">The context on which the variable must be found</param> /// <returns></returns> public ICallable getCalled(InterpretationContext context) { ICallable retVal = null; if (Designator != null) { retVal = Designator.getCalled(context); } return(retVal); }
/// <summary> /// Creates the graph associated to this expression, when the given parameter ranges over the X axis /// </summary> /// <param name="context">The interpretation context</param> /// <param name="parameter">The parameters of *the enclosing function* for which the graph should be created</param> /// <returns></returns> public override Functions.Graph createGraph(InterpretationContext context, Parameter parameter) { Functions.Graph retVal = base.createGraph(context, parameter); retVal = Functions.Graph.createGraph(GetValue(context), parameter); if (retVal == null) { throw new Exception("Cannot create graph for " + ToString()); } return(retVal); }
/// <summary> /// Provides the value of the function. /// The function returns true if the string passes the check. /// </summary> /// <param name="context"></param> /// <param name="actuals">the actual parameters values</param> /// <param name="explain"></param> /// <returns>The value for the function application</returns> public override IValue Evaluate(InterpretationContext context, Dictionary<Actual, IValue> actuals, ExplanationPart explain) { IValue retVal = EFSSystem.BoolType.False; int token = context.LocalScope.PushContext(); AssignParameters(context, actuals); StringValue number = context.findOnStack(Number).Value as StringValue; if (number != null && number.Val != "") { char[] tmp = number.Val.ToCharArray(); // Each character in the string is checked. The expected format is // #########FFFFF // ie. a sequence of digits, possibly followed by a sequence of 'F'. // numbersequence indicates that we are in the first part of the string bool numberSequence = true; for (int i = 0; i < tmp.Length; i++) { // If we encounter a letter that is not F, the value is incorrect if (Char.IsLetter(tmp[i]) && !tmp[i].Equals('F')) { break; } // If we encounter a number after the first 'F' character, the value is incorrect if (!numberSequence && Char.IsDigit(tmp[i])) { break; } if (Char.IsLetter(tmp[i])) { numberSequence = false; } // Once the whole string has been checked without error, set the return to true if (i == tmp.Length - 1) { retVal = EFSSystem.BoolType.True; } } } context.LocalScope.PopContext(token); return retVal; }
/// <summary> /// Provides the surface of this function if it has been statically defined /// </summary> /// <param name="context">the context used to create the surface</param> /// <param name="xParam">The X axis of this surface</param> /// <param name="yParam">The Y axis of this surface</param> /// <param name="explain"></param> /// <returns>The surface which corresponds to this expression</returns> public override Surface CreateSurface(InterpretationContext context, Parameter xParam, Parameter yParam, ExplanationPart explain) { Surface retVal = Surface.createSurface(GetValue(context, explain), xParam, yParam); if (retVal == null) { throw new Exception("Cannot create surface for " + ToString()); } retVal.XParameter = xParam; retVal.YParameter = yParam; return(retVal); }
/// <summary> /// Provides the value of the function /// </summary> /// <param name="context"></param> /// <param name="actuals">the actual parameters values</param> /// <param name="explain"></param> /// <returns>The value for the function application</returns> public override IValue Evaluate(InterpretationContext context, Dictionary<Actual, IValue> actuals, ExplanationPart explain) { IValue retVal = EFSSystem.BoolType.False; int token = context.LocalScope.PushContext(); AssignParameters(context, actuals); if (context.FindOnStack(Element).Value != EFSSystem.EmptyValue) { retVal = EFSSystem.BoolType.True; } context.LocalScope.PopContext(token); return retVal; }
/// <summary> /// Provides the value of the function /// </summary> /// <param name="context"></param> /// <param name="actuals">the actual parameters values</param> /// <param name="explain"></param> /// <returns>The value for the function application</returns> public override IValue Evaluate(InterpretationContext context, Dictionary<Actual, IValue> actuals, ExplanationPart explain) { IValue retVal = null; int token = context.LocalScope.PushContext(); AssignParameters(context, actuals); DoubleValue value = context.findOnStack(Value).Value as DoubleValue; if (value != null) { int res = (int) Math.Round(value.Val); retVal = new IntValue(ReturnType, res); } context.LocalScope.PopContext(token); return retVal; }
/// <summary> /// Provides the value of the function /// </summary> /// <param name="context"></param> /// <param name="actuals">the actual parameters values</param> /// <param name="explain"></param> /// <returns>The value for the function application</returns> public override IValue Evaluate(InterpretationContext context, Dictionary<Actual, IValue> actuals, ExplanationPart explain) { IValue retVal = null; int token = context.LocalScope.PushContext(); AssignParameters(context, actuals); BoolValue val = context.findOnStack(Value).Value as BoolValue; if (val != null) { if (val.Val) { retVal = EFSSystem.BoolType.False; } else { retVal = EFSSystem.BoolType.True; } } context.LocalScope.PopContext(token); return retVal; }
/// <summary> /// Provides the value of the function /// </summary> /// <param name="context"></param> /// <param name="actuals">the actual parameters values</param> /// <param name="explain"></param> /// <returns>The value for the function application</returns> public override IValue Evaluate(InterpretationContext context, Dictionary<Actual, IValue> actuals, ExplanationPart explain) { IValue retVal = null; int token = context.LocalScope.PushContext(); AssignParameters(context, actuals); ListValue value = context.FindOnStack(Collection) as ListValue; if (value != null) { Collection collectionType = value.Type as Collection; if (collectionType != null && collectionType.Type != null) { Type elementType = collectionType.Type; int i = 0; while (i < value.Val.Count && value.Val[i] != EFSSystem.EmptyValue) { i += 1; } if (i < value.Val.Count) { retVal = elementType.DefaultValue; value.Val[i] = retVal; } else { AddError("Cannot allocate element in list : list full"); } } } context.LocalScope.PopContext(token); return retVal; }
/// <summary> /// Provides the ICallable designated by this designator according to the interpretation context /// </summary> /// <param name="context"></param> /// <returns></returns> public ICallable GetCalled(InterpretationContext context) { ICallable retVal = GetReference(context) as ICallable; if (retVal == null) { Type type = GetDesignatorType(); if (type != null) { retVal = type.CastFunction; } } 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) { StructureValue retVal = null; Structure structureType = Structure.GetExpressionType() as Structure; if (structureType != null) { retVal = new StructureValue(this, context, explain); } else { AddError("Cannot determine structure type for " + ToString()); } return retVal; }
/// <summary> /// Provides the value associated to this Expression /// </summary> /// <param name="context">The context on which the value must be found</param> /// <returns></returns> public override Values.IValue GetValue(InterpretationContext context) { return 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; if (Term != null) { retVal = Term.GetValue(context, explain); } else { if (Not.Equals(UnaryOp)) { BoolValue b = Expression.GetValue(context, explain) as BoolValue; if (b != null) { if (b.Val) { retVal = EfsSystem.Instance.BoolType.False; } else { retVal = EfsSystem.Instance.BoolType.True; } } else { AddError("Expression " + Expression + " does not evaluate to boolean"); } } else if (Minus.Equals(UnaryOp)) { IValue val = Expression.GetValue(context, explain); IntValue intValue = val as IntValue; if (intValue != null) { retVal = new IntValue(intValue.Type, -intValue.Val); } else { DoubleValue doubleValue = val as DoubleValue; if (doubleValue != null) { retVal = new DoubleValue(doubleValue.Type, -doubleValue.Val); } } if (retVal == null) { AddError("Cannot negate value for " + Expression); } } else { retVal = Expression.GetValue(context, explain); } } return retVal; }
/// <summary> /// Provides the variable referenced by this expression, if any /// </summary> /// <param name="context">The context on which the variable must be found</param> /// <returns></returns> public override IVariable GetVariable(InterpretationContext context) { IVariable retVal = null; if (Term != null) { retVal = Term.GetVariable(context); } else if (Expression != null && UnaryOp == null) { // ReSharper disable once RedundantAssignment retVal = null; } else { AddError("Cannot get variable from expression" + ToString()); } return retVal; }
/// <summary> /// Provides the callable that is called by this expression /// </summary> /// <param name="context"></param> /// <param name="explain"></param> /// <returns></returns> public override ICallable GetCalled(InterpretationContext context, ExplanationPart explain) { ICallable retVal = null; if (Term != null) { retVal = Term.GetCalled(context, explain); } else if (Expression != null) { retVal = Expression.GetCalled(context, explain); } // TODO : Investigate why this if (retVal == null) { retVal = GetValue(context, explain) as ICallable; } return retVal; }
/// <summary> /// Creates the graph associated to this expression, when the given parameter ranges over the X axis /// </summary> /// <param name="context">The interpretation context</param> /// <param name="parameter">The parameters of *the enclosing function* for which the graph should be created</param> /// <param name="explain"></param> /// <returns></returns> public override Graph createGraph(InterpretationContext context, Parameter parameter, ExplanationPart explain) { Graph retVal = base.createGraph(context, parameter, explain); retVal = Graph.createGraph(GetValue(context, explain), parameter, explain); if (retVal == null) { throw new Exception("Cannot create graph for " + ToString()); } return retVal; }
/// <summary> /// Provides the variable designated by this designator according to the interpretation context /// </summary> /// <param name="context"></param> /// <returns></returns> public IVariable GetVariable(InterpretationContext context) { INamable reference = GetReference(context); IVariable retVal = reference as IVariable; return retVal; }
/// <summary> /// Perform additional checks based on the parameter type /// </summary> /// <param name="root">The element on which the errors should be reported</param> /// <param name="context">The evaluation context</param> /// <param name="actualParameters">The parameters applied to this function call</param> public override void additionalChecks(ModelElement root, InterpretationContext context, Dictionary<string, Expression> actualParameters) { CheckFunctionalParameter(root, context, actualParameters[Targets.Name], 1); }
/// <summary> /// Provides the value of the function /// </summary> /// <param name="context"></param> /// <param name="actuals">the actual parameters values</param> /// <param name="explain"></param> /// <returns>The value for the function application</returns> public override IValue Evaluate(InterpretationContext context, Dictionary<Actual, IValue> actuals, ExplanationPart explain) { IValue retVal = null; int token = context.LocalScope.PushContext(); AssignParameters(context, actuals); StructureValue startDate = context.findOnStack(StartDate).Value as StructureValue; if (startDate != null) { int year = GetIntValue(startDate, "Year"); int month = GetIntValue(startDate, "Month"); int day = GetIntValue(startDate, "Day"); int hour = GetIntValue(startDate, "Hour"); int minute = GetIntValue(startDate, "Minute"); int second = GetIntValue(startDate, "Second"); int tts = GetIntValue(startDate, "TTS"); DoubleValue addedTime = context.findOnStack(Increment).Value as DoubleValue; if (addedTime != null) { DateTime start = new DateTime(year, month, day, hour, minute, second, tts); DateTime currentTime = start.AddSeconds((double) addedTime.Val); retVal = GetEFSDate(currentTime, startDate.Type as Structure); } } 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> public override IValue GetValue(InterpretationContext context, ExplanationPart explain) { INamable retVal = Ref as IValue; if (retVal == null) { InterpretationContext ctxt = new InterpretationContext(context); for (int i = 0; i < Arguments.Count; i++) { if (retVal != null) { ctxt.Instance = retVal; } retVal = Arguments[i].GetValue(ctxt, explain); if (retVal == EFSSystem.EmptyValue) { break; } } } if (retVal == null) { AddError(ToString() + " does not refer to a value"); } return retVal as IValue; }
/// <summary> /// Provides the value of the prefix of the expression /// </summary> /// <param name="context">The context on which the value must be found</param> /// <param name="elementCount">The number of elements to consider</param> /// <param name="explain"></param> /// <returns></returns> public INamable GetPrefixValue(InterpretationContext context, int elementCount, ExplanationPart explain) { INamable retVal = null; InterpretationContext ctxt = new InterpretationContext(context); for (int i = 0; i < elementCount; i++) { if (retVal != null) { ctxt.Instance = retVal; } retVal = Arguments[i].GetValue(ctxt, explain); if (retVal == null) { retVal = Arguments[i].Ref; } if (retVal == EFSSystem.EmptyValue) { break; } } if (retVal == null) { AddError(ToString() + " prefix does not refer to a value"); } return retVal; }
/// <summary> /// Provides the callable that is called by this expression /// </summary> /// <param name="context"></param> /// <param name="explain"></param> /// <returns></returns> public override ICallable getCalled(InterpretationContext context, ExplanationPart explain) { ICallable retVal = Called; if (retVal == null) { AddError("Cannot evaluate call to " + ToString()); } return retVal; }
/// <summary> /// Provides the surface of this function if it has been statically defined /// </summary> /// <param name="context">the context used to create the surface</param> /// <param name="xParam">The X axis of this surface</param> /// <param name="yParam">The Y axis of this surface</param> /// <param name="explain"></param> /// <returns>The surface which corresponds to this expression</returns> public override Surface createSurface(InterpretationContext context, Parameter xParam, Parameter yParam, ExplanationPart explain) { Surface retVal = base.createSurface(context, xParam, yParam, explain); retVal = Surface.createSurface(GetValue(context, explain), xParam, yParam); if (retVal == null) { throw new Exception("Cannot create surface for " + ToString()); } retVal.XParameter = xParam; retVal.YParameter = yParam; return retVal; }
/// <summary> /// Provides the element referenced by this designator, given the enclosing element /// </summary> /// <param name="context"></param> /// <returns></returns> public INamable GetReference(InterpretationContext context) { INamable retVal = null; switch (Location) { case LocationEnum.Stack: retVal = context.LocalScope.GetVariable(Image); if (retVal == null) { AddError(Image + " not found on the stack", RuleChecksEnum.ExecutionFailed); } break; case LocationEnum.Instance: INamable instance = context.Instance; while (instance != null) { ISubDeclarator subDeclarator = instance as ISubDeclarator; if (subDeclarator != null) { INamable tmp = GetReferenceBySubDeclarator(subDeclarator); if (tmp != null) { if (retVal == null) { retVal = tmp; instance = null; } } } instance = EnclosingSubDeclarator(instance); } if (retVal == null) { AddError(Image + " not found in the current instance " + context.Instance.Name, RuleChecksEnum.ExecutionFailed); } break; case LocationEnum.This: retVal = context.Instance; break; case LocationEnum.Enclosing: ITypedElement typedElement = context.Instance as ITypedElement; while (typedElement != null && !(typedElement.Type is Structure)) { typedElement = typedElement.Enclosing as ITypedElement; } retVal = typedElement; break; case LocationEnum.Model: retVal = Ref; if (retVal == null) { AddError(Image + " not found in the enclosing model", RuleChecksEnum.ExecutionFailed); } break; case LocationEnum.NotDefined: AddError("Semantic analysis not performed on " + ToString(), RuleChecksEnum.ExecutionFailed); break; } return retVal; }
/// <summary> /// Provides the value designated by this designator according to the interpretation context /// </summary> /// <param name="context"></param> /// <returns></returns> public IValue GetValue(InterpretationContext context) { IValue retVal; INamable reference = GetReference(context); // Deref the reference, if required if (reference is IVariable) { retVal = (reference as IVariable).Value; } else { retVal = reference as IValue; } return retVal; }
/// <summary> /// Creates a list of changes to be applied on the system /// </summary> /// <param name="context">The context on which the changes should be computed</param> /// <param name="changes">The list of changes to be updated</param> /// <param name="explanation">The explanatino to fill, if any</param> /// <param name="apply">Indicates that the changes should be applied immediately</param> /// <param name="runner"></param> /// <returns>The list to fill with the changes</returns> public virtual void GetChanges(InterpretationContext context, ChangeList changes, ExplanationPart explanation, bool apply, Runner runner) { if (!DeActivated) { long start = Environment.TickCount; try { if (Statement != null) { Statement.GetChanges(context, changes, explanation, apply, runner); } else { AddError("Invalid actions statement"); } } catch (Exception e) { AddException(e); } long stop = Environment.TickCount; long span = (stop - start); if (RuleCondition != null && RuleCondition.EnclosingRule != null) { // Rule execution execution time (as opposed to guard evaluation) RuleCondition.EnclosingRule.ExecutionTimeInMilli += span; RuleCondition.EnclosingRule.ExecutionCount += 1; } } }
/// <summary> /// Provides the variable referenced by this expression, if any /// </summary> /// <param name="context">The context on which the variable must be found</param> /// <param name="explain"></param> /// <returns></returns> public override IVariable GetVariable(InterpretationContext context) { INamable current = null; InterpretationContext ctxt = new InterpretationContext(context); for (int i = 0; i < Arguments.Count; i++) { if (current != null) { // Current can be null on several loop iterations when the referenced element // does not references a variable (for instance, when it references a namespace) ctxt.Instance = current; } current = Arguments[i].GetVariable(ctxt); if (current == null) { current = Arguments[i].GetValue(ctxt, null); } } return current as IVariable; }
/// <summary> /// Provides the surface of this function if it has been statically defined /// </summary> /// <param name="context">the context used to create the surface</param> /// <param name="xParam">The X axis of this surface</param> /// <param name="yParam">The Y axis of this surface</param> /// <param name="explain"></param> /// <returns>The surface which corresponds to this expression</returns> public override Surface CreateSurface(InterpretationContext context, Parameter xParam, Parameter yParam, ExplanationPart explain) { Surface retVal = base.CreateSurface(context, xParam, yParam, explain); if (Term != null) { retVal = Surface.createSurface(xParam, yParam, GetValue(context, explain), explain); } else if (Expression != null) { if (UnaryOp == null) { retVal = Expression.CreateSurface(context, xParam, yParam, explain); } else { if (UnaryOp == Minus) { retVal = Expression.CreateSurface(context, xParam, yParam, explain); retVal.Negate(); } else { AddError("Cannot create surface with unary op " + UnaryOp); } } } retVal.XParameter = xParam; retVal.YParameter = yParam; return retVal; }
/// <summary> /// Provides the value of the function /// </summary> /// <param name="context"></param> /// <param name="actuals">the actual parameters values</param> /// <param name="explain"></param> /// <returns>The value for the function application</returns> public override IValue Evaluate(InterpretationContext context, Dictionary<Actual, IValue> actuals, ExplanationPart explain) { IValue retVal = null; int token = context.LocalScope.PushContext(); AssignParameters(context, actuals); Collection collectionType = (Collection) EFSSystem.FindType( OverallNameSpaceFinder.INSTANCE.findByName(EFSSystem.Dictionaries[0], "Kernel.SpeedAndDistanceMonitoring.TargetSpeedMonitoring"), "Kernel.SpeedAndDistanceMonitoring.TargetSpeedMonitoring.SpeedRestrictions"); ListValue collection = new ListValue(collectionType, new List<IValue>()); Function function = context.findOnStack(Targets).Value as Function; if (function != null && !function.Name.Equals("EMPTY")) { Graph graph1 = createGraphForValue(context, function, explain); ComputeTargets(graph1.Function, collection); } context.LocalScope.PopContext(token); retVal = collection; return retVal; }
// left +/-/*/div/exp right /// <summary> /// Performs the arithmetic operation based on the type of the result /// </summary> /// <param name="context">The context used to perform this operation</param> /// <param name="left"></param> /// <param name="Operation"></param> /// <param name="right"></param> /// <returns></returns> public override IValue PerformArithmericOperation(InterpretationContext context, IValue left, BinaryExpression.OPERATOR Operation, IValue right) { IValue retVal = null; left = derefEnumForArithmeticOperation(left); right = derefEnumForArithmeticOperation(right); IntValue int1 = left as IntValue; IntValue int2 = right as IntValue; if (int1 == null || int2 == null) { retVal = EFSSystem.DoubleType.PerformArithmericOperation(context, left, Operation, right); } else { retVal = EFSSystem.IntegerType.PerformArithmericOperation(context, left, Operation, right); } return retVal; }
/// <summary> /// Edits a value expression and provides the edited expression after user has performed his changes /// </summary> /// <param name="expression"></param> /// <returns></returns> private Expression EditExpression(Expression expression) { Expression retVal = expression; if (expression != null) { ModelElement.DontRaiseError(() => { InterpretationContext context = new InterpretationContext(Model) {UseDefaultValue = false}; IValue value = expression.GetExpressionValue(context, null); if (value != null) { StructureEditor.Window window = new StructureEditor.Window(); window.SetModel(value); window.ShowDialog(); string newExpression = value.ToExpressionWithDefault(); const bool doSemanticalAnalysis = true; const bool silent = true; retVal = new Parser().Expression(expression.Root, newExpression, AllMatches.INSTANCE, doSemanticalAnalysis, null, silent); } }); } return retVal; }
/// <summary> /// Creates the graph associated to this expression, when the given parameter ranges over the X axis /// </summary> /// <param name="context">The interpretation context</param> /// <param name="parameter">The parameters of *the enclosing function* for which the graph should be created</param> /// <param name="explain"></param> /// <returns></returns> public override Graph CreateGraph(InterpretationContext context, Parameter parameter, ExplanationPart explain) { Graph retVal = base.CreateGraph(context, parameter, explain); if (Term != null) { retVal = Graph.createGraph(GetValue(context, explain), parameter, explain); } else if (Expression != null) { if (UnaryOp == null) { retVal = Expression.CreateGraph(context, parameter, explain); } else if (UnaryOp == Minus) { retVal = Expression.CreateGraph(context, parameter, explain); retVal.Negate(); } else { throw new Exception("Cannot create graph where NOT operator is defined"); } } return retVal; }