/// <summary> /// Determines whether a user's answer is valid. /// </summary> public static bool AnswerIsValid(Question question, string input) { // Allow time-varying inputs // TODO: Validate these if (input.StartsWith("{") & input.EndsWith("}")) return true; // Allow "unstated" if (input.Trim() == "?") return true; // Otherwise, see if the input is consistent with the question type string qType = question.questionType; if (qType == "Tbool") return BoolIsValid(input); else if (qType == "Tnum") return NumberIsValid(question, input); else if (qType == "Tdate") return DateIsValid(question, input); return true; }
/// <summary> /// Adds the assertion relationship to the .akk unit test text. /// </summary> public static string AddUnitTestAssertRel(Facts.Fact theF, Question theQ) { string result = "- " + theQ.relationship + "(" + ((Thing)theF.Arg1).Id; if (theF.Arg2 == null) { // e.g. Rel(p) = result += ") = "; } else { // e.g. Rel(p,q) = result += "," + ((Thing)theF.Arg2).Id + ") = "; } return result; }
/// <summary> /// Validates date inputs. /// </summary> private static bool DateIsValid(Question question, string input) { try { DateTime theDate = DateTime.Parse(input); } catch { return false; } // Here you could validate that the number is between two // preset earliest and latest acceptable dates... return true; }
/// <summary> /// Validates numeric inputs. /// </summary> private static bool NumberIsValid(Question question, string input) { if (input.Trim() == "") return false; // Is the value a number at all? string answer = input.Trim('$'); if (!IsNumber(input)) { return false; } // Here you could validate that the number is between some // preset minimum and maximum values... return true; }