Click here to Skip to main content
15,892,674 members
Articles / Programming Languages / C#

Simple Ontology Support for C#

Rate me:
Please Sign up or sign in to vote.
4.17/5 (11 votes)
11 Jul 20073 min read 67.8K   1.7K   47  
Extending the class hierarchy with semantic information
// Ontology Library
// (C)Copyright 2007 by martin.faust@e56.de
//
//  This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Xml;

namespace E56.Ontology {

	/// <summary>Type information</summary>
	public class SemanticType {
		/// <summary>Type information</summary>
		private Type type;
		/// <summary>Virtual type?</summary>
		private bool isVirtual;
		/// <summary>The name</summary>
		private string name;
		/// <summary>The relationships</summary> 
		private Hashtable isA;
		
		/// <summary>The name</summary>
		public string Name { get { return name; }}
		/// <summary>Is this a virtual type?</summary>
		public bool IsVirtual { get { return isVirtual; }}
		/// <summary>Type information, if not virtual</summary>
		public Type Type { get { return type; }}
		
		/// <summary>Constructor</summary>
		public SemanticType(string _name, Type _type) {
			name = _name;
			type = _type;
			isVirtual = (type == null);
			isA = new Hashtable();
		}
		
		/// <summary>Returns a classification based on a given schema</summary>
		public SemanticType IsA(string schema) {
			return isA[schema] as SemanticType;
		}
		
		public void Add(string schema, SemanticType st) {
			isA[schema] = st;
		}
		
		/// <summary>Returns a string representation of this class</summary>
		public override string ToString() {
			return string.Format("[{0}/{1}]", name, (isVirtual?"virtual":type.FullName));
		}
	}
	
	/// <summary>The ontology based extension of oa simple object</summary>
	public class SemanticObject {
		/// <summary>Loaded schemas</summary>
		private static Hashtable schemas = new Hashtable();
		/// <summary>Entity information</summary>
		private static Hashtable ontology = new Hashtable();
		/// <summary>Get/Set semantic type information</summary>
		public SemanticType SemanticType { get { return ontology[this.GetType().Name] as SemanticType; }}
				
		/// <summary>Returns a classification based on a given schema</summary>
		public SemanticType IsA(string schema) {
			SemanticType st = ontology[this.GetType().Name] as SemanticType;
			return st.IsA(schema);
		}
		
		/// <summary>Is a given schema available?</summary>
		public static bool Available(string schema) {
			return schemas.ContainsKey(schema);
		}
		
		/// <summary>Read a classification scheme</summary>
		public static void Parse(string url) {
			string schema = Path.GetFileNameWithoutExtension(url);
			string _namespace = null;

			// Was the schema already loaded?
			if (schemas.ContainsKey(schema))
				return;
								
			XmlReader reader = new XmlTextReader(url); 			
			while (reader.Read()) {
				switch (reader.NodeType) {
				case XmlNodeType.Element:
					switch(reader.Name) {
					case "schema": // <schema namespace="x">
						{
							_namespace = reader.GetAttribute("namespace"); 
						} break;
					case "object": // <object name="x" [isA="y"] /> 
						{ 							
							SemanticType parent = null;	
							string name = reader.GetAttribute("name"); 
							string isA = reader.GetAttribute("isA"); 
							
							SemanticType st = ontology[name] as SemanticType;
							if (st != null) {
								if (isA != null) parent = ontology[isA] as SemanticType;
								if (parent != null) st.Add(schema, parent);
							}
							else {
								if (isA != null) parent = ontology[isA] as SemanticType;
								
								Assembly x = Assembly.GetCallingAssembly();
								Type type = x.GetType(_namespace + "." + name);
								st = new SemanticType(name, type);
								if (parent != null) st.Add(schema, parent);
								ontology[name] = st;
							}
						} break;
					default: 
						break;
					}
					break;
				case XmlNodeType.EndElement:
					break;
				case XmlNodeType.Text:
					break;
				default: break;
				}
			}
			schemas[schema] = true;
		}
	}	
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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


Written By
Architect
Germany Germany
Utilizing my experience of industry-scale real-time graphics programming, design, development of software, as well as European research projects I want to bring in new ideas for creative projects.
Companies:
- 2009-now BTC AG: software for the renewable energy sector.
- 2007-2009 Digital Media: Audio, Graphics and GIS Web Services(http://maps.bremen.de)
- 2001-2007 artecLab://art/work/technology: Mixed Reality, Computer Games and eLearning
- 1998-2001 STN ATLAS Elektronik: real-time graphics for ground warfare simulation

For a complete resume see http://e56.de/download/resume.pdf

Comments and Discussions