def testSignedForm(self, f, expTypeT, expSfsT): self.case += 1 self.tested += 1 print("CASE %d: %s" % (self.case, f.toString())) sfsT = frozenset() sfsF = frozenset() try: start = now() typeT = f.getType(True) typeF = f.getType(False) sfsT = frozenset( [signedFormToString(sf) for sf in f.signedSubf(True)]) sfsF = frozenset( [signedFormToString(sf) for sf in f.signedSubf(False)]) duration = now() - start except KeyboardInterrupt: raise KeyboardInterrupt() except: printException() if stopOnError: raise FailedTestException() return expSfsF = frozenset( [signedFormToString((not s, f)) for s, f in expSfsT]) expSfsT = frozenset([signedFormToString(sf) for sf in expSfsT]) sameT = expSfsT == sfsT sameF = expSfsF == sfsF okTypeT = len(expSfsT) <= 1 or expTypeT == typeT okTypeF = len(expSfsF) <= 1 or (ALPHA if expTypeT == BETA else BETA) == typeF self.time += duration if sameT and sameF and okTypeT and okTypeF: self.passed += 1 print('PASSED: time: %12.9f' % (duration, )) else: print('FAILED:') fstrT = signedFormToString((True, f)) fstrF = signedFormToString((False, f)) if not okTypeT: print('Unexpected type of %s: %s\n' % (fstrT, self.typeToString(typeT))) if not okTypeF: print('Unexpected type of %s: %s\n' % (fstrF, self.typeToString(typeF))) if not sameT: print('Unexpected subformulas of %s:\n%s\n' % (fstrT, '\n'.join(sfsT))) if not sameF: print('Unexpected subformulas of %s:\n%s\n' % (fstrF, '\n'.join(sfsF))) if stopOnError: raise FailedTestException() print('')
def testSignedForm(self, f, expTypeT, expSfsT): self.case += 1 self.tested += 1 print("CASE %d: %s" % (self.case, f.toString())) sfsT = frozenset() sfsF = frozenset() try: start = now() typeT = f.getType(True) typeF = f.getType(False) sfsT = frozenset([signedFormToString(sf) for sf in f.signedSubf(True)]) sfsF = frozenset([signedFormToString(sf) for sf in f.signedSubf(False)]) duration = now() - start except KeyboardInterrupt: raise KeyboardInterrupt() except: printException() if stopOnError: raise FailedTestException() return expSfsF = frozenset([ signedFormToString((not s, f)) for s, f in expSfsT ]) expSfsT = frozenset([ signedFormToString(sf) for sf in expSfsT ]) sameT = expSfsT == sfsT sameF = expSfsF == sfsF okTypeT = len(expSfsT) <= 1 or expTypeT == typeT okTypeF = len(expSfsF) <= 1 or (ALPHA if expTypeT == BETA else BETA) == typeF self.time += duration if sameT and sameF and okTypeT and okTypeF: self.passed += 1 print('PASSED: time: %12.9f' % (duration,)) else: print('FAILED:') fstrT = signedFormToString((True, f)) fstrF = signedFormToString((False, f)) if not okTypeT: print( 'Unexpected type of %r: %s\n' % (fstrT, self.typeToString(typeT)) ) if not okTypeF: print( 'Unexpected type of %r: %s\n' % (fstrF, self.typeToString(typeF)) ) if not sameT: print( 'Wrong subformulas of %r:\n%s\n' % (fstrT, '\n'.join(sfsT)) ) if not sameF: print( 'Wrong subformulas of %r:\n%s\n' % (fstrF, '\n'.join(sfsF)) ) if stopOnError: raise FailedTestException() print('')
def testTableau(self, expect_closed, sfs): self.case += 1 self.tested += 1 strSfs = [signedFormToString(sf) for sf in sfs ] print("CASE %d: %s" % (self.case, '; '.join(strSfs))) tableau = Node(False, Variable("")) try: start = now() tableau = TableauBuilder().build(sfs) duration = now() - start except KeyboardInterrupt: raise KeyboardInterrupt() except: printException() if stopOnError: raise FailedTestException() return if not isinstance(tableau, Tableau): print('FAILED: not a tableau.Tableau: %s' % type(tableau)) print() return if not isinstance(tableau.root, Node): print('FAILED: not a tableau.Node: %s' % type(tableau)) print() return closed = tableau.isClosed() badStructure = False try: self.testTableauStructure(tableau.root, [], strSfs) except BadTableauException as err: badStructure = str(err) except: printException() if stopOnError: raise FailedTestException() return size = tableau.size() self.time += duration self.size += size if closed: self.closed += 1 passed = closed == expect_closed and not badStructure if passed: self.passed += 1 print('PASSED: time: %12.9f tableau size: %3d %s' % (duration, size, self.closedToString(closed))) else: print('FAILED: ') if not passed or showAll: print('=====TABLEAU=====\n%s\n%s' % (tableau.toString(), '='*13)) if not passed: if closed != expect_closed: print('Tableau is %s, but should be %s' % (self.closedToString(closed), self.closedToString(expect_closed))) if badStructure: print(badStructure) if stopOnError: raise FailedTestException() print('')
def testTableauStructure(self, node, ancestors, strSfs): strSf = signedFormToString((node.sign, node.formula)) if node.source == None: if strSf not in strSfs: raise BadTableauException( 'Node (%d) has no source node and its formula not initial.' % (node.number,) ) elif node.source not in ancestors: raise BadTableauException( 'Node (%d) has source (%d) which is not one of its ancestors.' % (node.number, node.source.number,) ) else: src = node.source parent = ancestors[-1] strSourceSubfs = [ signedFormToString(sf) for sf in src.formula.signedSubf(src.sign) ] if strSf not in strSourceSubfs: raise BadTableauException( 'Node (%d) does not contain a subformula of (%d).' % ( node.number, src.number ) ) if (src.formula.getType(src.sign) == ALPHA and len(parent.children) != 1): raise BadTableauException( 'Node (%d) is a result of ALPHA rule for (%d) -- must have no siblings.' % ( node.number, src.number ) ) if (src.formula.getType(src.sign) == BETA and len(parent.children) != len(strSourceSubfs)): raise BadTableauException( 'Node (%d) should have %d siblings -- results of BETA rule for (%d).' % ( node.number, len(strSourceSubfs)-1, src.number ) ) ancestors.append(node) for child in node.children: self.testTableauStructure(child, ancestors, strSfs) if node.closed: # this is a 'closing' node if not isinstance(node.closedFrom, Node): raise BadTableauException( 'Close pair reference for closed node (%d) is not a node' % node.number ) if not node.closedFrom in ancestors: raise BadTableauException( 'Close pair (%d) is not an ancestor of the closed node (%d).' % (node.closedFrom.number, node.number) ) if node.sign != (not node.closedFrom.sign): raise BadTableauException( 'Close pair formula signs are wrong (%d, %d)' % (node.number, node.closedFrom.number) ) if node.formula.toString() != node.closedFrom.formula.toString(): raise BadTableauException( 'Close pair formulas do not match (%d, %d)' % (node.number, node.closedFrom.number) ) if not node.isClosed() and node.children == []: # open branch should be complete branchSet = frozenset((nd.sign, nd.formula.toString()) for nd in ancestors) for nd in ancestors: t = nd.formula.getType(nd.sign) if t == ALPHA: for sign, f in nd.formula.signedSubf(nd.sign): if not( (sign, f.toString()) in branchSet): raise BadTableauException( ('Branch (ending at %d) is open but not complete' + ' -- %d (ALPHA) is missing subformula %s') % ( node.number, nd.number, signedFormToString((sign, f)))) elif t == BETA: haveOne = False for sign, f in nd.formula.signedSubf(nd.sign): if (sign, f.toString()) in branchSet: haveOne = True break if not haveOne: raise BadTableauException( ('Branch (ending at %d) is open but not complete' + ' -- %d (BETA) is missing a subformula') % ( node.number, nd.number )) ancestors.pop()
def testTableau(self, expect_closed, sfs): self.case += 1 self.tested += 1 strSfs = [signedFormToString(sf) for sf in sfs] print("CASE %d: %s" % (self.case, '; '.join(strSfs))) tableau = Node(False, Variable("")) try: start = now() tableau = TableauBuilder().build(sfs) duration = now() - start except KeyboardInterrupt: raise KeyboardInterrupt() except: printException() if stopOnError: raise FailedTestException() return if not isinstance(tableau, Node): print('FAILED: not a tableau.Node: %s' % type(tableau)) print() return closed = tableau.isClosed() badStructure = False try: self.testTableauStructure(tableau, [], strSfs) except BadTableauException as err: badStructure = str(err) except: printException() if stopOnError: raise FailedTestException() return size = tableau.numberOfNodes() self.time += duration self.size += size if closed: self.closed += 1 passed = closed == expect_closed and not badStructure if passed: self.passed += 1 print('PASSED: time: %12.9f tableau size: %3d %s' % (duration, size, self.closedToString(closed))) else: print('FAILED: ') if not passed or showAll: print('=====TABLEAU=====\n%s\n%s' % (tableau.toString(), '=' * 13)) if not passed: if closed != expect_closed: print('Tableau is %s, but should be %s' % (self.closedToString(closed), self.closedToString(expect_closed))) if badStructure: print(badStructure) if stopOnError: raise FailedTestException() print('')
def testTableauStructure(self, node, ancestors, strSfs): strSf = signedFormToString((node.sign, node.formula)) if node.source == None: if strSf not in strSfs: raise BadTableauException( 'Node (%d) has no source node and its formula not initial.' % (node.number, )) elif node.source not in ancestors: raise BadTableauException( 'Node (%d) has source (%d) which is not one of its ancestors.' % ( node.number, node.source.number, )) else: src = node.source parent = ancestors[-1] strSourceSubfs = [ signedFormToString(sf) for sf in src.formula.signedSubf(src.sign) ] if strSf not in strSourceSubfs: raise BadTableauException( 'Node (%d) does not contain a subformula of (%d).' % (node.number, src.number)) if (src.formula.getType(src.sign) == ALPHA and len(parent.children) != 1): raise BadTableauException( 'Node (%d) is a result of ALPHA rule for (%d) -- must have no siblings.' % (node.number, src.number)) if (src.formula.getType(src.sign) == BETA and len(parent.children) != len(strSourceSubfs)): raise BadTableauException( 'Node (%d) should have %d siblings -- results of BETA rule for (%d).' % (node.number, len(strSourceSubfs) - 1, src.number)) ancestors.append(node) for child in node.children: self.testTableauStructure(child, ancestors, strSfs) if not node.closed and node.children == []: # open branch should be complete branchSet = frozenset( (nd.sign, nd.formula.toString()) for nd in ancestors) for nd in ancestors: t = nd.formula.getType(nd.sign) if t == ALPHA: for sign, f in nd.formula.signedSubf(nd.sign): if not ((sign, f.toString()) in branchSet): raise BadTableauException(( 'Branch (ending at %d) is open but not complete' + ' -- %d (ALPHA) is missing subformula %s') % (node.number, nd.number, signedFormToString( (sign, f)))) elif t == BETA: haveOne = False for sign, f in nd.formula.signedSubf(nd.sign): if (sign, f.toString()) in branchSet: haveOne = True break if not haveOne: raise BadTableauException( ('Branch (ending at %d) is open but not complete' + ' -- %d (BETA) is missing a subformula') % (node.number, nd.number)) ancestors.pop()