vote up 1 vote down
star
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.

flag
This may require the ontology to be annotated with metadata: semanticoverflow.com/questions/231/… – Andrew Matthews Nov 17 at 2:43

3 Answers

vote up 3 vote down
check

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|flag
Thanks Bill. Sounds like just what I need. – Andrew Matthews Nov 17 at 11:21
vote up 1 vote down

There is a nice post by Alexandra Rusina about Expando object type that is new to .Net 4 at
http://blogs.msdn.com/csharpfaq/archive/2009/10/01/dynamic-in-c-4-0-introducing-the-expandoobject.aspx.
There is also a follow-up post
http://blogs.msdn.com/csharpfaq/archive/2009/10/19/dynamic-in-c-4-0-creating-wrappers-with-dynamicobject.aspx

link|flag
vote up 1 vote down

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|flag

Your Answer

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.