public static ContextManager GetInstance() {
   if (manager == null) {
     manager = new ContextManager();
   }
   return manager;
 }
Exemplo n.º 2
0
        public void LoadFile(string path)
        {
            try {
                var name = Path.GetFileNameWithoutExtension(path);

                // Check Grammar cache
                SpeechGrammar grammar = FindGrammar(name);
                if (null != grammar)
                {
                    if (grammar.LastModified == File.GetLastWriteTime(path))
                    {
                        Log("Ignoring: " + name + " (no changes)"); return;
                    }
                }

                // New Grammar
                bool addToCache = false;
                if (null == grammar)
                {
                    grammar    = new SpeechGrammar(name);
                    addToCache = true;
                }

                // Load XML
                string xml = File.ReadAllText(path, Encoding.UTF8);
                if (!LoadXML(grammar, xml))
                {
                    return;
                }

                // Add new grammar to cache
                if (addToCache)
                {
                    Cache.Add(name, grammar);
                }

                // Setup grammar
                grammar.Path         = path;
                grammar.LastModified = File.GetLastWriteTime(path);

                // Check lazy
                grammar.Enabled = true;
                if ((path.IndexOf("lazy") >= 0) || Regex.IsMatch(xml, "root=\"lazy\\w+\"", RegexOptions.IgnoreCase))
                {
                    grammar.Enabled = false;
                }

                // Check context
                var ctx = ConfigManager.GetInstance().Find("speech.grammar.context", "");
                if (!String.IsNullOrEmpty(ctx))
                {
                    var context = ctx.Split('|');
                    if (context.Length > 0 && !Array.Exists(context, delegate(object s) { return(s.Equals(name)); }))
                    {
                        grammar.Enabled = false;
                    }
                }

                // Log
                Log("Loading: " + name + " (" + grammar.Enabled + ")" + " (" + path + ")");

                // Store default context
                if (grammar.Enabled)
                {
                    ContextManager.GetInstance().Default.Add(name);
                    Log("Add to context list: " + name);
                }
            }
            catch (Exception ex) {
                Error(ex);
            }
        }