public static void Invoke(Document doc) { var query = from k in doc.responseData.feed.entries let t = new TitleParser(k.title) select new { k, t }; var ruleset = new StringBuilder(); ruleset.AppendLine(@" set(hyper_res). set(factor). set(print_kept). formula_list(sos). "); #region VariableEqualsToAny ParamsFunc<string, string, string> VariableEqualsToAny = (variable, values) => { var w = new StringBuilder(); w.Append("("); values.ForEach( (k, i) => { if (i > 0) w.AppendLine(" | "); w.AppendLine("$EQ(" + variable + ", " + k.GetHashCode() + @") %" + k); } ); w.Append(")"); return w.ToString(); }; ParamsFunc<string, int, string> VariableEqualsToAnyInteger = (variable, values) => { var w = new StringBuilder(); w.Append("("); values.ForEach( (k, i) => { if (i > 0) w.AppendLine(" | "); w.AppendLine("$EQ(" + variable + ", " + k + @")"); } ); w.Append(")"); return w.ToString(); }; #endregion #region Question 1 ruleset.AppendLine(@" % Question 1 all x all z all u ( facts(x,category,u) & " + VariableEqualsToAny("u", "Animation") + @" -> ForChildren(x) ). "); ruleset.AppendLine(@" all x all z all u ( facts(x,category,u) & " + VariableEqualsToAny("u", "Animation", "Comedy", "Family", "Fantasy") + @" -> ForFamily(x) ). "); ruleset.AppendLine(@" all x all z all u ( facts(x,category,u) & " + VariableEqualsToAny("u", "Adventure", "Drama", "Fantasy", "Mystery", "Thriller", "Action", "Crime") + @" -> ForAdults(x) ). "); #endregion #region Question 2 ruleset.AppendLine(@" % Question 2 all x all z all u ( facts(x,year,u) & " + VariableEqualsToAnyInteger("u", DateTime.Now.Year, DateTime.Now.Year + 1) + @" -> ForGeeks(x) ). "); ruleset.AppendLine(@" all x all z all u ( facts(x,year,u) & " + VariableEqualsToAnyInteger("u", DateTime.Now.Year, DateTime.Now.Year + 1, DateTime.Now.Year - 1, DateTime.Now.Year - 2) + @" -> ForRecent(x) ). "); #endregion #region Question 3 ruleset.AppendLine(@" % Question 3 all x all z all u ( facts(x,raiting,u) & $GT(u, 65) -> GoodRaiting(x) ). all x all z all u ( facts(x,raiting,u) & $LT(u, 70) -> BadRaiting(x) ). "); #endregion #region Question 4 ruleset.AppendLine(@" % Question 4 all x all z all u ( facts(x,category,u) & " + VariableEqualsToAny("u", "TV shows") + @" -> IsTVShow(x) ). all x all z all u ( facts(x,category,u) & " + VariableEqualsToAny("u", "Movies") + @" -> IsMovie(x) ). "); #endregion #region facts foreach (var entry in query) { entry.k.content.ParseMovieItem( m => { ruleset.AppendLine("facts('" + entry.t.Title.Replace("'", @"-") + "', raiting, " + Convert.ToInt32(m.Raiting * 100) + ")."); } ); var p = new BasicFileNameParser(entry.t.Title); ruleset.AppendLine("facts('" + entry.t.Title.Replace("'", @"-") + "', year, " + entry.t.Year + ")."); //if (p.Season != null) // ruleset.AppendLine("facts('" + entry.t.Title.Replace("'", @"-") + "', season, " + int.Parse(p.Season) + ")."); //if (p.Episode != null) // ruleset.AppendLine("facts('" + entry.t.Title.Replace("'", @"-") + "', episode, " + int.Parse(p.Episode) + ")."); foreach (var category in entry.k.categories) { ruleset.AppendLine("facts('" + entry.t.Title.Replace("'", @"-") + "', category, " + category.GetHashCode() + "). %" + category); } } #endregion var Filters = new List<string>(); Func<string, Action> f = filter => () => Filters.Add(filter); ConsoleQuestions.Ask( new Options { { "There are some kids over here", f("ForChildren(x)")}, { "My family is in the room, keep it decent", f("ForFamily(x)")}, { "There are only some dudes in the room", f("ForAdults(x)")}, { "Neither", () => {}}, }, new Options { { "I am looking for new stuff", f("ForGeeks(x)")}, { "I haven't watched that much tv recently!", f("ForRecent(x)")}, { "I do not like recent movies at all!", f("-ForRecent(x)")}, { "Neither", () => {}}, }, new Options { { "I am looking for having a good time", f("GoodRaiting(x)")}, { "I want to suggest someething really bad to a friend", f("BadRaiting(x)")}, { "I cannot decide between options above", () => {}}, }, new Options { { "I cannot watch tv very long", f("IsTVShow(x)")}, { "30 minutes is not enough for me", f("IsMovie(x)")}, { "I cannot decide between options above", () => {}}, } ); #region IsSelected ruleset.AppendLine(@" % The anwser all x ( " ); Filters.ForEach( (k, i) => { if (i > 0) ruleset.AppendLine(" & "); ruleset.AppendLine(k); } ); ruleset.AppendLine(@" -> IsSelected(x) )." ); #endregion var otter = new OtterApplication(ruleset.ToString()); Console.WriteLine(); Console.WriteLine("Movies for you"); otter.ToConsole(k => k == "IsSelected"); File.WriteAllText(@"Data\IDX5711.in", ruleset.ToString()); File.WriteAllText(@"Data\IDX5711.out", otter.Output); }
private static void OtterTesting() { var otter = new OtterApplication( @"% this is the way comments start set(hyper_res). % another alternative is set(binary_res) set(factor). % probably comes automatically set(print_kept). % this will print out ALL derived and kept stuff formula_list(sos). % use list(sos) if not formula syntax rdfx1('xx 1',job,cleaner). rdfx1('xx 1',phone,123). rdfx1('xx 1',blog,'http://zproxy.wordpress.com'). rdfx1(s2,job,guard). rdfx1(s2,phone,345). rdfx1(s2,blog,'http://zproxy.wordpress.com'). rdfx1(s3,title,breakingnews). rdfx1(s3,link,www_epl_ee). % following three rows are not formula syntax: x,y,z,u,v,w are vars % -rdf(x,job,z) | % -rdf(x,phone,u) | % rdf(x,type,employee). % to be an employee one must have a job and a phone % % this is formula syntax all x all z all u ( rdfx1(x,job,z) & rdfx1(x,phone,u) -> rdfx1(x,type,employee) ). all x all z all u ( rdfx1(x,blog,z) -> rdfx1(x,type,blogger) ). all x all z all u ( rdfx1(x,type,employee) & rdfx1(x,type,blogger) -> rdfx1(x,type,smartguy) ). % and there is no query! try giving -rdf(s1,type,employee) as query. % useful for query: say that s1 is not s2 % s1!=s2. % query example: look for all guys not s2 % all x (rdf(x,type,employee) & % x!=s2 % -> % $answer(x))." ); Console.WriteLine(@"newly derived facts:"); otter.ToConsole(); }