Visual Studio DSL entry 8-create a state machine metadata model

Source: Internet
Author: User

In the previous sections, we introduced some basic knowledge about vs.net DSL with the default project we created, including domain classes, domain relationships, graphical symbols, and graphic ing, these things may seem abstract and have no direct relationship with the requirements described in section 2, but these concepts must be mastered before developing our own DSL. If you are not familiar with the requirements of the actual case we are going to do, Please carefully consider the requirements of our state machine. if you read this series for the first time or are not familiar with these basic concepts, we recommend that you take a look at the previous basics.
First, create a minimal lanauge template project according to section 3 to open the dsldefinition. dsl file:
1. Change the name of the root domain class examplemodel to statemachine. At the same time, the checkin relation examplemodelhaselements will be automatically renamed to statemachinehaselements.
2. modify the attribute name of the field role elements on the left side of the statemachinehaselements field to States. statemachinehaselements is automatically updated to statemachinehasstates. [You can also click Modify directly on the domain role, because the graphic domain role displays the attribute name rather than the role name ).
3. Modify the statemachinehasstates domain role that has been renamed to the right of the statemachinehasstates domain, and change the attribute name to state. [Note: the change here is name, not property name].
4. Modify the name of the domain class exampleelement to state.

Note that the changes are made based on the template project. You can also delete these automatically generated domain classes and add them again. in addition, you may be confused about the property name and domain role name. Please refer to the previous detailed distinction.
In fact, at this step, we have completed the DSL description of the metadata between the state machine and the state, and then we will complete the relationship between the state.
5. We can see that there is a reference relationship between the State and the State in the figure. This is exactly what we want. We modify the statereferencestargets to transition.
6. Modify targets to the property name successors and the domain role name predecessor.
7. Modify the sources attribute name to predecessors and the domain role name to successor.

Similarly, it is okay to re-Add the domain class instead of modifying the domain class.
Next, we will add some attributes to the domain class:
8. Right-click the transition field to add the domainproperty event, condition, action, label. The type is string.
Now let's take a look at our DSL, state machine statemachine, and state. there is a reference relationship transition between States, that is, the transfer described in our requirements. Its attribute is the number of state machine elements-event, alert condition, and action.
Next, we also need to add an attribute to the State to indicate the State Classification in the state machine, whether it is the starting state, ending state, or normal state. this attribute must be of the enumeration type. Next we need to add a custom Enumeration type:
9. Open DSL explorer, right-click the root node languagesm (which represents our DSL), and choose add domain enumeration.

Select the added domain Enumeration type, right-click the attribute, and change nameStatekindIn this way, except the common types, the statekind Enumeration type is exceeded in the domain types of the DSL browser. Similarly, we can add other external types in the same way ), for our metadata.
We can add an enumeration value for this enumeration type, right-click to addEnumeration literal,Add three enumerated values: Normal, initial, and final. The values correspond to 0, 1, and 2 respectively. Add an attribute kind for our domain class state. Select the statekind we just added for the data type.
Next, we will add a new domain class (drag the domain class from the toolbar to the domain class area on the left) and change it to action. This is our metadata "operation ", add two string-type attribute labels, code.
Now we need to consider the entry and exit operations mentioned in the metadata. before entering a status, there can be an operation in this status, and an exit operation can be performed when exiting a status, obviously, the status and operation should be an embedded relationship rather than a reference relationship, that is, our status can include zero or multiple entry operations, and zero or multiple exit operations, how can we use the DSL domain class to represent the entry and exit operations? If we use an attribute like the state description to identify whether to enter or exit an operation, is it feasible? In this case, the addition of operation actions requires special processing. In addition, if we establish multiple zero-to-many embedding relationships between State and action, the DSL compilation may fail, which is not allowed by vs.net DSL, this may cause ambiguity. [problems may occur in terms of domain classes and graphic ing].
Here we plan to implement it with another DSL feature, that is, the inheritance of the domain class. We create two new domain classes to indicate the entry and exit operations, they all inherit the operation action:
10. Add two new domain classesEntryactionAndExitaction,You do not need to add any attributes to them.
11. establish their inheritance relationship with the action. Select inheritance in the toolbox, select entryaction, and then point to the selected action.

After the action is completed, we can use the bring tree here to simplify the display of the domain class (the upper right figure ).
12. Establish the embedding relationship between entryaction and exitaction and state. Note that after selecting embedding relationship in the toolbox, point to entryaction from state. Note that the weight on the left is 0... * The weight on the right side is 1 .. 1. That is to say, a status can be either not operated or exited, or multiple. In addition, each entry operation and exit operation can only belong to one State.
Now let's take a look at the complete DSL metadata:

After saving the entire DSL file, we click to convert all templates (Transform all templates ),Vs.net DSL uses the T4 template file to generate the corresponding C # code based on the metadata in our DSL file. Note that every time we modify the metadata in the DSL file, remember to convert the template, make the change take effect. of course, you can also select a TT file and right-click to run the custom tool (run custom tool), which is generated separately for this file, especially when your DSL file is quite large, this improves the generation speed.
After the conversion is complete, you can recompile the entire solution to check whether any errors have occurred. Let's take a look at the domainclasses. CS File class diagram as in Section 5:

As you can see, the domain classes and domain relationships in the metadata are all reflected in the generated code.

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 Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.

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.