/** * ***************************************************************** * * @return true if a quantifiers in a quantifier list is not found in the body of the statement. */ private static boolean quantifierNotInStatement(Formula f) { if (f.theFormula == null || f.theFormula.length() < 1 || !f.listP() || f.empty()) return false; if (!Arrays.asList("forall", "exists").contains(f.car())) { Formula f1 = new Formula(); f1.read(f.car()); Formula f2 = new Formula(); f2.read(f.cdr()); return (quantifierNotInStatement(f1) || quantifierNotInStatement(f2)); } Formula form = new Formula(); form.read(f.theFormula); if (form.car() != null && form.car().length() > 0) { // This test shouldn't be needed. String rest = form.cdr(); // Quantifier list plus rest of statement Formula quant = new Formula(); quant.read(rest); String q = quant.car(); // Now just the quantifier list. String body = quant.cdr(); quant.read(q); ArrayList<String> qList = quant.argumentsToArrayList(0); // Put all the quantified variables into a list. if (rest.indexOf("exists") != -1 || rest.indexOf("forall") != -1) { // nested quantifiers Formula restForm = new Formula(); restForm.read(rest); restForm.read(restForm.cdr()); if (quantifierNotInStatement(restForm)) return true; } for (int i = 0; i < qList.size(); i++) { String var = (String) qList.get(i); if (body.indexOf(var) == -1) return true; } } return false; }
/** * ***************************************************************** Return a list of terms (for a * given argument position) that do not have a specified relation. * * @param kb the knowledge base * @param rel the relation name * @param argnum the argument position of the term * @param limit the maximum number of results to return, or -1 if all * @param letter the first letter of the term name */ public static ArrayList<String> termsWithoutRelation( KB kb, String rel, int argnum, int limit, char letter) { ArrayList<String> result = new ArrayList<String>(); Iterator<String> it = kb.getTerms().iterator(); while (it.hasNext()) { String term = it.next(); if (LOG_OPS.contains(term) || StringUtil.isNumeric(term)) // Exclude the logical operators and numbers continue; ArrayList<Formula> forms = kb.ask("arg", argnum, term); if (forms == null || forms.isEmpty()) { if (letter < 'A' || term.charAt(0) == letter) result.add(term); } else { boolean found = false; Iterator<Formula> it2 = forms.iterator(); while (it2.hasNext()) { Formula formula = (Formula) it2.next(); if (formula != null) { String pred = formula.car(); if (pred.equals(rel)) { found = true; break; } } else System.out.println( "Error in Diagnostics.termsWithoutRelation(): null formula for: " + term); } if (!found) { if (letter < 'A' || term.charAt(0) == letter) result.add(term); } } if (limit > 0 && result.size() > limit) { result.add("limited to " + limit + " results"); break; } } return result; }