Class Sequence

java.lang.Object
randoop.sequence.Sequence

public final class Sequence extends Object
An immutable sequence of Statements.

This class represents only the structure of a well-formed sequence of statements, and does not contain any information about the runtime behavior of the sequence. The class randoop.ExecutableSequence adds functionality that executes the sequence.

  • Field Details

    • statements

      public final SimpleList<Statement> statements
      The list of statements.
    • lastStatementVariables

      private transient List<Variable> lastStatementVariables
      The variables that are inputs or output for the last statement of this sequence: first the return variable if any (ie, if the operation is non-void), then the input variables. These hold the values "produced" by some statement of the sequence. Should be final but cannot because of serialization.
    • lastStatementTypes

      private transient List<Type> lastStatementTypes
      The types of elements of lastStatementVariables.
    • shouldInlineLiterals

      private transient boolean shouldInlineLiterals
      If true, inline primitive values rather than creating and using a variable.
    • activeFlags

      private BitSet activeFlags
      A set of bits, where there is one bit associated with each index (that is, each statement in the sequence). Active flags are used during generation, to determine what values in an existing sequence are useful to be used as inputs when creating a new sequence out of the existing one.

      Note that each statement's result is treated as active or not. If a statement might side-effect a variable's value, the variable name is used from the previous statement that defined it.

    • savedHashCode

      private final int savedHashCode
    • savedNetSize

      private final int savedNetSize
  • Constructor Details

    • Sequence

      public Sequence()
      Create a new, empty sequence.
    • Sequence

      private Sequence(SimpleList<Statement> statements, int hashCode, int netSize)
      Create a sequence that has the given statements and hashCode (hashCode is for optimization).

      See computeHashcode(SimpleList) for details on the hashCode.

      Parameters:
      statements - the statements of the new sequence
      hashCode - the hashcode for the new sequence
      netSize - the net size for the new sequence
    • Sequence

      public Sequence(SimpleList<Statement> statements)
      Create a sequence with the given statements.
      Parameters:
      statements - the statements
  • Method Details

    • zero

      public static Sequence zero(Type c)
      Returns a sequence that is of the form "Foo f = null;" where Foo is the given class.
      Parameters:
      c - the type for initialized variable
      Returns:
      the sequence consisting of the initialization
    • createSequenceForPrimitive

      public static Sequence createSequenceForPrimitive(Object value)
      Creates a sequence corresponding to the given non-null primitive value.
      Parameters:
      value - non-null reference to a primitive or String value
      Returns:
      a Sequence consisting of a statement created with the object
    • createSequence

      public static Sequence createSequence(TypedOperation operation, List<Sequence> inputSequences, List<Integer> indexes)
      Creates a sequence consisting of the given operation given the input.
      Parameters:
      operation - the operation for the sequence
      inputSequences - the sequences computing inputs to the operation
      indexes - the indices of the inputs to the operation; same length as inputSequences
      Returns:
      the sequence that applies the operation to the given inputs
    • createSequence

      public static Sequence createSequence(TypedOperation operation, TupleSequence elementsSequence)
    • extend

      public final Sequence extend(TypedOperation operation, List<Variable> inputVariables)
      Returns a new sequence that is equivalent to this sequence plus the given operation appended to the end.
      Parameters:
      operation - the operation to add
      inputVariables - the input variables
      Returns:
      the sequence formed by appending the given operation to this sequence
    • extend

      public final Sequence extend(TypedOperation operation, Variable... inputs)
      Returns a new sequence that is equivalent to this sequence plus the given statement appended at the end.
      Parameters:
      operation - the operation to add
      inputs - the input variables for the operation
      Returns:
      the sequence formed by appending the given operation to this sequence
    • extend

      public final Sequence extend(Statement statement, List<Variable> inputs)
      extend adds a new statement to this sequence using the operation of the given statement. Intended as the only place that we reach inside a Statement for its operation.
      Parameters:
      statement - is a Statement object from which the operation is copied
      inputs - is the list of variables for input
      Returns:
      sequence constructed from this one plus the operation
      See Also:
    • concatenate

      public static Sequence concatenate(List<Sequence> sequences)
      Create a new sequence that is the concatenation of the given sequences.
      Parameters:
      sequences - the list of sequences to concatenate
      Returns:
      the concatenation of the sequences in the list
    • getStatement

      public final Statement getStatement(int index)
      The statement for the statement at the given index.
      Parameters:
      index - the index of the statement position in this sequence
      Returns:
      the statement at the given position
    • size

      @Pure public final int size()
      The number of statements in the sequence.
      Returns:
      the number of statements in this sequence
    • getVariable

      public Variable getVariable(int i)
      The value created by the ith statement.
      Parameters:
      i - the statement index
      Returns:
      the variable created by the statement at the given index
    • getVariablesOfLastStatement

      List<Variable> getVariablesOfLastStatement()
      The variables involved in the last statement. This includes the output variable.
      Returns:
      the variables used in the last statement of this sequence
    • getTypesForLastStatement

      List<Type> getTypesForLastStatement()
      The types of all the variables involved in the last statement. This includes the output variable. The types returned are not the types in the signature of the Operation, but the types of the variables.
      Returns:
      the types of the variables in the last statement of this sequence
    • getLastVariable

      public Variable getLastVariable()
      The value created by the last statement in the sequence.
      Returns:
      the variable assigned to by the last statement of this sequence
    • getCreatingStatement

      public Statement getCreatingStatement(Variable value)
      The statement that created this value.
      Parameters:
      value - the variable
      Returns:
      the statement that assigned to this variable
    • getInputs

      public List<Variable> getInputs(int statementIndex)
      The inputs for the ith statement. Includes the receiver.
      Parameters:
      statementIndex - the index for the statement
      Returns:
      the list of variables for the statement at the given index
    • toCodeString

      @SideEffectFree public String toCodeString()
      Returns the Java source code representation of this sequence with values substituted for simple initializations. Similar to * ExecutableSequence.toCodeString() except does not include checks.
      Returns:
      a string containing Java code for this sequence
    • toFullCodeString

      private String toFullCodeString()
      Returns the Java source representation of this sequence showing all statements. Simplifications performed in toCodeString() are preserved but initializations used will also be printed.
      Returns:
      a string containing Java code for this sequence
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • hasActiveFlags

      public boolean hasActiveFlags()
    • isActive

      boolean isActive(int i)
    • setAllActiveFlags

      private void setAllActiveFlags()
    • clearAllActiveFlags

      public void clearAllActiveFlags()
    • clearActiveFlag

      public void clearActiveFlag(int i)
    • getRelativeIndexForVariable

      private static Sequence.RelativeNegativeIndex getRelativeIndexForVariable(int statementPosition, Variable v)
      Returns the relative negative index that would result if we use the given value as an input to the statement at position statementPosition.
      Parameters:
      statementPosition - the position of the statement
      v - the variable
      Returns:
      the relative negative index computed from the position and variable
    • getVariableForInput

      private Variable getVariableForInput(int statementPosition, Sequence.RelativeNegativeIndex input)
      Returns the Variable corresponding to the given input, which is an input to the statement at position statementPosition.
      Parameters:
      statementPosition - the statement position
      input - relative index of the input variable
      Returns:
      the variable at the relative index from the given statement position
    • computeHashcode

      private static int computeHashcode(SimpleList<Statement> statements)
      The hashcode of a sequence is the sum of each statement's hashcode. This seems good enough, and it makes computing hashCode of a concatenation of sequences faster (it's just the addition of each sequence's' hashCode). Otherwise, hashCode computation used to be a hotspot.
      Parameters:
      statements - the list of statements over which to compute the hash code
      Returns:
      the sum of the hash codes of the statements in the sequence
    • computeNetSize

      private static int computeNetSize(SimpleList<Statement> statements)
      Counts the number of statements in a list that are not initializations with a primitive type. For instance int var7 = 0.
      Parameters:
      statements - the list of Statement objects
      Returns:
      count of statements other than primitive initializations
    • computeLastStatementInfo

      private void computeLastStatementInfo()
    • checkRep

      private void checkRep()
      Representation invariant check.
    • equals

      public final boolean equals(Object o)
      Two sequences are equal if their statements(+inputs) are element-wise equal.
      Overrides:
      equals in class Object
    • verifyNotEqual

      private void verifyNotEqual(String message, Sequence other)
      Throws an exception if this sequence's toString() equals the given sequence's.
      Parameters:
      message - a diagnostic message
      other - a sequence whose toString() to compare to this
    • hashCode

      public final int hashCode()
      Overrides:
      hashCode in class Object
    • isValidIndex

      private boolean isValidIndex(int index)
      True iff this sequence contains a statement at the given index.
      Parameters:
      index - the index to check for a statement
      Returns:
      true if the index is the position of a statement in this sequence, false otherwise
    • getStatementsWithInputs

      private SimpleList<Statement> getStatementsWithInputs()
      An list of all the statements in this sequence.
      Returns:
      the list of all statements in this sequence
    • getStatementWithInputs

      private Statement getStatementWithInputs(int index)
      The statement(+inputs) at the given index.
      Parameters:
      index - the statement position
      Returns:
      the Statement at the given index
    • allVariablesForTypeLastStatement

      public List<Variable> allVariablesForTypeLastStatement(Type type, boolean onlyReceivers)
      Return all values of type type that are produced by, or might be side-effected by, the last statement. May return an empty list if onlyReceivers is true and the only values of the given type are nulls that are passed to the last statement as arguments.
      Parameters:
      type - return a list of sequences of this type
      onlyReceivers - if true, only return a sequence that is appropriate to use as a method call receiver
      Returns:
      a variable used in the last statement of the given type
    • randomVariableForTypeLastStatement

      public Variable randomVariableForTypeLastStatement(Type type, boolean onlyReceivers)
      The last statement produces multiple values of type type. Choose one of them at random.
      Parameters:
      type - return a sequence of this type
      onlyReceivers - if true, only return a sequence that is appropriate to use as a method call receiver
      Returns:
      a variable used in the last statement of the given type
    • randomVariableForType

      public Variable randomVariableForType(Type type, boolean onlyReceivers)
      Choose one of the statements that produces a values of type type.
      Parameters:
      type - return a sequence of this type
      onlyReceivers - if true, only return a sequence that is appropriate to use as a method call receiver
      Returns:
      a variable of the given type
    • checkIndex

      void checkIndex(int i)
    • checkInputs

      private void checkInputs(TypedOperation operation, List<Variable> inputVariables)
    • getInputsAsAbsoluteIndices

      public List<Integer> getInputsAsAbsoluteIndices(int i)
      The inputs for the ith statement, as indices. An index equal to x means that the input is the value created by the x-th statement in the sequence.
      Parameters:
      i - the statement index
      Returns:
      the absolute indices for the input variables in the given statement
    • appendCode

      public void appendCode(StringBuilder b, int index)
      Appends the statement at the given index to the StringBuilder.
      Parameters:
      b - the StringBuilder to which the code is appended
      index - the position of the statement to print in this Sequence
    • toParsableString

      public String toParsableString()
      Returns a string representing this sequence. The string can be parsed back into a sequence using the method Sequence.parse(String). In particular, the following invariant holds:
       st.equals(parse(st.toParsableCode()))
       
      See parse(List) for the required format of a String representing a Sequence.
      Returns:
      parsable string description of sequence
    • toParsableString

      private String toParsableString(String statementSep)
      Like toParsableString, but the client can specify a string that will be used a separator between statements.
      Parameters:
      statementSep - the statement separator
      Returns:
      the string representation of this sequence
    • parse

      public static Sequence parse(List<String> statements) throws SequenceParseException
      NOTE: the ONLY place this is used is in a test.

      Parse a sequence encoded as a list of strings, each string corresponding to one statement. This method is similar to parse(String), but expects the individual statements already as separate strings. Each statement is expected to be of the form:

         VAR = OPERATION : VAR ... VAR
       
      where the VAR are strings representing a variable name, and OPERATION is a string representing an Operation. For more on OPERATION, see the documentation for OperationParser.parse(String).

      The first VAR token represents the "output variable" that is the result of the statement call. The VAR tokens appearing after OPERATION represent the "input variables" to the statement call. At the i-th line, the input variable tokens should appear as an output variable for some previous j-th line, j < i.

      Here is an example of a list of lines representing a sequence.

      
       var0 = cons : java.util.HashMap.<init>() :
       var1 = prim : double:-1.0 :
       var2 = prim : java.lang.String:"hi!" :
       var3 = method : java.util.HashMap.put(java.lang.Object,java.lang.Object) : var0 var1 var2
       
      The above sequence corresponds to the following java code (with package names omitted for brevity):
       HashMap var0 = new HashMap();
       double var1 = -1.0;
       String var2 = "hi!";
       Object var3 = var0.put(var1, var2);
       
      When writing/reading sequences out to file: you have two options: serialize the sequences using java's serialization mechanism, or write them out as parsable text. Serialization is faster, and text is human-readable.
      Parameters:
      statements - the list of statement strings
      Returns:
      the sequence constructed from the list of strings
      Throws:
      SequenceParseException - if any statement cannot be parsed
    • parse

      public static Sequence parse(String string) throws SequenceParseException
      Parse a sequence encoded as a strings. Convenience method for parse(List), which parses a sequence of strings, each representing a Statement. See that method for more documentation on the string representation of a sequence.

      This method breaks up the given string into statements assuming that each statement is separated by a line separator character.

      The following invariant holds:

       st.equals(parse(st.toParsableCode()))
       
      When writing/reading sequences out to file: you have two options: serialize the sequences using java's serialization mechanism, or write them out as parsable text. Serialization is faster, and text is human-readable.
      Parameters:
      string - the string descriptor
      Returns:
      the sequence constructed by parsing the input string
      Throws:
      SequenceParseException - if string is not valid sequence
    • isNonreceiver

      public boolean isNonreceiver()
      A sequence representing a single primitive value, like "Foo var0 = null" or "int var0 = 1".
      Returns:
      true if this sequence is a single primitive initialization statement, false otherwise
    • hasUseOfMatchingClass

      public boolean hasUseOfMatchingClass(Pattern classNames)
      Test whether any statement of this sequence has an operation whose declaring class matches the given regular expression.
      Parameters:
      classNames - the regular expression to test class names
      Returns:
      true if any statement has operation with matching declaring class, false otherwise
    • getSubsequence

      Sequence getSubsequence(int index)
      Return a subsequence of this sequence that contains the statement at the given index. It does not necessarily contain the first element of this sequence.

      The result depends on the compositional structure of this sequence. The implementation avoids allocating new objects.

      Parameters:
      index - the statement position in this sequence
      Returns:
      the sequence containing the index position
    • log

      public void log()
      Write this sequence to the Randoop log.
    • shouldInlineLiterals

      boolean shouldInlineLiterals()
      By default, every value used in a sequence is named as a variable. If this variable is true, then primitive literals are used inline as arguments, without an explicit variable and initialization.
      Returns:
      true if literals should be inlined in this sequence, and false otherwise
    • doNotInlineLiterals

      public void doNotInlineLiterals()
      Disables inlining of variable values as arguments in this sequence. This is a hack to give the variable a name, so that post-conditions can refer to it.
    • getOperation

      public TypedOperation getOperation()
      Returns the operation from which this sequence was constructed. (Also known as the operation in the last statement of this sequence.
      Returns:
      the last operation of this sequence