public override Proposition Resolve(string[] key, SymbolIs[] state) { //Resolve child prepositions. _fact1 = _fact1.Resolve(key, state); _fact2 = _fact2.Resolve(key, state); SymbolIs sol1 = _fact1.CheckSolvable(key, state); SymbolIs sol2 = _fact2.CheckSolvable(key, state); //If both are false resolve to Not-True, if either is true resolve to True. if (sol1 == SymbolIs.False && sol2 == SymbolIs.False) { return(new Not(new True())); } else if (sol1 == SymbolIs.True || sol2 == SymbolIs.True) { return(new True()); } else { //If one of the facts can be proved false, it can be removed from the logic. if (sol1 == SymbolIs.False) { return(_fact2); } else if (sol2 == SymbolIs.False) { return(_fact1); } else { return(this); } } }
/// <summary> /// Find propositions that infer value /// </summary> /// <param name="value"></param> /// <returns></returns> public List <Proposition> FindReferal(Proposition value) { List <Proposition> Referals = new List <Proposition>(); // Check against each proposition in KB foreach (Proposition item in _StartWorld._KnowledgeBase.Propostions) { // For a proposition that refers to value if (item.getB() == value.getA()) { bool beenThere = false; // If found check that proposition has not already been used foreach (Proposition past in pastProps) { if (past == item) { beenThere = true; } } // If it hasn't. USE IT if (!beenThere) { pastProps.Add(item); Referals.Add(item); } } } return(Referals); }
public Proposition TryInfer(bool?[] Arguements) { if (this.Single | !(Operation == Operations.Implication | Operation == Operations.Biconditional)) { return(null); } Proposition CurrentProp = new Proposition(); if ((_ARef != null) && (_ARef.IsTrue(Arguements) == true)) { if (_BRef != null) { return(_BRef); } else { CurrentProp.setA(_B); return(CurrentProp); } } else if (Arguements[0] == true && _ARef == null) { if (_BRef != null) { return(_BRef); } else { CurrentProp.setA(_B); return(CurrentProp); } } return(null); }
private Proposition fringecases(String[] Prop) { if (Prop.Length == 1) { return(SetProp(Prop)); // single arguement prop no need to parse } else { int inferCount = 0; int inferPos = 0; for (int i = 0; i < Prop.Length; i++) { if (Prop[i].Contains("=>")) { inferCount++; inferPos = i; } } if (inferCount == 1) { Proposition CurrentProp = new Proposition(); string[] left = new string[inferPos]; //room for left of infer string[] right = new string[Prop.Length - left.Length - 1]; // room for what is left minus infer sign Array.Copy(Prop, 0, left, 0, left.Length); Array.Copy(Prop, inferPos + 1, right, 0, right.Length); CurrentProp = SetProp(left, Operations.Implication, right); return(CurrentProp); } else { return(generateProp(Prop)); //multi arguement prop, everything bracketed properly } } }
public override Proposition Resolve(string[] key, SymbolIs[] state) { //Resolve child prepositions. _requirement = _requirement.Resolve(key, state); _implication = _implication.Resolve(key, state); SymbolIs solution = CheckSolvable(key, state); if (solution == SymbolIs.True) { return(new True()); } else if (solution == SymbolIs.False) { return(new Not(new True())); } else { //If the requirement [A in A=>B] is true, return the implication [B]. SymbolIs req = _requirement.CheckSolvable(key, state); if (req == SymbolIs.True) { return(_implication); } else { return(this); } } }
public World(Proposition[] Propositions, int amount_args) { Proposition[] Temp = new Proposition[Propositions.Length - 1]; Array.Copy(Propositions, Temp, Temp.Length); _KnowledgeBase = new KnowledgeBase(Temp); _Goal = Propositions[Propositions.Length - 1]; Arguments = new bool?[amount_args]; }
/// <summary> /// Returns a array of parsed propositions as a Proposition[] /// Goal prop is last one /// </summary> /// <param name="Propositions">Takes in a String[] of propostion in english, assumes last one is goal</param> /// <returns></returns> public Proposition[] ParseProps(string[] Propositions) { Proposition[] result = new Proposition[Propositions.Length]; for (int i = 0; i < Propositions.Length; i++) { string[] splitted = SplitString(Propositions[i]); result[i] = fringecases(splitted); } return(result); }
/// <summary> Simplify the rule using the specified knowledge. Returns true if the rule was changed. </summary> public bool Resolve(string[] key, SymbolIs[] knowledge) { Proposition newRule = _rule.Resolve(key, knowledge); if (newRule == _rule) { return(false); } _rule = newRule; if (_rule is True) { _definesLiteral = true; } return(true); }
private void GenerateLiteral(Proposition literalPrep) { if (literalPrep is Not) { _literalPrep = literalPrep; _literalVal = new Literal(((Not)literalPrep).Symbol, true); _definesLiteral = true; } else if (literalPrep is Symbol) { _literalPrep = literalPrep; _literalVal = new Literal(((Symbol)literalPrep).ToString(), false); _definesLiteral = true; } }
/// <summary> /// Finds if value is a single original proposition from the test file /// </summary> /// <param name="value"></param> /// <returns></returns> public bool IsOriginal(Proposition value) { foreach (Proposition item in _StartWorld._KnowledgeBase.Propostions) { if (item.Single && (item.getA() == value.getA())) { if (item.ANotted) { return(false); } return(true); } } return(false); }
protected Proposition FindHornRule(List <string> elements) { Proposition totalRule = base.FindImplication(elements); if (totalRule is Implies) { GenerateLiteral(((Implies)totalRule).Implication); _definesLiteral = false; return(((Implies)totalRule).Requirement); } else { GenerateLiteral(totalRule); return(new True()); } }
/// <summary> /// /// </summary> /// <param name="left"></param> /// <param name="oper"></param> /// <param name="right">If right is null make a single prop</param> /// <returns></returns> private Proposition SetProp(string[] left, Operations oper, string[] right) { Proposition returnProp = new Proposition(); if (left.Length == 1) { if (left[0][0] == '~') // "~A" { left[0] = left[0].Substring(1); returnProp.ANotted = true; } returnProp.setA(String2Symbol(left[0])); } else { returnProp.setA(generateProp(left)); } if (right != null) // multi argument prop { if (right.Length == 1) { if (right[0][0] == '~') // "~A" { right[0] = right[0].Substring(1); returnProp.BNotted = true; } returnProp.setB(String2Symbol(right[0])); } else { returnProp.setB(generateProp(right)); } returnProp.Operation = oper; } return(returnProp); }
public override Proposition Resolve(string[] key, SymbolIs[] state) { //Resolve child prepositions. _fact1 = _fact1.Resolve(key, state); _fact2 = _fact2.Resolve(key, state); SymbolIs sol1 = _fact1.CheckSolvable(key, state); SymbolIs sol2 = _fact2.CheckSolvable(key, state); //If the either fact is Unknown return this, otherwise resolve to True if they are the same, or false if they are different. if (sol1 == sol2 && sol1 != SymbolIs.Unknown) { return(new True()); } else if (sol1 != SymbolIs.Unknown && sol2 != SymbolIs.Unknown) { return(new Not(new True())); } else { return(this); } }
public void AddToKB(Proposition PTemp) { _Propostions.Add(PTemp); }
public void setB(int B) { _BRef = null; _B = B; }
/* * public bool[] Arguments * { * get * { * return Arguments; * } * set * { * // if (Arguments == null || value.Length != Arguments.Length) * // throw new System.ArgumentException("Wrong size arguments array"); * Arguments = value; * } * } */ public World(KnowledgeBase KnowledgeBase, Proposition Goal) { _KnowledgeBase = KnowledgeBase; _Goal = Goal; }
public void AddToKB(Proposition PTemp) { _KnowledgeBase.AddToKB(PTemp); }
public void setB(Proposition B) { _BRef = B; _B = -1; }
public void setA(int A) { _ARef = null; _A = A; }
private Proposition generateProp(string[] PropositionString) { Proposition CurrentProp = new Proposition(); if (PropositionString.Length == 3) // base case { return(SetProp(new[] { PropositionString[0] }, String2Operation(PropositionString[1]), new[] { PropositionString[2] })); } if (PropositionString.Length <= 3) { throw new System.ArgumentException("Less than 3 agruements in base case"); } int ParenthesisCount = 0; Operations[] possible_linkers = new Operations[2] { Operations.NotSet, Operations.NotSet }; // before & after the first ( and before and after the last ) int[] Parenthesist_pos = new int[2]; for (int i = 0; i < PropositionString.Length; i++) //find high level brakets, srtring together prop left to right { if (PropositionString[i].Contains("(")) { if (ParenthesisCount == 0) // found the start of a top level braket { Parenthesist_pos[0] = i; if (i > 0) // Dont go out of array bounds { try{ // see if there is a valid operation linker possible_linkers[0] = String2Operation(PropositionString[i - 1]); if (possible_linkers[0] == Operations.Negation) // if it is a not, look past it { if (i > 1) { possible_linkers[0] = String2Operation(PropositionString[i - 2]); } else { possible_linkers[0] = Operations.NotSet; } } } catch (System.ArgumentException) { throw new System.ArgumentException("An operation should always come before a opening bracket"); } } } ParenthesisCount++; } else if (PropositionString[i] == ")") { ParenthesisCount--; if (ParenthesisCount == 0) // found the end of a top level braket { Parenthesist_pos[1] = i; if (Parenthesist_pos[0] == 0 && Parenthesist_pos[1] == PropositionString.Length - 1)// strip brackets if they are around the whole term { string[] stripbrackets = new string[PropositionString.Length - 2]; Array.Copy(PropositionString, 1, stripbrackets, 0, stripbrackets.Length); return(generateProp(stripbrackets)); } if (Parenthesist_pos[0] == 1 && Parenthesist_pos[1] == PropositionString.Length - 1 && PropositionString[0] == "~")// brackets around whole thing but the not { string[] stripbrackets = new string[PropositionString.Length - 3]; Array.Copy(PropositionString, 2, stripbrackets, 0, stripbrackets.Length); CurrentProp = SetProp(stripbrackets); CurrentProp.ANotted = true; return(CurrentProp); } if (i < PropositionString.Length - 1) // Dont go out of array bounds { try{ // see if there is a valid operation linker possible_linkers[1] = String2Operation(PropositionString[i + 1]); } catch (System.ArgumentException) { throw new System.ArgumentException("An operation should always come after a closing bracket"); } } if (possible_linkers[0] == Operations.NotSet && possible_linkers[1] == Operations.NotSet) { throw new System.ArgumentException("Parenthesis Or code error, no prop detected before or after highest lvl brakets"); } // recurse - take high level brackets put it on one side of prop, put whats left on other side if (possible_linkers[0] == Operations.NotSet) // if brakets are on left side { string[] left = new string[Parenthesist_pos[1] - Parenthesist_pos[0] - 1]; //room what is in brakets string[] right = new string[PropositionString.Length - left.Length - 3]; // room for what is left (- 1 for prop symbol -2 for brakets ) Array.Copy(PropositionString, Parenthesist_pos[0] + 1, left, 0, left.Length); // put what is in brakets in left Array.Copy(PropositionString, Parenthesist_pos[1] + 2, right, 0, right.Length); // put what is left in right CurrentProp = SetProp(left, possible_linkers[1], right); if (Parenthesist_pos[0] > 0 && PropositionString[Parenthesist_pos[0] - 1][0] == '~') { CurrentProp.ANotted = true; } return(CurrentProp); } if (possible_linkers[1] == Operations.NotSet) // if brakets are on right side { string[] right = new string[Parenthesist_pos[1] - Parenthesist_pos[0] - 1]; //room what is in brakets string[] left = new string[PropositionString.Length - right.Length - 3]; // room for what is left (- 2 for prop & 0 indexed array) Array.Copy(PropositionString, 0, left, 0, left.Length); // put what is in brakets in left Array.Copy(PropositionString, Parenthesist_pos[0] + 1, right, 0, right.Length); // put what is left in right CurrentProp = SetProp(left, possible_linkers[0], right); if (Parenthesist_pos[0] > 0 && PropositionString[Parenthesist_pos[0] - 1][0] == '~') { CurrentProp.BNotted = true; } return(CurrentProp); } } if (ParenthesisCount < 0) { throw new System.ArgumentException("Too many closing brackets in Prop"); } } if (i == PropositionString.Length - 1 && ParenthesisCount != 0) { throw new System.ArgumentException("Miss matched Parenthesis in Propostion"); } } if (ParenthesisCount != 0) { throw new System.ArgumentException("Miss matched Parenthesis in Propostion (Code logic error)"); } //If there are no brakets continue bellow string[] rightprop = new string[PropositionString.Length - 2]; Array.Copy(PropositionString, 2, rightprop, 0, PropositionString.Length - 2); CurrentProp = SetProp(new[] { PropositionString[0] }, String2Operation(PropositionString[1]), rightprop); return(CurrentProp); }
public Not(Proposition fact) { _fact = fact; }
public void setA(Proposition A) { _ARef = A; _A = -1; }
public Or(Proposition fact1, Proposition fact2) { _fact1 = fact1; _fact2 = fact2; }
public BiConditional(Proposition fact1, Proposition fact2) { _fact1 = fact1; _fact2 = fact2; }
/// <summary> /// A DFS to find paths that return only original propositions from the test file /// </summary> /// <param name="value"></param> /// <returns></returns> public List <int> Chain(Proposition value) { List <int> result = new List <int>(); List <Proposition> temp; // Singualr Proposition if (value.Single) { if (IsOriginal(value)) { result = new List <int>(); result.Add(value.getA()); return(result); } else if ((temp = FindReferal(value)).Count != 0) { foreach (Proposition Refered in temp) { result = Chain(Refered); if (result.Count != 0) { result.Add(Refered.getB()); } } } else { return(new List <int>()); } } // Implication Proposition else if (value.Operation == Operations.Implication) { if (value.IsAref()) { result = Chain(value.getARef()); } else if (IsOriginal(value.getA())) { result = new List <int>(); result.Add(value.getA()); return(result); } else if ((temp = FindReferal(value.getA())).Count != 0) { foreach (Proposition Refered in temp) { result = Chain(Refered); if (result.Count != 0) { result.Add(Refered.getB()); } } return(result); } else { return(new List <int>()); } } // And Proposition else if (value.Operation == Operations.Conjunction) { List <int> MiniListA = new List <int>(); List <int> MiniListB = new List <int>(); // Find A referals // if a ref if (value.IsAref()) { MiniListA = Chain(value.getARef()); } // If A is a Singular Original else if (IsOriginal(value.getA())) { MiniListA = new List <int>(); MiniListA.Add(value.getA()); } // Else find referals of A else if ((temp = FindReferal(value.getA())).Count != 0) { foreach (Proposition Refered in temp) { MiniListA = Chain(Refered); if (MiniListA.Count() != 0) { MiniListA.Add(value.getA()); } } } // If no referals else { return(new List <int>()); } // Find B referals // If b ref if (value.IsBref()) { MiniListB = Chain(value.getBRef()); } // If B is a Singular Original else if (IsOriginal(value.getB())) { MiniListB = new List <int>(); MiniListB.Add(value.getB()); } // else find referals else if ((temp = FindReferal(value.getB())).Count != 0) { foreach (Proposition Refered in temp) { MiniListB = Chain(Refered); if (MiniListB.Count() != 0) { MiniListB.Add(value.getB()); } } } // If no referals else { return(new List <int>()); } // If nether null then add together and return if ((MiniListA.Count() == 0) || (MiniListB.Count() == 0)) { return(new List <int>()); } result = new List <int>(); result.AddRange(MiniListA); result.AddRange(MiniListB); return(result); } // OR Proposition else if (value.Operation == Operations.Disjunction) { List <int> MiniListA = new List <int>(); List <int> MiniListB = new List <int>(); // Find referals of A // If A ref if (value.IsAref()) { MiniListA = Chain(value.getARef()); } // If A is a Singular Original else if (IsOriginal(value.getA())) { MiniListA = new List <int>(); MiniListA.Add(value.getA()); } // Else find referals else if ((temp = FindReferal(value.getA())).Count != 0) { foreach (Proposition Refered in temp) { MiniListA = Chain(Refered); if (MiniListA.Count() != 0) { MiniListA.Add(value.getA()); } } } //Find referals of B // If B ref if (MiniListA.Count() == 0) { if (value.IsBref()) { MiniListB = Chain(value.getBRef()); } // If B is a Singular Original else if (IsOriginal(value.getB())) { MiniListB = new List <int>(); MiniListB.Add(value.getB()); } // Else find referals else if ((temp = FindReferal(value.getB())).Count != 0) { foreach (Proposition Refered in temp) { MiniListB = Chain(Refered); if (MiniListB.Count() != 0) { MiniListB.Add(value.getB()); } } } } // If both empty, return empty if (MiniListA.Count == 0 && MiniListB.Count == 0) { return(new List <int>()); } //else add together and return result = new List <int>(); result.AddRange(MiniListA); result.AddRange(MiniListB); return(result); } // Finally return the result return(result); }
public Rule(string ruleAsText) { debugTextVersion = ruleAsText.Trim(); _rule = FindImplication(GenerateElements(debugTextVersion)); }
public Implies(Proposition requirement, Proposition implication) { _requirement = requirement; _implication = implication; }