SrgsDocument.Rules Property

 

Gets the collection of rules that are currently defined for the SrgsDocument class.

Namespace:   System.Speech.Recognition.SrgsGrammar
Assembly:  System.Speech (in System.Speech.dll)

public SrgsRulesCollection Rules { get; }

Property Value

Type: System.Speech.Recognition.SrgsGrammar.SrgsRulesCollection

Returns the rules defined for the SrgsDocument object.

You can add SrgsRule objects to the SrgsRulesCollection by using the Add method on the Rules property. If you initialize an SrgsDocument object and specify an SrgsRule object as the argument, the SrgsRule is automatically added to the SrgsRulesCollection for the SrgsDocument.

The following example creates a grammar that recognizes the phrase "A nation that has won the World Cup is" followed by the name of a country that has won the World Cup. The example creates an SrgsDocument object, and then creates a public rule named winnerRule. After adding a string to the rule winnerRule, the example creates two more rules (ruleEurope and ruleSAmerica), each containing a list of countries. Using the Add method, the example adds all three rules (winnerRule, ruleEurope, and ruleSAmerica) to the Rules property of the SrgsDocument.

public void WorldSoccerWinners ()
{

  // Create an SrgsDocument, create a new rule
  // and set its scope to public.
  SrgsDocument document = new SrgsDocument();
  SrgsRule winnerRule = new SrgsRule("WorldCupWinner");
  winnerRule.Scope = SrgsRuleScope.Public;

  // Add the introduction.
  winnerRule.Elements.Add(new SrgsItem("A nation that has won the world cup is: "));

  // Create the rule for the European nations.
  SrgsOneOf oneOfEurope = new SrgsOneOf(new SrgsItem[] {new SrgsItem("England"), 
    new SrgsItem("France"), new SrgsItem("Germany"), new SrgsItem("Italy")});
  SrgsRule ruleEurope = (new SrgsRule("EuropeanNations", new SrgsElement[] {oneOfEurope}));

  // Create the rule for the South American nations.
  SrgsOneOf oneOfSAmerica = new SrgsOneOf(new SrgsItem[] {new SrgsItem("Argentina"), 
    new SrgsItem("Brazil"), new SrgsItem("Uruguay")});
  SrgsRule ruleSAmerica = (new SrgsRule("SouthAmericanNations", new SrgsElement[] {oneOfSAmerica}));

  // Add references to winnerRule for ruleEurope and ruleSAmerica.
  winnerRule.Elements.Add(new SrgsOneOf(new SrgsItem[] {(new SrgsItem 
    (new SrgsRuleRef(ruleEurope))), new SrgsItem(new SrgsRuleRef(ruleSAmerica))}));

  // Add all the rules to the document and make winnerRule 
  // the root rule of the document.
  document.Rules.Add(new SrgsRule[] {winnerRule, ruleEurope, ruleSAmerica});
  document.Root = winnerRule;
}

.NET Framework
Available since 3.0
Return to top
Show: