Click here to Skip to main content
15,891,204 members
Articles / Programming Languages / C#

Ontology, Notation 3, and SPARQL

Rate me:
Please Sign up or sign in to vote.
4.91/5 (18 votes)
18 Feb 2011CPOL10 min read 73K   2.2K   32  
Introduction to the concept of ontology, Notation 3, and SPARQL.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using VDS.RDF;
using VDS.RDF.Parsing;
using VDS.RDF.Query;

namespace OntologyCodeProject
{
    static class Program
    {
        private const string getInsomnia = @"
PREFIX my: <http://www.codeproject.com/KB/recipes/n3_notation#>
SELECT ?name
WHERE {
    [ a my:person;
        my:suffers my:insomnia;
        my:name ?name].
}";
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            var parser = new Notation3Parser();
            var graph = new Graph();

            Console.WriteLine("Loading Notation-3 file.");
            parser.Load(graph, @"n3\ontology.n3");
            Console.WriteLine("Loaded Notation-3 file.");
            Console.WriteLine("Nodes:");
            foreach (Triple triple in graph.Triples)
            {
                Console.WriteLine("{0} {1} {2}", GetNodeString(triple.Subject), GetNodeString(triple.Predicate), GetNodeString(triple.Object));
            }
            Console.WriteLine();
            Console.WriteLine("Results of 'insomnia' query:");
            Console.WriteLine();
            SparqlResultSet resultSet = graph.ExecuteQuery(getInsomnia) as SparqlResultSet;
            if (resultSet != null)
            {
                Console.WriteLine("Results for variable 'name':");
                for (int i = 0; i < resultSet.Count; i++)
                {
                    SparqlResult result = resultSet[i];
                    Console.WriteLine("{0}. {1}", i+1, result["name"]);
                }
            }

            Console.ReadLine();
        }

        static string GetNodeString(INode node)
        {
            string s = node.ToString();
            switch (node.NodeType)
            {
                case NodeType.Uri:
                    int lio = s.LastIndexOf('#');
                    if (lio == -1)
                        return s;
                    else
                        return s.Substring(lio + 1);
                case NodeType.Literal:
                    return string.Format("\"{0}\"", s);
                default:
                    return s;
            }
        }
    }
}

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
Software Developer
Poland Poland
My name is Jacek. Currently, I am a Java/kotlin developer. I like C# and Monthy Python's sense of humour.

Comments and Discussions