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);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Parses the statement provided
 /// </summary>
 /// <param name="root">the root element for which this statement is created</param>
 /// <param name="expression"></param>
 /// <returns></returns>
 public Interpreter.Statement.Statement ParseStatement(ModelElement root, string expression)
 {
     try
     {
         return(Parser.Statement(root, expression));
     }
     catch (Interpreter.ParseErrorException exception)
     {
         root.AddException(exception);
         return(null);
     }
 }
        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);
            }
        }
        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 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));
            }
        }
        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>
        /// 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;
        }
Exemplo n.º 9
0
        /// <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>
        public Statement CheckStatement(ModelElement model, string expression)
        {
            Statement retVal = null;

            // Only check statements for model elements which are not updated
            if (!ParentHasBeenUpdated(model))
            {
                Parser parser = new Parser();
                try
                {
                    retVal = parser.Statement(model, expression);
                    if (retVal != null)
                    {
                        retVal.CheckStatement();
                    }
                    else
                    {
                        model.AddError("Cannot parse statement");
                    }
                }
                catch (Exception exception)
                {
                    model.AddException(exception);
                }
            }

            return retVal;
        }