📄 stenergy.java
字号:
/* An actor that outputs the sum of the inputs so far.
*/
package ptolemy.actor.lib;
import ptolemy.actor.IOPort;
import ptolemy.actor.TypedIOPort;
import ptolemy.data.BooleanToken;
import ptolemy.data.IntToken;
import ptolemy.data.Token;
import ptolemy.data.ScalarToken;
import ptolemy.data.expr.Parameter;
import ptolemy.data.type.BaseType;
import ptolemy.data.type.Type;
import ptolemy.kernel.CompositeEntity;
import ptolemy.kernel.util.*;
//////////////////////////////////////////////////////////////////////////
//// STEmergu
/**
Output the initial value plus the sum of all the inputs since
the last time a true token was received at the reset port.
One output is produced each time the actor is fired. The
inputs and outputs can be any token type that supports addition.
The output type is constrained to be the greater than or
equal to the input type and the initial value type.
*/
public class STEmergu extends Transformer {
/** Construct an actor with the given container and name.
* @param container The container.
* @param name The name of this actor.
* @exception IllegalActionException If the actor cannot be contained
* by the proposed container.
* @exception NameDuplicationException If the container already has an
* actor with this name.
*/
public STEmergu(CompositeEntity container, String name)
throws NameDuplicationException, IllegalActionException {
super(container, name);
E_out= new TypedIOPort(this, "E_out", false, true);
W_Size=new Parameter(this ,"W_Size",new IntToken(256));
// set the type constraints.
W_Size.setTypeEquals(BaseType.INT);
E_out.setTypeEquals(BaseType.DOUBLE);
output.setTypeAtLeast(input);
}
///////////////////////////////////////////////////////////////////
//// ports and parameters ////
public Parameter W_Size;
public TypedIOPort E_out ;
///////////////////////////////////////////////////////////////////
//// public methods ////
/** Clone the actor into the specified workspace. This calls the
* base class and then sets up the type constraints.
* @param workspace The workspace for the new object.
* @return A new actor.
* @exception CloneNotSupportedException If a derived class contains
* an attribute that cannot be cloned.
*/
public Object clone(Workspace workspace)
throws CloneNotSupportedException {
STEmergu newObject = (STEmergu)super.clone(workspace);
// set the type constraints.
newObject.output.setTypeAtLeast(newObject.input);
return newObject;
}
/** Consume at most one token from the <i>input</i> port,
* add it to the running sum, and produce the result at the
* <i>output</i> port. If there is no input token available,
* the current value of the running sum is the output value.
* If there is a true-valued token on the <i>reset</i> input,
* then the running sum is reset to the initial value before
* adding the input.
* @exception IllegalActionException If addition is not
* supported by the supplied tokens.
*/
public void fire() throws IllegalActionException {
_latestSum = _sum;
_latestCount=_count;
ScalarToken in=new IntToken(0);
if (input.hasToken(0))
{ in= (ScalarToken)input.get(0);
if(_latestCount!=0)
{
_latestSum = _latestSum.add(in.absolute()) ;
output.send(0,in);
--_latestCount;
}
else
{
E_out.send(0,_sum);
// if (input.hasToken(0))
// Token in =input.get(0);
output.send(0,in);
_latestSum=new IntToken(0);
_latestSum = _latestSum.add(in.absolute()) ;
_latestCount=((IntToken)(W_Size.getToken())).intValue()-1;
}
}
else
{output.send(0,new IntToken(0));
E_out.send(0, new IntToken(0) );}
}
public void initialize() throws IllegalActionException {
super.initialize();
_latestSum=new IntToken(0);
//_sum=output.getType().convert(new IntToken(0));
_sum = new IntToken(0);
_count=0;
_latestCount=((IntToken)(W_Size.getToken())).intValue();
}
public boolean postfire() throws IllegalActionException {
_sum = _latestSum;
_count=_latestCount;
return super.postfire();
}
///////////////////////////////////////////////////////////////////
//// private members ////
private Token _sum;
private Token _latestSum;
private int _latestCount;
private int _count=0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -