Skip to content

Commit d20c91e

Browse files
committed
Calculations
1 parent 93880b2 commit d20c91e

2 files changed

Lines changed: 72 additions & 38 deletions

File tree

content/en/docs/apidocs-mxsdk/apidocs/studio-pro-10/extensibility-api/csharp/extensibility-api-howtos/create-microflows-calculations.md

Lines changed: 71 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ weight: 8
77

88
## Introduction
99

10-
This how-to describes how you can create microflows which perform some calculations and return the result.
10+
This how-to describes how to create microflows that perform calculations and return the result.
1111

12-
You can download the example in this how-to in [this GitHub repository](https://github.com/mendix/ExtensionAPI-Samples).
12+
You can download the example in this how-to from [this GitHub repository](https://github.com/mendix/ExtensionAPI-Samples).
1313

1414
## Creating an Extension Class that Creates Microflows
1515

16-
1. Open the project that you previously created when you [created a menu extension](/apidocs-mxsdk/apidocs/csharp-extensibility-api-10/create-menu-extension/).
17-
2. Add a new folder *MicroflowTutorial* to your solution.
16+
1. Open the project you created when following [Create a Menu Extension](/apidocs-mxsdk/apidocs/csharp-extensibility-api-11/create-menu-extension/).
17+
2. Add a new folder named *MicroflowTutorial* to your solution.
1818
3. Create a `MenuExtension` class.
19-
4. Add a new class to the project and call it `CreateMicroflowsMenu.cs`.
20-
5. Replace the code in the file with the code below.
19+
4. Add a new class named *CreateMicroflowsMenu.cs*.
20+
5. Replace the code in the file with the following:
2121

2222
```csharp
2323
using Mendix.StudioPro.ExtensionsAPI.UI.Menu;
@@ -43,11 +43,14 @@ You can download the example in this how-to in [this GitHub repository](https://
4343
}
4444
```
4545

46-
As you can see, the `GetMenus` method is overridden to add your own menus to Studio Pro. The class `CalculationsMicroflowCreator` (which you will add shortly) will be called from the action of your menu. You can see that this class has been injected in the constructor of your menu extension.
46+
The code overrides the `GetMenus` method to add your custom menus into Studio Pro. The `CalculationsMicroflowCreator` class (added in the next step) is injected via the constructor and triggered by the menu action.
4747

48-
## Microflow Creator
48+
## Add the Microflow Creator Class
4949

50-
In this section, you will add the class `CalculationsMicroflowCreator.cs`, which will be injected into your menu extension so that it can be called from your menu action. Make sure to add the `Export` attribute to the class; otherwise, it will not be injected into your menu extension. The `ImportingConstructor` attribute on the constructor is also very important, as this allows the class to receive any required services via dependency injection.
50+
Add the `CalculationsMicroflowCreator.cs` class and follow the steps below:
51+
52+
1. Add the `Export` attribute to allow injection.
53+
2. Add the `ImportingConstructor` attribute to support dependency injection.
5154

5255
```csharp
5356
using Mendix.StudioPro.ExtensionsAPI.Model;
@@ -67,7 +70,11 @@ class CalculationsMicroflowCreator(IMicroflowService microflowService, IMicroflo
6770
}
6871
```
6972

70-
This class contains one public method, which is the one called by your menu. The method `CreateMicroflows` requires the current app as the parameter. The `CreateMicroflowsMenu` extension has access to the `CurrentApp` property, so it will pass it to the method when calling it from the menu action. The `CurrentApp` property is the `IModel` for the app that is currently open in Studio Pro. Every extension that inherits the type `UIExtensionBase` (such as a `MenuBarExtension`) has access to the `CurrentApp` property and can interact and modify the model.
73+
This class includes one public method called `CreateMicroflows`, which is triggered when you click your custom menu. It needs the current app as a parameter to work.
74+
75+
The `CreateMicroflowsMenu` extension can access the current app through the `CurrentApp` property. When the menu is clicked, it passes `CurrentApp` to `CreateMicroflows`.
76+
77+
The `CurrentApp` is the `IModel` that represents the app currently open in Studio Pro. Every extension that inherits from `UIExtensionBase` (like `MenuBarExtension`) has access to `CurrentApp`, which allows it to interact with and change the app's model.
7178

7279
Add the method as follows:
7380

@@ -84,22 +91,32 @@ public void CreateMicroflows(IModel currentApp)
8491
}
8592
```
8693

87-
As you can see, the `CreateMicroflows` method starts a new transaction, by calling `currentApp.StartTransaction`, which is the only way an extension can modify the model of the app. If your class tried to create microflows outside of a transaction, an error will be thrown. For more information, see [Interact with the Model API Using C#](/apidocs-mxsdk/apidocs/interact-with-model-api-10/).
94+
The `CreateMicroflows` method starts a new transaction by calling `currentApp.StartTransaction`, which is required to modify the model. If your class attempts to create microflows outside a transaction, it will result in an error. For more details, see [Interact with the Model API Using C#](/apidocs-mxsdk/apidocs/interact-with-model-api-10/).
8895

89-
`IMicroflowService` enables creating microflows. For details, see [Create a Microflow and Add Activities Using C#](/apidocs-mxsdk/apidocs/csharp-extensibility-api-10/create-microflow-add-activities/). It requires the current model (`IModel`), the containing module or folder inside a module (`IFolderBase`), a name, and an optional `MicroflowReturnValue`. The return value is actually used in the example, so you will see how to create one as well.
96+
Use `IMicroflowService` to create microflows. For more information, see [Create a Microflow and Add Activities Using C#](/apidocs-mxsdk/apidocs/csharp-extensibility-api-10/create-microflow-add-activities/). It requires:
9097

91-
A microflow returns a value with `IMicroflowExpression`. This can be achieved by using your `IMicroflowExpressionService`, which returns an expression from a String input, and set that expression as the microflow's return value.
98+
* `IModel` (the current mode)
99+
* `IFolderBase` (module or folder)
100+
* Microflow name
101+
* `MicroflowReturnValue` (optional)
92102

93-
A very simple `MicroflowReturnValue` can be created as follows:
103+
A microflow returns a value with `IMicroflowExpression`. This can be done by using `IMicroflowExpressionService`, which returns an expression from a string input and sets the expression as the microflow's return value.
104+
105+
An example simple return value is seen below:
94106

95107
```csharp
96108
new MicroflowReturnValue(DataType.Boolean, microflowExpressionService.CreateFromString("true or false"));
97109
```
98110

99111
However, the example will have more complicated expressions, which use parameter names. These parameter names match the return values from called microflows.
100-
This simple extension will create three microflows. Two of them will perform mathematical calculations (multiplication and addition) and each of them will return their result. The other microflow will call these two microflows in sequence, compute their two results (subtract the addition result from the multiplication result), and return true or false if the value is larger than 0.
101112

102-
The method `CreateMicroflowsInFolder` will create the two microflows and the return values. Add the method.
113+
This extension creates three microflows:
114+
115+
1. Multiplication microflow – multiplies integers and returns the result
116+
2. Addition microflow – adds decimals and returns the result
117+
3. Main microflow – Calls the above two microflows in sequence, subtracts the addition result from the multiplication result, and returns `true` or `false` if the value is larger than 0.
118+
119+
Use the `CreateMicroflowsInFolder` method to create the two microflows and their return values:
103120

104121
```csharp
105122
void CreateMicroflowsInFolder(IModel currentApp, IFolderBase folder)
@@ -117,9 +134,9 @@ void CreateMicroflowsInFolder(IModel currentApp, IFolderBase folder)
117134
}
118135
```
119136

120-
To create a microflow which performs a multiplication between two input parameters (two numbers in this case), you can use the code below. As you can see, the String `multiplication1` and the String `multiplication2` match the parameters used in the expression for the return value. Note that for an expression, the dollar sign `$` must be put in front of the parameter name in order to be recognized as a variable input.
137+
### Multiplication Microflow
121138

122-
You can also see that the `DataType` of both parameters is integer.
139+
To create a microflow that performs multiplication between two input parameters, use the code below:
123140

124141
```csharp
125142
void CreateMultiplicationMicroflow(IModel currentApp, IFolderBase folder, IMicroflow callingMicroflow, string outputVariableName)
@@ -142,9 +159,15 @@ void CreateMultiplicationMicroflow(IModel currentApp, IFolderBase folder, IMicro
142159
}
143160
```
144161

162+
The strings `multiplication1` and `multiplication2` match the parameters used in the expression for the return value. Note that for an expression, the dollar sign `$` must be put in front of the parameter name in order to be recognized as a variable input.
163+
164+
You can also see that the `DataType` of both parameters is integer.
165+
145166
{{< figure src="/attachments/apidocs-mxsdk/apidocs/extensibility-api/multiplication-microflow.png" >}}
146167

147-
To create a microflow that performs an addition between two decimal values, you can use the code below. Just like the multiplication microflow example above, you can see that the String `addition1` and the String `addition2` match the parameters used in the expression for the return value. You can also see that their `DataType` is decimal.
168+
### Addition Microflow
169+
170+
To create a microflow that performs addition between two decimal values, use the code below:
148171

149172
```csharp
150173
void CreateAdditionMicroflow(IModel currentApp, IFolderBase folder, IMicroflow callingMicroflow, string outputVariableName)
@@ -167,11 +190,15 @@ void CreateAdditionMicroflow(IModel currentApp, IFolderBase folder, IMicroflow c
167190
}
168191
```
169192

193+
Like the multiplication microflow example above, the strings `addition1` and `addition2` match the parameters used in the expression for the return value. Their `DataType` is decimal.
194+
170195
{{< figure src="/attachments/apidocs-mxsdk/apidocs/extensibility-api/addition-microflow.png" >}}
171196

172-
Once a microflow is created, in order to enable this microflow to be called by other microflows, you need to add a call activity (`IActionActivity`). In the example, you have a method called `CreatMicroflowCallActivity` that can be used by both your multiplication and addition microflows.
197+
## Create Call Activities
198+
199+
After you create a microflow, you need to add a call activity (`IActionActivity`) so it can be used by other microflows. In the example, there is a method called `CreatMicroflowCallActivity` that works for both your multiplication and addition microflows.
173200

174-
There are a few prerequisites that you must complete before a microflow can be called by another microflow. This method can be broken down into parts:
201+
Before one microflow can call another, you need to complete some prerequisites. This method can be broken down into parts:
175202

176203
```csharp
177204
var microflowCallActivity = currentApp.Create<IActionActivity>();
@@ -183,17 +210,15 @@ microflowCallActivity.Action = microflowCallAction;
183210
microflowCallAction.OutputVariableName = outputVariableName;
184211
```
185212

186-
In order to create `IActionActivity`, `IMicroflowCallAction` must also be created, and set as the `Action` property of the `IActionActivity`.
213+
1. Create `IActionActivity`. This is the activity that will call another microflow.
214+
2. Create `IMicroflowCallAction` and set it as the `Action` property of the `IActionActivity`.
215+
3. Create `IMicroflowCall` and set as the `MicroflowCall` property of the `IMicroflowCallAction`.
216+
4. Set `QualifiedName` of the microflow you want to call as the `Microflow` property of the `MicroflowCall` object.
217+
5. Set the `OutputVariableName` on `IActionActivity` so the calling microflow can use the result returned by the called microflow.
187218

188-
Then, for `IMicroflowCallAction`, `IMicroflowCall` must also be created and set as the `MicroflowCall` property of the `IMicroflowCallAction`.
219+
## Passing Parameters to Called Microflows
189220

190-
Next, `QualifiedName` of the microflow, which is to be called by this activity, must be set as the `Microflow` property of the `MicroflowCall` object.
191-
192-
Finally, you can set `OutputVariableName` on `IActionActivity`, which is what the calling microflow will read from the called microflow.
193-
194-
## Passing Parameters
195-
196-
It is also possible to pass a set of parameters to the action activity, which will be the inputs for the called microflow. This set of parameters is a simple `Tuple` of a name and an expression. In the example, these parameters are the two integers for the multiplication microflow and the two decimals for the addition microflow.
221+
You can pass parameters to the action activity, which will be the input for the called microflow. This set of parameters is a `Tuple` of a name and an expression. In the example, these parameters are the two integers for the multiplication microflow and the two decimals for the addition microflow.
197222

198223
```csharp
199224
foreach (var (parameterName, expression) in parameters)
@@ -206,7 +231,7 @@ foreach (var (parameterName, expression) in parameters)
206231
}
207232
```
208233

209-
The method in its entirety is below and can be pasted into your `CalculationsMicroflowCreator` class.
234+
Paste the code into your `CalculationsMicroflowCreator` class.
210235

211236
```csharp
212237
void CreateMicroflowCallActivity(IModel currentApp,
@@ -235,7 +260,7 @@ void CreateMicroflowCallActivity(IModel currentApp,
235260
}
236261
```
237262

238-
To create a call activity for your multiplication and addition microflows, you can use something like the code below. As you can see, the parameter names for the activity match the parameter name from the microflow and their values are also passed in for integers and decimals.
263+
To create a call activity for your multiplication and addition microflows, use code similar to the following:
239264

240265
```csharp
241266
CreateMicroflowCallActivity(currentApp, callingMicroflow, mathMicroflow,
@@ -249,15 +274,24 @@ CreateMicroflowCallActivity(currentApp, callingMicroflow, additionMicroflow,
249274
("addition2", "2.2"));
250275
```
251276

252-
The calling microflow looks as follows:
277+
As you can see, the parameter names for the activity match the parameter name from the microflow. Their values are passed in for integers and decimals.
278+
279+
The calling microflow looks as seen below:
253280

254281
{{< figure src="/attachments/apidocs-mxsdk/apidocs/extensibility-api/main-microflow.png" >}}
255282

256283
## Java Actions
257284

258-
Outside of this calculation examples, you might want to create a microflow activity that calls a Java action file. See below for how to add an activity, and action and a call to the microflow to achieve that. Same as in the previous examples, you have to do this inside a transaction (`IModel.StartTransaction`).
285+
You can create a microflow activity that calls a Java Action. This must be done inside a transaction (`IModel.StartTransaction`).
259286

260-
First, create an `IActionActivity`, just like the calculation example above, but then, its `Action` property will have the type `IJavaActionCallAction` instead of `IMicroflowCallAction`. This `IJavaActionCallAction` will need to know which `IJavaAction` is linked to. You can achieve this by setting the property `JavaAction` on the `IJavaActionCallAction` object to the `IQualifiedName` of the `IJavaAction`. If you are creating a brand new `IJavaAction`, it is important to add it to the module before accessing its `IQualifiedName`. If you have `IJavaAction` already, and you want to set up a call for that one, find it in the app and pass along its `IQualifiedName`. See below for an example.
287+
1. Start a transaction using `IModel.StartTransaction`.
288+
2. Create an `IActionActivity`.
289+
3. Set the `Action` property to `IJavaActionCallAction`.
290+
4. Link `IJavaActionCallAction` to the `IJavaAction`.
291+
5. Set the `JavaAction` property to the `IQualifiedName` of the `IJavaAction`.
292+
6. For `IJavaAction`:
293+
1. For a new `IJavaAction` – add it to the module. Only access its `IQualifiedName` after it has been added.
294+
2. For an existing `IJavaAction` – find it in the app and pass its `IQualifiedName` to the `IJavaAction` property of `IJavaActionCallAction`.
261295

262296
```csharp
263297
public void CreateMicroflowAndJavaAction(IModule module, IModel currentApp)
@@ -283,7 +317,7 @@ public void CreateMicroflowAndJavaAction(IModule module, IModel currentApp)
283317
}
284318
```
285319

286-
If you already have a Java action file that you previously created, simply pass its `IQualifiedName` to the Java action. You will need to query the model in order to retrieve the actual object. You can do so as follows:
320+
If you already previously created a Java action file, you can pass its `IQualifiedName` to the Java action. You will need to query the model to retrieve the actual object. You can do so as follows:
287321

288322
```csharp
289323
IQualifiedName FindJavaAction(string name, IModule module)
@@ -293,4 +327,4 @@ IQualifiedName FindJavaAction(string name, IModule module)
293327
}
294328
```
295329

296-
Download the [whole code](https://github.com/mendix/ExtensionAPI-Samples) to see the way it works in its entirety.
330+
Download the [whole code](https://github.com/mendix/ExtensionAPI-Samples) to see the full implementation.

content/en/docs/apidocs-mxsdk/apidocs/studio-pro-11/extensibility-api/csharp/extensibility-api-howtos/create-microflows-calculations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ You can download the example in this how-to from [this GitHub repository](https:
1616
1. Open the project you created when following [Create a Menu Extension](/apidocs-mxsdk/apidocs/csharp-extensibility-api-11/create-menu-extension/).
1717
2. Add a new folder named *MicroflowTutorial* to your solution.
1818
3. Create a `MenuExtension` class.
19-
4. Add a new class named `CreateMicroflowsMenu.cs`.
19+
4. Add a new class named *CreateMicroflowsMenu.cs*.
2020
5. Replace the code in the file with the code below:
2121

2222
```csharp

0 commit comments

Comments
 (0)