Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of improving the state machine by Visual Studio DSL?

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Most people do not understand the knowledge points of this article "what is the way to improve the state machine by Visual Studio DSL?", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "what is the way to improve the state machine in Visual Studio DSL?"

Vistual Studio DSL is specially designed by Microsoft for specific domain development, and its main function is to allow developers to design their own graphical tools. DSL is a tool that Microsoft will use for domain-specific development.

First, create a Minimal Lanauge template project according to section 3 and open the DSLDefinition.DSL file:

1. Change the name of the root domain class ExampleModel to StateMachine. At the same time, the check-in relationship ExampleModelHasElements will automatically be renamed to StateMachineHasElements.

two。 Change the attribute name of the domain role Elements on the left side of the domain relationship StateMachineHasElements (Property Name is not to modify the Name-- domain role name) to States. At the same time, the check-in relationship StateMachineHasElements is automatically updated to StateMachineHasStates. You can also click modify directly on the domain role, because the domain role of the diagram shows the attribute name instead of the role name.

3. Modify the renamed domain role StateMachine on the right side of the domain relationship StateMachineHasStates, and change the property Name to State. [note that Name is modified here, not Property Name].

4. Change the Name of domain class ExampleElement to State.

Note that the changes are based on the template project, of course, you can also delete these automatically generated domain classes and re-add them. In addition, you may be a little confused about the attribute name (property name) and the field role name (name) here, please see the previous detailed distinction. In fact, at this point, 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 states.

5. We can see that there is already a reference relationship between State and State in the figure, which is exactly what we want, and we change the relationship StateReferencesTargets to Transition.

6. Modify Targets to the property name Successors and the domain role name to Predecessor.

7. The property name of the modified Sources is Predecessors, and the domain role name is Successor.

Similarly, if it is not based on modification, but to re-add the domain class, let's add some properties to the domain class:

8. Add a domain attribute (DomainProperty) Event,Condition,Action,Label to the domain relationship Transition. All types are string. Now let's take a look at the reference relationship Transition between our DSL, the state machine StateMachine, and the state State.State, that is, the transfer described in our requirements, whose attributes are state machine meta data-event Event, alert condition Condition, and action Action.

Next, we need to add an attribute to the state to represent the state classification in the state machine, whether it is a start state, an end state, or a normal state. Then this property needs to be an enumerated type, so we need to add a custom enumerated type:

9. Open DSL Explorer, right-click on the root node LanguageSm (this represents our DSL), and select add Domain Enumeration.

Select the domain enumeration type you just added, right-select the property, and change Name to StateKind, so that under the Domain Types of the DSL browser, in addition to the general type, we have more StateKind enumeration types. Similarly, we can add other external types (External Type) in the same way for our metadata. We add enumerated values for this enumerated type, right-click to add Enumeration Literal, and add three enumerated values, Normal,Initial,Final, corresponding to 0, 1, and 2. Add a property Kind to our domain class State, and the data type Type selects the StateKind we just added.

Next, we add a new domain class (select Domain Class from the sidebar and drag it to the left domain class area), rename it to Action, which is our metadata "operation" and add two properties of type string Label,Code to the domain class.

Now we need to consider the entry and exit operations mentioned in the metadata. Before entering a state, there can be an entry operation for that state, and when exiting a state, there can be an exit operation. Obviously, between the state and the operation, it should be an embedded relationship rather than a reference relationship, that is, our state can contain zero or more entry operations, zero or more exit operations. So how can our entry and exit operations be represented by the domain class of DSL? If we also describe the state State, is it feasible for an attribute to distinguish between an entry operation and an exit operation? If this is the case, the addition of the operation Action, etc., requires special handling. In addition, if we establish more than zero embedding relationships between the state State and the operation Action, it will cause errors during DSL compilation, which is not allowed by Visual Studio DSL, which will lead to ambiguity. [there will be problems with both domain classes and graphic mapping]. Here we intend to use another feature of DSL, that is, domain class inheritance, to create two new domain classes to represent entry and exit operations, both of which inherit the operation Action:

10. Add two new domain classes, EntryAction and ExitAction, without adding any attributes to them.

11. Establish their inheritance relationship with Action, select Inheritance in the toolbox, select EntryAction first, and then point to the selected Action.

The image above is the finished Action, and we can use Bring Tree Here to simplify the display of domain classes (upper right).

twelve。 Establish the embedding relationship between EntryAction and ExitAction and State, and note that when you select embedding relationship in the toolbox, point to EntryAction. State from State. Notice that the multiplicity on the left is 0... *, the multiplicity on the right is 1. 1. That is to say, there can be no entry operation or exit operation in a state, or there can be more than one. And for each entry operation and exit operation, they can only belong to one state State. Let's now take a look at the entire DSL metadata we completed:

After saving the entire DSL file, we click to convert all templates (Transform All Templates). Visual Studio DSL generates the corresponding C # code with T4 template file according to the metadata in our DSL file. Note that we have to remember to convert the template every time we modify the metadata in the DSL file in order to make the change work. Of course, you can also select a tt file, right-run the customization tool (Run Custom Tool), and generate separately for this file, especially if your DSL file is quite large, which can improve the generation speed. After the conversion is complete, you can recompile the entire solution to see if any errors have occurred. Let's take a look at the DomainClasses.cs file class diagram, as we did in section 5:

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

The above is the content of this article on "what is the method of Visual Studio DSL to improve the state machine". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report