public static bool ValidateTautology(TableuxNode tree) { if (tree == null) { throw new ArgumentNullException(); } int leafsCounter = 0; int truthsCounter = 0; void IterateOverLeafs(TableuxNode node) { if (node == null) { return; } if (node.Left == null && node.Right == null) { // we are in a leaf leafsCounter++; truthsCounter += TableuxNode.IsTautology(node.ListOfNodes) ? 1 : 0; } else { IterateOverLeafs(node.Left); IterateOverLeafs(node.Right); } } IterateOverLeafs(tree); return(leafsCounter == truthsCounter); }
public static int CountLeaves(TableuxNode tree) { int leafsCounter = 0; void IterateOverLeafs(TableuxNode node) { if (node == null) { return; } if (node.Left == null && node.Right == null) { // we are in a leaf leafsCounter++; } else { IterateOverLeafs(node.Left); IterateOverLeafs(node.Right); } } IterateOverLeafs(tree); return(leafsCounter); }
/// <summary> /// Inserts a new TableuxNode into the current one /// </summary> /// <param name="toInsert"></param> /// <returns></returns> private bool Insert(TableuxNode toInsert) { if (Left == null) { Left = toInsert; return(true); } if (Right == null) { Right = toInsert; return(true); } return(false); }
public Tableux(Node root, bool containsQuantifiers = false) { GlobalCounter.nrOfBetaRules = 1; GlobalCounter.nrOfGammaRules = 0; GlobalCounter.nrOfGammaRules = 0; GlobalCounter.tautologyBeenIdentified = false; GlobalCounter.nrOfTruthsReturnedByQuantifiers = 0; GlobalCounter.nrOfTruthsReturnedNotByQuantifiers = 0; this.containsQuantifiers = containsQuantifiers; _root = Functions.NegateTree(root); List <Node> passIN = new List <Node>(1); passIN.Add(_root); tree = new TableuxNode(passIN); treeHasQuantifiers = HasQuantifiers(root); BuildTableux(tree); }
private void BuildTableux(TableuxNode root) { void ResetVariable() { isTautology = false; treeHasQuantifiers = HasQuantifiers(_root); truthsCounterForQuantifiers = 0; GlobalCounter.nrOfBetaRules = 1; GlobalCounter.nrOfGammaRules = 0; GlobalCounter.tautologyBeenIdentified = false; GlobalCounter.nrOfTruthsReturnedByQuantifiers = 0; GlobalCounter.nrOfTruthsReturnedNotByQuantifiers = 0; } if (!root.TableuxIsSimplifiable()) { return; } ResetVariable(); root.Generate(); }
public void Generate() { if (GlobalCounter.tautologyBeenIdentified || GlobalCounter.nrOfGammaRules > 250) { return; } if (!TableuxIsSimplifiable()) { return; } /* get the most important tree to work on (NotNode based trees come first always) */ var(priorityTree, position) = GetPriorityTree(); /* get id of all biimplications or nands if there are any */ Functions.GetRidOfBiImplicationAndNand(ref priorityTree); /* create a list that contains all previous elements but the one that you're gonna apply alpha/beta rules to */ var newList = GetListWithoutPriorityTree(position); // everything except for a tree that we're going to apply rules onto bool betaRuleWorked = false; if (IsBetaRule(priorityTree)) { GlobalCounter.nrOfBetaRules++; var(leftBranchList, rightBranchList) = ApplyBetaRules(priorityTree); leftBranchList.AddRange(newList); rightBranchList.AddRange(newList); TableuxNode newLeftNodeToInsert = new TableuxNode(leftBranchList); TableuxNode newRightNodeToInsert = new TableuxNode(rightBranchList); // insert two new nodes if (!(Insert(newLeftNodeToInsert) && Insert(newRightNodeToInsert))) { MessageBox.Show("Problems detected"); } else { betaRuleWorked = true; } } else if (IsDeltaRule(priorityTree)) { newList.AddRange(ApplyDeltaRules(priorityTree)); } else if (IsGammaRule(priorityTree)) { newList.AddRange(ApplyGammaRules(priorityTree)); GlobalCounter.nrOfGammaRules++; if (GlobalCounter.nrOfGammaRules > 250) { MessageBox.Show( "Have done 250 iterations... didn't find anything. So it's probably not a tautology"); return; } } else { newList.AddRange(ApplyAlphaRules(priorityTree)); } // check if beta rule has been applied, if so, then do nothing and proceed to work on left and right branches // if beta rule wasn't applied, then insert alpha-rule if (!betaRuleWorked) { TableuxNode newNodeToInsert = new TableuxNode(newList); var insertionResult = Insert(newNodeToInsert); if (!insertionResult) { throw new Exception("Problems detected"); } } if (Tableux.treeHasQuantifiers) { var isItTautologyNow = IsTautologyQuantifiers(listOfNodes); if (isItTautologyNow) { GlobalCounter.nrOfTruthsReturnedByQuantifiers++; } if (GlobalCounter.nrOfTruthsReturnedByQuantifiers == GlobalCounter.nrOfBetaRules) { MessageBox.Show("It's a tautology!"); GlobalCounter.tautologyBeenIdentified = true; return; } } else { var isTautologyNow = IsTautology(listOfNodes); if (isTautologyNow) { GlobalCounter.nrOfTruthsReturnedNotByQuantifiers++; } if (GlobalCounter.nrOfTruthsReturnedNotByQuantifiers == GlobalCounter.nrOfBetaRules) { GlobalCounter.tautologyBeenIdentified = true; return; } } /* recursively do it all over again until there's no more work that needs to be done */ Left?.Generate(); Right?.Generate(); }