The Wayback Machine - https://web.archive.org/web/20110717160140/http://blog.abodit.com:80/author/ian/

Ian

This user hasn't shared any biographical information

Homepage: http://www.abodit.com


Posts by Ian

Home power meters revisited

In an earlier post I discussed the utility (or otherwise) of the 24 hour power consumption graph and questioned why Google and Microsoft were both investing in this approach to home energy efficiency. Since then both Google and Microsoft have stopped their efforts in this area.


Interestingly, in Europe I’m seeing more and more homes with devices like the one shown here that provide real-time power consumption information. One of the more interesting uses for devices like these is as a check that everything has been turned off when a homeowner is about to leave the house. A quick glance at the meter can reveal if a heater has been left on in a bedroom. Of course the main water heater has the largest impact on the reading but homeowners learn what numbers represent ‘normal’ and can see at a glance when something else has been left on. Clearly a true smart home that can turn devices off when they are no longer in use is still a better long-term solution for this scenario but it’s interesting to see how a fairly simple device can at least provide an indication that everything is off without a significant investment in replacing light switches and device controllers. What would be nicer however would be if the meter included some kind of machine learning so it could show at a glance if the home is in a minimal power state or not.

C# Natural Language Engine connected to Microsoft Dynamics CRM 2011 Online

In an earlier post I discussed some ideas around a Semantic CRM.

Recently I’ve been doing some clean up work on my C# Natural Language Engine and decided to do a quick test connecting it to a real CRM. As you may know from reading my blog, this natural language engine is already heavily used in my home automation system to control lights, sprinklers, HVAC, music and more and to query caller ID logs and other information.

I recently refactored it to use the Autofac dependency injection framework and in the process realized just how close my NLP engine is to ASP.NET MVC 3 in its basic structure and philosophy! To use it you create Controller classes and put action methods in them. Those controller classes use Autofac to get all of the dependencies they may need (services like an email service, a repository, a user service, an HTML email formattting service, …) and then the methods in them represents a specific sentence parse using the various token types that the NLP engine supports. Unlike ASP.NET MVC3 there is no Route registration; the method itself represents the route (i.e. sentence structure) that it used to decide which method to call. Internally my NLP engine has its own code to match incoming words and phrases to tokens and then on to the action methods. In a sense the engine itself is one big dependency injection framework working against the action methods. I sometimes wish ASP.NET MVC 3 had the same route-registration-free approach to designing web applications (but also appreciate all the reasons why it doesn’t).

Another improvement I made recently to the NLP Engine was to develop a connector for the Twilio SMS service. This means that my home automation system can now accept SMS messages as well as all the other communication formats it supports: email, web chat, XMPP chat and direct URL commands. My Twilio connector to NLP supports message splitting and batching so it will buffer up outgoing messages to reach the limit of a single SMS and will send that. This lowers SMS charges and also allows responses that are longer than a single SMS message.

Using this new, improved version of my Natural Language Engine I decided to try connecting it to a CRM. I chose Microsoft Dynamics CRM 2011 and elected to use the strongly-typed, early-bound objects that you can generate for any instance of the CRM service. I added some simple sentences in an NLPRules project that allow you to tell it who you met, and to input some of their details. Unlike a traditional forms-based approach the user can decide what information to enter and what order to enter it in. The Natural Language Engine supports the concept of a conversation and can remember what you were discussing allowing a much more natural style of conversation that some simple rule-based engines and even allowing it to ask questions and get answers from the user.

Here’s a screenshot showing a sample conversation using Google Talk (XMPP/Jabber) and the resulting CRM record in Microsoft CRM 2011 Online. You could have the same conversation over SMS or email. Click to enlarge.

A natural language interface to CRM

Based on my limited testing this looks like another promising area where a truly fluent, conversational-style natural language engine could play a significant role. Note how it understands email addresses, phone numbers and such like and in code these all become strongly typed objects. Where it really excels is in temporal expressions where it can understand things like “who called on a Saturday in May last year?” and can construct an efficient SQL query from that.

Issaquah Triathlon Photos

I had the opportunity to photograph the Issaquah Triathlon today. Here are some pictures from the event:


From Issaquah Triathlon 2011, posted by Ian Mercer on 6/04/2011 (200 items)

Generated by Facebook Photo Fetcher


For more information about the Issaquah Triathlon please visit their Facebook page: http://www.facebook.com/IssaquahTri

SQL Random Order – some unexpected benefits

In this StackOverflow answer I explained the technique used by my home automation system to generate a random ordering for songs during playback.

By using a column called ‘Shuffle’ containing random integer values it becomes easy to generate a repeatable random ordering of the records in a database table. The query to access the table is …

Random random = new Random();
int seed = random.Next();
result = result.OrderBy(s => (~(s.Shuffle & seed)) & (s.Shuffle | seed)); // ^ seed);

Which uses an XOR operation in the database and orders by the results by the result of that operation.

This approach has several advantages as I outlined in my Stackoverflow answer:

Efficient: SQL handles the ordering, no need to fetch the whole table
Repeatable: (good for testing) – can use the same random seed to generate the same random order
Works on most (all?) Entity Framework supported databases

In my home automation system I calculate a new random seed each day and maintain the same seed for the whole day. This means I can play a playlist (actually there are no playlists, only tags, but that’s another story) and reliably get back to the same point in it.

One advantage that I hadn’t considered when I first added this feature is that because the shuffle column is across all songs and because playlists are just tags on songs I can now ensure that you only hear each song once a day even if it occurs in multiple playlists! In fact, I can simply calculate the value of shuffle XOR today’s random seed and then request only songs that are greater than that value making it easy to continue any playlist from any point without having to remember any state other than a single value.

This is the approach used by my home automation system to randomize playlists. It picks a new seed each day giving a consistent order during the day (allowing easy pause / resume capabilities) but a fresh look at each playlist each new day.

Timelapse video using the GoPro HD Hero

This weekend I tried the time lapse mode on my GoPro HD Hero. The weather wasn’t great but the results were fairly impressive. On the 60s interval mode the video was a little bit jerky but on the 10s mode it came out really smooth. The battery life was good enough for several hours of filming but it would have been nice if it was longer.

The other issue was in dealing with the sunset which caused the camera to completely wash out the bright details.

Apart from these minor issues I’m overall still very impressed with the GoPro HD Hero in both video mode and time-lapse mode.

Home Automation Calendar Integration

Calendar Home Automation Integration One feature of my home automation system is Google Calendar integration. What this enables is two things: (i) the house can record what happened on the calendar so I can see at a glance what’s been going on back home and (ii) the ability to put events on the calendar for the house to do certain things.

For example, we recently bought some new baby chicks and they need to be kept warm at light. An infra-red lamp in the chicken house is connected to an X-10 controlled outlet (behind a GFCI, of course since this is outdoors). The device that controls that outlet is called “Chicken lights” but also has several shorter aliases (e.g. “Chickens”). Using the home’s natural language engine that outlet can be controlled remotely by the chat interface, by the email interface, by the voice interface or by the calendar interface.

In this case, a simple recurring event on the calendar to turn the light on every night for 9 hours is sufficient and I can set the recurring event to stop after a few weeks because by then the chicks will no longer need it.

This complete integration of Calendar and Natural Language Engine functionality certainly makes for a very easy way to control and monitor a smart home!

Net result … happy chicks:

Class-free persistence and multiple inheritance in C# with MongoDB

Much as I appreciate Object Relational Mappers and the C# type system there’s a lot of work to do if you just want create and persist a few objects. MongoDB alleviates a lot of that work with its Bson serialization code that converts almost any object into a binary serialized object notation and provides easy round tripping with JSON.

But there’s no getting around the limitations of C# when it comes to multiple inheritance. You can use interfaces to get most of the benefits of multiple inheritance but implementing a tangled set of classes with multiple interfaces on them can lead to a lot of duplicate code.

What if there was a way to do multiple inheritance without every having to write a class? What if we could simply declare a few interfaces and then ask for an object that implements all of them and a way to persist it to disk and get it back? What if we could later take one of those objects and add another interface to it? “Crazy talk” I hear you say!

Well, maybe not so crazy … take a look at the open source project impromptu-interface and you’ll see some of what you’ll need to make this reality. It can take a .NET dynamic object and turn it into an object that implements a specific interface.

Combine that with a simple MongoDB document store and some cunning logic to link the two together and voila, we have persistent objects that can implement any interface dynamically and there’s absolutely no classes in sight anywhere!

Let’s take a look at it in use and then I’ll explain how it works. First, let’s define a few interfaces:

    public interface ILegs
    {
        int Legs { get; set; }
    }

    public interface IMammal
    {
        double BodyTemperatureCelcius { get; set; }
    }

    // Interfaces can use multiple inheritance:

    public interface IHuman: IMammal, ILegs
    {
        string Name { get; set; }
    }

    // We can have interfaces that apply to specific instances of a class: not all humans are carnivores

    public interface ICarnivore
    {
        string Prey { get; set; }
    }

Now let’s take a look at some code to create a few of these new dynamic documents and treat them as implementors of those interfaces. First we need a MongoDB connection:

            MongoServer MongoServer = MongoServer.Create(ConnectionString);
            MongoDatabase mongoDatabase = MongoServer.GetDatabase("Remember", credentials);

Next we grab a collection where we will persist our objects.

            var sampleCollection = mongoDatabase.GetCollection<SimpleDocument>("Sample");

Now we can create some objects adding interfaces to them dynamically and we get to use those strongly typed interfaces to set properties on them.

            var person1 = new SimpleDocument();
            person1.AddLike<IHuman>().Name = "John";
            person1.AddLike<ILegs>().Legs = 2;
            person1.AddLike<ICarniovore>().Prey = "Cattle";
            sampleCollection.Save(person1);

            var monkey1 = new SimpleDocument();
            monkey1.AddLike<IMammal>();                 // mark as a mammal
            monkey1.AddLike<ILegs>().Legs = 2;
            monkey1.AddLike<ICarniovore>().Prey = "Bugs";
            sampleCollection.Save(monkey1);

Yes, that’s it! That’s all we needed to do to create persisted objects that implement any collection of interfaces. Note how the IHuman is also an IMammal because our code will also support inheritance amongst interfaces. We can load them back in from MongoDB and get the strongly typed versions of them by using .AsLike() which returns a value of type T or null if that object doesn’t implement the interface T. But that’s not all, we can even add new interfaces to them later allowing an object to change type over time! Now, of course, you could do a lot of this just with dynamic types but then you lose Intellisense and compile time checking.

So next, let’s take a look at how we can query for objects that support a given interface and how we can get strongly typed objects back from MongoDB:

            var query = Query.EQ("int", typeof(IHuman).Name);
            var humans = sampleCollection.Find(query);

            Console.WriteLine("Examine the raw documents");

            foreach (var doc in humans)
            {
                Console.WriteLine(doc.ToJson());
            }

            Console.WriteLine("Use query results strongly typed");

            foreach (IHuman human in humans.Select(m => m.AsLike<IHuman>()))
            {
                Console.WriteLine(human.Name);
            }

            Console.ReadKey();

So how does this ‘magic’ work? First we need a simple Document class. It can be any old object class, no special requirements. At the moment it does wrap these interface properties up in a document inside it called ‘prop’ making it just a little bit harder to query and index but still fairly easy.

        /// <summary>
        /// A very simple document object
        /// </summary>
        public class SimpleDocument : DynamicObject
        {
            public ObjectId Id { get; set; }

            // All other properties are added dynamically and stored wrapped in another Document
            [BsonElement("prop")]
            protected BsonDocument properties = new BsonDocument();

            /// <summary>
            /// Interfaces that have been added to this object
            /// </summary>
            [BsonElement("int")]
            protected HashSet<string> interfaces = new HashSet<string>();

            /// <summary>
            /// Add support for an interface to this document if it doesn't already have it
            /// </summary>
            public T AddLike<T>()
                where T:class
            {
                interfaces.Add(typeof(T).Name);
                foreach (var @interface in typeof(T).GetInterfaces())
                    interfaces.Add(@interface.Name);
                return Impromptu.ActLike<T>(new Proxy(this.properties));
            }

            /// <summary>
            /// Cast this object to an interface only if it has previously been created as one of that kind
            /// </summary>
            public T AsLike<T>()
                where T : class
            {
                if (!this.interfaces.Contains(typeof(T).Name)) return null;
                else return Impromptu.ActLike<T>(new Proxy(this.properties));
            }

        }

Then we need a simple proxy object to wrap up the properties as a dynamic object that we can feed to Impromptu:

        public class Proxy : DynamicObject
        {
            public BsonDocument document { get; set; }

            public Proxy(BsonDocument document)
            {
                this.document = document;
            }
            public override bool TryGetMember(GetMemberBinder binder, out object result)
            {
                BsonValue res = null;
                this.document.TryGetValue(binder.Name, out res);
                result = res.RawValue;
                return true;            // We always support a member even if we don't have it in the dictionary
            }

            /// <summary>
            /// Set a property (e.g. person1.Name = "Smith")
            /// </summary>
            public override bool TrySetMember(SetMemberBinder binder, object value)
            {
                this.document.Add(binder.Name, BsonValue.Create(value));
                return true;
            }
        }

And that’s it! There is no other code required. Multiple-inheritance and code-free persistent objects are now a reality! All you need to do is design some interfaces and objects spring magically to life and get persisted easily.

[NOTE: This is experimental code: it's a prototype of an idea that's been bugging me for some time as I look at how to meld Semantic Web classes which have multiple inheritance relationships with C# classes (that don't) and with MongoDB's document-centric storage format. Does everything really have to be stored in a triple-store or is there some hybrid where objects can be stored with their properties and triple-store statements can be reserved for more complex relationships? Can we get semantic web objects back as meaningful C# objects with strongly typed properties on them? It's an interesting challenge and this approach appears to have some merit as a way to solve it.]

Finally got the 1U Atom Server racked up

Atom Server and 1U storage arrayThe Atom server I added to my home network is finally installed in the rack and I’ve begun moving storage over to it and off the rather overloaded home automation server. The 1U immediately below it houses 4 SATA drives (mostly 2TB now) with USB/Firewire connections. Readers of my blog will recall my disdain for RAID as a “backup” technology (it’s for availabilitynot backup) so the storage scheme I used is original ->backup -> second backup so there are three copies of everything.

The Atom server is running 64-bit Windows Server and seems surprisingly fast. I plan to run MongoDB on it too.

The ‘Learning Database’ or ‘Why do we need so many different databases?’

It’s 2011 and database management and design is still a tedious job. Software does more amazing things every day but when it comes to databases it’s back to 1970 in many ways.

Scrapy in action
Creative Commons License photo credit: Espen Klem

My ideal database would borrow from RDBMS (like SQL Server), Document databases (like MongoDB), Graph Databases and Semantic Web Triple Stores; it would be the perfect hybrid of all of these and it would configure itself to be as efficient as possible answering queries.

Initially it might start storing data using a triple store format (since that’s one of the simplest forms of database and yet is capable of some of the richest expression of facts), it might use some graph database techniques to improve performance on the triple store. As triples accumulate regarding a specific subject (or object) it would automatically cluster them and use MongoDB-like documents to store the data in a way that can be retrieved efficiently (like I do in my MongoDB triple store). As more data piles up and the important columns become apparent it would switch some of the data to a relational database format and add any necessary indexes. All this would happen totally automatically.

It would of course also include map-reduce, using whatever language you want to use, and it would shard and replicate itself across servers automatically to maximize performance or reliability or whatever you want it to maximize. Advanced semantic reasoning capabilities would also be included so you can ask complex SPARQL queries expressing logic that you simply cannot do with relational databases today.

At this point the DBAs in the audience are freaking out, partly because they don’t have a job any more, but also because they want to ask all the usual questions “what about constraints?”, “what about foreign keys?”, and of course “what about stored procedures?” because they can’t live without that Cobol-esque T-SQL syntax. Well, guess what DBAs, OWL is a much more powerful way of expressing all those constraints. Using OWL and other semantic web technologies you can build complex ontologies that define how the data is represented and the constraints on it. You can even create an Ontology for your rules, and an Ontology for your Ontology for you rules, …. and so on.

I think this is where databases will end up, and I hope that CJ Date (one of the fathers of modern databases) will still be around to see it!

A Semantic Web / Ontology Driven Approach to CRM

Recently I’ve been spending some time working with Microsoft Dynamics CRM 2011. It’s certainly a powerful system with lots of flexibility for customization, but the more time I spend with it staring at endless database tables joined together in myriad ways, and the more time I spend discussing the meaning of different Entities and how they will be used within an organizational content … the more convinced I become that there has to be a better way!

Semantic Web Ontology Driven CRM

The Semantic Web could well provide a better approach than this. For starters the complex data model would be gone – we’d use a triple store and have simple triples for all the data in the CRM. Normally the joins entailed by such an approach would make it prohibitively slow, but having seen all the tables and joins involved in MS-CRM I’m less concerned about that issue! There are also increasingly good triple-stores that can provide very efficient querying capabilities.

A completely open schema would allow users to add data that traditionally gets lumped into the Notes field in a CRM. For example “John likes baseball”.

A semantic approach would allow the organization to create an ontology to describe what they mean by ‘customer’ as opposed to what the CRM vendor meant by ‘customer’. Better yet they would be able to annotate their ontology with those descriptions and additional rules (e.g. constraints expressed in OWL) using the same approach.

A semantic approach would allow the organization to organize entities by much more than a simple object hierarchy. In MS-CRM for example when working with early-bound entities there is no relationship between a Customer and a Lead other than the fact that they are both Entities. Ideally you want to know that each is also a Person and as such will have a First Name and a Last Name etc. In the Semantic Web you aren’t limited to a single-hierarchy like you are in object-oriented languages, instead, you can create multiple ‘is a’ relationships. So, if you want to define that, say, baseball is a sport and soccer is a sport you can add that fact to your Ontology. With that fact in there you can now deduce the fact that “John likes sport” using a semantic reasoner.

A semantic approach would allow for much more powerful querying capabilities. For example, a query like “find all customers who like sports but haven’t been to an event in the last six months and send them an invite to the game on Saturday” could be mostly translated into a SPARQL query.

There’s the odd academic reference to using Ontologies with CRM, e.g. this one but no commercial products that I know of that have taken this approach.

Are there any? and what do you think of the idea? Comments as always greatly appreciated.