Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Simple Ontology Support for C#

0.00/5 (No votes)
11 Jul 2007 1  
Extending the class hierarchy with semantic information

Introduction

The class hierarchy of an object-oriented programming language represents a static ontology. It defines a terminology for entities, their attributes and relations. Problems arise when an application needs different classifications at runtime. This may lead to ambiguities because of multiple inheritance or hacks that are needed to implement this feature.

This article describes an approach that extends the existing class information by additional ontologies to provide rich and dynamic semantic information. A case study is presented, that shows the flexibility and added value of the concept and how it can be used. In addition, this method integrates well with the object-oriented design process.

Background

Gruber (1993) defines an Ontology as 'an explicit specification of a conceptualization', i.e. a view onto a system is described by a standardized terminology and relations between entities. In many cases, there is no single ontology but many different and concurrent ones (cmp. Hesse (2002)).

A class hierarchy of an object-oriented programming language, e.g. C#, defines a static Ontology; it describes a view onto a system in terms of objects, attributes and relationships. The following figure shows an example:

A sample class hierarchy.

The example is taken from an implementation of a bond graph. A bond graph consists of different types of nodes and bonds between them. The problem here is, that different algorithms require a special view onto the nodes, e.g. sometimes the nodes are classified by "type" and sometimes by their number of "ports". The SE and SF nodes, for example, can be classified as: by-type as sources and by-port as single ports.

Due to the lack of support of multiple inheritance in C#, it is not possible to implement this concurrent classification. Interfaces are a kind of solution but this would lose much valuable information.

The idea is to extend the class hierarchy by ontologies and to cross-reference the entities:

Ontology and Class Hierarchy.

Depending on the used ontology, a SE node is either a Source or a SinglePort. The advantage is that the ambiguity of the father-child relationship does not lead to a conflict and a single (clean) class hierarchy is maintained.

Using the code

Ontologies are implemented through the E56.Ontology library, which provides classes for semantic annotations of existing C# classes. SemanticType is the counterpart of System.Type and the base for the extension. A semantic type is virtual if no C# class with the same name exists, otherwise this class is cross-referenced. SemanticObject extends System.Object. To use a class in an ontology, it must be derived from SemanticObject.

An Example:

  class Element : SemanticObject { ... }
  class Node : Element { ... }
  class SE : Node { ... }

Different Ontologies can be loaded through:

  SemanticObject.Parse("by-port.xml");
  SemanticObject.Parse("by-type.xml");

Now, we can use the additional information:

  // Create an instance of a class

  SF sf = new SF();
  // Zugriff auf dessen semantischen Typs

  SemanticType root = sf.SemanticType;
  // Using the schema 'by-port' sf is a...

  SemanticType st1 = sf.IsA("by-port");
  Console.WriteLine("'by-port': {0} is a {1}", root, st1);
  // Using the schema 'by-typet' sf is a...

  SemanticType st2 = sf.IsA("by-type");
  Console.WriteLine("'by-type': {0} is a {1}", root, st2);

Through the SemanticType property, semantic type information can be accessed. This is implemented as a lookup of the C# type within the Ontology. The method IsA(string schema) traverses the hierarchy and returns the father object. The result is:

  'by-port': [SF/BondGraphs.Core.SF] is a [SinglePort/virtual]
  'by-type': [SF/BondGraphs.Core.SF] is a [Source/virtual]

Technical information

The implementation is based on the System.Reflection API, that allows access to classes, methods, and even variables at runtime. The ontology itself is described in a simple XML format, which will be replaced by OWL. The Web Ontology Language (OWL) is a W3C standard for specifying semantic information in a machine readable way.

The by-type schema looks like this:

<?xml version="1.0" ?>
<schema namespace="example">
  <object name="Element" />
  <object name="Node" isA="Element" />
  <object name="Source" isA="Node" />
  <object name="SF" isA="Source" />
  <object name="SE" isA="Source" />
</schema>

References

  • Gruber, T. R. (1993). A Translation Approach to Portable Ontology Specifications. Knowledge Acquisition, 5(2):199�220.
  • Hesse, W. (2002). Ontologie(n). Informatik Spektrum, 25(6):477�480.
  • W3C (2004). Web Ontology Language. http://www.w3.org/2004/OWL/, accessed 11 July 2007.

History

  • 11 July 2007 - Initial version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here