001 package aima.test.logictest.foltest; 002 003 import java.util.ArrayList; 004 import java.util.List; 005 import java.util.Map; 006 007 import junit.framework.TestCase; 008 import aima.logic.fol.inference.InferenceProcedure; 009 import aima.logic.fol.inference.InferenceResult; 010 import aima.logic.fol.inference.proof.Proof; 011 import aima.logic.fol.kb.FOLKnowledgeBase; 012 import aima.logic.fol.kb.FOLKnowledgeBaseFactory; 013 import aima.logic.fol.parsing.ast.Constant; 014 import aima.logic.fol.parsing.ast.Function; 015 import aima.logic.fol.parsing.ast.NotSentence; 016 import aima.logic.fol.parsing.ast.Predicate; 017 import aima.logic.fol.parsing.ast.Term; 018 import aima.logic.fol.parsing.ast.TermEquality; 019 import aima.logic.fol.parsing.ast.Variable; 020 021 /** 022 * @author Ciaran O'Reilly 023 * 024 */ 025 public abstract class CommonFOLInferenceProcedureTests extends TestCase { 026 027 // 028 // Protected Methods 029 // 030 protected void testDefiniteClauseKBKingsQueryCriminalXFalse( 031 InferenceProcedure infp) { 032 FOLKnowledgeBase kkb = FOLKnowledgeBaseFactory 033 .createKingsKnowledgeBase(infp); 034 List<Term> terms = new ArrayList<Term>(); 035 terms.add(new Variable("x")); 036 Predicate query = new Predicate("Criminal", terms); 037 InferenceResult answer = kkb.ask(query); 038 assertTrue(null != answer); 039 assertTrue(answer.isPossiblyFalse()); 040 assertFalse(answer.isTrue()); 041 assertFalse(answer.isUnknownDueToTimeout()); 042 assertFalse(answer.isPartialResultDueToTimeout()); 043 assertTrue(0 == answer.getProofs().size()); 044 } 045 046 protected void testDefiniteClauseKBKingsQueryRichardEvilFalse( 047 InferenceProcedure infp) { 048 FOLKnowledgeBase kkb = FOLKnowledgeBaseFactory 049 .createKingsKnowledgeBase(infp); 050 List<Term> terms = new ArrayList<Term>(); 051 terms.add(new Constant("Richard")); 052 Predicate query = new Predicate("Evil", terms); 053 InferenceResult answer = kkb.ask(query); 054 assertTrue(null != answer); 055 assertTrue(answer.isPossiblyFalse()); 056 assertFalse(answer.isTrue()); 057 assertFalse(answer.isUnknownDueToTimeout()); 058 assertFalse(answer.isPartialResultDueToTimeout()); 059 assertTrue(0 == answer.getProofs().size()); 060 } 061 062 protected void testDefiniteClauseKBKingsQueryJohnEvilSucceeds( 063 InferenceProcedure infp) { 064 FOLKnowledgeBase kkb = FOLKnowledgeBaseFactory 065 .createKingsKnowledgeBase(infp); 066 List<Term> terms = new ArrayList<Term>(); 067 terms.add(new Constant("John")); 068 Predicate query = new Predicate("Evil", terms); 069 InferenceResult answer = kkb.ask(query); 070 071 assertTrue(null != answer); 072 assertFalse(answer.isPossiblyFalse()); 073 assertTrue(answer.isTrue()); 074 assertFalse(answer.isUnknownDueToTimeout()); 075 assertFalse(answer.isPartialResultDueToTimeout()); 076 assertTrue(1 == answer.getProofs().size()); 077 assertTrue(0 == answer.getProofs().get(0).getAnswerBindings().size()); 078 } 079 080 protected void testDefiniteClauseKBKingsQueryEvilXReturnsJohnSucceeds( 081 InferenceProcedure infp) { 082 FOLKnowledgeBase kkb = FOLKnowledgeBaseFactory 083 .createKingsKnowledgeBase(infp); 084 List<Term> terms = new ArrayList<Term>(); 085 terms.add(new Variable("x")); 086 Predicate query = new Predicate("Evil", terms); 087 InferenceResult answer = kkb.ask(query); 088 089 assertTrue(null != answer); 090 assertFalse(answer.isPossiblyFalse()); 091 assertTrue(answer.isTrue()); 092 assertFalse(answer.isUnknownDueToTimeout()); 093 assertFalse(answer.isPartialResultDueToTimeout()); 094 assertTrue(1 == answer.getProofs().size()); 095 assertTrue(1 == answer.getProofs().get(0).getAnswerBindings().size()); 096 assertEquals(new Constant("John"), answer.getProofs().get(0) 097 .getAnswerBindings().get(new Variable("x"))); 098 } 099 100 protected void testDefiniteClauseKBKingsQueryKingXReturnsJohnAndRichardSucceeds( 101 InferenceProcedure infp) { 102 FOLKnowledgeBase kkb = FOLKnowledgeBaseFactory 103 .createKingsKnowledgeBase(infp); 104 List<Term> terms = new ArrayList<Term>(); 105 terms.add(new Variable("x")); 106 Predicate query = new Predicate("King", terms); 107 InferenceResult answer = kkb.ask(query); 108 109 assertTrue(null != answer); 110 assertFalse(answer.isPossiblyFalse()); 111 assertTrue(answer.isTrue()); 112 assertFalse(answer.isUnknownDueToTimeout()); 113 assertFalse(answer.isPartialResultDueToTimeout()); 114 assertTrue(2 == answer.getProofs().size()); 115 assertTrue(1 == answer.getProofs().get(0).getAnswerBindings().size()); 116 assertTrue(1 == answer.getProofs().get(1).getAnswerBindings().size()); 117 118 boolean gotJohn, gotRichard; 119 gotJohn = gotRichard = false; 120 Constant cJohn = new Constant("John"); 121 Constant cRichard = new Constant("Richard"); 122 for (Proof p : answer.getProofs()) { 123 Map<Variable, Term> ans = p.getAnswerBindings(); 124 assertEquals(1, ans.size()); 125 if (cJohn.equals(ans.get(new Variable("x")))) { 126 gotJohn = true; 127 } 128 if (cRichard.equals(ans.get(new Variable("x")))) { 129 gotRichard = true; 130 } 131 } 132 assertTrue(gotJohn); 133 assertTrue(gotRichard); 134 } 135 136 protected void testDefiniteClauseKBWeaponsQueryCriminalXReturnsWestSucceeds( 137 InferenceProcedure infp) { 138 FOLKnowledgeBase wkb = FOLKnowledgeBaseFactory 139 .createWeaponsKnowledgeBase(infp); 140 List<Term> terms = new ArrayList<Term>(); 141 terms.add(new Variable("x")); 142 Predicate query = new Predicate("Criminal", terms); 143 144 InferenceResult answer = wkb.ask(query); 145 146 assertTrue(null != answer); 147 assertFalse(answer.isPossiblyFalse()); 148 assertTrue(answer.isTrue()); 149 assertFalse(answer.isUnknownDueToTimeout()); 150 assertFalse(answer.isPartialResultDueToTimeout()); 151 assertTrue(1 == answer.getProofs().size()); 152 assertTrue(1 == answer.getProofs().get(0).getAnswerBindings().size()); 153 assertEquals(new Constant("West"), answer.getProofs().get(0) 154 .getAnswerBindings().get(new Variable("x"))); 155 } 156 157 protected void testHornClauseKBRingOfThievesQuerySkisXReturnsNancyRedBertDrew( 158 InferenceProcedure infp) { 159 FOLKnowledgeBase rotkb = FOLKnowledgeBaseFactory 160 .createRingOfThievesKnowledgeBase(infp); 161 List<Term> terms = new ArrayList<Term>(); 162 terms.add(new Variable("x")); 163 Predicate query = new Predicate("Skis", terms); 164 165 InferenceResult answer = rotkb.ask(query); 166 167 assertTrue(null != answer); 168 assertFalse(answer.isPossiblyFalse()); 169 assertTrue(answer.isTrue()); 170 assertFalse(answer.isUnknownDueToTimeout()); 171 // DB can expand infinitely so is only partial. 172 assertTrue(answer.isPartialResultDueToTimeout()); 173 assertEquals(4, answer.getProofs().size()); 174 assertEquals(1, answer.getProofs().get(0).getAnswerBindings().size()); 175 assertEquals(1, answer.getProofs().get(1).getAnswerBindings().size()); 176 assertEquals(1, answer.getProofs().get(2).getAnswerBindings().size()); 177 assertEquals(1, answer.getProofs().get(3).getAnswerBindings().size()); 178 179 List<Constant> expected = new ArrayList<Constant>(); 180 expected.add(new Constant("Nancy")); 181 expected.add(new Constant("Red")); 182 expected.add(new Constant("Bert")); 183 expected.add(new Constant("Drew")); 184 for (Proof p : answer.getProofs()) { 185 expected.remove(p.getAnswerBindings().get(new Variable("x"))); 186 } 187 assertEquals(0, expected.size()); 188 } 189 190 protected void testFullFOLKBLovesAnimalQueryKillsCuriosityTunaSucceeds( 191 InferenceProcedure infp, boolean expectedToTimeOut) { 192 FOLKnowledgeBase akb = FOLKnowledgeBaseFactory 193 .createLovesAnimalKnowledgeBase(infp); 194 List<Term> terms = new ArrayList<Term>(); 195 terms.add(new Constant("Curiosity")); 196 terms.add(new Constant("Tuna")); 197 Predicate query = new Predicate("Kills", terms); 198 199 InferenceResult answer = akb.ask(query); 200 assertTrue(null != answer); 201 if (expectedToTimeOut) { 202 assertFalse(answer.isPossiblyFalse()); 203 assertFalse(answer.isTrue()); 204 assertTrue(answer.isUnknownDueToTimeout()); 205 assertFalse(answer.isPartialResultDueToTimeout()); 206 assertTrue(0 == answer.getProofs().size()); 207 } else { 208 assertFalse(answer.isPossiblyFalse()); 209 assertTrue(answer.isTrue()); 210 assertFalse(answer.isUnknownDueToTimeout()); 211 assertFalse(answer.isPartialResultDueToTimeout()); 212 assertTrue(1 == answer.getProofs().size()); 213 assertTrue(0 == answer.getProofs().get(0).getAnswerBindings() 214 .size()); 215 } 216 } 217 218 protected void testFullFOLKBLovesAnimalQueryNotKillsJackTunaSucceeds( 219 InferenceProcedure infp, boolean expectedToTimeOut) { 220 FOLKnowledgeBase akb = FOLKnowledgeBaseFactory 221 .createLovesAnimalKnowledgeBase(infp); 222 List<Term> terms = new ArrayList<Term>(); 223 terms.add(new Constant("Jack")); 224 terms.add(new Constant("Tuna")); 225 NotSentence query = new NotSentence(new Predicate("Kills", terms)); 226 227 InferenceResult answer = akb.ask(query); 228 229 assertTrue(null != answer); 230 if (expectedToTimeOut) { 231 assertFalse(answer.isPossiblyFalse()); 232 assertFalse(answer.isTrue()); 233 assertTrue(answer.isUnknownDueToTimeout()); 234 assertFalse(answer.isPartialResultDueToTimeout()); 235 assertTrue(0 == answer.getProofs().size()); 236 } else { 237 assertFalse(answer.isPossiblyFalse()); 238 assertTrue(answer.isTrue()); 239 assertFalse(answer.isUnknownDueToTimeout()); 240 assertFalse(answer.isPartialResultDueToTimeout()); 241 assertTrue(1 == answer.getProofs().size()); 242 assertTrue(0 == answer.getProofs().get(0).getAnswerBindings() 243 .size()); 244 } 245 } 246 247 protected void testFullFOLKBLovesAnimalQueryKillsJackTunaFalse( 248 InferenceProcedure infp, boolean expectedToTimeOut) { 249 FOLKnowledgeBase akb = FOLKnowledgeBaseFactory 250 .createLovesAnimalKnowledgeBase(infp); 251 List<Term> terms = new ArrayList<Term>(); 252 terms.add(new Constant("Jack")); 253 terms.add(new Constant("Tuna")); 254 Predicate query = new Predicate("Kills", terms); 255 256 InferenceResult answer = akb.ask(query); 257 258 assertTrue(null != answer); 259 if (expectedToTimeOut) { 260 assertFalse(answer.isPossiblyFalse()); 261 assertFalse(answer.isTrue()); 262 assertTrue(answer.isUnknownDueToTimeout()); 263 assertFalse(answer.isPartialResultDueToTimeout()); 264 assertTrue(0 == answer.getProofs().size()); 265 } else { 266 assertTrue(answer.isPossiblyFalse()); 267 assertFalse(answer.isTrue()); 268 assertFalse(answer.isUnknownDueToTimeout()); 269 assertFalse(answer.isPartialResultDueToTimeout()); 270 assertTrue(0 == answer.getProofs().size()); 271 } 272 } 273 274 protected void testEqualityAxiomsKBabcAEqualsCSucceeds( 275 InferenceProcedure infp) { 276 FOLKnowledgeBase akb = FOLKnowledgeBaseFactory 277 .createABCEqualityKnowledgeBase(infp, true); 278 279 TermEquality query = new TermEquality(new Constant("A"), new Constant( 280 "C")); 281 282 InferenceResult answer = akb.ask(query); 283 284 assertTrue(null != answer); 285 assertFalse(answer.isPossiblyFalse()); 286 assertTrue(answer.isTrue()); 287 assertFalse(answer.isUnknownDueToTimeout()); 288 assertFalse(answer.isPartialResultDueToTimeout()); 289 assertTrue(1 == answer.getProofs().size()); 290 assertTrue(0 == answer.getProofs().get(0).getAnswerBindings().size()); 291 } 292 293 protected void testEqualityAndSubstitutionAxiomsKBabcdFFASucceeds( 294 InferenceProcedure infp) { 295 FOLKnowledgeBase akb = FOLKnowledgeBaseFactory 296 .createABCDEqualityAndSubstitutionKnowledgeBase(infp, true); 297 298 List<Term> terms = new ArrayList<Term>(); 299 terms.add(new Constant("A")); 300 Function fa = new Function("F", terms); 301 terms = new ArrayList<Term>(); 302 terms.add(fa); 303 TermEquality query = new TermEquality(new Function("F", terms), 304 new Constant("A")); 305 306 InferenceResult answer = akb.ask(query); 307 308 assertTrue(null != answer); 309 assertFalse(answer.isPossiblyFalse()); 310 assertTrue(answer.isTrue()); 311 assertFalse(answer.isUnknownDueToTimeout()); 312 assertFalse(answer.isPartialResultDueToTimeout()); 313 assertTrue(1 == answer.getProofs().size()); 314 assertTrue(0 == answer.getProofs().get(0).getAnswerBindings().size()); 315 } 316 317 protected void xtestEqualityAndSubstitutionAxiomsKBabcdPDSucceeds( 318 InferenceProcedure infp) { 319 FOLKnowledgeBase akb = FOLKnowledgeBaseFactory 320 .createABCDEqualityAndSubstitutionKnowledgeBase(infp, true); 321 322 List<Term> terms = new ArrayList<Term>(); 323 terms.add(new Constant("D")); 324 Predicate query = new Predicate("P", terms); 325 326 InferenceResult answer = akb.ask(query); 327 328 assertTrue(null != answer); 329 assertFalse(answer.isPossiblyFalse()); 330 assertTrue(answer.isTrue()); 331 assertFalse(answer.isUnknownDueToTimeout()); 332 assertFalse(answer.isPartialResultDueToTimeout()); 333 assertTrue(1 == answer.getProofs().size()); 334 assertTrue(0 == answer.getProofs().get(0).getAnswerBindings().size()); 335 } 336 337 protected void testEqualityAndSubstitutionAxiomsKBabcdPFFASucceeds( 338 InferenceProcedure infp, boolean expectedToTimeOut) { 339 FOLKnowledgeBase akb = FOLKnowledgeBaseFactory 340 .createABCDEqualityAndSubstitutionKnowledgeBase(infp, true); 341 342 List<Term> terms = new ArrayList<Term>(); 343 terms.add(new Constant("A")); 344 Function fa = new Function("F", terms); 345 terms = new ArrayList<Term>(); 346 terms.add(fa); 347 Function ffa = new Function("F", terms); 348 terms = new ArrayList<Term>(); 349 terms.add(ffa); 350 Predicate query = new Predicate("P", terms); 351 352 InferenceResult answer = akb.ask(query); 353 354 if (expectedToTimeOut) { 355 assertFalse(answer.isPossiblyFalse()); 356 assertFalse(answer.isTrue()); 357 assertTrue(answer.isUnknownDueToTimeout()); 358 assertFalse(answer.isPartialResultDueToTimeout()); 359 assertTrue(0 == answer.getProofs().size()); 360 } else { 361 assertTrue(null != answer); 362 assertFalse(answer.isPossiblyFalse()); 363 assertTrue(answer.isTrue()); 364 assertFalse(answer.isUnknownDueToTimeout()); 365 assertFalse(answer.isPartialResultDueToTimeout()); 366 assertTrue(1 == answer.getProofs().size()); 367 assertTrue(0 == answer.getProofs().get(0).getAnswerBindings() 368 .size()); 369 } 370 } 371 372 protected void testEqualityNoAxiomsKBabcAEqualsCSucceeds( 373 InferenceProcedure infp, boolean expectedToFail) { 374 FOLKnowledgeBase akb = FOLKnowledgeBaseFactory 375 .createABCEqualityKnowledgeBase(infp, false); 376 377 TermEquality query = new TermEquality(new Constant("A"), new Constant( 378 "C")); 379 380 InferenceResult answer = akb.ask(query); 381 382 assertTrue(null != answer); 383 if (expectedToFail) { 384 assertTrue(answer.isPossiblyFalse()); 385 assertFalse(answer.isTrue()); 386 assertFalse(answer.isUnknownDueToTimeout()); 387 assertFalse(answer.isPartialResultDueToTimeout()); 388 assertTrue(0 == answer.getProofs().size()); 389 } else { 390 assertFalse(answer.isPossiblyFalse()); 391 assertTrue(answer.isTrue()); 392 assertFalse(answer.isUnknownDueToTimeout()); 393 assertFalse(answer.isPartialResultDueToTimeout()); 394 assertTrue(1 == answer.getProofs().size()); 395 assertTrue(0 == answer.getProofs().get(0).getAnswerBindings() 396 .size()); 397 } 398 } 399 400 protected void testEqualityAndSubstitutionNoAxiomsKBabcdFFASucceeds( 401 InferenceProcedure infp, boolean expectedToFail) { 402 FOLKnowledgeBase akb = FOLKnowledgeBaseFactory 403 .createABCDEqualityAndSubstitutionKnowledgeBase(infp, false); 404 405 List<Term> terms = new ArrayList<Term>(); 406 terms.add(new Constant("A")); 407 Function fa = new Function("F", terms); 408 terms = new ArrayList<Term>(); 409 terms.add(fa); 410 TermEquality query = new TermEquality(new Function("F", terms), 411 new Constant("A")); 412 413 InferenceResult answer = akb.ask(query); 414 415 assertTrue(null != answer); 416 if (expectedToFail) { 417 assertTrue(answer.isPossiblyFalse()); 418 assertFalse(answer.isTrue()); 419 assertFalse(answer.isUnknownDueToTimeout()); 420 assertFalse(answer.isPartialResultDueToTimeout()); 421 assertTrue(0 == answer.getProofs().size()); 422 } else { 423 assertFalse(answer.isPossiblyFalse()); 424 assertTrue(answer.isTrue()); 425 assertFalse(answer.isUnknownDueToTimeout()); 426 assertFalse(answer.isPartialResultDueToTimeout()); 427 assertTrue(1 == answer.getProofs().size()); 428 assertTrue(0 == answer.getProofs().get(0).getAnswerBindings() 429 .size()); 430 } 431 } 432 433 protected void testEqualityAndSubstitutionNoAxiomsKBabcdPDSucceeds( 434 InferenceProcedure infp, boolean expectedToFail) { 435 FOLKnowledgeBase akb = FOLKnowledgeBaseFactory 436 .createABCDEqualityAndSubstitutionKnowledgeBase(infp, false); 437 438 List<Term> terms = new ArrayList<Term>(); 439 terms.add(new Constant("D")); 440 Predicate query = new Predicate("P", terms); 441 442 InferenceResult answer = akb.ask(query); 443 444 assertTrue(null != answer); 445 if (expectedToFail) { 446 assertTrue(answer.isPossiblyFalse()); 447 assertFalse(answer.isTrue()); 448 assertFalse(answer.isUnknownDueToTimeout()); 449 assertFalse(answer.isPartialResultDueToTimeout()); 450 assertTrue(0 == answer.getProofs().size()); 451 } else { 452 assertFalse(answer.isPossiblyFalse()); 453 assertTrue(answer.isTrue()); 454 assertFalse(answer.isUnknownDueToTimeout()); 455 assertFalse(answer.isPartialResultDueToTimeout()); 456 assertTrue(1 == answer.getProofs().size()); 457 assertTrue(0 == answer.getProofs().get(0).getAnswerBindings() 458 .size()); 459 } 460 } 461 462 protected void testEqualityAndSubstitutionNoAxiomsKBabcdPFFASucceeds( 463 InferenceProcedure infp, boolean expectedToFail) { 464 FOLKnowledgeBase akb = FOLKnowledgeBaseFactory 465 .createABCDEqualityAndSubstitutionKnowledgeBase(infp, false); 466 467 List<Term> terms = new ArrayList<Term>(); 468 terms.add(new Constant("A")); 469 Function fa = new Function("F", terms); 470 terms = new ArrayList<Term>(); 471 terms.add(fa); 472 Function ffa = new Function("F", terms); 473 terms = new ArrayList<Term>(); 474 terms.add(ffa); 475 Predicate query = new Predicate("P", terms); 476 477 InferenceResult answer = akb.ask(query); 478 479 assertTrue(null != answer); 480 if (expectedToFail) { 481 assertTrue(answer.isPossiblyFalse()); 482 assertFalse(answer.isTrue()); 483 assertFalse(answer.isUnknownDueToTimeout()); 484 assertFalse(answer.isPartialResultDueToTimeout()); 485 assertTrue(0 == answer.getProofs().size()); 486 } else { 487 assertFalse(answer.isPossiblyFalse()); 488 assertTrue(answer.isTrue()); 489 assertFalse(answer.isUnknownDueToTimeout()); 490 assertFalse(answer.isPartialResultDueToTimeout()); 491 assertTrue(1 == answer.getProofs().size()); 492 assertTrue(0 == answer.getProofs().get(0).getAnswerBindings() 493 .size()); 494 } 495 } 496 }