protected override int QueryUsingRule(Rule query) { List <HornRule> checkedRules = new List <HornRule>(); bool stateChanged = true; while (stateChanged) { stateChanged = false; //Try to solve each of the rules given the current knowledge. foreach (HornRule HR in KB) { if (checkedRules.Contains(HR)) { continue; } //Resolve the rule. If it was fully resolved: HR.Resolve(literalsKey, literalsSolutions); if (HR.IsFullyResolved) { //If this rule proves a literal (i.e. doesn't return Unknown), add the literal to the knowledge and remove the rule. HornRule.Literal newLiteral = HR.ProveLiteral(literalsKey, literalsSolutions); if (newLiteral.state != SymbolIs.Unknown) { stateChanged = true; checkedRules.Add(HR); //Loop through the knowledge table and add the new information at the correct index. for (int i = 0; i < literalsKey.Length; ++i) { if (literalsKey[i] == newLiteral.symbol) { if (literalsSolutions[i] == SymbolIs.Unknown) { literalsSolutions[i] = newLiteral.state; } else if (literalsSolutions[i] != newLiteral.state) { throw new Exception("KnowledgeBase is contradictory"); } } } } } } //If the query has been proven, return either true or false based on the outcome. SymbolIs solution = query.CheckSolved(literalsKey, literalsSolutions); if (solution == SymbolIs.True) { return(1); } else if (solution == SymbolIs.False) { return(0); } } //End of While loop return(0); }
public override bool SolveAndQuery(string symbol) { bool stateChanged = true; while (stateChanged) { stateChanged = false; //Try to solve each of the rules given the current knowledge. foreach (HornRule R in KB) { //Resolve the rule. If it was fully resolved: R.Resolve(literalsKey, literalsSolutions); if (R.IsFullyResolved) { //If this rule proves a literal (i.e. doesn't return Unknown), add the literal to the knowledge and remove the rule. HornRule.Literal newLiteral = R.ProveLiteral(literalsKey, literalsSolutions); if (newLiteral.state != SymbolIs.Unknown) { stateChanged = true; //Loop through the knowledge table and add the new information at the correct index. for (int i = 0; i < literalsKey.Length; ++i) { if (literalsKey[i] == newLiteral.symbol) { if (literalsSolutions[i] == SymbolIs.Unknown) { literalsSolutions[i] = newLiteral.state; } else if (literalsSolutions[i] != newLiteral.state) { throw new Exception("KnowledgeBase is contradictory"); } } } //If the symbol being proven is the query symbol, return true. if (newLiteral.symbol == symbol) { return(true); } } } } } //End of While loop return(false); }
public override bool SolveAndQuery(string symbol) { //Initialize List <string> literalsToSolve = new List <string>(); literalsToSolve.Add(symbol); List <HornRule> rulesToSolve = new List <HornRule>(); List <HornRule> checkedRules = new List <HornRule>(); bool stateChanged = true; while (stateChanged == true) { stateChanged = false; //Add the the list of rules to solve any rules which are relevant to our query (I.e. are relevant to symbols which are relevant). //Then add each symbol used by those rules to the list of literals to solve. foreach (HornRule HR in KB) { if (checkedRules.Contains(HR)) { continue; } for (int i = 0; i < literalsToSolve.Count; ++i) { if (HR.LiteralIsImplication(literalsToSolve[i])) { stateChanged = true; rulesToSolve.Add(HR); checkedRules.Add(HR); foreach (string s in HR.symbols) { if (!literalsToSolve.Contains(s)) { literalsToSolve.Add(s); } } break; } } } //Try to solve each of the rules given the current knowledge. //If a rule is solved, remove it from the list, and add any symbols it entails to the KB. for (int r = 0; r < rulesToSolve.Count; ++r) { //Resolve the rule. If it was fully resolved: rulesToSolve[r].Resolve(literalsKey, literalsSolutions); if (rulesToSolve[r].IsFullyResolved) { //If this rule proves a literal (i.e. doesn't return Unknown), add the literal to the knowledge and remove the rule. HornRule.Literal newLiteral = rulesToSolve[r].ProveLiteral(literalsKey, literalsSolutions); if (newLiteral.state != SymbolIs.Unknown) { stateChanged = true; //Loop through the knowledge table and add the new information at the correct index. for (int i = 0; i < literalsKey.Length; ++i) { if (literalsKey[i] == newLiteral.symbol) { if (literalsSolutions[i] == SymbolIs.Unknown) { literalsSolutions[i] = newLiteral.state; } else if (literalsSolutions[i] != newLiteral.state) { throw new Exception("KnowledgeBase is contradictory"); } } } //If the symbol being proven is the query symbol, return true. if (newLiteral.symbol == symbol) { return(true); } //Stop trying to find rules relevant to the proven literal. literalsToSolve.Remove(newLiteral.symbol); //Remove the solved rule from the list, and go back one index. rulesToSolve.RemoveAt(r); --r; } } } } //End of While loop. return(false); }