/// <summary> /// Constructor /// </summary> /// <param name="root"></param> /// <param name="log"></param> /// <param name="structure"></param> /// <param name="associations"></param> /// <param name="parsingData">Additional information about the parsing process</param> public StructExpression(ModelElement root, ModelElement log, Expression structure, Dictionary<Designator, Expression> associations, ParsingData parsingData) : base(root, log, parsingData) { Structure = SetEnclosed(structure); SetAssociation(associations); }
/// <summary> /// Constructor /// </summary> /// <param name="root">The root for which this expression should be evaluated</param> /// <param name="log"></param> /// <param name="expression">The enclosed expression</param> /// <param name="unaryOp">the unary operator for this unary expression</param> /// <param name="start">The start character for this expression in the original string</param> /// <param name="end">The end character for this expression in the original string</param> public UnaryExpression(ModelElement root, ModelElement log, Expression expression, string unaryOp, int start, int end) : base(root, log, start, end) { Expression = SetEnclosed(expression); UnaryOp = unaryOp; }
public override void visit(Generated.Expectation obj, bool subNodes) { Tests.Expectation expect = obj as Tests.Expectation; if (expect != null) { try { expect.Messages.Clear(); if (!expect.Expression.Contains("%")) { Interpreter.Expression expression = checkExpression(expect, expect.Expression); if (!expect.EFSSystem.BoolType.Match(expression.GetExpressionType())) { expect.AddError("Expression type should be Boolean"); } } if (expect.getCondition() != null && !expect.getCondition().Contains("%")) { Interpreter.Expression expression = checkExpression(expect, expect.getCondition()); if (!expect.EFSSystem.BoolType.Match(expression.GetExpressionType())) { expect.AddError("Condition type should be Boolean"); } } } catch (Exception exception) { expect.AddException(exception); } } base.visit(obj, subNodes); }
/// <summary> /// Checks an expression associated to a model element /// </summary> /// <param name="model">The model element on which the expression is defined</param> /// <param name="expression">The expression to check</param> /// <returns>the expression parse tree</returns> private Interpreter.Expression checkExpression(ModelElement model, string expression) { Interpreter.Expression retVal = null; Interpreter.Parser parser = model.EFSSystem.Parser; try { retVal = parser.Expression(model, expression); if (retVal != null) { retVal.checkExpression(); Types.Type type = retVal.GetExpressionType(); if (type == null) { model.AddError("Cannot determine expression type (5) for " + retVal.ToString()); } } else { model.AddError("Cannot parse expression"); } } catch (Exception exception) { model.AddException(exception); } return(retVal); }
/// <summary> /// Constructor /// </summary> /// <param name="root"></param> /// <param name="log"></param> /// <param name="structure"></param> /// <param name="associations"></param> /// <param name="start">The start character for this expression in the original string</param> /// <param name="end">The end character for this expression in the original string</param> public StructExpression(ModelElement root, ModelElement log, Expression structure, Dictionary<Designator, Expression> associations, int start, int end) : base(root, log, start, end) { Structure = structure; SetAssociation(associations); }
public override void visit(Generated.Case obj, bool visitSubNodes) { Functions.Case cas = obj as Functions.Case; try { Interpreter.Expression expression = cas.Expression; if (expression != null) { expression.checkExpression(); Types.Type expressionType = cas.Expression.GetExpressionType(); if (expressionType != null) { if (!cas.EnclosingFunction.ReturnType.Match(expressionType)) { cas.AddError("Expression type (" + expressionType.FullName + ") does not match function return type (" + cas.EnclosingFunction.ReturnType.Name + ")"); } } else { cas.AddError("Cannot determine expression type (6) for " + cas.Expression.ToString()); } } else { cas.AddError("Cannot evaluate expression " + cas.ExpressionText); } } catch (Exception e) { cas.AddException(e); } base.visit(obj, visitSubNodes); }
/// <summary> /// Constructor /// </summary> /// <param name="root">The root for which this expression should be evaluated</param> /// <param name="expression">The enclosed expression</param> /// <param name="unaryOp">the unary operator for this unary expression</parparam> public UnaryExpression(ModelElement root, Expression expression, string unaryOp = null) : base(root) { Expression = expression; Expression.Enclosing = this; UnaryOp = unaryOp; }
/// <summary> /// Constructor /// </summary> /// <param name="root">The root for which this expression should be evaluated</param> /// <param name="expression">The enclosed expression</param> /// <param name="unaryOp"> /// the unary operator for this unary expression</parparam> /// <param name="start">The start character for this expression in the original string</param> /// <param name="end">The end character for this expression in the original string</param> public UnaryExpression(ModelElement root, ModelElement log, Expression expression, string unaryOp, int start, int end) : base(root, log, start, end) { Expression = expression; Expression.Enclosing = this; UnaryOp = unaryOp; }
/// <summary> /// Constructor /// </summary> /// <param name="expression">the functional expression</param> /// <param name="log"></param> /// <param name="parameters">the function parameters</param> /// <param name="root"></param> /// <param name="parsingData">Additional information about the parsing process</param> public FunctionExpression(ModelElement root, ModelElement log, List<Parameter> parameters, Expression expression, ParsingData parsingData) : base(root, log, parsingData) { Parameters = parameters; Expression = SetEnclosed(expression); InitDeclaredElements(); }
/// <summary> /// Constructor /// </summary> /// <param name="left"></param> /// <param name="op"></param> /// <param name="right"></param> public BinaryExpression(ModelElement root, Expression left, OPERATOR op, Expression right) : base(root) { Left = left; Left.Enclosing = this; Operation = op; Right = right; Right.Enclosing = this; }
/// <summary> /// Constructor /// </summary> /// <param name="root"></param> public StructExpression(ModelElement root, Expression structure, Dictionary<string, Expression> associations) : base(root) { Structure = structure; Associations = associations; foreach (Expression expr in Associations.Values) { expr.Enclosing = this; } }
/// <summary> /// Constructor /// </summary> /// <param name="expression">the functional expression</param> /// <param name="parameters">the function parameters</param> /// <param name="root"></param> public FunctionExpression(ModelElement root, List<Parameter> parameters, Expression expression) : base(root) { Parameters = parameters; Expression = expression; Expression.Enclosing = this; InitDeclaredElements(); }
/// <summary> /// Constructor /// </summary> /// <param name="expression">the functional expression</param> /// <param name="log"></param> /// <param name="parameters">the function parameters</param> /// <param name="root"></param> /// <param name="start">The start character for this expression in the original string</param> /// <param name="end">The end character for this expression in the original string</param> public FunctionExpression(ModelElement root, ModelElement log, List<Parameter> parameters, Expression expression, int start, int end) : base(root, log, start, end) { Parameters = parameters; Expression = expression; Expression.Enclosing = this; InitDeclaredElements(); }
/// <summary> /// Constructor /// </summary> /// <param name="root">the root element for which this expression should be parsed</param> /// <param name="log"></param> /// <param name="boundVariableName">The name of the bound variable</param> /// <param name="bindingExpression">The binding expression which provides the value of the variable</param> /// <param name="expression">The expression to be evaluated</param> /// <param name="parsingData">Additional information about the parsing process</param> public LetExpression(ModelElement root, ModelElement log, string boundVariableName, Expression bindingExpression, Expression expression, ParsingData parsingData) : base(root, log, parsingData) { BoundVariable = CreateBoundVariable(boundVariableName, null); BindingExpression = SetEnclosed(bindingExpression); Expression = SetEnclosed(expression); InitDeclaredElements(); }
/// <summary> /// Constructor /// </summary> /// <param name="root"></param> /// <param name="log"></param> /// <param name="expression">The expression to stabilize</param> /// <param name="initialValue">The initial value for this stabilisation computation</param> /// <param name="condition">The condition which indicates that the stabilisation is not complete</param> /// <param name="parsingData">Additional information about the parsing process</param> public StabilizeExpression(ModelElement root, ModelElement log, Expression expression, Expression initialValue, Expression condition, ParsingData parsingData) : base(root, log, parsingData) { Expression = SetEnclosed(expression); InitialValue = SetEnclosed(initialValue); Condition = SetEnclosed(condition); LastIteration = CreateBoundVariable("PREVIOUS", null); CurrentIteration = CreateBoundVariable("CURRENT", null); InitDeclaredElements(); }
/// <summary> /// Constructor /// </summary> /// <param name="root">the root element for which this expression should be parsed</param> /// <param name="start">The start character for this expression in the original string</param> /// <param name="end">The end character for this expression in the original string</param> /// <param name="boundVariableName">The name of the bound variable</param> /// <param name="bindingExpression">The binding expression which provides the value of the variable</param> /// <param name="expression">The expression to be evaluated</param> public LetExpression(ModelElement root, ModelElement log, string boundVariableName, Expression bindingExpression, Expression expression, int start, int end) : base(root, log, start, end) { BoundVariable = (Variable) acceptor.getFactory().createVariable(); BoundVariable.Enclosing = this; BoundVariable.Name = boundVariableName; BindingExpression = bindingExpression; BindingExpression.Enclosing = this; Expression = expression; Expression.Enclosing = this; InitDeclaredElements(); }
public override void visit(Generated.PreCondition obj, bool subNodes) { Rules.PreCondition preCondition = obj as Rules.PreCondition; if (preCondition != null) { try { // Check whether the expression is valid Interpreter.Expression expression = checkExpression(preCondition, preCondition.Condition); if (!preCondition.Dictionary.EFSSystem.BoolType.Match(expression.GetExpressionType())) { preCondition.AddError("Expression type should be Boolean"); } Types.ITypedElement element = OverallTypedElementFinder.INSTANCE.findByName(preCondition, preCondition.findVariable()); if (element != null) { if (element.Type is Types.StateMachine) { if (preCondition.findOperator() != null) { if (preCondition.findOperator().CompareTo("==") == 0) { preCondition.AddWarning("Operator == should not be used for state machines"); } else if (preCondition.findOperator().CompareTo("!=") == 0) { preCondition.AddWarning("Operator != should not be used for state machines"); } } } } } catch (Exception exception) { preCondition.AddException(exception); } } base.visit(obj, subNodes); }
/// <summary> /// Constructor /// </summary> /// <param name="root"></param> /// <param name="expression">The expression to stabilize</param> /// <param name="initialValue">The initial value for this stabilisation computation</param> /// <param name="condition">The condition which indicates that the stabilisation is not complete</param> public StabilizeExpression(ModelElement root, Expression expression, Expression initialValue, Expression condition) : base(root) { Expression = expression; Expression.Enclosing = this; InitialValue = initialValue; InitialValue.Enclosing = this; Condition = condition; Condition.Enclosing = this; LastIteration = (Variables.Variable)Generated.acceptor.getFactory().createVariable(); LastIteration.Enclosing = this; LastIteration.Name = "PREVIOUS"; CurrentIteration = (Variables.Variable)Generated.acceptor.getFactory().createVariable(); CurrentIteration.Enclosing = this; CurrentIteration.Name = "CURRENT"; InitDeclaredElements(); }
/// <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, InterpretationContext context, Expression expression, int count) { 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"); } } }
/// <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.GetExpressionValue(context, explain) as BoolValue; if (val != null) { retVal = val.Val; } else { throw new Exception("Cannot evaluate value of " + expression); } return retVal; }
/// <summary> /// Adds an expression as a parameter /// </summary> /// <param name="name">the name of the actual parameter</param> /// <param name="expression">the actual parameter value</param> public void AddActualParameter(string name, Expression expression) { if (name == null) { ActualParameters.Add(expression); } else { if (!NamedActualParameters.ContainsKey(name)) { NamedActualParameters[name] = expression; } else { AddError("Actual parameter " + name + " is bound twice"); } } expression.Enclosing = this; }
/// <summary> /// Constructor /// </summary> /// <param name="root">The root element for which this element is built</param> /// <param name="called">The called function</param> public Call(ModelElement root, Expression called) : base(root) { Called = called; Called.Enclosing = this; }
/// <summary> /// Creates the tree according to the expression text /// </summary> public InterpreterTreeNode Compile() { if (ReferencedStructureExpression == null) { ReferencedStructureExpression = new Parser().Expression(this, ExpressionText, IsType.INSTANCE); if (ReferencedStructureExpression != null) { foreach (Usage usage in ReferencedStructureExpression.StaticUsage.AllUsages) { usage.Mode = Usage.ModeEnum.Interface; } } } return ReferencedStructureExpression; }
/// <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(Expression expression, Parameter parameter) { bool retVal = false; Call call = expression as Call; if (call != null) { foreach (Expression expr in call.AllParameters) { foreach (ITypedElement element in expr.GetRightSides()) { if (element == parameter) { retVal = true; break; } } } } return retVal; }
/// <summary> /// Constructor /// </summary> /// <param name="root">The root for which this expression should be evaluated</param> /// <param name="log"></param> /// <param name="expression">The enclosed expression</param> /// <param name="unaryOp">the unary operator for this unary expression</param> /// <param name="parsingData">Additional information about the parsing process</param> public UnaryExpression(ModelElement root, ModelElement log, Expression expression, string unaryOp, ParsingData parsingData) : base(root, log, parsingData) { Expression = SetEnclosed(expression); UnaryOp = unaryOp; }
/// <summary> /// Visits an expression /// </summary> /// <param name="expression"></param> protected virtual void VisitExpression(Expression expression) { if (expression != null) { // ReSharper disable CanBeReplacedWithTryCastAndCheckForNull BinaryExpression binaryExpression = expression as BinaryExpression; if (binaryExpression != null) { VisitBinaryExpression(binaryExpression); } else if (expression is Call) { VisitCall((Call) expression); } else if (expression is DerefExpression) { VisitDerefExpression((DerefExpression) expression); } else if (expression is FunctionExpression) { VisitFunctionExpression((FunctionExpression) expression); } else if (expression is ListExpression) { VisitListExpression((ListExpression) expression); } else if (expression is NumberExpression) { VisitNumberExpression((NumberExpression) expression); } else if (expression is StringExpression) { VisitStringExpression((StringExpression) expression); } else if (expression is StructExpression) { VisitStructExpression((StructExpression) expression); } else if (expression is UnaryExpression) { VisitUnaryExpression((UnaryExpression) expression); } else if (expression is CountExpression) { VisitCountExpression((CountExpression) expression); } else if (expression is FirstExpression) { VisitFirstExpression((FirstExpression) expression); } else if (expression is ForAllExpression) { VisitForAllExpression((ForAllExpression) expression); } else if (expression is LastExpression) { VisitLastExpression((LastExpression) expression); } else if (expression is MapExpression) { VisitMapExpression((MapExpression) expression); } else if (expression is ReduceExpression) { VisitReduceExpression((ReduceExpression) expression); } else if (expression is SumExpression) { VisitSumExpression((SumExpression) expression); } else if (expression is ThereIsExpression) { VisitThereIsExpression((ThereIsExpression) expression); } else if (expression is StabilizeExpression) { VisitStabilizeExpression((StabilizeExpression) expression); } else if (expression is LetExpression) { VisitLetExpression((LetExpression) expression); } // ReSharper restore CanBeReplacedWithTryCastAndCheckForNull } }
/// <summary> /// Indicates if the expression if of the form parameter less than xxx or xxx greater than 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, Expression expression) { bool retVal = false; BinaryExpression binaryExpression = expression as BinaryExpression; if (binaryExpression != null) { retVal = binaryExpression.Right.Ref == parameter || binaryExpression.Left.Ref == parameter || FunctionCallOnParameter(binaryExpression.Right, parameter) || FunctionCallOnParameter(binaryExpression.Left, parameter); } return retVal; }
private void CheckActualAgainstFormal(Dictionary<string, Expression> actuals, Expression expression, Parameter parameter) { actuals[parameter.Name] = expression; expression.checkExpression(); Types.Type argumentType = expression.GetExpressionType(); if (argumentType == null) { AddError("Cannot evaluate argument type for argument " + expression.ToString()); } else { if (parameter.Type == null) { AddError("Cannot evaluate formal parameter type for " + parameter.Name); } else { if (!parameter.Type.Match(argumentType)) { AddError("Invalid argument " + expression.ToString() + " type, expected " + parameter.Type.FullName + ", actual " + argumentType.FullName); } } } }
/// <summary> /// Constructor /// </summary> /// <param name="root">The root element for which this model is built</param> /// <param name="log"></param> /// <param name="literal"></param> /// <param name="parsingData">Additional information about the parsing process</param> public Term(ModelElement root, ModelElement log, Expression literal, ParsingData parsingData) : base(root, log, parsingData) { LiteralValue = SetEnclosed(literal); }
/// <summary> /// Provides the states used in an expression /// </summary> /// <param name="expression"></param> /// <returns></returns> public static List<State> GetStates(Expression expression) { List<State> retval = new List<State>(); if (expression != null) { foreach (IValue value in expression.GetLiterals()) { State state = value as State; if (state != null) { retval.Add(state); } } Call call = expression as Call; if (call != null) { Function function = call.Called.GetStaticCallable() as Function; if (function != null) { foreach (IValue value in function.GetLiterals()) { State state = value as State; if (state != null) { retval.Add(state); } } } } } 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> /// Clears the expression tree to ensure new compilation /// </summary> public void CleanCompilation() { ReferencedStructureExpression = null; }
/// <summary> /// Browses through the expression to find the value to edit /// </summary> /// <param name="expression"></param> /// <returns></returns> private Expression VisitExpression(Expression expression) { Expression retVal = expression; BinaryExpression binaryExpression = expression as BinaryExpression; if (binaryExpression != null) { binaryExpression.Left = VisitExpression(binaryExpression.Left); binaryExpression.Right = VisitExpression(binaryExpression.Right); } UnaryExpression unaryExpression = expression as UnaryExpression; if (unaryExpression != null) { if (unaryExpression.Expression != null) { unaryExpression.Expression = VisitExpression(unaryExpression.Expression); } else if (unaryExpression.Term != null) { VisitTerm(unaryExpression.Term); } } StructExpression structExpression = expression as StructExpression; if (structExpression != null) { retVal = EditExpression(structExpression); } Call call = expression as Call; if (call != null) { foreach (Expression subExpression in call.AllParameters) { VisitExpression(subExpression); } } return retVal; }
/// <summary> /// Constructor /// </summary> /// <param name="root">The root element for which this model is built</param> /// <param name="literal"></param> /// <param name="start">The start character for this expression in the original string</param> /// <param name="end">The end character for this expression in the original string</param> public Term(ModelElement root, ModelElement log, Expression literal, int start, int end) : base(root, log, start, end) { LiteralValue = literal; }