SyntaxHighlighter

Saturday, April 3, 2010

The road to F#

Yesterday I got called to a meeting to talk about a prototype which I can't speak of.
However, I can talk about one requirement which is to read information from a ms project file. Things like tasks and resources. I've never parsed a ms project file before but I figured it would be similar to any other office object model using yucky COM underneath. So I fired up VS2010 and did a file -> new project on the F# node and started down the normal path of adding a reference to the ms project library blah blah blah.
Then I had to launch ms project 2007 and load a project file, and then I could go back to FSI and start to write some code. After about 2 min of hacking, here's what I got

#r @"C:\Windows\assembly\GAC\Microsoft.Office.Interop.MSProject\12.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.MSProject.dll"

open Microsoft.Office.Interop.MSProject

let projectapp = new ApplicationClass()
let project = projectapp.ActiveProject
for t in project.Tasks do
    printfn "%A" t.Name

Not to shabby. However, I did have to open project and load a .mpp file (which is the project file format).
This is not a big deal since the app requires it, but I thought to myself... what if it did not require it?
So I started searching around for a library or just information the file format itself to see if I could build something to parse it directly. After about 10 min I found mpxj, this library is originally written in Java and then they wrote a .NET library on top of it. So .NET interops with this library. Best part is... you can work with many versions of MS project and you don't need to have it installed. You can point to the file load it up and pull out almost any data you wish. I thought it might sound to good to be true, so I downloaded it and tried to use it in F#. After 20 min of hacking and reading the documentation... I came up with this.

#r @"local path to this file ->\mpxj.dll"
#r @"local path to this file ->\IKVM.OpenJDK.Core.dll"
#r @"local path to this file ->\IKVM.OpenJDK.Util.dll"
#r @"local path to this file ->\IKVM.OpenJDK.Beans.dll"
#r @"local path to this file ->\IKVM.OpenJDK.Charsets.dll"
#r @"local path to this file ->\IKVM.OpenJDK.jdbc.dll"
#r @"local path to this file ->\IKVM.OpenJDK.Misc.dll"
#r @"local path to this file ->\IKVM.OpenJDK.Security.dll"
#r @"local path to this file ->\IKVM.OpenJDK.SwingAWT.dll"
#r @"local path to this file ->\IKVM.OpenJDK.Text.dll"
#r @"local path to this file ->\IKVM.OpenJDK.XML.API.dll"
#r @"local path to this file ->\IKVM.OpenJDK.XML.Bind.dll"
#r @"local path to this file ->\IKVM.OpenJDK.XML.Parse.dll"
#r @"local path to this file ->\IKVM.Runtime.dll"
#r @"local path to this file ->\poi-3.6-20091214.dll"


open System
open net.sf.mpxj
open net.sf.mpxj.reader
open net.sf.mpxj.writer

let filepath = "b4ubuild_sample_07.mpp"
let preader = ProjectReaderUtility.getProjectReader(filepath)
let pfile = preader.read(filepath)
let ta = pfile.getAllTasks()
let resources = pfile.getAllResources()

for i = 0 to ta.size() do
    let t = (ta.get(i) :?> Task) //down casting in F# (taking an obj and casting it to typeof(Task))
    let taskName = t.getName()  
    Console.WriteLine(taskName)

for i = 0 to resources.size() do
    let r = (resources.get(i) :?> Resource)
    let rname = r.getName()
    Console.WriteLine(rname)


Now that's more like it. I've officially decided to invest in F#.  The last time I made a big investment like this was around 2005 when I first started playing with WPF. :-)

Friday, March 19, 2010

The functional programming journey

For the last year and half I've been playing around with F#. Accomplishing little task like simple File I/O, Winform UI Development, standard calculations, etc. Doing all of this because I have a F# book and a few web resources. So I figured that I would finally break down and do what the experts say I should do... which is to learn another functional programming language. A few weeks ago I went looking on I Tunes-U for some functional programming lectures. To my surprise I found quite a bit of lectures. UC Berkley & Stanford lectures stood out the most so I tried the Stanford lectures. Programming Paradigms by Jerry Cain is a great series of lectures covering C/C++/Java/LISP/Python/networking and more. I chose to jump into the LISP section of the series. After reviewing the lectures it gave me a fresh perspective on F#, and suddenly F# made a lot more sense in my head. Now I'm able to do thing like interface with WPF from F#, I wrote my own F# library and leveraged it in a C# project I'm working on, and the more I learn about core functional fundamentals the more I learn about F#. I guess I just needed a deeper understanding in order to wrap my mind around F# and functional programming in general. And I must say, the more I realize what the F# compiler is doing behind the scenes the more I appreciate it. Just yesterday I was writing some c# code in a project and thought to
myself "I wish I was in F# right now, because I'd be done with this task if I was". :-)
What does that mean exactly... should I make the transition to functional programming? Sometimes I think I could. I'd just need to beef up on my math skills, and I'd be all set. I'll save that topic for another day. :-)

Tuesday, April 14, 2009

Brilliant!!!!

(Udi Dahan) Make Roles Explicit!!!
I happen to catch this talk on my lunch break today. And it's simply brilliant. For some it may seem obvious, but for me... the light bulb definitely went off. Lately, I've been going through the design pattern books again... even took out Refactoring by Martin Fowler for review. Even though they make sense I've always had trouble implementing it in a brown field project. I'm currently faced with a few projects at work right now. One is a major project that I'm maintaining, and the other is actually a green field project that I'm also working on. It's exciting because it's a WPF application that hosts a 3D Model viewer made by the folks over at Autodesk.
It's COM but it's still cool. My job is to parse the 3D model for data and surface it in WPF.
Their are a lot of moving parts... such as... XLinq,Xceed Datagrid, LINQ2SQL, SQL, custom DWF parser (to parse DWF files), 3rd party COM API (Autodesk), Log4net, IOC, MVP, etc.
So it certainly can get quite confusing quite quickly. Having an over arching strategy to help manage complexity is key. After watching the video, it became clear and now I'm continuing the green field project in this specific manner. Make Roles Explicit!!!!

Thanks Udi,
-Develop with passion

Monday, March 16, 2009

Functional Programming Is In Me!

The next book I'm going to buy will be Don Syme's Expert F#.
This guy is like the Anders Hejlsberg of C#. He even brought generics to C#, and since he created F# in MSR (Microsoft Research in Cambridge) I figured he's the best person to learn from.

This past weekend was supposed to be the weekend to brush up on the latest version of the Micro Framework (3.0) so that I can finally start building the medical device/application for monitoring my heart levels, and Download the latest CTP of F# and start writing some basic applications with it. Unfortunately, the wife wanted to go to IKEA and buy some hard wood flooring for her office/ballet studio. Since we consider ourselves to be fairly savy IKEA DIY people... we decided to give it a try. Man did I under estimate that project... not only did it take all weekend, but I had to buy new power tools at the local Home Depot. It took all day and night on Saturday plus Sunday morning to complete the room. I'm never doing that again. However, the floor turned out great and the wife is happy to dance on hardwood instead of carpet. :-)

Unfortunately, it wasn't until 2am on Sunday before I was able to get to the Micro Framework updates. Still the same though, some API changes but mostly the same. I just need to find a wearable device that I can port the framework too. At the moment I'm simulating as much as I can through the emulator... so far that's been a good experience and it's very similar to the compact framework stuff. As for F#.... well... after I saw this video I had to immediately download the bits and start playing around. I have to say... that I'm digging F#. It's worth playing with it just for the interactive window in VS2008 alone. To be able to write code, highlight it, and run it with out building or compiling is cool!

I need to run down to B&N after I get off work and pick up the book for more in depth fun. :-)

-Develop with Passion

Wednesday, February 11, 2009

Object Databases in .NET

I'm probably going to get a lot of push back on this... especially from DB guys I know( Douglas, Thom, Glenn, you know who you are). :-)
Anyway, I've recently discovered db4.0... this is a really good object database implementation.
So good... that is supports T-SQL and LINQ. It's file based but the speed is impressive. Now... I'm not saying that this is finally the end of databases, but it's a good start for any small apps like mine that don't need the overhead of a powerful db platform like SQL/Oracle. I think it's wise to use a technology like this for small persistence scenarios... for instance, in my day job I deal with lots of calculations, estimating, etc. Quite a few of the calculations are calculated on the fly which initially takes a while depending on the state that the user left the application. So instead of dismissing persistence in this scenario and paying the cost for calculating rows and rows of data, I can store it all in a light weight, 1 .dll, 1 file, database of calculated values.


Below is an attempt to give you a better idea with out having to download the free stuff. :-)

using System;
using System.IO;

using Db4objects.Db4o.Query;
using Db4objects.Db4o.Tutorial;

namespace Db40Examples
{
public class Example1
{
public static void Main(string[] args)
{
IObjectContainer db = Db4oFactory.OpenFile("Path to filename here");
try
{
StoreFirstPilot(db); //create
StoreSecondPilot(db); //create
RetrieveAllPilots(db); //read
RetrievePilotByName(db); //read
RetrievePilotByExactPoints(db); //read
UpdatePilot(db); //update
DeleteFirstPilotByName(db); //delete
DeleteSecondPilotByName(db); //delete

}
finally
{
db.Close();
}
}

public static void AccessDb4o()
{
IObjectContainer db = Db4oFactory.OpenFile("Path to filename here");
try
{
// do something with db4o
}
finally
{
db.Close();
}
}

public static void StoreFirstPilot(IObjectContainer db)
{
Pilot pilot1 = new Pilot("Michael Schumacher", 100);
db.Store(pilot1);
Console.WriteLine("Stored {0}", pilot1);
}

public static void StoreSecondPilot(IObjectContainer db)
{
Pilot pilot2 = new Pilot("Rubens Barrichello", 99);
db.Store(pilot2);
Console.WriteLine("Stored {0}", pilot2);
}

public static void RetrieveAllPilotQBE(IObjectContainer db)
{
Pilot proto = new Pilot(null, 0);
IObjectSet result = db.QueryByExample(proto);
ListResult(result);
}

public static void RetrieveAllPilots(IObjectContainer db)
{
IObjectSet result = db.QueryByExample(typeof(Pilot));
ListResult(result);
}

public static void RetrievePilotByName(IObjectContainer db)
{
Pilot proto = new Pilot("Michael Schumacher", 0);
IObjectSet result = db.QueryByExample(proto);
ListResult(result);
}

public static void RetrievePilotByExactPoints(IObjectContainer db)
{
Pilot proto = new Pilot(null, 100);
IObjectSet result = db.QueryByExample(proto);
ListResult(result);
}

public static void UpdatePilot(IObjectContainer db)
{
IObjectSet result = db.QueryByExample(new Pilot("Michael Schumacher", 0));
Pilot found = (Pilot)result.Next();
found.AddPoints(11);
db.Store(found);
Console.WriteLine("Added 11 points for {0}", found);
RetrieveAllPilots(db);
}

public static void DeleteFirstPilotByName(IObjectContainer db)
{
IObjectSet result = db.QueryByExample(new Pilot("Michael Schumacher", 0));
Pilot found = (Pilot)result.Next();
db.Delete(found);
Console.WriteLine("Deleted {0}", found);
RetrieveAllPilots(db);
}

public static void DeleteSecondPilotByName(IObjectContainer db)
{
IObjectSet result = db.QueryByExample(new Pilot("Rubens Barrichello", 0));
Pilot found = (Pilot)result.Next();
db.Delete(found);
Console.WriteLine("Deleted {0}", found);
RetrieveAllPilots(db);
}

Of course no example would be complete with out LINQ....


public static void StoreObjects(IObjectContainer db)
{
db.Store(new Car("Ferrari", (new Pilot("Michael Schumacher", 100))));
db.Store(new Car("BMW", (new Pilot("Rubens Barrichello", 99))));
}

public static void RetrievePilot(IObjectContainer db)
{
IEnumerable result = from Pilot p in db
where p.Name.StartsWith("Michael")
select p;
ListResult(result);
}

public static void RetrievePilotByCars(IObjectContainer db)
{
IEnumerable result = from Car c in db
where c.Model.StartsWith("F")
&& (c.Pilot.Points > 99 && c.Pilot.Points <150)
select c.Pilot;
ListResult(result);
}

public static void RetrievePilotUnoptimized(IObjectContainer db)
{
IEnumerable result = from Pilot p in db
where (p.Points - 81) == p.Name.Length
select p;
ListResult(result);
}



So far it's proven to be a quick way to get persistence up and going in my app with out all the worrying about "is my schema right?" kind of questions in my head. It's definitely going to be something I'll try to incorporate in to my unit tests in the future.

-Develop With Passion

Gearing up for Mix09

It's official!!! I'm heading to Mix09 in Las Vegas next month. Luckily, I was invited by a blue badger. Since this is my first Microsoft Conference ever (outside the road shows and free msdn events) I'm definitely going to live it up. Get swag, get some questions answered and come back to Seattle with some new tricks and better approaches. Since I've been heavily involved with Silverlight2 and WPF I've been getting the old designer/animator bug in me again. :-)
It's nice to break out my old 3D stuff and Flash stuff and refresh the skills enough to reuse them in a .net application. I honestly never thought I'd type that, but it's true. The latest application that I'm working on is for a High School in Eastern Washington(as I mentioned in a previous post). The app is entirely silverlight2 and c# and is heavy on the animation side. I wanted to build an IPhone like experience for the application. Since it needs to appeal to teenagers, I figure eye candy like that had to be done. So far it's working out(for the most part). I'm constantly second guessing myself on the transitions from one screen to the next. I want full control over this, so that I can ease in and out anyway I want. Thus far I've been using Nikhil code base as a guide, but I still think there should be a better way. So hopefully I'll learn something about that at mix.
Also... the backend story of the application is not traditional by any means. I'll speak more on this in a later post, but the concept of object databases (at least in my mind)has taken on a whole new meaning.

-Develop With Passion

Monday, January 19, 2009

What's up with this economy?

The economic issues have certainly had an effect on me. I've been dev'n like there's no tomorrow. I've done many projects both small and large lately(Silverlight2 & WPF) that have had a tremendous impact on my career and family. In keeping with the last post I'm still very much in full swing to getting my business off the ground. I've changed the name which reflects more of what services I offer and less on products that I've built (classic rookie mistake), and I've used quite a bit of earnings to put back in to the business ( productivity tools, frameworks, and new hardware).

Currently, I've scaled way back on projects due to a new gig I recently received last year.
It's a great company so far... I've been loaded with a new Dell Precision m6400 quad core/8 gigs of ram notebook. I've been brought on to lead them in to the next generation of end 2 end construction project estimation software. I'll be leading yet another port/re-write of a 2.0 Winform in to a 3.5 WPF application . This one is already turning into a whopper of project. The design is out of wac in a lot places, there's a massive amount of coupling due to the developer building the app as if it's one big API. It sort of resembles the old View/Mediator pattern which is so opposite to the way things are designed now. Sigh...

On the flip side of that I'm building a online training module for a friend of the family.
She's on a board of directors for a high school in eastern Washington, so the module needs to appeal to teens. Of course it's going to be in Silverlight2. :-)

Hopefully that will lead to funding which will allow me to market it to other schools (hopefully).
First things first though... I gotta get a killer UI for it. That will come from a kick ass design studio I found(and worked a silverlight gig for) in Seattle. They will build the UI and I will make it come to life. :-)

Until next time (not two months from now)
-Develop with passion