Thursday, June 25, 2009

Embedding MGrammar Files Within Your Project

Intellipad is an awesome tool, but if you really want to do some work with Oslo you are probably going to need to write a little code.  This implies that you will need to get your MGrammar definition files into your Visual Studio project.

Once you create and do some testing on your MGrammar definition files within Intellipad save them with the extension .MG and include them within your Visual Studio.NET project.  Next you will need to do a bit of hand-coding on your project file, the easiest way to do this is just to click on “Unload” and the “Edit”.

image image

Once you do that, you will need to modify your project file with the following changes:




and then finally change the action for your .MG file


Here are the actual lines for you to cut-and-paste enjoyment:


<MGrammarLibraryPath Condition="'$(MgCompilerPath)'!=''">$(MgCompilerPath)</MGrammarLibraryPath>
MGrammarLibraryPath Condition="'$(MGrammarLibraryPath)'==''">$(ProgramFilesx86)\Microsoft Oslo\1.0\bin</MGrammarLibraryPath>

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\M\v1.0\Microsoft.M.Embedded.targets" />

Once you’ve done this, you will need to reload your project.

At this point when you compile, your MGrammar should get compiled and included in your project as a resource.  You can verify this by using Reflector.


Now to actually do something with this in your application you need to load the resource.  The following chunk of code will do that for you:

DynamicParser parser = null;

using (var img = MImage.LoadFromResource(System.Reflection.Assembly.GetExecutingAssembly(), ""))
    var factory = img.ParserFactories["Pets.MyPets"
    parser = factory.Create();

Also as a side note, you will need to add the following reference to your project.

using Microsoft.M;
using System.DataFlow;

Both can be found in the <Program Files>\Microsoft Oslo\1.0\bin directory

Now that we have our grammar loaded from a resource into M’s DynamicParser instance we can pass in some text and hopefully get something meaningful out of it.  Stay tuned for an upcoming post…


1 comment:

  1. Very cool bytemaster. Looking forward to your next post.