Implement an Activity that Calculates a Logarithm
Implementing a custom activity allows you to build a reusable, completely custom behavior to augment the existing activities that come with VertiGIS Studio Workflow.
In this article, you will learn how to create a custom activity that calculates the logarithm of a number with a given base.
Prerequisites
Extending VertiGIS Studio Workflow for Mobile requires development and deployment of a custom VertiGIS Studio Mobile Application using the VertiGIS Studio Mobile SDK
Follow the instructions in the VertiGIS Studio Mobile SDK page to set up the environment for extending Workflow for VertiGIS Studio Mobile.
A working knowledge of C# and .NET Standard is recommended before extending Workflow for VertiGIS Studio Mobile
Set up the Activity Skeleton
- Create a new file
CalculateLog.cs
in the platform agnostic project of the VertiGIS Studio Mobile Quickstart. - Add a new skeleton workflow activity that implements
IActivityHandler
.
using App1.Workflow;
using VertiGIS.Mobile.Composition;
using VertiGIS.Workflow.Runtime;
using System.Collections.Generic;
using System.Threading.Tasks;
[assembly: Export(typeof(CalculateLog))]
namespace App1.Workflow
{
public class CalculateLog : IActivityHandler
{
public static string Action { get; } = "uuid:<uuid>::CalculateLog";
public Task<IDictionary<string, object?>> Execute(IDictionary<string, object?> inputs, IActivityContext context)
{
return Task.FromResult((IDictionary<string, object?>)new Dictionary<string, object?>());
}
}
}
Register the Activity with the IActivityHandlerFactory
- Create a new file,
ActivityLoader.cs
in the platform agnostic project of the VertiGIS Studio Mobile Quickstart. - Implement the
IActivityHandlerFactory
interface and register the activity skeleton we created in the constructor.
Learn more about activity registration in the activity reference.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using VertiGIS.Mobile.Composition;
using VertiGIS.Workflow.Runtime;
using VertiGIS.Workflow.Runtime.Definition;
using VertiGIS.Workflow.Runtime.Execution;
using App1.Workflow;
[assembly: Export(typeof(ActivityLoader), SingleInstance = true, AsImplementedInterfaces = true)]
namespace App1.Workflow
{
public class ActivityLoader : IActivityHandlerFactory
{
/// <summary>
/// Gets a mapping of action names to implementations of <see cref="IActivityHandler"/>s.
/// </summary>
private Dictionary<string, Func<IActivityHandler>> RegisteredActivities { get; } = new Dictionary<string, Func<IActivityHandler>>();
public ActivityLoader(Func<CalculateLog> calculateLogFactory)
{
RegisteredActivities[CalculateLog.Action] = calculateLogFactory;
}
/// <summary>
/// Creates an <see cref="IActivityHandler"/>.
/// </summary>
/// <param name="action">The name of the action to create.</param>
/// <param name="token">The cancellation token.</param>
/// <param name="inspector">The <see cref="ProgramInspector"/> for the program.</param>
/// <returns>The activity handler for the given action.</returns>
public Task<IActivityHandler> Create(string action, CancellationToken token, ProgramInspector inspector = null)
{
if (action == null || token.IsCancellationRequested)
{
return Task.FromResult<IActivityHandler>(null);
}
if (RegisteredActivities.TryGetValue(action, out Func<IActivityHandler> handlerType))
{
return Task.FromResult(handlerType());
}
else
{
return Task.FromResult<IActivityHandler>(null);
}
}
}
}
Implement the Custom Activity
We now have a empty activity that VertiGIS Studio Mobile can use that takes no inputs and produces no outputs. By changing the inputs, outputs, and execute logic, you can create your own custom activity that calculates the logarithm of a number.
First, let's change the execute logic to parse inputs that make sense for a logarithm activity.
using App1.Workflow;
using VertiGIS.Mobile.Composition;
using VertiGIS.Workflow.Runtime;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
[assembly: Export(typeof(CalculateLog))]
namespace App1.Workflow
{
public class CalculateLog : IActivityHandler
{
public static string Action { get; } = "uuid:<uuid>::CalculateLog";
public Task<IDictionary<string, object?>> Execute(IDictionary<string, object?> inputs, IActivityContext context)
{
double? logBase = null;
if (inputs.ContainsKey("base"))
{
logBase = inputs["base"] as double?;
}
var value = (double)inputs["value"];
return Task.FromResult((IDictionary<string, object?>)new Dictionary<string, object?>());
}
}
}
Next, modify the Execute
method of the activity to calculate the logarithm of a number given an optional base
using App1.Workflow;
using VertiGIS.Mobile.Composition;
using VertiGIS.Workflow.Runtime;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
[assembly: Export(typeof(CalculateLog))]
namespace App1.Workflow
{
public class CalculateLog : IActivityHandler
{
public static string Action { get; } = "uuid:<uuid>::CalculateLog";
public Task<IDictionary<string, object?>> Execute(IDictionary<string, object?> inputs, IActivityContext context)
{
double? logBase = null;
if (inputs.ContainsKey("base"))
{
logBase = inputs["base"] as double?;
}
var value = (double)inputs["value"];
double logResult;
if (logBase != null)
{
logResult = Math.Log(value, (double)logBase);
}
else
{
logResult = Math.Log(value);
}
return Task.FromResult((IDictionary<string, object?>)new Dictionary<string, object?>()
{
["result"] = logResult
});
}
}
}
Test your Activity
Now you can build a workflow for VertiGIS Studio Mobile that uses your new activity!
If you want your custom activity to show up with a friendly user interface in VertiGIS Studio Workflow Designer, check out Registering .NET Activities with VertiGIS Studio Workflow Designer.
The RunActivity
activity can be used to execute your activity by the name defined in CalculateLog.cs
(for this example, uuid:<uuid>::CalculateLog
).
You can
download this demo workflow
that runs the custom activity and
import it into the VertiGIS Studio Workflow Designer.
Next you need to run the workflow you just created in your VertiGIS Studio Mobile SDK project.
You can do this by configuring the layout and app config to run a workflow. You will need to copy the ID of the the workflow you created into the app.json
https://apps.vertigisstudio.com/workflow/designer/#workflow= 44010fc421dd4659b74fb921e09ba594
- App Config
- Layout
- UI
{
"schemaVersion": "1.0",
"items": [
{
"$type": "layout",
"id": "desktop-layout",
"url": "resource://layout-large.xml",
"tags": ["large"]
},
{
"$type": "workflow",
"id": "custom-workflow",
"title": "Custom Workflow",
"target": "#taskbar",
"portalItem": "<your-workflow-id>"
},
{
"$type": "menu",
"id": "iwtm",
"items": [
{
"title": "Run Custom Workflow",
"isEnabled": true,
"iconId": "workflow",
"action": {
"name": "workflow.run",
"arguments": {
"id": "custom-workflow"
}
}
}
]
}
]
}
<?xml version="1.0" encoding="utf-8" ?>
<layout
xmlns="https://geocortex.com/layout/v1"
xmlns:gxm="https://geocortex.com/layout/mobile/v1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://geocortex.com/layout/v1 ../../ViewerSpec/layout/layout-mobile.xsd">
<gxm:taskbar id="taskbar" orientation="vertical">
<map slot="main">
<stack margin="0.8" slot="top-right" halign="end">
<iwtm config="iwtm"/>
</stack>
</map>
</gxm:taskbar>
</layout>
Relevant SDK Sample
Check out the relevant VertiGIS Studio Mobile SDK Sample:
Next Steps
Now you know how to build a basic Workflow activity for VertiGIS Studio Mobile. Next, you can learn how to access app properties in an activity, build custom form elements, use the ArcGIS Runtime SDK for .NET in your activities and more.
Implement a Custom Form Element
Implement a custom form element for applications like VertiGIS Studio Mobile
Add a Callout to the Map through a Custom Activity
Access the map in custom activities for VertiGIS Studio Mobile
Use the ArcGIS Runtime SDK for .NET in an activity.
Use the ArcGIS Runtime SDK for .NET in an activity or form element
Reference other Third Party Libraries
Reference other third party libraries in your activities and form elements