Class PipelineDeclaration

java.lang.Object
nz.org.riskscape.rl.ast.PipelineDeclaration
All Implemented Interfaces:
AST, PipelineAST

public final class PipelineDeclaration extends Object implements PipelineAST

AST for a pipeline declaration - the root of the AST.

  • Field Details

    • EMPTY

      public static final PipelineDeclaration EMPTY

      An empty PipelineDeclaration. Useful in various situations where a null might otherwise be used, or might require a different API method to handle a base case.

  • Constructor Details

  • Method Details

    • getBoundary

      public Optional<Pair<Token,Token>> getBoundary()
      Specified by:
      getBoundary in interface AST
      Returns:
      a pair of tokens that represent the start and end of this expression, to be used when giving users information about where errors have occurred. If the expression has been rebuilt then the boundary may be empty or it may be from the original AST. Intended for use in problems to trace errors back to their source.
    • findDefinition

      public Optional<PipelineDeclaration.Found> findDefinition(@NonNull @NonNull String stepName)

      Search through for the definition of a step with the given name, using either the step id or alias

      Parameters:
      stepName - name (id or alias) of a step to look for
      Returns:
      the first found declaration of the found step, along with the step chain that it was found in, or empty if nothing was found
    • find

      Convenience version of find(BiPredicate) that drops the chain from the predicate

    • find

      General purpose step-finding method. Find a step using a predicate, returning the first step that matches (in ast order).

      Parameters:
      predicate - test each step against this predicate
      Returns:
      the first step (and chain) that matched, or empty if nothing found.
    • findAll

      General purpose step-finding method. Find a list of steps using a predicate, returning all steps that match (in ast order).

      Parameters:
      predicate - test each step against this predicate
      Returns:
      a list of PipelineDeclaration.Found objects for the steps that matched the predicate
    • replace

      public PipelineDeclaration replace(@NonNull @NonNull StepChain found, @NonNull @NonNull StepChain newChain) throws IllegalStateException

      Return a new PipelineDeclaration, with a replacement step chain

      Parameters:
      found - the step chain to replace
      newChain - the replacement chain
      Returns:
      a new PipelineDeclaration with the replacement made
      Throws:
      IllegalStateException - if found is not part of this pipeline declaration
    • getUniqueParameters

      public List<ParameterToken> getUniqueParameters()
      Returns:
      a the unique set of ParameterTokens in this PipelineDeclaration, but as a List to preserve the order that they were found in.
    • getAllParameters

      public List<ParameterToken> getAllParameters()
      Returns:
      the list of all ParameterTokens in this pipeline, one for each occurrence within the AST.
    • replaceParameters

      public ResultOrProblems<PipelineDeclaration> replaceParameters(Map<String,Expression> replacements)

      Builds a new PipelineDeclaration by replacing tokens with those specified, while trying to preserve whitespace

      Parameters:
      replacements - parameter names mapped to replacement expressions.
      Returns:
      a new PipelineDeclaration, or problems if any of the parameter tokens found in the pipeline declaration that were not replaced.
    • add

      public PipelineDeclaration add(@NonNull @NonNull StepChain toAdd)

      Return a new PipelineDeclaration with the given step chain added to the end of this PipelineDeclaration's list of chains

      Parameters:
      toAdd - the chain to add
      Returns:
      a new PipelineDeclaration containing the given chain
    • add

      Return a new PipelineDeclaration that is the combination of this pipeline and the other - no merge or squishing or anything clever is going on

    • getFirst

      public StepChain getFirst()
      Returns:
      the 0th chain in this declaration
      Throws:
      IndexOutOfBoundsException - if this declaration is empty
    • getLast

      public StepChain getLast()
      Returns:
      the last chain in this declaration
      Throws:
      IndexOutOfBoundsException - if this declaration is empty
    • isEmpty

      public boolean isEmpty()
      Returns:
      true if this declaration has no chains
    • appendString

      protected void appendString(StringBuilder appendTo)

      Append a simplified string representation to the builder. Default implementation uses appendSource. Subclasses can override to provide a more concise representation.

    • appendSource

      protected void appendSource(StringBuilder appendTo)

      Implementers extend this to append their source to the given string builder. Done via string builder to encourage a more efficient implementation than is possible with concatenating multiple toString calls that typically contruct a string builder anyway.

    • stepDefinitionIterator

      public Iterator<StepDefinition> stepDefinitionIterator()
      Returns:
      an Iterator that yields all of the StepDefinitions in this pipeline declaration in the order they appear in the source. Skips all the StepReferences.
    • size

      public int size()
      Returns:
      the number of chains (not steps) in this pipeline declaration
    • getStepNameFunction

      public Function<StepDeclaration,String> getStepNameFunction(StepNamingPolicy policy)
      Parameters:
      policy - the step naming policy that will be applied
      Returns:
      a function that gives the name for a step definition, returning either a unique generated name, if none was assigned, or the explicitly assigned name
    • checkValid

      Check that this PipelineDeclaration is valid. This check includes checking for step redefinition and pipeline cycles.

      Parameters:
      nameFunction - maps a StepDefinition to a name, see getStepNameFunction(StepNamingPolicy)
      Returns:
      function that provides step names for this pipeline or errors found
    • withMetadata

      public PipelineDeclaration withMetadata(PipelineMetadata newMetadata)

      Clones this PipelineDeclaration but with different metadata.

    • accept

      public <T, R> R accept(PipelineVisitor<T,R> visitor, T data)
      Specified by:
      accept in interface PipelineAST
    • equals

      public boolean equals(Object o)
    • canEqual

      protected boolean canEqual(Object other)
    • hashCode

      public int hashCode()
    • getChains

      public List<StepChain> getChains()
    • getMetadata

      public PipelineMetadata getMetadata()
    • appendSource

      protected static void appendSource(AST ast, StringBuilder appendTo)

      Helper method for appending the source of another ast element to a string builder, efficiently if possible.

    • toString

      public final String toString()

      Returns a simplified view of the ast in a source-like fashion, but with a lot of details left out for conciseness

      Overrides:
      toString in class Object
    • toSource

      public final String toSource()
      Specified by:
      toSource in interface AST
      Returns:
      a normalized version of the AST that can be parsed to be exactly the same AST as this