Skip to main content

Configure the gSharp Project

The new solution is set up to compile correctly and the testbed project is set as the startup project, but it cannot run correctly in its current state. It will run, but it will produce an error because it cannot resolve contexts and points. These need to be configured.

Tank Demo​

To illustrate how a gSharp calculation assembly is built and configured, this tutorial uses an over-simplified example of calculating the volume of liquid in a cylindrical tank. As the level of the liquid changes, the volume is calculated as:

volume = Math.PI * Math.Pow(diameter / 2, 2) * level;

You'll use this expression in the C# TankVolume class that was created by the template you used earlier.

TankVolume.cs​

The TankVolume class represents a single gSharp module and is currently the only calculation in your GSharpTankDemo assembly. You'll need to modify it later to actually calculate tank volumes, but for now, open TankVolume.cs and review its contents (comments and using statements have been removed for clarity).

namespace GSharpTankDemo;

public class TankVolume : CalculationModuleAFBase
{
[PointDataDirectionInput]
public AFAttrDoublePoint MyPoint1 { get; set; }

[PointDataDirectionOutput]
public AFAttrDoublePoint MyPoint2 { get; set; }

public override void Initialize()
{
}

public override void Execute(ISchedule schedule, DateTime timestamp, CancellationToken cancelToken)
{
Log.Info("Msg1", "Adding 1 to {PointName}.", MyPoint1.Name);
double point1Value = MyPoint1.Value;
MyPoint2.Value = point1Value + 1;
}

public override void Dispose()
{
}
}

The main points to consider in this class are:

CalculationModuleAFBase Base Class​

The TankVolume class inherits CalculationModuleAFBase. This is necessary because gSharp uses this base class to recognize which classes to import when the assembly is registered and then uses the overridable methods of the class for execution. It also has AF-specific properties and methods that are helpful for interacting with the AF system.

note

A single assembly can contain multiple classes that inherit CalculationModuleAFBase, and each will be imported by gSharp when the assembly is registered. The assembly may contain other classes that do not inherit CalculationModuleAFBase, but they will not be imported.

Module Points​

The points MyPoint1 and MyPoint2 are of type AFAttrDoublePoint. When the assembly is registered, these points will be imported into gSharp as module Points for this module. They represent the input and output data for the calculation and are mapped directly to AF Attributes when resolved to a context.

These points are defined here as public properties, but they can also be fields, with public, protected, or private access modifiers. Public properties are recommended. As long as they are of type AFAttrDoublePoint—or one of the other AFAttrXxxxPoint types—they will be recognized by the gSharp server when the assembly is registered.

Point Data Direction​

The two points MyPoint1 and MyPoint2 are adorned with PointDataDirectionInput and PointDataDirectionOutput attributes, respectively. These attributes indicate to gSharp whether a point is intended for input, output, or both. Both of these attributes can be used for the same point, or the PointDataDirectionBidirectional attribute can be used instead. If no attribute is specified, it is assumed to be bidirectional.

The DataDirection.Output attribute value indicates to gSharp that if the point's value is changed during the calculation and the calculation does not explicitly save the change, gSharp will save the change after the calculation has completed.

When the calculation is executed, gSharp will already have initialized each valid point with its appropriate references according to the specified context, either in gSharp or in the testbed (see below).

Initialize Method​

public override void Initialize()

This method is used for performing any initialization tasks that should be done before the calculation is executed. This method is called once when the module has been instantiated and initialized internally.

By the time this method is called, the module is completely initialized and all pre-assigned properties are assigned. Some of these pre-assigned properties include:

  • Name – The name of the module.
  • Context – A reference to the object that represents the module's context.
  • DataPoints – A collection containing all of the AFDataPoint points defined by the module.
  • StateData – Various properties that indicate the current state of the module.
  • Log – A logger to write log entries to the log.

Execute Method​

public override void Execute(ISchedule schedule, DateTime timestamp, CancellationToken cancelToken)

This method is where the calculation is done. It is called by the gSharp server with the following arguments:

  • ISchedule schedule is information about the schedule that generated the calculation. In the testbed this will be an ad hoc schedule, but in gSharp it represents the actual schedule.
  • DateTime timestamp is the timestamp of the event. Note that this timestamp does not necessarily represent the current time.
  • CancellationToken cancelToken is used to determine if gSharp requires the calculation to stop. This is only important for long-running calculations that either execute a loop or make calls to external libraries that require a cancellation token.

Dispose Method​

public override void Dispose()

This method is used to close or dispose unmanaged resources such as files, streams, and handles held by the module. It can be regarded as an opportunity to perform any tasks that need to be done after a module is shut down.

note

This method is the module's implementation of the standard System.IDisposable interface.

TankVolume Modifications​

The TankVolume.cs class must be modified to calculate the volume of the tanks it represents.

Module Points​

The existing MyPoint1 and MyPoint2 properties don't have any meaning and should be more descriptive as to what they represent.

Rename MyPoint1 to Diameter and, because MyPoint2 is already an output point, rename it to Volume so it can be used as the output point of the calculation.

tip

It's best to use Visual Studio's renaming feature, because these points are used elsewhere in the solution and those references will be updated for you.

You also need to define a second input point, Level, as the other input point of the calculation, and adorn it with [PointDataDirectionInput] so that it is also recognized as an input point.

The resulting points should look like this:

[PointDataDirectionInput]
public AFAttrDoublePoint Diameter { get; set; }

[PointDataDirectionInput]
public AFAttrDoublePoint Level { get; set; }

[PointDataDirectionOutput]
public AFAttrDoublePoint Volume { get; set; }

Additionally, you can specify a default unit of measure for each point using the [PointDataDefaultUom("")] attribute. Specify the unit name or abbreviation as the argument:

[PointDataDefaultUom("cm")]
[PointDataDirectionInput]
public AFAttrDoublePoint Diameter { get; set; }

[PointDataDefaultUom("cm")]
[PointDataDirectionInput]
public AFAttrDoublePoint Level { get; set; }

[PointDataDefaultUom("cubic centimeter")] // cm³ would be tidier but some systems use cm3
[PointDataDirectionOutput]
public AFAttrDoublePoint Volume { get; set; }

If the corresponding AF attribute for a point also has a default unit of measure defined, gSharp will automatically do the unit conversion when reading and writing values.

Execute Method​

The Execute method is where the calculation code is placed and is called each time the calculation is to be run. The default content of the Execute method is created by the Visual Studio gSharp template to serve as a simple example or reminder of some of the kinds of things to do in this method. It is not useful in calculating the volume of a tank and should be removed.

The first step in calculating the volume is to obtain the values of the Diameter and Level points. These point properties automatically contain the current value of the corresponding AF attributes via the Value property, but because the Value property is of type object it has been boxed by .NET.

It might also be helpful, at least at first, to log a message each time the calculation is run. This is done using the Log property of the base class:

Log.Info("CalcMsg", "Calculated the volume of tank '{ContextName}' with a level of {LevelValue} to be {VolumeValue}", Context.ContextName, Level.DataValueToString(), Volume.DataValueToString());

In this logging example, the Log.Info() method is called to log the message at the Info log level for the convenience of this tutorial. Normally you would log this kind of message using the Log.Debug() method so that log messages only appear when the module's log level is switched to Debug.

Modify the Execute method to look something like this:

public override void Execute(ISchedule schedule, DateTime timestamp, CancellationToken cancelToken)
{
// Retrieve the values of the input points
double diameter = Diameter.Value;
double level = Level.Value;

// Calculate the volume
double volume = Math.PI * Math.Pow(diameter / 2, 2) * level;

// Set the calulated volume to the output point
Volume.Value = volume;

// Log the caluclation
Log.Info("CalcMsg", $"Calculated the volume of tank '{Context.ContextName}' with a level of {Level.ValueToString()} to be {Volume.ValueToString()}");
}

Your TankVolume module is now ready to be compiled and tested.