From 730b45057d39bdfda680c618b962accb27fe481a Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Mon, 20 Nov 2017 22:08:03 +0100 Subject: [PATCH] Begin creating TBox programmatically --- .gitignore | 1 + data/tbox.ttl | 7 ++ src/main/java/hsh/examplecode/SampleCode.java | 41 ++++++++++++ src/main/java/hsh/ins_jena/App.java | 33 +--------- .../java/hsh/ins_jena/model/Generator.java | 64 +++++++++++++++++++ 5 files changed, 116 insertions(+), 30 deletions(-) create mode 100644 src/main/java/hsh/examplecode/SampleCode.java create mode 100644 src/main/java/hsh/ins_jena/model/Generator.java diff --git a/.gitignore b/.gitignore index c9ca779..6a08c6d 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ target/surefire-reports/ # Own files test.ttl +output/* diff --git a/data/tbox.ttl b/data/tbox.ttl index e6fd303..1a5e86a 100644 --- a/data/tbox.ttl +++ b/data/tbox.ttl @@ -5,6 +5,13 @@ @prefix foaf: . @prefix rev: . +# Our classes +:GameConsole rdf:type rdf:Class . + +:PortableGameConsole rdf:type rdf:Class ; + rdfs:subclassOf :GameConsole . + + # Our own properties :ceo rdf:type rdf:Property ; rdfs:domain foaf:Organization ; diff --git a/src/main/java/hsh/examplecode/SampleCode.java b/src/main/java/hsh/examplecode/SampleCode.java new file mode 100644 index 0000000..3ab4839 --- /dev/null +++ b/src/main/java/hsh/examplecode/SampleCode.java @@ -0,0 +1,41 @@ +package hsh.examplecode; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; + +import org.apache.jena.rdf.model.Model; +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; + +/** + * Hello world! + * + */ +public class SampleCode { + public static void main(String[] args) { + System.out.println("Hello World!"); + String familyUri = "http://family/"; + String relationshipUri = "http://purl.org/vocab/relationship/"; + + Model model = ModelFactory.createDefaultModel(); + Resource donald = model.createResource(familyUri + "donald"); + Resource tick = model.createResource(familyUri + "tick"); + + Property childOf = model.createProperty(relationshipUri, "childOf"); + Property knows = model.createProperty(relationshipUri, "knows"); + tick.addProperty(childOf, donald); + + Statement st1 = model.createStatement(donald, knows, tick); + model.add(st1); + FileOutputStream outfile; + try { + outfile = new FileOutputStream("./test.ttl"); + model.write(outfile, "TURTLE"); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/src/main/java/hsh/ins_jena/App.java b/src/main/java/hsh/ins_jena/App.java index 035727e..e9beb95 100644 --- a/src/main/java/hsh/ins_jena/App.java +++ b/src/main/java/hsh/ins_jena/App.java @@ -1,13 +1,6 @@ package hsh.ins_jena; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; - -import org.apache.jena.rdf.model.Model; -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 hsh.ins_jena.model.Generator; /** * Hello world! @@ -15,27 +8,7 @@ import org.apache.jena.rdf.model.Statement; */ public class App { public static void main(String[] args) { - System.out.println("Hello World!"); - String familyUri = "http://family/"; - String relationshipUri = "http://purl.org/vocab/relationship/"; - - Model model = ModelFactory.createDefaultModel(); - Resource donald = model.createResource(familyUri + "donald"); - Resource tick = model.createResource(familyUri + "tick"); - - Property childOf = model.createProperty(relationshipUri, "childOf"); - Property knows = model.createProperty(relationshipUri, "knows"); - tick.addProperty(childOf, donald); - - Statement st1 = model.createStatement(donald, knows, tick); - model.add(st1); - FileOutputStream outfile; - try { - outfile = new FileOutputStream("./test.ttl"); - model.write(outfile, "TURTLE"); - } catch (FileNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + System.out.println("Generating TBox ..."); + Generator.createTBox(); } } diff --git a/src/main/java/hsh/ins_jena/model/Generator.java b/src/main/java/hsh/ins_jena/model/Generator.java new file mode 100644 index 0000000..5f6b848 --- /dev/null +++ b/src/main/java/hsh/ins_jena/model/Generator.java @@ -0,0 +1,64 @@ +package hsh.ins_jena.model; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; + +import org.apache.jena.rdf.model.Model; +import org.apache.jena.rdf.model.ModelFactory; +import org.apache.jena.rdf.model.Property; +import org.apache.jena.rdf.model.Resource; + +public class Generator { + + // Our own namespace uri + private static String ownUri = "http://example.com/ins_uebung/#"; + + // Uris for other vocabularies we use + private static String rdfUri = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; + private static String rdfsUri = "http://www.w3.org/2000/01/rdf-schema#"; + private static String xsdUri = "http://www.w3.org/2001/XMLSchema#"; + private static String foafUri = "http://xmlns.com/foaf/0.1/"; + private static String revUri = "http://purl.org/stuff/rev#"; + + public static void createTBox() { + Model model = ModelFactory.createDefaultModel(); + + // Some boilerplate stuff + Property rdfType = model.createProperty(rdfUri+"type"); + Resource rdfClass = model.createResource(rdfUri+"Class"); + + Property rdfsSubclassOf = model.createProperty(rdfsUri+"subclassOf"); + + Resource gameConsole = model.createResource(ownUri+"GameConsole"); + Resource portableGameConsole = model.createResource(ownUri+"PortableGameConsole"); + + model.add(model.createStatement(gameConsole, rdfType, rdfClass)); + model.add(model.createStatement(portableGameConsole, rdfType, rdfClass)); + model.add(model.createStatement(portableGameConsole, rdfsSubclassOf, gameConsole)); + + /* + Resource donald = model.createResource(familyUri + "donald"); + Resource tick = model.createResource(familyUri + "tick"); + + Property childOf = model.createProperty(relationshipUri, "childOf"); + Property knows = model.createProperty(relationshipUri, "knows"); + tick.addProperty(childOf, donald); + + Statement st1 = model.createStatement(donald, knows, tick); + model.add(st1); + */ + + Generator.writeModelToFile(model, "./tbox.ttl"); + } + + private static void writeModelToFile(Model model, String path) { + FileOutputStream outfile = null; + try { + outfile = new FileOutputStream(path); + model.write(outfile, "TURTLE"); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + +}