/// <summary> /// Executes the query. /// </summary> /// <returns> /// string that reprsent the result /// </returns> public string Execute() { Delegate[] arr = queriesList.GetInvocationList(); int queryNum = rand.Next(arr.Length); Heuristics h = ((HeuristicsBank.NumberHeuristics)arr[queryNum])(user); int number = conn.ExecuteScalarCommand(h.Command); return(string.Format(h.ResultFormat, number)); }
/// <summary> /// Gets the year heuristics. /// </summary> /// <param name="conn">The database connector.</param> /// <returns> YearHeuristics delegate </returns> public static YearHeuristics GetYearHeuristics(DataBaseConnector conn) { YearHeuristics queriesList = (User u, int f, int t) => { string format; if (f != t) { format = "The top 10 popular genres between " + f + " and " + t + " are:\n{0}"; } else { format = "The top 10 popular genres in the year " + f + " are:\n{0}"; } SelfExecuterHeuristics heuristics = HeuristicsFactory.CreateSelfExecuterHeuristics("select genere_name from genres, genresbyartist, artists where genres.id_genre = genresbyartist.id_genre and genresbyartist.id_artist = artists.id_artist and begin_date_year between " + f + " and " + t + " group by genresbyartist.id_genre order by sum(artists.num_of_hits) desc limit 10;", format, conn); SelfExecuterHeuristics.ExecuteDel del = (DataBaseConnector connector) => { List <Dictionary <string, string> > result = conn.ExecuteCommand(heuristics.Command); if (result.Count == 0) { if (f != t) { return("We don't have records of genres between the years " + f + " and " + t); } else { return("We don't have records of genres in the year " + f); } } StringBuilder builder = new StringBuilder(); foreach (Dictionary <string, string> record in result) { builder.AppendLine(record["genere_name"]); } return(string.Format(heuristics.ResultFormat, builder.ToString())); }; heuristics.Executer = del; return(heuristics); }; queriesList += (User u, int f, int t) => { string format; if (f != t) { format = "The top 10 artists that released most songs between " + f + " and " + t + " are:\n{0}"; } else { format = "The top 10 artists that released most songs in the year " + f + " are:\n{0}"; } SelfExecuterHeuristics heuristics = HeuristicsFactory.CreateSelfExecuterHeuristics("select distinct artist_name,count(*) as total from artists,songsbyartist,songs where artists.id_artist = songsbyartist.id_artist and songsbyartist.id_song=songs.id_song and release_date_year between " + f + " and " + t + " group by songsbyartist.id_artist order by count(*) desc limit 10;", format, conn); SelfExecuterHeuristics.ExecuteDel del = (DataBaseConnector connector) => { List <Dictionary <string, string> > result = conn.ExecuteCommand(heuristics.Command); if (result.Count == 0) { if (f != t) { return("There are no artists that released songs between " + f + " and " + t); } else { return("There are no artists that released songs in the year " + f); } } StringBuilder builder = new StringBuilder(); foreach (Dictionary <string, string> record in result) { builder.AppendLine(record["artist_name"] + " with " + record["total"] + " songs"); } return(string.Format(heuristics.ResultFormat, builder.ToString())); }; heuristics.Executer = del; return(heuristics); }; queriesList += (User u, int f, int t) => { string format; if (f != t) { format = "The number of artists that died between the years " + f + " and " + t + " is {0}"; } else { format = "The number of artists that died in the year " + f + " is {0}"; } SelfExecuterHeuristics heuristics = HeuristicsFactory.CreateSelfExecuterHeuristics("select count(id_artist) from artists where end_date_year between " + f + " and " + t, format, conn); SelfExecuterHeuristics.ExecuteDel del = (DataBaseConnector connector) => { int num = conn.ExecuteScalarCommand(heuristics.Command); return(string.Format(heuristics.ResultFormat, num)); }; heuristics.Executer = del; return(heuristics); }; queriesList += (User u, int f, int t) => { string format; if (f != t) { format = "The top 10 popular songs released between " + f + " and " + t + " are:\n{0}"; } else { format = "The top 10 popular songs released in " + f + " are:\n{0}"; } SelfExecuterHeuristics heuristics = HeuristicsFactory.CreateSelfExecuterHeuristics("select distinct song_name,num_of_hits from songs where release_date_year between " + f + " and " + t + " order by num_of_hits DESC limit 10;", format, conn); SelfExecuterHeuristics.ExecuteDel del = (DataBaseConnector connector) => { List <Dictionary <string, string> > result = conn.ExecuteCommand(heuristics.Command); if (result.Count == 0) { if (f != t) { return("There are no songs released between " + f + " and " + t); } else { return("There are no songs released in " + f); } } StringBuilder builder = new StringBuilder(); foreach (Dictionary <string, string> record in result) { builder.AppendLine(record["song_name"]); } return(string.Format(heuristics.ResultFormat, builder.ToString())); }; heuristics.Executer = del; return(heuristics); }; queriesList += (User u, int f, int t) => { string format; if (f != t) { format = "The most popular atrtist born between " + f + " and " + t + " is: {0}"; } else { format = "The most popular atrtist born in " + f + " is: {0}"; } SelfExecuterHeuristics heuristics = HeuristicsFactory.CreateSelfExecuterHeuristics("select artist_name, num_of_hits from artists where begin_date_year between " + f + " and " + t + " order by num_of_hits DESC limit 1; ", format, conn); SelfExecuterHeuristics.ExecuteDel del = (DataBaseConnector connector) => { List <Dictionary <string, string> > result = conn.ExecuteCommand(heuristics.Command); if (result.Count == 0) { if (f != t) { return("There are no artists born between " + f + " and " + t); } else { return("There are no artists born in " + f); } } return(string.Format(heuristics.ResultFormat, result[0]["artist_name"])); }; heuristics.Executer = del; return(heuristics); }; queriesList += (User u, int f, int t) => { string format; if (f != t) { format = "The top 10 popular artists that born between " + f + " and " + t + " are:\n{0}"; } else { format = "The top 10 popular artists that born in the year " + f + " are:\n{0}"; } SelfExecuterHeuristics heuristics = HeuristicsFactory.CreateSelfExecuterHeuristics("select artist_name,num_of_hits from artists where begin_date_year between " + f + " and " + t + " group by id_artist order by num_of_hits DESC limit 10;", format, conn); SelfExecuterHeuristics.ExecuteDel del = (DataBaseConnector connector) => { List <Dictionary <string, string> > result = conn.ExecuteCommand(heuristics.Command); if (result.Count == 0) { if (f != t) { return("There are no artists born between " + f + " and " + t); } else { return("There are no artists born in " + f); } } StringBuilder builder = new StringBuilder(); foreach (Dictionary <string, string> record in result) { builder.AppendLine(record["artist_name"]); } return(string.Format(heuristics.ResultFormat, builder.ToString())); }; heuristics.Executer = del; return(heuristics); }; queriesList += (User u, int f, int t) => { string format; if (f != t) { format = "The number of artists born between " + f + " and " + t + " is {0}"; } else { format = "The number of artists born in " + f + " is {0}"; } SelfExecuterHeuristics heuristics = HeuristicsFactory.CreateSelfExecuterHeuristics("select count(id_artist) from artists where begin_date_year between " + f + " and " + t, format, conn); SelfExecuterHeuristics.ExecuteDel del = (DataBaseConnector connector) => { int num = connector.ExecuteScalarCommand(heuristics.Command); return(string.Format(format, num)); }; heuristics.Executer = del; return(heuristics); }; queriesList += (User u, int f, int t) => { string format; if (f != t) { format = "The number of songs released between " + f + " and " + t + " is {0}"; } else { format = "The number of songs released in " + f + " is {0}"; } SelfExecuterHeuristics heuristics = HeuristicsFactory.CreateSelfExecuterHeuristics("select count(id_song) from songs where release_date_year between " + f + " and " + t, format, conn); SelfExecuterHeuristics.ExecuteDel del = (DataBaseConnector connector) => { int num = connector.ExecuteScalarCommand(heuristics.Command); return(string.Format(format, num)); }; heuristics.Executer = del; return(heuristics); }; queriesList += (User u, int f, int t) => { string format; if (f != t) { format = "These are artists born between " + f + " and " + t + ":\n{0}"; } else { format = "These are artists born in " + f + ":\n{0}"; } SelfExecuterHeuristics heuristics = HeuristicsFactory.CreateSelfExecuterHeuristics("select artist_name from artists where begin_date_year between " + f + " and " + t + " limit 10", format, conn); SelfExecuterHeuristics.ExecuteDel del = (DataBaseConnector connector) => { List <string> result = connector.ExecuteOneColumnCommand(heuristics.Command); if (result.Count == 0) { if (f != t) { return("There are no artists born between " + f + " and " + t); } else { return("There are no artists born in " + f); } } StringBuilder stringBuilder = new StringBuilder(); foreach (string artist in result) { stringBuilder.AppendLine(artist); } return(string.Format(format, stringBuilder.ToString())); }; heuristics.Executer = del; return(heuristics); }; queriesList += (User u, int f, int t) => { string format; if (f != t) { format = "These are songs released between " + f + " and " + t + ":\n{0}"; } else { format = "These are songs released in " + f + ":\n{0}"; } SelfExecuterHeuristics heuristics = HeuristicsFactory.CreateSelfExecuterHeuristics("select song_name from songs where release_date_year between " + f + " and " + t + " limit 10", format, conn); SelfExecuterHeuristics.ExecuteDel del = (DataBaseConnector connector) => { List <string> result = connector.ExecuteOneColumnCommand(heuristics.Command); if (result.Count == 0) { if (f != t) { return("There are no songs released between " + f + " and " + t); } else { return("There are no songs released in " + f); } } StringBuilder stringBuilder = new StringBuilder(); foreach (string artist in result) { stringBuilder.AppendLine(artist); } return(string.Format(format, stringBuilder.ToString())); }; heuristics.Executer = del; return(heuristics); }; return(queriesList); }