Package org.protelis.vm.impl
Class AbstractExecutionContext<S extends AbstractExecutionContext<S>>
- java.lang.Object
-
- org.protelis.vm.impl.AbstractExecutionContext<S>
-
- Type Parameters:
S- self-type. Subclasses must parameterize AbstractExecutionContext with themselves, and return themselves in instance(). This forces a compiler check on the type of instanced contexts, ensuring (if no foolish cast is used) that restricted contexts have all the methods available in the mainExecutionContext. For instance, if your class is MyContext, it should be written as MyContext extends AbstractExecutionContext<MyContext>.
- All Implemented Interfaces:
ExecutionContext
public abstract class AbstractExecutionContext<S extends AbstractExecutionContext<S>> extends java.lang.Object implements ExecutionContext
Partial implementation of ExecutionContext, containing functionality expected to be shared between most implementations. Instantiations of Protelis should generally extend this class.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractExecutionContext(ExecutionEnvironment execenv, NetworkManager netmgr)Create a new AbstractExecutionContext with a default, time-efficient code path factory.protectedAbstractExecutionContext(ExecutionEnvironment executionEnvironment, NetworkManager networkManager, CodePathFactory codePathFactory)Create a new AbstractExecutionContext with the specifiedCodePathFactory.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description <T,R>
Field<R>buildField(java.util.function.Function<T,R> computeValue, T localValue)Builds a newField, fetching data from all the aligned neighbors.<T,R>
Field<R>buildFieldDeferred(java.util.function.Function<T,R> computeValue, T currentLocal, java.util.function.Supplier<T> toBeSent)Builds a newField, fetching data from all the aligned neighbors.voidcommit()Called just after the VM is executed, to finalize information of the execution for the environment.protected voidcommitRecursively()recursively commits on restricted contexts.java.lang.NumbergetDeltaTime()ExecutionEnvironmentgetExecutionEnvironment()protected java.util.Map<Reference,?>getFunctions()Support for first-class functions by returning the set of currently accessible functions.protected NetworkManagergetNetworkManager()Accessor for abstract network interface.<P> PgetPersistent(java.util.function.Supplier<P> ifAbsent)Gets a value that survives across rounds.java.util.Map<CodePath,java.lang.Object>getStoredState()java.lang.ObjectgetVariable(Reference name)Look up the value of a variable from the local environment.protected abstract Sinstance()Produce a child execution context, for encapsulated evaluation of sub-programs.voidnewCallStackFrame(byte... id)voidnewCallStackFrame(int... id)voidputMultipleVariables(java.util.Map<Reference,?> map)Pushes multiple variables.voidputVariable(Reference name, java.lang.Object value)Puts a variable value, overwriting the previous one, if any.SrestrictDomain(Field<?> f)Give a field, returns a newExecutionContextwhose domain is the same of the field one.voidreturnFromCallFrame()returns from the last frame.<T> TrunInNewStackFrame(int id, java.util.function.Function<ExecutionContext,T> operation)voidsetGloballyAvailableReferences(java.util.Map<Reference,?> knownFunctions)Used internally to support first-class functions by make the functions of a program accessible for reflection at runtime.voidsetPersistent(java.lang.Object o)Stores a value to be used in future rounds via getPersistent.voidsetup()Called just before the VM is executed, to enable and preparations needed in the environment.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.protelis.vm.ExecutionContext
getCurrentTime, getDeviceUID, nextRandomDouble
-
-
-
-
Constructor Detail
-
AbstractExecutionContext
protected AbstractExecutionContext(ExecutionEnvironment execenv, NetworkManager netmgr)
Create a new AbstractExecutionContext with a default, time-efficient code path factory.- Parameters:
execenv- The execution environmentnetmgr- Abstract network interface to be used
-
AbstractExecutionContext
protected AbstractExecutionContext(ExecutionEnvironment executionEnvironment, NetworkManager networkManager, CodePathFactory codePathFactory)
Create a new AbstractExecutionContext with the specifiedCodePathFactory. Subclasses which want to use hashing or other means to encodeCodePaths can call this constructor, e.g.:super(executionEnvironment, networkManager, new HashingCodePathFactory(Hashing.sha256()));
- Parameters:
executionEnvironment- The execution environmentnetworkManager- Abstract network interface to be usedcodePathFactory- The code path factory to use
-
-
Method Detail
-
buildField
public final <T,R> Field<R> buildField(java.util.function.Function<T,R> computeValue, T localValue)
Description copied from interface:ExecutionContextBuilds a newField, fetching data from all the aligned neighbors. A neighbor is considered to be aligned it it has reached the exact sameDefaultTimeEfficientCodePath. The field will always contain at least one value, namely the value of the local device.- Specified by:
buildFieldin interfaceExecutionContext- Type Parameters:
T- the type of the inputR- the type of the output- Parameters:
computeValue- a function that will be applied to localValue and the equivalents shared from neighbors in the process of constructing this field: the field consists of the values returned from applying computeValue to each of device's valuelocalValue- the local value for this field- Returns:
- a new
Fieldcontaining the local device value and the values for any of the aligned neighbors
-
buildFieldDeferred
public final <T,R> Field<R> buildFieldDeferred(java.util.function.Function<T,R> computeValue, T currentLocal, java.util.function.Supplier<T> toBeSent)
Description copied from interface:ExecutionContextBuilds a newField, fetching data from all the aligned neighbors. A neighbor is considered to be aligned it it has reached the exact sameDefaultTimeEfficientCodePath. The field will always contain at least one value, namely the value of the local device. The deferred version does not immediately schedule the local value for being sent away. Rather, it schedules the providedSupplierto be executed at the end of the round for obtaining the value to be shared. This function is the base upon which theShareCallis built.- Specified by:
buildFieldDeferredin interfaceExecutionContext- Type Parameters:
T- the type of the inputR- the type of the output- Parameters:
computeValue- a function that will be applied to localValue and the equivalents shared from neighbors in the process of constructing this field: the field consists of the values returned from applying computeValue to each of device's valuecurrentLocal- the value to be used as local for this fieldtoBeSent- aSupplierwhich will be used to compute the local value for this field that will get exported at the end of the round- Returns:
- a new
Fieldcontaining the local device value and the values for any of the aligned neighbors
-
commit
public final void commit()
Description copied from interface:ExecutionContextCalled just after the VM is executed, to finalize information of the execution for the environment.- Specified by:
commitin interfaceExecutionContext
-
commitRecursively
protected final void commitRecursively()
recursively commits on restricted contexts.
-
getDeltaTime
public java.lang.Number getDeltaTime()
- Specified by:
getDeltaTimein interfaceExecutionContext- Returns:
- the current time, relative to last round
-
getExecutionEnvironment
public final ExecutionEnvironment getExecutionEnvironment()
- Specified by:
getExecutionEnvironmentin interfaceExecutionContext- Returns:
- The current
ExecutionEnvironment
-
getFunctions
protected final java.util.Map<Reference,?> getFunctions()
Support for first-class functions by returning the set of currently accessible functions.- Returns:
- Map from function name to function objects
-
getNetworkManager
protected final NetworkManager getNetworkManager()
Accessor for abstract network interface.- Returns:
- Current abstract network interface
-
getPersistent
public final <P> P getPersistent(java.util.function.Supplier<P> ifAbsent)
Description copied from interface:ExecutionContextGets a value that survives across rounds. If a value was stored at the previous iteration, then the previous value is returned. Otherwise, the alternative value is computed via isAbsent and stored for the future.- Specified by:
getPersistentin interfaceExecutionContext- Type Parameters:
P- type of the value- Parameters:
ifAbsent- replacement- Returns:
- the value
-
getVariable
public final java.lang.Object getVariable(Reference name)
Description copied from interface:ExecutionContextLook up the value of a variable from the local environment.- Specified by:
getVariablein interfaceExecutionContext- Parameters:
name- The variable to be looked up- Returns:
- Value of the variable, or null if it cannot be found.
-
getStoredState
public final java.util.Map<CodePath,java.lang.Object> getStoredState()
- Returns:
- a view on the state stored in this context.
-
instance
protected abstract S instance()
Produce a child execution context, for encapsulated evaluation of sub-programs.- Returns:
- Child execution context
-
newCallStackFrame
public final void newCallStackFrame(byte... id)
- Specified by:
newCallStackFramein interfaceExecutionContext- Parameters:
id- stack frame type
-
newCallStackFrame
public final void newCallStackFrame(int... id)
- Specified by:
newCallStackFramein interfaceExecutionContext- Parameters:
id- stack frame type
-
putMultipleVariables
public final void putMultipleVariables(java.util.Map<Reference,?> map)
Description copied from interface:ExecutionContextPushes multiple variables.- Specified by:
putMultipleVariablesin interfaceExecutionContext- Parameters:
map- the variables to push
-
putVariable
public final void putVariable(Reference name, java.lang.Object value)
Description copied from interface:ExecutionContextPuts a variable value, overwriting the previous one, if any.- Specified by:
putVariablein interfaceExecutionContext- Parameters:
name- variable referencevalue- variable value
-
restrictDomain
public final S restrictDomain(@Nonnull Field<?> f)
Description copied from interface:ExecutionContextGive a field, returns a newExecutionContextwhose domain is the same of the field one.- Specified by:
restrictDomainin interfaceExecutionContext- Parameters:
f- the field- Returns:
- the restricted domain
-
returnFromCallFrame
public final void returnFromCallFrame()
Description copied from interface:ExecutionContextreturns from the last frame.- Specified by:
returnFromCallFramein interfaceExecutionContext
-
runInNewStackFrame
public final <T> T runInNewStackFrame(int id, java.util.function.Function<ExecutionContext,T> operation)- Specified by:
runInNewStackFramein interfaceExecutionContext- Type Parameters:
T- the return type- Parameters:
id- stack frame typeoperation- the operation to run in the new context- Returns:
- the result of the evaluation
-
setGloballyAvailableReferences
public final void setGloballyAvailableReferences(java.util.Map<Reference,?> knownFunctions)
Description copied from interface:ExecutionContextUsed internally to support first-class functions by make the functions of a program accessible for reflection at runtime.- Specified by:
setGloballyAvailableReferencesin interfaceExecutionContext- Parameters:
knownFunctions- Collection of accessible functions, associating function name and value.
-
setPersistent
public final void setPersistent(java.lang.Object o)
Description copied from interface:ExecutionContextStores a value to be used in future rounds via getPersistent.- Specified by:
setPersistentin interfaceExecutionContext- Parameters:
o- the value to store
-
setup
public final void setup()
Description copied from interface:ExecutionContextCalled just before the VM is executed, to enable and preparations needed in the environment.- Specified by:
setupin interfaceExecutionContext
-
-