Warm tip: This article is reproduced from serverfault.com, please click

Owl class rdf in dotnetrdf

发布于 2021-01-25 22:46:18

What is the best way to generate below using dotnetrdf? Assuming I have defined al the namespaces, I am trying to output the following:

<owl:Class rdf:about = "http://my.taxonomies.com/myModel/Lion" >
    <rdfs:label xml:lang = "en"> Lion </ rdfs:label>
    <rdfs:subClassOf> <owl:Class rdf:about = "http://my.taxonomies.com/myModel/Animals" />
    <rdfs:subClassOf>
</owl:Class>

The tutorials I went through did not have an owl class example.

Thanks

Questioner
RayW63
Viewed
0
Kal 2021-01-30 20:00:51

You have two options when creating OWL ontologies in dotNetRDF. You can create a graph and use the Assert methods to assert the triples you want in your ontology graph (this is the low-level API if you like); or you can use the helper classes and methods in the VDS.RDF.Ontology namespace which abstract away some of the steps you need to take when making an ontology graph.

There are docs for the basic operations of the low-level API here, and for the ontology API here

This is an example of creating your graph using the low-level APIs:

var g = new Graph();
// Add namespaces (RDF and RDFS are already declared)
g.NamespaceMap.AddNamespace("owl", UriFactory.Create("http://www.w3.org/2002/07/owl#"));

// Create nodes (this could be done inline in the Assert code if you prefer
var lion = g.CreateUriNode(UriFactory.Create("http://my.taxonomies.com/myModel/Lion"));
var animals = g.CreateUriNode(UriFactory.Create("http://my.taxonomies.com/myModel/Animals"));
var a = g.CreateUriNode("rdf:type");
var owlClass = g.CreateUriNode("owl:Class");
var rdfsLabel = g.CreateUriNode("rdfs:label");
var rdfsSubclassOf = g.CreateUriNode("rdfs:subclassOf");

// Assert triples
g.Assert(lion, a, owlClass);
g.Assert(lion, rdfsLabel, g.CreateLiteralNode("Lion", "en"));
g.Assert(lion, rdfsSubclassOf, animals);

When this graph is serialized as RDF/XML the output you get is:

<?xml version="1.0" encoding="utf-16"?>
<rdf:RDF xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <owl:Class rdf:about="http://my.taxonomies.com/myModel/Lion">
    <rdfs:label xml:lang="en">Lion</rdfs:label>
    <rdfs:subclassOf rdf:resource="http://my.taxonomies.com/myModel/Animals" />
  </owl:Class>
</rdf:RDF>

And this code creates the same graph, using the Ontology helpers:

 var o = new OntologyGraph();
var lion = o.CreateOntologyClass(UriFactory.Create("http://my.taxonomies.com/myModel/Lion"));
lion.AddType(UriFactory.Create(OntologyHelper.OwlClass));
lion.AddLabel("Lion", "en");
var animals = o.CreateOntologyClass(UriFactory.Create("http://my.taxonomies.com/myModel/Animals"));
lion.AddSuperClass(animals);

The RDF/XML generated for this graph is the same as before:

<?xml version="1.0" encoding="utf-16"?>
<rdf:RDF xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <owl:Class rdf:about="http://my.taxonomies.com/myModel/Lion">
    <rdfs:label xml:lang="en">Lion</rdfs:label>
    <rdfs:subClassOf rdf:resource="http://my.taxonomies.com/myModel/Animals" />
  </owl:Class>
</rdf:RDF>

The OntologyGraph class actually extends Graph so you can mix-and-match these modes, using either the low-level or the higher-level APIs on it.