package jtp.iw; import java.text.Format; import java.util.*; import java.net.*; import java.io.*; import jtp.fol.*; import jtp.ui.rs.*; public class IWUtils { // // Available IW services as of 9/18/03, as listed at: // http://www.ksl.stanford.edu/software/iw/faq/registering/ // static String _registrarAddress = "http://belo.stanford.edu:8080/iwregistrar/"; static String _addAnswers = _registrarAddress + "AddAnswers"; static String _createNodeSet = _registrarAddress + "CreateNodeSet"; static String _createQuery = _registrarAddress + "CreateQuery"; static String _getURIFromURL = _registrarAddress + "GetURIFromURL"; // // Registered JTP inference steps as of 9/23/03, as listed at: // http://onto.stanford.edu:8080/iwregistrar/index.jsp // static String _newRegistrarAddress = "http://www.ksl.stanford.edu/software/IW/newregistry/DPR/"; static String DEMOD = _newRegistrarAddress + "Demod.daml#Demod"; static String GMP = _newRegistrarAddress + "GMP.daml#GMP"; static String MEMBER = _newRegistrarAddress + "Member.daml#Member"; static String REFORMULATION = _newRegistrarAddress + "Reformulation.daml#Reformulation"; static String SUBSUMPTION = _newRegistrarAddress + "Subsump.daml#Subsump"; static String TIMEPOINT = _newRegistrarAddress + "TimePoint.daml#TimePoint"; static String TIMEPOINTMAP = _newRegistrarAddress + "TimePointMap.daml#TimePointMap"; static String TOLD = _newRegistrarAddress + "Told.daml#Told"; static String FUNC = _newRegistrarAddress + "Func.daml#Func"; // URI for jtp, as registered with IW as of 9/21/03. static String JTPURI = "http://www.ksl.stanford.edu/software/IW/newregistry/IE/JTP.daml#JTP"; public static void printErrorMessage(String _taggedfile) { if (_taggedfile == null || _taggedfile.equals("") || _taggedfile.indexOf("") == -1 || _taggedfile.indexOf("") == -1) { System.out.println("Tagged file has not response code."); } else { String _rc = _taggedfile.substring(_taggedfile.indexOf("") + 4,_taggedfile.indexOf("")); if (!_rc.equals("00")) { int _b = _taggedfile.indexOf(""); int _e = _taggedfile.indexOf(""); if ( _b != -1 && _e != 1 && _b + 10> _e) { System.out.println("Error message is malformed"); } else { System.out.println("Error message: " + _taggedfile.substring(_b + 10,_e)); } } } } public static String filterTag(String _tagName, String _taggedfile) { //System.out.println(_taggedfile); if (_tagName == null || _tagName.equals("") || _taggedfile == null || _taggedfile.equals("")) { return "Tagged file is empty"; } else { printErrorMessage(_taggedfile); String _tagBegin = "<" + _tagName + ">"; String _tagEnd = ""; if (_taggedfile.indexOf(_tagBegin) == -1 || _taggedfile.indexOf(_tagEnd) == -1) { return ""; } else { return _taggedfile.substring(_taggedfile.indexOf(_tagBegin) + _tagBegin.length(),_taggedfile.indexOf(_tagEnd)); } } } // // Produces an IW Query. // public static String generateQuery(Proof proof, String _baseQueryURI, String _baseQueryPath, Hashtable _baseQueryHash, String _queryname, int _totAns) { Format _KIFFormat = new KIFSentenceFormatPlain(); String _queryURI = _baseQueryURI + _queryname + ".daml#" + _queryname; try { String _createQueryQuery = ""; //============================================================= // URI //============================================================= _createQueryQuery += "qy=" + URLEncoder.encode(_queryURI,"UTF-8"); //============================================================= // inference engine //============================================================= _createQueryQuery += "&ie=" + URLEncoder.encode(JTPURI,"UTF-8"); //============================================================= // labeling sentence //============================================================= if (proof == null || proof.getGoal() == null) { _createQueryQuery += "&qycont=EMPTY_QUERY"; } else { String goal = proof.getGoal().toString(); _createQueryQuery += "&qycont=" + URLEncoder.encode(goal,"UTF-8"); } //============================================================= // Add answers //============================================================= String _nsList = ""; for (int i = 1; i < _totAns + 1; i++) { // answers are node sets with number 0 (zero) String _answerName = _queryname + "ns" + i + "_0"; String _answerURI = _baseQueryURI + _answerName + ".daml#" + _answerName; _nsList += "," + URLEncoder.encode(_answerURI,"UTF-8"); } if (!_nsList.equals("")) _createQueryQuery += "&nslist=" + _nsList.substring(1); //============================================================= // Write the node set files //============================================================= URL createQueryURI = new URL(_createQuery); String _content = filterTag("result",GetURLContents.Post(createQueryURI.toString(),_createQueryQuery,"application/x-www-form-urlencoded")); System.out.println("Writing " + _queryURI); if(_baseQueryPath != null) { try { byte _b[] = _content.toString().getBytes(); String _queryFile = _baseQueryPath + _queryname + ".daml"; File _dump = new File(_queryFile); FileOutputStream _fos = new FileOutputStream(_dump); _fos.write(_b); _fos.close(); } catch (IOException ioxc) { throw new RuntimeException(ioxc.toString()); } } if(_baseQueryHash != null) { _baseQueryHash.put(_queryURI, _content.toString()); } } catch (Exception _e) { _e.printStackTrace(); } return _queryURI; } // // Produces an IW NodeSet. // public static void generateNodeSet(Proof proof, List _URIs, int _answer, String _baseNodeSetURI, String _baseNodeSetPath, Hashtable _baseNodeSetHash, String _queryName) { Format _KIFFormat = new KIFSentenceFormatPlain(); try { int _position = _URIs.indexOf(proof); String _createNodeSetQuery = ""; String _nameNodeSet = _queryName + "ns" + _answer + "_" + _position; String _nameRootNodeSet = _queryName + "ns" + _answer + "_0"; String _nodeSetURI = _baseNodeSetURI + _nameNodeSet + ".daml#" + _nameNodeSet; String _rootNodeSetURI = _baseNodeSetURI + _nameRootNodeSet + ".daml#" + _nameRootNodeSet; String _queryURI = _baseNodeSetURI + _queryName + ".daml#" + _queryName; List _sps = proof.getSubProofs(); //============================================================= // URI //============================================================= _createNodeSetQuery += "ns=" + URLEncoder.encode(_nodeSetURI,"UTF-8"); //============================================================= // inference engine //============================================================= _createNodeSetQuery += "&ie=" + URLEncoder.encode(JTPURI,"UTF-8"); //============================================================= // labeling sentence //============================================================= String goal = proof.getGoal().toString(); _createNodeSetQuery += "&nscont=" + URLEncoder.encode(goal,"UTF-8"); //============================================================= // associated query //============================================================= if (_position == 0) _createNodeSetQuery += "&qy=" + URLEncoder.encode(_queryURI,"UTF-8"); //============================================================= // associated root node set //============================================================= if (_position > 0) _createNodeSetQuery += "&nsroot=" + URLEncoder.encode(_rootNodeSetURI,"UTF-8"); //============================================================= // inference rule //============================================================= String _inferenceRuleURI = ""; if (proof instanceof DirectAssertion || _sps == null || _sps.size() == 0) _inferenceRuleURI = TOLD; else if (proof instanceof jtp.frame.EnumeratingReasoner.ProofStep || proof instanceof jtp.rs.TranslationReasoningStep || proof instanceof RuleCreationReasoningStep || proof instanceof jtp.frame.vc.MembershipProofStep || proof instanceof jtp.frame.ValueLinkCreationReasoningStep || proof instanceof jtp.frame.listen.VCListenerCreationReasoningStep || proof instanceof jtp.frame.InferredValueReasoningStep || proof instanceof jtp.gmp.ModusPonensProofStep) _inferenceRuleURI = GMP; else if (proof instanceof DemodulationReasoningStep) _inferenceRuleURI = DEMOD; else if (proof instanceof jtp.func.FunctionProofStep) _inferenceRuleURI = FUNC; else if (proof instanceof jtp.frame.fc.RuleInstallationProofStep) _inferenceRuleURI = MEMBER; else if (proof instanceof jtp.time.demod.TimePointMappingReasoningStep) _inferenceRuleURI = TIMEPOINTMAP; else if (proof instanceof jtp.modelim.AskingQueryProcessor$ReformulationProofStep) _inferenceRuleURI = REFORMULATION; else System.out.println("Error, unrecognized inference rule."); if (!_inferenceRuleURI.equals("")) { _createNodeSetQuery += "&rl=" + URLEncoder.encode(_inferenceRuleURI,"UTF-8"); } //============================================================= // variable bindings //============================================================= String _termList = ""; String _varList = ""; for (Iterator it = getIWBindings(proof).entrySet().iterator(); it.hasNext();) { Map.Entry _entry = (Map.Entry)it.next(); _termList += "," + URLEncoder.encode(_entry.getKey().toString(),"UTF-8"); _varList += "," + URLEncoder.encode(_entry.getValue().toString(),"UTF-8"); } if (!_termList.equals("") && !_varList.equals("")) { // indexes of substrings are '1' to drop the first comma _createNodeSetQuery += "&trlist=" + _termList.substring(1); _createNodeSetQuery += "&vrlist=" + _varList.substring(1); } //============================================================= // antecedents //============================================================= if (_sps != null && _sps.size() > 0) { String _antList = ""; for (Iterator it2 = _sps.iterator(); it2.hasNext();) { String _antNodeSet = _queryName + "ns" + _answer + "_" + _URIs.indexOf((Proof)it2.next()); String _antURI = _baseNodeSetURI + _antNodeSet + ".daml#" + _antNodeSet; _antList += "," + URLEncoder.encode(_antURI,"UTF-8"); } if (!_antList.equals("") && !_antList.equals("")) { // index of substring is '1' to drop the first comma _createNodeSetQuery += "&nsant=" + _antList.substring(1); } } //============================================================= // knowledge sources //============================================================= String ontologyURL = ""; if (proof instanceof DirectAssertion) { if (((DirectAssertion)proof).getKBLocation() != null && ! ((DirectAssertion)proof).getKBLocation().toString().equals("")) { ontologyURL = ((DirectAssertion)proof).getKBLocation().toString(); } } else if (proof instanceof RuleCreationReasoningStep) { if (((RuleCreationReasoningStep)proof).getKBLocation() != null && ! ((RuleCreationReasoningStep)proof).getKBLocation().toString().equals("")) { ontologyURL = ((RuleCreationReasoningStep)proof).getKBLocation().toString(); } } else if (proof instanceof VCListenerCreationReasoningStep) { if (((VCListenerCreationReasoningStep)proof).getKBLocation() != null && ! ((VCListenerCreationReasoningStep)proof).getKBLocation().toString().equals("")) { ontologyURL = ((VCListenerCreationReasoningStep)proof).getKBLocation().toString(); } } if (!ontologyURL.equals("")) { String _ontURI = filterTag("result",GetURLContents.exec(_getURIFromURL + "?url=" + URLEncoder.encode(ontologyURL,"UTF-8"))); if (_ontURI != null && !_ontURI.equals("")) { _createNodeSetQuery += "&kslist=" + _ontURI; } } //============================================================= // Write the node set files //============================================================= URL createNodeSetURI = new URL(_createNodeSet); String _content = filterTag("result",GetURLContents.Post(createNodeSetURI.toString(),_createNodeSetQuery,"application/x-www-form-urlencoded")); System.out.println("Writing " + _nodeSetURI); if(_baseNodeSetPath != null) { try { byte _b[] = _content.toString().getBytes(); String _nodeSetFile = _baseNodeSetPath + _nameNodeSet + ".daml"; File _dump = new File(_nodeSetFile); FileOutputStream _fos = new FileOutputStream(_dump); _fos.write(_b); _fos.close(); } catch (IOException ioxc) { throw new RuntimeException(ioxc.toString()); } } if(_baseNodeSetHash != null) { _baseNodeSetHash.put(_nodeSetURI, _content.toString()); } } catch (Exception _e) { _e.printStackTrace(); } return; } }