Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use the RDF from the OpenTox Task API
  • Loading branch information
egonw committed Nov 6, 2011
1 parent ffcaf04 commit dc01fea
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 10 deletions.
Expand Up @@ -20,10 +20,14 @@
package net.bioclipse.opentox.api;

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.util.HashMap;

import net.bioclipse.core.domain.StringMatrix;
import net.bioclipse.opentox.Activator;
import net.bioclipse.rdf.business.IRDFStore;
import net.bioclipse.rdf.business.RDFManager;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.DeleteMethod;
Expand All @@ -33,6 +37,17 @@
public class Task {

private static final Logger logger = Logger.getLogger(Task.class);

private static RDFManager rdf = new RDFManager();

private final static String QUERY_TASK_DETAILS =
"PREFIX ot: <http://www.opentox.org/api/1.1#>" +
"" +
"SELECT * WHERE {" +
" ?task ot:hasStatus ?status ." +
" OPTIONAL { ?task ot:percentageCompleted ?completed }" +
" OPTIONAL { ?task ot:resultURI ?result }" +
"}";

public static void delete(String task) throws IOException, GeneralSecurityException {
HttpClient client = new HttpClient();
Expand Down Expand Up @@ -61,39 +76,42 @@ public static TaskState getState(String task)
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(task);
HttpMethodHelper.addMethodHeaders(method,
new HashMap<String,String>() {{ put("Accept", "text/uri-list"); }}
new HashMap<String,String>() {{ put("Accept", "application/rdf+xml"); }}
);
method.getParams().setParameter("http.socket.timeout", new Integer(Activator.TIME_OUT));
method.setRequestHeader("Accept", "text/uri-list");
method.setRequestHeader("Accept", "application/rdf+xml");
client.executeMethod(method);
int status = method.getStatusCode();
logger.debug("Task status: " + status);

TaskState state = new TaskState();
logger.debug("Task: " + task);
logger.debug(" -> " + status);
InputStream result = method.getResponseBodyAsStream();
// logger.debug("RDF: " + result);
switch (status) {
case 404:
logger.error("Task gone missing (404): " + task);
state.setExists(false);
break;
case 200:
String result = method.getResponseBodyAsString();
if (result == null || result.length() == 0)
if (result == null)
throw new IOException("Missing dataset URI for finished (200) Task.");
state.setFinished(true);
state.setResults(result);
state.setResults(getResultSetURI(createStore(result)));
break;
case 201:
state.setFinished(true);
state.setRedirected(true);
state.setResults(method.getResponseBodyAsString());
state.setResults(getResultSetURI(createStore(result)));
break;
case 202:
state.setFinished(false);
state.setPercentageCompleted(getPercentageCompleted(createStore(result)));
break;
default:
logger.error("Task error (" + status + "): " + task);
logger.debug("Response: " + result);
throw new IllegalStateException(
"Service error: " + status + ":\n " +
method.getStatusText()
Expand All @@ -103,6 +121,58 @@ public static TaskState getState(String task)
method.releaseConnection();
return state;
}

private static float getPercentageCompleted(IRDFStore store) {
try {
StringMatrix matrix = rdf.sparql(store, QUERY_TASK_DETAILS);
if (matrix != null && matrix.getRowCount() != 0 &&
matrix.hasColumn("completed")) {
String floatStr = matrix.get(0, "completed");
if (floatStr.contains("^^"))
floatStr = floatStr.substring(0, floatStr.indexOf("^^"));
logger.debug("Found completed: " + floatStr);
return Float.parseFloat(floatStr);
} else {
return 0.0f;
}
} catch (Exception e) {
logger.debug("Error while getting the percentage: " + e.getMessage());
}
return 0.0f;
}

private static IRDFStore createStore(InputStream rdfResults) {
IRDFStore store = rdf.createInMemoryStore();
try {
return rdf.importFromStream(store, rdfResults, "RDF/XML", null);
} catch (Exception e) {
logger.debug("Error while creating RDF from String: " + e.getMessage());
logger.debug(e);
}
throw new IllegalStateException(
"Service error: unexpected RDF content:\n" + rdfResults
);
}

private static String getResultSetURI(IRDFStore store) {
try {
StringMatrix matrix = rdf.sparql(store, QUERY_TASK_DETAILS);
logger.debug("SPARQL results (URI): " + matrix);
if (matrix != null && matrix.getRowCount() != 0 &&
matrix.hasColumn("result")) {
String uri = matrix.get(1, "result");
if (uri.contains("^^")) uri = uri.substring(0, uri.indexOf("^^"));
logger.debug("Found uri: " + uri);
return uri;
}
} catch (Exception e) {
logger.debug("Error while getting the result set URI: " + e.getMessage());
logger.debug(e);
}
throw new IllegalStateException(
"Service error: missing result URI"
);
}

public static void main(String[] args) throws Exception {
String task = "http://apps.ideaconsult.net:8080/ambit2/task/1";
Expand Down
Expand Up @@ -21,16 +21,36 @@

public class TaskState {

/** The status of the task. */
enum STATUS {
CANCELLED,
COMPLETED,
RUNNING,
ERROR,
UNKNOWN
}

private STATUS status = STATUS.UNKNOWN;
private boolean exists = false;
private boolean isFinished = false;
private boolean isRedirected = false;
private String results = null;

private float percentageCompleted = 0.0f;

public boolean isFinished() {
return isFinished;
return status == STATUS.ERROR || status == STATUS.COMPLETED ;
}
public void setStatus(STATUS status) {
this.status = status;
}
public STATUS getStatus() {
return this.status;
}
public void setFinished(boolean isFinished) {
this.isFinished = isFinished;
if (isFinished) {
this.status = STATUS.COMPLETED;
} else {
this.status = STATUS.RUNNING;
}
}
public boolean isRedirected() {
return isRedirected;
Expand All @@ -50,5 +70,11 @@ public void setExists(boolean exists) {
public boolean exists() {
return exists;
}
public void setPercentageCompleted(float percentageCompleted) {
this.percentageCompleted = percentageCompleted;
}
public float getPercentageCompleted() {
return percentageCompleted;
}

}

0 comments on commit dc01fea

Please sign in to comment.