Пример #1
0
  /**
   * *****************************************************************
   *
   * @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;
  }
Пример #2
0
  /**
   * ***************************************************************** Returns a list of terms, each
   * of which is an instance of some exhaustively decomposed class but is not an instance of any of
   * the subclasses that constitute the exhaustive decomposition. For example, given (instance E A)
   * and (partition A B C D), then E is included in the list of terms to be returned if E is not a
   * instance of B, C, or D.
   */
  public static ArrayList<String> membersNotInAnyPartitionClass(KB kb) {

    ArrayList<String> result = new ArrayList<String>();
    try {
      TreeSet<String> reduce = new TreeSet<String>();
      // Use all partition statements and all
      // exhaustiveDecomposition statements.
      ArrayList<Formula> forms = kb.ask("arg", 0, "partition");
      if (forms == null) forms = new ArrayList<Formula>();
      ArrayList<Formula> forms2 = kb.ask("arg", 0, "exhaustiveDecomposition");
      if (forms2 != null) forms.addAll(forms2);
      boolean go = true;
      Iterator<Formula> it = forms.iterator();
      while (go && it.hasNext()) {
        Formula form = it.next();
        String parent = form.getArgument(1);
        ArrayList<String> partition = form.argumentsToArrayList(2);
        List<String> instances = kb.getTermsViaPredicateSubsumption("instance", 2, parent, 1, true);
        if ((instances != null) && !instances.isEmpty()) {
          boolean isInstanceSubsumed = false;
          boolean isNaN = true;
          String inst = null;
          Iterator<String> it2 = instances.iterator();
          while (go && it2.hasNext()) {
            isInstanceSubsumed = false;
            isNaN = true;
            inst = it2.next();
            try { // For diagnostics, try to avoid treating numbers as bonafide terms.
              double dval = Double.parseDouble(inst);
              isNaN = Double.isNaN(dval);
            } catch (Exception nex) {
            }
            if (isNaN) {
              Iterator<String> it3 = partition.iterator();
              while (it3.hasNext()) {
                String pclass = it3.next();
                if (kb.isInstanceOf(inst, pclass)) {
                  isInstanceSubsumed = true;
                  break;
                }
              }
              if (isInstanceSubsumed) continue;
              else reduce.add(inst);
            }
            if (reduce.size() > 99) go = false;
          }
        }
      }
      result.addAll(reduce);
      if (result.size() > 99) result.add("limited to 100 results");
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return result;
  }