示例#1
0
  /**
   * ***************************************************************** Iterating through all
   * formulas, return a proof of an inconsistent or redundant one, if such a thing exists.
   */
  public static String kbConsistencyCheck(KB kb) {

    int timeout = 10;
    int maxAnswers = 1;
    String proof;
    String result = null;

    StringBuffer answer = new StringBuffer();
    KB empty = makeEmptyKB("consistencyCheck");

    System.out.println("=================== Consistency Testing ===================");
    try {
      Formula theQuery = new Formula();
      Collection allFormulas = kb.formulaMap.values();
      Iterator it = allFormulas.iterator();
      while (it.hasNext()) {
        Formula query = (Formula) it.next();
        FormulaPreprocessor fp = new FormulaPreprocessor();
        ArrayList processedQueries =
            fp.preProcess(query, false, kb); // may be multiple because of row vars.
        // System.out.println(" query = " + query);
        // System.out.println(" processedQueries = " + processedQueries);

        String processedQuery = null;
        Iterator q = processedQueries.iterator();

        System.out.println(
            "INFO in Diagnostics.kbConsistencyCheck(): size = " + processedQueries.size());
        while (q.hasNext()) {
          Formula f = (Formula) q.next();
          System.out.println("INFO in Diagnostics.kbConsistencyCheck(): formula = " + f.theFormula);
          processedQuery = f.makeQuantifiersExplicit(false);
          System.out.println(
              "INFO in Diagnostics.kbConsistencyCheck(): processedQuery = " + processedQuery);
          proof = StringUtils.join(empty.ask(processedQuery, timeout, maxAnswers), " ");
          StringBuffer a = new StringBuffer();
          a.append(reportAnswer(kb, proof, query, processedQuery, "Redundancy"));
          //  if (answer.length() != 0) return answer;
          answer.append(a);

          StringBuffer negatedQuery = new StringBuffer();
          negatedQuery.append("(not " + processedQuery + ")");
          proof = StringUtils.join(empty.ask(negatedQuery.toString(), timeout, maxAnswers), " ");
          a.append(reportAnswer(kb, proof, query, negatedQuery.toString(), "Inconsistency"));
          if (a.length() != 0) {
            answer.append(a);
            return answer.toString();
          }
        }
        empty.tell(query.theFormula);
      }
    } catch (Exception ex) {
      return ("Error in Diagnostics.kbConsistencyCheck() while executing query: "
          + ex.getMessage());
    }
    return "No contradictions or redundancies found.";
  }
示例#2
0
  /**
   * *************************************************************** Return the most specific type
   * for skolem variable.
   *
   * @param tpp The structure learned from E's response
   * @param kb The knowledge base used to find skolem term's types
   *     <p>For example, original binding = esk3_0 set binding = "An instance of Human" (Human is
   *     the most specific type for esk3_0 in the given proof)
   *     <p>original binding = esk3_1 set binding = "An instance of Human, Agent" (If multiple types
   *     are found for esk3_1)
   */
  public static void findTypesForSkolemTerms(TPTP3ProofProcessor tpp, KB kb) {

    ArrayList<String> bindings = tpp.bindings;
    FormulaPreprocessor fp = new FormulaPreprocessor();
    for (int i = 0; i < bindings.size(); i++) {
      String binding = bindings.get(i);
      if (binding.startsWith("esk")) {
        ArrayList<String> skolemStmts = ProofProcessor.returnSkolemStmt(binding, tpp.proof);
        HashSet<String> types = new HashSet<>();
        for (String skolemStmt : skolemStmts) {
          Pattern p = Pattern.compile("\\(instance ([a-zA-Z0-9\\-_]+) ([a-zA-Z0-9\\-_]+)");
          Matcher m = p.matcher(skolemStmt);
          while (m.find()) {
            String cl = m.group(2);
            types.add(cl);
          }

          p = Pattern.compile("\\(subclass ([a-zA-Z0-9\\-_]+) ([a-zA-Z0-9\\-]+)");
          m = p.matcher(skolemStmt);
          while (m.find()) {
            String cl = m.group(2);
            types.add(cl);
          }
        }
        if (kb.kbCache.checkDisjoint(kb, types) == true) {
          // check if there are contradiction among the types returned from E
          bindings.remove(binding);
          binding = "Get type contradiction for " + binding + " in " + types;
          bindings.add(binding);
        } else {
          fp.winnowTypeList(types, kb);
          if (types != null && types.size() > 0) {
            if (types.size() == 1) {
              binding = "an instance of " + types.toArray()[0];
            } else {
              binding = "an instance of ";
              boolean start = true;
              for (String t : types) {
                if (start) {
                  binding += t;
                  start = false;
                } else {
                  binding += ", " + t;
                }
              }
            }
            bindings.set(i, binding);
          }
        }
      } else {
        binding = TPTP2SUMO.transformTerm(binding);
        bindings.set(i, binding);
      }
    }
  }