public final class Sequence
extends java.lang.Object
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.
Modifier and Type | Class and Description |
---|---|
static class |
Sequence.RelativeNegativeIndex
Used internally (i.e.
|
Modifier and Type | Field and Description |
---|---|
private java.util.BitSet |
activeFlags
A set of bits, where there is one bit associated with each index (that is, each statement in
the sequence).
|
private java.util.List<Type> |
lastStatementTypes
The types of elements of
lastStatementVariables . |
private java.util.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.
|
private int |
savedHashCode |
private int |
savedNetSize |
private boolean |
shouldInlineLiterals
If true, inline primitive values rather than creating and using a variable.
|
SimpleList<Statement> |
statements
The list of statements.
|
Modifier | Constructor and Description |
---|---|
|
Sequence()
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).
|
Modifier and Type | Method and Description |
---|---|
java.util.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. |
void |
appendCode(java.lang.StringBuilder b,
int index)
Appends the statement at the given index to the
StringBuilder . |
(package private) void |
checkIndex(int i) |
private void |
checkInputs(TypedOperation operation,
java.util.List<Variable> inputVariables)
Argument checker for
extend(randoop.operation.TypedOperation, java.util.List<randoop.sequence.Variable>) method. |
private void |
checkRep()
Representation invariant check.
|
void |
clearActiveFlag(int i) |
void |
clearAllActiveFlags() |
private static int |
computeHashcode(SimpleList<Statement> statements)
The hashcode of a sequence is the sum of each statement's hashcode.
|
private void |
computeLastStatementInfo()
Set
lastStatementVariables and lastStatementTypes . |
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(java.util.List<Sequence> sequences)
Create a new sequence that is the concatenation of the given sequences.
|
static Sequence |
concatenate(Sequence... sequences)
Create a new sequence that is the concatenation of the given sequences.
|
static Sequence |
createSequence(TypedOperation operation,
java.util.List<Sequence> inputSequences,
java.util.List<java.lang.Integer> indexes)
Creates a sequence that extends the concatenation of
inputSequences with one call
consisting of the given operation given the input. |
static Sequence |
createSequence(TypedOperation operation,
TupleSequence elementsSequence) |
static Sequence |
createSequenceForPrimitive(java.lang.Object value)
Creates a singleton sequence corresponding to the given non-null primitive value.
|
void |
doNotInlineLiterals()
Disables inlining of variable values as arguments in this sequence.
|
boolean |
equals(@Nullable java.lang.Object o)
Two sequences are equal if their statements(+inputs) are element-wise equal.
|
Sequence |
extend(Statement statement,
java.util.List<Variable> inputs)
extend adds a new statement to this sequence using the operation of the given statement.
|
Sequence |
extend(TypedOperation operation,
java.util.List<Variable> inputVariables)
Returns a new sequence that is equivalent to this sequence plus the given operation appended to
the end.
|
Sequence |
extend(TypedOperation operation,
Variable... inputs)
Returns a new sequence that is equivalent to this sequence plus the given statement appended at
the end.
|
Statement |
getCreatingStatement(Variable value)
The statement that created this value.
|
java.util.List<Variable> |
getInputs(int statementIndex)
The inputs for the ith statement.
|
java.util.List<java.lang.Integer> |
getInputsAsAbsoluteIndices(int i)
The inputs for the ith statement, as indices.
|
Variable |
getLastVariable()
The value created by the last statement in the sequence.
|
TypedOperation |
getOperation()
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.
|
Statement |
getStatement(int index)
The statement for the statement at the given index.
|
private SimpleList<Statement> |
getStatementsWithInputs()
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.
|
(package private) java.util.List<Type> |
getTypesForLastStatement()
The types of all the variables involved in the last statement.
|
Variable |
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.
|
(package private) java.util.List<Variable> |
getVariablesOfLastStatement()
The variables involved in the last statement.
|
boolean |
hasActiveFlags() |
int |
hashCode() |
boolean |
hasUseOfMatchingClass(java.util.regex.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 |
isNonreceiver()
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.
|
private boolean |
matchesVariable(Variable var,
Type type,
boolean onlyReceivers)
Checks if the given variable matches the specified type and receiver conditions.
|
int |
numMethodCalls()
The number of method calls in this sequence.
|
static Sequence |
parse(java.util.List<java.lang.String> statements)
NOTE: the ONLY place this is used is in a test.
|
static Sequence |
parse(java.lang.String string)
Parse a sequence encoded as a strings.
|
Variable |
randomVariableForType(Type type,
boolean onlyReceivers)
Choose one of the statements that produces a values of type
type . |
Variable |
randomVariableForTypeLastStatement(Type type,
boolean onlyReceivers)
The last statement produces multiple values of type
type . |
private void |
setAllActiveFlags() |
(package private) boolean |
shouldInlineLiterals()
By default, every value used in a sequence is named as a variable.
|
int |
size()
The number of statements in the sequence.
|
java.lang.String |
toCodeString()
Returns the Java source code representation of this sequence with values substituted for simple
initializations.
|
private java.lang.String |
toFullCodeString()
Returns the Java source representation of this sequence showing all statements.
|
java.lang.String |
toParsableString()
Returns a string representing this sequence.
|
private java.lang.String |
toParsableString(java.lang.String statementSep)
Like toParsableString, but the client can specify a string that will be used a separator
between statements.
|
java.lang.String |
toString() |
private void |
verifyNotEqual(java.lang.String message,
Sequence other)
Throws an exception if this sequence's
toString() equals the given sequence's. |
static Sequence |
zero(Type c)
Returns a singleton sequence that is of the form "Foo f = null;" where Foo is the given class.
|
public final SimpleList<Statement> statements
private final transient java.util.List<Variable> lastStatementVariables
private final transient java.util.List<Type> lastStatementTypes
lastStatementVariables
.private transient boolean shouldInlineLiterals
private java.util.BitSet activeFlags
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.
private final int savedHashCode
private final int savedNetSize
public Sequence()
private Sequence(SimpleList<Statement> statements, int hashCode, int netSize)
See computeHashcode(SimpleList)
for details on the hashCode.
statements
- the statements of the new sequencehashCode
- the hashcode for the new sequencenetSize
- the net size for the new sequencepublic Sequence(SimpleList<Statement> statements)
statements
- the statementspublic static Sequence zero(Type c)
c
- the type for initialized variablepublic static Sequence createSequenceForPrimitive(java.lang.Object value)
value
- non-null reference to a primitive or String valueSequence
consisting of a statement created with the objectpublic static Sequence createSequence(TypedOperation operation, java.util.List<Sequence> inputSequences, java.util.List<java.lang.Integer> indexes)
inputSequences
with one call
consisting of the given operation given the input.operation
- the operation for the sequenceinputSequences
- the sequences computing inputs to the operationindexes
- the indices of the inputs to the operation; same length as inputSequencespublic static Sequence createSequence(TypedOperation operation, TupleSequence elementsSequence)
public final Sequence extend(TypedOperation operation, java.util.List<Variable> inputVariables)
operation
- the operation to addinputVariables
- the input variablespublic final Sequence extend(TypedOperation operation, Variable... inputs)
operation
- the operation to addinputs
- the input variables for the operationpublic final Sequence extend(Statement statement, java.util.List<Variable> inputs)
Statement
for its operation.statement
- is a Statement
object from which the operation is copiedinputs
- is the list of variables for inputextend(TypedOperation, List)
public static Sequence concatenate(java.util.List<Sequence> sequences)
sequences
- the list of sequences to concatenatepublic static Sequence concatenate(Sequence... sequences)
sequences
- the sequences to concatenatepublic final Statement getStatement(int index)
index
- the index of the statement position in this sequence@Pure public final int size(@UnknownInitialization Sequence this)
public final int numMethodCalls()
public Variable getVariable(int i)
i
- the statement indexjava.util.List<Variable> getVariablesOfLastStatement()
java.util.List<Type> getTypesForLastStatement()
public Variable getLastVariable()
public Statement getCreatingStatement(Variable value)
value
- the variablepublic java.util.List<Variable> getInputs(int statementIndex)
statementIndex
- the index for the statement@SideEffectFree public java.lang.String toCodeString()
ExecutableSequence.toCodeString()
except does not include
checks.private java.lang.String toFullCodeString()
toCodeString()
are preserved but initializations used will also be
printed.public java.lang.String toString()
toString
in class java.lang.Object
public boolean hasActiveFlags()
boolean isActive(int i)
private void setAllActiveFlags(@UnderInitialization Sequence this)
public void clearAllActiveFlags()
public void clearActiveFlag(int i)
private static Sequence.RelativeNegativeIndex getRelativeIndexForVariable(int statementPosition, Variable v)
statementPosition
- the position of the statementv
- the variableprivate Variable getVariableForInput(int statementPosition, Sequence.RelativeNegativeIndex input)
statementPosition
- the statement positioninput
- relative index of the input variableprivate static int computeHashcode(SimpleList<Statement> statements)
statements
- the list of statements over which to compute the hash codeprivate static int computeNetSize(SimpleList<Statement> statements)
int var7 = 0
.statements
- the list of Statement
objectsprivate void computeLastStatementInfo(@UnderInitialization Sequence this)
lastStatementVariables
and lastStatementTypes
.private void checkRep(@UnknownInitialization(value=Sequence.class) Sequence this)
public final boolean equals(@Nullable java.lang.Object o)
equals
in class java.lang.Object
private void verifyNotEqual(java.lang.String message, Sequence other)
toString()
equals the given sequence's.message
- a diagnostic messageother
- a sequence whose toString()
to compare to thispublic final int hashCode()
hashCode
in class java.lang.Object
private boolean isValidIndex(int index)
index
- the index to check for a statementprivate SimpleList<Statement> getStatementsWithInputs()
private Statement getStatementWithInputs(int index)
index
- the statement positionStatement
at the given indexpublic java.util.List<Variable> allVariablesForTypeLastStatement(Type type, boolean onlyReceivers)
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.type
- return a list of sequences of this typeonlyReceivers
- if true, only return a sequence that is appropriate to use as a method
call receiverprivate boolean matchesVariable(Variable var, Type type, boolean onlyReceivers)
var
- the variable to checktype
- the type to matchonlyReceivers
- whether to restrict to receiver variablespublic Variable randomVariableForTypeLastStatement(Type type, boolean onlyReceivers)
type
. Choose one of them at random.type
- return a sequence of this typeonlyReceivers
- if true, only return a sequence that is appropriate to use as a method
call receiverpublic Variable randomVariableForType(Type type, boolean onlyReceivers)
type
.type
- return a sequence of this typeonlyReceivers
- if true, only return a sequence that is appropriate to use as a method
call receivervoid checkIndex(int i)
private void checkInputs(TypedOperation operation, java.util.List<Variable> inputVariables)
extend(randoop.operation.TypedOperation, java.util.List<randoop.sequence.Variable>)
method. These checks should be caught by checkRep()
too.operation
- the operation to addinputVariables
- the input variablespublic java.util.List<java.lang.Integer> getInputsAsAbsoluteIndices(int i)
i
- the statement indexpublic void appendCode(java.lang.StringBuilder b, int index)
StringBuilder
.b
- the StringBuilder
to which the code is appendedindex
- the position of the statement to print in this Sequence
public java.lang.String toParsableString()
st.equals(parse(st.toParsableCode()))See
parse(List)
for the required format of a String representing a Sequence.private java.lang.String toParsableString(java.lang.String statementSep)
statementSep
- the statement separatorpublic static Sequence parse(java.util.List<java.lang.String> statements) throws SequenceParseException
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 ... VARwhere 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.
statements
- the list of statement stringsSequenceParseException
- if any statement cannot be parsedpublic static Sequence parse(java.lang.String string) throws SequenceParseException
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.
string
- the string descriptorSequenceParseException
- if string is not valid sequencepublic boolean isNonreceiver()
public boolean hasUseOfMatchingClass(java.util.regex.Pattern classNames)
classNames
- the regular expression to test class namesSequence getSubsequence(int index)
The result depends on the compositional structure of this sequence. The implementation avoids allocating new objects.
index
- the statement position in this sequencepublic void log()
boolean shouldInlineLiterals()
public void doNotInlineLiterals()
public TypedOperation getOperation()