public void Copy_CopyingBiImplicationWithTwoRandomVariableSymbols_ExpectedDifferentReferencesForConnective() { // Arrange // Act // Assert BiImplication biImplication = generateBiImplication(); Copy_CopyingBinaryConnectiveWithTwoRandomVariableSymbols_ExpectedDifferentReferencesForConnective(biImplication); }
public void Constructor_CreateNegatedBiImplicationOfLiterals_BetaRuleShouldBeAppliedBothChildrenShouldHaveSetsOfTwoElementsOneLiteralAndOneNegatedLiteralInEachSet() { // Arrange Negation negatedBiImplication = new Negation(); BiImplication biImplication = (BiImplication)PropositionGenerator.CreateBinaryConnectiveWithRandomSymbols(BiImplication.SYMBOL); negatedBiImplication.LeftSuccessor = biImplication; HashSet <Proposition> propositions = new HashSet <Proposition>() { negatedBiImplication }; // Act SemanticTableauxElement semanticTableauxElement = new SemanticTableauxElement(propositions); HashSet <Proposition> propositionSetOfLeftChild = semanticTableauxElement.LeftChild.Propositions; HashSet <Proposition> propositionSetOfRightChild = semanticTableauxElement.RightChild.Propositions; int actualNumberOfPropositionsInLeftSet = propositionSetOfLeftChild.Count; int actualNumberOfPropositionsInRightSet = propositionSetOfRightChild.Count; int expectedNumberOfPropositionsInSet = 2; // Assert string message = "Because both the left and right set should have two children in their set after applying beta rule for negated bi-implication."; actualNumberOfPropositionsInLeftSet.Should().Be(expectedNumberOfPropositionsInSet, message); actualNumberOfPropositionsInRightSet.Should().Be(expectedNumberOfPropositionsInSet, message); TestSetToHaveOneLiteralAndOneNegatedLiteral(propositionSetOfLeftChild); TestSetToHaveOneLiteralAndOneNegatedLiteral(propositionSetOfRightChild); }
public void Visit(BiImplication visitable) { var root = ConvertBiImplicationToDisjunction(visitable); root.Belongs = visitable.Belongs; visitable.Belongs.Components.Add(root); visitable.Belongs.Components.Remove(visitable); Calculate(root); }
public void BiImplicationConstructorTest() { BiImplication a = new BiImplication(); Assert.IsNotNull(a.Childs); Assert.AreEqual('=', a.Name); Assert.AreEqual(2, a.nOperand); Assert.AreEqual(SymbolType.operational, a.Type); }
public void Calculate_DetermineAllPossibleValuesBetweenTwoPropositionVariables_ExpectedToReturnTrueWhenBothHaveEqualTruthValues(bool leftTruthValue, bool rightTruthValue, bool expectedTruthValue) { // Arrange BiImplication biImplication = generateBiImplication(); string message = "because bi implication is true when both left and right successor have the same truth value."; // Act // Assert Calculate_DetermineAllPossibleValuesBetweenTwoPropositionVariables(biImplication, message, leftTruthValue, rightTruthValue, expectedTruthValue); }
public void toNandTest() { Variable A = new Variable('A'); Variable B = new Variable('B'); BiImplication iff = new BiImplication(A, B); Symbol nand = iff.toNand(); Assert.AreEqual("(((A % A) % (B % B)) % (A % B))", nand.ToString()); }
public void OperateTwoTest() { Variable p = new Variable('p'); Variable q = new Variable('q'); BiImplication a = new BiImplication(); a.Operate(p, q); Assert.AreEqual(p, a.Childs[0]); Assert.AreEqual(q, a.Childs[1]); Assert.ThrowsException <ArgumentNullException>(() => a.Operate(null, null)); }
public void OperateListTest() { List <Variable> vars = new List <Variable>() { new Variable('p'), new Variable('q') }; BiImplication a = new BiImplication(); a.Operate(vars); Assert.AreEqual(vars[0], a.Childs[0]); Assert.AreEqual(vars[1], a.Childs[1]); }
public void Replace_ReplacingExistingBoundVariableInComplexExpression_ShouldReturnTrueForChildrenContainingReplacedVariable() { // Arrange char variableToBeReplaced = 'w'; List <char> variablesSetOne = new List <char>() { 'u', variableToBeReplaced }; // Does not contain the replacement variable List <char> variablesSetTwo = new List <char>() { 'v', 'x' }; Predicate predicateWithReplacementVariable = new Predicate(PREDICATE_SYMBOL, variablesSetOne); // So we have unique references Predicate predicateWithoutReplacementVariableOne = new Predicate(PREDICATE_SYMBOL, variablesSetTwo); Predicate predicateWithoutReplacementVariableTwo = new Predicate(PREDICATE_SYMBOL, variablesSetTwo); BinaryConnective root = new Conjunction(); BinaryConnective rootLeft = new BiImplication(); rootLeft.LeftSuccessor = predicateWithoutReplacementVariableOne; rootLeft.RightSuccessor = predicateWithReplacementVariable; root.LeftSuccessor = rootLeft; root.RightSuccessor = predicateWithoutReplacementVariableTwo; char replacementVariable = 'c'; // Act root.Replace(variableToBeReplaced, replacementVariable); bool isReplacedOnPredicateWithVariable = predicateWithReplacementVariable.IsReplaced(variableToBeReplaced); bool isReplacedOnPredicateWithoutVariableOne = predicateWithoutReplacementVariableOne.IsReplaced(variableToBeReplaced); bool isReplacedOnPredicateWithoutVariableTwo = predicateWithoutReplacementVariableTwo.IsReplaced(variableToBeReplaced); // Assert isReplacedOnPredicateWithVariable.Should().BeTrue($"Since the bound variable {variableToBeReplaced} should be repalced replaced by {replacementVariable}"); isReplacedOnPredicateWithoutVariableOne.Should().BeFalse($"Since the predicate does NOT contain {variableToBeReplaced}"); isReplacedOnPredicateWithoutVariableTwo.Should().BeFalse($"Since the predicate does NOT contain {variableToBeReplaced}"); }
public void BiImplicationConstructorTest1() { Variable p = new Variable('p'); Variable q = new Variable('q'); BiImplication a = new BiImplication(p, q); Assert.IsNotNull(a.Childs); Assert.AreEqual('=', a.Name); Assert.AreEqual(2, a.nOperand); Assert.AreEqual(SymbolType.operational, a.Type); Assert.AreEqual(p, a.Childs[0]); Assert.AreEqual(q, a.Childs[1]); // Assert.ThrowsException <ArgumentNullException>(() => new BiImplication(null, null)); }
private Component ConvertBiImplicationToDisjunction(BiImplication visitable) { var root = new Disjunction(); var left = new Conjunction(); var right = new Conjunction(); var rightLeft = new Negation(); var rightRight = new Negation(); _binaryTree.InsertNode(left, BinaryTree.CloneNode(visitable.LeftNode, _binaryTree)); _binaryTree.InsertNode(left, BinaryTree.CloneNode(visitable.RightNode, _binaryTree)); _binaryTree.InsertNode(rightLeft, BinaryTree.CloneNode(visitable.LeftNode, _binaryTree)); _binaryTree.InsertNode(rightRight, BinaryTree.CloneNode(visitable.RightNode, _binaryTree)); _binaryTree.InsertNode(right, rightLeft); _binaryTree.InsertNode(right, rightRight); _binaryTree.InsertNode(root, left); _binaryTree.InsertNode(root, right); return(root); }
public void GetTruthValueArrayTest() { Variable p = new Variable('p'); Variable q = new Variable('q'); BiImplication a = new BiImplication(p, q); bool[] dict = new bool[130]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { dict['p'] = i == 1; dict['q'] = j == 1; Assert.AreEqual(i == j, a.GetTruthValue(dict)); } } }
public void ToStringTest() { Variable p = new Variable('p'); Variable q = new Variable('q'); BiImplication a = new BiImplication(p, q); Assert.AreEqual("(p = q)", a.ToString()); Not Left = new Not(p); Or Right = new Or(p, q); a.Operate(Left, Right); Assert.AreEqual("(~p = (p | q))", a.ToString()); Left = new Not(new Nand(p, q)); a.Operate(Left, Right); Assert.AreEqual("(~(p % q) = (p | q))", a.ToString()); }
public void IsClosed_CreateNegatedBiImplicationOfLiteralsWithLiteralsInParentSetToCloseBranches_ParentElementShouldBeClosed() { // Arrange BiImplication biImplication = (BiImplication)PropositionGenerator.CreateBinaryConnectiveWithRandomSymbols(BiImplication.SYMBOL); Proposition literal = biImplication.LeftSuccessor; Negation negatedLiteral = new Negation(); negatedLiteral.LeftSuccessor = biImplication.LeftSuccessor; HashSet <Proposition> propositions = new HashSet <Proposition>() { literal, biImplication, negatedLiteral }; // Act SemanticTableauxElement semanticTableauxElement = new SemanticTableauxElement(propositions); // Assert semanticTableauxElement.IsClosed().Should().BeTrue("Because it should be possible to close branches when there is at least any of the literals in the parent set as well as any negated literal"); }
public void GetTruthValueDictTest() { Variable p = new Variable('p'); Variable q = new Variable('q'); BiImplication a = new BiImplication(p, q); Dictionary <char, bool> dict = new Dictionary <char, bool>(); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { dict['p'] = i == 1; dict['q'] = j == 1; Assert.AreEqual(i == j, a.GetTruthValue(dict)); } } dict.Remove('p'); Assert.ThrowsException <KeyNotFoundException>( () => a.GetTruthValue(dict)); }
public void Visit(BiImplication visitable) { var nandRoot = new Disjunction(); var nandLeft = new Conjunction(); var nandRight = new Conjunction(); var nandLeftLeft = new Negation(); var nandLeftRight = new Negation(); InsertNodeSingle(nandLeftLeft, visitable.LeftNode); InsertNodeSingle(nandLeftRight, visitable.RightNode); BinaryTree.InsertNode(nandRight, visitable.LeftNode); BinaryTree.InsertNode(nandRight, visitable.RightNode); BinaryTree.InsertNode(nandLeft, nandLeftLeft); BinaryTree.InsertNode(nandLeft, nandLeftRight); BinaryTree.InsertNode(nandRoot, nandLeft); BinaryTree.InsertNode(nandRoot, nandRight); Calculate(nandRoot); BinaryTree.Root = nandRoot.Nand; visitable.Nand = nandRoot.Nand; }
public void Visit(BiImplication visitable) => GenerateInfixGenerator(visitable, '=');
/** * The result of the expansion is an Or, * which split the current node into several branches * * A v B -> A v B (2 branches) * ~(A ^ B) = ~A v ~B * ~(A <=> B) = (A ^ ~B) v (~A ^ B) * * A => B = ~A v B * A <=> B = (A ^ B) v (~A ^ ~B) * */ public void ExpandToOrTest() { Symbol s; Variable A = new Variable('a'); Variable B = new Variable('b'); List <Symbol> res; List <Symbol> expected; // test 01 s = new Or(A, B); res = SemanticTableau.ExpandToOr(s); expected = new List <Symbol>() { A, B }; Assert.AreEqual(expected.Count(), res.Count()); for (int i = 0; i < expected.Count(); i++) { Assert.AreEqual(expected[i], res[i]); } // test 01 s = new Or(A, B); res = SemanticTableau.ExpandToOr(s); expected = new List <Symbol>() { A, B }; Assert.AreEqual(expected.Count(), res.Count()); for (int i = 0; i < expected.Count(); i++) { Assert.AreEqual(expected[i], res[i]); } // test 02 s = new Not(new And(A, B)); res = SemanticTableau.ExpandToOr(s); expected = new List <Symbol>() { new Not(A), new Not(B) }; Assert.AreEqual(expected.Count(), res.Count()); for (int i = 0; i < expected.Count(); i++) { Assert.AreEqual(expected[i].ToString(), res[i].ToString()); } // test 03: ~(A <=> B) = (A ^ ~B) v (~A ^ B) s = new Not(new BiImplication(A, B)); res = SemanticTableau.ExpandToOr(s); expected = new List <Symbol>() { new And(A, new Not(B)), new And(new Not(A), B) }; Assert.AreEqual(expected.Count(), res.Count()); for (int i = 0; i < expected.Count(); i++) { Assert.AreEqual(expected[i].ToString(), res[i].ToString()); } // test 04: A => B = ~A v B s = new Implication(A, B); res = SemanticTableau.ExpandToOr(s); expected = new List <Symbol>() { new Not(A), B }; Assert.AreEqual(expected.Count(), res.Count()); for (int i = 0; i < expected.Count(); i++) { Assert.AreEqual(expected[i].ToString(), res[i].ToString()); } // test 05: A <=> B = (A ^ B) v (~A ^ ~B) s = new BiImplication(A, B); res = SemanticTableau.ExpandToOr(s); expected = new List <Symbol>() { new And(A, B), new And(new Not(A), new Not(B)) }; Assert.AreEqual(expected.Count(), res.Count()); for (int i = 0; i < expected.Count(); i++) { Assert.AreEqual(expected[i].ToString(), res[i].ToString()); } }
//Methods public Proposition InsertInBinaryTree(ref string s) { Proposition root = null; switch (s[0]) { case '>': { root = new Implication(); s = s.Substring(2); while (s[0] != ',') { root.LeftNode = InsertInBinaryTree(ref s); s = s.Substring(1); } s = s.Substring(1); while (s[0] != ')') { root.RightNode = InsertInBinaryTree(ref s); s = s.Substring(1); } break; } case '=': { root = new BiImplication(); s = s.Substring(2); while (s[0] != ',') { root.LeftNode = InsertInBinaryTree(ref s); s = s.Substring(1); } s = s.Substring(1); while (s[0] != ')') { root.RightNode = InsertInBinaryTree(ref s); s = s.Substring(1); } break; } case '&': { root = new And(); s = s.Substring(2); while (s[0] != ',') { root.LeftNode = InsertInBinaryTree(ref s); s = s.Substring(1); } s = s.Substring(1); while (s[0] != ')') { root.RightNode = InsertInBinaryTree(ref s); s = s.Substring(1); } break; } case '|': { root = new Or(); s = s.Substring(2); while (s[0] != ',') { root.LeftNode = InsertInBinaryTree(ref s); s = s.Substring(1); } s = s.Substring(1); while (s[0] != ')') { root.RightNode = InsertInBinaryTree(ref s); s = s.Substring(1); } break; } case '~': { root = new Negation(); s = s.Substring(2); while (s[0] != ')') { root.LeftNode = InsertInBinaryTree(ref s); s = s.Substring(1); } break; } case '%': { root = new NAND(); s = s.Substring(2); while (s[0] != ',') { root.LeftNode = InsertInBinaryTree(ref s); s = s.Substring(1); } s = s.Substring(1); while (s[0] != ')') { root.RightNode = InsertInBinaryTree(ref s); s = s.Substring(1); } break; } default: { root = new Pro(); (root as Pro).Notation = s[0].ToString(); break; } } return(root); }
public void Visit(BiImplication visitable) => visitable.Data = (visitable.LeftNode.Data && visitable.RightNode.Data) || (!visitable.LeftNode.Data && !visitable.RightNode.Data);
/// <summary> /// Build the Abstract Syntax Tree using prefix expression input /// </summary> public void Build() { // Remove white space and separator (assume that operand has only 1 character) Expression = Regex.Replace(Expression, @"\s+", ""); if (Expression.Contains(",,")) { throw new ArgumentException(); } Expression = Regex.Replace(Expression, ",", ""); // Mark the appeared variables int[] variableAppeared = new int[130]; bool[] currentlyBounded = new bool[130]; // The stack used for building AST List <Symbol> utilityStack = new List <Symbol>(); for (int i = 0; i < Expression.Length; i++) { /////// Close parenthesis, the signal to trigger an operation if (Expression[i] == ')') { HandleCloseBracket(utilityStack, currentlyBounded, variableAppeared); } /////// End Close parenthesis else if (availablePlaceholder.Contains(Expression[i])) { utilityStack.Add(new Placeholder(Expression[i])); } // Predicate P(x,y,z,t) else if (availablePredicate.Contains(Expression[i])) { //NumPredicateVariables[Expression[i]] = Predicate P = new Predicate(Expression[i]); utilityStack.Add(P); // P takes 0 object variables if (i == Expression.Length - 1 || Expression[i + 1] != '(') { if (NumPredicateVariables[P.Name] > 0) { throw new InvalidDataException("Wrong number of object variables for predicate " + P.Name); } NumPredicateVariables[P.Name] = 0; P.nChild = 0; } variableAppeared[P.Name]++; } // Variable else if (availableVariable.Contains(Expression[i])) { variableAppeared[Expression[i]]++; Variable v = new Variable(Expression[i]); if (currentlyBounded[Expression[i]]) { v.Bounded = true; } utilityStack.Add(v); } // Constant (True/False) else if (availableConstant.Contains(Expression[i])) { Constant cons = new Constant(Expression[i]); utilityStack.Add(cons); } // Operator else if (availableOperator.Contains(Expression[i])) { Symbol currentOperator; switch (Expression[i]) { case '&': currentOperator = new And(); break; case '|': currentOperator = new Or(); break; case '%': currentOperator = new Nand(); break; case '=': currentOperator = new BiImplication(); break; case '>': currentOperator = new Implication(); break; case '~': currentOperator = new Not(); break; default: currentOperator = new And(); break; } // add to stack utilityStack.Add(currentOperator); } else if (availableQuantifier.Contains(Expression[i])) { HasQuantifier = true; char quantName = Expression[i]; string tmpBoundVar = ""; for (int j = i + 1; j < Expression.Length; j++) { if (availableVariable.Contains(Expression[j])) { tmpBoundVar += Expression[j]; currentlyBounded[Expression[j]] = true; } else if (Expression[j] != '.' || j == i + 1) { throw new ArgumentException("Wrong input format at " + j); } else { i = j; break; } } Symbol currentQuantifier; switch (quantName) { case '@': currentQuantifier = new Universal(tmpBoundVar); break; default: currentQuantifier = new Existential(tmpBoundVar); break; } utilityStack.Add(currentQuantifier); } // Invalid symbol else { throw new ArgumentException("Invalid symbol."); } } if (utilityStack.Count != 1) { throw new ArgumentException("Wrong format."); } Root = utilityStack[0]; for (char c = 'A'; c <= 'Z'; c++) { if (variableAppeared[c] > 0) { ListVariable.Add(c); } } for (char c = 'a'; c <= 'z'; c++) { if (variableAppeared[c] > 0) { ListVariable.Add(c); } } }
public static Component CloneNode(Component node, BinaryTree bt, char current = '!', char rename = '!') { Component newNode; if (node is BiImplication) { newNode = new BiImplication(); } else if (node is Implication) { newNode = new Implication(); } else if (node is Conjunction) { newNode = new Conjunction(); } else if (node is Disjunction) { newNode = new Disjunction(); } else if (node is Negation negation) { newNode = new Negation(); ((Negation)newNode).GammaProcessed = negation.GammaProcessed; } else if (node is Nand) { newNode = new Nand(); } else if (node is TrueFalse) { newNode = new TrueFalse(((TrueFalse)node).Data); } else if (node is Predicate predicate) { newNode = new Predicate(node.Symbol); predicate.ObjectVariables.Variables.ForEach(x => ((IVariableContainer)newNode) .ObjectVariables.AddPropositionalVariable(CloneVariableForPredicate(x, current, rename)) ); } else if (node is Universal universal) { newNode = new Universal(); universal.ObjectVariables.Variables.ForEach(x => ((IVariableContainer)newNode) .ObjectVariables.AddPropositionalVariable(CloneVariableForPredicate(x, current, rename)) ); ((Universal)newNode).GammaProcessed = universal.GammaProcessed; } else if (node is Existential existential) { newNode = new Existential(); existential.ObjectVariables.Variables.ForEach(x => ((IVariableContainer)newNode) .ObjectVariables.AddPropositionalVariable(CloneVariableForPredicate(x, current, rename)) ); } else {// newNode = new Variable(((Variable)node).Symbol, ((Variable)node).BindVariable); bt.PropositionalVariables.AddPropositionalVariable((Variable)newNode); } newNode.Parent = node.Parent; if (node is CompositeComponent) { if (node is Negation) { newNode.LeftNode = CloneNode(node.LeftNode, bt, current, rename); } else { newNode.LeftNode = node.LeftNode != null?CloneNode(node.LeftNode, bt, current, rename) : node.LeftNode; newNode.RightNode = node.RightNode != null?CloneNode(node.RightNode, bt, current, rename) : node.RightNode; } } newNode.NodeNumber++; return(newNode); }
// help with creating binary tree private void InsertTreeHelper(ref Node n, string c) { switch (c) { case "&": n = new AndOperator(c); n.LeftNode = this._myStack.Pop(); n.RightNode = this._myStack.Pop(); this._myStack.Push(n); break; case "|": n = new OrOperator(c); n.LeftNode = this._myStack.Pop(); n.RightNode = this._myStack.Pop(); this._myStack.Push(n); break; case ">": n = new Implication(c); n.LeftNode = this._myStack.Pop(); n.RightNode = this._myStack.Pop(); this._myStack.Push(n); break; case "~": n = new Negation(c); n.RightNode = this._myStack.Pop(); this._myStack.Push(n); break; case "=": n = new BiImplication(c); n.LeftNode = this._myStack.Pop(); n.RightNode = this._myStack.Pop(); this._myStack.Push(n); break; case "%": n = new NANDOperator(c); n.LeftNode = this._myStack.Pop(); n.RightNode = this._myStack.Pop(); this._myStack.Push(n); break; case "!": n = new Existential(c, new PredicateVariable(this._myStack.Pop().ToString())); n.RightNode = this._myStack.Pop(); this._myStack.Push(n); break; case "@": n = new Universal(c, new PredicateVariable(this._myStack.Pop().ToString())); n.RightNode = this._myStack.Pop(); this._myStack.Push(n); break; default: if (char.IsLower(char.Parse(c))) { n = new PredicateVariable(c); this._myStack.Push(n); } else { n = new Variable(c); int total = this._myStack.Count; for (int i = 0; i < total; i++) { if (_myStack.Peek() is PredicateVariable p) { n.AddVariable(p); _myStack.Pop(); } } this._myStack.Push(n); } break; } }