Select to view content in your preferred language

Problems Validating Attribute Rules Using arcpy in an Enterprise Environment

446
0
09-12-2023 04:24 AM
StefanAngerer
Occasional Contributor

Hello Esri Community,

I hope you're all doing well. I am currently working on developing an arcpy toolbox that aims to streamline the validation of validation attribute rules for our end users. The goal is to simplify the validation process, eliminating the need for end users to open the error inspector, click on "Evaluate Rules" (and ensure that only updates made in the current database version are validated), select the correct map extent, and so on.

The script I've developed works perfectly on a local file geodatabase, looking something like this - at least the relevant part: 

class Tool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Validate"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        # Workspace parameter
        workspace = arcpy.Parameter(
            displayName="Workspace",
            name="workspace",
            datatype="DEWorkspace",
            parameterType="Required",
            direction="Input")
        
        # Utility network parameter
        utility_network = arcpy.Parameter(            
            displayName="Utility Network",
            name="utility_network", 
            datatype="DEUtilityNetwork",
            parameterType="Required", 
            direction="Input")


        # Boolean parameter for asset validation
        validate_assets = arcpy.Parameter(
            displayName="Validate Assets",
            name="validate_assets",
            datatype="GPBoolean",
            parameterType="Optional",
            direction="Input")

        # Boolean parameter for calculating subnetworks
        # TODO: Implement calculate subnetworks
        calculate_subnetworks = arcpy.Parameter(
            displayName="Calculate Subnetworks",
            name="calculate_subnetworks",
            datatype="GPBoolean",
            parameterType="Optional",
            direction="Input")

        # Boolean parameter for cleaning dirty areas
        validate_network_topology = arcpy.Parameter(
            displayName="Validate Network Topology",
            name="validate_network_topology",
            datatype="GPBoolean",
            parameterType="Optional",
            direction="Input")

        params = [workspace, utility_network, validate_assets, calculate_subnetworks, validate_network_topology]
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        try:
            # Set progressor
            arcpy.SetProgressor(type="default", message="Validation process initialized")

            # Retrieve parameter values
            workspace = parameters[0].valueAsText
            utility_network = parameters[1].value
            validate_assets = parameters[2].value
            validate_network_topology = parameters[4].value

            # Set up environment and workspace
            arcpy.env.workspace = workspace
            arcpy.SetProgressor(type="default", message="Parameters and workspace loaded")

            # Evaluate Validation Rule
            if validate_assets:
                arcpy.SetProgressor(type="default", message="Validating assets...")
                error_layers = ['GDB_ValidationPointErrors', 'GDB_ValidationLineErrors', 'GDB_ValidationPolygonErrors']
                
                # Evaluate validation rules
                arcpy.management.EvaluateRules(workspace, "VALIDATION_RULES")
                arcpy.AddMessage("Asset validation complete.")

                total_error_count = 0  # To count the total number of errors
                for error_layer in error_layers:
                    record_count = int(arcpy.management.GetCount(error_layer).getOutput(0))
                    total_error_count += record_count
                    if record_count == 0:
                        arcpy.AddMessage(f"No {error_layer} found.")
                    else:
                        arcpy.AddError(f"{record_count} {error_layer} found.")
                arcpy.AddMessage("Asset evaluation complete.")
                
                # Provide summary of asset validation
                if total_error_count == 0:
                    arcpy.AddMessage("No errors found in assets.")
                else:
                    arcpy.AddError(f"Total {total_error_count} errors found in assets.")
                    arcpy.AddError("Validation process cannot proceed until asset errors are resolved.")
                    return  # Stop further execution

Sadly the Tool is encountering issues when applied in our enterprise environment. Here's an overview of what I'm facing:

1. Validation Errors Not Updating: Even though there are validation errors in the map, it seems that no validation errors are added when I run the script. This is despite using a feature layer service URL as the workspace input for the "Evaluate Rules" tool. Has anyone encountered a similar issue and found a solution? It works seamlessly when using the error inspector.

2. Evaluate Only Current Version Changes: In the error inspector, there's an option to evaluate rules only for assets that were modified in the current version. However, I can't seem to find this option in the Python tool, and there's no relevant parameter in the tool description. Is this feature exclusive to the error inspector, or is there a way to implement it in my Python script?

3. Iteration Over Error Layers: I'm experiencing problems when attempting to iterate over the error layers. I can't even open the error layers on our enterprise environment, getting an error that looks something like this:

errorinspectorerror.png

Furthermore, the `GetCount` tool doesn't seem to accept the error layer names as expected. Does anyone have suggestions on how I can count the rows in error layers through an arcpy script?

I've scoured the web for solutions to these issues, but I haven't found direct answers. After trying various approaches for hours, I'm reaching out to the community for assistance. Any insights or guidance would be greatly appreciated.

Thank you in advance for your help!

Best regards,
Stefan

PS: After writing this whole text I got another question in mind. How important is the whole "Workspace"-thing, for using arcpy in enterprise environment. Is this still usable or do I have to access all the data over feature service url's?

0 Kudos
0 Replies