net.ponder2.objects
Class P2Block

java.lang.Object
  extended by net.ponder2.objects.P2Object
      extended by net.ponder2.objects.P2Block
All Implemented Interfaces:
java.io.Serializable, P2Serializable, ManagedObject, Ponder2Message

public class P2Block
extends P2Object
implements ManagedObject

Acts as a closure encompassing PonderTalk statements. Any variables used by the statements within the block are untouched once the block is created. Blocks may have arguments that are handed in when they are executed. When a block is executed it returns returns the value of the last statement executed within the block. c.f. function calls.

Version:
$Id:$
Author:
Kevin Twidle
See Also:
Serialized Form

Constructor Summary
protected P2Block()
          constructor for Ponder2 serialisation
  P2Block(java.util.Map<java.lang.String,P2Object> variables, com.twicom.qdparser.TaggedElement block)
           
 
Method Summary
 P2Block asBlock()
          Returns itself as a Ponder2 block
protected  P2Object execute(P2Object source, P2Object... args)
          Executes the block with anArray of arguments.
 P2Object getVariable(java.lang.String var)
          Used for testing.
protected  P2Object onError(P2Block anErrorBlock)
          associates anErrorBlock with the receiver.
 P2Object operation_array(P2Object source, P2Array anArray)
          Executes the block with anArray of arguments.
 P2Object operation_array(P2Object source, P2Hash vars, P2Array anArray)
          Executes the block with anArray of arguments.
protected  boolean operation_hasArgs(P2Object source, P2Object aHash)
          Returns true if aHash contains entries that match all the names of the arguments that the block requires.
 P2Object operation_value0(P2Object source)
          executes the block with no arguments.
 P2Object operation_value1(P2Object source, P2Object arg0)
          Executes the block with one argument: arg0.
 P2Object operation_value2(P2Object source, P2Object arg0, P2Object arg1)
          Executes the block with two arguments: arg0 and arg1.
 P2Object operation_value3(P2Object source, P2Object arg0, P2Object arg1, P2Object arg2)
          Executes the block with three arguments: arg0, arg1 and arg2.
 P2Object operation_valueHash(P2Object source, P2Object aHash)
          The block is executed with the values of its arguments being taken by name from aHash.
 P2Object operation_valueVars(P2Object source, P2Object aHash)
          The block is executed with the values in aHash being added to the block's environment variables.
 P2Object readXml(com.twicom.qdparser.TaggedElement xml, java.util.Map<java.lang.Integer,P2Serializable> read)
           
 java.lang.String toString()
           
protected  void whileFalse(P2Object source, P2Object aBlock)
          While the receiver is false, keep executing aBlock with no arguments
protected  void whileTrue(P2Object source, P2Object aBlock)
          While the receiver is true, keep executing aBlock with no arguments
 com.twicom.qdparser.TaggedElement writeXml(java.util.Set<P2Object> written)
           
 
Methods inherited from class net.ponder2.objects.P2Object
asArray, asBoolean, asClass, asDouble, asFloat, asHash, asInteger, asLong, asNumber, asP2Array, asString, create, create, create, create, create, create, create, create, create, create, create, create, create, create, create, create, create, fromXml, getManagedObject, getOID, getSMC, operation, operation, setSMC
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

P2Block

protected P2Block()
constructor for Ponder2 serialisation


P2Block

public P2Block(java.util.Map<java.lang.String,P2Object> variables,
               com.twicom.qdparser.TaggedElement block)
Method Detail

asBlock

public P2Block asBlock()
                throws Ponder2ArgumentException
Description copied from class: P2Object
Returns itself as a Ponder2 block

Overrides:
asBlock in class P2Object
Returns:
the block value
Throws:
Ponder2ArgumentException

operation_hasArgs

protected boolean operation_hasArgs(P2Object source,
                                    P2Object aHash)
                             throws Ponder2Exception
Returns true if aHash contains entries that match all the names of the arguments that the block requires. Otherwise returns false.

Parameters:
aHash - the named arguments for the block
Returns:
a Ponder2 boolean, true if the arguments match the hash
Throws:
Ponder2ArgumentException
Ponder2Exception

operation_valueHash

public P2Object operation_valueHash(P2Object source,
                                    P2Object aHash)
                             throws Ponder2Exception
The block is executed with the values of its arguments being taken by name from aHash. Answers the value of the last statement executed by the block. Throws an error if aHash does not satisfy the block's arguments.

Parameters:
aHash - the named arguments for the block
Returns:
the result of the block execution
Throws:
Ponder2Exception

operation_valueVars

public P2Object operation_valueVars(P2Object source,
                                    P2Object aHash)
                             throws Ponder2Exception
The block is executed with the values in aHash being added to the block's environment variables. Variables in aHash will overwrite environment variables with the same name. The values are added for this call only and do not persist. Answers the value of the last statement executed by the block.

Parameters:
aHash - the named arguments for the block
Returns:
the result of the block execution
Throws:
Ponder2Exception

operation_value0

public P2Object operation_value0(P2Object source)
                          throws Ponder2Exception
executes the block with no arguments. Answers with the result of the last statement executed by the block.

Parameters:
source - the originator of the command
Returns:
the result of the block execution
Throws:
Ponder2Exception

operation_value1

public P2Object operation_value1(P2Object source,
                                 P2Object arg0)
                          throws Ponder2Exception
Executes the block with one argument: arg0. Answers with the result of the last statement executed by the block.

Parameters:
source - the originator of the command
arg0 - the first argument
Returns:
the result of the block execution
Throws:
Ponder2Exception

operation_value2

public P2Object operation_value2(P2Object source,
                                 P2Object arg0,
                                 P2Object arg1)
                          throws Ponder2Exception
Executes the block with two arguments: arg0 and arg1. Answers with the result of the last statement executed by the block.

Parameters:
source - the originator of the command
arg0 - the first argument
arg1 - the second argument
Returns:
the result of the block execution
Throws:
Ponder2Exception

operation_value3

public P2Object operation_value3(P2Object source,
                                 P2Object arg0,
                                 P2Object arg1,
                                 P2Object arg2)
                          throws Ponder2Exception
Executes the block with three arguments: arg0, arg1 and arg2. Answers with the result of the last statement executed by the block.

Parameters:
source - the originator of the command
arg0 - the first argument
arg1 - the second argument
arg2 - the third argument
Returns:
the result of the block execution
Throws:
Ponder2Exception

operation_array

public P2Object operation_array(P2Object source,
                                P2Array anArray)
                         throws Ponder2Exception
Executes the block with anArray of arguments. Answers with the result of the last statement executed by the block.

Parameters:
source - the originator of the command
anArray - the Ponder2 array of arguments to be used
Returns:
the result of the block execution
Throws:
Ponder2Exception

operation_array

public P2Object operation_array(P2Object source,
                                P2Hash vars,
                                P2Array anArray)
                         throws Ponder2Exception
Executes the block with anArray of arguments. Answers with the result of the last statement executed by the block.

Parameters:
source - the originator of the command
vars - extra vars to be added to the environment
anArray - the Ponder2 array of arguments to be used
Returns:
the result of the block execution
Throws:
Ponder2Exception

whileTrue

protected void whileTrue(P2Object source,
                         P2Object aBlock)
                  throws Ponder2Exception
While the receiver is true, keep executing aBlock with no arguments

Parameters:
source - the originator of the command
aBlock - the block to be executed
Throws:
Ponder2Exception

whileFalse

protected void whileFalse(P2Object source,
                          P2Object aBlock)
                   throws Ponder2Exception
While the receiver is false, keep executing aBlock with no arguments

Parameters:
source - the originator of the command
aBlock - the block to be executed
Throws:
Ponder2Exception

execute

protected P2Object execute(P2Object source,
                           P2Object... args)
                    throws Ponder2Exception
Executes the block with anArray of arguments. Answers with the result of the last statement executed by the block.

Parameters:
source - the originator of the command
args - the array of arguments to be used
Returns:
the result of the block execution
Throws:
Ponder2Exception

onError

protected P2Object onError(P2Block anErrorBlock)
associates anErrorBlock with the receiver. The error block is executed if a Ponder2 error occurs in the receiver. The error block is given one P2Error argument which contains the error details.

Parameters:
anErrorBlock -

writeXml

public com.twicom.qdparser.TaggedElement writeXml(java.util.Set<P2Object> written)
                                           throws Ponder2OperationException
Specified by:
writeXml in interface P2Serializable
Overrides:
writeXml in class P2Object
Throws:
Ponder2OperationException

readXml

public P2Object readXml(com.twicom.qdparser.TaggedElement xml,
                        java.util.Map<java.lang.Integer,P2Serializable> read)
                 throws Ponder2OperationException,
                        Ponder2ArgumentException
Specified by:
readXml in interface P2Serializable
Specified by:
readXml in class P2Object
Throws:
Ponder2OperationException
Ponder2ArgumentException

getVariable

public P2Object getVariable(java.lang.String var)
Used for testing. Returns the value of the named variable.

Parameters:
var - the name of the variable
Returns:
the value of the named variable

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2008 Imperial College. All Rights Reserved.