ProcedureManager

public class ProcedureManager extends Observable<ProcedureManager.Observer>

Manages procedure definitions, references and names.

Nested Class Summary

class ProcedureManager.ArgumentIndexUpdate Describes the re-indexing of argument order during a procedure mutation. 
interface ProcedureManager.Observer Callback listener for changes regarding procedures on the workspace. 

Constant Summary

String CALL_NO_RETURN_BLOCK_TYPE
String CALL_WITH_RETURN_BLOCK_TYPE
String DEFINE_NO_RETURN_BLOCK_TYPE
String DEFINE_WITH_RETURN_BLOCK_TYPE
String NAME_FIELD
String PROCEDURE_NAME_FIELD

Inherited Field Summary

Public Constructor Summary

ProcedureManager(BlocklyController controller, Workspace workspace)
Constructor for a new ProcedureManager.

Public Method Summary

void
addDefinition(Block block)
Adds a block containing a procedure definition to the managed list.
void
addReference(Block block)
Adds a reference to a procedure.
void
clear()
Removes all tracked procedures from this manager, and notifies all Observers.
boolean
boolean
containsDefinition(String procedureName)
Map<String, Block>
static List<String>
getProcedureArguments(Block block)
If the block is a procedure block, this getProcedureArgumentsgetProcedureArguments returns the argument list.
static String
getProcedureName(Block block)
If the block is a procedure definition or procedure call/reference, it returns the name of the procedure.
List<Block>
getReferences(String procedureName)
boolean
hasProcedureDefinitionWithReturn()
Returns whether the workspace includes a procedures_defreturn block.
boolean
static boolean
isDefinition(Block block)
Determines if a block is procedure definition.
static boolean
isReference(Block block)
Determines if a block is procedure call.
void
mutateProcedure(String originalProcedureName, ProcedureInfo updatedProcedureInfo, List<ProcedureManager.ArgumentIndexUpdate> argIndexUpdates)
Updates all blocks related to a specific procedure with respect to name, arguments, and whether the definition can contain a statement sequence.
void
mutateProcedure(Block procedureBlock, ProcedureInfo updatedProcedureInfo)
Convenience form of mutateProcedure(String, ProcedureInfo, List) that assumes the arguments maintain their same index.
List<Block>
removeDefinition(Block block)
Removes the block containing the procedure definition from the manager, and removes all references as well.
void
removeReference(Block block)
Removes a reference to a procedure.

Inherited Method Summary

Constants

public static final String CALL_NO_RETURN_BLOCK_TYPE

Constant Value: "procedures_callnoreturn"

public static final String CALL_WITH_RETURN_BLOCK_TYPE

Constant Value: "procedures_callreturn"

public static final String DEFINE_NO_RETURN_BLOCK_TYPE

Constant Value: "procedures_defnoreturn"

public static final String DEFINE_WITH_RETURN_BLOCK_TYPE

Constant Value: "procedures_defreturn"

public static final String NAME_FIELD

Constant Value: "name"

public static final String PROCEDURE_NAME_FIELD

Constant Value: "NAME"

Public Constructors

public ProcedureManager (BlocklyController controller, Workspace workspace)

Constructor for a new ProcedureManager.

Parameters
controller The controller managing the provided workspace.
workspace The workspace represented (passed in separately from the controller because the workspace constructor probably hasn't finished yet).

Public Methods

public void addDefinition (Block block)

Adds a block containing a procedure definition to the managed list. If a procedure by that name is already defined, creates a new unique name for the procedure and renames the block.

Parameters
block A block containing the definition of the procedure to add.

public void addReference (Block block)

Adds a reference to a procedure.

Parameters
block The reference to add.
Throws
IllegalStateException if the referenced procedure has not been defined.

public void clear ()

Removes all tracked procedures from this manager, and notifies all Observers.

public boolean containsDefinition (Block block)

Parameters
block

public boolean containsDefinition (String procedureName)

Parameters
procedureName

public Map<String, Block> getDefinitionBlocks ()

Returns
  • All the registered procedure definition blocks, keyed by canonical procedure name.

public static List<String> getProcedureArguments (Block block)

If the block is a procedure block, this getProcedureArgumentsgetProcedureArguments returns the argument list. Otherwise, it returns null.

Parameters
block The block queried.
Returns
  • The list of argument for defined or referenced by this block, if it is a procedure block. Otherwise null;

public static String getProcedureName (Block block)

If the block is a procedure definition or procedure call/reference, it returns the name of the procedure.

Parameters
block The block in question.
Returns
  • The name of the procedure defined or referenced by the block, if it is a procedure block. Otherwise, null.

public List<Block> getReferences (String procedureName)

Parameters
procedureName

public boolean hasProcedureDefinitionWithReturn ()

Returns whether the workspace includes a procedures_defreturn block. Used determine the visibility of procedures_ifreturn block in the ProcedureCustomCategory.

Returns
  • Whether the workspace includes a procedures_defreturn block.

public boolean hasReferences (Block block)

Parameters
block The block being referenced.
Returns
  • True if the block is referenced one or more times.

public static boolean isDefinition (Block block)

Determines if a block is procedure definition.

Parameters
block The block in question.
Returns
  • True, if the block type is a recognized type of procedure definition.

public static boolean isReference (Block block)

Determines if a block is procedure call.

Parameters
block The block in question.
Returns
  • True, if the block type is a recognized type of procedure call.

public void mutateProcedure (String originalProcedureName, ProcedureInfo updatedProcedureInfo, List<ProcedureManager.ArgumentIndexUpdate> argIndexUpdates)

Updates all blocks related to a specific procedure with respect to name, arguments, and whether the definition can contain a statement sequence. If any of the optional arguments are null, the existing values from the blocks are used.

Parameters
originalProcedureName The name of the procedure, before this method.
updatedProcedureInfo The info with which to update procedure mutators.
argIndexUpdates A list of mappings from original argument index to new index.
Throws
IllegalArgumentException If any originalProcedureName is not found, if optUpdatedProcedureName is not a valid procedure name, or if argument name is invalid.
IllegalStateException If updatedProcedureInfo fails to serialize or deserialize.

public void mutateProcedure (Block procedureBlock, ProcedureInfo updatedProcedureInfo)

Convenience form of mutateProcedure(String, ProcedureInfo, List) that assumes the arguments maintain their same index.

Parameters
procedureBlock A procedure block
updatedProcedureInfo The info with which to update procedure mutators.
Throws
IllegalArgumentException If the old and new argument counts do not match.

public List<Block> removeDefinition (Block block)

Removes the block containing the procedure definition from the manager, and removes all references as well. Returns a list of Blocks to recursively delete.

Parameters
block A block containing the definition of the procedure to remove.
Returns
  • A list of Blocks that referred to the procedure defined by block.

public void removeReference (Block block)

Removes a reference to a procedure.

Parameters
block The reference to remove.
Throws
IllegalStateException if the referenced procedure has not been defined..