//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...
}
}
}
}
});