|
| 1 | +#!/usr/bin/python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Copyright 2017 Pascual Martinez-Gomez |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +import unittest |
| 19 | + |
| 20 | +from abduction_tools import get_tree_pred_args |
| 21 | +from abduction_tools import GetPremisesThatMatchConclusionArgs |
| 22 | +from tree_tools import tree_or_string |
| 23 | + |
| 24 | +class GetTreePredArgsTestCase(unittest.TestCase): |
| 25 | + def test_premise_one_var(self): |
| 26 | + coq_line = 'H2 : _kiss x1' |
| 27 | + expected_args = tree_or_string('x1') |
| 28 | + args = get_tree_pred_args(coq_line) |
| 29 | + self.assertEqual(expected_args, args) |
| 30 | + |
| 31 | + def test_premise_zero_var(self): |
| 32 | + coq_line = 'H2 : True' |
| 33 | + expected_args = None |
| 34 | + args = get_tree_pred_args(coq_line) |
| 35 | + self.assertEqual(expected_args, args) |
| 36 | + |
| 37 | + def test_premise_case_var(self): |
| 38 | + coq_line = 'H2 : _woman (Acc x1)' |
| 39 | + expected_args = tree_or_string('(Acc x1)') |
| 40 | + args = get_tree_pred_args(coq_line) |
| 41 | + self.assertEqual(expected_args, args) |
| 42 | + |
| 43 | +class GetPremisesThatMatchConclusionArgsTestCase(unittest.TestCase): |
| 44 | + def test_one_casevar_one_prem_match(self): |
| 45 | + premise_lines = [ |
| 46 | + "H2 : _kiss x1", |
| 47 | + "H : _man (Subj x1)", |
| 48 | + "H0 : _woman (Acc x1)"] |
| 49 | + conclusion_line = "_person (Acc x1)" |
| 50 | + expected_premises = [ |
| 51 | + "H0 : _woman (Acc x1)"] |
| 52 | + matching_premises = GetPremisesThatMatchConclusionArgs( |
| 53 | + premise_lines, conclusion_line) |
| 54 | + self.assertEqual(expected_premises, matching_premises) |
| 55 | + |
| 56 | + def test_one_casevar_zero_prem_match(self): |
| 57 | + premise_lines = [ |
| 58 | + "H2 : _kiss x1", |
| 59 | + "H : _man (Subj x1)", |
| 60 | + "H0 : _woman (Acc x1)"] |
| 61 | + conclusion_line = "_person (Acc x2)" |
| 62 | + expected_premises = [] |
| 63 | + matching_premises = GetPremisesThatMatchConclusionArgs( |
| 64 | + premise_lines, conclusion_line) |
| 65 | + self.assertEqual(expected_premises, matching_premises) |
| 66 | + |
| 67 | + def test_one_casevar_zero_prem_match2(self): |
| 68 | + premise_lines = [ |
| 69 | + "H2 : _kiss x1", |
| 70 | + "H : _man (Subj x1)", |
| 71 | + "H0 : _woman (Acc x1)"] |
| 72 | + conclusion_line = "_person (Ind x1)" |
| 73 | + expected_premises = [] |
| 74 | + matching_premises = GetPremisesThatMatchConclusionArgs( |
| 75 | + premise_lines, conclusion_line) |
| 76 | + self.assertEqual(expected_premises, matching_premises) |
| 77 | + |
| 78 | + def test_one_var_one_prems_match(self): |
| 79 | + premise_lines = [ |
| 80 | + "H2 : _kiss x1", |
| 81 | + "H : _man (Subj x1)", |
| 82 | + "H0 : _woman (Acc x1)"] |
| 83 | + conclusion_line = "_greets x1" |
| 84 | + expected_premises = [ |
| 85 | + "H2 : _kiss x1"] |
| 86 | + matching_premises = GetPremisesThatMatchConclusionArgs( |
| 87 | + premise_lines, conclusion_line) |
| 88 | + self.assertEqual(expected_premises, matching_premises) |
| 89 | + |
| 90 | + def test_one_var_zero_prems_match(self): |
| 91 | + premise_lines = [ |
| 92 | + "H2 : _kiss x1", |
| 93 | + "H : _man (Subj x1)", |
| 94 | + "H0 : _woman (Acc x1)"] |
| 95 | + conclusion_line = "_greets x2" |
| 96 | + expected_premises = [] |
| 97 | + matching_premises = GetPremisesThatMatchConclusionArgs( |
| 98 | + premise_lines, conclusion_line) |
| 99 | + self.assertEqual(expected_premises, matching_premises) |
| 100 | + |
| 101 | + def test_one_anonvar_one_prems_match(self): |
| 102 | + # Use anonymous variables (e.g. ?284 in the conclusion) to match |
| 103 | + # any variable in the premises. |
| 104 | + premise_lines = [ |
| 105 | + "H2 : _kiss x1", |
| 106 | + "H : _man (Subj x1)", |
| 107 | + "H0 : _woman (Acc x1)"] |
| 108 | + conclusion_line = "_greets ?284" |
| 109 | + expected_premises = ["H2 : _kiss x1"] |
| 110 | + matching_premises = GetPremisesThatMatchConclusionArgs( |
| 111 | + premise_lines, conclusion_line) |
| 112 | + self.assertEqual(expected_premises, matching_premises) |
| 113 | + |
| 114 | + def test_one_anonvar_two_prems_match(self): |
| 115 | + # Use anonymous variables (e.g. ?284 in the conclusion) to match |
| 116 | + # any case-constrained variable in the premises. |
| 117 | + premise_lines = [ |
| 118 | + "H2 : _kiss x1", |
| 119 | + "H : _bird (Subj x2)", |
| 120 | + "H : _man (Subj x1)", |
| 121 | + "H0 : _woman (Acc x1)"] |
| 122 | + conclusion_line = "_greets (Subj ?284)" |
| 123 | + expected_premises = [ |
| 124 | + "H : _bird (Subj x2)", |
| 125 | + "H : _man (Subj x1)"] |
| 126 | + matching_premises = GetPremisesThatMatchConclusionArgs( |
| 127 | + premise_lines, conclusion_line) |
| 128 | + self.assertEqual(expected_premises, matching_premises) |
| 129 | + |
| 130 | +if __name__ == '__main__': |
| 131 | + suite1 = unittest.TestLoader().loadTestsFromTestCase(GetTreePredArgsTestCase) |
| 132 | + suite2 = unittest.TestLoader().loadTestsFromTestCase( |
| 133 | + GetPremisesThatMatchConclusionArgsTestCase) |
| 134 | + suites = unittest.TestSuite([suite1, suite2]) |
| 135 | + unittest.TextTestRunner(verbosity=2).run(suites) |
0 commit comments