Some progress ~

This commit is contained in:
felixm456 2018-01-02 00:55:37 +01:00 committed by Maschell
parent d01f306b1e
commit ede0ef0b5a
4 changed files with 118 additions and 69 deletions

View File

@ -40,4 +40,4 @@
rev:reviewer :JPT ; rev:reviewer :JPT ;
rev:text "Yet another gaming console. I lost a tetris game once. Meh."^^xsd:string . rev:text "Yet another gaming console. I lost a tetris game once. Meh."^^xsd:string .
:Switch :hasPrice "329,00"^^:PriceEur . :Switch :hasPrice "329,00"^^:PriceEur .

View File

@ -4,6 +4,7 @@
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rev: <http://purl.org/stuff/rev#> . @prefix rev: <http://purl.org/stuff/rev#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
# Our own properties # Our own properties
:ceo rdf:type rdf:Property ; :ceo rdf:type rdf:Property ;
@ -24,7 +25,7 @@
:numOfSupportedControllers rdf:type rdf:Property ; :numOfSupportedControllers rdf:type rdf:Property ;
rdfs:domain :GameConsole ; rdfs:domain :GameConsole ;
rdfs:range xsd:string . rdfs:range xsd:int .
:predecessorOfConsole rdf:type rdf:Property ; :predecessorOfConsole rdf:type rdf:Property ;
rdfs:domain :GameConsole ; rdfs:domain :GameConsole ;
@ -48,19 +49,42 @@
:hasPrice rdf:type rdf:Property ; :hasPrice rdf:type rdf:Property ;
rdfs:domain :GameConsole ; rdfs:domain :GameConsole ;
rdfs:range :PriceEur . rdfs:range :xsd:string .
# rdfs:Datatype # rdfs:Datatype
:PriceEur rdf:type rdfs:Datatype . :PriceEur rdf:type rdfs:Datatype .
:PriceEur rdfs:label "Preis in Euro"^^xsd:string . :PriceEur rdfs:label "Preis in Euro"^^xsd:string .
# Anwendungsbeispiel: :Switch :hasPrice "329,00"^^:PriceEur . # Anwendungsbeispiel: :Switch :hasPrice "329,00"^^:PriceEur .
# rdfs:subPropertyOf # rdfs:subPropertyOf
:predecessorOfConsole rdfs:subPropertyOf :relatedToConsole . :predecessorOfConsole rdfs:subPropertyOf :relatedToConsole .
:successorOfConsole rdfs:subPropertyOf :relatedToConsole . :successorOfConsole rdfs:subPropertyOf :relatedToConsole .
# rdfs:subClassOf # rdfs:subClassOf
:PortableGameConsole2 rdf:type rdfs:Class .
:PortableGameConsole rdf:type rdfs:Class . :PortableGameConsole rdf:type rdfs:Class .
:GameConsole rdf:type rdfs:Class . :GameConsole rdf:type rdfs:Class .
:PortableGameConsole rdfs:subClassOf :GameConsole . :PortableGameConsole rdfs:subClassOf :GameConsole .
:HomeConsole owl:equivalentClass [
rdf:type owl:Class ;
owl:intersectionOf (
:GameConsole [owl:complementOf :PortableGameConsole]
)
] .
:NintendoConsole owl:equivalentClass [
rdf:type owl:Restriction ;
owl:onProperty :madeBy ;
owl:someValuesFrom :Nintendo
] .
:PartyConsole rdf:equivalentClass [
rdf:type owl:Restriction ;
owl:minQualifiedCardinality "2"^^xsd:nonNegativeInteger ;
owl:onProperty :numOfSupportedControllers ;
owl:onClass :GameConsole
] .

View File

@ -3,6 +3,7 @@ package hsh.ins_jena;
import java.io.File; import java.io.File;
import java.util.Iterator; import java.util.Iterator;
import org.apache.jena.ontology.OntModelSpec;
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;
@ -10,16 +11,22 @@ import org.apache.jena.query.QueryFactory;
import org.apache.jena.rdf.model.InfModel; 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.rdf.model.Property;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.rdf.model.StmtIterator;
import org.apache.jena.reasoner.Reasoner; import org.apache.jena.reasoner.Reasoner;
import org.apache.jena.reasoner.ReasonerRegistry; import org.apache.jena.reasoner.ReasonerRegistry;
import org.apache.jena.reasoner.ValidityReport; import org.apache.jena.reasoner.ValidityReport;
import org.apache.jena.reasoner.ValidityReport.Report;
import org.apache.jena.util.FileManager; import org.apache.jena.util.FileManager;
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/foobar/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);
@ -27,87 +34,102 @@ public class App {
} }
private static void readAndHandleFiles(String inputPath) { private static void readAndHandleFiles(String inputPath) {
Model tboxModel = FileManager.get().loadModel("file:" + inputPath + "/" + Generator.T_BOX_FILENAME_XML); // Model owlTboxModel = FileManager.get().loadModel("file:" + "data/owl.ttl");
Model aboxModel = FileManager.get().loadModel("file:" + inputPath + "/" + Generator.A_BOX_FILENAME_XML); // Model tboxModel = FileManager.get().loadModel("file:" + inputPath + "/" +
// Generator.T_BOX_FILENAME_XML);
// Model aboxModel = FileManager.get().loadModel("file:" + inputPath + "/" +
// Generator.A_BOX_FILENAME_XML);
Model tboxModel = FileManager.get().loadModel("file:" + "data/tbox.ttl");
Model aboxModel = FileManager.get().loadModel("file:" + "data/abox.ttl");
// Combine both models to an RDFS model // Combine both models to an RDFS model
InfModel rdfsModel = ModelFactory.createRDFSModel(tboxModel, aboxModel); /*
* Model tBoxComplete = ModelFactory.createDefaultModel();
* tBoxComplete.add(owlTboxModel); tBoxComplete.add(aboxModel);
*/
// Do some validity checking // Let's create an owl reasoner
ValidityReport validity = rdfsModel.validate(); Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
reasoner = reasoner.bindSchema(tboxModel);
OntModelSpec ontModelSpec = OntModelSpec.OWL_DL_MEM_RULE_INF;
ontModelSpec.setReasoner(reasoner);
InfModel infModel = ModelFactory.createOntologyModel(ontModelSpec, aboxModel);
ValidityReport validity = infModel.validate();
if (validity.isValid()) { if (validity.isValid()) {
System.out.println("\nRDFSModel Validity Report: OK"); System.out.println("Validation: OK");
} else { } else {
System.out.println("\nRDFSModel Validity Report: Conflicts!"); System.out.println("Conflicts");
for (Iterator<ValidityReport.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);
} }
} }
// Let's create an rdfs reasoner printResource(infModel, "Switch");
Reasoner rdfsReasoner = ReasonerRegistry.getRDFSReasoner(); printResource(infModel, "PartyConsole");
rdfsReasoner = rdfsReasoner.bindSchema(rdfsModel); printResource(infModel, "NintendoConsole");
InfModel infModel = ModelFactory.createInfModel(rdfsReasoner, rdfsModel);
System.err.println("infModel");
System.out.println(infModel);
// Let's define a bunch of queries // Let's define a bunch of queries
Query queryReleaseYear = QueryFactory.create( Query queryReleaseYear = QueryFactory.create("PREFIX : <" + Generator.OWN_URI + ">" + "SELECT ?console\n"
"PREFIX : <" + Generator.OWN_URI + ">"
+ "SELECT ?console\n"
+ "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( Query queryNintendoConsoles = QueryFactory
"PREFIX : <" + Generator.OWN_URI + ">" .create("PREFIX : <" + Generator.OWN_URI + ">" + "PREFIX rdf: <" + Generator.RDF_URI + ">"
+ "SELECT ?console\n" + "SELECT ?console\n" + "WHERE {\n" + " ?console rdf:type :NintendoConsole .\n" + "}");
+ "WHERE {\n"
+ " ?console :madeBy :Nintendo .\n"
+ "}");
Query queryCEOForConsole = QueryFactory.create( Query queryCEOForConsole = QueryFactory.create("PREFIX : <" + Generator.OWN_URI + ">" + "PREFIX rdf: <"
"PREFIX : <" + Generator.OWN_URI + ">" + + Generator.RDF_URI + ">" + "PREFIX rdfs: <" + Generator.RDFS_URI + ">" + "PREFIX foaf: <"
"PREFIX rdf: <" + Generator.RDF_URI + ">" + + Generator.FOAF_URI + ">" + "CONSTRUCT {\n" + " ?ceo :isRelatedToConsole ?console .\n" + "}\n"
"PREFIX rdfs: <" + Generator.RDFS_URI + ">" + + "WHERE {\n" + " ?console rdf:type :GameConsole .\n" + " ?console :madeBy ?org .\n"
"PREFIX foaf: <" + Generator.FOAF_URI + ">" + + " ?org :ceo ?ceo .\n" + " ?ceo rdf:type foaf:Person .\n" + "}\n" + "");
"CONSTRUCT {\n" +
" ?ceo :isRelatedToConsole ?console .\n" +
"}\n" +
"WHERE {\n" +
" ?console rdf:type :GameConsole .\n" +
" ?console :madeBy ?org .\n" +
" ?org :ceo ?ceo .\n" +
" ?ceo rdf:type foaf:Person .\n" +
"}\n" +
"");
Query queryAll = QueryFactory.create("PREFIX : <" + Generator.OWN_URI + ">" + " " + " SELECT ?s ?p ?p WHERE {" + "?s ?p ?o .}"); Query queryAll = QueryFactory
.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 // Let's do more
QueryExecution queryExecLocalReleaseDate = QueryExecutionFactory.create(queryReleaseYear, infModel); QueryExecution queryExecLocalReleaseDate = QueryExecutionFactory.create(queryCEOForConsole, infModel);
QueryExecution queryExecRemoteReleaseDate = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT, queryReleaseYear); QueryExecution queryExecRemoteReleaseDate = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT,
queryReleaseYear);
QueryExecution queryExecLocalCEOtoConsole = QueryExecutionFactory.create(queryCEOForConsole, infModel); QueryExecution queryExecLocalCEOtoConsole = QueryExecutionFactory.create(queryCEOForConsole, infModel);
QueryExecution queryExecRemoteCEOtoConsole = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT, queryCEOForConsole); QueryExecution queryExecRemoteCEOtoConsole = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT,
queryCEOForConsole);
System.err.println("Doing local release date query"); System.out.println(">> Doing local release date query");
printQueryResult(queryExecLocalReleaseDate); printQueryResult(queryExecLocalReleaseDate);
System.err.println("Doing remote release date query"); System.out.println(">> Doing remote release date query");
printQueryResult(queryExecRemoteReleaseDate); printQueryResult(queryExecRemoteReleaseDate);
System.err.println("Doing local CEO query"); System.out.println(">> Doing local CEO query");
printQueryConstructs(queryExecLocalCEOtoConsole); printQueryConstructs(queryExecLocalCEOtoConsole);
System.err.println("Doing remote CEO query"); System.out.println(">> Doing remote CEO query");
printQueryConstructs(queryExecRemoteCEOtoConsole); printQueryConstructs(queryExecRemoteCEOtoConsole);
} }
private static void printResource(Model model, String resource) {
Resource nForce = model.getResource("http://example.com/ins_uebung/#" + resource);
System.out.println(resource + "*:");
printStatements(model, nForce, null, null);
}
public static void printStatements(Model m, Resource s, Property p, Resource o) {
for (StmtIterator i = m.listStatements(s, p, o); i.hasNext();) {
Statement stmt = i.nextStatement();
System.out.println(" - " + PrintUtil.print(stmt));
}
}
private static void printQueryResult(QueryExecution queryExec) { private static void printQueryResult(QueryExecution queryExec) {
queryExec.execSelect().forEachRemaining(qs -> System.out.println(qs)); queryExec.execSelect().forEachRemaining(qs -> System.out.println(qs));
} }

View File

@ -27,11 +27,13 @@ public class Generator {
public static final String XSD_URI = "http://www.w3.org/2001/XMLSchema#"; public static final String XSD_URI = "http://www.w3.org/2001/XMLSchema#";
public static final String FOAF_URI = "http://xmlns.com/foaf/0.1/"; public static final String FOAF_URI = "http://xmlns.com/foaf/0.1/";
public static final String REV_URI = "http://purl.org/stuff/rev#"; public static final String REV_URI = "http://purl.org/stuff/rev#";
public static final String OWL_URI = "http://www.w3.org/2002/07/owl#";
public static final String MODEL_TURTLE = "TURTLE"; public static final String MODEL_TURTLE = "TURTLE";
public static final String MODEL_JSON = "JSON-LD"; public static final String MODEL_JSON = "JSON-LD";
public static final String MODEL_RDF_XML = "RDF/XML"; public static final String MODEL_RDF_XML = "RDF/XML";
public static void createTBoxAndABox() { public static void createTBoxAndABox() {
createTBoxAndABox("."); createTBoxAndABox(".");
} }
@ -197,6 +199,7 @@ public class Generator {
switchReviewByJPT.addProperty(revReviewer, jpt); switchReviewByJPT.addProperty(revReviewer, jpt);
switchReviewByJPT.addProperty(revText, "Yet another gaming console. I lost a tetris game once. Meh."); switchReviewByJPT.addProperty(revText, "Yet another gaming console. I lost a tetris game once. Meh.");
cSwitch.addProperty(revHasReview, switchReviewByJPT); cSwitch.addProperty(revHasReview, switchReviewByJPT);
cSwitch.addProperty(hasPrice, "329,00€"); cSwitch.addProperty(hasPrice, "329,00€");