GetValue() protected abstract method

Provides the value associated to this Expression
protected abstract GetValue ( DataDictionary.Interpreter.InterpretationContext context, ExplanationPart explain ) : IValue
context DataDictionary.Interpreter.InterpretationContext The context on which the value must be found
explain ExplanationPart
return IValue
        /// <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>
        /// 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);
        }
Ejemplo n.º 3
0
        /// <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);
        }
Ejemplo n.º 4
0
        /// <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);
        }
Ejemplo n.º 5
0
        /// <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>
        /// Creates the graph for the unbount parameters provided
        /// </summary>
        /// <param name="context"></param>
        /// <param name="expression"></param>
        /// <param name="function"></param>
        /// <param name="unbound"></param>
        /// <returns></returns>
        private Surface createSurfaceForUnbound(InterpretationContext context, Expression expression, Function function, List <Parameter> unbound)
        {
            Surface retVal = null;

            if (unbound.Count == 0)
            {
                if (function != null)
                {
                    Parameter xAxis = null;

                    if (function.FormalParameters.Count > 0)
                    {
                        xAxis = (Parameter)function.FormalParameters[0];
                    }
                    Parameter yAxis = null;
                    if (function.FormalParameters.Count > 1)
                    {
                        yAxis = (Parameter)function.FormalParameters[1];
                    }
                    retVal = function.createSurfaceForParameters(context, xAxis, yAxis);
                }
                else
                {
                    retVal = Surface.createSurface(expression.GetValue(context), null, null);
                }
            }
            else if (unbound.Count == 1)
            {
                Graph graph = createGraphForUnbound(context, expression, function, unbound);
                retVal = Surface.createSurface(graph.Function, unbound[0], null);
            }
            else
            {
                if (function == null)
                {
                    retVal = expression.createSurface(context, unbound[0], unbound[1]);
                }
                else
                {
                    retVal = function.createSurfaceForParameters(context, unbound[0], unbound[1]);
                }
            }
            return(retVal);
        }
        /// <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 graph for the unbount parameters provided
        /// </summary>
        /// <param name="context"></param>
        /// <param name="expression"></param>
        /// <param name="function"></param>
        /// <param name="unbound"></param>
        /// <returns></returns>
        private Surface createSurfaceForUnbound(InterpretationContext context, Expression expression, Function function, List<Parameter> unbound)
        {
            Surface retVal = null;

            if (unbound.Count == 0)
            {
                if (function != null)
                {
                    Parameter xAxis = null;

                    if (function.FormalParameters.Count > 0)
                    {
                        xAxis = (Parameter)function.FormalParameters[0];
                    }
                    Parameter yAxis = null;
                    if (function.FormalParameters.Count > 1)
                    {
                        yAxis = (Parameter)function.FormalParameters[1];
                    }
                    retVal = function.createSurfaceForParameters(context, xAxis, yAxis);
                }
                else
                {
                    retVal = Surface.createSurface(expression.GetValue(context), null, null);
                }
            }
            else if (unbound.Count == 1)
            {
                Graph graph = createGraphForUnbound(context, expression, function, unbound);
                retVal = Surface.createSurface(graph.Function, unbound[0], null);
            }
            else
            {
                if (function == null)
                {
                    retVal = expression.createSurface(context, unbound[0], unbound[1]);
                }
                else
                {
                    retVal = function.createSurfaceForParameters(context, unbound[0], unbound[1]);
                }
            }
            return retVal;
        }
Ejemplo n.º 9
0
        /// <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);
        }
Ejemplo n.º 10
0
        /// <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)
            {
                DataDictionary.ModelElement.DontRaiseError(() =>
                {
                    InterpretationContext context = new InterpretationContext(Model) {UseDefaultValue = false};
                    IValue value = expression.GetValue(context, null);
                    if (value != null)
                    {
                        Window window = new Window();
                        window.SetModel(value);
                        window.ShowDialog();

                        string newExpression = value.ToExpressionWithDefault();
                        const bool doSemanticalAnalysis = true;
                        const bool silent = true;
                        retVal = EFSSystem.INSTANCE.Parser.Expression(expression.Root, newExpression,
                            AllMatches.INSTANCE, doSemanticalAnalysis, null, silent);
                    }
                });
            }

            return retVal;
        }
Ejemplo n.º 11
0
        /// <summary>
        ///     Provides the value of the expression provided
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="expression"></param>
        /// <param name="explain"></param>
        /// <returns></returns>
        private bool getBoolValue(ModelElement instance, Expression expression, ExplanationPart explain)
        {
            bool retVal;

            InterpretationContext context = new InterpretationContext(instance);
            BoolValue val = expression.GetValue(context, explain) as BoolValue;

            if (val != null)
            {
                retVal = val.Val;
            }
            else
            {
                throw new Exception("Cannot evaluate value of " + expression);
            }

            return retVal;
        }