Semweb4j/rdfreactor/step0

From semanticweb.org
Jump to: navigation, search

Up | Previous lessons | Next step

Contents

[edit] Step 0: From RDFS to Java code

To use RDFReactor, you need an ontology, like

wine.rdfs
wine.owl

For each object in the ontology, RDFReactor will generate a class with links between objects realised as Java properties.

[edit] generating from RDFS

String rdfsFile = "./src/main/java/org/ontoware/semweb4j/lessons/lesson5/peopletag.rdfs.n3";
String outDir = "./src/main/java/";
String targetPackage = "org.ontoware.semweb4j.lessons.lesson5.gen";
String semantics = CodeGenerator.SEMANTICS_RDFS;

You will be able to use in future versions:

CodeGenerator.SEMANTICS_OWL;
CodeGenerator.SEMANTICS_RDFS_AND_OWL;

But semantics other than RDFS is currently not supported.

boolean skipBuiltins = true;
boolean alwaysWriteToModel = true;
String prefix = "";
CodeGenerator.generate(rdfsFile, outDir, targetPackage, semantics, skipBuiltins, alwaysWriteToModel, prefix);

outDir is where the files are put, targetPackage is written in the header of each file. If skipBuiltins is set to false, internal helper classes are re-built even if it's not necessary. Normally, you don't need this. The prefix String can be set to e.g. "gen_" to let all generated classes have that prefix.

The classes generated by running this code are ready-to-use. Remember: don't generate always before using the classes - if you didn't change the ontology, there is no need to rebuild the code!

[edit] ontology used in example

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl:  <http://www.w3.org/2002/07/owl#> .
@prefix xsd:  <http://www.w3.org/2001/XMLSchema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/#> .
@prefix : <http://example.com/semweb4j#> .

# classes

:Person a rdfs:Class
; rdfs:label "Person"
; rdfs:comment "the 'atoms' of PeopleTag, people"
; owl:sameAs foaf:Person
.

:Tag a rdfs:Class
; rdfs:label "Tag"
; rdfs:comment "the tags which are assigned to people"
.

# properties of objects

:name a rdf:Property
; rdfs:label "has name"
; rdfs:comment "a name of a person or tag"
; rdfs:subPropertyOf rdfs:label
; rdfs:domain :Person
; rdfs:domain :Tag
; rdfs:range rdfs:Literal
.

:tag a rdf:Property
; rdfs:label "has tag"
; rdfs:comment "a tag which has been assigned"
; rdfs:subPropertyOf rdfs:label
; rdfs:domain :Person
; rdfs:range :Tag
.

# relations between objects

:knows a rdf:Property
; rdfs:label "knows"
; rdfs:comment "a known person"
; rdfs:domain :Person
; rdfs:range :Person
; owl:sameAs foaf:knows
.

This is N3 notation. The OWL snippets aren't problematic for RDFReactor - with SEMANTICS.RDFS, they are just ignored.

[edit] output

JModel
  JPackage org.ontoware.semweb4j.lessons.lesson5.gen
    JClass Tag -> http://example.com/semweb4j#Tag
      extends: Thing, 
      comment: the tags which are assigned to people
      Name (-1/-1)  -> http://example.com/semweb4j#name, types: 'java.lang.String',. Inverse: Name.
    JClass Person -> http://example.com/semweb4j#Person
      extends: Thing, 
      comment: the 'atoms' of PeopleTag, people
      Name (-1/-1)  -> http://example.com/semweb4j#name, types: 'java.lang.String',. Inverse: Name.
      Tag (-1/-1)  -> http://example.com/semweb4j#tag, types: 'Tag',. Inverse: Tag.
      Knows (-1/-1)  -> http://example.com/semweb4j#knows, types: 'Person',. Inverse: Knows.
    JClass Thing -> http://www.w3.org/2000/01/rdf-schema#Class
      extends: org.ontoware.rdfreactor.schema.rdfschema.Class, 
      comment: This class acts as a catch-all for all properties, for which no domain has specified.

>>>>>>  written to /home/konrad/workspace/semweb4j/tutorial/./src/main/java

The generated code is discussed in the next step.

Personal tools
Namespaces

Variants
Actions
Navigation
services
Toolbox