示例#1
0
    def __init__(self, *args, **kargs):
        """Creates a new Logical Formula
        LF(fol) -> lf

        The first parameter must be the source to be used to generate the LF.
        If it is a FOL, you can specify the argument 'header' to eliminate the header if it is there
        """
        if len(args) > 0:
            header = kargs.get('header', None)
            fol = copy(args[0])
            if isinstance(fol, str):
                fol = FOL(fol)
            if header and args[0].info[0] == header:
                fol.info = fol.info[-1]
            #    print "*"
            # print ">", header, fol
            try:
                # print '\n-', fol
                logger.debug('FOL: %s', fol)
                fol.convert2PrenexForm()
                logger.debug('PRENEX FOL: %s', fol)
                # print '*', fol
                fol.skolemize()
                logger.debug('SKOLEMIZED FOL: %s', fol)
                fol.push_operand(FOL.OR)
                self.info = fol.info
                logger.debug('LF: %s', self)
            except AttributeError as e:
                raise Exception('Not a valid FOL%s' % fol)
        else:
            self.info = []
示例#2
0
 def test__parse_aux(self):
     in_list = [['and', '(', 'A', ',', 'B', ')'],
                ['or', '(', 'A', ',', 'and', '(', 'B', ',', 'C', ')', ')'],
                ['some', '(', 'A', ',', 'all', '(', 'B', ',', 'and', '(', 'b', ',', 'a', '(', 'c', ')', ')', ')',
                 ')'],
                []]
     out_list = [['and', ['A'], ['B']],
                 ['or', ['A'], ['and', ['B'], ['C']]],
                 ['some', ['A'], ['all', ['B'], ['and', ['b'], ['a', ['c']]]]],
                 None]
     fol = FOL()
     for in_test, out_test in zip(in_list, out_list):
         output = fol._parse_aux(in_test, [])
         assert output == out_test, (output, out_test)
示例#3
0
 def __eq__(self, other):
     if self is None or other is None:
         if self is None and other is None:
             return True
         else:
             return False
     return FOL.equals_predicate(self.info, other.info)
示例#4
0
 def test__parse_aux_exception(self):
     # TODO more exception tests
     in_list = [
         ['some', '(', 'A', ',', ' ', ',', 'and', '(', 'b', ',', 'a', '(', 'c', ')', ')', ')']
     ]
     for in_test in in_list:
         assert_raises(FOL._parse_aux(in_test, []))
示例#5
0
    def test__push_quantifiers(self):
        in_list = [
            ['some', ['A'], ['and', ['b'], ['some', ['B'], ['a', ['c']]]]]
        ]
        out_list = [
            ['some', ['A'], ['some', ['B'], ['and', ['b'], ['a', ['c']]]]]
        ]

        for in_test, out_test in zip(in_list, out_list):
            output = FOL._push_quantifiers(in_test)
            assert output == out_test, (output, out_test)
示例#6
0
    def test_skolemize(self):
        # TODO test with for alls
        in_list = [
            ['some', ['A'], ['and', ['b'], ['some', ['B'], ['a', ['c']]]]],
            ['fol', ['1'], ['some', ['A'], ['and', ['b'], ['some', ['B'], ['a', ['c']]]]]],
            # some(A,and(4,all(B,a(4)))) -> all(B,and(4,a(4)))
            ['some', ['A'], ['and', ['b'], ['all', ['B'], ['a', ['c']]]]],
            ['some', ['A'], ['and', ['b'], ['all', ['B'], ['a', ['B']]]]],
        ]
        out_list = [
            ['and', ['b'], ['a', ['c']]],
            ['and', ['b'], ['a', ['c']]],
            ['all', ['B'], ['and', ['b'], ['a', ['c']]]],
            ['all', ['B'], ['and', ['b'], ['a', ['B']]]],  # TODO this b should be changed
        ]

        for in_test, test in zip(in_list, out_list):
            f = FOL()
            f.info = in_test
            f.skolemize()
            output = f.info
            assert output == test, (output, test)
示例#7
0
 def test_convert2PrenexForm(self):
     in_list = [
         # some(A,and(b,some(B,a(c)))) -> some(A,some(B,and(b,a(c))))
         ['some', ['A'], ['and', ['b'], ['some', ['B'], ['a', ['c']]]]],
         # fol(1, some(A,and(b,some(B,a(c))))) -> some(A,some(B,and(b,a(c))))
         ['fol', ['1'], ['some', ['A'], ['and', ['b'], ['some', ['B'], ['a', ['c']]]]]],
         ['some', ['A'], ['and', ['b'], ['all', ['B'], ['a', ['c']]]]],
         # some(A, and (b, all(B, a(b)))) -> some(A, all(B, and (4, a(4))))
         ['some', ['A'], ['and', ['b'], ['all', ['B'], ['some', ['C'], ['a', ['C']]]]]],
     ]
     out_list = [
         ['some', ['A'], ['some', ['B'], ['and', ['b'], ['a', ['c']]]]],
         ['fol', ['1'], ['some', ['A'], ['some', ['B'], ['and', ['b'], ['a', ['c']]]]]],
         ['some', ['A'], ['all', ['B'], ['and', ['b'], ['a', ['c']]]]],
         ['some', ['A'], ['all', ['B'], ['some', ['C'], ['and', ['b'], ['a', ['C']]]]]],
     ]
     for in_test, test in zip(in_list, out_list):
         f = FOL()
         f.info = in_test
         f.convert2PrenexForm()
         output = f.info
         assert output == test, (output, test)
示例#8
0
    def test__split(self):
        out_list = [['and', '(', 'A', ',', 'B', ')'],
                    ['or', '(', 'A', ',', 'and', '(', 'B', ',', 'C', ')', ')'],
                    ['or', '(', 'A', ',', 'and', '(', '\'Bubles are magical\'', ',', 'C', ')', ')'],
                    ['or', '(', 'A', ',', 'and', '(', r"'Bubles are \'awe\\nsome\''", ',', 'C', ')', ')'],
                    ['some', '(', 'A', ',', 'and', '(', 'b', ',', 'a', '(', 'c', ')', ')', ')']]
        in_list = list(map(lambda x: ''.join(x) + '.', out_list))

        in_list.append('   some  (  A  , all  (  B     , and(     b, a(  c) \n)\t)    ) ')
        out_list.append(
            ['some', '(', 'A', ',', 'all', '(', 'B', ',', 'and', '(', 'b', ',', 'a', '(', 'c', ')', ')', ')', ')'])
        for in_test, out_test in zip(in_list, out_list):
            output = FOL._split(in_test)
            assert output == out_test, (output, out_test)
示例#9
0
 def split(self):
     """Split the clause into many smaller clauses"""
     frontier = [self.info]
     out = []
     while len(frontier) > 0:
         root = frontier.pop()
         while FOL.is_quantifier(root[0]):
             assert len(root) > 1, 'Invalid FOL'
             root = root[-1]
         # print '##' if root[0] == FOL.AND else ('xx'+str(root[0]))
         if root[0] == FOL.AND:
             frontier.extend(root[1:])
         else:
             lf = LF()
             lf.info = copy(root)
             out.append(lf)
     return out
示例#10
0
    def test__push_negation(self):
        # TODO add more tests
        in_list = [
            # not(some(A,and(b,some(B, a(c))))) -> all(A, or(not(b),all(B, not(a(c)))))
            ['not', ['some', ['A'], ['and', ['b'], ['some', ['B'], ['a', ['c']]]]]],
            # some(A,and(b,not(some(B,a(4))))) -> some(A,and(b,all(B,not(a(4))))))
            ['some', ['A'], ['and', ['b'], ['not', ['some', ['B'], ['a', ['c']]]]]],
            ['some', ['A'], ['and', ['b'], ['some', ['B'], ['a', ['c']]]]],
        ]
        out_list = [
            ['all', ['A'], ['or', ['not', ['b']], ['all', ['B'], ['not', ['a', ['c']]]]]],
            ['some', ['A'], ['and', ['b'], ['all', ['B'], ['not', ['a', ['c']]]]]],
            ['some', ['A'], ['and', ['b'], ['some', ['B'], ['a', ['c']]]]],
        ]

        for in_test, out_test in zip(in_list, out_list):
            output = FOL._push_negation(in_test)
            assert output == out_test, (output, out_test)
示例#11
0
def test_someA_with_a_and():
    """some(A, and(b, a(c)))"""
    f1 = FOL('some(A, and(b, a(c)))').info
    l1 = ['some', ['A'], ['and', ['b'], ['a', ['c']]]]
    assert FOL.equals_predicate(f1, l1)
示例#12
0
    def test_is_special(self):
        in_list = ['and', 'AND', 'or', 'OR', 'not', 'Not', 'NOT', 'Dumbledore']
        out_list = [True, False, True, False, True, False, False, False]

        for in_test, out_test in zip(in_list, out_list):
            assert FOL.is_special(in_test) == out_test
示例#13
0
    def test_is_quantifier(self):
        in_list = ['and', 'AND', 'or', 'OR', 'Dumbledore']
        out_list = [True, False, True, False, False]

        for in_test, out_test in zip(in_list, out_list):
            assert FOL.is_operator(in_test) == out_test
示例#14
0
 def test_expand_predicates(self):
     boxer = BoxerWebAPI()
     fol = FOL('')
     fol.info = [
         'some', ['A'],
         [
             'some', ['B'],
             [
                 'some', ['C'],
                 [
                     'and',
                     [
                         'and', ['n1time', ['B']],
                         [
                             'and', ['a1around', ['A']],
                             [
                                 'and', ['r1Theme', ['A'], ['B']],
                                 ['pernamjohn', ['C']]
                             ]
                         ]
                     ],
                     [
                         'some', ['D'],
                         [
                             'some', ['E'],
                             [
                                 'and', ['r1Time', ['E'], ['B']],
                                 [
                                     'and', ['r1even', ['E']],
                                     [
                                         'and', ['a1faster', ['D']],
                                         [
                                             'and',
                                             ['r1Manner', ['E'], ['D']],
                                             [
                                                 'and',
                                                 ['r1Actor', ['E'], ['C']],
                                                 ['v1run', ['E']]
                                             ]
                                         ]
                                     ]
                                 ]
                             ]
                         ]
                     ]
                 ]
             ]
         ]
     ]
     expanded_fol = FOL('')
     expanded_fol.info = [
         'some', ['A'],
         [
             'some', ['B'],
             [
                 'some', ['C'],
                 [
                     'and',
                     [
                         'and', ['noun', ['B'], ['time']],
                         [
                             'and', ['adjective', ['A'], ['around']],
                             [
                                 'and', ['theme', ['A'], ['B']],
                                 [
                                     'and', ['person', ['C']],
                                     ['noun', ['C'], ['john']]
                                 ]
                             ]
                         ]
                     ],
                     [
                         'some', ['D'],
                         [
                             'some', ['E'],
                             [
                                 'and',
                                 ['relation', ['E'], ['B'], ['time']],
                                 [
                                     'and', ['relation', ['E'], ['even']],
                                     [
                                         'and',
                                         ['adjective', ['D'], ['faster']],
                                         [
                                             'and',
                                             [
                                                 'relation', ['E'], ['D'],
                                                 ['manner']
                                             ],
                                             [
                                                 'and',
                                                 ['actor', ['E'], ['C']],
                                                 ['verb', ['E'], ['run']]
                                             ]
                                         ]
                                     ]
                                 ]
                             ]
                         ]
                     ]
                 ]
             ]
         ]
     ]
     boxer._expandFOLpredicates(fol)
     assert fol == expanded_fol