Class ArcadeEvaluator

  • All Implemented Interfaces:
    Loadable

    public class ArcadeEvaluator
    extends java.lang.Object
    implements Loadable
    This object allows you to evaluate an ArcadeExpression under a given ArcadeProfile.

    This Object allows you to set up, execute and query information about an Arcade script. For more general information on Arcade and its usages we refer you to the Arcade online documentation. To construct the ArcadeEvaluator object you need to supply an ArcadeExpression along with an ArcadeProfile. ArcadeExpression is an object that contains the Arcade script plus some additional metadata while ArcadeProfile specifies availability of global variables and limits what functions are available. The basic workflow is:

    • Construct an ArcadeEvaluator object using ArcadeExpression and ArcadeProfile.
    • Query information about a script.
    • Insert the required global values into a Dictionary
    • Call evaluateAsync(Map)
    • Use the returned ArcadeEvaluationResult.
    From an ArcadeEvaluator object you can query information about
    • What globals are used in the script.
    • What attributes a particular global variable uses.
    • Is the script considered stable.
    Determining the global variables used in a script informs us what globals need to be supplied when an evaluation is done. It should be possible to rely on what is specified in the profile but in some instances you could provide a subset. getAttributesAsync(String, FeatureTable) lets you dig deeper to determine what attributes are required on a specific global variable. This can be used to ensure the query to the database requests the correct attributes for a feature. isStable() property tells us if the given script will return the same value for an identical set of global variables. For this to be true a script must avoid using some of the builtin functions, such as `now()` or `random()`. ArcadeEvaluator.isStable() can be combined with the knowledge of global variables used and the attributes needed by them, making it possible to construct caches of results or avoid running scripts when updated values will not alter the value returned by the script.
    Since:
    100.14.0
    • Constructor Detail

      • ArcadeEvaluator

        public ArcadeEvaluator​(ArcadeExpression expression,
                               ArcadeProfile profile)
        Creates an ArcadeEvaluator object.
        Parameters:
        expression - arcade expression you want to evaluate
        profile - the ArcadeProfile context the expression should be evaluated under
        Throws:
        java.lang.IllegalArgumentException - if expression is null
        java.lang.IllegalArgumentException - if profile is null
        Since:
        100.14.0
    • Method Detail

      • getExpression

        public ArcadeExpression getExpression()
        Gets the Arcade expression of the Arcade evaluator.
        Returns:
        the Arcade expression of the Arcade evaluator
        Since:
        100.14.0
      • getProfileVariablesUsed

        public java.util.List<java.lang.String> getProfileVariablesUsed()
        Return an array of the strings containing the names of all global variable used in the script.

        A collection of names of the global used in the script. Each global name should have an dictionary of globals values passed to the evaluate function.

        Returns:
        return an array of the strings containing the names of all global variable used in the script
        Since:
        100.14.0
      • isStable

        public boolean isStable()
        Returns whether a script is considered stable.

        A script is considered stable if the script will return the same result given the same inputs. There are certain functions listed below that will return different values on each evaluation. Use of these functions means the script is not considered stable. Arcade functions that stop a script being considered stable:

        • now
        • today
        • timeStamp
        • random
        • guid
        • nextSequence
        Returns:
        returns whether a script is considered stable
        Since:
        100.14.0
      • getProfile

        public ArcadeProfile getProfile()
        Gets the Arcade profile under which the script should evaluate.
        Returns:
        the Arcade profile under which the script should evaluate
        Since:
        100.14.0
      • evaluateAsync

        public ListenableFuture<ArcadeEvaluationResult> evaluateAsync​(java.util.Map<java.lang.String,​java.lang.Object> profileVariables)
        Evaluate the script using the supplied profileVariables.

        The elements of the passed in dictionary are placed into the interpreter as global variables ready to be accessed during evaluation. The name and type of these should comply with the globals specified in the profile that the ArcadeEvaluator was created with. Values are not persisted across evaluations so need to be resupplied each evaluation. Each value of a key/value pair in the dictionary is converted to a type understood by the interpreter. This is then placed into the interpreter as a global variable using the key as its name. Values in the dictionary can be any of the following runtime types:

        To be explicit on the conversions of compound types.
        • GeoElement types are converted to Arcade Feature type
        • Location is converted to an Arcade Feature type
        • FeatureTable types are converted to Arcade FeatureSet type
        • Geodatabase is converted to Arcade FeatureSetCollection type
        • ServiceGeodatabase is converted to Arcade FeatureSetCollection type
        • GeoModel is converted to Arcade FeatureSetCollection type
        Parameters:
        profileVariables - the globals variables for the script as key/value pairs. Required for evaluation.
        Returns:
        the task object representing the asynchronous evaluation of an Arcade script. The value of the task result is an ArcadeEvaluationResult object.
        Throws:
        java.lang.IllegalArgumentException - if profileVariables is null
        Since:
        100.14.0
      • getAttributesAsync

        public ListenableFuture<java.util.List<java.lang.String>> getAttributesAsync​(java.lang.String variableName,
                                                                                     FeatureTable attributeNameSource)
        Return an array of attributes used in the script for a given global variable.

        To fully support the wildcard expansion in the expects statement, calculation of used attributes on a global variable requires an object that can supply a full set of attribute names.

        Parameters:
        variableName - the name of the global variable whose used attributes are required
        attributeNameSource - a FeatureTable from which we can get a list of all attributes names for the global variable of interest
        Returns:
        the task object representing the asynchronous retrieval of the attributes looked up on a global variable in the script. The value of the task result is a List<String>.
        Throws:
        java.lang.IllegalArgumentException - if variableName is null
        java.lang.IllegalArgumentException - if attributeNameSource is null
        Since:
        100.14.0
      • getAttributesAsync

        public ListenableFuture<java.util.List<java.lang.String>> getAttributesAsync​(java.lang.String variableName,
                                                                                     GeoElement attributeNameSource)
        Return an array of attributes used in the script for a given global variable.

        To fully support the wildcard expansion in the expects statement, calculation of used attributes on a global variable requires an object that can supply a full set of attribute names.

        Parameters:
        variableName - the name of the global variable whose used attributes are required
        attributeNameSource - a GeoElement from which we can get a list of all attributes names for the global variable of interest
        Returns:
        the task object representing the asynchronous retrieval of the attributes looked up on a global variable in the script. The value of the task result is a List<String>.
        Throws:
        java.lang.IllegalArgumentException - if variableName is null
        java.lang.IllegalArgumentException - if attributeNameSource is null
        Since:
        100.14.0
      • getLoadStatus

        public LoadStatus getLoadStatus()
        Description copied from interface: Loadable
        Returns the LoadStatus of the loadable resource.
        Specified by:
        getLoadStatus in interface Loadable
        Returns:
        the LoadStatus of the loadable resource
      • cancelLoad

        public void cancelLoad()
        Description copied from interface: Loadable
        Cancels loading metadata for the object.

        Cancels loading the metadata if the object is loading, and always invokes the done loading listener.

        A load operation that is in progress (LoadStatus.LOADING state) can be cancelled by calling this method and the resource will transition from LoadStatus.LOADING to LoadStatus.FAILED_TO_LOAD state. If the load operation was successfully cancelled, a CancellationException will be returned from Loadable.getLoadError().

        Cancellation should be used carefully because all enqueued done loading listeners for that resource instance will get invoked with an error stating that the operation was cancelled. Thus, one component in the application can cancel the load operation initiated by other components.

        This method does nothing if the resource is not in LoadStatus.LOADING state.

        Specified by:
        cancelLoad in interface Loadable
      • loadAsync

        public void loadAsync()
        Description copied from interface: Loadable
        Loads the metadata of the loadable resource asynchronously.

        The load status changes from LoadStatus.NOT_LOADED to LoadStatus.LOADING. A listener can be added via Loadable.addDoneLoadingListener(java.lang.Runnable) that is invoked upon completion of the asynchronous load operation.

        If the load operation completes successfully, the load status will be LoadStatus.LOADED, which means the resource has loaded its metadata.

        If the load operation failed, the load status will be LoadStatus.FAILED_TO_LOAD and the error can be retrieved by calling Loadable.getLoadError().

        This method can be called concurrently and repeatedly, but only one attempt is ever made to perform the load operation. If a load operation is already in progress (LoadStatus.LOADING state) when loadAsync is called, it simply piggy-backs on the outstanding operation and the done loading listener added to the loadable resource is enqueued to be invoked when that operation completes. If the operation has already completed (LoadStatus.LOADED or LoadStatus.FAILED_TO_LOAD state) when loadAsync is called, the done loading listener is immediately invoked when added to the loadable resource.

        If a loadable resource has failed to load, calling loadAsync on it subsequently will not change its state. The done loading listener will be invoked immediately when added to the loadable resource. In order to retry loading the resource, Loadable.retryLoadAsync() needs to be used.

        A load operation that is in progress (LoadStatus.LOADING state) can be cancelled by calling Loadable.cancelLoad().

        Specified by:
        loadAsync in interface Loadable
      • retryLoadAsync

        public void retryLoadAsync()
        Description copied from interface: Loadable
        Loads or retries loading metadata for the object asynchronously.

        Will retry loading the metadata if the object's load status is LoadStatus.FAILED_TO_LOAD. Will load the object if it is not loaded. Will not retry to load the object if the object is loaded.

        For more details on the load process see Loadable.loadAsync().

        Specified by:
        retryLoadAsync in interface Loadable
      • addDoneLoadingListener

        public void addDoneLoadingListener​(java.lang.Runnable runner)
        Description copied from interface: Loadable
        Adds a listener to the loadable resource that is invoked when loading has completed.

        The listener may be added at any point, whether the loadable resource has already completed loading or not.

        • For resources that are not loaded when the listener is added (LoadStatus is NOT_LOADED or LOADING): When the resource completes loading, the listener will be invoked on the UI thread if it is added from the UI thread, otherwise it is not guaranteed on which thread the listener is invoked.
        • For resources that are already loaded when the listener is added (LoadStatus is LOADED or FAILED_TO_LOAD): The listener will be called immediately, on the current thread.

        Alternatively, to be notified when there is any change in the load status, use the Loadable.addLoadStatusChangedListener(LoadStatusChangedListener) method instead.

        Specified by:
        addDoneLoadingListener in interface Loadable
        Parameters:
        runner - a Runnable that is invoked upon completion of the load operation
      • removeDoneLoadingListener

        public boolean removeDoneLoadingListener​(java.lang.Runnable runner)
        Description copied from interface: Loadable
        Removes a done loading listener from the loadable resource.
        Specified by:
        removeDoneLoadingListener in interface Loadable
        Parameters:
        runner - the listener to be removed
        Returns:
        true if the listener was removed, otherwise false
      • addLoadStatusChangedListener

        public void addLoadStatusChangedListener​(LoadStatusChangedListener listener)
        Description copied from interface: Loadable
        Adds a LoadStatusChangedListener to the loadable resource that is invoked whenever the load status changes.

        Adding this listener on the UI thread will cause it to be invoked on the UI thread, otherwise it is not guaranteed on which thread the listener is invoked.

        The listener will not be called if added to a loadable resource that has already completed loading. To be notified when a loadable resource has completed loading, including if the resource is already loaded when the listener is added, use the Loadable.addDoneLoadingListener(Runnable) method.

        Specified by:
        addLoadStatusChangedListener in interface Loadable
        Parameters:
        listener - the LoadStatusChangedListener to be added
      • removeLoadStatusChangedListener

        public boolean removeLoadStatusChangedListener​(LoadStatusChangedListener listener)
        Description copied from interface: Loadable
        Removes a LoadStatusChangedListener from the loadable resource.
        Specified by:
        removeLoadStatusChangedListener in interface Loadable
        Parameters:
        listener - the LoadStatusChangedListener to be removed
        Returns:
        true if the listener was removed, otherwise false