Domain Specific Blog

.NET Chat, sort of, but not really, Domain Specific specific

About the author

Author Name is someone.
E-mail me Send mail

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2012

Using ShapeCompartments - What to do and What Not to do

OK, slightly embarrasing this one, but I decided to add it here for a couple of reasons.
  • One, lets face it, only pretend people are reading this (comment to prove me wrong)
  • Two, it :( stumped me for a week, and if I can help just 1 (pretend) person avoid the horrid "No standard web pages containing all your search terms were found" Google page, then my karma will lighten a touch
What I wanted to do, I though would be relatively common for a DSL, but I was struggling to work out how to do it. I needed Model Element (lets call it an Entity for the moment) on my DSL Diagram to have 1 : * properties. I didn't want to have to model all of them as separate Model Elements related to the Entity on the DSL Diagram, but embedded in the Entity itself, with an Add New .. Context Menu option, such as adding class attributes on the Class Diagram in Visual Studio

Adding Properties to a DSL Model Element
I created a 'Property' Domain Class, and assigned it to the Entity Domain Class as an embedded relationship, Transformed Templates, fired up the Hive and hoped that it would 'Just Work'. I didn't hold much hope, and it turned out I had no right to as all I got for my (lazy) troubles was a nice rectangle representing my Entity when I dragged one onto my diagram.
No Properties

As it turned out, it was quite simple, perhaps sematically unintuitive. In fact it was simple enough to allow me to make a complete mess of a few days coding.

What I did was note that there was a Compartment Shape available to use in your model, and assumed it represented, not the whole shape for the Entity, but the indivdual Compartment for my attributes, so I added it to my Domain Class as well as its basic shape, and tried mapping my properties to it.

What not to do!

Now, things began to get a little more confusing, my transformation ran fine, however, when I fired up the Hive, I was met with the following in the generated code:

     // Multiple mappings have been defined for the class Entity.
     // Either implement a method as described below, or remove the multiple mappings from the DSL definition.
     //
     // Method:
     // private DslDiagrams::NodeShape CreateShapeForEntity(Entity newElement)
     //
     {
     //
     }
    // must be implemented in a partial class of Blog1Diagram. Given an instance of Entity,
    // the method should return a new shape or connector instance that should be associated with this element. If no shape or connector should be created, the method should return null.
    DslDiagrams::NodeShape newShape = CreateShapeForEntity((global::Pelion.Blog1.Entity)element);

At this point, in my defense, I was beginning to suspect that I was missing something fairly important (and most likely obvious), and did attempt to RTFM (in an MSDN sense, something fairly basic and confusing at this early stage of DSL Tools), and to WTFV (watch the training videos), none of which touched on this element of modelling. The sample code in the VS SDK did seem to point to the fact that I was on the correct track, but I didn't pay to much attention to them at first.

Instead I tried to code a new shape, something I am sure would have worked, by my results were laughable. I won't go through them here.

Anyhow, to cut a painful story short, I fired up the SDK samples again, and had that 'I've lost how many days?' sinking feeling when I realised the answer was, of course, obvious. The CompartmentShape defines the Domain Class's shape and all its compartments, and what the confusing message in the code was telling me was that I had defined 2 shapes, and which one did I want.

I deleted the non-Compartment shape, added a compartment and associated the Entity Property to it.
 
The correct set up

I hope this has helped at least one pretend reader on their way.

My properties

Currently rated 3.1 by 16 people

  • Currently 3.125/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: DSL
Posted by john on Thursday, May 22, 2008 4:48 PM
Permalink | Comments (1) | Post RSSRSS comment feed

Flag Domain Enumerations

This is a nice one I discovered this morning, by setting the Flag Property of an enumeration, enumeration bitwise values and an editor box are supplied by default.

Consider we have an property, for the sake of this example I'll use Font, that uses an enumeration to define its state, with possible values of Bold, Underlined, Italic etc.

Where an owning object of such a property, like Pen, may have be in multiple states concurrerntly (eg, a Bold, Underlined state), we could define each property as a boolean, to say that property is in affect or not, or we could use bitwise shifting of an enumeration.  To do this we would define our enumeration as having set, bitshifted, values:

public enum FontProperty 
{
    Bold = 1 << 0, // 1 bit shifted 0 position to the left, therefore 0001 = 1
    Underlined = 1 << 1, // 1 bit shifted 1 position to the left, therefore 0010 = 2
    Italic = 1 << 2, // 1 bit shifted 2 positions to the left, therefore 0100 = 4
    Strikethrough = 1 << 3, // 1 bit shifted 3 positions to the left, therefore 0100 = 8  

 

We could then set our object state as follows:

Pen pen = new Pen();
pen.Font = FontProperty.Bold | Underlined

This would effectively set the Font property (confusingly, sorry, of type FontProperty) to be 3, or 0011, the amalgamation of 0001 and 0010, which is basic binary addition, a nice way of describing multiple states. This can be translated back as shown below:

//Test for Bold
if ((FontProperty &  FontProperty.Bold) != 0)
{
    //Font is bold


We can do a similar thing in a DSL definition: 


Define a Domain Enumeration (essentially just an enumeration like any other in .NET that is created in context of a DSL Definition) from the DSL explorer

 
Set the Enumeration Name property, and the flag property to True

 
Create the enumeration values 

 
Note, the bitwise integer value of the enumeration has been automatically set

Add a Domain Property to your owning Domain Class with your enumeration as its type

Now, when the property is used in the generated DSL, a checked box editor window has been automatically provided, and the correct bitwise value set 

Currently rated 3.0 by 15 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: DSL
Posted by john on Wednesday, May 14, 2008 7:16 AM
Permalink | Comments (0) | Post RSSRSS comment feed