package edu.neu.ccs.task.rdf;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import javax.xml.stream.XMLStreamException;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.shared.NoWriterForLangException;
import edu.neu.ccs.task.agent.AgentConsole;
public class AgentConsoleWithRdf extends AgentConsole {
public AgentConsoleWithRdf(String logfile)
throws IOException, InstantiationException, IllegalAccessException,
ClassNotFoundException {
this(logfile, new TaskModelSetWithRdf());
}
public AgentConsoleWithRdf(
String logfile,
TaskModelSetWithRdf modelset) throws IOException {
super(logfile, new TaskEngineWithRdf(modelset));
}
@Override
protected TaskModelSetWithRdf getModelSet() {
return (TaskModelSetWithRdf) super.getModelSet();
}
@Override
protected TaskEngineWithRdf getEngine() {
return (TaskEngineWithRdf) super.getEngine();
}
protected Model getRdfModel() {
return getEngine().getRdfModel();
}
@Override
protected void printBanner() {
super.printBanner();
out.println("DTask metadata extensions");
out.println("Version 0.4 internal");
out.println("-----------------------");
}
public void _loadrdf(String s) throws IOException {
String language = null;
String location = null;
String[] parts = s.split("\\s+", 2);
if (parts.length == 2) {
language = parts[0].trim();
location = parts[1].trim();
} else
location = s.trim();
getModelSet().loadRdf(location, language);
getEngine().updateRdfPrefixMappings();
}
public void _dumprdf(String s) throws IOException {
Model rdf = getEngine().getRuntimeRdfModel();
String language = "N3-PP";
String outputFile = null;
String[] parts = s.split("\\s+", 3);
for (String part : parts) {
part = part.trim();
if (part.equalsIgnoreCase("STATIC"))
rdf = getEngine().getStaticRdfModel();
else if (part.equalsIgnoreCase("RUNTIME"))
rdf = getEngine().getRuntimeRdfModel();
else if (part.equalsIgnoreCase("ALL"))
rdf = getEngine().getRdfModel();
else if ( ! "".equalsIgnoreCase(part)) {
try {
if (rdf.getWriter(part) != null)
language = part;
else
outputFile = part;
} catch (NoWriterForLangException e) {
outputFile = part;
}
}
}
if (outputFile == null)
rdf.write(out, language);
else {
OutputStream out = new FileOutputStream(outputFile);
try {
rdf.write(out, language);
} finally {
out.close();
}
}
}
public void _rdfgraph(String s) throws IOException, XMLStreamException {
Model rdf = getEngine().getRuntimeRdfModel();
String outputFile = null;
String[] parts = s.split("\\s+", 2);
for (String part : parts) {
part = part.trim();
if (part.equalsIgnoreCase("STATIC"))
rdf = getEngine().getStaticRdfModel();
else if (part.equalsIgnoreCase("RUNTIME"))
rdf = getEngine().getRuntimeRdfModel();
else if (part.equalsIgnoreCase("ALL"))
rdf = getEngine().getRdfModel();
else if ( ! "".equalsIgnoreCase(part))
outputFile = part;
}
if (outputFile == null)
GraphML.write(out, rdf);
else {
Writer out = new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8");
try {
GraphML.write(out, rdf);
out.flush();
} finally {
out.close();
}
}
}
public void _sparql(String queryStr) {
Query query = QueryFactory.create(queryStr);
QueryExecution q = QueryExecutionFactory.create(query, getRdfModel());
switch (query.getQueryType()) {
case Query.QueryTypeSelect:
ResultSetFormatter.out(q.execSelect());
break;
case Query.QueryTypeAsk:
ResultSetFormatter.out(q.execAsk());
out.println();
break;
case Query.QueryTypeConstruct:
q.execConstruct().write(out, "N3-PP");
break;
case Query.QueryTypeDescribe:
q.execDescribe().write(out, "N3-PP");
break;
default:
out.println("Unknown query type");
}
q.close();
}
public void _filter(String filter) {
if ("".equals(filter.trim()))
out.println(getModelSet().getFilterQuery());
else
getModelSet().setFilterQuery(filter.trim());
}
// TODO this is cut-and-pasted from AgentMain -- some refactoring needed
public static void main(String[] args) throws Exception {
String logFile = "dtask.log";
String srcFile = null;
for (int i=0; i<args.length; i++) {
if ("-log".equals(args[i]))
logFile = args[++i];
else {
srcFile = args[i];
break;
}
}
AgentConsole console = new AgentConsoleWithRdf(logFile);
if (srcFile != null)
console._source(srcFile);
console.run();
}
}