Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / Win32

Fuzzy Ontology Framework

Rate me:
Please Sign up or sign in to vote.
4.92/5 (12 votes)
17 Mar 2012CPOL19 min read 66.8K   7.4K   37  
Integration of fuzzy OWL ontology modelling with .NET
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
using FuzzyFramework.Sets;
using FuzzyFramework.Members;
using FuzzyFramework.Dimensions;

namespace OntologyIntegration
{
    /// <summary>
    /// Represents an indiviudal specified by the UpdateIndividual method
    /// </summary>
    public class Individual : IMember
    {
        #region private members
        private IndividualClass _class;
        private object _object;
        private string _name;
        private bool _invalid = true;
        private bool _active = true;
        private Dictionary<string,object> _properties = new Dictionary<string,object>();
        /// <summary>
        /// definition of this individual for the reasoner
        /// </summary>
        private string _reasonerDefinition;
        protected FuzzySet _concepts;
        protected decimal _index = 0;
        #endregion

        #region constructors
        internal Individual(object obj, IndividualClass indClass)
        {
            _object = obj;
            _class = indClass;

            //check if active
            
            _active = indClass.IndividualActiveMember == null || ((bool) GetValue(obj, indClass.IndividualActiveMember));
            
            //find the name
            _name = (string) GetValue(obj, indClass.IndividualNameMember);

            //find the properties
            foreach (MemberInfo memberInfo in indClass.IndividualPropertyMembers)
            {
                object propertyValue = GetValue(obj, memberInfo);
                _properties.Add(memberInfo.Name, propertyValue);
            }
        }

        protected static object GetValue(object obj, MemberInfo memberInfo)
        {
            Exception ex = null;
            if (memberInfo is FieldInfo)
            {
                FieldInfo fi = ((FieldInfo)memberInfo);
                try
                {
                    return fi.GetValue(obj);
                }
                catch (Exception e) { ex = e; }
            }
            else if (memberInfo is PropertyInfo)
            {
                PropertyInfo pi = ((PropertyInfo)memberInfo);
                try
                {
                    return pi.GetValue(obj, null);
                }
                catch (Exception e) { ex = e; }
            }
            else
            {
                throw new OntologyIntegratorException(String.Format("Member {2} of type {0} is not supported by reflection: {1}. FieldInfo or PropertyInfo supported only.", obj.GetType().FullName, memberInfo.GetType().FullName, memberInfo.Name));
            }

            throw new OntologyIntegratorException(String.Format("Invokation of member {0}.{1} failed.", obj.GetType().FullName, memberInfo.Name), ex);

        }

        #endregion

        #region public members
        /// <summary>
        /// Specifies that the ReasonerDefinition kept in this object is invalid, and needs to be re-generated before its use
        /// </summary>
        internal bool IsInvalid
        {
            get { return _invalid; }
        }

        /// <summary>
        /// Invalidates the ReasonerDefinition generated in past. Typically when content of the object has changed.
        /// </summary>
        public void Invalidate()
        {
            _invalid = true;
        }

        /// <summary>
        /// Validates the definition of the inidividual
        /// - Individuals stored in the ontology are validated once their definitions projected to the ontology
        /// - Individuals being sent directly to the reasoner are validated once their ReasonerDefinition property updated
        /// </summary>
        internal void Validate()
        {
            _invalid = false;
        }

        /// <summary>
        /// returns object specifying the class of the individual.
        /// </summary>
        public IndividualClass Class
        {
            get { return _class; }
        }

        /// <summary>
        /// False if the individal is not active any more, and hence should be removed from the collection of individuals being sent to the reasoner.
        /// </summary>
        public bool Active
        {
            get { return _active; }
        }

        /// <summary>
        /// Returns name (i.e. unique identifier without the uri base) of the individal exactly as it was specified by the source .NET object
        /// FYI name is extracted from the class member with attribute IndividualNameAttribute.
        /// </summary>
        public string OriginalName
        {
            get { return _name; }
        }

        /// <summary>
        /// Returns name of the individual in its alphanumerical representation. I.e. all characters from outside [a-zA-Z0-9] are removed.
        /// Please note that this may actually lead to a duplicate names. If there are two different objects with identifiers "üben" and "ben", these will be both considered "ben" in the end.
        /// </summary>
        public string Name
        {
            get { return System.Text.RegularExpressions.Regex.Replace(OriginalName, "[\\W]", ""); }
        }

        /// <summary>
        /// Returns full name of the individual, including the uri base.
        /// </summary>
        public string FullName
        {
            get
            {
                return this.Class.Owner.IndividualsUriBase + "#" + this.Name;
            }
        }

        /// <summary>
        /// Definition of this individual for the reasoner
        /// </summary>
        public string ReasonerDefinition
        {
            get
            {
                if (this.Class.StoreInOntology)
                    throw new OntologyIntegratorException("Class of this individual specifies that the definition of this individual should be stored in the source ontology rather than being sent directly to the reasoner.");

                if (this.IsInvalid)
                    throw new OntologyIntegratorException("Defnition of this individual is not valid.");

                return this._reasonerDefinition;
            }

            internal set
            {
                this._reasonerDefinition = value;
            }
        }

        /// <summary>
        /// Returns fuzzy set of concepts this individual belogns to.
        /// </summary>
        public FuzzySet Concepts
        {
            get
            {
                this.Class.Owner.RefreshMembershipsIfNecessary();
                return _concepts;
            }

            internal set
            {
                _concepts = value;
            }
        }

        /// <summary>
        /// Returns an object this individual serves as a wrapper for.
        /// </summary>
        public object Object
        {
            get
            {
                return _object;
            }
        }

        #endregion


        #region Implementation of the IMember interface
        /// <summary>
        /// Caption
        /// </summary>
        public string Caption { get { return this.Name; } }
        /// <summary>
        /// Dimension
        /// </summary>
        public IDimension Dimension { get { return OntologyIntegrator.INDIVIDUALS; } }
        /// <summary>
        /// Index
        /// </summary>
        public decimal ToDecimal 
        { 
            get { return _index; }
            internal set { _index = value; }
        }

        #endregion

    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Czech Republic Czech Republic
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions