package jtp.iw; import java.util.*; import java.io.*; import java.net.*; import jtp.*; import jtp.fol.*; import jtp.rs.*; import jtp.proof.*; import jtp.iw.*; public class IWHandler { String _queryName = null; String _baseNodeSetURI = null; String _baseNodeSetPath = null; Hashtable _baseNodeSetHash = null; // // IWHandler can handle a single ReasoningStep, or a ReasoningStepIterator. // But if it's the former, it just uses it to create a ReasoningStepIterator // during construction and then proceeds to treat the two cases identically. // ReasoningStepIterator rsi = null; // // Constructors // public IWHandler(ReasoningStep _rs, String qn, String uri, String path) { rsi = new ArrayReasoningStepIterator(_rs); _queryName = qn; _baseNodeSetURI = uri; _baseNodeSetPath = path; _baseNodeSetHash = null; } public IWHandler(ReasoningStepIterator _rsi, String qn, String uri, String path) { rsi = _rsi; _queryName = qn; _baseNodeSetURI = uri; _baseNodeSetPath = path; _baseNodeSetHash = null; } // // Publish the proof (Do everything associated with IW) // List _steps = new ArrayList(10000); public void traverseFirst(Proof proof) { _steps.add(proof); Iterator _sps = proof.getSubProofs().iterator(); while (_sps.hasNext()) { Proof _antecedent = (Proof)_sps.next(); traverseFirst(_antecedent); } } public void traverseSecond(Proof proof, int _answer) { IWUtils.generateNodeSet(proof,_steps,_answer,_baseNodeSetURI,_baseNodeSetPath,_baseNodeSetHash,_queryName); Iterator _sps = proof.getSubProofs().iterator(); while (_sps.hasNext()) { Proof _antecedent = (Proof)_sps.next(); traverseSecond(_antecedent,_answer); } } public String publish() throws Exception { String topStep = null; System.out.println("\nPublishing query over Inference Web."); // // Loop through the reasoning step iterator for this query. // ReasoningStep rs; int counter = 1; String result = ""; HashSet answers = new HashSet(); Proof proof = null; while((rs = rsi.next()) != null) { if (duplicateAnswer(rs, answers)) continue; if (counter == 1) { proof = (Proof)rs; } try { System.out.println("Generating node set files for binding " + counter); _steps.clear(); traverseFirst((Proof)rs); traverseSecond((Proof)rs,counter); } catch (Exception e) { System.out.println("\nInference Web problem for answer " + counter + "."); } counter++; } System.out.println("Generating query file"); topStep = IWUtils.generateQuery(proof,_baseNodeSetURI,_baseNodeSetPath,_baseNodeSetHash,_queryName, counter - 1); return topStep; } }