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 ?
-
using Microsoft. visual Studio. modeling;
-
-
namespace company. languagesm
-
{
-
[ ruleon ( typeof ( transition )]
-
Public sealed class transitionlabelrule : changerule
-
{
-
}
-
}
-
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 ?
Public override voidElementpropertychanged (ElementpropertychangedeventargsE)
-
{
-
TransitionT = E. modelelementAsTransition;
-
-
// Compute label when event changes
-
If(E. domainproperty. ID =Transition. Eventdomainpropertyid)
-
T. Label = computesummary (E. newvalueAs string, T. condition, T. action );
-
// Compute label when condition changes
-
Else if(E. domainproperty. ID =Transition. Conditiondomainpropertyid)
-
T. Label = computesummary (T. event, E. newvalueAs string, T. action );
-
-
// Compute label when action changes
-
Else if(E. domainproperty. ID =Transition. Actiondomainpropertyid)
T. Label = computesummary (T. event, T. condition, E. newvalueAs string);
-
-
// Compute event, condition, action when label changes
-
Else if(E. domainproperty. ID =Transition. Labeldomainpropertyid)
-
Computeproperties (E. newvalueAs string, T );
-
-
}
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 ?
-
namespace company. languagesm
-
{
-
Public partial class languagesmdomainmodel
-
{
-
protected override type [] getcustomdomainmodeltypes ()
-
{
-
return New type [] { typeof ( transitionlabelrule ), };
-
}
-
}
-
}
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.