Connectors between compartment shape entries with DSL Tools – Version 2

Tags: , , , ,
Add comments

Few months ago I wrote a series of articles and released a library to create connectors between the entries of compartment shapes in Microsoft DSL Tools. In the meantime I found a setting that wasn’t working with my library and now it is time for version 2. I will release this new version as part of JaDAL – Just another DSL-Tools Addon Library on CodePlex.

If you don’t have any idea what I’m talking about please take a look at (at least) the first part of the series, but be careful and DO NOT read the second part! The second part contains obsolete information that will be corrected here. Part 3 and part 4 describe the inside of the library and they are still valid for the new version.

sample

Changes from version 1 to 2

In the original version you have to decide (using the xml file) for the target and source of the connection to be mapped to a compartment or a regular shape. I thought this wouldn’t be an issue since you must know the mapping of your classes. I believed it until I created my DSL with the use of inheritance and some base class that can be mapped to regular or compartment shapes. For this base class I couldn’t create a mapping that goes from a regular or a compartment shape to a regular or compartment shape.

I removed this constraint and in version 2 you needn’t care about it. The library can handle all these cases (even the trivial mapping from regular to regular shape). And even the best: it turned out that removing this constraint made the code much clearer and simpler (both the code of the library itself and the code you have to write). Before this change there were many different cases (first regular to compartment, then compartment to regular, then compartment to compartment and at the end regular to regular which throws an exception) and now everything works the same way.

Lesson learned: If you can do something in an abstract and general way, don’t create different cases and handle only some of them!

And again: the user guide

In the 2nd part of this series I presented a user guide with a step by step walkthrough of creating a DSL using the compartment mapping. I will now create the same DSL with version 2. Most steps are still the same but I repeat them for completeness. The main difference is the xml file format at step 7 and the class you have to write yourself in steps 9 to 11.

You will find this example (and a more advanced one) in the release download over at CodePlex.

  1. You need TTxGen to generate some code from my templates. See my article for more information and a workaround for an installation problem.
  2. I assume you have a DSL solution and at least two Domain Classes mapped to two compartment shapes and two more Domain Classes used as entries of the compartment shapes created. In my example these classes are called Parent1, Entry1, Parent2 and Entry2.
    The entries need a unique identifier. You could use a name property for this, but there will be some difficulties if the user creates two entries in one shape with the same name, so I will use a guid. (Don’t forget to create a new guid in the constructor or another proper place.)
  3. Create a Reference Relationship from Parent1 to Parent2. This will be automatically named Parent1ReferencesParent2. We will use this reference to present the relationship from one entry to another. I would like to create Reference Relationships from this relationship to the entries, but relationships of relationships are not supported. We have to store the guids of the entries in the relationship and add two Domain Properties for this purpose to it. I named them fromEntry and toEntry.
    DslDefinition 
  4. Set the Allows Duplicates property of the Parent1ReferencesParent2 relationship to true.
  5. Set the Is Custom property of the Connection Builder (Parent1ReferencesParent2Builder) in the DSL Explorer to true.
  6. Add a reference to JaDAL.dll to both the Dsl and DslPackage project.
  7. Add a new xml file (in my example CompartmentMappings.xml) to your solution and write the following code in it:
    <?xml version="1.0" encoding="utf-8" ?>
    <CompartmentMappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="CompartmentMappings.xsd"
     namespace="BenjaminSchroeter.CompartmentMapping">
     
      <Connection name="Parent1ReferencesParent2" 
        allowSelfReference="true"
        suppressEntryDeletePropagation="false">
        
        <Source allowHead="false">
          <DomainClass name="Parent1" />
          <EntryDomainClass name="Entry1"/>
        </Source>
        
        <Target allowHead="true">
          <DomainClass name="Parent2"/>
          <EntryDomainClass name="Entry2"/>
        </Target>
        
        <Connector name="Connector"/>
      </Connection>
    
      <CompartmentShape name="CompartmentShape1"/>
      <CompartmentShape name="CompartmentShape2"/>
    </CompartmentMappings>

    (If you place the xsd file in the same directory you get IntelliSense and error checking for free.)

  8. Place the CompartmentMappings.tt file in the same directory, right-click on the xml file and choose Generate xGen Template. A CompartmentMappings_xGen.tt file will be created below the xml file and there you have to add the following lines:
    <#@ ParentFileInjector processor="TTxGenDirectiveProcessor" 
        requires="fileName='CompartmentMappings.xml'" #>
    <#@ include file="CompartmentMappings.tt" #>

    (Be sure you use the right xml file name here.)

  9. Now you have to write some code by yourself. In the generated cs file (CompartmentMappings_xGen.cs) there will be a class named Parent1ReferencesParent2BuilderInstance with three missing methods you have to override. Create a partial class in a new file and implement these methods (see 10 and 11).

  10. Implement the CreateElementLink() method in this file. Here you have to add code to store the selected compartment entries for source and target in the properties of the relationship (see 3).
    protected override ElementLink CreateElementLink
        (Parent1 source, 
         SelectedCompartmentPartType sourcePartType,
         Entry1 sourceEntry, 
         Parent2 target,
         SelectedCompartmentPartType targetPartType, 
         Entry2 targetEntry)
    {
        Parent1ReferencesParent2 result;
        result = new Parent1ReferencesParent2(source, target);
        if(sourcePartType== SelectedCompartmentPartType.Head)
            // use a empty guid for the Head
            result.fromEntry = Guid.Empty; 
        else
            result.fromEntry = sourceEntry.Guid;
    
        if(targetPartType== SelectedCompartmentPartType.Head)
            // use a empty guid for the Head
            result.toEntry = Guid.Empty;
        else
            result.toEntry = targetEntry.Guid;
    
        return result;
    }

  11. In the CreateElementLink() method you have stored the source and target entry information somewhere; my library does not know about this location. So we need two additional methods to compare an entry with a relationship and answer the question "Is this entry the source of a given relationship?" and the same for the target. These code resists in the same file:
    public override bool IsEntryConnectionSource
        (Entry1 entry, 
         Parent1ReferencesParent2 connection)
    {
        if (entry == null)
            return connection.fromEntry.Equals(Guid.Empty);
    
        return connection.fromEntry.Equals(entry.Guid);
    }
    
    public override bool IsEntryConnectionTarget
        (Entry2 entry, 
         Parent1ReferencesParent2 connection)
    {
        if (entry == null)
            return connection.toEntry.Equals(Guid.Empty);
    
        return connection.toEntry.Equals(entry.Guid);
    }

    Always care for entry == null, this will be used to check whether the head of the shape is meant. Even if you do not allow the head to be source or target this method will be called with a null parameter from time to time.

  12. Create a partial class for your Domain Model (CompartmentMappingExampleDomainModel) and add the following methods.
    protected override Type[] GetCustomDomainModelTypes()
    {
      return CompartmentMappingUtil.AllCompartmentMappingRules(this);
    }

    (If you have already some custom rules, you can use an overloaded version of the AllCompartmentMappingRules() method.)

  13. In the DslPackage project create a partial class for the generated Command Set and add the following code:

    protected override IList<MenuCommand> GetMenuCommands()
    {
        return CompartmentMappingUtil.RemoveRerouteCommand
                                    (base.GetMenuCommands());
    }

That’s all. The mapping from compartment entry to compartment entry should be working after compiling the solution.

Advanced Features

As I mention above you can mix compartment and regular shapes. You only need to create a DSL with these shapes and configure the xml file another way. As you see, inside the <Connection> element there is no longer a reference to the diagram elements, only the domain classes are used here. Obviously if you use inheritance all your classes need to have the same base class for <DomainClass> itself and for <EntryDomainClass>. If the concrete class is mapped to a compartment shape, the compartment mappings, otherwise a mapping of regular shapes, will be used.

If one of source or target is always a regular shape, please use ModelElement for <EntryDomainClass>.

At the bottom of the xml file you have to list all shapes that play a role in your mapping. Use the xml elements <CompartmentShape> and <RegularShape> therefore.

Inside your Parent1ReferencesParent2BuilderInstance class you can override some more methods to allow or forbid the creation of connectors. I think the names and signatures speak for themselves:

  • bool CanAcceptAsCompartmentSource
    (SOURCE_ELEMENT candidate,
    SelectedCompartmentPartType partType,
    SOURCE_COMPARTMENT_ENTRY candidateEntry)
  • bool CanAcceptAsCompartmentTarget
    (TARGET_ELEMENT candidate,
    SelectedCompartmentPartType partType,
    TARGET_COMPARTMENT_ENTRY candidateEntry)
  • bool CanAcceptAsCompartmentSourceAndTarget
    (SOURCE_ELEMENT sourceElement,
    SelectedCompartmentPartType sourcePartType,
    SOURCE_COMPARTMENT_ENTRY sourceEntry,
    TARGET_ELEMENT targetElement,
    SelectedCompartmentPartType targetPartType,
    TARGET_COMPARTMENT_ENTRY targetEntry)

Advanced Sample

In addition to the sample above I build a second one. There I want to create mappings form some Inputs to some Outputs, where one of these Inputs or Outputs can be an entry of a compartment shape or a whole regular shape. This regular shape contains a property Kind that makes the shape to an Input or an Output (to show you the use of the CanAcceptAs... methods).

advancedsample

The DSL model looks like this:

advanceddslmodel

The project with all sources is part of the samples in the JaDAL download.

Download

The compartment mapping library and both samples are part of JaDAL – Just another DSL-Tools Addon Library which you can download from CodePlex.

7 Responses to “Connectors between compartment shape entries with DSL Tools – Version 2”

  1. Ticklish Techs » Blog Archive » Connectors between compartment shape entries with DSL Tools - part 2 Says:

    […] This code is now hosted at CodePlex as part of JaDAL. And a follow-up article was […]

  2. Ticklish Techs » Blog Archive » Connectors between compartment shape entries with DSL Tools - part 3 Says:

    […] (2008-05-21): This code is now hosted at CodePlex as part of JaDAL. And a follow-up article was […]

  3. Ticklish Techs » Blog Archive » Finding and removing a default command in DSL Tools (part 4 of Compartment Mappings) Says:

    […] (2008-05-21): This code is now hosted at CodePlex as part of JaDAL. And a follow-up article was […]

  4. Ticklish Techs » Blog Archive » Connectors between compartment shape entries with DSL Tools - part 1 Says:

    […] (2008-05-21): This code is now hosted at CodePlex as part of JaDAL. And a follow-up article was […]

  5. Má»™t số bài há»?c vá»? DSL sÆ°u tầm (luôn cập nhật) « IT Technology Says:

    […] 11. Connectors between compartment shape entries with DSL Tools – Version 2 […]

  6. Mark Says:

    In the requires=”fileName=’CompartmentMappings.xml’” #> your single quotes are not the correct kind.

    Correction: requires=”fileName=’CompartmentMappings.xml'” #>

    The transform will give an error if the single quote is not corrected.

    I suspect these were changed on you when you made the post.

  7. Debty Says:

    Ahaan… I will follow.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in