Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ExFaKT/Client/src/main/java/client/explain/ExplanationExtractorClient.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
89 lines (61 sloc)
2.67 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package client.explain; | |
import com.google.gson.Gson; | |
import com.google.inject.Singleton; | |
import extendedsldnf.datastructure.IQueryExplanations; | |
import extendedsldnf.datastructure.QueryExplanations; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.entity.ContentType; | |
import org.apache.http.entity.StringEntity; | |
import org.apache.http.impl.client.HttpClients; | |
import utils.json.CustomGson; | |
import web.data.Query; | |
import java.io.*; | |
@Singleton | |
public class ExplanationExtractorClient { | |
// static String URL; | |
// final static String URL = "http://localhost:9350"; | |
String url; | |
public static ExplanationExtractorClient instance; //= new ExplanationExtractorClient(); | |
// static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); | |
private ExplanationExtractorClient() { | |
this.url= "http://sedna.mpi-inf.mpg.de:9350"; | |
} | |
private ExplanationExtractorClient(String url){ | |
this.url=url; | |
} | |
public IQueryExplanations getExplanations(Query query) throws IOException { | |
Gson gson = CustomGson.getInstance().getGson(); | |
String queryJson = gson.toJson(query); | |
// | |
QueryExplanations queryExplanations = null; | |
HttpPost p = new HttpPost(url); | |
p.setEntity(new StringEntity(queryJson, | |
ContentType.create("application/json"))); | |
HttpClient c = HttpClients.createDefault(); | |
// System.out.println(p.getEntity().getContent().toString()); | |
HttpResponse response = c.execute(p); | |
if (response.getStatusLine().getStatusCode() == 200) { | |
HttpEntity entity = response.getEntity(); | |
if (entity != null) { | |
InputStream instream = entity.getContent(); | |
BufferedReader br = new BufferedReader(new InputStreamReader(instream)); | |
queryExplanations = gson.fromJson(br, QueryExplanations.class); | |
// System.out.println(queryExplanations); | |
} | |
} | |
return queryExplanations; | |
} | |
public static ExplanationExtractorClient getInstance (String url) { | |
if (instance==null) | |
instance=new ExplanationExtractorClient(url); | |
return instance; | |
// return instance; | |
} | |
public static void main (String[]args) throws IOException { | |
Query exampleQ = new Query("<Albert_Einstein>", "<wasBornIn>", "<Germany>", "wasBornIn(?x,?y):- birthPlace(?x,?z), in(?z,?y).\nwasBornIn(?x,?y):- birthPlace(?x,?z), city_in(?z,?y)."); | |
ExplanationExtractorClient.getInstance("localhost:9400").getExplanations(exampleQ); | |
} | |
} |