Programmatically generating OWL class hierarchy wi

2019-04-15 01:57发布

问题:

I want to programmatically generate an ontology using OWL by supplying a vector. My goal is to be able to open the produced OWL file in Protégé and make use of Jena.

Input Vector
The vector which i want to pass:
[[layer, network layer, data link layer, physical layer], [network, computer network], [data link], [ontology, ontology extraction]].


Expected Output

The output should have the following tree-like hierarchy structure:

layer
  -network layer
  -data link layer
  -physical layer
network
  -computer network
ontology
  -ontology extraction
data link

The hierarchical structure, where network layer is below layer and so on, is significantly important.

This is an example of the file I want to generate:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <owl:Class rdf:about="#network"/>
  <owl:Class rdf:about="#ontology"/>
  <owl:Class rdf:about="#physical_layer">
    <rdfs:subClassOf>
      <owl:Class rdf:about="#layer"/>
    </rdfs:subClassOf>
  </owl:Class>
  <owl:Class rdf:about="#data_link_layer">
    <rdfs:subClassOf rdf:resource="#layer"/>
  </owl:Class>
  <owl:Class rdf:about="#network_layer">
    <rdfs:subClassOf rdf:resource="#layer"/>
  </owl:Class>
  <owl:Class rdf:about="#computer_network">
    <rdfs:subClassOf rdf:resource="#network"/>
  </owl:Class>
  <owl:Class rdf:about="#ontology_extraction">
    <rdfs:subClassOf rdf:resource="#ontology"/>
  </owl:Class>
</rdf:RDF>

回答1:

Your question isn't very clear (see comment, above) so I'm going to take a guess that you want to programmatically create a class hierarchy. The outline code for doing this using Jena would be:

OntModel m = ... your model ... ;
NS = "http://your.domain/example#";

// define the various classes

OntClass layer = m.createClass( NS + "Layer" );
layer.setLabel( "layer", "en" );

OntClass networkLayer = m.createClass( NS + "NetworkLayer" );
layer.setLabel( "network layer", "en" );
// ...

// create the class hierarchy

layer.addSubClass( networkLayer );
// ...

// save the file
FileWriter out = null;
try {
    out = new FileWriter( "./test.owl" );
    m.write( out, "RDF/XML-ABBREV" );
}
finally {
    if (out != null) {
        try {out.close()) ) catch (IOException ignore) {}
    }
}