Visual Studio DSL entry 11-add rules for the state machine designer

Source: Internet
Author: User

we have improved the display of the designer in the previous section. In this section, we will go deep into some logic details of the state machine designer, add logical rules to our designer. prepare for generating Code .
before starting, let's take a look at the relationship between several transition attributes:
1. when you edit the event, condition, and Action attributes, the label attributes are automatically calculated and displayed, the calculation logic is event [ condition ]/ action
2. when the attribute label is modified, the values of event, condition, and action can also be automatically updated.
we use the vs.net DSL rules for implementation:
1. add the mcode folder under the DSL project to store our custom code. This is a common practice in DSL development. It is impossible for a strong DSL to meet the requirements without writing any code, generally, you need to combine the generated code and custom code when developing a DSL or using a DSL. in the customcode folder, create a folder named "validation" to store handwritten custom verification classes.
2. add the transitionlabelrule class under the validation folder.
3. modify transitionlabelrule to inherit from changerule, and use the ruleon attribute label to identify the rule applied to the domain relationship transition.

hide row numbers repeat Code ?
    1.   using  Microsoft. visual Studio. modeling; 
    2.  
    3.   namespace  company. languagesm 
    4. {
    5.  [ ruleon  ( typeof  ( transition )] 
    6.   Public sealed class   transitionlabelrule :  changerule  
    7.   {
    8. }
    9. }
    10.  

4. in the rule class, we need to implement the only changerule method elementpropertychanged (elementpropertychangedeventargs e). From this method, we can see that this rule is triggered when some attributes of the Transition change, parameter type: elementpropertychangedeventargs. This parameter contains the current model element modelelement, the edited attribute domainproperty, the original value oldvalue, and the new value newvalue. We only need to judge the current attribute. If it is the preceding event, when condition, action, and lable are modified, the remaining attributes are calculated.

Hide row number Copy code ?
  1. Public override voidElementpropertychanged (ElementpropertychangedeventargsE)
  2. {
  3. TransitionT = E. modelelementAsTransition;
  4. // Compute label when event changes
  5. If(E. domainproperty. ID =Transition. Eventdomainpropertyid)
  6. T. Label = computesummary (E. newvalueAs string, T. condition, T. action );
  7. // Compute label when condition changes
  8. Else if(E. domainproperty. ID =Transition. Conditiondomainpropertyid)
  9. T. Label = computesummary (T. event, E. newvalueAs string, T. action );
  10. // Compute label when action changes
  11. Else if(E. domainproperty. ID =Transition. Actiondomainpropertyid)
  12. T. Label = computesummary (T. event, T. condition, E. newvalueAs string);
  13. // Compute event, condition, action when label changes
  14. Else if(E. domainproperty. ID =Transition. Labeldomainpropertyid)
  15. Computeproperties (E. newvalueAs string, T );
  16. }

Computesummary is our auxiliary method. The value of lable is calculated by event, condition, and action. The computeproperties method matches the values of the other three attributes by the value of lable. finally, you can directly assign values to the attributes of the domain class. these two auxiliary methods are not listed here. You can see them in the download code. (In this case, the entire rule event is committed by default ).
5. re-run the project and you will find that the write rule does not work. When you modify the transition attribute, it does not jump to the transitionlabelrule breakpoint. What is the problem? This is actually related to the rule mechanism of vs.net DSL. We also need to register this rule and add the mongoagesmdomainmodel (domainmodel under generatedcode) under customcode. A partial class generated by TT). In the getcustomdomainmodeltypes method of this class, add the set of custom rule, so that when vs.net is loading, the rule is automatically loaded and added to the rule set.

hide row numbers repeat Code ?
    1.   namespace  company. languagesm 
    2. {
    3.   Public partial class   languagesmdomainmodel  
    4.   {
    5.   protected override   type  [] getcustomdomainmodeltypes () 
    6. {
    7.   return New   type  [] { typeof  ( transitionlabelrule ), };
    8. }
    9. }
    10. }

6. generate all conversion templates. Let's test our rules:
 

Code download

 

Reference resources
1. Development Guide for specific fields of visual stuido DSL tools
2. dsl tools lab http://code.msdn.microsoft.com/DSLToolsLab series tutorial [this series of entry cases of the main reference]

Author: lone knight (like a year of water)
Source: http://lonely7345.cnblogs.com/
The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent andArticleThe original text connection is clearly displayed on the page. Otherwise, the legal liability is retained.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.