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
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.
This is very helpful indeed Kal. What output would you expect to get with the sequence you suggested? When I am mixing/matching rdfs with owl like you suggest, the output transforms rdfs:label to owl:label and rdfs:subClassOf to owl:subClassOf. Is this expected? Thanks
Hi @RayW63, I missed out one step in the OntologyGraph example that sets the type of the Lion resource to owl:Class. I've updated the answer with that bit of code and also the output that I get when running this code in a simple command-line app. As you will see, the resources and properties there are in the correct namespaces. From what you say it sounds like possibly you have got an incorrect namespace mapping in your graph - it would be worth checking that you are setting the owl namespace to the correct URI.