- 27 Jul 2022
- 3 Minutes to read
- Print
- DarkLight
SDK: Rule Steps (Advanced)
- Updated on 27 Jul 2022
- 3 Minutes to read
- Print
- DarkLight
Overview
Creating advanced Rule steps allows developers to further control all aspects of the Rule being constructed. Advanced Rule steps can allow a developer to create enhancements for their users like creating custom design-time validation messages to enhance the designer's ability to appropriately configure a Rule.
Example A
This example will demonstrate how to create an advanced Rule step.
- Create a public class and decorate it with the AutoRegisterRuleStep attribute.AutoRegisterRuleStep Properties
Property Description string name Sets the name of the Rule which will show up in the Rule Designer System.Type anchorType Sets the type the Rule will mainly interact with. This property is used to limit the Rules that are shown in the Rule Designer based on which anchor data is selected for a Rule statement. For example, if this value is set to int32, the Rule will not show up as a possible choice in the Designer if selecting a string as anchor data. bool anchorTypeIsList Similar to the anchorType property, this property is used to limit which Rules show in the Rule Designer based on the anchor data selected. However, this property limits the Rules shown based on whether or not the anchor data is or isn't an array. params string[] categories Sets a category in which the Rule will be displayed in the Rule Designer [AutoRegisterRuleStep("String Has Value", typeof(string), false, "Sample SDK Rules")] public class SampleRule : AbstractRuleStep
The decorated class will show in the Designer as follows: - Configure the class to inherit from AbstractRuleStep. This interface provides a default implementation for all the methods of the IRuleStep interface. You can customize these default implementations by overriding them. The first method you will want to override is the Run method. The default implementation of this will always evaluate false. The following code snippet shows how to override the Run method to evaluate if the anchor data is not null or empty.
public override bool Run(RuleStepExecutionData data) { return (data.Data[this.AnchorData.Name] != null && data.Data[this.AnchorData.Name] != string.Empty); }
When used in the Rule Designer, the Rule will be displayed as below when configured to evaluate Flow Data.BaseURLToPortal as the anchor data. - Provide the Rule a verb to be used by the Designer to describe in sentence form what the Rule is going to do by overriding the GetVerbInfo method.
public override string GetVerbInfo(IInputMapping[] mappings) { return "Has Value"; }
The Designer will display the following from this configuration: - The Rule has no input data because it is only evaluating whether or not the anchor data has a value. For cases where the Rule has input data, override the GetValueInfo method to format how this input data appears in the text of the Rule. The below code snippet shows an example of input data and how to format the data to show up in the Rule Designer.For more information on adding input data to a Rule, see SDK: Consuming Data (Advanced).
public override DataDescription[] InputData { get { return new DataDescription[] { new DataDescription(new DecisionsNativeType(typeof(string)), "value"), }; } } public override string GetValueInfo(IInputMapping[] mappings) { string value = "[Not Specified]"; IInputMapping valueMapping = (mappings ?? new IInputMapping[0]).FirstOrDefault(m => m != null && "value" == m.InputDataName); if (valueMapping != null) value = base.GetValueInfo(valueMapping); return value; }
The configuration will display as follows if the value "tom@decisions" is configured:
Example B
The following example shows how to construct a Rule with a property value. This Rule will evaluate the length of a string to see if its length is less than a number property.
The property of the rule would be written like this:
[WritableValue]
private int number;
[PropertyClassification(0, "Number", "Settings")]
public virtual int Number
{
get { return number; }
set
{
number = value;
OnPropertyChanged("Number");
}
}
The Run method would use the property like this:
public override bool Run(RuleStepExecutionData data)
{
return (((string)data.Data[this.AnchorData.Name]).Length < number);
}
This property would show up in the Rule Designer as a value that can be hardcoded. Below is how it will be displayed in the Rule Designer.
Adding design-time validation to the Rule would prevent the user from entering a number value less than 1. To do this, configure the class to inherit from and implement the IValidationSource interface. Also, add InvalidateVerbInfo() to the property so that the validations show up correctly in the Designer when the value of the property is changed. The following is a code snippet of how the property and implemented IValidationSource would be displayed.
[WritableValue]
private int number;
[PropertyClassification(0, "Number", "Settings")]
public virtual int Number
{
get { return number; }
set
{
number = value;
OnPropertyChanged("Number");
InvalidateVerbInfo();
}
}
public ValidationIssue[] GetValidationIssues()
{
List issues = new List();
if (number < 1)
issues.Add(new ValidationIssue(this, "Number must be greater than 0", null, BreakLevel.Fatal, "Number"));
return issues.ToArray();
}