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 with server side components requires an on-premises installation of VertiGIS Studio Workflow. You will need administrative access to this installation.
Follow the instructions in the VertiGIS Studio Workflow Server page to set up your development environment and VertiGIS Studio Workflow Server extension project.
A working knowledge of C# and .NET Standard is recommended before extending VertiGIS Studio Workflow Server
Set up the Activity Skeleton
- Create a new file
CalculateLog.cs
in the project. - Add a new skeleton workflow activity that implements
IActivityHandler
.
Use a unique prefix on the Action
property to avoid it conflicting with someone else's custom activities.
using VertiGIS.Workflow.Runtime;
using System.Collections.Generic;
using System.Threading.Tasks;
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 an Assembly Attribute
- Create a new file,
Properties\AssemblyInfo.cs
- Add the custom VertiGIS Studio Workflow attribute to
AssemblyInfo.cs
:
[assembly: VertiGIS.Workflow.Runtime.WorkflowActivities]
This registers the assembly to a namespace that VertiGIS Studio Workflow Server can discover.
By convention, assembly attributes are typically added to a file called "Properties\AssemblyInfo.cs"
, but they can be added to any code file.
Implement the Custom Activity
We now have a empty activity that VertiGIS Studio Workflow Server 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 VertiGIS.Workflow.Runtime;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
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)
{
var value = Convert.ToDouble(inputs["value"]);
double? logBase = null;
if (inputs.ContainsKey("base") && inputs["base"] != null)
{
logBase = Convert.ToDouble(inputs["base"]);
}
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 VertiGIS.Workflow.Runtime;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
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)
{
var value = Convert.ToDouble(inputs["value"]);
double? logBase = null;
if (inputs.ContainsKey("base") && inputs["base"] != null)
{
logBase = Convert.ToDouble(inputs["base"]);
}
double logResult;
if (logBase != null)
{
logResult = Math.Log(value, logBase.Value);
}
else
{
logResult = Math.Log(value);
}
return Task.FromResult((IDictionary<string, object?>)new Dictionary<string, object>()
{
["result"] = logResult
});
}
}
}
Deploy the Activity
Follow the instructions to deploy VertiGIS Studio Workflow Server activities.
Test your Activity
Server workflows that run on your VertiGIS Studio Workflow Server deployment can now run this custom activity.
Registering .NET activity stubs provides a user friendly interface for your custom activities in VertiGIS Studio Workflow Designer.
You can run the custom activity by referencing it by action with the RunActivity
activity.
You can
download this demo server workflow
that runs the custom activity and
import it into the VertiGIS Studio Workflow Designer.