1
1

Channel 9 (a Microsoft community site) has an interesting video, by Rob Bagby, about the use of dynamic language capabilities in C# to expose XML elements as though they were object properties. I'd like to do the same for a C# class representing an OWL class. I could then expose more Properties on my object as more of it's OWL properties become known.

Is there a commonly accepted way of doing this in the dynamic language community? Have you done this, or do you know of a framework that does it? What would be the programming idiom for working with code that uses dynamic properties like this? This is a new feature in C# so there are no common idioms for working with properties that may or may not exist.

asked 17 Nov '09, 02:26

Andrew's gravatar image

Andrew ♦♦
1.5k1413
accept rate: 26%

edited 17 Nov '09, 02:40

This may require the ontology to be annotated with metadata: http://www.semanticoverflow.com/questions/231/is-there-an-ontology-to-link-to-and-annotate-owl-ontologies

(17 Nov '09, 02:43) Andrew ♦♦ Andrew's gravatar image

Every object in Ruby has a method called 'method_missing' which is called as a last resort if the interpreter can't find any declaration of a method call on an object.

The implementation of ActiveRecord in Ruby on Rails makes heavy use of this to dynamically create methods according to some conventions it defines. This includes automatically creating methods corresponding to names of columns in database tables for subclasses of ActiveRecord.

I don't know what is included in the C# dynamic language features, but this type of approach might be of some use to you.

This explains some of it: http://weblog.jamisbuck.org/2006/12/1/under-the-hood-activerecord-base-find-part-3

Eyal Oren and colleagues from DERI applied some of these ideas to develop ActiveRDF http://www.activerdf.org/ I'm not familiar with the details of how they have implemented this, but I'm sure it would be worth a look.

link

answered 17 Nov '09, 07:53

billroberts's gravatar image

billroberts
81639
accept rate: 47%

Thanks Bill. Sounds like just what I need.

(17 Nov '09, 11:21) Andrew ♦♦ Andrew's gravatar image
link

answered 18 Nov '09, 11:35

ViktorZ's gravatar image

ViktorZ
275148
accept rate: 0%

Disclaimer: this code is not optimized in any way. I am absolutely open to suggestion of any kind. As a matter of fact, I am keen on learning the best way to work with semantic data, but so far I have not found solid code which will help convince my colleagues.

This code is an adaptation of the code in this blogpost. It basically converts a Json object into an dynamic object which can then be easily queried. Since Freebase returns Json I found this extension method very helpful. You will need the Json.Net library which can be downloaded from codeplex.

public static class JTokenExtensions
{
    public static object ToDynamicObject(this JToken token)
    {
        if (token is JValue)
        {
            return ((JValue)token).Value;
        }

        if (token is JObject)
        {
            var expando = new ExpandoObject();

            var query = from childToken in (token) 
                        where childToken is JProperty 
                        select childToken as JProperty;

            foreach (var property in query.ToList())
            {
                var propertyName = string.Format("{0}{1}", char.ToUpper(property.Name[0]), property.Name.Substring(1));
                ((IDictionary<string, object>)expando).Add(propertyName, property.Value.ToDynamicObject());
            }

            return expando;
        }

        if ((token is JArray))
        {
            return token.Select(t => t.ToDynamicObject()).ToList();
        }

        throw new ArgumentException(string.Format("Unknown token type '{0}'", token.GetType()), "token");
    }
}

Example usage:

var json = SomeMethodThatGetsJsonFromFreebase();
var jsonObject = JObject.Parse(json);
dynamic response = jsonObject.ToDynamicObject();

var query = from a in (response.A0.Result.Album as List<dynamic>)
            where a.Name.ToUpperInvariant().StartsWith("A")
            select a;

foreach (var album in query)
{
    Console.Out.WriteLine(album.Name);
}
link

answered 27 Dec '09, 17:12

Florian%20Hoornaar's gravatar image

Florian Hoor...
8515
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×593
×13
×1

Asked: 17 Nov '09, 02:26

Seen: 4,770 times

Last updated: 27 Dec '09, 17:12