| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| |
| |
| <title>Apache Jena - Jena schemagen HOWTO</title> |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| |
| <link href="/css/bootstrap.min.css" rel="stylesheet" media="screen"> |
| <link href="/css/bootstrap-extension.css" rel="stylesheet" type="text/css"> |
| <link href="/css/jena.css" rel="stylesheet" type="text/css"> |
| <link rel="shortcut icon" href="/images/favicon.ico" /> |
| |
| <script src="https://code.jquery.com/jquery-2.2.4.min.js" |
| integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" |
| crossorigin="anonymous"></script> |
| <script src="/js/jena-navigation.js" type="text/javascript"></script> |
| <script src="/js/bootstrap.min.js" type="text/javascript"></script> |
| |
| <script src="/js/improve.js" type="text/javascript"></script> |
| |
| |
| </head> |
| |
| <body> |
| |
| <nav class="navbar navbar-default" role="navigation"> |
| <div class="container"> |
| <div class="navbar-header"> |
| <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse"> |
| <span class="icon-bar"></span> |
| <span class="icon-bar"></span> |
| <span class="icon-bar"></span> |
| </button> |
| <a class="navbar-brand" href="/index.html"> |
| <img class="logo-menu" src="/images/jena-logo/jena-logo-notext-small.png" alt="jena logo">Apache Jena</a> |
| </div> |
| |
| <div class="collapse navbar-collapse navbar-ex1-collapse"> |
| <ul class="nav navbar-nav"> |
| <li id="homepage"><a href="/index.html"><span class="glyphicon glyphicon-home"></span> Home</a></li> |
| <li id="download"><a href="/download/index.cgi"><span class="glyphicon glyphicon-download-alt"></span> Download</a></li> |
| <li class="dropdown"> |
| <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-book"></span> Learn <b class="caret"></b></a> |
| <ul class="dropdown-menu"> |
| <li class="dropdown-header">Tutorials</li> |
| <li><a href="/tutorials/index.html">Overview</a></li> |
| <li><a href="/documentation/fuseki2/index.html">Fuseki Triplestore</a></li> |
| <li><a href="/documentation/notes/index.html">How-To's</a></li> |
| <li><a href="/documentation/query/manipulating_sparql_using_arq.html">Manipulating SPARQL using ARQ</a></li> |
| <li><a href="/tutorials/rdf_api.html">RDF core API tutorial</a></li> |
| <li><a href="/tutorials/sparql.html">SPARQL tutorial</a></li> |
| <li><a href="/tutorials/using_jena_with_eclipse.html">Using Jena with Eclipse</a></li> |
| <li class="divider"></li> |
| <li class="dropdown-header">References</li> |
| <li><a href="/documentation/index.html">Overview</a></li> |
| <li><a href="/documentation/query/index.html">ARQ (SPARQL)</a></li> |
| <li><a href="/documentation/assembler/index.html">Assembler</a></li> |
| <li><a href="/documentation/tools/index.html">Command-line tools</a></li> |
| <li><a href="/documentation/rdfs/">Data with RDFS Inferencing</a></li> |
| <li><a href="/documentation/geosparql/index.html">GeoSPARQL</a></li> |
| <li><a href="/documentation/inference/index.html">Inference API</a></li> |
| <li><a href="/documentation/javadoc.html">Javadoc</a></li> |
| <li><a href="/documentation/ontology/">Ontology API</a></li> |
| <li><a href="/documentation/permissions/index.html">Permissions</a></li> |
| <li><a href="/documentation/extras/querybuilder/index.html">Query Builder</a></li> |
| <li><a href="/documentation/rdf/index.html">RDF API</a></li> |
| <li><a href="/documentation/rdfconnection/">RDF Connection - SPARQL API</a></li> |
| <li><a href="/documentation/io/">RDF I/O</a></li> |
| <li><a href="/documentation/rdfstar/index.html">RDF-star</a></li> |
| <li><a href="/documentation/shacl/index.html">SHACL</a></li> |
| <li><a href="/documentation/shex/index.html">ShEx</a></li> |
| <li><a href="/documentation/jdbc/index.html">SPARQL over JDBC</a></li> |
| <li><a href="/documentation/tdb/index.html">TDB</a></li> |
| <li><a href="/documentation/tdb2/index.html">TDB2</a></li> |
| <li><a href="/documentation/query/text-query.html">Text Search</a></li> |
| </ul> |
| </li> |
| |
| <li class="drop down"> |
| <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-book"></span> Javadoc <b class="caret"></b></a> |
| <ul class="dropdown-menu"> |
| <li><a href="/documentation/javadoc.html">All Javadoc</a></li> |
| <li><a href="/documentation/javadoc/arq/">ARQ</a></li> |
| <li><a href="/documentation/javadoc_elephas.html">Elephas</a></li> |
| <li><a href="/documentation/javadoc/fuseki2/">Fuseki</a></li> |
| <li><a href="/documentation/javadoc/geosparql/">GeoSPARQL</a></li> |
| <li><a href="/documentation/javadoc/jdbc/">JDBC</a></li> |
| <li><a href="/documentation/javadoc/jena/">Jena Core</a></li> |
| <li><a href="/documentation/javadoc/permissions/">Permissions</a></li> |
| <li><a href="/documentation/javadoc/extras/querybuilder/">Query Builder</a></li> |
| <li><a href="/documentation/javadoc/shacl/">SHACL</a></li> |
| <li><a href="/documentation/javadoc/tdb/">TDB</a></li> |
| <li><a href="/documentation/javadoc/text/">Text Search</a></li> |
| </ul> |
| </li> |
| |
| <li id="ask"><a href="/help_and_support/index.html"><span class="glyphicon glyphicon-question-sign"></span> Ask</a></li> |
| |
| <li class="dropdown"> |
| <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-bullhorn"></span> Get involved <b class="caret"></b></a> |
| <ul class="dropdown-menu"> |
| <li><a href="/getting_involved/index.html">Contribute</a></li> |
| <li><a href="/help_and_support/bugs_and_suggestions.html">Report a bug</a></li> |
| <li class="divider"></li> |
| <li class="dropdown-header">Project</li> |
| <li><a href="/about_jena/about.html">About Jena</a></li> |
| <li><a href="/about_jena/architecture.html">Architecture</a></li> |
| <li><a href="/about_jena/citing.html">Citing</a></li> |
| <li><a href="/about_jena/team.html">Project team</a></li> |
| <li><a href="/about_jena/contributions.html">Related projects</a></li> |
| <li><a href="/about_jena/roadmap.html">Roadmap</a></li> |
| <li class="divider"></li> |
| <li class="dropdown-header">ASF</li> |
| <li><a href="http://www.apache.org/">Apache Software Foundation</a></li> |
| <li><a href="http://www.apache.org/foundation/sponsorship.html">Become a Sponsor</a></li> |
| <li><a href="http://www.apache.org/licenses/LICENSE-2.0">License</a></li> |
| <li><a href="http://www.apache.org/security/">Security</a></li> |
| <li><a href="http://www.apache.org/foundation/thanks.html">Thanks</a></li> |
| </ul> |
| </li> |
| |
| |
| |
| |
| <li id="edit"><a href="https://github.com/apache/jena-site/edit/main/source/documentation/tools/schemagen.md" title="Edit this page on GitHub"><span class="glyphicon glyphicon-pencil"></span> Edit this page</a></li> |
| </ul> |
| </div> |
| </div> |
| </nav> |
| |
| |
| <div class="container"> |
| <div class="row"> |
| <div class="col-md-12"> |
| <div id="breadcrumbs"> |
| |
|
|
|
|
|
|
|
|
|
|
|
|
| <ol class="breadcrumb">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| <li><a href='/documentation'>DOCUMENTATION</a></li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| <li><a href='/documentation/tools'>TOOLS</a></li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| <li class="active">SCHEMAGEN</li>
|
|
|
|
|
|
|
|
|
| </ol>
|
|
|
|
|
|
|
| |
| |
| </div> |
| <h1 class="title">Jena schemagen HOWTO</h1> |
| |
| <p>The <code>schemagen</code> provided with Jena is used to convert an OWL or RDFS vocabulary into a Java class file that contains static |
| constants for the terms in the vocabulary. This documents outlines |
| the use of schemagen, and the various options and templates that |
| may be used to control the output.</p> |
| <p>Schemagen is typically invoked from the command line or from a |
| built script (such as Ant). Synopsis of the command:</p> |
| <pre><code>java jena.schemagen -i <input> [-a <namespaceURI>] [-o <output file>] [-c <config uri>] [-e <encoding>] ... |
| </code></pre> |
| <p>Schemagen is highly configurable, either with command line options |
| or by RDF information read from a configuration file. <strong>Many</strong> |
| other options are defined, and these are described in detail below. |
| Note that the <code>CLASSPATH</code> environment variable must be set to |
| include the Jena <code>.jar</code> libraries.</p> |
| <h2 id="summary-of-configuration-options">Summary of configuration options</h2> |
| <p>For quick reference, here is a list of all of the schemagen options |
| (both command line and configuration file). The use of these |
| options is explained in detail below.</p> |
| <p>Table 1: schemagen options</p> |
| <table> |
| <thead> |
| <tr> |
| <th>Command line option</th> |
| <th>RDF config file property</th> |
| <th>Meaning</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><code>-a <uri></code></td> |
| <td><code>sgen:namespace</code></td> |
| <td>The namespace URI for the vocabulary. Names with this URI as prefix are automatically included in the generated vocabulary. If not specified, the base URI of the ontology is used as a default (but note that some ontology documents don’t define a base URI).</td> |
| </tr> |
| <tr> |
| <td><code>-c <filename></code><br /><code>-c <url></code></td> |
| <td></td> |
| <td>Specify an alternative config file.</td> |
| </tr> |
| <tr> |
| <td><code>--classdec <string></code></td> |
| <td><code>sgen:classdec</code></td> |
| <td>Additional decoration for class header (such as <code>implements</code>)</td> |
| </tr> |
| <tr> |
| <td><code>--classnamesuffix <string></code></td> |
| <td><code>sgen:classnamesuffix</code></td> |
| <td>Option for adding a suffix to the generated class name, e.g. “Vocab”.</td> |
| </tr> |
| <tr> |
| <td><code>--classSection <string></code></td> |
| <td><code>sgen:classSection</code></td> |
| <td>Section declaration comment for class section.</td> |
| </tr> |
| <tr> |
| <td><code>--classTemplate <string></code></td> |
| <td><code>sgen:classTemplate</code></td> |
| <td>Template for writing out declarations of class resources.</td> |
| </tr> |
| <tr> |
| <td><code>--datatypesSection <string></code></td> |
| <td><code>sgen:datatypesSection</code></td> |
| <td>Section declaration comment for datatypes section.</td> |
| </tr> |
| <tr> |
| <td><code>--datatypeTemplate <string></code></td> |
| <td><code>sgen:datatypeTemplate</code></td> |
| <td>Template for writing out declarations of datatypes.</td> |
| </tr> |
| <tr> |
| <td><code>--declarations <string></code></td> |
| <td><code>sgen:declarations</code></td> |
| <td>Additional declarations to add at the top of the class.</td> |
| </tr> |
| <tr> |
| <td><code>--dos</code></td> |
| <td><code>sgen:dos</code></td> |
| <td>Use MSDOS-style line endings (i.e. \r\n). Default is Unix-style line endings.</td> |
| </tr> |
| <tr> |
| <td><code>-e <string></code></td> |
| <td><code>sgen:encoding</code></td> |
| <td>The surface syntax of the input file (e.g. RDF/XML, N3). Defaults to RDF/XML.</td> |
| </tr> |
| <tr> |
| <td><code>--footer <string></code></td> |
| <td><code>sgen:footer</code></td> |
| <td>Template for standard text to add to the end of the file.</td> |
| </tr> |
| <tr> |
| <td><code>--header <string></code></td> |
| <td><code>sgen:header</code></td> |
| <td>Template for the file header, including the class comment.</td> |
| </tr> |
| <tr> |
| <td><code>-i <filename></code> <br /><code>-i <url></code></td> |
| <td><code>sgen:input</code></td> |
| <td>Specify the input document to load</td> |
| </tr> |
| <tr> |
| <td><code>--include <uri></code></td> |
| <td><code>sgen:include</code></td> |
| <td>Option for including non-local URI’s in vocabulary</td> |
| </tr> |
| <tr> |
| <td><code>--individualsSection <string></code></td> |
| <td><code>sgen:individualsSection</code></td> |
| <td>Section declaration comment for individuals section.</td> |
| </tr> |
| <tr> |
| <td><code>--individualTemplate <string></code></td> |
| <td><code>sgen:individualTemplate</code></td> |
| <td>Template for writing out declarations of individuals.</td> |
| </tr> |
| <tr> |
| <td><code>--inference</code></td> |
| <td><code>sgen:inference</code></td> |
| <td>Causes the model that loads the document prior to being processed to apply inference rules appropriate to the language. E.g. OWL inference rules will be used on a <code>.owl</code> file.</td> |
| </tr> |
| <tr> |
| <td><code>--marker <string></code></td> |
| <td><code>sgen:marker</code></td> |
| <td>Specify the marker string for substitutions, default is ‘%’</td> |
| </tr> |
| <tr> |
| <td><code>-n <string></code></td> |
| <td><code>sgen:classname</code></td> |
| <td>The name of the generated class. The default is to synthesise a name based on input document name.</td> |
| </tr> |
| <tr> |
| <td><code>--noclasses</code></td> |
| <td><code>sgen:noclasses</code></td> |
| <td>Option to suppress classes in the generated vocabulary file</td> |
| </tr> |
| <tr> |
| <td><code>--nocomments</code></td> |
| <td><code>sgen:noComments</code></td> |
| <td>Turn off all comment output in the generated vocabulary</td> |
| </tr> |
| <tr> |
| <td><code>--nodatatypes</code></td> |
| <td><code>sgen:nodatatypes</code></td> |
| <td>Option to suppress datatypes in the generated vocabulary file.</td> |
| </tr> |
| <tr> |
| <td><code>--noheader</code></td> |
| <td><code>sgen:noHeader</code></td> |
| <td>Prevent the output of a file header, with class comment etc.</td> |
| </tr> |
| <tr> |
| <td><code>--noindividuals</code></td> |
| <td><code>sgen:noindividuals</code></td> |
| <td>Option to suppress individuals in the generated vocabulary file.</td> |
| </tr> |
| <tr> |
| <td><code>--noproperties</code></td> |
| <td><code>sgen:noproperties</code></td> |
| <td>Option to suppress properties in the generated vocabulary file.</td> |
| </tr> |
| <tr> |
| <td><code>-o <filename></code> <br /> <code>-o <dir></code></td> |
| <td><code>sgen:output</code></td> |
| <td>Specify the destination for the output. If the given value evaluates to a directory, the generated class will be placed in that directory with a file name formed from the generated (or given) class name with “.java” appended.</td> |
| </tr> |
| <tr> |
| <td><code>--nostrict</code></td> |
| <td><code>sgen:noStrict</code></td> |
| <td>Option to turn off strict checking for ontology classes and properties (prevents <code>ConversionExceptions</code>).</td> |
| </tr> |
| <tr> |
| <td><code>--ontology</code></td> |
| <td><code>sgen:ontology</code></td> |
| <td>The generated vocabulary will use the ontology API terms, in preference to RDF model API terms.</td> |
| </tr> |
| <tr> |
| <td><code>--owl</code></td> |
| <td><code>sgen:owl</code></td> |
| <td>Specify that the language of the source is OWL (the default). Note that RDFS is a subset of OWL, so this setting also suffices for RDFS.</td> |
| </tr> |
| <tr> |
| <td><code>--package <string></code></td> |
| <td><code>sgen:package</code></td> |
| <td>Specify the Java package name and directory.</td> |
| </tr> |
| <tr> |
| <td><code>--propSection <string></code></td> |
| <td><code>sgen:propSection</code></td> |
| <td>Section declaration comment for properties section.</td> |
| </tr> |
| <tr> |
| <td><code>--propTemplate <string></code></td> |
| <td><code>sgen:propTemplate</code></td> |
| <td>Template for writing out declarations of property resources.</td> |
| </tr> |
| <tr> |
| <td><code>-r <uri></code></td> |
| <td></td> |
| <td>Specify the uri of the root node in the RDF configuration model.</td> |
| </tr> |
| <tr> |
| <td><code>--rdfs</code></td> |
| <td><code>sgen:rdfs</code></td> |
| <td>Specify that the language of the source ontology is RDFS.</td> |
| </tr> |
| <tr> |
| <td><code>--strictIndividuals</code></td> |
| <td><code>sgen:strictIndividuals</code></td> |
| <td>When selecting the individuals to include in the output class, schemagen will normally include those individuals whose <code>rdf:type</code> is in the included namespaces for the vocabulary. However, if <code>strictIndividuals</code> is turned on, then all individuals in the output class must themselves have a URI in the included namespaces.</td> |
| </tr> |
| <tr> |
| <td><code>--uppercase</code></td> |
| <td><code>sgen:uppercase</code></td> |
| <td>Option for mapping constant names to uppercase (like Java constants). Default is to leave the case of names unchanged.</td> |
| </tr> |
| <tr> |
| <td><code>--includeSource</code></td> |
| <td><code>sgen:includeSource</code></td> |
| <td>Serializes the source code of the vocabulary, and includes this into the generated class file. At class load time, creates a <code>Model</code> containing the definitions from the source</td> |
| </tr> |
| </tbody> |
| </table> |
| <h2 id="what-does-schemagen-do">What does schemagen do?</h2> |
| <p>RDFS and OWL provide a very convenient means to define a |
| controlled vocabulary or ontology. For general ontology processing, |
| Jena provides various API’s to allow the source files to be read in |
| and manipulated. However, when developing an application, it is |
| frequently convenient to refer to the controlled vocabulary terms |
| directly from Java code. This leads typically to the declaration of |
| constants, such as:</p> |
| <pre><code> public static final Resource A_CLASS = new ResourceImpl( "http://example.org/schemas#a-class" ); |
| </code></pre> |
| <p>When these constants are defined manually, it is tedious and |
| error-prone to maintain them in sync with the source ontology |
| file. Schemagen automates the production of Java constants that |
| correspond to terms in an ontology document. By automating the step |
| from source vocabulary to Java constants, a source of error and |
| inconsistency is removed.</p> |
| <h3 id="example">Example</h3> |
| <p>Perhaps the easiest way to explain the detail of what schemagen |
| does is to show an example. Consider the following mini-RDF |
| vocabulary:</p> |
| <pre><code><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" |
| xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" |
| xmlns="http://example.org/eg#" |
| xml:base="http://example.org/eg"> |
| <rdfs:Class rdf:ID="Dog"> |
| <rdfs:comment>A class of canine companions</rdfs:comment> |
| </rdfs:Class> |
| <rdf:Property rdf:ID="petName"> |
| <rdfs:comment>The name that everyone calls a dog</rdfs:comment> |
| <rdfs:domain rdf:resource="http://example.org/eg#Dog" /> |
| </rdf:Property> |
| <rdf:Property rdf:ID="kennelName"> |
| <rdfs:comment>Posh dogs have a formal name on their KC certificate</rdfs:comment> |
| </rdf:Property> |
| <Dog rdf:ID="deputy"> |
| <rdfs:comment>Deputy is a particular Dog</rdfs:comment> |
| <kennelName>Deputy Dawg of Chilcompton</kennelName> |
| </Dog> |
| </rdf:RDF> |
| </code></pre> |
| <p>We process this document with a command something like: |
| <code>Java jena.schemagen -i deputy.rdf -a http://example.org/eg#</code> |
| to produce the following generated class:</p> |
| <pre><code>/* CVS $Id: schemagen.html,v 1.16 2010-06-11 00:08:23 ian_dickinson Exp $ */ |
| |
| import org.apache.jena.rdf.model.*; |
| |
| /** |
| * Vocabulary definitions from deputy.rdf |
| * @author Auto-generated by schemagen on 01 May 2003 21:49 |
| */ |
| public class Deputy { |
| /** <p>The RDF model that holds the vocabulary terms</p> */ |
| private static Model m_model = ModelFactory.createDefaultModel(); |
| |
| /** <p>The namespace of the vocabulary as a string {@value}</p> */ |
| public static final String NS = "http://example.org/eg#"; |
| |
| /** <p>The namespace of the vocabulary as a resource {@value}</p> */ |
| public static final Resource NAMESPACE = m_model.createResource( "http://example.org/eg#" ); |
| |
| /** <p>The name that everyone calls a dog</p> */ |
| public static final Property petName = m_model.createProperty( "http://example.org/eg#petName" ); |
| |
| /** <p>Posh dogs have a formal name on their KC certificate</p> */ |
| public static final Property kennelName = m_model.createProperty( "http://example.org/eg#kennelName" ); |
| |
| /** <p>A class of canine companions</p> */ |
| public static final Resource Dog = m_model.createResource( "http://example.org/eg#Dog" ); |
| |
| /** <p>Deputy is a particular Dog</p> */ |
| public static final Resource deputy = m_model.createResource( "http://example.org/eg#deputy" ); |
| |
| } |
| </code></pre> |
| <p>Some things to note in this example. All of the named classes, |
| properties and individuals from the source document are translated |
| to Java constants (below we show how to be more selective than |
| this). The properties of the named resources are <em>not</em> translated: |
| schemagen is for giving access to the names in the vocabulary or |
| schema, not to perform a general translation of RDF to Java. The |
| RDFS comments from the source code are translated to Javadoc |
| comments. Finally, we no longer directly call <code>new ResourceImpl</code>: |
| this idiom is no longer recommended by the Jena team.</p> |
| <p>We noted earlier that schemagen is highly configurable. One |
| additional argument generates a vocabulary file that uses Jena’s |
| ontology API, rather than the RDF model API. We change <code>rdfs:Class</code> |
| to <code>owl:Class</code>, and invoke |
| <code>Java jena.schemagen -i deputy.rdf -b http://example.org/eg# --ontology</code> |
| to get:</p> |
| <pre><code>/* CVs $Id: schemagen.html,v 1.16 2010-06-11 00:08:23 ian_dickinson Exp $ */ |
| |
| import org.apache.jena.rdf.model.*; |
| import org.apache.jena.ontology.*; |
| /** |
| * Vocabulary definitions from deputy.rdf |
| * @author Auto-generated by schemagen on 01 May 2003 22:03 |
| */ |
| public class Deputy { |
| /** <p>The ontology model that holds the vocabulary terms</p> */ |
| private static OntModel m_model = ModelFactory.createOntologyModel( ProfileRegistry.OWL_LANG ); |
| |
| /** <p>The namespace of the vocabulary as a string {@value}</p> */ |
| public static final String NS = "http://example.org/eg#"; |
| |
| /** <p>The namespace of the vocabulary as a resource {@value}</p> */ |
| public static final Resource NAMESPACE = m_model.createResource( "http://example.org/eg#" ); |
| |
| /** <p>The name that everyone calls a dog</p> */ |
| public static final Property petName = m_model.createProperty( "http://example.org/eg#petName" ); |
| |
| /** <p>Posh dogs have a formal name on their KC certificate</p> */ |
| public static final Property kennelName = m_model.createProperty( "http://example.org/eg#kennelName" ); |
| |
| /** <p>A class of canine companions</p> */ |
| public static final OntClass Dog = m_model.createClass( "http://example.org/eg#Dog" ); |
| |
| /** <p>Deputy is a particular Dog</p> */ |
| public static final Individual deputy = m_model.createIndividual( Dog, "http://example.org/eg#deputy" ); |
| |
| } |
| </code></pre> |
| <h2 id="general-principles">General principles</h2> |
| <p>In essence, schemagen will load a single vocabulary file, |
| and generate a Java |
| class that contains static constants for the named classes, |
| properties and instances of the vocabulary. Most of the generated |
| components of the output Java file can be controlled by option |
| flags, and formatted with a template. Default templates are |
| provided for all elements, so the minimum amount of necessary |
| information is actually very small.</p> |
| <p>Options can be specified on the command line (when invoking |
| schemagen), or may be preset in an RDF file. Any mixture of command |
| line and RDF option specification is permitted. Where a given |
| option is specified both in an RDF file and on the command line, |
| the command line setting takes precedence. Thus the options in the |
| RDF file can be seen as defaults.</p> |
| <h3 id="specifying-command-line-options">Specifying command line options</h3> |
| <p>To specify a command line option, add its name (and optional value) |
| to the command line when invoking the schemagen tool. E.g: |
| <code>Java jena.schemagen -i myvocab.owl --ontology --uppercase</code></p> |
| <h3 id="specifying-options-in-an-rdf-file">Specifying options in an RDF file</h3> |
| <p>To specify an option in an RDF file, create a resource of type |
| <code>sgen:Config</code>, with properties corresponding to the option names |
| listed in Table 1. The following fragment shows a small options |
| file. A complete example configuration file is shown in |
| <a href="#appendixa">appendix A</a>.</p> |
| <p>By default, schemagen will look for a configuration file named |
| <code>schemagen.rdf</code> in the current directory. To specify another |
| configuration, use the <code>-c</code> option with a URL to reference the |
| configuration. Multiple configurations (i.e. multiple <code>sgen:Config</code> |
| nodes) can be placed in one RDF document. In this case, each |
| configuration node must be named, and the URI specified in the <code>-r</code> |
| command line option. If there is no <code>-r</code> option, schemagen will |
| look for a node of type <code>rdf:type sgen:Config</code>. If there are |
| multiple such nodes in the model, it is indeterminate which one |
| will be used.</p> |
| <h3 id="using-templates">Using templates</h3> |
| <p>We have several times referred to a template being used to |
| construct part of the generated file. What is a template? Simply |
| put, it is a fragment of output file. Some templates will be used |
| at most once (for example the file header template), some will be |
| used many times (such as the template used to generate a class |
| constant). In order to make the templates adaptable to the job |
| they’re doing, before it is written out a template has |
| <em>keyword substitution</em> performed on it. This looks for certain |
| keywords delimited by a pair of special characters (% by default), |
| and replaces them with the current binding for that keyword. Some |
| keyword bindings stay the same throughout the processing of the |
| file, and some are dependent on the language element being |
| processed. The substitutions are:</p> |
| <p>Table 2: Substitutable keywords in templates</p> |
| <table> |
| <thead> |
| <tr> |
| <th>Keyword</th> |
| <th>Meaning</th> |
| <th>Typical value</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td>classname The name of the Java class being generated</td> |
| <td>Automatically defined from the document name, or given with the <code>-n</code> option</td> |
| <td></td> |
| </tr> |
| <tr> |
| <td>date</td> |
| <td>The date and time the class was generated</td> |
| <td></td> |
| </tr> |
| <tr> |
| <td>imports</td> |
| <td>The Java imports for this class</td> |
| <td></td> |
| </tr> |
| <tr> |
| <td>nl</td> |
| <td>The newline character for the current platform</td> |
| <td></td> |
| </tr> |
| <tr> |
| <td>package</td> |
| <td>The Java package name</td> |
| <td>As specified by an option. The option just gives the package name, schemagen turns the name into a legal Java statement.</td> |
| </tr> |
| <tr> |
| <td>sourceURI</td> |
| <td>The source of the document being processed</td> |
| <td>As given by the <code>-i</code> option or in the config file.</td> |
| </tr> |
| <tr> |
| <td>valclass</td> |
| <td>The Java class of the value being defined</td> |
| <td>E.g. Property for vocabulary properties, Resource for classes in RDFS, or OntClass for classes using the ontology API</td> |
| </tr> |
| <tr> |
| <td>valcreator</td> |
| <td>The method used to generate an instance of the Java representation</td> |
| <td>E.g. <code>createResource</code> or <code>createClass</code></td> |
| </tr> |
| <tr> |
| <td>valname</td> |
| <td>The name of the Java constant being generated</td> |
| <td>This is generated from the name of the resource in the source file, adjusted to be a legal Java identifier. By default, this will preserve the case of the RDF constant, but setting <code>--uppercase</code> will map all constants to upper-case names (a common convention in Java code).</td> |
| </tr> |
| <tr> |
| <td>valtype</td> |
| <td>The rdf:type for an individual</td> |
| <td>The class name or URI used when creating an individual in the ontology API</td> |
| </tr> |
| <tr> |
| <td>valuri</td> |
| <td>The full URI of the value being defined</td> |
| <td>From the RDF, without adjustment.</td> |
| </tr> |
| </tbody> |
| </table> |
| <h2 id="details-of-schemagen-options">Details of schemagen options</h2> |
| <p>We now go through each of the configuration options in detail.</p> |
| <p><strong>Note</strong>: for brevity, we assume a standard prefix <code>sgen</code> is |
| defined for resource URI’s in the schemagen namespace. The |
| expansion for <code>sgen</code> is: |
| <code>http://jena.hpl.hp.com/2003/04/schemagen#</code>, thus:</p> |
| <pre><code>xmlns:sgen="http://jena.hpl.hp.com/2003/04/schemagen#" |
| </code></pre> |
| <h3 id="note-on-legal-java-identifiers">Note on legal Java identifiers</h3> |
| <p>Schemagen will attempt to ensure that all generated code will |
| compile as legal Java. Occasionally, this means that identifiers |
| from input documents, which are legal components of RDF URI |
| identifiers, have to be modified to be legal Java identifiers. |
| Specifically, any character in an identifier name that is not a |
| legal Java identifier character will be replaced with the character |
| ‘_’ (underscore). Thus the name ‘<code>trading-price</code>’ might become |
| <code>'trading_price</code>’. In addition, Java requires that identifiers be |
| distinct. If a name clash is detected (for example, <code>trading-price</code> |
| and <code>trading+price</code> both map to the same Java identifier), |
| schemagen will add disambiguators to the second and subsequent |
| uses. These will be based on the role of the identifier; for |
| example property names are disambiguated by appending <code>_PROPn</code> for |
| increasing values of <code>n</code>. In a well-written ontology, identifiers |
| are typically made distinct for clarity and ease-of-use by the |
| ontology users, so the use of the disambiguation tactic is rare. |
| Indeed, it may be taken as a hint that refactoring the ontology |
| itself is desirable.</p> |
| <h3 id="specifying-the-configuration-file">Specifying the configuration file</h3> |
| <table> |
| <thead> |
| <tr> |
| <th></th> |
| <th></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><strong>Command line</strong></td> |
| <td><code>-c <*config-file-path*></code><br /><code>-c <*config-file-URL*></code></td> |
| </tr> |
| <tr> |
| <td><strong>Config file</strong></td> |
| <td>n/a</td> |
| </tr> |
| </tbody> |
| </table> |
| <p>The default configuration file name is <code>schemagen.rdf</code> in the |
| current directory. To specify a different configuration file, |
| either as a file name on the local file system, or as a URL (e.g. |
| an <code>http:</code> address), the config file location is passed with the |
| <code>-c</code> option. If no <code>-c</code> option is given, and there is no |
| configuration file in the current directory, schemagen will |
| continue and use default values (plus the other command line |
| options) to configure the tool. If a file name or URL is given with |
| <code>-c</code>, and that file cannot be located, schemagen will stop with an |
| error.</p> |
| <p>Schemagen will assume the language encoding of the |
| configuration file is implied by the filename/URL suffix: “.n3” |
| means N3, “.nt” means NTRIPLES, “.rdf” and “.owl” mean “RDF/XML”. |
| By default it assumes RDF/XML.</p> |
| <h3 id="specifying-the-configuration-root-in-the-configuration-file">Specifying the configuration root in the configuration file</h3> |
| <table> |
| <thead> |
| <tr> |
| <th></th> |
| <th></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><strong>Command line</strong></td> |
| <td><code>-r <*config-root-URI*></code></td> |
| </tr> |
| <tr> |
| <td><strong>Config file</strong></td> |
| <td>n/a</td> |
| </tr> |
| </tbody> |
| </table> |
| <p>It is possible to have more than one set of configuration options |
| in one configuration file. If there is only one set of |
| configuration options, schemagen will locate the root by searching |
| for a resource of rdf:type sgen:Config. If there is more than one, |
| and no root is specified on the command line, it is not specified |
| which set of configuration options will be used. The root URI given |
| as a command line option must match exactly with the URI given in |
| the configuration file. For example:</p> |
| <pre><code>Java jena.schemagen -c config/localconf.rdf -r http://example.org/sg#project1 |
| </code></pre> |
| <p>matches:</p> |
| <pre><code>... |
| <sgen:Config rdf:about="http://example.org/SG#project1"> |
| .... |
| </sgen:Config> |
| </code></pre> |
| <h3 id="specifying-the-input-document">Specifying the input document</h3> |
| <table> |
| <thead> |
| <tr> |
| <th></th> |
| <th></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><strong>Command line</strong></td> |
| <td><code>-i <*input-file-path*></code><br /><code>-i <*input-URL*></code></td> |
| </tr> |
| <tr> |
| <td><strong>Config file</strong></td> |
| <td><code><sgen:input rdf:resource="*inputURL*" /></code></td> |
| </tr> |
| </tbody> |
| </table> |
| <p>The only mandatory argument to schemagen is the input document to |
| process. This can be specified in the configuration file, though |
| this does, of course, mean that the same configuration cannot be |
| applied to multiple different input files for consistency. However, |
| by specifying the input document in the default configuration file, |
| schemagen can easily be invoked with the minimum of command line |
| typing. For other means of automating schemagen, see |
| <a href="#ant">using schemagen with Ant</a>.</p> |
| <h3 id="specifying-the-output-location">Specifying the output location</h3> |
| <table> |
| <thead> |
| <tr> |
| <th></th> |
| <th></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><strong>Command line</strong></td> |
| <td><code>-o <*input-file-path*></code><br /><code>-o <*output-dir*></code></td> |
| </tr> |
| <tr> |
| <td><strong>Config file</strong></td> |
| <td><code><sgen:output rdf:datatype="&xsd;string">*output-path-or-dir*</sgen:output></code></td> |
| </tr> |
| </tbody> |
| </table> |
| <p>Schemagen must know where to write the generated Java file. By |
| default, the output is written to the standard output. Various |
| options exist to change this. The output location can be specified |
| either on the command line, or in the configuration file. If |
| specified in the configuration file, the resource must be a string |
| literal, denoting the file path. If the path given resolves to an |
| existing directory, then it is assumed that the output will be |
| based on the <a href="#class-name">name</a> of the generated class (i.e. it |
| will be the class name with Java appended). Otherwise, the path is |
| assumed to point to a file. Any existing file that has the given |
| path name will be overwritten.</p> |
| <p>By default, schemagen will create files that have the Unix |
| convention for line-endings (i.e. ‘\n’). To switch to DOS-style |
| line endings, use <code>--dos</code>.</p> |
| <table> |
| <thead> |
| <tr> |
| <th></th> |
| <th></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><strong>Command line</strong></td> |
| <td><code>--dos</code></td> |
| </tr> |
| <tr> |
| <td><strong>Config file</strong></td> |
| <td><code><sgen:dos rdf:datatype="&xsd;boolean">true</sgen:dos></code></td> |
| </tr> |
| </tbody> |
| </table> |
| <h3 id="specifying-the-class-name">Specifying the class name</h3> |
| <table> |
| <thead> |
| <tr> |
| <th></th> |
| <th></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><strong>Command line</strong></td> |
| <td><code>-n <*class-name*></code></td> |
| </tr> |
| <tr> |
| <td><strong>Config file</strong></td> |
| <td><code><sgen:classname rdf:datatype="&xsd;string">*classname*</sgen:classname></code></td> |
| </tr> |
| </tbody> |
| </table> |
| <p>By default, the name of the class will be based on the name of the |
| input file. Specifically, the last component of the input |
| document’s path name, with the prefix removed, becomes the class |
| name. By default, the initial letter is adjusted to a capital to |
| conform to standard Java usage. Thus <code>file:vocabs/trading.owl</code> |
| becomes <code>Trading.java</code>. To override this default algorithm, a class |
| name specified by <code>-n</code> or in the config file is used exactly as |
| given.</p> |
| <p>Sometimes it is convenient to have all vocabulary files |
| distinguished by a common suffix, for example <code>xyzSchema.java</code> or |
| <code>xyzVocabs.java</code>. This can be achieved by the classname-suffix |
| option:</p> |
| <table> |
| <thead> |
| <tr> |
| <th></th> |
| <th></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><strong>Command line</strong></td> |
| <td><code>--classnamesuffix <*suffix*></code></td> |
| </tr> |
| <tr> |
| <td><strong>Config file</strong></td> |
| <td><code><sgen:classnamesuffix rdf:datatype="&xsd;string">*suffix*</sgen:classnamesuffix></code></td> |
| </tr> |
| </tbody> |
| </table> |
| <p>See also the <a href="#java-ids">note on legal Java identifiers</a>, which |
| applies to generated class names.</p> |
| <h3 id="specifying-the-vocabulary-namespace">Specifying the vocabulary namespace</h3> |
| <table> |
| <thead> |
| <tr> |
| <th></th> |
| <th></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><strong>Command line</strong></td> |
| <td><code>-a <*namespace-URI*></code></td> |
| </tr> |
| <tr> |
| <td><strong>Config file</strong></td> |
| <td><code><sgen:namespace rdf:datatype="&xsd;string">*namespace*</sgen:namespace></code></td> |
| </tr> |
| </tbody> |
| </table> |
| <p>Since ontology files are often modularised, it is not the case that |
| all of the resource names appearing in a given document are being |
| defined by that ontology. They may appear simply as part of the |
| definitions of other terms. Schemagen assumes that there is one |
| primary namespace for each document, and it is names from that |
| namespace that will appear in the generated Java file.</p> |
| <p>In an OWL ontology, this namespace is computed by |
| finding the owl:Ontology element, and using its |
| namespace as the primary namespace of the ontology. This may not be |
| available (it is not, for example, a part of RDFS) or correct, so |
| the namespace may be specified directly with the <code>-a</code> option or in |
| the configuration file.</p> |
| <p>Schemagen does not, in the present version, permit more than one |
| primary namespace per generated Java class. However, constants from |
| namespaces other than the primary namespace may be included in the |
| generated Java class by the include option:</p> |
| <table> |
| <thead> |
| <tr> |
| <th></th> |
| <th></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><strong>Command line</strong></td> |
| <td><code>--include <*namespace-URI*></code></td> |
| </tr> |
| <tr> |
| <td><strong>Config file</strong></td> |
| <td><code><sgen:include rdf:datatype="&xsd;string">*namespace*</sgen:include></code></td> |
| </tr> |
| </tbody> |
| </table> |
| <p>The include option may repeated multiple times to include a variety |
| of constants from other namespaces in the output class.</p> |
| <p>Since OWL and RDFS ontologies may include individuals that are |
| named instances of declared classes, schemagen will include |
| individuals among the constants that it generates in Java. By |
| default, an individual will be included if its class has a URI that |
| is in one of the permitted namespaces for the vocabulary, even if |
| the individual itself is not in that namespace. If the option |
| <code>strictIndividuals</code> is set, individuals are <strong>only</strong> included if |
| they have a URI that is in the permitted namespaces for the |
| vocabulary.</p> |
| <table> |
| <thead> |
| <tr> |
| <th></th> |
| <th></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><strong>Command line</strong></td> |
| <td><code>--strictIndividuals</code></td> |
| </tr> |
| <tr> |
| <td><strong>Config file</strong></td> |
| <td><code><sgen:strictIndividuals /></code></td> |
| </tr> |
| </tbody> |
| </table> |
| <h3 id="specifying-the-syntax-encoding-of-the-input-document">Specifying the syntax (encoding) of the input document</h3> |
| <table> |
| <thead> |
| <tr> |
| <th></th> |
| <th></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><strong>Command line</strong></td> |
| <td><code>-e <*encoding*></code></td> |
| </tr> |
| <tr> |
| <td><strong>Config file</strong></td> |
| <td><code><sgen:encoding rdf:datatype="&xsd;string">*encoding*</sgen:encoding></code></td> |
| </tr> |
| </tbody> |
| </table> |
| <p>Jena can parse a number of different presentation syntaxes for RDF |
| documents, including RDF/XML, N3 and NTRIPLE. By default, the |
| encoding will be derived from the name of the input document (e.g. |
| a document <code>xyz.n3</code> will be parsed in N3 format), or, if the |
| extension is non-obvious the default is RDF/XML. The encoding, and |
| hence the parser, to use on the input document may be specified by |
| the encoding configuration option.</p> |
| <h3 id="choosing-the-style-of-the-generated-class-ontology-or-plain-rdf">Choosing the style of the generated class: ontology or plain RDF</h3> |
| <table> |
| <thead> |
| <tr> |
| <th></th> |
| <th></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><strong>Command line</strong></td> |
| <td><code>--ontology</code></td> |
| </tr> |
| <tr> |
| <td><strong>Config file</strong></td> |
| <td><code><sgen:ontology rdf:datatype="&xsd;boolean">*true or false*</sgen:ontology></code></td> |
| </tr> |
| </tbody> |
| </table> |
| <p>By default, the Java class generated by schemagen will generate |
| constants that are plain RDF Resource, Property or Literal |
| constants. When working with OWL or RDFS ontologies, it may |
| be more convenient to have constants that are OntClass, |
| ObjectProperty, DatatypeProperty and Individual Java objects. To |
| generate these ontology constants, rather than plain RDF constants, |
| set the ontology configuration option.</p> |
| <p>Furthermore, since Jena can handle input ontologies in |
| OWL (the default), and RDFS, it is necessary to be able to specify |
| which language is being processed. This will affect both the |
| parsing of the input documents, and the language profile selected |
| for the constants in the generated Java class.</p> |
| <table> |
| <thead> |
| <tr> |
| <th></th> |
| <th></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><strong>Command line</strong></td> |
| <td><code>--owl</code></td> |
| </tr> |
| <tr> |
| <td><strong>Config file</strong></td> |
| <td><code><sgen:owl rdf:datatype="&xsd;boolean">true</sgen:owl></code></td> |
| </tr> |
| </tbody> |
| </table> |
| <table> |
| <thead> |
| <tr> |
| <th></th> |
| <th></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><strong>Command line</strong></td> |
| <td><code>--rdfs</code></td> |
| </tr> |
| <tr> |
| <td><strong>Config file</strong></td> |
| <td><code><sgen:rdfs rdf:datatype="&xsd;boolean">true</sgen:owl></code></td> |
| </tr> |
| </tbody> |
| </table> |
| <p>Prior to Jena 2.2, schemagen used a Jena model to load the input |
| document that also applied some <em>rules of inference</em> to the input |
| data. So, for example, a resource that is mentioned as the |
| <code>owl:range</code> of a property can be inferred to be |
| <code>rdf:type owl:Class</code>, and hence listed in the class constants in |
| the generated Java class, even if that fact is not directly |
| asserted in the input model. From Jena 2.2 onwards, this option is |
| now <strong>off by default</strong>. If correct handling of an input document by |
| schemagen requires the use of inference rules, this must be |
| specified by the <code>inference</code> option.</p> |
| <table> |
| <thead> |
| <tr> |
| <th></th> |
| <th></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><strong>Command line</strong></td> |
| <td><code>--inference</code></td> |
| </tr> |
| <tr> |
| <td><strong>Config file</strong></td> |
| <td><code><sgen:inference rdf:datatype="&xsd;boolean">true</sgen:owl></code></td> |
| </tr> |
| </tbody> |
| </table> |
| <h3 id="specifying-the-java-package">Specifying the Java package</h3> |
| <table> |
| <thead> |
| <tr> |
| <th></th> |
| <th></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><strong>Command line</strong></td> |
| <td><code>--package <*package-name*></code></td> |
| </tr> |
| <tr> |
| <td><strong>Config file</strong></td> |
| <td><code><sgen:package rdf:datatype="&xsd;string">*package-name*</sgen:package></code></td> |
| </tr> |
| </tbody> |
| </table> |
| <p>By default, the Java class generated by schemagen will not be in a |
| Java package. Set the package configuration option to specify the |
| Java package name. <strong>Change from Jena 2.6.4-SNAPSHOT onwards:</strong> |
| Setting the package name will affect the directory into which the |
| generated class will be written: directories will be appended to |
| the <a href="#output">output directory</a> to match the Java package.</p> |
| <h3 id="additional-decorations-on-the-main-class-declaration">Additional decorations on the main class declaration</h3> |
| <table> |
| <thead> |
| <tr> |
| <th></th> |
| <th></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><strong>Command line</strong></td> |
| <td><code>--classdec <*class-declaration*></code></td> |
| </tr> |
| <tr> |
| <td><strong>Config file</strong></td> |
| <td><code><sgen:classdec rdf:datatype="&xsd;string">*class-declaration*</sgen:classdec></code></td> |
| </tr> |
| </tbody> |
| </table> |
| <p>In some applications, it may be convenient to add additional |
| information to the declaration of the Java class, for example that |
| the class implements a given interface (such as |
| <code>java.lang.Serializable</code>). Any string given as the value of the |
| class-declaration option will be written immediately after |
| “<code>public class <i>ClassName</i></code>”.</p> |
| <h3 id="adding-general-declarations-within-the-generated-class">Adding general declarations within the generated class</h3> |
| <table> |
| <thead> |
| <tr> |
| <th></th> |
| <th></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><strong>Command line</strong></td> |
| <td><code>--declarations <*declarations*></code></td> |
| </tr> |
| <tr> |
| <td><strong>Config file</strong></td> |
| <td><code><sgen:declarations rdf:datatype="&xsd;string">*declarations*</sgen:declarations></code></td> |
| </tr> |
| </tbody> |
| </table> |
| <p>Some more complex vocabularies may require access to static |
| constants, or other Java objects or factories to fully declare the |
| constants defined by the given templates. Any text given by the |
| declarations option will be included in the generated class after |
| the class declaration but before the body of the declared |
| constants. The value of the option should be fully legal Java code |
| (though the <a href="#templates">template</a> substitutions will be performed |
| on the code). Although this option can be declared as a command |
| line option, it is typically easier to specify as a value in a |
| configuration options file.</p> |
| <h3 id="omitting-sections-of-the-generated-vocabulary">Omitting sections of the generated vocabulary</h3> |
| <table> |
| <thead> |
| <tr> |
| <th></th> |
| <th></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><strong>Command line</strong></td> |
| <td><code>--noclasses</code><br /><code>--nodatatypes</code><br /><code>--noproperties</code><br /><code>--noindividuals</code></td> |
| </tr> |
| <tr> |
| <td><strong>Config file</strong></td> |
| <td><code><sgen:noclassses rdf:datatype="&xsd;boolean">true</sgen:noclassses></code><br /><code><sgen:nodatatypes rdf:datatype="&xsd;boolean">true</sgen:nodatatypes></code><br /><code><sgen:noproperties rdf:datatype="&xsd;boolean">true</sgen:noproperties></code><br /><code><sgen:noindividuals rdf:datatype="&xsd;boolean">true</sgen:noindividuals></code></td> |
| </tr> |
| </tbody> |
| </table> |
| <p>By default, the vocabulary class generated from a given ontology |
| will include constants for each of the included classes, datatypes, properties |
| and individuals in the ontology. To omit any of these groups, use |
| the corresponding <em>noXYZ</em> configuration option. For example, |
| specifying <code>--noproperties</code> means that the generated class will not |
| contain any constants corresponding to predicate names from the |
| ontology, irrespective of what is in the input document.</p> |
| <h3 id="section-header-comments">Section header comments</h3> |
| <table> |
| <thead> |
| <tr> |
| <th></th> |
| <th></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><strong>Command line</strong></td> |
| <td><code>--classSection *<section heading>*</code><br /><code>--datatypeSection *<section heading>*</code><br /><code>--propSection *<section heading>*</code><br /><code>--individualSection *<section heading*></code><br /><code>--header *<file header section>*</code><br /><code>--footer *<file footer section>*</code></td> |
| </tr> |
| <tr> |
| <td><strong>Config file</strong></td> |
| <td><code><sgen:classSection rdf:datatype="&xsd;string">*section heading*</sgen:classSection></code><br /><code><sgen:datatypeSection rdf:datatype="&xsd;string">*section heading*</sgen:datatypeSection></code><br /><code><sgen:propSection rdf:datatype="&xsd;string">*section heading*</sgen:propSection></code><br /><code><sgen:individualSection rdf:datatype="&xsd;string">*section heading*</sgen:individualSection></code><br /><code><sgen:header rdf:datatype="&xsd;string">*file header*</sgen:header></code><br /><code><sgen:footer rdf:datatype="&xsd;string">*file footer*</sgen:footer></code></td> |
| </tr> |
| </tbody> |
| </table> |
| <p>Some coding styles use block comments to delineate different |
| sections of a class. These options allow the introduction of |
| arbitrary Java code, though typically this will be a comment block, |
| at the head of the sections of class constant declarations, |
| datatype constant declarations, property constant declarations, |
| and individual constant declarations.</p> |
| <h3 id="include-vocabulary-source-code">Include vocabulary source code</h3> |
| <table> |
| <thead> |
| <tr> |
| <th></th> |
| <th></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><strong>Command line</strong></td> |
| <td><code>--includeSource</code></td> |
| </tr> |
| <tr> |
| <td><strong>Config file</strong></td> |
| <td><code>\<sgen:includeSource rdf:datatype="&xsd;boolean">true\</sgen:includeSource></code></td> |
| </tr> |
| </tbody> |
| </table> |
| <p>Schemagen’s primary role is to provide Java constants corresponding to the names in |
| a vocabulary. Sometimes, however, we may need more information from the vocabulary |
| source file to available. For example, to know the domain and range of the properties |
| in the vocabulary. If you set the configuration parameter <code>--includeSource</code>, schemagen |
| will:</p> |
| <ul> |
| <li>convert the input vocabulary into string form and include that string form in the |
| generated Java class</li> |
| <li>create a Jena model when the Java vocabulary class is first loaded, and load the string-ified |
| vocabulary into that model</li> |
| <li>attach the generated constants to that model, so that, for example, you can look up the |
| declared domain and range of a property or the declared super-classes of a class.</li> |
| </ul> |
| <p>Note that Java compilers typically impose some limit on the size of a Java source file (or, more |
| specifically, on the size of <code>.class</code> file they will generate. Loading a particularly large |
| vocabulary with <code>--includeSource</code> may risk breaching that limit.</p> |
| <h2 id="using-schemagen-with-maven">Using schemagen with Maven</h2> |
| <p><a href="http://maven.apache.org/">Apache Maven</a> is a build automation tool typically used for Java. You can use <code>exec-maven-plugin</code> and <code>build-helper-maven-plugin</code> to run <code>schemagen</code> as part of the <code>generate-sources</code> goal of your project. The following example shows one way of performing this task. The developer should customize command-line options or use a configuration file instead as needed.</p> |
| <pre><code> <build> |
| <plugins> |
| <plugin> |
| <groupId>org.codehaus.mojo</groupId> |
| <artifactId>exec-maven-plugin</artifactId> |
| <executions> |
| <execution> |
| <phase>generate-sources</phase> |
| <goals> |
| <goal>java</goal> |
| </goals> |
| <configuration> |
| <mainClass>jena.schemagen</mainClass> |
| <commandlineArgs> |
| --inference \ |
| -i ${basedir}/src/main/resources/example.ttl \ |
| -e TTL \ |
| --package org.example.ont \ |
| -o ${project.build.directory}/generated-sources/java \ |
| -n ExampleOnt |
| </commandlineArgs> |
| </configuration> |
| </execution> |
| </executions> |
| </plugin> |
| <plugin> |
| <groupId>org.codehaus.mojo</groupId> |
| <artifactId>build-helper-maven-plugin</artifactId> |
| <executions> |
| <execution> |
| <id>add-source</id> |
| <goals> |
| <goal>add-source</goal> |
| </goals> |
| <configuration> |
| <sources> |
| <source>${project.build.directory}/generated-sources/java</source> |
| </sources> |
| </configuration> |
| </execution> |
| </executions> |
| </plugin> |
| </plugins> |
| </build> |
| </code></pre> |
| <p>At this point you can run <code>mvn generate-sources</code> in your project to cause <code>schemagen</code> to run and create your Java source (note that this goal is run automatically from <code>mvn compile</code> or <code>mvn install</code>, so there really isn’t any reason to run it manually unless you wish to just generate the source). The source file is placed in the maven standard <code>target/generated-sources/java</code> directory, which is added to the project classpath by <code>build-helper-maven-plugin</code>.</p> |
| <h2 id="using-schemagen-with-ant">Using schemagen with Ant</h2> |
| <p><a href="http://ant.apache.org/">Apache Ant</a> is a tool for automating build |
| steps in Java (and other language) projects. For example, it is the |
| tool used to compile the Jena sources to the jena.jar file, and to |
| prepare the Jena distribution prior to download. Although it would |
| be quite possible to create an Ant <em>taskdef</em> to automate the |
| production of Java classes from input vocabularies, we have not yet |
| done this. Nevertheless, it is straightforward to use schemagen |
| from an ant build script, by making use of Ant’s built-in Java |
| task, which can execute an arbitrary Java program.</p> |
| <p>The following example shows a complete ant target definition for |
| generating ExampleVocab.java from example.owl. It ensures that the |
| generation step is only performed when example.owl has been updated |
| more recently than ExampleVocab.java (e.g. if the definitions in |
| the owl file have recently been changed).</p> |
| <pre><code> <!-- properties --> |
| <property name="vocab.dir" value="src/org/example/vocabulary" /> |
| <property name="vocab.template" value="${rdf.dir}/exvocab.rdf" /> |
| <property name="vocab.tool" value="jena.schemagen" /> |
| |
| <!-- Section: vocabulary generation --> |
| <target name="vocabularies" depends="exVocab" /> |
| |
| <target name="exVocab.check"> |
| <uptodate |
| property="exVocab.nobuild" |
| srcFile="${rdf.dir}/example.owl" |
| targetFile="${vocab.dir}/ExampleVocab.java" /> |
| </target> |
| |
| <target name="exVocab" depends="exVocab.check" unless="exVocab.nobuild"> |
| <Java classname="${vocab.tool}" classpathref="classpath" fork="yes"> |
| <arg value="-i" /> |
| <arg value="file:${rdf.dir}/example.owl" /> |
| <arg value="-c" /> |
| <arg value="${vocab.template}" /> |
| <arg value="--classnamesuffix" /> |
| <arg value="Vocab" /> |
| <arg value="--include" /> |
| <arg value="http://example.org/2004/01/services#" /> |
| <arg value="--ontology" /> |
| </Java> |
| </target> |
| </code></pre> |
| <p>Clearly it is up to each developer to find the appropriate balance |
| between options that are specified via the command line options, |
| and those that are specified in the configuration options file |
| (<code>exvocab.rdf</code> in the above example). This is not the only, nor |
| necessarily the “right” way to use schemagen from Ant, but if it |
| points readers in the appropriate direction to produce a custom |
| target for their own application it will have served its purpose.</p> |
| <h2 id="appendix-a-complete-example-configuration-file">Appendix A: Complete example configuration file</h2> |
| <p>The source of this example is provided in the Jena download as |
| <code>etc/schemagen.rdf</code>. For clarity, RDF/XML text is highlighted in |
| blue.</p> |
| <pre><code><?xml version='1.0'?> |
| |
| <!DOCTYPE rdf:RDF [ |
| <!ENTITY jena 'http://jena.hpl.hp.com/'> |
| |
| <!ENTITY rdf 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'> |
| <!ENTITY rdfs 'http://www.w3.org/2000/01/rdf-schema#'> |
| <!ENTITY owl 'http://www.w3.org/2002/07/owl#'> |
| <!ENTITY xsd 'http://www.w3.org/2001/XMLSchema#'> |
| <!ENTITY base '&jena;2003/04/schemagen'> |
| <!ENTITY sgen '&base;#'> |
| ]> |
| |
| <rdf:RDF |
| xmlns:rdf ="&rdf;" |
| xmlns:rdfs ="&rdfs;" |
| xmlns:owl ="&owl;" |
| xmlns:sgen ="&sgen;" |
| xmlns ="&sgen;" |
| xml:base ="&base;" |
| > |
| |
| <!-- |
| Example schemagen configuration for use with jena.schemagen |
| Not all possible options are used in this example, see Javadoc and Howto for full details. |
| |
| Author: Ian Dickinson, mailto:ian.dickinson@hp.com |
| CVs: $Id: schemagen.html,v 1.16 2010-06-11 00:08:23 ian_dickinson Exp $ |
| --> |
| |
| <sgen:Config> |
| <!-- specifies that the source document uses OWL --> |
| <sgen:owl rdf:datatype="&xsd;boolean">true</sgen:owl> |
| |
| <!-- specifies that we want the generated vocab to use OntClass, OntProperty, etc, not Resource and Property --> |
| <sgen:ontology rdf:datatype="&xsd;boolean">true</sgen:ontology> |
| |
| <!-- specifies that we want names mapped to uppercase (as standard Java constants) --> |
| <sgen:uppercase rdf:datatype="&xsd;boolean">true</sgen:uppercase> |
| |
| <!-- append Vocab to class name, so input beer.owl becomes BeerVocab.java --> |
| <sgen:classnamesuffix rdf:datatype="&xsd;string">Vocab</sgen:classnamesuffix> |
| |
| <!-- the Java package that the vocabulary is in --> |
| <sgen:package rdf:datatype="&xsd;string">com.example.vocabulary</sgen:package> |
| |
| <!-- the directory or file to write the results out to --> |
| <sgen:output rdf:datatype="&xsd;string">src/com/example/vocabulary</sgen:output> |
| |
| <!-- the template for the file header --> |
| <sgen:header rdf:datatype="&xsd;string">/***************************************************************************** |
| * Source code information |
| * ----------------------- |
| * Original author Jane Smart, example.com |
| * Author email jane.smart@example.com |
| * Package @package@ |
| * Web site @website@ |
| * Created %date% |
| * Filename $RCSfile: schemagen.html,v $ |
| * Revision $Revision: 1.16 $ |
| * Release status @releaseStatus@ $State: Exp $ |
| * |
| * Last modified on $Date: 2010-06-11 00:08:23 $ |
| * by $Author: ian_dickinson $ |
| * |
| * @copyright@ |
| *****************************************************************************/ |
| |
| |
| // Package |
| /////////////////////////////////////// |
| %package% |
| |
| |
| // Imports |
| /////////////////////////////////////// |
| %imports% |
| |
| |
| |
| /** |
| * Vocabulary definitions from %sourceURI% |
| * @author Auto-generated by schemagen on %date% |
| */</sgen:header> |
| |
| <!-- the template for the file footer (note @footer@ is an Ant-ism, and will not be processed by SchemaGen) --> |
| <sgen:footer rdf:datatype="&xsd;string"> |
| /* |
| @footer@ |
| */ |
| </sgen:footer> |
| |
| <!-- template for extra declarations at the top of the class file --> |
| <sgen:declarations rdf:datatype="&xsd;string"> |
| /** Factory for generating symbols */ |
| private static KsValueFactory s_vf = new DefaultValueFactory(); |
| </sgen:declarations> |
| |
| <!-- template for introducing the properties in the vocabulary --> |
| <sgen:propSection rdf:datatype="&xsd;string"> |
| // Vocabulary properties |
| /////////////////////////// |
| </sgen:propSection> |
| |
| <!-- template for introducing the classes in the vocabulary --> |
| <sgen:classSection rdf:datatype="&xsd;string"> |
| // Vocabulary classes |
| /////////////////////////// |
| </sgen:classSection> |
| |
| <!-- template for introducing the datatypes in the vocabulary --> |
| <sgen:datatypeSection rdf:datatype="&xsd;string"> |
| // Vocabulary datatypes |
| /////////////////////////// |
| </sgen:datatypeSection> |
| |
| <!-- template for introducing the individuals in the vocabulary --> |
| <sgen:individualsSection rdf:datatype="&xsd;string"> |
| // Vocabulary individuals |
| /////////////////////////// |
| </sgen:individualsSection> |
| |
| <!-- template for doing fancy declarations of individuals --> |
| <sgen:individualTemplate rdf:datatype="&xsd;string">public static final KsSymbol %valname% = s_vf.newSymbol( "%valuri%" ); |
| |
| /** Ontology individual corresponding to {@link #%valname%} */ |
| public static final %valclass% _%valname% = m_model.%valcreator%( %valtype%, "%valuri%" ); |
| </sgen:individualTemplate> |
| |
| </sgen:Config> |
| |
| </rdf:RDF> |
| </code></pre> |
| |
| |
| </div> |
| </div> |
| |
| </div> |
| |
| <footer class="footer"> |
| <div class="container" style="font-size:80%" > |
| <p> |
| Copyright © 2011–2022 The Apache Software Foundation, Licensed under the |
| <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>. |
| </p> |
| <p> |
| Apache Jena, Jena, the Apache Jena project logo, Apache and the Apache feather logos are trademarks of |
| The Apache Software Foundation. |
| <br/> |
| <a href="https://privacy.apache.org/policies/privacy-policy-public.html" |
| >Apache Software Foundation Privacy Policy</a>. |
| </p> |
| </div> |
| </footer> |
| |
| |
| <script type="text/javascript"> |
| var link = $('a[href="' + this.location.pathname + '"]'); |
| if (link != undefined) |
| link.parents('li,ul').addClass('active'); |
| </script> |
| |
| </body> |
| </html> |