Exemple #1
0
        /**
         * Parses the given RuleName into a network of GrammarNodes.
         *
         * @param initialRuleName
         *            the RuleName rule to parse
         * @return a grammar graph
         */
        private GrammarGraph ProcessRuleName(JSGFRuleName initialRuleName)
        {
            this.LogInfo("parseRuleName: " + initialRuleName);
            GrammarGraph result = RuleStack.Contains(initialRuleName.GetRuleName());

            if (result != null)
            { // its a recursive call
                return(result);
            }
            else
            {
                result = new GrammarGraph(this);
                RuleStack.Push(initialRuleName.GetRuleName(), result);
            }
            JSGFRuleName ruleName = _ruleGrammar.Resolve(initialRuleName);

            if (ruleName == JSGFRuleName.Null)
            {
                result.StartNode.Add(result.EndNode, 0.0f);
            }
            else if (ruleName == JSGFRuleName.Void)
            {
                // no connection for void
            }
            else
            {
                if (ruleName == null)
                {
                    throw new JSGFGrammarException("Can't resolve "
                                                   + initialRuleName + " g "
                                                   + initialRuleName.GetFullGrammarName());
                }
                JSGFRuleGrammar rg = Manager.RetrieveGrammar(ruleName
                                                             .GetFullGrammarName());
                if (rg == null)
                {
                    throw new JSGFGrammarException("Can't resolve grammar name "
                                                   + ruleName.GetFullGrammarName());
                }

                JSGFRule rule = rg.GetRule(ruleName.GetSimpleRuleName());
                if (rule == null)
                {
                    throw new JSGFGrammarException("Can't resolve rule: "
                                                   + ruleName.GetRuleName());
                }
                GrammarGraph ruleResult = ProcessRule(rule);
                if (result != ruleResult)
                {
                    result.StartNode.Add(ruleResult.StartNode, 0.0f);
                    ruleResult.EndNode.Add(result.EndNode, 0.0f);
                }
            }
            RuleStack.Pop();
            return(result);
        }
Exemple #2
0
        /**
         * Commit changes to all loaded grammars and all changes of grammar since
         * the last commitChange
         *
         * @throws JSGFGrammarParseException
         * @throws JSGFGrammarException
         */
        public virtual void CommitChanges()
        {
            try
            {
                if (LoadGrammar)
                {
                    if (Manager == null)
                    {
                        GetGrammarManager();
                    }
                    _ruleGrammar = LoadNamedGrammar(GrammarName);
                    LoadImports(_ruleGrammar);
                    LoadGrammar = false;
                }

                Manager.LinkGrammars();
                RuleStack = new RuleStack();
                NewGrammar();

                FirstNode = CreateGrammarNode("<sil>");
                GrammarNode finalNode = CreateGrammarNode("<sil>");
                finalNode.SetFinalNode(true);

                // go through each rule and create a network of GrammarNodes
                // for each of them

                foreach (String ruleName in _ruleGrammar.GetRuleNames())
                {
                    if (_ruleGrammar.IsRulePublic(ruleName))
                    {
                        String       fullName        = GetFullRuleName(ruleName);
                        GrammarGraph publicRuleGraph = new GrammarGraph(this);
                        RuleStack.Push(fullName, publicRuleGraph);
                        JSGFRule     rule  = _ruleGrammar.GetRule(ruleName);
                        GrammarGraph graph = ProcessRule(rule);
                        RuleStack.Pop();

                        FirstNode.Add(publicRuleGraph.StartNode, 0.0f);
                        publicRuleGraph.EndNode.Add(finalNode, 0.0f);
                        publicRuleGraph.StartNode.Add(graph.StartNode,
                                                      0.0f);
                        graph.EndNode.Add(publicRuleGraph.EndNode, 0.0f);
                    }
                }
                PostProcessGrammar();
                if (Logger.Level == LogLevel.All)
                {
                    DumpGrammar();
                }
            }
            catch (UriFormatException mue)
            {
                throw new IOException("bad base grammar URL " + BaseUrl + ' ' + mue);
            }
        }
Exemple #3
0
        /**
         * Commit changes to all loaded grammars and all changes of grammar since
         * the last commitChange
         *
         * @throws JSGFGrammarParseException
         * @throws JSGFGrammarException
         */

        public override void CommitChanges()
        {
            try
            {
                if (LoadGrammar)
                {
                    if (Manager == null)
                    {
                        GetGrammarManager();
                    }
                    LoadXML();
                    LoadGrammar = false;
                }

                RuleStack = new RuleStack();
                NewGrammar();

                FirstNode = CreateGrammarNode("<sil>");
                var finalNode = CreateGrammarNode("<sil>");
                finalNode.SetFinalNode(true);

                // go through each rule and create a network of GrammarNodes
                // for each of them

                foreach (var entry in _rules)
                {
                    var publicRuleGraph = new GrammarGraph(this);
                    RuleStack.Push(entry.Key, publicRuleGraph);
                    var graph = ProcessRule(entry.Value);
                    RuleStack.Pop();

                    FirstNode.Add(publicRuleGraph.StartNode, 0.0f);
                    publicRuleGraph.EndNode.Add(finalNode, 0.0f);
                    publicRuleGraph.StartNode.Add(graph.StartNode,
                                                  0.0f);
                    graph.EndNode.Add(publicRuleGraph.EndNode, 0.0f);
                }
                PostProcessGrammar();
            }
            catch (UriFormatException mue)
            {
                throw new IOException("bad base grammar URL " + BaseUrl + ' ' + mue);
            }
        }