Class ActorUtils


  • public class ActorUtils
    extends Object
    Helper class for actors.
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    • Field Detail

      • FLOW_FILENAME_LONG

        public static final String FLOW_FILENAME_LONG
        the variable for the full flow filename.
        See Also:
        Constant Field Values
      • FLOW_FILENAME_SHORT

        public static final String FLOW_FILENAME_SHORT
        the variable for the short flow filename.
        See Also:
        Constant Field Values
      • FLOW_FILENAME_RESTORE

        public static final String FLOW_FILENAME_RESTORE
        the variable for the flow restoration filename (long filename, but with .props extension).
        See Also:
        Constant Field Values
      • FLOW_START_TIMESTAMP

        public static final String FLOW_START_TIMESTAMP
        the variable for the start timestamp of the flow.
        See Also:
        Constant Field Values
      • PROJECT_MODULES

        public static final String PROJECT_MODULES
        the variable for the project modules (comma-separated list).
        See Also:
        Constant Field Values
      • PROGRAMMATIC_VARIABLES

        public static final String[] PROGRAMMATIC_VARIABLES
        programmatically set variables.
      • PROGRAMMATIC_VARIABLES_HELP

        public static final Map<String,​String> PROGRAMMATIC_VARIABLES_HELP
        programmatically set variables.
      • PROCEDURAL_TRANSFORMER

        public static final String PROCEDURAL_TRANSFORMER
        procedural type: transformer.
        See Also:
        Constant Field Values
      • ENV_INVALID_VARIABLES_LENIENT

        public static final String ENV_INVALID_VARIABLES_LENIENT
        the environment variable to enable lenient checking for invalid variables.
        See Also:
        Constant Field Values
      • m_InvalidVariablesLenient

        protected static Boolean m_InvalidVariablesLenient
        whether to be lenient with invalid variables.
    • Constructor Detail

      • ActorUtils

        public ActorUtils()
    • Method Detail

      • isLoggingEnabled

        protected static boolean isLoggingEnabled()
        Returns whether logging is enabled.
        Returns:
        true if enabled
      • enumerate

        protected static void enumerate​(Actor actor,
                                        List<Actor> children,
                                        ActorFilter filter)
        Enumerates all children of the given actor (depth-first search).
        Parameters:
        actor - the actor to obtain all children from
        children - for collecting the children
        filter - for filtering the classes
      • enumerate

        public static List<Actor> enumerate​(Actor actor)
        Enumerates all children of the given actor (depth-first search).
        Parameters:
        actor - the actor to obtain all children from
        Returns:
        all the children (if any)
      • enumerate

        public static List<Actor> enumerate​(Actor actor,
                                            Class[] filter)
        Enumerates all children of the given actor (depth-first search).
        Parameters:
        actor - the actor to obtain all children from
        filter - the classes of actors to only enumerate, null for no filtering
        Returns:
        all the children (if any)
      • enumerate

        public static List<Actor> enumerate​(Actor actor,
                                            ActorFilter filter)
        Enumerates all children of the given actor (depth-first search).
        Parameters:
        actor - the actor to obtain all children from
        filter - the filter to use
        Returns:
        all the children (if any)
      • replace

        public static int replace​(OptionHandler handler,
                                  Comparable find,
                                  Comparable replace,
                                  boolean recursive)
        Replaces any occurrence of the object to find with the replacement. Can be done recursive. Performs the replacement also in arrays and nested OptionHandlers.
        Parameters:
        handler - the OptionHandler to process
        find - the object to look for
        replace - the replacement
        recursive - if true then all children (if any) of the actor will be processed as well
        Returns:
        the number of replacements
      • replace

        public static int replace​(OptionHandler handler,
                                  Comparable find,
                                  Comparable replace,
                                  boolean recursive,
                                  HashSet<Class> excluded)
        Replaces any occurrence of the object to find with the replacement. Can be done recursive. Performs the replacement also in arrays and nested OptionHandlers.
        Parameters:
        handler - the OptionHandler to process
        find - the object to look for
        replace - the replacement
        recursive - if true then all children (if any) of the actor will be processed as well
        excluded - the base classes to skip
        Returns:
        the number of replacements
      • uniqueName

        public static boolean uniqueName​(Actor actor,
                                         ActorHandler handler,
                                         int index)
        Updates the name of the actor to make it unique among all the other actors of the given handler.
        Parameters:
        actor - the actor which name needs to be made unique
        handler - the handler to take into account
        index - the index that this actor will be placed
        Returns:
        true if the name was updated
      • uniqueName

        public static boolean uniqueName​(Actor actor,
                                         Set<String> names)
        Updates the name of the actor to make it unique among all the other specified names.
        Parameters:
        actor - the actor which name needs to be made unique
        names - the existing names
        Returns:
        true if the name was updated
      • uniqueName

        public static boolean uniqueName​(Actor actor,
                                         Set<String> names,
                                         boolean tailRemoval)
        Updates the name of the actor to make it unique among all the other specified names.
        Parameters:
        actor - the actor which name needs to be made unique
        names - the existing names
        tailRemoval - whether the "tail" (eg ' (2)') can be removed to determine basename
        Returns:
        true if the name was updated
      • uniqueNames

        public static boolean uniqueNames​(Actor[] actors)
        Updates the names of the actors to make then unique among all of them. NB: any suffix matching "-XYZ" gets stripped first.
        Parameters:
        actors - the actors which names need to be made unique
        Returns:
        true if at least one name was updated
      • findActorHandlers

        public static List<ActorHandler> findActorHandlers​(Actor actor)
        Returns a list of actor handlers, starting from the current note (excluded). The search goes up in the actor hierarchy, up to the root (i.e., the last item in the returned list will be most likely a "Flow" actor).
        Parameters:
        actor - the actor to start the search from
        Returns:
        the list of actor handlers found along the path to the root actor
      • findActorHandlers

        public static List<ActorHandler> findActorHandlers​(Actor actor,
                                                           boolean mustAllowStandalones)
        Returns a list of actor handlers, starting from the current note (excluded). The search goes up in the actor hierarchy, up to the root (i.e., the last item in the returned list will be most likely a "Flow" actor).
        Parameters:
        actor - the actor to start the search from
        mustAllowStandalones - whether the handler must allow standalones
        Returns:
        the list of actor handlers found along the path to the root actor
      • findActorHandlers

        public static List<ActorHandler> findActorHandlers​(Actor actor,
                                                           boolean mustAllowStandalones,
                                                           boolean includeSameLevel)
        Returns a list of actor handlers, starting from the current note (excluded). The search goes up in the actor hierarchy, up to the root (i.e., the last item in the returned list will be most likely a "Flow" actor).
        Parameters:
        actor - the actor to start the search from
        mustAllowStandalones - whether the handler must allow standalones
        includeSameLevel - allows adding of actor handlers that are on the same level as the current actor, but have a lower index in the parent
        Returns:
        the list of actor handlers found along the path to the root actor
      • isStandalone

        public static boolean isStandalone​(Actor actor)
        Checks whether this actor is a standalone.
        Parameters:
        actor - the actor to check
        Returns:
        true if standalone
      • isSource

        public static boolean isSource​(Actor actor)
        Checks whether this actor is a source (output).
        Parameters:
        actor - the actor to check
        Returns:
        true if source
      • isSink

        public static boolean isSink​(Actor actor)
        Checks whether this actor is a sink (input).
        Parameters:
        actor - the actor to check
        Returns:
        true if sink
      • isTransformer

        public static boolean isTransformer​(Actor actor)
        Checks whether this actor is a transformer (input/output).
        Parameters:
        actor - the actor to check
        Returns:
        true if transformer
      • isControlActor

        public static boolean isControlActor​(Actor actor)
        Checks whether this actor is a control actor.
        Parameters:
        actor - the actor to check
        Returns:
        true if control actor
      • isActorHandler

        public static boolean isActorHandler​(Actor actor)
        Checks whether this actor is an actor handler.
        Parameters:
        actor - the actor to check
        Returns:
        true if actor handler
      • isInteractive

        public static boolean isInteractive​(Actor actor)
        Checks whether the actor or its sub-actors are interactive ones. Does not check any callable actors. Cannot check any internally used actors if they are either not exposed (like ExternalActorHandler) or not yet executed/generated, so no guaranteed outcome.
        Parameters:
        actor - the actor to check
        Returns:
        true if at least one (active) interactive actor detected
      • write

        public static boolean write​(String filename,
                                    Actor actor)
        Writes the actor to a file.
        Parameters:
        filename - the file to write to
        actor - the actor to write
        Returns:
        true if successfully written
      • read

        public static Actor read​(String filename)
        Reads an actor from a file.
        Parameters:
        filename - the file to read the actor
        Returns:
        the actor or null in case of an error
      • read

        public static Actor read​(String filename,
                                 MessageCollection errors)
        Reads an actor from a file.
        Parameters:
        filename - the file to read the actor
        errors - for storing (potential) errors, ignored if null
        Returns:
        the actor or null in case of an error
      • read

        public static Actor read​(String filename,
                                 MessageCollection errors,
                                 MessageCollection warnings)
        Reads an actor from a file.
        Parameters:
        filename - the file to read the actor
        errors - for storing (potential) errors, ignored if null
        warnings - for storing (potential) warnings, ignored if null
        Returns:
        the actor or null in case of an error
      • findCallableTransformers

        public static Hashtable<String,​Integer> findCallableTransformers​(Actor actor)
        Locates callable transformers.
        Parameters:
        actor - the actor (and its sub-actors) to check
        Returns:
        the names of the callable actors referenced and how often they were referenced
      • findClosestType

        protected static Actor findClosestType​(ActorHandler handler,
                                               Class type)
        Checks an actor handler's children whether they contain the actor type we're looking for.
        Parameters:
        handler - the actor handler to check
        type - the type of actor to find the closest for
        Returns:
        the closest actor or null if not found
      • findClosestType

        public static Actor findClosestType​(Actor actor,
                                            Class type)
        Tries to find the closest type in the actor tree, starting with the current actor.
        Parameters:
        actor - the actor to start from
        type - the type of actor to find the closest for
        Returns:
        the closest actor or null if not found
      • findClosestType

        public static Actor findClosestType​(Actor actor,
                                            Class type,
                                            boolean includeSameLevel)
        Tries to find the closest type in the actor tree, starting with the current actor.
        Parameters:
        actor - the actor to start from
        type - the type of actor to find the closest for
        includeSameLevel - whether to include actor handlers that are on the same level as the actor, but with a lower index in the parent
        Returns:
        the closest actor or null if not found
      • findClosestTypes

        protected static List<Actor> findClosestTypes​(ActorHandler handler,
                                                      Class type)
        Checks an actor handler's children whether they contain the actor type we're looking for.
        Parameters:
        handler - the actor handler to check
        type - the type of actor to find the closest for
        Returns:
        the closest actors, empty list if none found
      • findClosestTypes

        public static List<Actor> findClosestTypes​(Actor actor,
                                                   Class type)
        Tries to find the closest types in the actor tree, starting with the current actor.
        Parameters:
        actor - the actor to start from
        type - the type of actor to find the closest for
        Returns:
        the closest actors or empty list if not found
      • findClosestTypes

        public static List<Actor> findClosestTypes​(Actor actor,
                                                   Class type,
                                                   boolean includeSameLevel)
        Tries to find the closest types in the actor tree, starting with the current actor.
        Parameters:
        actor - the actor to start from
        type - the type of actor to find the closest for
        includeSameLevel - whether to include actor handlers that are on the same level as the actor, but with a lower index in the parent
        Returns:
        the closest actors or empty list if not found
      • determineLocation

        public static Point determineLocation​(Window window,
                                              int x,
                                              int y)
        Determines the initial location of the window.
        Parameters:
        window - the window to determine the location for
        x - the position (-1: left, -2: middle, -3: right)
        y - the position (-1: top, -2: middle, -3: bottom)
        Returns:
        the position
      • determineLocation

        public static Point determineLocation​(GraphicsConfiguration gc,
                                              Dimension size,
                                              int x,
                                              int y)
        Determines the initial location of the window.
        Parameters:
        gc - the graphics configuration
        size - the size of the window
        x - the position (-1: left, -2: middle, -3: right)
        y - the position (-1: top, -2: middle, -3: bottom)
        Returns:
        the position
      • determineSize

        public static Dimension determineSize​(Window window,
                                              int x,
                                              int y,
                                              int width,
                                              int height)
        Determines the height of the window.
        Parameters:
        window - the frame/dialog to determine the location for
        x - the position (-1: left, -2: middle, -3: right)
        y - the position (-1: top, -2: middle, -3: bottom)
        width - the width (0: as is, -1: maximum width)
        height - the width (0: as is, -1: maximum height)
        Returns:
        the size
      • getDatabaseConnection

        public static AbstractDatabaseConnection getDatabaseConnection​(Actor actor,
                                                                       Class cls,
                                                                       AbstractDatabaseConnection defCon)
        Returns the database connection object to use.
        Parameters:
        actor - the actor start the search from (towards the root)
        cls - the DatabaseConnection actor class to look for
        defCon - the default database connection, in case none is found in the flow
        Returns:
        the connection object to use
      • removeDisabledActors

        public static Actor removeDisabledActors​(Actor actor)
        Removes all disabled actors (recursively) from the actor.

        NB: creates a copy of the actor first before removing the disabled actors.
        Parameters:
        actor - the actor to process
        Returns:
        the cleaned up actor (or original, if nothing changed)
      • setActors

        protected static String setActors​(ActorHandler handler,
                                          Actor[] actors)
        Sets the actors in the handler.
        Parameters:
        handler - the handler to update
        actors - the actors to set
        Returns:
        null if successful, otherwise error message
      • createExternalActor

        public static Actor createExternalActor​(Actor[] actors,
                                                ActorHandler suggestion)
        Ensures that the actors are enclosed in an "instantiable" wrapper.
        Parameters:
        actors - the actors to enclose
        suggestion - the suggested actor handler, can be null
        Returns:
        the processed actor
      • locate

        public static Actor locate​(ActorPath path,
                                   Actor parent)
        Tries to locate the actor specified by the path parts.
        Parameters:
        parent - the parent to start with
        path - the path elements to traverse (below the parent)
        Returns:
        the located actor or null if none found
      • locate

        public static Actor locate​(ActorPath path,
                                   Actor parent,
                                   boolean included,
                                   boolean quiet)
        Tries to locate the actor specified by the path parts.
        Parameters:
        parent - the parent to start with
        path - the path elements to traverse (below the parent)
        included - whether the actor should be included in the search, rather than searching only below
        quiet - whether to suppress any error messages
        Returns:
        the located actor or null if none found
      • locate

        public static Actor locate​(String path,
                                   Actor root)
        Locates the actor in the actor tree based on the specified path.
        Parameters:
        path - the path of the node to locate
        root - the root actor start the search
        Returns:
        the located actor or null if none found
      • locate

        public static Actor locate​(String path,
                                   Actor root,
                                   boolean included,
                                   boolean quiet)
        Locates the actor in the actor tree based on the specified path.
        Parameters:
        path - the path of the node to locate
        root - the root actor start the search
        included - whether the actor should be included in the search, rather than searching only below
        quiet - whether to suppress any error messages
        Returns:
        the located actor or null if none found
      • checkForSource

        public static String checkForSource​(Actor actor)
        Checks whether the actor has a source as first actor (can be nested).
        Parameters:
        actor - the actor to analyze
        Returns:
        null if ok, otherwise error message
      • checkForSource

        public static String checkForSource​(Actor[] actors)
        Checks whether the actors have a source as first actor (can be nested).
        Parameters:
        actors - the actors to analyze
        Returns:
        null if ok, otherwise error message
      • checkForExternalActor

        public static boolean checkForExternalActor​(Actor actor)
        Checks whether an external actor is present in this sub-tree.
        Parameters:
        actor - the actor to analyze
        Returns:
        true if external actor present
      • checkForCallableActorUser

        public static boolean checkForCallableActorUser​(Actor actor)
        Checks whether a callable actor user is present in this sub-tree.
        Parameters:
        actor - the actor to analyze
        Returns:
        true if callable actor user present
      • cleanUpFlow

        public static Actor cleanUpFlow​(Actor actor)
        Cleans up the flow, e.g., removing disabled actors, unused callable actors.
        Parameters:
        actor - the flow to clean up
        Returns:
        null if nothing changed, otherwise the updated flow
        See Also:
        CleanUpProcessor
      • checkFlow

        public static String checkFlow​(Actor actor,
                                       File file)
        Checks the flow.
        Parameters:
        actor - the flow to check
        file - the actor file to use
        Returns:
        null if all checks passed, otherwise the warnings
        See Also:
        CheckProcessor
      • checkFlow

        public static String checkFlow​(Actor actor,
                                       boolean variables,
                                       boolean storage,
                                       File file)
        Checks the flow.
        Parameters:
        actor - the flow to check
        variables - whether the check variable usage
        storage - whether to check storage usage
        file - the actor file to use
        Returns:
        null if all checks passed, otherwise the warnings
        See Also:
        CheckProcessor
      • transform

        public static List transform​(Actor transformer,
                                     Object input)
                              throws Exception
        Processes the data using the specified transformer. Automatically sets up, executes, wraps up and cleans up the actor.
        Parameters:
        transformer - the transformer to use
        input - the input data
        Returns:
        the generated output data as list, null if none produced.
        Throws:
        Exception - if actor is not a transformer or transformation fails
      • updateErrorHandler

        public static void updateErrorHandler​(Actor actor,
                                              ErrorHandler handler,
                                              boolean trace)
        Updates the error handler.
        Parameters:
        actor - the actor to update the error handler for
        handler - the handler to use
        trace - whether to output the names of the actors that were updated
      • getFunctionalAspect

        public static String getFunctionalAspect​(Actor actor)
        Determines the functional aspect of an actor.
        Parameters:
        actor - the actor to investigate
        Returns:
        the functional description
      • getProceduralAspect

        public static String getProceduralAspect​(Actor actor)
        Determines the procedural aspect of an actor.
        Parameters:
        actor - the actor to investigate
        Returns:
        the procedural description
      • updateFlowAwarePaintlets

        public static void updateFlowAwarePaintlets​(PaintablePanel panel,
                                                    Actor actor)
        Updates all the "flow-aware" paintlets of the panel with the specified actor.
        Parameters:
        panel - this panel's paintlets get updated
        actor - the actor to set
      • updateFlowAwarePaintlet

        public static void updateFlowAwarePaintlet​(Paintlet paintlet,
                                                   Actor actor)
        Updates a "flow-aware" paintlet with the specified actor.
        Parameters:
        paintlet - the paintlet to update, if necessary
        actor - the actor to set
      • extractActorNames

        public static List<String> extractActorNames​(Actor current,
                                                     String text)
        Extracts all the valid actor names from the given text fragment that exist within the specified flow.
        Parameters:
        current - the flow to verify the actor paths with
        text - the text to extract the names from
        Returns:
        the full names of the located actors
      • forceVariables

        public static String forceVariables​(Actor actor,
                                            Variables vars)
        Forces the update of the variables of this actor.
        Parameters:
        actor - the actor to update
        vars - the variables to propagate
        Returns:
        null if successful, otherwise error message
      • sort

        public static boolean sort​(SortableActorHandler handler,
                                   ActorComparator comp)
        Sorts the child-actors using the supplied comparator.
        Parameters:
        handler - the handler to sort the children for
        comp - the comparator to use
        Returns:
        if the order was modified
      • determineNumActors

        public static int determineNumActors​(Actor flow)
        Returns how many actors this flow is made of.
        Parameters:
        flow - the flow to check
        Returns:
        the number of actors
      • strip

        public static Actor strip​(Actor actor)
        Returns a stripped down version of the actor, i.e., for ActorHandlers, a copy of the actor without any sub-actors gets returned.
        Parameters:
        actor - the actor to strip down
        Returns:
        the stripped down actor
        See Also:
        ActorHandler
      • ensureValidVariables

        public static String ensureValidVariables​(Actor actor)
        Ensures that the variables within this actor are valid.
        Parameters:
        actor - the actor to check
        Returns:
        null if all variables are valid
      • checkInvalidVariablesLenient

        protected static boolean checkInvalidVariablesLenient()
        Returns whether invalid variables are to be treated lenient (ie no error, just warning).
        Returns:
        true if to treat lenient
        See Also:
        ENV_INVALID_VARIABLES_LENIENT
      • getFlowID

        public static int getFlowID​(Actor source)
        Returns the flow ID (if possible) for the given actor.
        Parameters:
        source - the actor to get the flow ID for
        Returns:
        the ID, -1 if unable to obtain