
TPTPWorld.InterfaceTPTP Maven / Gradle / Ivy
package TPTPWorld;
import java.io.*;
import java.net.URL;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.articulate.sigma.*;
public class InterfaceTPTP {
public static String TPTPWorld = "";
public static String BuiltInDir = "";
public static String defaultSystemBuiltIn = "";
public static String defaultSystemLocal = "";
public static String defaultSystemRemote = "";
public static String SoTPTP;
public static String tptp4X;
public static boolean tptpWorldExists = false;
public static boolean builtInExists = false;
public static ArrayList systemListBuiltIn = new ArrayList();
public static ArrayList systemListLocal = new ArrayList();
public static ArrayList systemListRemote = new ArrayList();
public static String SystemOnTPTPFormReplyURL =
"http://www.cs.miami.edu/~tptp/cgi-bin/SystemOnTPTPFormReply";
private static Logger logger;
private static class ATPResult {
public static String cleanResult = "";
public static String originalResult = "";
public static String printResult = "";
public static String idvResult = "";
public ATPResult () {
cleanResult = "";
originalResult = "";
printResult = "";
idvResult = "";
}
}
/** ***************************************************************
*/
public static void init() {
if (logger == null)
logger = Logger.getLogger(InterfaceTPTP.class.getName().toString());
logger.info("Initializing InterfaceTPTP.");
TPTPWorld = KBmanager.getMgr().getPref("tptpHomeDir");
BuiltInDir = TPTPWorld + "/Systems";
//----Check available Systems
String systemsInfo = BuiltInDir + "/SystemInfo";
builtInExists = (new File(BuiltInDir)).exists() && (new File(systemsInfo)).exists();
SoTPTP = TPTPWorld + "/SystemExecution/SystemOnTPTP";
tptp4X = TPTPWorld + "/ServiceTools/tptp4X";
tptpWorldExists = (new File(SoTPTP)).exists();
BufferedReader reader;
String responseLine;
//----Check builtin systems
if (builtInExists) {
systemListBuiltIn = SystemOnTPTP.listSystems(BuiltInDir, "SoTPTP");
defaultSystemBuiltIn = "EP---0.999";
}
//----Check local TPTP
if (tptpWorldExists) {
try {
String command = SoTPTP + " " + "-w" + " " + "SoTPTP";
Process proc = Runtime.getRuntime().exec(command);
systemListLocal.add("Choose system");
reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
//----Read List of Local Systems
while ((responseLine = reader.readLine()) != null) {
systemListLocal.add(responseLine);
//----Try use EP as the default system
if (responseLine.startsWith("EP---")) {
defaultSystemLocal = responseLine;
}
}
reader.close();
} catch (Exception ioe) {
logger.severe("Exception: " + ioe.getMessage());
}
}
//----Call RemoteSoT to retrieve remote list of systems
Hashtable URLParameters = new Hashtable();
//----Note, using www.tptp.org does not work
systemListRemote.add("Choose system");
URLParameters.put("NoHTML","1");
URLParameters.put("QuietFlag","-q2");
URLParameters.put("SubmitButton","ListSystems");
URLParameters.put("ListStatus","SoTPTP");
try {
URL replyUrl = new URL(SystemOnTPTPFormReplyURL);
//InputStreamReader isr = null;
InputStreamReader isr = new InputStreamReader(ClientHttpRequest.post(replyUrl,
URLParameters));
reader = new BufferedReader(isr);
//----Read List of Remote Systems
while ((responseLine = reader.readLine()) != null) {
systemListRemote.add(responseLine);
//----Try use EP as the default system
if (responseLine.startsWith("EP---")) {
defaultSystemRemote = responseLine;
}
else defaultSystemRemote = "";
}
reader.close();
} catch (Exception ioe) {
logger.severe("Exception: " + ioe.getMessage());
}
}
/** ***************************************************************
*/
public static String callTPTP(String location,
String systemChosen,
String problemFile,
int timeout,
String quietFlag,
String tstpFormat) throws Exception {
String result = "";
if (location.equalsIgnoreCase("builtin"))
result = callBuiltInTPTP(systemChosen,
problemFile,
timeout,
quietFlag,
tstpFormat).originalResult;
else if (location.equalsIgnoreCase("local"))
result = callLocalTPTP(systemChosen,
problemFile,
timeout,
quietFlag,
tstpFormat).originalResult;
else if (location.equalsIgnoreCase("remote"))
result = callRemoteTPTP(systemChosen,
problemFile,
timeout,
quietFlag,
tstpFormat).originalResult;
else
throw new Exception("There's no SystemOnTPTP location \""+location+"\".");
return trimUnexpectedTokens(result);
}
/** ***************************************************************
* This method attempts to remove from the String input any
* unexpected and spurious leading tokens, such as those resulting
* from error or status messages.
*
* @param input The entire multi-line response returned by a call
* to built-in or remote SystemOnTPTP
*
* @return A String with any unexpected leading text removed
*/
private static String trimUnexpectedTokens(String input) {
String output = input;
String trimmed = null;
try {
if ((output instanceof String) && !output.equals("")) {
output = output.trim();
List highLevelForms = Arrays.asList("%",
"fof(",
"cnf(",
"include",
"input_formula",
"input_clause");
int idx = -1;
int nextIdx = -1;
for (String token : highLevelForms) {
nextIdx = output.indexOf(token);
if ((nextIdx > -1) && ((idx == -1) || (nextIdx < idx)))
idx = nextIdx;
}
if (idx == -1) {
trimmed = output;
output = "";
}
else if (idx > 0) {
trimmed = output.substring(0, idx);
output = output.substring(idx);
}
}
if (trimmed != null) {
logger.info((((input instanceof String) && (input.length() > 20))
? (input.substring(0, 20) + " ...")
: input) + "; trimmed == \"" + trimmed + "\"");
}
}
catch (Exception ex) {
logger.warning("ERROR in InterfaceTPTP.trimUnexpectedTokens("
+ (((input instanceof String) && (input.length() > 20))
? (input.substring(0, 20) + " ...")
: input) + "); trimmed == \"" + trimmed + "\"; output == \"" + output + "\"");
ex.printStackTrace();
}
return output;
}
/** ***************************************************************
*/
public static ATPResult callRemoteTPTP (String systemChosen,
String problemFile,
int timeout,
String quietFlag,
String tstpFormat) {
ATPResult atpOut = new ATPResult ();
BufferedReader reader = null;
try {
String responseLine = "";
boolean tptpEnd = false;
//----Need to check the name exists
Hashtable URLParameters = new Hashtable();
URLParameters.put("NoHTML","1");
if (quietFlag.equals("IDV")) {
URLParameters.put("IDV","-T");
URLParameters.put("QuietFlag","-q4");
URLParameters.put("X2TPTP",tstpFormat);
}
else if (quietFlag.equals("hyperlinkedKIF")) {
URLParameters.put("QuietFlag","-q3");
URLParameters.put("X2TPTP","-S");
}
else {
URLParameters.put("QuietFlag",quietFlag);
URLParameters.put("X2TPTP",tstpFormat);
}
//----Need to offer automode
URLParameters.put("System___System",systemChosen);
URLParameters.put("TimeLimit___TimeLimit", new Integer(timeout));
URLParameters.put("ProblemSource","UPLOAD");
URLParameters.put("UPLOADProblem",new File(problemFile));
URLParameters.put("SubmitButton","RunSelectedSystems");
URL url = new URL(SystemOnTPTPFormReplyURL);
//InputStreamReader isr = null;
InputStreamReader isr =
new InputStreamReader(ClientHttpRequest.post(url,URLParameters));
reader = new BufferedReader(isr);
atpOut.printResult += "";
while ((responseLine = reader.readLine()) != null) {
responseLine = trimUnexpectedTokens(responseLine);
responseLine = StringUtil.safeToKifNamespaceDelimiters(responseLine);
if (responseLine.startsWith("Loading IDV"))
tptpEnd = true;
if (StringUtil.isNonEmptyString(responseLine)
&& !responseLine.startsWith("%")
&& !tptpEnd)
atpOut.cleanResult += responseLine + "\n";
if (tptpEnd && quietFlag.equals("IDV"))
atpOut.idvResult += responseLine + "\n";
atpOut.originalResult += responseLine + "\n";
if (!quietFlag.equals("hyperlinkedKIF") && !quietFlag.equals("IDV"))
atpOut.printResult += responseLine + "\n";
}
atpOut.idvResult += "
\n";
atpOut.printResult += "
";
}
catch (Exception ex) {
logger.warning("Error: " + ex.getStackTrace());
ex.printStackTrace();
}
finally {
try {
if (reader != null) reader.close();
}
catch (Exception ioe) {
logger.warning("Error: " + ioe.getStackTrace());
ioe.printStackTrace();
}
}
return atpOut;
}
/** ***************************************************************
*/
public static ATPResult callLocalTPTP (String systemChosen,
String problemFile,
int timeout,
String quietFlag,
String tstpFormat) {
if (logger.isLoggable(Level.FINER)) {
String[] params = {"systemChosen = " + systemChosen, "problemFile = " + problemFile, "timeout = " + timeout, "quietFlag = " + quietFlag, "tstpFormat = " + tstpFormat};
logger.entering("InterfaceTPTP", "callLocalTPTP", params);
}
ATPResult atpOut = new ATPResult ();
BufferedReader reader = null;
try {
String responseLine = "";
Process proc;
String command;
if (quietFlag.equals("hyperlinkedKIF")) {
command = SoTPTP + " " +
"-q3" + " " + // quietFlag
systemChosen + " " +
timeout + " " +
"-S" + " " + //tstpFormat
problemFile;
}
else if (quietFlag.equals("IDV")) {
command = SoTPTP + " " +
"-q4" + " " + // quietFlag
systemChosen + " " +
timeout + " " +
"-S" + " " + //tstpFormat
problemFile;
}
else {
command = SoTPTP + " " +
quietFlag + " " +
systemChosen + " " +
timeout + " " +
tstpFormat + " " +
problemFile;
}
logger.finer("command: " + command);
proc = Runtime.getRuntime().exec(command);
reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
atpOut.printResult += ""; while ((responseLine = reader.readLine()) != null) { //responseLine = trimUnexpectedTokens(responseLine); responseLine = StringUtil.safeToKifNamespaceDelimiters(responseLine); atpOut.originalResult += responseLine + "\n"; if (StringUtil.isNonEmptyString(responseLine) && !responseLine.startsWith("%")) atpOut.cleanResult += responseLine + "\n"; if (!quietFlag.equals("hyperlinkedKIF") && !quietFlag.equals("IDV")) atpOut.printResult += responseLine + "\n"; } atpOut.printResult += ""; } catch (Exception ex) { logger.severe("ERROR: " + ex.getStackTrace()); ex.printStackTrace(); } finally { try { if (reader != null) reader.close(); } catch (Exception ioe) { logger.warning("ERROR: " + ioe.getStackTrace()); ioe.printStackTrace(); } } logger.exiting("InterfaceTPTP", "callLocalTPTP", atpOut); return atpOut; } /** *************************************************************** */ public static ATPResult callBuiltInTPTP (String systemChosen, String problemFile, int timeout, String quietFlag, String tstpFormat) { ATPResult atpOut = new ATPResult (); try { String qq; String format = ""; String result = ""; if (quietFlag.equals("IDV")) { qq = "-q4"; format = "-S"; } else if (quietFlag.equals("hyperlinkedKIF")) { qq = "-q4"; format = "-S"; } else { qq = quietFlag; format = tstpFormat; } result = SystemOnTPTP.SystemOnTPTP(systemChosen, BuiltInDir, timeout, qq, format, problemFile); result = trimUnexpectedTokens(result); result = StringUtil.safeToKifNamespaceDelimiters(result); atpOut.originalResult = result; atpOut.printResult += "
"; if (!quietFlag.equals("hyperlinkedKIF") && !quietFlag.equals("IDV")) atpOut.printResult += result; if (quietFlag.equals("IDV")) { // IDV can't handle comments? StringTokenizer st = new StringTokenizer(result,"\n"); String temp = ""; while (st.hasMoreTokens()) { String next = st.nextToken(); if (StringUtil.isNonEmptyString(next) && !next.startsWith("%")) temp += next + "\n"; } atpOut.cleanResult = temp; } else atpOut.cleanResult = result; atpOut.printResult += ""; } catch (Exception ex) { logger.severe("ERROR: " + ex.getStackTrace()); ex.printStackTrace(); } return atpOut; } /** *************************************************************** */ public static String queryTPTP (String stmt, int timeout, int maxAnswers, String lineHtml, String systemChosen, String location, String quietFlag, String kbName, String language) throws Exception { if (logger.isLoggable(Level.FINER)) { String[] params = {"stmt = " + stmt, "timeout = " + timeout, "maxAnswers = " + maxAnswers, "lineHtml = " + lineHtml, "systemChosen = " + systemChosen, "location = " + location, "quietFlag = " + quietFlag, "kbName = " + kbName, "language = " + language}; logger.entering("InterfaceTPTP", "queryTPTP", params); } logger.finest("BuiltIn Exists? : " + builtInExists); logger.finest("TPTP World Exists? :" + tptpWorldExists); //----Setup String resultAll = ""; String tstpFormat = "-S"; KB kb = KBmanager.getMgr().getKB(kbName); String originalKBFileName = null; String responseLine; BufferedReader reader; //----------------------------------------------------------------------------- //----Code for doing the query String TPTP_QUESTION_SYSTEM = "SNARK---"; String TPTP_ANSWER_SYSTEM = "Metis---"; StringBuffer sbStatu1s = new StringBuffer(); String kbFileName = null; Formula conjectureFormula; //----Result of query (passed to tptp4X then passed to HTMLformatter.formatProofResult) String result = ""; String newResult = ""; String idvResult = ""; String originalResult = ""; String command; Process proc; boolean isQuestion = systemChosen.startsWith(TPTP_QUESTION_SYSTEM); String conjectureTPTPFormula = ""; // Build query: //----------------------------------------------------------------------------- //----Add KB contents here conjectureFormula = new Formula(); conjectureFormula.theFormula = stmt; String oldConjecture = conjectureFormula.theFormula; conjectureFormula.theFormula = conjectureFormula.makeQuantifiersExplicit(true); boolean suppressAnswerExtraction = oldConjecture.equals(conjectureFormula.theFormula); logger.finest("conjectureFormula.theFormula == " + conjectureFormula.theFormula + "\nsuppressAnswerExtraction == " + suppressAnswerExtraction); //if (suppressAnswerExtraction) resultAll += "suppress definite answers
"; SUMOformulaToTPTPformula stptp = new SUMOformulaToTPTPformula(); stptp._f = conjectureFormula; stptp.tptpParse(conjectureFormula,true, kb); Iterator
"; //----Get symbols from lastAnswer TreeSet
There was an Answer before!
"; String bindConjecture = ("fof(bindConj, conjecture,(" + LooksDifferent.bindConjecture(originalConjecture, originalAnswer, lastAnswer) + "))."); //----With new bindConjecture, take last result, filter out anything with LDs in it, put in prover String axioms = LooksDifferent.filterLooksDifferent(originalResult); //----Redo proof using OneAnswerSystem again String bindProblem = axioms + " " + bindConjecture; String bindResult = AnswerFinder.findProof(bindProblem, BuiltInDir); newResult = TPTP2SUMO.convert(bindResult, lastAnswer, true); } boolean isTheorem = SystemOnTPTP.isTheorem(originalResult); boolean isCounterSatisfiable = SystemOnTPTP.isCounterSatisfiable(originalResult); boolean proofExists = SystemOnTPTP.proofExists(originalResult); int timeUsed = SystemOnTPTP.timeUsed(originalResult); if (isTheorem) { if (proofExists) { try { //----Remove bindings, if no existential quantifiers have been //----made explicit, i.e., the query is closed if (suppressAnswerExtraction) { int opnTagIdx = newResult.indexOf("
"; } else if (isCounterSatisfiable) resultAll += "Answer "+(numAnswers+1)+". No [CounterSatisfiable]
"; else if (numAnswers == 0) resultAll += "Answer "+(numAnswers+1)+". No
"; } //----If lastAnswer != null (we found an answer) && there is an answer (lastAnswer.size() > 0) if (lastAnswer != null && lastAnswer.size() > 0) numAnswers++; //----Add query time limit to while loop break } while (numAnswers < maxAnswers && lastAnswer != null && lastAnswer.size() > 0); //----Delete the kbFile if (originalKBFileName != null) (new File(originalKBFileName)).delete(); logger.exiting("InterfaceTPTP", "queryTPTP", resultAll); return resultAll; } /** *************************************************************** */ public static void main () { } }
© 2015 - 2025 Weber Informatics LLC | Privacy Policy