Class Sequence
Statement
s.
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.
-
Nested Class Summary
Modifier and TypeClassDescription(package private) static final class
Used internally (i.e. -
Field Summary
Modifier and TypeFieldDescriptionprivate BitSet
A set of bits, where there is one bit associated with each index (that is, each statement in the sequence).The types of elements oflastStatementVariables
.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.private final int
private final int
private boolean
If true, inline primitive values rather than creating and using a variable.final SimpleList<Statement>
The list of statements. -
Constructor Summary
ModifierConstructorDescriptionSequence()
Create a new, empty sequence.Sequence
(SimpleList<Statement> statements) Create a sequence with the given statements.private
Sequence
(SimpleList<Statement> statements, int hashCode, int netSize) Create a sequence that has the given statements and hashCode (hashCode is for optimization). -
Method Summary
Modifier and TypeMethodDescriptionallVariablesForTypeLastStatement
(Type type, boolean onlyReceivers) Return all values of typetype
that are produced by, or might be side-effected by, the last statement.void
appendCode
(StringBuilder b, int index) Appends the statement at the given index to theStringBuilder
.(package private) void
checkIndex
(int i) private void
checkInputs
(TypedOperation operation, List<Variable> inputVariables) private void
checkRep()
Representation invariant check.void
clearActiveFlag
(int i) void
private static int
computeHashcode
(SimpleList<Statement> statements) The hashcode of a sequence is the sum of each statement's hashcode.private void
SetlastStatementVariables
andlastStatementTypes
.private static int
computeNetSize
(SimpleList<Statement> statements) Counts the number of statements in a list that are not initializations with a primitive type.static Sequence
concatenate
(List<Sequence> sequences) Create a new sequence that is the concatenation of the given sequences.static Sequence
createSequence
(TypedOperation operation, List<Sequence> inputSequences, List<Integer> indexes) Creates a sequence consisting of the given operation given the input.static Sequence
createSequence
(TypedOperation operation, TupleSequence elementsSequence) static Sequence
createSequenceForPrimitive
(Object value) Creates a sequence corresponding to the given non-null primitive value.void
Disables inlining of variable values as arguments in this sequence.final boolean
Two sequences are equal if their statements(+inputs) are element-wise equal.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.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.final Sequence
extend adds a new statement to this sequence using the operation of the given statement.getCreatingStatement
(Variable value) The statement that created this value.getInputs
(int statementIndex) The inputs for the ith statement.getInputsAsAbsoluteIndices
(int i) The inputs for the ith statement, as indices.The value created by the last statement in the sequence.Returns the operation from which this sequence was constructed.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.final Statement
getStatement
(int index) The statement for the statement at the given index.private SimpleList<Statement>
An list of all the statements in this sequence.private Statement
getStatementWithInputs
(int index) The statement(+inputs) at the given index.(package private) Sequence
getSubsequence
(int index) Return a subsequence of this sequence that contains the statement at the given index.The types of all the variables involved in the last statement.getVariable
(int i) The value created by the ith statement.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.The variables involved in the last statement.boolean
final int
hashCode()
boolean
hasUseOfMatchingClass
(Pattern classNames) Test whether any statement of this sequence has an operation whose declaring class matches the given regular expression.(package private) boolean
isActive
(int i) boolean
A sequence representing a single primitive value, like "Foo var0 = null" or "int var0 = 1".private boolean
isValidIndex
(int index) True iff this sequence contains a statement at the given index.void
log()
Write this sequence to the Randoop log.static Sequence
Parse a sequence encoded as a strings.static Sequence
NOTE: the ONLY place this is used is in a test.randomVariableForType
(Type type, boolean onlyReceivers) Choose one of the statements that produces a values of typetype
.randomVariableForTypeLastStatement
(Type type, boolean onlyReceivers) The last statement produces multiple values of typetype
.private void
(package private) boolean
By default, every value used in a sequence is named as a variable.final int
size()
The number of statements in the sequence.Returns the Java source code representation of this sequence with values substituted for simple initializations.private String
Returns the Java source representation of this sequence showing all statements.Returns a string representing this sequence.private String
toParsableString
(String statementSep) Like toParsableString, but the client can specify a string that will be used a separator between statements.toString()
private void
verifyNotEqual
(String message, Sequence other) Throws an exception if this sequence'stoString()
equals the given sequence's.static Sequence
Returns a sequence that is of the form "Foo f = null;" where Foo is the given class.
-
Field Details
-
statements
The list of statements. -
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
The types of elements oflastStatementVariables
. -
shouldInlineLiterals
private transient boolean shouldInlineLiteralsIf true, inline primitive values rather than creating and using a variable. -
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
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 sequencehashCode
- the hashcode for the new sequencenetSize
- the net size for the new sequence
-
Sequence
Create a sequence with the given statements.- Parameters:
statements
- the statements
-
-
Method Details
-
zero
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
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 sequenceinputSequences
- the sequences computing inputs to the operationindexes
- the indices of the inputs to the operation; same length as inputSequences- Returns:
- the sequence that applies the operation to the given inputs
-
createSequence
-
extend
Returns a new sequence that is equivalent to this sequence plus the given operation appended to the end.- Parameters:
operation
- the operation to addinputVariables
- the input variables- Returns:
- the sequence formed by appending the given operation to this sequence
-
extend
Returns a new sequence that is equivalent to this sequence plus the given statement appended at the end.- Parameters:
operation
- the operation to addinputs
- the input variables for the operation- Returns:
- the sequence formed by appending the given operation to this sequence
-
extend
extend adds a new statement to this sequence using the operation of the given statement. Intended as the only place that we reach inside aStatement
for its operation.- Parameters:
statement
- is aStatement
object from which the operation is copiedinputs
- is the list of variables for input- Returns:
- sequence constructed from this one plus the operation
- See Also:
-
concatenate
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
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
The value created by the ith statement.- Parameters:
i
- the statement index- Returns:
- the variable created by the statement at the given index
-
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
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
The value created by the last statement in the sequence.- Returns:
- the variable assigned to by the last statement of this sequence
-
getCreatingStatement
The statement that created this value.- Parameters:
value
- the variable- Returns:
- the statement that assigned to this variable
-
getInputs
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
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
Returns the Java source representation of this sequence showing all statements. Simplifications performed intoCodeString()
are preserved but initializations used will also be printed.- Returns:
- a string containing Java code for this sequence
-
toString
-
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 statementv
- the variable- Returns:
- the relative negative index computed from the position and variable
-
getVariableForInput
Returns the Variable corresponding to the given input, which is an input to the statement at position statementPosition.- Parameters:
statementPosition
- the statement positioninput
- relative index of the input variable- Returns:
- the variable at the relative index from the given statement position
-
computeHashcode
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
Counts the number of statements in a list that are not initializations with a primitive type. For instanceint var7 = 0
.- Parameters:
statements
- the list ofStatement
objects- Returns:
- count of statements other than primitive initializations
-
computeLastStatementInfo
private void computeLastStatementInfo()SetlastStatementVariables
andlastStatementTypes
. -
checkRep
private void checkRep()Representation invariant check. -
equals
Two sequences are equal if their statements(+inputs) are element-wise equal. -
verifyNotEqual
Throws an exception if this sequence'stoString()
equals the given sequence's.- Parameters:
message
- a diagnostic messageother
- a sequence whosetoString()
to compare to this
-
hashCode
public final int hashCode() -
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
An list of all the statements in this sequence.- Returns:
- the list of all statements in this sequence
-
getStatementWithInputs
The statement(+inputs) at the given index.- Parameters:
index
- the statement position- Returns:
- the
Statement
at the given index
-
allVariablesForTypeLastStatement
Return all values of typetype
that are produced by, or might be side-effected by, the last statement. May return an empty list ifonlyReceivers
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 typeonlyReceivers
- 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
The last statement produces multiple values of typetype
. Choose one of them at random.- Parameters:
type
- return a sequence of this typeonlyReceivers
- 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
Choose one of the statements that produces a values of typetype
.- Parameters:
type
- return a sequence of this typeonlyReceivers
- 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
-
getInputsAsAbsoluteIndices
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
Appends the statement at the given index to theStringBuilder
.- Parameters:
b
- theStringBuilder
to which the code is appendedindex
- the position of the statement to print in thisSequence
-
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()))
Seeparse(List)
for the required format of a String representing a Sequence.- Returns:
- parsable string description of sequence
-
toParsableString
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
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 forOperationParser.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.
The above sequence corresponds to the following java code (with package names omitted for brevity):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
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
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
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
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
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
-