Use spaces instead of tabs

This commit is contained in:
felixm456 2018-01-02 11:58:08 +01:00 committed by Maschell
parent 8445045290
commit 506abd85c0
1 changed files with 93 additions and 99 deletions

View File

@ -25,123 +25,117 @@ import org.apache.jena.util.PrintUtil;
import hsh.ins_jena.model.Generator; import hsh.ins_jena.model.Generator;
public class App { public class App {
public static String OUTPUT_PATH = "./output"; public static String OUTPUT_PATH = "./output";
public static String SPARQL_ENDPOINT = "http://localhost:3030/test/query"; public static String SPARQL_ENDPOINT = "http://localhost:3030/test/query";
public static void main(String[] args) { public static void main(String[] args) {
generateFiles(OUTPUT_PATH); generateFiles(OUTPUT_PATH);
readAndHandleFiles(OUTPUT_PATH); readAndHandleFiles(OUTPUT_PATH);
} }
private static void readAndHandleFiles(String inputPath) { private static void readAndHandleFiles(String inputPath) {
// Model owlTboxModel = FileManager.get().loadModel("file:" + "data/owl.ttl"); // Model owlTboxModel = FileManager.get().loadModel("file:" + "data/owl.ttl");
// Model tboxModel = FileManager.get().loadModel("file:" + inputPath + "/" + // Model tboxModel = FileManager.get().loadModel("file:" + inputPath + "/" +
// Generator.T_BOX_FILENAME_XML); // Generator.T_BOX_FILENAME_XML);
// Model aboxModel = FileManager.get().loadModel("file:" + inputPath + "/" + // Model aboxModel = FileManager.get().loadModel("file:" + inputPath + "/" +
// Generator.A_BOX_FILENAME_XML); // Generator.A_BOX_FILENAME_XML);
Model tboxModel = FileManager.get().loadModel("file:" + "data/tbox.ttl"); Model tboxModel = FileManager.get().loadModel("file:" + "data/tbox.ttl");
Model aboxModel = FileManager.get().loadModel("file:" + "data/abox.ttl"); Model aboxModel = FileManager.get().loadModel("file:" + "data/abox.ttl");
// Combine both models to an RDFS model // Combine both models to an RDFS model
/* /*
* Model tBoxComplete = ModelFactory.createDefaultModel(); * Model tBoxComplete = ModelFactory.createDefaultModel();
* tBoxComplete.add(owlTboxModel); tBoxComplete.add(aboxModel); * tBoxComplete.add(owlTboxModel); tBoxComplete.add(aboxModel);
*/ */
// Let's create an owl reasoner // Let's create an owl reasoner
Reasoner reasoner = ReasonerRegistry.getOWLReasoner(); Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
reasoner = reasoner.bindSchema(tboxModel); reasoner = reasoner.bindSchema(tboxModel);
OntModelSpec ontModelSpec = OntModelSpec.OWL_DL_MEM_RULE_INF; OntModelSpec ontModelSpec = OntModelSpec.OWL_DL_MEM_RULE_INF;
ontModelSpec.setReasoner(reasoner); ontModelSpec.setReasoner(reasoner);
InfModel infModel = ModelFactory.createOntologyModel(ontModelSpec, aboxModel); InfModel infModel = ModelFactory.createOntologyModel(ontModelSpec, aboxModel);
ValidityReport validity = infModel.validate(); ValidityReport validity = infModel.validate();
if (validity.isValid()) { if (validity.isValid()) {
System.out.println("Validation: OK"); System.out.println("Validation: OK");
} else { } else {
System.out.println("Conflicts"); System.out.println("Conflicts");
for (Iterator<Report> i = validity.getReports(); i.hasNext();) { for (Iterator<Report> i = validity.getReports(); i.hasNext();) {
ValidityReport.Report report = (ValidityReport.Report) i.next(); ValidityReport.Report report = (ValidityReport.Report) i.next();
System.out.println(" - " + report); System.out.println(" - " + report);
} }
} }
printResource(infModel, "Switch"); printResource(infModel, "Switch");
printResource(infModel, "PartyConsole"); printResource(infModel, "PartyConsole");
printResource(infModel, "NintendoConsole"); printResource(infModel, "NintendoConsole");
// Let's define a bunch of queries // Let's define a bunch of queries
Query queryReleaseYear = QueryFactory.create("PREFIX : <" + Generator.OWN_URI + ">" + "SELECT ?console\n" Query queryReleaseYear = QueryFactory.create("PREFIX : <" + Generator.OWN_URI + ">" + "SELECT ?console\n" + "WHERE {\n"
+ "WHERE {\n" + " ?console :madeBy :Nintendo .\n" + " ?console :releaseYear ?releaseYear . \n" + " ?console :madeBy :Nintendo .\n" + " ?console :releaseYear ?releaseYear . \n" + " FILTER(?releaseYear > 2015)\n" + "}");
+ " FILTER(?releaseYear > 2015)\n" + "}");
Query queryNintendoConsoles = QueryFactory Query queryNintendoConsoles = QueryFactory.create("PREFIX : <" + Generator.OWN_URI + ">" + "PREFIX rdf: <" + Generator.RDF_URI + ">"
.create("PREFIX : <" + Generator.OWN_URI + ">" + "PREFIX rdf: <" + Generator.RDF_URI + ">" + "SELECT ?console\n" + "WHERE {\n" + " ?console rdf:type :NintendoConsole .\n" + "}");
+ "SELECT ?console\n" + "WHERE {\n" + " ?console rdf:type :NintendoConsole .\n" + "}");
Query queryCEOForConsole = QueryFactory.create("PREFIX : <" + Generator.OWN_URI + ">" + "PREFIX rdf: <" Query queryCEOForConsole = QueryFactory.create("PREFIX : <" + Generator.OWN_URI + ">" + "PREFIX rdf: <" + Generator.RDF_URI + ">" + "PREFIX rdfs: <"
+ Generator.RDF_URI + ">" + "PREFIX rdfs: <" + Generator.RDFS_URI + ">" + "PREFIX foaf: <" + Generator.RDFS_URI + ">" + "PREFIX foaf: <" + Generator.FOAF_URI + ">" + "CONSTRUCT {\n" + " ?ceo :isRelatedToConsole ?console .\n" + "}\n"
+ Generator.FOAF_URI + ">" + "CONSTRUCT {\n" + " ?ceo :isRelatedToConsole ?console .\n" + "}\n" + "WHERE {\n" + " ?console rdf:type :GameConsole .\n" + " ?console :madeBy ?org .\n" + " ?org :ceo ?ceo .\n"
+ "WHERE {\n" + " ?console rdf:type :GameConsole .\n" + " ?console :madeBy ?org .\n" + " ?ceo rdf:type foaf:Person .\n" + "}\n" + "");
+ " ?org :ceo ?ceo .\n" + " ?ceo rdf:type foaf:Person .\n" + "}\n" + "");
Query queryAll = 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 .}");
// Let's execute one query and print its results // Let's execute one query and print its results
QueryExecution queryExecLocalConsoles = QueryExecutionFactory.create(queryAll, infModel); QueryExecution queryExecLocalConsoles = QueryExecutionFactory.create(queryAll, infModel);
System.err.println("Show consoles from local model"); System.err.println("Show consoles from local model");
// printQueryResult(queryExecLocalConsoles); // printQueryResult(queryExecLocalConsoles);
QueryExecution queryExecNintendoConsoles = QueryExecutionFactory.create(queryNintendoConsoles, infModel);
System.out.println(">> Doing local NintendoConsoles query");
printQueryResult(queryExecNintendoConsoles);
// Let's do more QueryExecution queryExecNintendoConsoles = QueryExecutionFactory.create(queryNintendoConsoles, infModel);
QueryExecution queryExecLocalReleaseDate = QueryExecutionFactory.create(queryCEOForConsole, infModel); System.out.println(">> Doing local NintendoConsoles query");
QueryExecution queryExecRemoteReleaseDate = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT, printQueryResult(queryExecNintendoConsoles);
queryReleaseYear);
QueryExecution queryExecLocalCEOtoConsole = QueryExecutionFactory.create(queryCEOForConsole, infModel); // Let's do more
QueryExecution queryExecRemoteCEOtoConsole = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT, QueryExecution queryExecLocalReleaseDate = QueryExecutionFactory.create(queryCEOForConsole, infModel);
queryCEOForConsole); QueryExecution queryExecRemoteReleaseDate = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT, queryReleaseYear);
System.out.println(">> Doing local release date query"); QueryExecution queryExecLocalCEOtoConsole = QueryExecutionFactory.create(queryCEOForConsole, infModel);
printQueryResult(queryExecLocalReleaseDate); QueryExecution queryExecRemoteCEOtoConsole = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT, queryCEOForConsole);
System.out.println(">> Doing remote release date query");
printQueryResult(queryExecRemoteReleaseDate);
System.out.println(">> Doing local CEO query"); System.out.println(">> Doing local release date query");
printQueryConstructs(queryExecLocalCEOtoConsole); printQueryResult(queryExecLocalReleaseDate);
System.out.println(">> Doing remote CEO query"); System.out.println(">> Doing remote release date query");
printQueryConstructs(queryExecRemoteCEOtoConsole); printQueryResult(queryExecRemoteReleaseDate);
}
private static void printResource(Model model, String resource) { System.out.println(">> Doing local CEO query");
Resource nForce = model.getResource("http://example.com/ins_uebung/#" + resource); printQueryConstructs(queryExecLocalCEOtoConsole);
System.out.println(resource + "*:"); System.out.println(">> Doing remote CEO query");
printStatements(model, nForce, null, null); printQueryConstructs(queryExecRemoteCEOtoConsole);
} }
public static void printStatements(Model m, Resource s, Property p, Resource o) { private static void printResource(Model model, String resource) {
for (StmtIterator i = m.listStatements(s, p, o); i.hasNext();) { Resource nForce = model.getResource("http://example.com/ins_uebung/#" + resource);
Statement stmt = i.nextStatement(); System.out.println(resource + "*:");
System.out.println(" - " + PrintUtil.print(stmt)); printStatements(model, nForce, null, null);
} }
}
private static void printQueryResult(QueryExecution queryExec) { public static void printStatements(Model m, Resource s, Property p, Resource o) {
queryExec.execSelect().forEachRemaining(qs -> System.out.println(qs)); for (StmtIterator i = m.listStatements(s, p, o); i.hasNext();) {
} Statement stmt = i.nextStatement();
System.out.println(" - " + PrintUtil.print(stmt));
}
}
private static void printQueryConstructs(QueryExecution queryExec) { private static void printQueryResult(QueryExecution queryExec) {
queryExec.execConstruct().listStatements().toList().stream().forEach(s -> System.out.println(s)); queryExec.execSelect().forEachRemaining(qs -> System.out.println(qs));
} }
private static void generateFiles(String outputhPath) { private static void printQueryConstructs(QueryExecution queryExec) {
new File(OUTPUT_PATH).mkdirs(); queryExec.execConstruct().listStatements().toList().stream().forEach(s -> System.out.println(s));
System.out.println("Generating model and saving it..."); }
Generator.createTBoxAndABox(OUTPUT_PATH);
System.out.println("done."); private static void generateFiles(String outputhPath) {
} new File(OUTPUT_PATH).mkdirs();
System.out.println("Generating model and saving it...");
Generator.createTBoxAndABox(OUTPUT_PATH);
System.out.println("done.");
}
} }