/// <summary> /// Assesses the state of asserted facts and then decides what to do next. /// </summary> public static void ProcessRequest(string request) { Facts.Fact goalblob = ParseRequest(request); InitializeSession(); while (true) { // Get the response object from the interview engine Engine.Response response = Engine.Investigate(new List <Facts.Fact>() { goalblob }); // Ask the current question, or display the results if (!response.InvestigationComplete) { // Ask the next question DisplayQuestion(response); // Get and validate the answer GetAndParseAnswer(response); } else { DisplayResults(goalblob); break; } } // Display all facts that have been asserted (for diagnostic purposes) // Console.WriteLine("\nFacts: \n" + Facts.AssertedFacts()); }
/// <summary> /// Displays a question to the user. /// </summary> private static void DisplayQuestion(Engine.Response response) { // Get data about the next question Facts.Fact theFact = response.NextFact; Question theQ = Templates.GetQ(theFact.Relationship); // TODO: Display appropriate question control (using theQ.questionType) // White space Console.WriteLine(); // Display progress percentage Console.WriteLine("Percent complete: " + response.PercentComplete); // Display question text string qText = theFact.QuestionText(); // QuestionText(theFact, theQ); Console.WriteLine(qText); AkkTest.assertedRelationship = AkkTest.AddUnitTestAssertRel(theFact, theQ); // Display an explanation (if any) if (theQ.explanation != "") { Console.WriteLine("Note: " + theQ.explanation); } }
/// <summary> /// Gets the and validates the user's answer to a question. /// </summary> private static void GetAndParseAnswer(Engine.Response response) { // Get data pertaining to the current question string currentRel = response.NextFact.Relationship; Question currentQuestion = Templates.GetQ(currentRel); string currentQType = currentQuestion.questionType; // Read (and gently massage) the answer string answer = Console.ReadLine(); answer = CleanBooleans(currentQType, answer); // Validate answer, then assert it if (AnswerIsValid(currentQuestion, answer)) { AssertAnswer(response, answer); } else { GetAndParseAnswer(response); } }
/// <summary> /// Asserts a fact. /// </summary> public static void AssertAnswer(Engine.Response response, string val) { // Get the data pertaining to the current question Thing subj = (Thing)response.NextFact.Arg1; Thing obj = (Thing)response.NextFact.Arg2; string rel = response.NextFact.Relationship; string qType = Templates.GetQ(rel).questionType; // Create the fact/relationship text for the .akk unit test if (qType != "Tset") { AkkTest.testStr += AkkTest.assertedRelationship; } // Assert the fact (converted to the proper type of Tvar) if (qType == "Tbool") { // Asserts the fact (handling uncertainty) // Also, creates part of the .akk unit test string if (val == "?") { Facts.Assert(subj, rel, obj, new Tbool(Hstate.Uncertain)); AkkTest.testStr += "Tbool(?)\r\n"; } else { Facts.Assert(subj, rel, obj, TboolFromTemporalString(val)); AkkTest.testStr += val + "\r\n"; } } else if (qType == "Tstr") { if (val == "?") { Facts.Assert(subj, rel, obj, new Tstr(Hstate.Uncertain)); AkkTest.testStr += "Tstr(?)\r\n"; } else { Facts.Assert(subj, rel, obj, TstrFromTemporalString(val)); AkkTest.testStr += "\"" + val + "\"\r\n"; } } else if (qType == "Tnum") { if (val == "?") { Facts.Assert(subj, rel, obj, new Tnum(Hstate.Uncertain)); AkkTest.testStr += "Tnum(?)\r\n"; } else { Facts.Assert(subj, rel, obj, TnumFromTemporalString(val)); AkkTest.testStr += val + "\r\n"; } } else if (qType == "Tdate") { if (val == "?") { Facts.Assert(subj, rel, obj, new Tdate(Hstate.Uncertain)); AkkTest.testStr += "Tdate(?)\r\n"; } else { Facts.Assert(subj, rel, obj, TdateFromTemporalString(val)); AkkTest.testStr += val + "\r\n"; } } else if (qType == "Tset") { if (val == "?") { Facts.Assert(subj, rel, obj, new Tset(Hstate.Uncertain)); AkkTest.testStr += "Tset(?)\r\n"; } else { // Assert an empty set if (val == "[]") { Tset result = new Tset(); result.SetEternally(); Facts.Assert(subj, rel, obj, result); } else { // Create a list of Things string[] items = val.Split(new char[] { ';' }); List <Thing> list = new List <Thing>(); string thingList = ""; // for .akk unit tests foreach (string i in items) { string name = i.Trim(); list.Add(Facts.AddThing(name)); thingList += name + ","; } // Assert the Tset Facts.Assert(subj, rel, obj, new Tset(list)); // Build the .akk unit test string AkkTest.testStr += "- Things " + thingList.TrimEnd(',') + "\r\n"; AkkTest.testStr += AkkTest.assertedRelationship; AkkTest.testStr += "[[" + val.Replace(";", ",") + "]]\r\n"; } } } }