Skip to main content

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

Important

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.

note

A working knowledge of C# and .NET Standard is recommended before extending VertiGIS Studio Workflow Server

Set up the Activity Skeleton

  1. Create a new file CalculateLog.cs in the project.
  2. Add a new skeleton workflow activity that implements IActivityHandler.
tip

Use a unique prefix on the Action property to avoid it conflicting with someone else's custom activities.

YourProjectName/CalculateLog.cs
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

  1. Create a new file, Properties\AssemblyInfo.cs
  2. Add the custom VertiGIS Studio Workflow attribute to AssemblyInfo.cs:
YourProjectName/Properties/AssemblyInfo.cs
[assembly: VertiGIS.Workflow.Runtime.WorkflowActivities]`

This registers the assembly to a namespace that VertiGIS Studio Workflow Server can discover.

note

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.

YourProjectName/CalculateLog.cs
using VertiGIS.Workflow.Runtime;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace App1.Workflow
{
public class CalculateLog : IActivityHandler
{
public const string Action = "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

YourProjectName/CalculateLog.cs
using VertiGIS.Workflow.Runtime;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace App1.Workflow
{
public class CalculateLog : IActivityHandler
{
public const string Action = "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
});
}
}
}

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.

tip

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.