/// <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);
        }
        public void TestApplyStatement()
        {
            Dictionary test = CreateDictionary("Test");
            NameSpace n1 = CreateNameSpace(test, "N1");
            Structure s1 = CreateStructure(n1, "S1");
            StructureElement el1 = CreateStructureElement(s1, "E1", "Boolean");
            Structure s2 = CreateStructure(n1, "S2");
            StructureElement el2 = CreateStructureElement(s2, "E2", "S1");
            Function function = CreateFunction(n1, "f", "S1");

            Collection collection = CreateCollection(n1, "Col", "S1", 10);
            Variable v = CreateVariable(n1, "V", "Col");

            Compiler.Compile_Synchronous(true, true);

            RuleCondition rc = CreateRuleAndCondition(n1, "Rule1");
            Parser parser = new Parser();

            {
                ApplyStatement statement = parser.Statement(rc, "APPLY X <- X", true, true) as ApplyStatement;
                Assert.IsNotNull(statement);
            }

            {
                ApplyStatement statement = parser.Statement(rc, "APPLY X <- X ON V | X.", true, true) as ApplyStatement;
                Assert.IsNotNull(statement);

                DerefExpression deref = statement.ConditionExpression as DerefExpression;
                Assert.IsNotNull(deref);

                ITypedElement element = deref.Arguments[0].Ref as ITypedElement;
                Assert.IsNotNull(element);
                Assert.AreEqual(element.Type, s1);
            }
        }
        public void TestFunctionCall()
        {
            Dictionary test = CreateDictionary("Test");
            NameSpace n1 = CreateNameSpace(test, "N1");
            Structure s1 = CreateStructure(n1, "S1");
            StructureElement el1 = CreateStructureElement(s1, "E1", "Boolean");
            Structure s2 = CreateStructure(n1, "S2");
            StructureElement el2 = CreateStructureElement(s2, "E2", "S1");
            Variable v = CreateVariable(n1, "V", "S1");
            v.setDefaultValue("N1.S1 { E1 => True }");
            Function function = CreateFunction(n1, "f", "S1");

            Compiler.Compile_Synchronous(true, true);

            RuleCondition rc = CreateRuleAndCondition(n1, "Rule1");
            Parser parser = new Parser();

            {
                //                   0         1
                //                   012345678901
                const string text = "V <- f().S";
                VariableUpdateStatement statement = parser.Statement(rc, text, true, true) as VariableUpdateStatement;
                ContextGrabber grabber = new ContextGrabber();
                Assert.AreEqual(v, grabber.GetContext(0, statement));
                Assert.AreEqual(v, grabber.GetContext(1, statement));
                Assert.IsNull(grabber.GetContext(2, statement));
                Assert.IsNull(grabber.GetContext(3, statement));
                Assert.IsNull(grabber.GetContext(4, statement));
                Assert.AreEqual(function, grabber.GetContext(5, statement));
                Assert.AreEqual(function, grabber.GetContext(6, statement));
                Assert.AreEqual(function, grabber.GetContext(7, statement));
                Assert.AreEqual(s1, grabber.GetContext(8, statement));
                Assert.IsNull(grabber.GetContext(9, statement));
                Assert.IsNull(grabber.GetContext(10, statement));
            }

            {
                //                   0
                //                   0123456789
                const string text = "V <- f().";
                VariableUpdateStatement statement = parser.Statement(rc, text, true, true) as VariableUpdateStatement;
                ContextGrabber grabber = new ContextGrabber();
                Assert.AreEqual(v, grabber.GetContext(0, statement));
                Assert.AreEqual(v, grabber.GetContext(1, statement));
                Assert.IsNull(grabber.GetContext(2, statement));
                Assert.IsNull(grabber.GetContext(3, statement));
                Assert.IsNull(grabber.GetContext(4, statement));
                Assert.AreEqual(function, grabber.GetContext(5, statement));
                Assert.AreEqual(function, grabber.GetContext(6, statement));
                Assert.AreEqual(function, grabber.GetContext(7, statement));
                Assert.AreEqual(s1, grabber.GetContext(8, statement));
                Assert.IsNull(grabber.GetContext(9, statement));
            }
        }
        /// <summary>
        ///     Ensures that the string provided is a type
        /// </summary>
        /// <param name="text"></param>
        /// <param name="instance"></param>
        /// <returns></returns>
        public override bool AdditionalCheck(string text, ModelElement instance)
        {
            bool retVal = base.AdditionalCheck(text, instance);

            if (retVal && instance != null)
            {
                Expression expression = new Parser().Expression(instance, text, IsType.INSTANCE, true, null,
                    true);
                retVal = (expression != null && expression.Ref != null) && !(expression.Ref is Function);
            }

            return retVal;
        }
        public void TestApplyStatement()
        {
            Dictionary test = CreateDictionary("Test");
            NameSpace n1 = CreateNameSpace(test, "N1");
            Structure s1 = CreateStructure(n1, "S1");
            StructureElement el1 = CreateStructureElement(s1, "E1", "Boolean");
            Structure s2 = CreateStructure(n1, "S2");
            StructureElement el2 = CreateStructureElement(s2, "E2", "S1");
            Function function = CreateFunction(n1, "f", "S1");

            Collection collection = CreateCollection(n1, "Col", "S1", 10);
            Variable v = CreateVariable(n1, "V", "Col");

            Compiler.Compile_Synchronous(true, true);

            RuleCondition rc = CreateRuleAndCondition(n1, "Rule1");
            Parser parser = new Parser();

            {
                //                   0         1         2
                //                   012345678901234567890123
                const string text = "APPLY X <- X ON V | X.";
                ApplyStatement statement = parser.Statement(rc, text, true, true) as ApplyStatement;
                Assert.IsNotNull(statement);
                ContextGrabber grabber = new ContextGrabber();
                Assert.IsNull(grabber.GetContext(0, statement));
                Assert.IsNull(grabber.GetContext(1, statement));
                Assert.IsNull(grabber.GetContext(2, statement));
                Assert.IsNull(grabber.GetContext(3, statement));
                Assert.IsNull(grabber.GetContext(4, statement));
                Assert.IsNull(grabber.GetContext(5, statement));
                Assert.AreEqual(statement.IteratorVariable, grabber.GetContext(6, statement));
                Assert.AreEqual(statement.IteratorVariable, grabber.GetContext(7, statement));
                Assert.IsNull(grabber.GetContext(8, statement));
                Assert.IsNull(grabber.GetContext(9, statement));
                Assert.IsNull(grabber.GetContext(10, statement));
                Assert.AreEqual(statement.IteratorVariable, grabber.GetContext(11, statement));
                Assert.AreEqual(statement.IteratorVariable, grabber.GetContext(12, statement));
                Assert.IsNull(grabber.GetContext(13, statement));
                Assert.IsNull(grabber.GetContext(14, statement));
                Assert.IsNull(grabber.GetContext(15, statement));
                Assert.AreEqual(v, grabber.GetContext(16, statement));
                Assert.AreEqual(v, grabber.GetContext(17, statement));
                Assert.IsNull(grabber.GetContext(18, statement));
                Assert.IsNull(grabber.GetContext(19, statement));
                Assert.AreEqual(statement.IteratorVariable, grabber.GetContext(20, statement));
                Assert.AreEqual(statement.IteratorVariable, grabber.GetContext(21, statement));
                Assert.IsNull(grabber.GetContext(22, statement));
            }
        }
        /// <summary>
        /// Checks a statement 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.Statement.Statement checkStatement(ModelElement model, string expression)
        {
            Interpreter.Statement.Statement retVal = null;

            Interpreter.Parser parser = model.EFSSystem.Parser;
            try
            {
                retVal = parser.Statement(model, expression);
                retVal.CheckStatement();
            }
            catch (Exception exception)
            {
                model.AddException(exception);
            }
            return(retVal);
        }
        public void TestUncacheFunctionBasedOnInnerFunctionCall()
        {
            Dictionary test = CreateDictionary("Test");
            NameSpace n1 = CreateNameSpace(test, "N1");
            Collection c = CreateCollection(n1, "C", "Integer", 10);
            Variable a = CreateVariable(n1, "a", "C");
            a.setDefaultValue("[1]");
            Variable b = CreateVariable(n1, "b", "C");
            b.setDefaultValue("[10]");

            Function f = CreateFunction(n1, "f", "C");
            Case always_f = CreateCase(f, "Always", "g() + h()");

            Function g = CreateFunction(n1, "g", "C");
            Case always_g = CreateCase(g, "Always", "a");

            Function h = CreateFunction(n1, "h", "C");
            Case always_h = CreateCase(h, "Always", "b");

            RuleCondition updateGlobalVariables = CreateRuleAndCondition(n1, "Update Global variables");
            Action act1 = CreateAction(updateGlobalVariables, "a <- a + [2]");
            Action act2 = CreateAction(updateGlobalVariables, "b <- b + [20]");

            Compiler.Compile_Synchronous(true);

            Expression expression = new Parser().Expression(test, "N1.f()");
            ListValue value = expression.GetExpressionValue(new InterpretationContext(), null) as ListValue;

            Assert.IsNotNull(value);
            Assert.AreEqual(2, value.Val.Count);

            // ReSharper disable once UseObjectOrCollectionInitializer
            Runner runner = new Runner(false);
            runner.CheckForCompatibleChanges = true;
            runner.Cycle();

            value = expression.GetExpressionValue(new InterpretationContext(), null) as ListValue;

            Assert.IsNotNull(value);
            Assert.AreEqual(4, value.Val.Count);
        }
        public void TestUpdateRule()
        {
            Dictionary dictionary = CreateDictionary("Test");
            NameSpace nameSpace = CreateNameSpace(dictionary, "NameSpace");

            Variable var = CreateVariable(nameSpace, "TheVariable", "Integer");
            var.setDefaultValue("0");

            RuleCondition condition1 = CreateRuleAndCondition(nameSpace, "Rule1");
            Rule rule = condition1.EnclosingRule;
            Action action = CreateAction(condition1, "TheVariable <- 1");

            Compiler.Compile_Synchronous(true);
            System.Runner = new Runner(false);

            Expression expression = new Parser().Expression(dictionary, "NameSpace.TheVariable");
            IValue value = expression.GetExpressionValue(new InterpretationContext(), null);
            Assert.AreEqual(value.LiteralName, "0");

            System.Runner.Cycle();

            value = expression.GetExpressionValue(new InterpretationContext(), null);
            Assert.AreEqual(value.LiteralName, "1");

            Dictionary update = CreateDictionary("TestUpdate");
            update.setUpdates(dictionary.Guid);
            NameSpace updNameSpace = CreateNameSpace(update, "NameSpace");
            updNameSpace.setUpdates(nameSpace.Guid);

            RuleCondition updCondition = CreateRuleAndCondition(updNameSpace, "Rule1");
            Rule updRule = updCondition.EnclosingRule;
            Action updAction = CreateAction(updCondition, "TheVariable <- 2");
            updRule.setUpdates(rule.Guid);

            System.Runner.Cycle();

            value = expression.GetExpressionValue(new InterpretationContext(), null);
            Assert.AreEqual(value.LiteralName, "2");
        }
        public void TestParameterTypeInDefaultNameSpace()
        {
            Dictionary dictionary = CreateDictionary("Test");
            NameSpace defaultNameSpace = CreateNameSpace(dictionary, "Default");
            NameSpace nameSpace = CreateNameSpace(dictionary, "N1");

            Enum enumeration = CreateEnum(defaultNameSpace, "Enum");
            EnumValue value1 = CreateEnumValue(enumeration, "First");
            EnumValue value2 = CreateEnumValue(enumeration, "Second");

            Function function = CreateFunction(nameSpace, "F1", "Boolean");

            Parameter param = new Parameter();
            param.setTypeName("Enum");
            param.setName("Value");
            function.appendParameters(param);

            Case cas1 = CreateCase(function, "Case 1", "True", "Value == Enum.First");
            Case cas2 = CreateCase(function, "Case 2", "False");

            Dictionary dictionary2 = CreateDictionary("TestUpdate");
            dictionary2.setUpdates(dictionary.Guid);

            Function updatedFunction = function.CreateFunctionUpdate(dictionary2);
            Case cas3 = (Case) updatedFunction.Cases[0];
            PreCondition preCondition = (PreCondition) cas3.PreConditions[0];
            preCondition.ExpressionText = "Value == Enum.Second";

            Compiler.Compile_Synchronous(true);

            RuleCheckerVisitor ruleChecker = new RuleCheckerVisitor(dictionary2);
            ruleChecker.visit(updatedFunction);
            Assert.IsNull(ErrorMessage(updatedFunction));

            Expression expression = new Parser().Expression(dictionary, "N1.F1(Enum.Second)");
            IValue value = expression.GetExpressionValue(new InterpretationContext(), null);
            Assert.AreEqual(System.BoolType.True, value);
        }
        public void TestFunctionCall()
        {
            Dictionary test = CreateDictionary("Test");
            NameSpace n1 = CreateNameSpace(test, "N1");
            Structure s1 = CreateStructure(n1, "S1");
            StructureElement el1 = CreateStructureElement(s1, "E1", "Boolean");
            Structure s2 = CreateStructure(n1, "S2");
            StructureElement el2 = CreateStructureElement(s2, "E2", "S1");
            Variable v = CreateVariable(n1, "V", "S1");
            v.setDefaultValue("N1.S1 { E1 => True }");
            Function function = CreateFunction(n1, "f", "S1");

            Compiler.Compile_Synchronous(true, true);

            RuleCondition rc = CreateRuleAndCondition(n1, "Rule1");
            Parser parser = new Parser();

            {
                VariableUpdateStatement statement =
                    parser.Statement(rc, "V <- f().S", true, true) as VariableUpdateStatement;
                Assert.IsNotNull(statement);
                Assert.AreEqual(statement.VariableIdentification.Ref, v);

                DerefExpression deref = statement.Expression as DerefExpression;
                Assert.IsNotNull(deref);
                Assert.AreEqual(deref.Arguments[0].Ref, s1);
            }

            {
                VariableUpdateStatement statement = parser.Statement(rc, "V <- f().", true, true) as VariableUpdateStatement;
                Assert.IsNotNull(statement);
                Assert.AreEqual(statement.VariableIdentification.Ref, v);

                DerefExpression deref = statement.Expression as DerefExpression;
                Assert.IsNotNull(deref);
                Assert.AreEqual(deref.Arguments[0].Ref, s1);
            }
        }
Example #11
0
        /// <summary>
        ///     Indicates that the expression is valid for this IExpressionable
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public bool checkValidExpression(string expression)
        {
            bool retVal = false;

            Statement tree = new Parser().Statement(this, expression, true);
            retVal = tree != null;

            return retVal;
        }
Example #12
0
        /// <summary>
        ///     Indicates that the expression is valid for this IExpressionable
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public bool checkValidExpression(string expression)
        {
            // ReSharper disable once JoinDeclarationAndInitializer
            bool retVal;

            Expression tree = new Parser().Expression(this, expression, null, false, null, true);
            retVal = tree != null;

            return retVal;
        }
        public void TestVariableAndTypeWithSameName()
        {
            Dictionary dictionary = CreateDictionary("Test");
            NameSpace nameSpace = CreateNameSpace(dictionary, "NameSpace");

            Structure structure = CreateStructure(nameSpace, "ModelElement");
            StructureElement structElem = CreateStructureElement(structure, "Value", "Boolean");
            structElem.setDefault("True");

            Variable variable = CreateVariable(nameSpace, "ModelElement", "ModelElement");
            variable.SubVariables["Value"].Value = System.BoolType.False;

            Expression expression = new Parser().Expression(dictionary, "NameSpace.ModelElement.Value");
            IValue value = expression.GetExpressionValue(new InterpretationContext(), null);

            Assert.AreEqual(value, variable.SubVariables["Value"].Value);
        }
        /// <summary>
        ///     Gets a value based on its image
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public override IValue getValue(string image)
        {
            IValue retVal = null;

            if (Char.IsLetter(image[0]) || image[0] == '_')
            {
                int lastDotPosition = image.LastIndexOf('.');
                if (lastDotPosition > 0)
                {
                    string prefix = image.Substring(0, lastDotPosition);
                    Expression typeExpression = new Parser().Expression(this, prefix,
                        IsType.INSTANCE, true, null, true);
                    if (typeExpression != null && typeExpression.Ref == this)
                    {
                        image = image.Substring(lastDotPosition + 1);
                    }
                }

                List<INamable> tmp = new List<INamable>();
                ISubDeclaratorUtils.Find(this, image, tmp);
                if (tmp.Count == 1)
                {
                    retVal = tmp[0] as IValue;
                }

                if (retVal == null)
                {
                    throw new Exception("Cannot find boolean from " + image);
                }
            }

            return retVal;
        }
        public void TestUpdateMultipleFunctions()
        {
            Dictionary dictionary = CreateDictionary("Test");
            NameSpace nameSpace = CreateNameSpace(dictionary, "N1");

            Function function = CreateFunction(nameSpace, "f", "Bool");
            Case cas1 = CreateCase(function, "Case 1", "q()");

            Function function2 = CreateFunction(nameSpace, "q", "Bool");
            Case cas2 = CreateCase(function2, "Case 1", "True");

            Dictionary dictionary2 = CreateDictionary("TestUpdate");
            dictionary2.setUpdates(dictionary.Guid);

            Function updatedFunction = function.CreateFunctionUpdate(dictionary2);
            Case cas3 = (Case) updatedFunction.Cases[0];
            cas3.ExpressionText = "NOT q(param => 3)";

            Function updatedFunction2 = function2.CreateFunctionUpdate(dictionary2);
            Parameter intParameter = CreateParameter(updatedFunction2, "param", "Integer");
            Case cas4 = (Case) updatedFunction2.Cases[0];
            cas4.ExpressionText = "False";

            Compiler.Compile_Synchronous(true);

            Expression expression = new Parser().Expression(dictionary, "N1.f()");
            IValue value = expression.GetExpressionValue(new InterpretationContext(), null);
            Assert.AreEqual(System.BoolType.True, value);
        }
        /// <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;
        }
        public void TestParameterTypeReference()
        {
            Dictionary dictionary = CreateDictionary("Test");
            NameSpace nameSpace = CreateNameSpace(dictionary, "N1");

            Enum enumeration = CreateEnum(nameSpace, "Enum");
            EnumValue value1 = CreateEnumValue(enumeration, "First");

            Function function = CreateFunction(nameSpace, "f", "Bool");

            Parameter param = new Parameter();
            param.setTypeName("N1.Enum");
            param.setName("Value");

            function.appendParameters(param);
            Case cas1 = CreateCase(function, "Case 1", "True", "Value == Enum.First");

            Dictionary dictionary2 = CreateDictionary("TestUpdate");
            dictionary2.setUpdates(dictionary.Guid);

            Function updatedFunction = function.CreateFunctionUpdate(dictionary2);
            Case cas3 = (Case) updatedFunction.Cases[0];
            cas3.ExpressionText = "False";

            Compiler.Compile_Synchronous(true);

            Expression expression = new Parser().Expression(dictionary, "N1.f(N1.Enum.First)");
            IValue value = expression.GetExpressionValue(new InterpretationContext(), null);
            Assert.AreEqual(System.BoolType.False, value);
        }
        public string format_default_message(string expression)
        {
            string retVal = "<not a structure type>";

            int index = expression.IndexOf("<-");
            if (index > 0)
            {
                string variableText = expression.Substring(0, index).Trim();
                Expression expressionTree = new Parser().Expression(Dictionary, variableText);
                if (expressionTree != null)
                {
                    Structure structureType = expressionTree.GetExpressionType() as Structure;
                    retVal = structureType.DefaultValue.LiteralName;
                }
            }

            return retVal;
        }
        public void TestListExpression()
        {
            Dictionary test = CreateDictionary("Test");
            NameSpace n1 = CreateNameSpace(test, "N1");
            Structure s1 = CreateStructure(n1, "S1");
            StructureElement el1 = CreateStructureElement(s1, "E1", "Boolean");
            Structure s2 = CreateStructure(n1, "S2");
            StructureElement el2 = CreateStructureElement(s2, "E2", "S1");
            Function function = CreateFunction(n1, "f", "S1");

            Collection collection = CreateCollection(n1, "Col", "S1", 10);
            Variable v = CreateVariable(n1, "V", "Col");

            Compiler.Compile_Synchronous(true, true);

            RuleCondition rc = CreateRuleAndCondition(n1, "Rule1");
            Parser parser = new Parser();

            {
                VariableUpdateStatement statement = parser.Statement(rc, "V <- [S1 { E1 => Tr", true, true) as VariableUpdateStatement;
                Assert.IsNotNull(statement);

                UnaryExpression unaryExpression = statement.Expression as UnaryExpression;
                Assert.IsNotNull(unaryExpression);
                ListExpression listExpression = unaryExpression.Term.LiteralValue as ListExpression;
                Assert.IsNotNull(listExpression);
                Assert.AreEqual(listExpression.ListElements.Count, 1);

                StructExpression structExpression = listExpression.ListElements[0] as StructExpression;
                Assert.IsNotNull(structExpression);
            }
        }
        /// <summary>
        ///     Handles a drop event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TimeLineControl_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent("WindowsForms10PersistentObject", false))
            {
                BaseTreeNode sourceNode = e.Data.GetData("WindowsForms10PersistentObject") as BaseTreeNode;
                if (sourceNode != null)
                {
                    VariableTreeNode variableNode = sourceNode as VariableTreeNode;
                    if (variableNode != null)
                    {
                        SubStep subStep = SubStepRelatedToMousePosition();
                        if (subStep != null)
                        {
                            // Create the default value
                            IValue value = null;
                            Expression expression = null;
                            string defaultValue = variableNode.Item.Default;
                            if (defaultValue != null)
                            {
                                const bool doSemanticalAnalysis = true;
                                const bool silent = true;
                                expression = new Parser().Expression(variableNode.Item, defaultValue,
                                    AllMatches.INSTANCE, doSemanticalAnalysis, null, silent);
                            }

                            if (expression != null)
                            {
                                InterpretationContext context = new InterpretationContext {UseDefaultValue = false};
                                value = expression.GetExpressionValue(context, null);
                            }

                            if (value == null || value is EmptyValue)
                            {
                                Structure structureType = variableNode.Item.Type as Structure;
                                if (structureType != null)
                                {
                                    const bool setDefaultValue = false;
                                    value = new StructureValue(structureType, setDefaultValue);
                                }
                            }

                            // Create the action or the expectation according to the keyboard modifier keys
                            if (value != null)
                            {
                                if ((e.KeyState & Ctrl) != 0)
                                {
                                    Expectation expectation = (Expectation) acceptor.getFactory().createExpectation();
                                    expectation.ExpressionText = variableNode.Item.FullName + " == " + value.FullName;
                                    subStep.appendExpectations(expectation);
                                }
                                else
                                {
                                    Action action = (Action) acceptor.getFactory().createAction();
                                    action.ExpressionText = variableNode.Item.FullName + " <- " + value.FullName;
                                    subStep.appendActions(action);
                                }
                            }
                            else
                            {
                                MessageBox.Show(
                                    Resources
                                        .StaticTimeLineControl_TimeLineControl_DragDrop_Cannot_evaluate_variable_default_value,
                                    Resources.StaticTimeLineControl_TimeLineControl_DragDrop_Cannot_create_event,
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                            }
                        }
                    }
                }
            }
        }
        public void TestMapExpression()
        {
            Dictionary test = CreateDictionary("Test");
            NameSpace n1 = CreateNameSpace(test, "N1");
            Structure s1 = CreateStructure(n1, "S1");
            StructureElement el1 = CreateStructureElement(s1, "E1", "Boolean");
            Structure s2 = CreateStructure(n1, "S2");
            StructureElement el2 = CreateStructureElement(s2, "E2", "S1");
            Function function = CreateFunction(n1, "f", "S1");

            Collection collection = CreateCollection(n1, "Col", "S1", 10);
            Variable v = CreateVariable(n1, "V", "Col");

            Compiler.Compile_Synchronous(true, true);

            RuleCondition rc = CreateRuleAndCondition(n1, "Rule1");
            Parser parser = new Parser();

            {
                MapExpression expression = parser.Expression(rc, "MAP V | X. USING X IN X.E2", null, true, null, true, true) as MapExpression;
                Assert.IsNotNull(expression);

                DerefExpression deref = expression.Condition as DerefExpression;
                Assert.IsNotNull(deref);

                ITypedElement element = deref.Arguments[0].Ref as ITypedElement;
                Assert.IsNotNull(element);
                Assert.AreEqual(element.Type, s1);
            }
        }
Example #22
0
        /// <summary>
        ///     Indicates that the expression is valid for this IExpressionable
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public bool checkValidExpression(string expression)
        {
            // ReSharper disable once RedundantAssignment
            bool retVal = false;

            Statement tree = new Parser().Statement(this, expression, true);
            retVal = tree != null;

            return retVal;
        }
        public void TestMapExpression()
        {
            Dictionary test = CreateDictionary("Test");
            NameSpace n1 = CreateNameSpace(test, "N1");
            Structure s1 = CreateStructure(n1, "S1");
            StructureElement el1 = CreateStructureElement(s1, "E1", "Boolean");
            Structure s2 = CreateStructure(n1, "S2");
            StructureElement el2 = CreateStructureElement(s2, "E2", "S1");
            Function function = CreateFunction(n1, "f", "S1");

            Collection collection = CreateCollection(n1, "Col", "S1", 10);
            Variable v = CreateVariable(n1, "V", "Col");

            Compiler.Compile_Synchronous(true, true);

            RuleCondition rc = CreateRuleAndCondition(n1, "Rule1");
            Parser parser = new Parser();

            {
                //                   0         1         2
                //                   012345678901234567890123456
                const string text = "MAP V | X. USING X IN X.E1";
                MapExpression expression = parser.Expression(rc, text, null, true, null, true, true) as MapExpression;
                Assert.IsNotNull(expression);
                ContextGrabber grabber = new ContextGrabber();
                Assert.IsNull(grabber.GetContext(0, expression));
                Assert.IsNull(grabber.GetContext(1, expression));
                Assert.IsNull(grabber.GetContext(2, expression));
                Assert.IsNull(grabber.GetContext(3, expression));
                Assert.AreEqual(v, grabber.GetContext(4, expression));
                Assert.AreEqual(v, grabber.GetContext(5, expression));
                Assert.IsNull(grabber.GetContext(6, expression));
                Assert.IsNull(grabber.GetContext(7, expression));
                Assert.AreEqual(expression.IteratorVariable, grabber.GetContext(8, expression));
                Assert.AreEqual(expression.IteratorVariable, grabber.GetContext(9, expression));
                Assert.IsNull(grabber.GetContext(10, expression));
                Assert.IsNull(grabber.GetContext(11, expression));
                Assert.IsNull(grabber.GetContext(12, expression));
                Assert.IsNull(grabber.GetContext(13, expression));
                Assert.IsNull(grabber.GetContext(14, expression));
                Assert.IsNull(grabber.GetContext(15, expression));
                Assert.IsNull(grabber.GetContext(16, expression));
                Assert.IsNull(grabber.GetContext(17, expression));
                Assert.IsNull(grabber.GetContext(18, expression));
                Assert.IsNull(grabber.GetContext(19, expression));
                Assert.IsNull(grabber.GetContext(20, expression));
                Assert.IsNull(grabber.GetContext(21, expression));
                Assert.AreEqual(expression.IteratorVariable, grabber.GetContext(22, expression));
                Assert.AreEqual(expression.IteratorVariable, grabber.GetContext(23, expression));
                Assert.AreEqual(el1, grabber.GetContext(24, expression));
                Assert.AreEqual(el1, grabber.GetContext(25, expression));
                Assert.AreEqual(el1, grabber.GetContext(26, expression));
                Assert.IsNull(grabber.GetContext(27, expression));
            }
        }
        public void TestStructureDereference()
        {
            Dictionary dictionary = CreateDictionary("Test");
            NameSpace nameSpace = CreateNameSpace(dictionary, "N1");
            Structure structure = CreateStructure (nameSpace, "Struct");
            StructureElement el1 = CreateStructureElement (structure, "e1", "Boolean");
            Function f = CreateFunction (nameSpace, "f", "Struct");
            Variable v = CreateVariable (nameSpace, "v", "Struct");
            Variable v2 = CreateVariable(nameSpace, "v2", "Boolean");

            Parser parser = new Parser ();
            {
                DerefExpression expression = parser.Expression (nameSpace, "N1.v.e1") as DerefExpression;
                Assert.IsNotNull (expression);
                Assert.AreEqual(3, expression.Arguments.Count);
                Assert.AreEqual(v, expression.Arguments[1].Ref);
                Assert.AreEqual (el1, expression.Arguments [2].Ref);
            }
            {
                DerefExpression expression = parser.Expression (nameSpace, "N1.f().e1") as DerefExpression;
                Assert.IsNotNull (expression);
                Assert.AreEqual (2, expression.Arguments.Count);
                Assert.AreEqual (structure, expression.Arguments [0].Ref);
                Assert.AreEqual (el1, expression.Arguments [1].Ref);
            }
            {
                DerefExpression expression = parser.Expression(nameSpace, "N1.Struct.e1") as DerefExpression;
                Assert.IsNotNull(expression);
                Assert.AreEqual(3, expression.Arguments.Count);
                Assert.AreEqual(structure, expression.Arguments[1].Ref);
                Assert.AreEqual(el1, expression.Arguments[2].Ref);
            }

            RuleCondition condition = CreateRuleAndCondition (nameSpace, "Test");
            Action action1 = CreateAction (condition, "Struct.e1 <- True");
            Action action2 = CreateAction(condition, "v2 <- Struct.e1");
            Action action3 = CreateAction(condition, "f().e1 <- True");
            Action action4 = CreateAction(condition, "v2 <- f().e1");

            Collection collection = CreateCollection (nameSpace, "Col", "Struct", 10);
            Variable v3 = CreateVariable (nameSpace, "v3", "Col");
            Action action5 = CreateAction (condition, "(FIRST X IN v3).e1 <- True");

            RuleCondition ruleCondition2 = CreateRuleAndCondition(structure, "Rule");
            Action action6 = CreateAction (ruleCondition2, "THIS.e1 <- True");
            Action action7 = CreateAction(ruleCondition2, "v2 <- THIS.e1");

            RuleCheckerVisitor visitor = new RuleCheckerVisitor(dictionary);
            visitor.visit(nameSpace);

            Assert.True(HasMessagePart(action1, "structure should not be used to reference an instance"));
            Assert.True(HasMessagePart(action2, "structure should not be used to reference an instance"));
            Assert.False(HasMessagePart(action3, "structure should not be used to reference an instance"));
            Assert.False(HasMessagePart(action4, "structure should not be used to reference an instance"));
            Assert.False(HasMessagePart(action5, "structure should not be used to reference an instance"));
            Assert.False(HasMessagePart(action6, "structure should not be used to reference an instance"));
            Assert.False(HasMessagePart(action7, "structure should not be used to reference an instance"));
        }
        /// <summary>
        /// Parses the current statement or expression and returns the interpreter tree node
        /// </summary>
        /// <param name="text">The text to parse</param>
        /// <returns></returns>
        private InterpreterTreeNode Parse(string text)
        {
            InterpreterTreeNode retVal = null;

            if (!String.IsNullOrEmpty(text))
            {
                Parser parser = new Parser();
                retVal = parser.Statement(Instance as ModelElement, EditionTextBox.Text, true, true);
                if (retVal == null)
                {
                    retVal = parser.Expression(Instance as ModelElement, text, AllMatches.INSTANCE, true, null, true, true);
                }
            }

            return retVal;
        }
        public void TestUpdateRemoved()
        {
            Dictionary dictionary = CreateDictionary("Test");
            NameSpace nameSpace = CreateNameSpace(dictionary, "N1");
            Function function = CreateFunction(nameSpace, "f", "Bool");
            Case cas1 = CreateCase(function, "Case 1", "q()");

            Function function2 = CreateFunction(nameSpace, "q", "Bool");
            Case cas2 = CreateCase(function2, "Case 1", "True");

            Dictionary dictionary2 = CreateDictionary("TestUpdate");
            dictionary2.setUpdates(dictionary.Guid);

            Function updatedFunction = function.CreateFunctionUpdate(dictionary2);
            updatedFunction.setIsRemoved(true);

            Compiler.Compile_Synchronous(true);

            Expression expression = new Parser().Expression(dictionary, "N1.f()");

            Assert.AreEqual(Utils.ModelElement.Errors.Count, 1);
        }
        /// <summary>
        ///     Applies this step activation be registering it in the activation cache
        /// </summary>
        /// <param name="runner"></param>
        public override void Apply(Runner runner)
        {
            base.Apply(runner);

            TimeLine.SubStepActivationCache[SubStep] = this;
            foreach (VariableUpdate update in Updates)
            {
                TimeLine.AddModelEvent(update, runner, true);
            }

            // Store the step corresponding expectations
            foreach (Expectation expectation in subStep.Expectations)
            {
                bool addExpectation = true;

                if (expectation.getKind() == acceptor.ExpectationKind.aInstantaneous)
                {
                    if (!String.IsNullOrEmpty(expectation.getCondition()))
                    {
                        Expression expression = new Parser().Expression(expectation,
                            expectation.getCondition());
                        BoolValue value =
                            expression.GetExpressionValue(new InterpretationContext(expectation), null) as BoolValue;
                        if (value != null)
                        {
                            addExpectation = value.Val;
                        }
                        else
                        {
                            throw new Exception("Cannot evaluate " + expectation.getCondition() + " as a boolean value");
                        }
                    }
                }

                if (addExpectation)
                {
                    TimeLine.AddModelEvent(new Expect(expectation, runner.CurrentPriority), runner, true);
                }
            }
        }
        /// <summary>
        ///     Parses the image and provides the corresponding value
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public override IValue getValue(string image)
        {
            IValue retVal = null;

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

            return retVal;
        }
Example #29
0
        /// <summary>
        ///     Allows to refresh the view, when the value of a model changed
        /// </summary>
        /// <param name="modelElement"></param>
        /// <param name="changeKind"></param>
        /// <returns>True if the view should be refreshed</returns>
        public override bool HandleValueChange(IModelElement modelElement, Context.ChangeKind changeKind)
        {
            bool retVal = base.HandleValueChange(modelElement, changeKind);

            if (retVal)
            {
                if (Variable != null)
                {
                    Expression expression = new Parser().Expression(
                        EnclosingFinder<Dictionary>.find(Variable), Variable.FullName);
                    IVariable variable = expression.GetVariable(new InterpretationContext());
                    if (variable != Variable)
                    {
                        SetVariable(variable);
                    }
                    else
                    {
                        structureTreeListView.RefreshObject(Variable);
                        structureTreeListView.Refresh();
                    }
                }
            }

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

            if (image.Length > 0 && (Char.IsLetter(image[0]) || image[0] == '_'))
            {
                int lastDotPosition = image.LastIndexOf('.');
                if (lastDotPosition > 0)
                {
                    string prefix = image.Substring(0, lastDotPosition);
                    Expression typeExpression = new Parser().Expression(this, prefix,
                        IsType.INSTANCE, true, null, true);
                    if (typeExpression != null && typeExpression.Ref == this)
                    {
                        image = image.Substring(lastDotPosition + 1);
                    }
                }

                retVal = findEnumValue(image);
                if (retVal == null)
                {
                    AddError("Cannot create range value from " + image);
                }
            }
            else
            {
                try
                {
                    switch (getPrecision())
                    {
                        case acceptor.PrecisionEnum.aIntegerPrecision:
                        {
                            Decimal val = Decimal.Parse(image);
                            Decimal min = MinValueAsLong;
                            Decimal max = MaxValueAsLong;
                            if (val >= min && val <= max)
                            {
                                retVal = new IntValue(this, val);
                            }
                        }
                            break;

                        case acceptor.PrecisionEnum.aDoublePrecision:
                        {
                            double val = getDouble(image);
                            double min = MinValueAsDouble;
                            double max = MaxValueAsDouble;
                            if (val >= min && val <= max)
                            {
                                retVal = new DoubleValue(this, val);
                            }
                            break;
                        }
                    }
                }
                catch (Exception exception)
                {
                    AddException(exception);
                }
            }

            return retVal;
        }
        private void openStructureEditorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bool dialogShown = false;
            ExplanationPart part = Instance as ExplanationPart;
            if (part != null)
            {
                IValue value = part.RightPart as IValue;
                if (value != null)
                {
                    StructureEditor.Window window = new StructureEditor.Window();
                    window.SetModel(value);
                    window.ShowDialog();
                    dialogShown = true;
                }

                Action action = part.Element as Action;
                if (!dialogShown && action != null)
                {
                    VisitStatement(action.Statement);
                    dialogShown = true;
                }

                Expectation expectation = part.Element as Expectation;
                if (!dialogShown && expectation != null)
                {
                    VisitExpression(expectation.Expression);
                    dialogShown = true;
                }
            }

            if (!dialogShown)
            {
                const bool doSemanticalAnalysis = true;
                const bool silent = true;
                ModelElement root = Instance as ModelElement;
                if (root == null)
                {
                    root = EfsSystem.Instance.Dictionaries[0];
                }

                string text = EditionTextBox.Text;
                Expression expression = new Parser().Expression(root, text, AllMatches.INSTANCE,
                    doSemanticalAnalysis, null, silent);
                if (expression != null)
                {
                    expression = VisitExpression(expression);
                    EditionTextBox.Text = expression.ToString();
                }

                Statement statement = new Parser().Statement(root, text, silent);
                if (statement != null)
                {
                    statement = VisitStatement(statement);
                    EditionTextBox.Text = statement.ToString();
                }
            }
        }
Example #32
0
        /// <summary>
        ///     Indicates that the expression is valid for this IExpressionable
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public bool checkValidExpression(string expression)
        {
            bool retVal = false;

            Expression tree = new Parser().Expression(this, expression, null, false, null, true);
            retVal = tree != null;

            return retVal;
        }