GetScriptExpression Method
Gets the Arcade expression that defines the rule.
This method must be called on the MCT. Use QueuedTask.Run.
public string GetScriptExpression()
Public Function GetScriptExpression() As String
Return Value
The Arcade expression.
Evaluate AttributeRule Expression
//Consult https://github.com/Esri/arcade-expressions/ and
//https://developers.arcgis.com/arcade/profiles/attribute-rules/ for
//more examples and arcade reference
QueuedTask.Run(() =>
{
//Retrieve the desired feature class/table
var def = featLayer.GetFeatureClass().GetDefinition();
//get the desired attribute rule whose expression is to be
//evaluated.
//AttributeRuleType.All, Calculation, Constraint, Validation
var validation_rule = def.GetAttributeRules(
AttributeRuleType.Validation).FirstOrDefault();
if (validation_rule == null)
return;
//Get the expression
var expr = validation_rule.GetScriptExpression();
//construct a CIMExpressionInfo
var arcade_expr = new CIMExpressionInfo()
{
Expression = expr,
//Return type can be string, numeric, or default
ReturnType = ExpressionReturnType.Default
};
System.Diagnostics.Debug.WriteLine($"Evaluating {expr}:");
//Construct an evaluator
//we are using ArcadeProfile.AttributeRules profile...
//Consult: https://developers.arcgis.com/arcade/profiles/
using (var arcade = ArcadeScriptEngine.Instance.CreateEvaluator(
arcade_expr, ArcadeProfile.AttributeRuleValidation))
{
//we are evaluating the expression against all features
using (var rc = featLayer.Search())
{
while (rc.MoveNext())
{
//Provision values for any profile variables referenced...
//in our case we assume '$feature'
//...use arcade.ProfileVariablesUsed() if necessary...
var variables = new List<KeyValuePair<string, object>>() {
new KeyValuePair<string, object>("$feature", rc.Current)
};
//evaluate the expression per feature
try
{
var result = arcade.Evaluate(variables).GetResult();
//'Validation' attribute rules return true or false...
var valid = System.Boolean.Parse(result.ToString());
System.Diagnostics.Debug.WriteLine(
$"{rc.Current.GetObjectID()} valid: {valid}");
}
//handle any exceptions
catch (InvalidProfileVariableException ipe)
{
//something wrong with the profile variable specified
//TODO...
}
catch (EvaluationException ee)
{
//something wrong with the query evaluation
//TODO...
}
}
}
}
});
Get attribute rules of a dataset
public void GetAttributeRules(Geodatabase geodatabase, string tableName)
{
using (TableDefinition tableDefinition = geodatabase.GetDefinition<TableDefinition>(tableName))
{
// Get all attribute rule types
IReadOnlyList<AttributeRuleDefinition> ruleDefinitions = tableDefinition.GetAttributeRules();
// Iterate rule definitions
foreach (AttributeRuleDefinition ruleDefinition in ruleDefinitions)
{
AttributeRuleType ruleType = ruleDefinition.GetAttributeRuleType();
string ruleDescription = ruleDefinition.GetDescription();
bool isAttributeFieldEditable = ruleDefinition.GetIsFieldEditable();
string arcadeVersionToSupportRule = ruleDefinition.GetMinimumArcadeVersion();
int ruleEvaluationOrder = ruleDefinition.GetEvaluationOrder();
AttributeRuleTriggers triggeringEvents = ruleDefinition.GetTriggeringEvents();
string scriptExpression = ruleDefinition.GetScriptExpression();
// more properties
}
}
}
Target Platforms: Windows 11, Windows 10
ArcGIS Pro version: 3.2 or higher.