Installation and start-up
Plugin to Microsoft Visual Studio 2010 is installed by double-clicking on the package with the .VSIX extension, and then follow the instructions. In the case of this project a file is named BielastyAdam.VisualModelForFSharp.DslPackage. Pack looks like Figure 1 below.
Figure 1: Plugin VisualModelForFSharp for Microsoft Visual Studio 2010
The package will be distributed via the Visual Studio Gallery website. Then it will be available to other users via the Extension Manager, which is built-in the tool for Microsoft Visual Studio 2010 that allows you to install plugins. Alternatively, the plugin can be shared on any web page. Users have to download and install it manually.
In the Extension Manager the plugin can be accessed by selecting the tools menu:
Tools->Extension Manager…
Extension Manager window is shown in Figure 2 below. Here you can find the installed plug-ins and see their status. Using the Extension Manager, you can search other plug-ins from Visual Studio Gallery online.
If you can see the plug-in Visual Model For F #, it means that the installation was successful.
To use the functionality offered by this addition, you must create a new F # project. This is shown in Figure 3. To do this, click on the toolbar in the following order.
File-> New-> Project …
You will see the following window. Please select the application F#.
The next step is to create a sister project to the last. To do this properly you need to select the main root of the tree in theSolution Explorer. Click the right mouse button on the branch
Add-> New Project …
and the best is to choose the empty Empty Project project, as it is shown in Figure 4 below.
Our main solution should look now like on Figure 5. We have the F# project, and selected project C#.In the latter models will be created and transformed .tt files from the source code.
It should be now added to the project files so that you can model the functional programs and generate source code.The files responsible for these things add up automatically when you select a project
Add-> New Item …
when you hit the right button on the C# project. You have to do so, because of C# is available to transform text files with .tt extension.
The following window (Fig. 6) shows that among the options we have to choosethe Visual Model For F #. Select this item, enter the name in theName field, such as Model1.vmffs, and clickAdd to add.
After completing the previous steps our solution tree should look like Figure 7 below. New files have been added to the project: Model1.vmffs file is the most important file that can be opened with a visual editor. Just click on it twice. Model1.vmffs.diagram text file stores the character model.Another file is Model1.tt, i.e. to transform a text file. The result transformed model is shown asModel1.fs file, which is a source code file in F#.
Note: If Model1.tt file does not appear automatically in the tree with the project, you must create it manually.To do this, right-click on the name of the project such as OurModelProject and select the new .tt file (Text Template).Then add the following code:
<#@ output extension= ".fs" #> <#@ VisualModelForFSharp processor= "VisualModelForFSharpDirectiveProcessor" requires= "fileName='$fileinputname$.vmffs'" #> <#@ template inherits= "Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation" #> //plugin: Visual Model For F# //author: Adam Bielasty //website: www.bielasty.pl <#= this .RootModel.GenerateCode() #> |
Listing 1: TT Template
In place of $fileinputname$.vmffs insert the name of the file containing the model, e.g.Model1.vmffs.
If you want to have something visible in the Model1.fs file, always after finishing the model, click onthe Transform All Templates button shown in Figure 8 below.After this action all the files with the .tt extension will be transformed according to their definition.
Finally, we copy and paste the following result of transformation or just paste the .fs file to the project application from F#.In this way we can create multiple files with source code F #, from which we can use F# in the project.
The following Figure 9 is presented in the main window of Microsoft Visual Studio 2010 during the functional modeling.The left part is a set of components that can be dragged and dropped to the main space.
Figure 9: Main window of VS 2010 during the modeling
Nested modules – example 1
This example shows the possibilities of the created tool when we model modules. They can be nested within each other, as shown in Figure 1
This model illustrats a number of modules that have a function with the same name. In this case, it is simply a function without arguments that returns anything. You can also call itfield because it takes no arguments. This field does not have a unique name for the entire model, just that it will be unique within the module in which it is located.
At the top level model, which is in RootModel, we have placed an item calledCodeBlock1 CodeBlock. The source code contained in this block is simply a fixed number of value 10. From this element type we can define many relationsLink to Function. This means that every function will be definedFunction1 body of code that resides in the element CodeBlock.
This example uses the functions as elements included in the modules. Other possible to place there elements areTypeElement classes. In this example, they are not used, but they are equally good items to put them in the model as functions.
Below (Fig. 1) shows an example with nested modules and other elements.
For the so defined model we get below (Listing 1) piece of source code in F#. You can see that the elements of the diagram are transformed to the corresponding source code.
Levels of nesting of modules can be identified by looking at the indentation in the code. Generated source is consistent with what was presented and discussed in a previous post on modules.
Using the model, one could easily design a simple hierarchy of modules, focusing more on logic than on the entire program, the F# language syntax. It is an advantage that is achieved through-developed tool.
let Function1 = 10 module ModuleA = let Function1 = 10 module ModuleAA = let Function1 = 10 module ModuleB = let Function1 = 10 module ModuleBA = module ModuleBAA = let Function1 = 10 module ModuleBAB = let Function1 = 10 module ModuleBB = let Function1 = 10
let Function1 = 10 module ModuleA = let Function1 = 10 module ModuleAA = let Function1 = 10 module ModuleB = let Function1 = 10 module ModuleBA = module ModuleBAA = let Function1 = 10 module ModuleBAB = let Function1 = 10 module ModuleBB = let Function1 = 10
Listing 1: The source code generated from the model of Figure 1
Rich Inheritance – Example 2
In this example, a program was modeled (Figure 1), which has a number of classes/types, with the inheritance relationships between them.
The below diagram shows the type called Shape, which has been marked as abstract. This type represents an abstract shape. In this class there is an abstract methodArea, which will return the basic type float. This method will have to be overwritten (called the override) for each of the classes that inherit from the classShape. Environment Microsoft Visual Studio 2010 even by compiling the program will tell you that the Area method should be defined in each class that inherits from the abstractShape element.
Each class has overridden the method Area, and has been used to keywordoverride. In addition, Circle class has defined a property calledpi, which will store an approximation of Pi needed to calculate the surface area.
Each of the three classes, which have derived from the Shape class in some other way calculate a surface area of the figure. The model is defined by three blocks of code namesCodeBlockForCircle, CodeBlockForSquare and CodeBlockForTriangle, which were supplemented with the appropriate code.
The following listing is a visible source code generated on the basis of this model.
The code is properly formatted and suitable for compilation, if you complete the relevant sectionsdo and let. It will be possible to compile the code even when the sectionlet of the class Circle is only supplemented by hand, and sectionsdo are removed everywhere.
You can see that each of the overwritten methods to calculate the surface area have a body, which is consistent with what is contained in the respective blocks of code.
Thanks to the plugin, it’s easy to model the hierarchy of classes. In designing the model, the user focuses on the project and therefore makes less mistakes in writing the source code.
[<AbstractClass>]type Shape () = do //to do abstract member Area : float type Circle (r : float) = inherit Shape() let pi = do //to do override this.Area = pi * r * r type Square (a : float) = inherit Shape() do //to do override this.Area = a * a type Triangle (a : float,h : float) = inherit Shape() do //to do override this.Area = a * h * 0.5
[<AbstractClass>] type Shape () = do //to do abstract member Area : float type Circle (r : float) = inherit Shape() let pi = do //to do override this.Area = pi * r * r type Square (a : float) = inherit Shape() do //to do override this.Area = a * a type Triangle (a : float,h : float) = inherit Shape() do //to do override this.Area = a * h * 0.5
Listing 1: The source code generated from the model of Figure 1
More information you can find on my blog: www.bielasty.pl
If you have interesting ideas on how you candevelop a project, oryou notice any bugs, please contact me.