public List <HornClauseClass> GetKB(String file) { List <HornClauseClass> result = new List <HornClauseClass>(); String[] fileData = System.IO.File.ReadAllLines(file); int ClauseLine = 0; for (int i = 0; i < fileData.Count(); i++) { if (fileData[i].ToUpper() == "TELL") { ClauseLine = i + 1; break; } } fileData[ClauseLine] = fileData[ClauseLine].Replace(" ", string.Empty); String[] line = fileData[ClauseLine].Split(';'); foreach (String sentence in line.Where(n => n != "")) { HornClauseClass temp = Sentence2Clause(sentence); if (temp != null) { result.Add(temp); } } return(result); }
public override string EvaluateQuery(QueryClass Query, Boolean Debug) { string Result = "YES:"; // initialise our goals list to the symbol(s) contained within the query List <string> goals = Query.QueryClause.GetSymbols(); List <string> searched = new List <string>(); // starting from the symbol query, iteratively check if each symbol in the goals list // is a conclusion of another KB clause, or is a fact. // if the symbol is a conclusion of another KB clause, then we add the symbols of // the premise of that clause to the goals symbols list while (goals.Count > 0) { String goal = goals[0]; goals.RemoveAt(0); searched.Add(goal); HornClauseClass ex = isConclusionOrFact(goal);//Not sure why I called it ex. name can be changed - Thomas if (ex == null) { //null should say that it wasn't a conclusion or a fact in the knowledge base //and therefore should return null return("NO"); } else if (ex.GetType().Name == "HornClauseImplicationClass") { //probably should keep the clauses List <String> temp = ex.GetPremiseSymbols(); foreach (String str in temp) { if (!goals.Contains(str) && !searched.Contains(str)) { goals.Add(str); } } } } searched.Reverse(); for (int i = 0; i < searched.Count; i++) { Result += " " + searched[i]; if (i < searched.Count - 1) { Result += ","; } } return(Result); }
public HornClauseAndClass(HornClauseClass Premise1, HornClauseClass Premise2) { this.Premise1 = Premise1; this.Premise2 = Premise2; this.Type = HornClauseClassType.And; }
public HornClauseClass Premise1; // Phrases can consist of other phrases public HornClauseBracketClass(HornClauseClass Premise1) { this.Premise1 = Premise1; this.Type = HornClauseClassType.Bracket; }
public HornClauseOrClass(HornClauseClass Premise1, HornClauseClass Premise2) { this.Premise1 = Premise1; this.Premise2 = Premise2; this.Type = HornClauseClassType.Or; }
private HornClauseClass Sentence2Clause(String sentence) { /*if (sentence.Contains("(")) * { * String[] separator = { "(" }; * String temp1 = sentence.Substring (0, sentence.IndexOf ('(')); * * String temp2 = sentence.Substring (sentence.IndexOf ('(')+1,sentence.Length-sentence.IndexOf ('(')-1); * String[] RHS; * * * int OpenBracketCount = 0; * int SplitIndex = 0; * * for (int charNum = 0; charNum < temp2.Length; charNum ++) { * if (temp2[charNum] == ')') { * if (OpenBracketCount == 0) { * // we've reached the corresponding close bracket * // save this as the SplitIndex * SplitIndex = charNum; * break; * } else { * // if not the corresponding bracket, then decrement the OpenBracketCount * OpenBracketCount = OpenBracketCount - 1; * } * } * else if(temp2[charNum] == '('){ * // then increment the OpenBracketCount * OpenBracketCount = OpenBracketCount + 1; * } * } * * // split the left hand side and then add it * String temp3 = temp2.Substring (0, SplitIndex); * String temp4 = temp2.Substring (SplitIndex+1,temp2.Length-SplitIndex-1); * * // append the temp3 to the temp1 string * temp1 = temp1 + temp3; * * * HornClauseClass tempHorn1 = Sentence2Clause(temp1); * HornClauseClass tempHorn2 = Sentence2Clause(temp4); * return new HornClauseBidirectionalClass(tempHorn1, tempHorn2); * } * else*/if (sentence.Contains("<=>")) { String[] separator = { "<=>" }; String[] temp = sentence.Split(separator, System.StringSplitOptions.RemoveEmptyEntries); HornClauseClass tempHorn1 = Sentence2Clause(temp[0]); HornClauseClass tempHorn2 = Sentence2Clause(temp[1]); return(new HornClauseBidirectionalClass(tempHorn1, tempHorn2)); } else if (sentence.Contains("=>")) { String[] separator = { "=>" }; String[] temp = sentence.Split(separator, System.StringSplitOptions.RemoveEmptyEntries); HornClauseClass tempHorn1 = Sentence2Clause(temp[0]); HornClauseClass tempHorn2 = Sentence2Clause(temp[1]); return(new HornClauseImplicationClass(tempHorn1, tempHorn2)); } else if (sentence.Contains("\\/")) { String[] separator = { "\\/" }; String[] temp = sentence.Split(separator, 2, System.StringSplitOptions.RemoveEmptyEntries); HornClauseClass tempHorn1 = Sentence2Clause(temp[0]); HornClauseClass tempHorn2 = Sentence2Clause(temp[1]); return(new HornClauseOrClass(tempHorn1, tempHorn2)); } else if (sentence.Contains("&")) { String[] temp = sentence.Split(new char[] { '&' }, 2); HornClauseClass tempHorn1 = Sentence2Clause(temp[0]); HornClauseClass tempHorn2 = Sentence2Clause(temp[1]); return(new HornClauseAndClass(tempHorn1, tempHorn2)); } else if (sentence.Contains("~")) { String[] separator = { "~" }; String[] temp = sentence.Split(separator, System.StringSplitOptions.RemoveEmptyEntries); HornClauseClass tempHorn1 = Sentence2Clause(temp [0]); return(new HornClauseNotClass(tempHorn1)); } else if (Regex.IsMatch(sentence, "^[a-zA-Z][a-zA-Z0-9_]*$")) { return(new HornClauseFactClass(sentence)); } else { return(null); } }
public HornClauseClass Premise1; // Phrases can consist of other phrases public HornClauseNotClass(HornClauseClass Premise1) { this.Premise1 = Premise1; this.Type = HornClauseClassType.Not; }
public HornClauseClass QueryClause; // Clause to represent the query being asked of the KB // Methods public QueryClass(HornClauseClass Clause) { this.QueryClause = Clause; //this.InferenceType = InferenceTypeENUM.TT; }
public HornClauseBidirectionalClass(HornClauseClass Premise1, HornClauseClass Premise2) { this.Premise1 = Premise1; this.Premise2 = Premise2; this.Type = HornClauseClassType.Biconditional; }
public HornClauseImplicationClass(HornClauseClass Premise, HornClauseClass Conclusion) { this.Premise = Premise; this.Conclusion = Conclusion; this.Type = HornClauseClassType.Implication; }