Try to add a reasoner. Not properly working now.

This commit is contained in:
Jan Philipp Timme 2017-12-27 17:30:45 +01:00
parent 08a8c05b2d
commit 6d245000fd

View File

@ -1,16 +1,19 @@
package hsh.ins_jena; package hsh.ins_jena;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.util.Iterator;
import java.io.FileNotFoundException;
import java.io.InputStream;
import org.apache.jena.query.Query; import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution; import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory; import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory; import org.apache.jena.query.QueryFactory;
import org.apache.jena.rdf.model.InfModel;
import org.apache.jena.rdf.model.Model; import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory; import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.reasoner.Reasoner;
import org.apache.jena.reasoner.ReasonerRegistry;
import org.apache.jena.reasoner.ValidityReport;
import org.apache.jena.util.FileManager;
import hsh.ins_jena.model.Generator; import hsh.ins_jena.model.Generator;
@ -24,20 +27,31 @@ public class App {
} }
private static void readAndHandleFiles(String inputPath) { private static void readAndHandleFiles(String inputPath) {
Model model = ModelFactory.createDefaultModel(); Model tboxModel = FileManager.get().loadModel("file:" + inputPath + "/" + Generator.T_BOX_FILENAME_XML);
Model aboxModel = FileManager.get().loadModel("file:" + inputPath + "/" + Generator.T_BOX_FILENAME_XML);
InputStream inT, inA; // Combine both models to an RDFS model
try { InfModel rdfsModel = ModelFactory.createRDFSModel(tboxModel, aboxModel);
inT = new FileInputStream(new File(inputPath + "/" + Generator.T_BOX_FILENAME_XML));
inA = new FileInputStream(new File(inputPath + "/" + Generator.A_BOX_FILENAME_XML)); // Do some validity checking
model.read(inT, ""); ValidityReport validity = rdfsModel.validate();
model.read(inA, ""); if (validity.isValid()) {
} catch (FileNotFoundException e) { System.out.println("\nValidity Report: OK");
e.printStackTrace(); } else {
System.out.println("\nValidity Report: Conflicts!");
for (Iterator<ValidityReport.Report> i = validity.getReports(); i.hasNext(); ) {
ValidityReport.Report report = (ValidityReport.Report)i.next();
System.out.println(" - " + report);
}
} }
System.err.println("Loaded model:"); // Let's create an rdfs reasoner
System.out.println(model); Reasoner rdfsReasoner = ReasonerRegistry.getRDFSReasoner();
rdfsReasoner = rdfsReasoner.bindSchema(tboxModel);
InfModel infModel = ModelFactory.createInfModel(rdfsReasoner, rdfsModel);
System.err.println("infModel");
System.out.println(infModel);
Query queryReleaseYear = QueryFactory.create( Query queryReleaseYear = QueryFactory.create(
"PREFIX : <" + Generator.OWN_URI + ">" "PREFIX : <" + Generator.OWN_URI + ">"
@ -45,6 +59,14 @@ public class App {
+ "WHERE {\n" + " ?console :madeBy :Nintendo .\n" + " ?console :releaseYear ?releaseYear . \n" + "WHERE {\n" + " ?console :madeBy :Nintendo .\n" + " ?console :releaseYear ?releaseYear . \n"
+ " FILTER(?releaseYear > 2015)\n" + "}"); + " FILTER(?releaseYear > 2015)\n" + "}");
Query queryConsoles = QueryFactory.create(
"PREFIX : <" + Generator.OWN_URI + ">"
+ "SELECT ?console\n"
+ "WHERE {\n"
+ " ?console :madeBy :Nintendo .\n"
+ "}");
Query queryCEOForConsole = QueryFactory.create( Query queryCEOForConsole = QueryFactory.create(
"PREFIX : <" + Generator.OWN_URI + ">" + "PREFIX : <" + Generator.OWN_URI + ">" +
"PREFIX rdf: <" + Generator.RDF_URI + ">" + "PREFIX rdf: <" + Generator.RDF_URI + ">" +
@ -61,14 +83,18 @@ public class App {
"}\n" + "}\n" +
""); "");
// Query query = QueryFactory //Query queryAll = QueryFactory.create("PREFIX : <" + Generator.OWN_URI + ">" + " " + " SELECT ?s ?p ?p WHERE {" + "?s ?p ?o .}");
// .create("PREFIX : <" + Generator.OWN_URI + ">" + " " + " SELECT ?s ?p ?p WHERE {" + "?s ?p ?o .}");
QueryExecution queryExecLocalReleaseDate = QueryExecutionFactory.create(queryReleaseYear, model); QueryExecution queryExecLocalConsoles = QueryExecutionFactory.create(queryConsoles, infModel);
QueryExecution queryExecRemoteReleaseDate = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT,queryReleaseYear); System.err.println("Show consoles from local model");
printQueryResult(queryExecLocalConsoles);
QueryExecution queryExecLocalCEOtoConsole = QueryExecutionFactory.create(queryCEOForConsole, model);
QueryExecution queryExecRemoteCEOtoConsole = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT,queryCEOForConsole); QueryExecution queryExecLocalReleaseDate = QueryExecutionFactory.create(queryReleaseYear, infModel);
QueryExecution queryExecRemoteReleaseDate = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT, queryReleaseYear);
QueryExecution queryExecLocalCEOtoConsole = QueryExecutionFactory.create(queryCEOForConsole, infModel);
QueryExecution queryExecRemoteCEOtoConsole = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT, queryCEOForConsole);
System.err.println("Doing local release date query"); System.err.println("Doing local release date query");
printQueryResult(queryExecLocalReleaseDate); printQueryResult(queryExecLocalReleaseDate);