internal XmlGrammar GetMainGrammar() { if (ConfigManager != null && ConfigManager.FindAllComponentsOfType <IMovieSensor>().Any()) { List <Showtime> showTimes = ConfigManager.FindAllComponentsOfType <IMovieSensor>().SelectMany(s => s.GetMovieShowtimes(new SamiDateTime(DateTimeRange.AnyTime))).ToList(); List <String> movieNames = showTimes.Select(showtime => showtime.MovieTitle).Distinct().ToList(); if (movieNames.Count == 0) { // If the movie list is empty, the grammar file becomes invalid. // Therefore, I will add a nonsensical string that will never be detect // to prevent the grammar from erroring. // A better solution should be found for this, but it's not too important // right now. movieNames.Add("XYZ123"); } List <String> theaterNames = showTimes.Select(showtime => showtime.Theater).Distinct().ToList(); if (theaterNames.Count == 0) { // If the movie list is empty, the grammar file becomes invalid. // Therefore, I will add a nonsensical string that will never be detect // to prevent the grammar from erroring. // A better solution should be found for this, but it's not too important // right now. theaterNames.Add("XYZ123"); } XmlDocument doc = new XmlDocument(); doc.Load(ConfigManager.GetPathForFile("MovieGrammar.grxml", GetType())); XmlElement oneof = GrammarUtility.CreateListOfPossibleStrings(doc, movieNames); XmlElement rule = GrammarUtility.CreateElement(doc, "rule", new Dictionary <string, string> { { "id", "MovieName" }, { "scope", "public" }, }); rule.AppendChild(oneof); doc.LastChild.AppendChild(rule); oneof = GrammarUtility.CreateListOfPossibleStrings(doc, theaterNames); rule = GrammarUtility.CreateElement(doc, "rule", new Dictionary <string, string> { { "id", "TheaterName" }, { "scope", "public" }, }); rule.AppendChild(oneof); doc.LastChild.AppendChild(rule); return(new XmlGrammar(doc)); } return(null); }
public override void Initialize(IConfigurationManager configManager) { base.Initialize(configManager); if (ConfigManager.FindAllComponentsOfType <IFootballSensor>().Any()) { IEnumerable <FootballTeam> teams = ConfigManager.FindAllComponentsOfType <IFootballSensor>().SelectMany(s => s.Teams).ToList(); if (teams.Any()) { XmlDocument doc = new XmlDocument(); doc.Load(ConfigManager.GetPathForFile("FootballGrammar.grxml", GetType())); XmlElement oneof = GrammarUtility.CreateElement(doc, "one-of", new Dictionary <String, String>()); foreach (FootballTeam possibleVal in teams) { XmlElement item = GrammarUtility.CreateElement(doc, "item", new Dictionary <String, String>()); XmlElement teamCityItem = GrammarUtility.CreateElement(doc, "item", new Dictionary <String, String> { { "repeat", "0-1" } }); teamCityItem.InnerText = possibleVal.LocationName; XmlElement teamNameItem = GrammarUtility.CreateElement(doc, "item", new Dictionary <String, String>()); teamNameItem.InnerText = possibleVal.Name; XmlElement tag = GrammarUtility.CreateElement(doc, "tag", new Dictionary <String, String>()); tag.InnerText = "out = \"" + possibleVal.Key + "\";"; item.AppendChild(teamCityItem); item.AppendChild(teamNameItem); item.AppendChild(tag); oneof.AppendChild(item); } XmlElement rule = GrammarUtility.CreateElement(doc, "rule", new Dictionary <string, string> { { "id", "FootballTeams" }, { "scope", "public" }, }); rule.AppendChild(oneof); doc.LastChild.AppendChild(rule); Provider = new GrammarProvider(new XmlGrammar(doc)); } else { _foundTeams = false; } } }
private XmlGrammar GetGrammar() { if (IsValid) { XmlDocument doc = new XmlDocument(); doc.Load(ConfigManager.GetPathForFile("ZWaveUtilityGrammar.grxml", GetType())); IEnumerable <String> locations = (ConfigManager as IInternalConfigurationManager).FindAllComponentsOfTypeEvenInvalid <IZWaveNode>().Select(s => s.Name); XmlElement oneof = GrammarUtility.CreateListOfPossibleStrings(doc, locations); XmlElement rule = GrammarUtility.CreateElement(doc, "rule", new Dictionary <string, string> { { "id", "NodeName" }, { "scope", "public" }, }); rule.AppendChild(oneof); doc.LastChild.AppendChild(rule); return(new XmlGrammar(doc)); } else { return(null); } }