Beispiel #1
0
        /// <summary>
        /// Parse a HyXML formatted file
        ///
        /// Notes:
        /// 1) Assumes guards and invariants are formatted as infix strings (e.g., 5 + x >= y is a valid guard or invariant, whereas (>= (+ 5 x) y) is not)
        /// 2) Assumes resets are formatted as: varToReset' = expr
        /// 3) Assumes differential inclusions are formatted as: x1_dot >= a and x1_dot <= b; more generally, any relations are okay, we just don't support x1_dot \in [a, b]
        ///
        /// 1) Eventually modify HyLink to generate HyXML using MathML for guards, invariants, and resets
        /// </summary>
        /// <param name="path"></param>
        public static void ParseInputFile(String path)
        {
            Controller.Instance.Sys = new Holism();
            ConcreteHybridAutomaton h = null;
            Transition       t        = null;
            ConcreteLocation l        = null;
            ElementNames     n        = ElementNames.parameter;

            if (!File.Exists(path))
            {
                throw new FileNotFoundException("Input file not found: " + path);
            }

            XmlTextReader reader = new XmlTextReader(path);

            while (reader.Read())
            {
                try
                {
                    switch (reader.NodeType)
                    {
                    case XmlNodeType.Element:
                    {
                        n = (ElementNames)(Enum.Parse(typeof(ElementNames), reader.Name, true));
                        switch (n)
                        {
                        case ElementNames.parameter:
                        {
                            Variable p = ParseVariableParameter(reader);
                            break;
                        }

                        case ElementNames.predicate:
                        {
                            break;
                        }

                        // since properties can refer to elements of the automata, we save their strings and parse them after all automata have been fully constructed
                        case ElementNames.property:
                        {
                            ParseProperty(reader);
                            break;
                        }

                        case ElementNames.assumption:
                        {
                            ParseAssumption(reader);
                            break;
                        }

                        case ElementNames.action:
                        {
                            ParseReset(reader, t);
                            break;
                        }

                        case ElementNames.automaton:
                        {
                            String name = reader.GetAttribute(AutomatonAttributes.name.ToString());
                            h = new ConcreteHybridAutomaton(Controller.Instance.Sys, name);
                            break;
                        }

                        case ElementNames.dai:
                        {
                            ParseDAI(reader, h, l);
                            break;
                        }

                        case ElementNames.guard:
                        {
                            ParseGuard(reader, t);
                            break;
                        }

                        case ElementNames.uguard:
                        {
                            if (t == null)
                            {
                                throw new System.Exception("Error parsing transition: transition not specified properly before reaching universally quantified guard.");
                            }

                            String uguard = reader.GetAttribute(GuardAttributes.equn.ToString());
                            if (uguard.Length > 0)
                            {
                                Antlr.Runtime.Tree.CommonTree tmptree = math.Expression.Parse(uguard);
                                t.UGuard = LogicalExpression.CreateTerm(tmptree);
                            }

                            break;
                        }

                        case ElementNames.initial:
                        {
                            if (h == null)
                            {
                                throw new System.Exception("Error parsing initial: automaton not specified properly before reaching initial.");
                            }

                            String init = reader.GetAttribute(InitialAttributes.equn.ToString());
                            if (init.Length > 0)
                            {
                                h.InitialString = init;
                            }

                            break;
                        }

                        case ElementNames.invariant:
                        {
                            if (l == null)
                            {
                                throw new System.Exception("Error parsing transition: location not specified properly before reaching invariant.");
                            }

                            String inv = reader.GetAttribute(InvariantAttributes.equn.ToString());
                            if (inv.Length > 0)
                            {
                                Antlr.Runtime.Tree.CommonTree tmptree = math.Expression.Parse(inv);
                                l.Invariant = LogicalExpression.CreateTerm(tmptree);
                            }

                            break;
                        }

                        case ElementNames.stop:
                        {
                            if (l == null)
                            {
                                throw new System.Exception("Error parsing transition: location not specified properly before reaching stopping condition.");
                            }

                            String stop = reader.GetAttribute(StopAttributes.equn.ToString());
                            if (stop.Length > 0)
                            {
                                Antlr.Runtime.Tree.CommonTree tmptree = math.Expression.Parse(stop);
                                l.Stop = LogicalExpression.CreateTerm(tmptree);
                            }

                            break;
                        }

                        case ElementNames.mode:
                        {
                            l = ParseLocation(reader, h);
                            break;
                        }

                        case ElementNames.transition:
                        {
                            t = ParseTransition(reader, h, l);
                            break;
                        }

                        case ElementNames.variable:
                        {
                            ParseVariable(reader, h);
                            break;
                        }

                        case ElementNames.holism:
                        {
                            // todo
                            break;
                        }

                        default:
                        {
                            throw new Exception("Bad element specified (uncrecognized).");
                            break;
                        }
                        }
                        break;
                    }

                    // destroy temporary arguments
                    case XmlNodeType.EndElement:
                    {
                        n = (ElementNames)(Enum.Parse(typeof(ElementNames), reader.Name, true));
                        switch (n)
                        {
                        case ElementNames.parameter:
                        {
                            break;
                        }

                        case ElementNames.action:
                        {
                            break;
                        }

                        case ElementNames.automaton:
                        {
                            h.finishConstruction();                 // finish building the concrete automaton: create equations for initial conditions, assert constraints on locations, etc.
                            Controller.Instance.Sys.addHybridAutomaton(h);

                            foreach (var v in Controller.Instance.Sys.Variables)
                            {
                                if (v.UpdateType == Variable.VarUpdateType.discrete && v.Type == Variable.VarType.index)
                                {
                                    Controller.Instance.Z3.Assumptions.Add(Controller.Instance.Z3.MkLe((ArithExpr)Controller.Instance.IntZero, (ArithExpr)Controller.Instance.GlobalVariables[v.Name]));
                                    Controller.Instance.Z3.Assumptions.Add(Controller.Instance.Z3.MkLe((ArithExpr)Controller.Instance.GlobalVariables[v.Name], (ArithExpr)Controller.Instance.Params["N"]));
                                    Controller.Instance.Z3.Assumptions.Add(Controller.Instance.Z3.MkLe((ArithExpr)Controller.Instance.IntZero, (ArithExpr)Controller.Instance.GlobalVariablesPrimed[v.Name]));
                                    Controller.Instance.Z3.Assumptions.Add(Controller.Instance.Z3.MkLe((ArithExpr)Controller.Instance.GlobalVariablesPrimed[v.Name], (ArithExpr)Controller.Instance.Params["N"]));
                                }
                            }
                            h = null;
                            break;
                        }

                        case ElementNames.dai:
                        {
                            break;
                        }

                        case ElementNames.guard:
                        {
                            break;
                        }

                        case ElementNames.invariant:
                        {
                            break;
                        }

                        case ElementNames.mode:
                        {
                            ParseHyXML.ParseLocationEnd(reader, h, l);
                            l = null;
                            break;
                        }

                        case ElementNames.transition:
                        {
                            //l.addTransition(t);
                            // todo: search the locations by id to find the prestate and poststate locations
                            t = null;
                            break;
                        }

                        case ElementNames.variable:
                        {
                            break;
                        }

                        default:
                        {
                            break;
                        }
                        }

                        break;
                    }

                    default:
                    {
                        break;
                    }
                    }
                }
                catch (NoViableAltException ex)
                {
                    System.Console.WriteLine("ERROR: parser error");
                    System.Console.WriteLine("Line " + reader.LineNumber + " position " + reader.LinePosition + " element name " + reader.Name);
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine("ERROR: parser error");
                    System.Console.WriteLine("Line " + reader.LineNumber + " position " + reader.LinePosition + " element name " + reader.Name);
                }
            }
            reader.Close();
        }
Beispiel #2
0
        public void ToConcreteTest()
        {
            uint N;
            uint TN;

            N  = 0;
            TN = 0;
            List <SymmetricType> types = new List <SymmetricType>();

            types.Add(new SymmetricType(TN, Controller.Instance.Z3.MkTrue()));
            SymmetricState target   = new SymmetricState(N, types.ToArray());
            Expr           expected = null;
            Expr           actual;

            actual = target.ToConcrete();
            //Assert.AreEqual(expected, actual);

            N     = 1;
            TN    = 1;
            types = new List <SymmetricType>();


            Holism sys = new Holism();
            ConcreteHybridAutomaton h = new ConcreteHybridAutomaton(sys, "test");
            ConcreteLocation        aloc;
            ConcreteLocation        bloc;

            StringReader  tr;
            XmlTextReader reader;

            tr     = new StringReader("<holism><mode id='0' name='aloc' initial='True'></mode><mode id='0' name='bloc' initial='False'></mode></holism>");
            reader = new XmlTextReader(tr);
            reader.Read();
            reader.Read();
            aloc = ParseHyXML.ParseLocation(reader, h);
            reader.Read();
            ParseHyXML.ParseLocationEnd(reader, h, aloc);
            reader.Read();
            bloc = ParseHyXML.ParseLocation(reader, h);
            reader.Read();
            ParseHyXML.ParseLocationEnd(reader, h, bloc);

            expected = LogicalExpression.CreateTerm("q[1] == aloc");
            types.Add(new SymmetricType(TN, LogicalExpression.CreateTerm("q[i] == aloc")));
            target = new SymmetricState(N, types.ToArray());
            actual = target.ToConcrete();
            //expected = expected.Substitute(Controller.Instance.Indices["i"], Controller.Instance.Z3.MkInt(1));
            //Assert.AreEqual(expected, actual);
            Assert.IsTrue(Controller.Instance.Z3.ProveEqual(actual, expected));

            expected = LogicalExpression.CreateTerm("q[1] == aloc and q[2] == aloc");
            types    = new List <SymmetricType>();
            types.Add(new SymmetricType(2, LogicalExpression.CreateTerm("q[i] == aloc")));
            target = new SymmetricState(2, types.ToArray());
            actual = target.ToConcrete();
            Assert.IsTrue(Controller.Instance.Z3.ProveEqual(actual, expected));

            expected = LogicalExpression.CreateTerm("q[1] == aloc and q[2] == aloc and q[3] == aloc");
            types    = new List <SymmetricType>();
            types.Add(new SymmetricType(3, LogicalExpression.CreateTerm("q[i] == aloc")));
            target = new SymmetricState(3, types.ToArray());
            actual = target.ToConcrete();
            Assert.IsTrue(Controller.Instance.Z3.ProveEqual(actual, expected));


            //target.visit(0, 3);


            List <uint> ids = new List <uint>();

            ids.Add(1);
            ids.Add(2);
            ids.Add(3);
            ids.Add(4);

            var perms = SymHelp.Permutations(ids);

            //ids.Add(5);
            //ids.Add(6);

            /*
             * IEnumerable<uint> t1 = SymmetricState.Combinations(ids, 1);
             * String s = "";
             * foreach (var v in t1)
             * {
             *  s += v + ", ";
             * }
             * IEnumerable<uint> t2 = SymmetricState.Combinations(ids, 2);
             * foreach (var v in t2)
             * {
             *  s += v + ", ";
             * }
             * IEnumerable<uint> t3 = SymmetricState.Combinations(ids, 3);
             * foreach (var v in t3)
             * {
             *  s += v + ", ";
             * }
             * IEnumerable<uint> t4 = SymmetricState.Combinations(ids, 4);
             * foreach (var v in t4)
             * {
             *  s += v + ", ";
             * }*/

            /*
             * uint T1 = 2;
             * uint T2 = 2;
             * uint T3 = (uint)(ids.Count - (T1 + T2));
             * var ids_t1 = SymHelp.Combinations(ids, (int)T1);
             * //s = "";
             * string s = "";
             * foreach (var v in ids_t1)
             * {
             *  List<uint> vt = new List<uint>(v);
             *  var ids_t2 = SymHelp.Combinations(ids.FindAll(id => !vt.Contains(id)), (int)T2);
             *
             *
             *  //foreach (var stmp in v)
             *  //{
             * //      s += stmp;
             * //  }
             * //  s += ", ";
             *
             *  foreach (var v2 in ids_t2)
             *  {
             *      List<uint> vt2 = new List<uint>(v2);
             *      var ids_t3 = SymHelp.Combinations(ids.FindAll(id => !vt.Contains(id) && !vt2.Contains(id)), (int)T3);
             *
             *      foreach (var v3 in ids_t3)
             *      {
             *          List<uint> idset = new List<uint>(v);
             *          idset.AddRange(v2);
             *          idset.AddRange(v3);
             *
             *          foreach (var stmp in idset)
             *          {
             *              s += stmp;
             *          }
             *          s += ", ";
             *      }
             *  }
             * }*/
            //IEnumerable<uint> combinations = ;


            expected = LogicalExpression.CreateTerm("(q[1] == aloc and q[2] == bloc) or (q[1] == bloc and q[2] == aloc)");
            types    = new List <SymmetricType>();
            types.Add(new SymmetricType(1, LogicalExpression.CreateTerm("q[i] == aloc")));
            types.Add(new SymmetricType(1, LogicalExpression.CreateTerm("q[i] == bloc")));
            target = new SymmetricState(2, types.ToArray());
            actual = target.ToConcrete();
            Assert.IsTrue(Controller.Instance.Z3.ProveEqual(actual, expected));
        }