Beispiel #1
0
        /// <summary>
        /// Provides the states used in an expression
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static List <Constants.State> GetStates(Interpreter.Expression expression)
        {
            List <Constants.State> retval = new List <Constants.State>();

            if (expression != null)
            {
                foreach (Values.IValue value in expression.GetLiterals())
                {
                    Constants.State state = value as Constants.State;
                    if (state != null)
                    {
                        retval.Add(state);
                    }
                }

                Interpreter.Call call = expression as Interpreter.Call;
                if (call != null)
                {
                    Functions.Function function = call.Called.getStaticCallable() as Functions.Function;
                    if (function != null)
                    {
                        foreach (Values.IValue value in function.GetLiterals())
                        {
                            Constants.State state = value as Constants.State;
                            if (state != null)
                            {
                                retval.Add(state);
                            }
                        }
                    }
                }
            }

            return(retval);
        }
Beispiel #2
0
        /// <summary>
        /// Expression of the case
        /// </summary>
        public bool EvaluatePreConditions(InterpretationContext context)
        {
            bool retVal = true;

            foreach (DataDictionary.Rules.PreCondition preCondition in PreConditions)
            {
                Interpreter.Expression expression = preCondition.ExpressionTree;
                Values.BoolValue       value      = expression.GetValue(context) as Values.BoolValue;

                if (value != null)
                {
                    retVal = retVal && value.Val;
                }
                else
                {
                    retVal = false;
                }

                if (!retVal)
                {
                    break;
                }
            }
            return(retVal);
        }
Beispiel #3
0
        /// <summary>
        /// Provides the actual value for the preconditions
        /// </summary>
        /// <param name="context">The context on which the precondition must be evaluated</param>
        /// <returns></returns>
        public bool EvaluatePreConditions(Interpreter.InterpretationContext context)
        {
            bool retVal = true;

            foreach (DataDictionary.Rules.PreCondition preCondition in PreConditions)
            {
                try
                {
                    Interpreter.Expression expression = preCondition.ExpressionTree;
                    Values.BoolValue       value      = expression.GetValue(context) as Values.BoolValue;
                    if (value != null)
                    {
                        retVal = retVal && value.Val;
                    }
                    else
                    {
                        retVal = false;
                        // TODO : Handle Error
                    }

                    if (!retVal)
                    {
                        break;
                    }
                }
                catch (Exception e)
                {
                    preCondition.AddException(e);
                    retVal = false;
                    break;
                }
            }

            return(retVal);
        }
        /// <summary>
        /// Parses the image and provides the corresponding value
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public override Values.IValue getValue(string image)
        {
            Values.IValue retVal = null;

            Interpreter.Expression expression = EFSSystem.Parser.Expression(this, image);
            if (expression != null)
            {
                retVal = expression.GetValue(new Interpreter.InterpretationContext(this));
            }

            return(retVal);
        }
        /// <summary>
        /// Gets a value based on its image
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public override Values.IValue getValue(string image)
        {
            Values.IValue retVal = null;

            Interpreter.Expression expression = EFSSystem.Parser.Expression(this, image);
            Types.Type             type       = expression.GetExpressionType() as Types.Type;
            if (type != null && Match(type))
            {
                retVal = expression.GetValue(new Interpreter.InterpretationContext());
            }

            return(retVal);
        }
        /// <summary>
        /// Indicates if the expression if of the form parameter <= xxx or xxx <= parameter
        /// </summary>
        /// <param name="parameter">The parameter of the template</param>
        /// <param name="expression">The expression to analyze</param>
        /// <returns></returns>
        private bool ExpressionBasedOnParameter(Parameter parameter, Interpreter.Expression expression)
        {
            bool retVal = false;

            Interpreter.BinaryExpression binaryExpression = expression as Interpreter.BinaryExpression;
            if (binaryExpression != null)
            {
                retVal = binaryExpression.Right.Ref == parameter ||
                         binaryExpression.Left.Ref == parameter ||
                         FunctionCallOnParameter(binaryExpression.Right, parameter) ||
                         FunctionCallOnParameter(binaryExpression.Left, parameter);
            }

            return(retVal);
        }
        /// <summary>
        /// Indicates that the expression is a function call using the parameter as argument value
        /// </summary>
        /// <param name="expression">The expression to evaluate</param>
        /// <param name="parameter">The parameter</param>
        /// <returns></returns>
        private bool FunctionCallOnParameter(Interpreter.Expression expression, Parameter parameter)
        {
            bool retVal = false;

            Interpreter.Call call = expression as Interpreter.Call;
            if (call != null)
            {
                foreach (Interpreter.Expression expr in call.AllParameters)
                {
                    foreach (Types.ITypedElement element in expr.GetRightSides())
                    {
                        if (element == parameter)
                        {
                            retVal = true;
                            break;
                        }
                    }
                }
            }

            return(retVal);
        }
Beispiel #8
0
            /// <summary>
            /// Check if this rule corresponds to a transition for this state machine
            /// </summary>
            /// <param name="obj"></param>
            /// <param name="visitSubNodes"></param>
            public override void visit(Generated.RuleCondition obj, bool visitSubNodes)
            {
                Rules.RuleCondition ruleCondition = (Rules.RuleCondition)obj;

                foreach (Rules.Action action in ruleCondition.Actions)
                {
                    foreach (Interpreter.Statement.VariableUpdateStatement update in action.UpdateStatements)
                    {
                        Types.Type targetType = update.TargetType;
                        if (targetType is StateMachine)
                        {
                            Interpreter.Expression expressionTree = update.Expression;
                            if (expressionTree != null)
                            {
                                // HaCK: This is a bit rough, but should be sufficient for now...
                                foreach (Constants.State stt1 in GetStates(expressionTree))
                                {
                                    // TargetState is the target state either in this state machine or in a sub state machine
                                    Constants.State targetState = StateMachine.StateInThisStateMachine(stt1);

                                    int  transitionCount = Transitions.Count;
                                    bool filteredOut     = false;

                                    // Finds the enclosing state of this action to determine the source state of this transition
                                    Constants.State enclosingState = Utils.EnclosingFinder <Constants.State> .find(action);

                                    if (enclosingState != null)
                                    {
                                        filteredOut = filteredOut || AddTransition(update, stt1, null, enclosingState);
                                    }

                                    if (!filteredOut)
                                    {
                                        foreach (Rules.PreCondition preCondition in ruleCondition.AllPreConditions)
                                        {
                                            // A transition from one state to another has been found
                                            foreach (Constants.State stt2 in GetStates(preCondition.ExpressionTree))
                                            {
                                                filteredOut = filteredOut || AddTransition(update, stt1, preCondition, stt2);
                                            }
                                        }
                                    }

                                    if (Transitions.Count == transitionCount)
                                    {
                                        if (targetState == stt1 && targetState.EnclosingStateMachine == StateMachine)
                                        {
                                            if (!filteredOut)
                                            {
                                                // No precondition could be found => one can reach this state at anytime
                                                Transitions.Add(new Rules.Transition(null, null, update, targetState));
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                action.AddError("Cannot parse expression");
                            }
                        }
                    }
                }

                base.visit(obj, visitSubNodes);
            }
        /// <summary>
        /// Ensures that the parameter provided corresponds to a function double->double
        /// </summary>
        /// <param name="root">Element on which the errors shall be attached</param>
        /// <param name="context">The context used to evaluate the expression</param>
        /// <param name="expression">The expression which references the function</param>
        /// <param name="count">the expected number of parameters</param>
        protected virtual void CheckFunctionalParameter(ModelElement root, Interpreter.InterpretationContext context, Interpreter.Expression expression, int count)
        {
            Types.Type type = expression.GetExpressionType();

            Function function = type as Function;

            if (function != null)
            {
                if (function.FormalParameters.Count == count)
                {
                    foreach (Parameter parameter in function.FormalParameters)
                    {
                        if (!parameter.Type.IsDouble())
                        {
                            root.AddError(expression.ToString() + " does not takes a double for parameter " + parameter.Name);
                        }
                    }
                }
                else
                {
                    root.AddError(expression.ToString() + " does not take " + count + "parameter(s) as input");
                }

                if (!function.ReturnType.IsDouble())
                {
                    root.AddError(expression.ToString() + " does not return a double");
                }
            }
            else
            {
                if (!type.IsDouble())
                {
                    root.AddError(expression.ToString() + " type is not double");
                }
            }
        }