I am attempting to load a Turtle file into Android studio and use the Androjena library to run queries against the Turtle file. I am able to do this in Eclipse with JavaFX with no issue. However, in the Intellij IDE I am getting a FATAL error which obviously crashes my app. I have a method called runQuery() that is called in order to run a query on the File:
public String runQuery(){
String stringQuery = "PREFIX foaf: <http://xmlns.com/foaf/0.1/> \n" +
"PREFIX dbo: <http://dbpedia.org/ontology/> \n" +
"SELECT ?birthDate WHERE { \n" +
"?barack foaf:name \"Barack Obama\"@en .\n" +
"?barack dbo:birthDate ?birthDate \n" +
"}";
String answer = "";
Model model = FileManager.get().loadModel("sample_pres.ttl", "TTL");
Query query = QueryFactory.create(stringQuery);
try {
QueryExecution qexec = QueryExecutionFactory.create(query, model);
ResultSet results = qexec.execSelect();
while(results.hasNext()) {
QuerySolution soln = results.nextSolution();
Literal answerLiteral = soln.getLiteral("birthDate");
answer = answerLiteral.toString();
}
}
catch(Exception ignore) {
}
this.answer = answer;
return answer;
}
The line of code that is giving me issues is the FileManager.get().loadModel() line. Here is the Exception that I am getting:
com.hp.hpl.jena.shared.NotFoundException: Not found: sample_pres.ttl
So I am gathering that Android is not finding the file, although the file is in my Assets folder. I am assuming that I don't/cannot use the AssetManager since I am not attempting to include a FileInputStream. So I'm fairly stuck at this point. Here is a picture of my project structure:
I added the assets folder under app/src/main in my project structure. I am relatively new to Android Studio and I know that in JavaFX from eclipse I could simply use the absolute path for the File in order to access it, and I know that this obviously will not work in Android Studio. I couldn't find one example however of loading a Turtle file from a local source in the Android project (my assets folder) and executing a query. Every example or question on this site appears to be in regards to running a query from an external endpoint over an internet connection. So This is partially why I am confused. I am not sure how to run the query from a Local source in Android studio and reference the Turtle file from my assets folder in order to avoid the com.hp.hpl.jena.shared.NotFoundException
I found the answer. The issue is that asset files in android studio cannot be read in android studio. They have to be converted to a FileOutputStream, even when working with .ttl files and reading them in turtle. Here is an example of the code:
String filePath = context.getFilesDir() + File.separator + "my_turtle.ttl";
File destinationFile = new File(filePath);
FileOutputStream outputStream = new FileOutputStream(destinationFile);
AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open("sample_3.ttl");
byte[] buffer = new byte[1024];
int length = 0;
while((length = inputStream.read(buffer)) != -1){
outputStream.write(buffer,0,length);
}
inputStream.close();
outputStream.close();
Model model = null;
model = FileManager.get().loadModel(filePath,"TTL");
Query query = QueryFactory.create(stringQuery);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
ResultSet results = qexec.execSelect();
while(results.hasNext()) {
QuerySolution soln = results.nextSolution();
Literal answerLiteral = soln.getLiteral("abstract");
answer = answerLiteral.toString();
System.out.println(answer);
}
if(!answer.equals("")){
this.answer = answer;
return answer;
}
else{
return "I could not find an answer";
}