@@ -0,0 +1,176 @@ | |||
package application; | |||
import java.io.IOException; | |||
import javax.servlet.ServletException; | |||
import javax.servlet.http.HttpServletRequest; | |||
import javax.servlet.http.HttpServletResponse; | |||
import log.QueryLogger; | |||
import org.json.*; | |||
import org.eclipse.jetty.server.Request; | |||
import org.eclipse.jetty.server.handler.AbstractHandler; | |||
import rdf.Sparql; | |||
import qa.GAnswer; | |||
import qa.Globals; | |||
import qa.Matches; | |||
public class GanswerHandler extends AbstractHandler{ | |||
public static String errorHandle(String status,String message,String question,QueryLogger qlog){ | |||
JSONObject exobj = new JSONObject(); | |||
try { | |||
exobj.put("status", status); | |||
exobj.put("message", message); | |||
exobj.put("questions", question); | |||
if(qlog!=null&&qlog.rankedSparqls!=null&&qlog.rankedSparqls.size()>0){ | |||
exobj.put("sparql", qlog.rankedSparqls.get(0).toStringForGStore2()); | |||
} | |||
} catch (Exception e1) { | |||
} | |||
return exobj.toString(); | |||
} | |||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) | |||
throws IOException, ServletException { | |||
String question = ""; | |||
QueryLogger qlog = null; | |||
try{ | |||
response.setContentType("text/html;charset=utf-8"); | |||
response.setStatus(HttpServletResponse.SC_OK); | |||
//step1: parsing input json | |||
String data = request.getParameter("data"); | |||
data = data.replace("%22","\""); | |||
JSONObject jsonobj = new JSONObject(); | |||
int needAnswer = 0; | |||
int needSparql = 1; | |||
question = "Show me all Czech movies"; | |||
jsonobj = new JSONObject(data); | |||
needAnswer = jsonobj.getInt("maxAnswerNum"); | |||
needSparql = jsonobj.getInt("needSparql"); | |||
question = jsonobj.getString("questions"); | |||
Globals.MaxAnswerNum = needAnswer; | |||
//step2 run GAnswer Logic | |||
String input = question; | |||
GAnswer ga = new GAnswer(); | |||
qlog = ga.getSparqlList(input); | |||
if(qlog == null || qlog.rankedSparqls == null){ | |||
try { | |||
baseRequest.setHandled(true); | |||
response.getWriter().println(errorHandle("500","UnvalidQuestionException: the question you input is invalid, please check",question,qlog)); | |||
} catch (Exception e1) { | |||
} | |||
return; | |||
} | |||
int idx; | |||
//step2 construct response | |||
JSONObject resobj = new JSONObject(); | |||
resobj.put("status", "200"); | |||
resobj.put("id","test"); | |||
resobj.put("query",jsonobj.getString("questions")); | |||
JSONObject tmpobj = new JSONObject(); | |||
if(needAnswer > 0){ | |||
if(qlog!=null && qlog.rankedSparqls.size()!=0){ | |||
Sparql curSpq = null; | |||
Matches m = null; | |||
for(idx = 1;idx<=Math.min(qlog.rankedSparqls.size(), 5);idx+=1){ | |||
curSpq = qlog.rankedSparqls.get(idx-1); | |||
if(curSpq.tripleList.size()>0&&curSpq.questionFocus!=null){ | |||
m = ga.getAnswerFromGStore2(curSpq); | |||
} | |||
if(m!=null&&m.answers!=null){ | |||
qlog.sparql = curSpq; | |||
qlog.match = m; | |||
break; | |||
} | |||
} | |||
curSpq = ga.getUntypedSparql(curSpq); | |||
if(curSpq!=null){ | |||
m = ga.getAnswerFromGStore2(curSpq); | |||
} | |||
if(m!=null&&m.answers!=null){ | |||
qlog.sparql = curSpq; | |||
qlog.match = m; | |||
} | |||
if(qlog.match==null) | |||
qlog.match=new Matches(); | |||
if(qlog.sparql==null) | |||
qlog.sparql = qlog.rankedSparqls.get(0); | |||
qlog.reviseAnswers(); | |||
//adding variables to result json | |||
JSONArray vararr = new JSONArray(); | |||
for(String var : qlog.sparql.variables){ | |||
vararr.put(var); | |||
} | |||
resobj.put("vars", vararr); | |||
//adding answers to result json | |||
JSONArray ansobj = new JSONArray(); | |||
JSONObject bindingobj; | |||
System.out.println(qlog.match.answersNum); | |||
for(int i=0;i<qlog.match.answersNum;i++){ | |||
int j = 0; | |||
bindingobj = new JSONObject(); | |||
for(String var:qlog.sparql.variables){ | |||
JSONObject bidobj = new JSONObject(); | |||
if(qlog.match.answers[i][j].startsWith("\"")) | |||
bidobj.put("type", "literal"); | |||
else | |||
bidobj.put("type", "uri"); | |||
String[] ansRiv = qlog.match.answers[i][j].split(":"); | |||
bidobj.put("value", ansRiv[ansRiv.length-1]); | |||
System.out.println(qlog.match.answers[i][j]); | |||
j += 1; | |||
bindingobj.put(var, bidobj); | |||
} | |||
ansobj.put(bindingobj); | |||
} | |||
tmpobj.put("bindings", ansobj); | |||
} | |||
resobj.put("results", tmpobj); | |||
} | |||
if(needSparql>0){ | |||
JSONArray spqarr = new JSONArray(); | |||
for(idx=0;idx<needSparql&&idx<qlog.rankedSparqls.size();idx+=1){ | |||
spqarr.put(qlog.rankedSparqls.get(idx).toStringForGStore2()); | |||
} | |||
resobj.put("sparql", spqarr); | |||
} | |||
baseRequest.setHandled(true); | |||
response.getWriter().println(resobj.toString()); | |||
} | |||
catch(Exception e){ | |||
if(e instanceof IOException){ | |||
try { | |||
baseRequest.setHandled(true); | |||
response.getWriter().println(errorHandle("500","IOException",question,qlog)); | |||
} catch (Exception e1) { | |||
} | |||
} | |||
else if(e instanceof JSONException){ | |||
try { | |||
baseRequest.setHandled(true); | |||
response.getWriter().println(errorHandle("500","JSONException",question,qlog)); | |||
} catch (Exception e1) { | |||
} | |||
} | |||
else if(e instanceof ServletException){ | |||
try { | |||
baseRequest.setHandled(true); | |||
response.getWriter().println(errorHandle("500","ServletException",question,qlog)); | |||
} catch (Exception e1) { | |||
} | |||
} | |||
else { | |||
try { | |||
baseRequest.setHandled(true); | |||
response.getWriter().println(errorHandle("500","Unkown Exception",question,qlog)); | |||
} catch (Exception e1) { | |||
} | |||
} | |||
} | |||
} | |||
} |
@@ -0,0 +1,37 @@ | |||
package application; | |||
import org.eclipse.jetty.server.Server; | |||
import org.eclipse.jetty.server.handler.ContextHandler; | |||
import org.eclipse.jetty.server.handler.ContextHandlerCollection; | |||
import org.eclipse.jetty.server.handler.ErrorHandler; | |||
import org.eclipse.jetty.server.Handler; | |||
import qa.Globals; | |||
public class GanswerHttp { | |||
public static void main(String[] args) throws Exception { | |||
//step 1: initialize the server with a given port | |||
Server server = new Server(9999); | |||
//step 2: attach gAnswer function handler to the server | |||
ContextHandler contextGS = new ContextHandler("/gSolve"); | |||
contextGS.setHandler(new GanswerHandler()); | |||
ContextHandler contextGI = new ContextHandler("/gInfo"); | |||
contextGI.setHandler(new GinfoHandler()); | |||
ContextHandlerCollection contexts = new ContextHandlerCollection(); | |||
contexts.setHandlers(new Handler[] {contextGS, contextGI}); | |||
server.setHandler(contexts); | |||
//step 3: attach gAnswer error handler to the server | |||
//TODO: using default error handler currently. Should replace it with a custom one | |||
ErrorHandler errorHandler = new ErrorHandler(); | |||
errorHandler.setShowStacks(false); | |||
server.addBean(errorHandler); | |||
//step 4: start the server and initialize gAnswer | |||
server.start(); | |||
server.dumpStdErr(); | |||
Globals.init(); | |||
server.join(); | |||
System.out.println("Server ready!"); | |||
} | |||
} |
@@ -0,0 +1,106 @@ | |||
package application; | |||
import java.io.*; | |||
import java.net.*; | |||
import java.lang.*; | |||
import java.net.URL; | |||
import java.net.URLConnection; | |||
import java.net.URLEncoder; | |||
import java.net.URLDecoder; | |||
import java.io.UnsupportedEncodingException; | |||
import java.util.List; | |||
import java.util.Map; | |||
public class GanswerHttpConnector { | |||
public static final String defaultServerIP = "59.108.48.19"; | |||
public static final int defaultServerPort = 9999; | |||
private String serverIP; | |||
private int serverPort; | |||
public GanswerHttpConnector() { | |||
this.serverIP = GanswerHttpConnector.defaultServerIP; | |||
this.serverPort = GanswerHttpConnector.defaultServerPort; | |||
} | |||
public GanswerHttpConnector(int _port) { | |||
this.serverIP = GanswerHttpConnector.defaultServerIP; | |||
this.serverPort = _port; | |||
} | |||
public GanswerHttpConnector(String _ip, int _port) { | |||
this.serverIP = _ip; | |||
this.serverPort = _port; | |||
} | |||
public String sendGet(String param,String context) { | |||
String url = "http://" + this.serverIP + ":" + this.serverPort+context; | |||
StringBuffer result = new StringBuffer(); | |||
BufferedReader in = null; | |||
System.out.println("parameter: "+param); | |||
try { | |||
param = URLEncoder.encode(param, "UTF-8"); | |||
} | |||
catch (UnsupportedEncodingException ex) { | |||
throw new RuntimeException("Broken VM does not support UTF-8"); | |||
} | |||
try { | |||
//careful: if you encode the "?data=" part, jetty may not accept such a encoding | |||
String urlNameString = url + "/?data=" + param; | |||
System.out.println("request: "+urlNameString); | |||
URL realUrl = new URL(urlNameString); | |||
URLConnection connection = realUrl.openConnection(); | |||
connection.setRequestProperty("accept", "*/*"); | |||
connection.setRequestProperty("connection", "Keep-Alive"); | |||
//set agent to avoid: speed limited by server if server think the client not a browser | |||
connection.setRequestProperty("user-agent", | |||
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); | |||
connection.connect(); | |||
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); | |||
String line; | |||
while ((line = in.readLine()) != null) { | |||
result.append(line+"\n"); | |||
} | |||
} catch (Exception e) { | |||
System.out.println("error in get request: " + e); | |||
e.printStackTrace(); | |||
} | |||
finally { | |||
try { | |||
if (in != null) { | |||
in.close(); | |||
} | |||
} catch (Exception e2) { | |||
e2.printStackTrace(); | |||
} | |||
} | |||
return result.toString(); | |||
} | |||
public String gSolve(String data){ | |||
String param = data; | |||
String rst = sendGet(param,"/gSolve"); | |||
System.out.println(rst); | |||
return rst; | |||
} | |||
public String gInfo(){ | |||
String param = ""; | |||
String rst = sendGet(param,"/gInfo"); | |||
System.out.println(rst); | |||
return rst; | |||
} | |||
public static void main(String[] args){ | |||
GanswerHttpConnector ghc = new GanswerHttpConnector(); | |||
String data = "{\"dictionary\":{\"id\":\"test\"},\"log\":\"1\",\"maxAnswerNum\":\"3\",\"needSparql\":\"2\",\"questions\":\"\"}"; | |||
ghc.gInfo(); | |||
ghc.gSolve(data); | |||
} | |||
} |
@@ -0,0 +1,80 @@ | |||
package application; | |||
import java.io.IOException; | |||
import javax.servlet.ServletException; | |||
import javax.servlet.http.HttpServletRequest; | |||
import javax.servlet.http.HttpServletResponse; | |||
import log.QueryLogger; | |||
import org.json.*; | |||
import org.eclipse.jetty.server.Request; | |||
import org.eclipse.jetty.server.handler.AbstractHandler; | |||
import qa.Globals; | |||
public class GinfoHandler extends AbstractHandler{ | |||
public static String errorHandle(String status,String message,String question,QueryLogger qlog){ | |||
JSONObject exobj = new JSONObject(); | |||
try { | |||
exobj.put("status", status); | |||
exobj.put("message", message); | |||
exobj.put("questions", question); | |||
if(qlog!=null&&qlog.rankedSparqls!=null&&qlog.rankedSparqls.size()>0){ | |||
exobj.put("sparql", qlog.rankedSparqls.get(0).toStringForGStore2()); | |||
} | |||
} catch (Exception e1) { | |||
} | |||
return exobj.toString(); | |||
} | |||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) | |||
throws IOException, ServletException { | |||
try{ | |||
response.setContentType("text/html;charset=utf-8"); | |||
response.setStatus(HttpServletResponse.SC_OK); | |||
JSONObject infoobj = new JSONObject(); | |||
infoobj.put("version", Globals.Version); | |||
infoobj.put("dictionary", Globals.DictionaryPath); | |||
infoobj.put("dataset", Globals.Dataset); | |||
infoobj.put("GDB system", Globals.GDBsystem); | |||
//TODO add more info | |||
baseRequest.setHandled(true); | |||
response.getWriter().println(infoobj.toString()); | |||
} | |||
catch(Exception e){ | |||
if(e instanceof IOException){ | |||
try { | |||
baseRequest.setHandled(true); | |||
response.getWriter().println(errorHandle("500","IOException","",null)); | |||
} catch (Exception e1) { | |||
} | |||
} | |||
else if(e instanceof JSONException){ | |||
try { | |||
baseRequest.setHandled(true); | |||
response.getWriter().println(errorHandle("500","JSONException","",null)); | |||
} catch (Exception e1) { | |||
} | |||
} | |||
else if(e instanceof ServletException){ | |||
try { | |||
baseRequest.setHandled(true); | |||
response.getWriter().println(errorHandle("500","ServletException","",null)); | |||
} catch (Exception e1) { | |||
} | |||
} | |||
else { | |||
try { | |||
baseRequest.setHandled(true); | |||
response.getWriter().println(errorHandle("500","Unkown Exception","",null)); | |||
} catch (Exception e1) { | |||
} | |||
} | |||
} | |||
} | |||
} |
@@ -0,0 +1,25 @@ | |||
package application; | |||
import java.io.IOException; | |||
import javax.servlet.ServletException; | |||
import javax.servlet.http.HttpServletRequest; | |||
import javax.servlet.http.HttpServletResponse; | |||
import org.eclipse.jetty.server.Request; | |||
import org.eclipse.jetty.server.handler.AbstractHandler; | |||
public class HelloHandler extends AbstractHandler { | |||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) | |||
throws IOException, ServletException { | |||
response.setContentType("text/html;charset=utf-8"); | |||
response.setStatus(HttpServletResponse.SC_OK); | |||
baseRequest.setHandled(true); | |||
String data = request.getParameter("data"); | |||
response.getWriter().println("<h1>Hello World</h1>"); | |||
response.getWriter().println("Request url: " + target); | |||
response.getWriter().println(data); | |||
} | |||
} |
@@ -0,0 +1,16 @@ | |||
package application; | |||
import org.eclipse.jetty.server.Server; | |||
import org.eclipse.jetty.server.handler.ContextHandler; | |||
public class HelloWorld { | |||
public static void main(String[] args) throws Exception { | |||
Server server = new Server(8888); | |||
ContextHandler context = new ContextHandler(); | |||
context.setContextPath("/hello"); | |||
context.setHandler(new HelloHandler()); | |||
server.setHandler(context); | |||
server.start(); | |||
server.join(); | |||
} | |||
} |
@@ -261,6 +261,7 @@ public class GAnswer { | |||
String[] varLineContents = rawLines[0].split("\t"); | |||
int varNum = varLineContents.length; | |||
ret.answers = new String[ansNum][varNum]; | |||
ret.answersNum = ansNum; | |||
System.out.println("ansNum=" + ansNum); | |||
System.out.println("varNum=" + varNum); | |||
@@ -27,6 +27,10 @@ public class Globals { | |||
// entity linking system | |||
public static DBpediaLookup dblk; | |||
public static int MaxAnswerNum = 100; | |||
public static String Dataset = "dbpedia 2016"; | |||
public static String DictionaryPath = "default"; | |||
public static String Version = "0.1.2"; | |||
public static String GDBsystem = "gStore v0.7.2"; | |||
/* | |||
* evaluationMethod: | |||