Exemple #1
0
        private StateNode parseNode(XmlElement node, Declarations decls)
        {
            //Console.WriteLine("    Parsing node ...");

            string id = node.GetAttribute("id");

            if (id == null)
            {
                throw new ParseException("Expected id in <location>");
            }

            XmlNode namenode = node.SelectSingleNode("./name");
            string  name     = string.IsNullOrEmpty(getElementText(namenode))
                ? String.Format("Unnamed{0}", _noname++)
                : getElementText(namenode);

            NodeMode mode = NodeMode.Regular;

            if (node.SelectSingleNode("./commited") != null)
            {
                mode = NodeMode.Commited;
            }
            else
            if (node.SelectSingleNode("./urgent") != null)
            {
                mode = NodeMode.Commited;
            }

            StateNode sn = new StateNode(name, id, mode);

            sn.Rules = parseRules(node, sn.Name, decls);
            return(sn);
        }
 // add paramters later
 public Template(string name, Declarations declarations, StateNode initial, StateNode[] nodes, StateTransition[] transitions)
 {
     Name         = name;
     Declarations = declarations;
     Initial      = initial;
     Nodes        = nodes;
     Transitions  = transitions;
 }
Exemple #3
0
        private static string getModeString(StateNode sn)
        {
            switch (sn.Mode)
            {
            case NodeMode.Regular:
                return("U2C_StateNormal");

            case NodeMode.Urgent:
                return("U2C_StateUrgent");

            case NodeMode.Commited:
                return("U2C_StateCommited");
            }
            throw new CodeGenException("Unknown mode!");
        }
Exemple #4
0
        private void FormatNode(Template t, StateNode sn)
        {
            string name = setUniqueName(sn, String.Format("{0}_STATE_{1}", t.Name, sn.Name));

            if (_isSource)
            {
                string guard_name = FormatGuardFunction(name, t.Declarations, sn.Rules);

                app("const U2C_TRANSITION* {0}_OUT[] = ", name);
                app("    {{");
                foreach (StateTransition st in t.Transitions.Where(x => x.Source == sn))
                {
                    app("        &{0}, // {1}", getUniqueName(st), st.Name);
                }
                app("    NULL");
                app("    }};");
                app();

                app("const U2C_STATENODE_DATA {0}_DATA =", name);
                app("    {{");
                app("        DBG_FIELD(\"{0}\")	// name", sn.Name);
                app("        (U2C_TRANSITION* const*)&{0}_OUT,	// target", name);
                app("        {0}, // mode", getModeString(sn));
                app("        {0}, // guard", guard_name);
                app("    }};");
                app();
                app("U2C_STATENODE {0} =", name);
                app("    {{");
                app("        &{0}_DATA,         // data", name);
                app("        NULL,              // state_enter");
                app("        NULL,              // state_leave");
                app("        NULL,              // context");
                app("    }};");
                app();
            }
            else
            {
                app("extern U2C_STATENODE {0};", name);
            }
        }
Exemple #5
0
        public StateTransition(string tmplname, StateNode src, StateNode tgt, String name = null)
        {
            int    id    = 0;
            string names = String.Format("{0}__{1}", src.BaseName, tgt.BaseName);

            string fullname = tmplname + "_" + names;

            _idCounter.TryGetValue(fullname, out id);
            _idCounter[fullname] = id + 1;

            if (name != null)
            {
                BaseName = name;
            }
            else
            {
                BaseName = String.Format("{0}", id);
            }

            Name   = String.Format("{0}__{1}", names, BaseName);
            Source = src;
            Target = tgt;
        }
Exemple #6
0
        private Template parseTemplate(XmlElement node, Declarations globaldecls)
        {
            try
            {
                TemplateState state = new TemplateState();
                XmlNodeList   subnodes;

                XmlNode namenode = node.SelectSingleNode("./name");
                if (namenode == null || string.IsNullOrEmpty(getElementText(namenode)))
                {
                    throw new ParseException("No name node!");
                }
                state._name = getElementText(namenode);
                if (_templates.Count > 0 && !_templates.Contains(state._name))
                {
                    return(null);
                }

                Console.WriteLine("Parsing template: " + state._name);

                XmlNode decnode = node.SelectSingleNode("./declaration");
                if (decnode != null)
                {
                    state._declarations = parseDeclarations(getElementText(decnode), globaldecls);
                }
                else
                {
                    state._declarations = new Declarations()
                    {
                        Parent = globaldecls
                    }
                };

                subnodes = node.SelectNodes("./location");
                if (subnodes != null)
                {
                    foreach (XmlNode subnode in subnodes)
                    {
                        StateNode sn = parseNode((XmlElement)subnode, state._declarations);
                        state._nodes.Add(sn);
                    }
                }
                subnodes = node.SelectNodes("./transition");
                if (subnodes != null)
                {
                    foreach (XmlNode subnode in subnodes)
                    {
                        StateTransition st = parseTransition((XmlElement)subnode, state);
                        state._transitions.Add(st);
                    }
                }

                XmlNode initnode = node.SelectSingleNode("./init");
                if (initnode == null || string.IsNullOrEmpty(((XmlElement)initnode).GetAttribute("ref")))
                {
                    throw new ParseException("No init node!");
                }

                state._init = findNode(((XmlElement)initnode).GetAttribute("ref"), state);
                if (state._init == null)
                {
                    throw new ParseException("Init node not found");
                }

                XmlNode parameter = node.SelectSingleNode("./parameter");
                if (parameter != null)
                {
                    throw new ParseException("template parameters not yet supported, easy to add if needed!");
                }

                return(new Template(state._name, state._declarations, state._init, state._nodes.ToArray(), state._transitions.ToArray()));
            }
            catch (SelectNotSupportException)
            {
                return(null); // for now
            }
        }
Exemple #7
0
        private StateTransition parseTransition(XmlElement node, TemplateState state)
        {
            //Console.WriteLine("    Parsing transition ...");

            string id = node.GetAttribute("id");

            if (id == null)
            {
                throw new ParseException("Expected id in <location>");
            }

            XmlNode source = node.SelectSingleNode("./source");

            if (source == null)
            {
                throw new ParseException("Expected source in <location>");
            }
            XmlNode target = node.SelectSingleNode("./target");

            if (target == null)
            {
                throw new ParseException("Expected source in <location>");
            }
            string srcid = ((XmlElement)source).GetAttribute("ref");

            if (string.IsNullOrEmpty(srcid))
            {
                throw new ParseException("No source id for transition");
            }
            string tgtid = ((XmlElement)target).GetAttribute("ref");

            if (string.IsNullOrEmpty(tgtid))
            {
                throw new ParseException("No target id for transition");
            }

            StateNode tgtnode = findNode(tgtid, state);

            if (tgtnode == null)
            {
                throw new ParseException("Target node not found for transition");
            }

            StateNode srcnode = findNode(srcid, state);

            if (srcnode == null)
            {
                throw new ParseException("Source node not found for transition");
            }

            string      name      = null;
            XmlNodeList namenodes = node.SelectNodes("./label[@kind='comments']");

            if (namenodes != null)
            {
                XmlNode namenode = (from XmlNode n in namenodes select n).SingleOrDefault(n => getElementText(n).Contains("{name="));
                if (namenode != null)
                {
                    string txt = getElementText(namenode);
                    int    pos = txt.IndexOf("{name=");
                    txt = txt.Substring(pos + 6);
                    pos = txt.IndexOf("}");
                    if (pos > 0)
                    {
                        name = txt.Substring(0, pos);
                    }
                }
            }
            StateTransition st = new StateTransition(state._name, srcnode, tgtnode, name);

            st.Rules = parseRules(node, st.Name, state._declarations);
            return(st);
        }