Class AddSubtract

  • All Implemented Interfaces:
    java.lang.Cloneable, Actor, Executable, FiringsRecordable, Initializable, TypedActor, Changeable, Debuggable, DebugListener, Derivable, Instantiable, ModelErrorHandler, MoMLExportable, Moveable, Nameable
    Direct Known Subclasses:
    AddSubtractAll, ComplexAddSubtract

    public class AddSubtract
    extends TypedAtomicActor

    A polymorphic adder/subtractor. This adder has two input ports, both of which are multiports, and one output port, which is not. The types on the ports are undeclared and will be resolved by the type resolution mechanism. Data that arrives on the input port named plus will be added, and data that arrives on the input port named minus will be subtracted. Any token type supporting addition and subtraction can be used. In most domains, either input port can be left unconnected. Thus, to get a simple adder (with no subtractor), just leave the minus input unconnected.

    The plus input port will typically resolve to the least upper bound of the types presented to it. Thus, for example, if one input channel comes from a source of type BooleanToken and another comes from a source of type IntToken, the resolved type will be StringToken, and addition will be that implemented in StringToken (which concatenates strings). Notice that StringToken does not support subtraction, so if any inputs are presented to the minus port, an exception will be thrown at run time.

    Currently, the type system is quite liberal about the resolved types it will permit at the inputs. In particular, it may permit the plus and minus inputs to resolve to types that cannot in fact be subtracted. In these cases, a run-time error will occur. In the future, we hope that the type system will intercept such errors before run time.

    This actor does not require that each input channel have a token upon firing. It will add or subtract available tokens at the inputs and ignore the channels that do not have tokens. It consumes at most one input token from each port. If no input tokens are available at all, then no output is produced.

    Since:
    Ptolemy II 0.3
    Version:
    $Id$
    Author:
    Yuhong Xiong and Edward A. Lee
    Pt.AcceptedRating:
    Green (bilung)
    Pt.ProposedRating:
    Green (eal)
    • Field Detail

      • minus

        public TypedIOPort minus
        Input for tokens to be subtracted. This is a multiport, and its type is inferred from the connections.
      • output

        public TypedIOPort output
        Output port. The type is inferred from the connections.
      • plus

        public TypedIOPort plus
        Input for tokens to be added. This is a multiport, and its type is inferred from the connections.
    • Method Detail

      • fire

        public void fire()
                  throws IllegalActionException
        If there is at least one token on the input ports, add tokens from the plus port, subtract tokens from the minus port, and send the result to the output port. At most one token is read from each channel, so if more than one token is pending, the rest are left for future firings. If none of the input channels has a token, do nothing. If none of the plus channels have tokens, then the tokens on the minus channels are subtracted from a zero token of the same type as the first token encountered on the minus channels.
        Specified by:
        fire in interface Executable
        Overrides:
        fire in class AtomicActor<TypedIOPort>
        Throws:
        IllegalActionException - If there is no director, or if addition and subtraction are not supported by the available tokens.
      • _customTypeConstraints

        protected java.util.Set<Inequality> _customTypeConstraints()
        Set the plus port to be greater than or equal to the output port if backward type inference is enabled.
        Overrides:
        _customTypeConstraints in class TypedAtomicActor
        Returns:
        A set of Inequalities