Click here to Skip to main content
15,892,768 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 67K   7.4K   37  
Integration of fuzzy OWL ontology modelling with .NET
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using OntologyIntegration;
using FuzzyFramework.Sets;

namespace AWQuery
{
    public partial class SalesPersonsForm : Form
    {
        public SalesPersonsForm()
        {
            InitializeComponent();
        }

        //Create an ObjectContext instance
        private AdventureWorksEntities.AdventureWorksEntities awContext;
        //Instance of our ontology integrator
        public OntologyIntegrator oi;
        //Collection of employees
        public IEnumerable<AWQuery.SalesPerson> colSalesPersons;

        private void FindSalesPersons()
        {

           
            //Initialize the ObjectContext of the E-R Mapper
            awContext = new AdventureWorksEntities.AdventureWorksEntities();
            
            //Initialize ontology integrator
            oi = new OntologyIntegrator();

            //Load the individuals, and either store in the ontology source RDF/XML file, or just send them to the reasoner.
            //This depends on X in the attribute [Individual(StoreInOntology=X)].
            #region Definition of how the SalesPerson wrapper will be populated by Entity classes

            //Define query which returns all SalesPersons, together with their employee information.
            var salesPersonQuery =
                from s in awContext.SalesPersons
                    .Include("Employee.Contact")


                select new SalesPerson
                {
                    firstName = s.Employee.Contact.FirstName,
                    middleName = s.Employee.Contact.MiddleName,
                    lastName = s.Employee.Contact.LastName,
                    title = s.Employee.Title,
                    salesLastYear = s.SalesLastYear,
                    commissionPct = s.CommissionPct
                };

            #endregion

            //Retrieve employees from the database
            colSalesPersons = salesPersonQuery.AsEnumerable<AWQuery.SalesPerson>();

            //Populate the grid with one employee on each row
            this.dgwSalesPersons.DataSource = colSalesPersons;
            dgwSalesPersons.AllowUserToDeleteRows = false;
            dgwSalesPersons.AllowUserToAddRows = false;
            dgwSalesPersons.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);


            //Register the individuals in the OntologyIntegrator.
            oi.UpdateIndividuals(colSalesPersons);

            //Loads concepts - synchronously
            //oi.UpdateOntology(false, true);
            //bgwUpodateOntology_RunWorkerCompleted(this, null);

            //Load concepts - asynchronously by means of background worker. This is just to display the progress.
            //The synchronous approach above works as well
            bgwUpodateOntology.ProgressChanged += new ProgressChangedEventHandler(bgwUpodateOntology_ProgressChanged);
            bgwUpodateOntology.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgwUpodateOntology_RunWorkerCompleted);
            bgwUpodateOntology.RunWorkerAsync(oi);
            

        }


        void bgwUpodateOntology_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //The progress bar is not necessary any more
            this.prbProgress.Visible = false;
            this.lblProgress.Visible = false;
            
            //FYI This is the concept whom individuals are of our interest:
            //Concept capableAndCheapSalesPerson = oi.GetConceptByUri("http://www.slavicek.net/ontologies/SalesPersons.owl#CapableAndCheapSalesPerson");
            //...but for the illustrative purposes,
            //we display a grid; individuals in rows, membership of individuals per concept in columns
            foreach (Concept concept in oi.Concepts)
            {
                DataGridViewColumn column = new DataGridViewTextBoxColumn();
                column.Name = concept.Name;
                column.DefaultCellStyle.BackColor = Color.LightYellow;

                dgwSalesPersons.Columns.Add(column);

                for (int r = 0; r < dgwSalesPersons.Rows.Count; r++)
                {
                    SalesPerson salesPerson = (SalesPerson)dgwSalesPersons.Rows[r].DataBoundItem;
                    Individual individual = oi.GetIndividualByName(salesPerson.Name);
                    DiscreteSet conceptsOfThisIndividual = (DiscreteSet)individual.Concepts;
                    dgwSalesPersons.Rows[r].Cells[column.Name].Value = conceptsOfThisIndividual.IsMember(concept);
                }
            }
        }

        private void SalesPersonsForm_Shown(object sender, EventArgs e)
        {
            FindSalesPersons();
        }

        #region Background thread process reporting
        /// <summary>
        /// Backround thread progress reporting
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void oi_UpdateOntologyProgressChanged(object sender, UpdateOntologyProgressChangedEventArgs e)
        {
            this.bgwUpodateOntology.ReportProgress(e.ProgressPercentage, e.CurrentOperationDesc);
        }

        /// <summary>
        /// UI thread progress reporting
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void bgwUpodateOntology_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.prbProgress.Value = e.ProgressPercentage;
            this.lblProgress.Text = (string)e.UserState;
        }

        /// <summary>
        /// Background thread - main method
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bgwUpodateOntology_DoWork(object sender, DoWorkEventArgs e)
        {
            oi.UpdateOntologyProgressChanged += new EventHandler<UpdateOntologyProgressChangedEventArgs>(oi_UpdateOntologyProgressChanged);
            oi.UpdateOntology(false, true);
        } 
        #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