randoop
Class Sequence

Object
  extended by Sequence
All Implemented Interfaces:
Serializable, WeightedElement

public final class Sequence
extends Object
implements Serializable, WeightedElement

Immutable.

A sequence of statements. Each element in the sequence represents a particular Statement, like a method call "Foo f = m(i1...iN)" or a declaration "int x = 0".

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.

See Also:
Serialized Form

Nested Class Summary
Modifier and Type Class and Description
protected static class Sequence.RelativeNegativeIndex
           
 
Field Summary
Modifier and Type Field and Description
 BitSet activeFlags
           
 double lastTimeUsed
           
protected  int savedHashCode
           
 SimpleList<Statement> statements
           
 
Constructor Summary
Constructor and Description
Sequence()
          Create a new, empty sequence.
Sequence(SimpleList<Statement> statements)
           
 
Method Summary
Modifier and Type Method and Description
protected  void checkIndex(int i)
           
 void clearActiveFlag(int i)
           
 void clearAllActiveFlags()
           
static Sequence concatenate(List<Sequence> sequences)
          Create a new sequence that is the concatenation of the given sequences.
static Sequence create(PrimitiveOrStringOrNullDecl info)
          Returns a sequence that consists of a single primitive declaration statement (e.g.
 boolean equals(Object o)
          Two sequences are equal if their statements(+inputs) are element-wise equal.
 Sequence extend(StatementKind statement, List<Variable> inputVariables)
          Returns a new sequence that is equivalent to this sequence plus the given statement appended at the end.
 Sequence extend(StatementKind statement, Variable... inputs)
          Returns a new sequence that is equivalent to this sequence plus the given statement appended at the end.
 List<Variable> getAllVariables()
          The values associated with this sequence.
 StatementKind getCreatingStatement(Variable value)
          The statement that created this value.
 Variable getFirstVariableFromLastStatementVariables()
           
 List<Variable> getInputs(int statementIndex)
          The inputs for the ith statement.
 List<Integer> getInputsAsAbsoluteIndices(int i)
          The inputs for the ith statement, as indices.
 StatementKind getLastStatement()
          The last Statement in the sequence.
 List<Class<?>> getLastStatementTypes()
          The types of all the variables involved in the last statement.
 List<Variable> getLastStatementVariables()
          The variables involved in the last statement.
 Variable getLastVariable()
          The value created by the last statement in the sequence.
 int getNetSize()
           
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.
 StatementKind getStatementKind(int index)
          The statement for the statement at the given index.
protected  Statement getStatementWithInputs(int index)
          The statement(+inputs) at the given index.
 Variable getVariable(int i)
          The value created by the ith statement.
 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.
 List<Variable> getVariablesOfType(Class<?> clazz, Reflection.Match match)
          All the variables declared in this sequences whose type matches the given class.
 double getWeight()
           
 boolean hasActiveFlags()
           
 int hashCode()
           
 boolean isActive(int i)
           
 boolean isOwnerOf(Variable v)
           
 boolean isPrimitive()
          A sequence representing a single primitive values, like "Foo var0 = null" or "int var0 = 1".
 int lastUseBefore(int idx, Variable var)
           
static Sequence parse(List<String> statements)
          Parse a sequence encoded as a list of strings, each string corresponding to one statement.
static Sequence parse(String string)
          Parse a sequence encoded as a strings.
 void printStatement(StringBuilder b, int index)
           
 Variable randomVariableForType(Class<?> t, Reflection.Match match)
           
 Variable randomVariableForTypeLastStatement(Class<?> t, Reflection.Match match)
           
 Variable randomVariableOfTypeLastStatement(Class<?> clazz, Reflection.Match match)
          A value declared in this sequence whose type matches the given class.
static void readTextSequences(String file, Collection<Sequence> collection)
          Reads a file containing a collection of sequences in textual representation.
 Sequence repeatLast(int times)
           
 void setActiveFlag(int i)
           
 void setAllActiveFlags()
           
 int size()
          The number of statements in the sequence.
 String toCodeString()
          A Java source code representation of this sequence.
 MSequence toModifiableSequence()
           
 String toParseableString()
          Returns a string representing this sequence.
 String toParseableString(String statementSep)
          Like toParseableString, but the client can specify a string that will be used a separator between statements.
 String toString()
          Equivalent to toParseableString().
 List<Variable> valuesAppearingInLastStatement(Class<?> clazz, Reflection.Match match)
          All the values declared in this sequences whose type matches the given class.
static Sequence zero(Class<?> c)
          Returns a sequence that is of the form "Foo f = null;" where Foo is the given class.
 
Methods inherited from class Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

lastTimeUsed

public double lastTimeUsed

statements

public final SimpleList<Statement> statements

activeFlags

public BitSet activeFlags

savedHashCode

protected final int savedHashCode
Constructor Detail

Sequence

public Sequence()
Create a new, empty sequence.


Sequence

public Sequence(SimpleList<Statement> statements)
Method Detail

getWeight

public double getWeight()
Specified by:
getWeight in interface WeightedElement

getStatementKind

public final StatementKind getStatementKind(int index)
The statement for the statement at the given index.


size

public final int size()
The number of statements in the sequence.


getVariable

public Variable getVariable(int i)
The value created by the ith statement.


getAllVariables

public List<Variable> getAllVariables()
The values associated with this sequence.


getLastStatementVariables

public List<Variable> getLastStatementVariables()
The variables involved in the last statement. This includes the output variable.


getLastStatementTypes

public List<Class<?>> getLastStatementTypes()
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 StatementKind, but the types of the variables.


getLastVariable

public Variable getLastVariable()
The value created by the last statement in the sequence.


getLastStatement

public StatementKind getLastStatement()
The last Statement in the sequence.


getCreatingStatement

public StatementKind getCreatingStatement(Variable value)
The statement that created this value.


getInputs

public List<Variable> getInputs(int statementIndex)
The inputs for the ith statement.


toCodeString

public String toCodeString()
A Java source code representation of this sequence.


toString

public String toString()
Equivalent to toParseableString().

Overrides:
toString in class Object

hasActiveFlags

public boolean hasActiveFlags()

isActive

public boolean isActive(int i)

setAllActiveFlags

public void setAllActiveFlags()

clearAllActiveFlags

public void clearAllActiveFlags()

setActiveFlag

public void setActiveFlag(int i)

clearActiveFlag

public void clearActiveFlag(int i)

getRelativeIndexForVariable

public 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.


getVariableForInput

public 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.


create

public static Sequence create(PrimitiveOrStringOrNullDecl info)
Returns a sequence that consists of a single primitive declaration statement (e.g. int i = 1;)


zero

public static Sequence zero(Class<?> c)
Returns a sequence that is of the form "Foo f = null;" where Foo is the given class.


equals

public final boolean equals(Object o)
Two sequences are equal if their statements(+inputs) are element-wise equal.

Overrides:
equals in class Object

getNetSize

public final int getNetSize()

hashCode

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

getStatementWithInputs

protected final Statement getStatementWithInputs(int index)
The statement(+inputs) at the given index.


randomVariableForTypeLastStatement

public Variable randomVariableForTypeLastStatement(Class<?> t,
                                                   Reflection.Match match)

randomVariableForType

public Variable randomVariableForType(Class<?> t,
                                      Reflection.Match match)

randomVariableOfTypeLastStatement

public final Variable randomVariableOfTypeLastStatement(Class<?> clazz,
                                                        Reflection.Match match)
A value declared in this sequence whose type matches the given class. Returns null if there are no matches.


valuesAppearingInLastStatement

public final List<Variable> valuesAppearingInLastStatement(Class<?> clazz,
                                                           Reflection.Match match)
All the values declared in this sequences whose type matches the given class. Returns an empty list if there are no matches.


getVariablesOfType

public List<Variable> getVariablesOfType(Class<?> clazz,
                                         Reflection.Match match)
All the variables declared in this sequences whose type matches the given class. Returns an empty list if there are no matches.


checkIndex

protected void checkIndex(int i)

extend

public final Sequence extend(StatementKind statement,
                             List<Variable> inputVariables)
Returns a new sequence that is equivalent to this sequence plus the given statement appended at the end.


extend

public final Sequence extend(StatementKind statement,
                             Variable... inputs)
Returns a new sequence that is equivalent to this sequence plus the given statement appended at the end.


concatenate

public static Sequence concatenate(List<Sequence> sequences)
Create a new sequence that is the concatenation of the given sequences.


getFirstVariableFromLastStatementVariables

public Variable getFirstVariableFromLastStatementVariables()

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.


printStatement

public void printStatement(StringBuilder b,
                           int index)

repeatLast

public Sequence repeatLast(int times)

toModifiableSequence

public MSequence toModifiableSequence()

isOwnerOf

public boolean isOwnerOf(Variable v)

toParseableString

public String toParseableString()
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.toParseableCode()))
See the parse for the required format of a String representing a Sequence.


toParseableString

public String toParseableString(String statementSep)
Like toParseableString, but the client can specify a string that will be used a separator between statements.


parse

public static Sequence parse(List<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 = STATEMENT_KIND : VAR ... VAR Where the VAR are strings representing a variable name, and STATEMENT_KIND is a string representing a StatementKind. For more on STATEMENT_KIND, see the documentation for StatementKinds.parse(String). The first VAR token represents the "output variable" that is the result of the statement call. The VAR tokens appearing after STATEMENT_KIND 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.() : 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 parseable text. Serialization is faster, and text is human-readable.

Throws:
SequenceParseException

parse

public static Sequence parse(String string)
                      throws SequenceParseException
Parse a sequence encoded as a strings. Convience 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.toParseableCode())) When writing/reading sequences out to file: you have two options: serialize the sequences using java's serialization mechanism, or write them out as parseable text. Serialization is faster, and text is human-readable.

Throws:
SequenceParseException

readTextSequences

public static void readTextSequences(String file,
                                     Collection<Sequence> collection)
Reads a file containing a collection of sequences in textual representation. The file should be made up of a list of records, each as follows: START SEQUENCE END SEQUENCE


lastUseBefore

public int lastUseBefore(int idx,
                         Variable var)

isPrimitive

public boolean isPrimitive()
A sequence representing a single primitive values, like "Foo var0 = null" or "int var0 = 1".