001/* A subscriber that aggregates messages from multiple publishers.
002
003 Copyright (c) 2006-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.actor.lib;
029
030import ptolemy.actor.SubscriptionAggregatorPort;
031import ptolemy.data.Token;
032import ptolemy.data.expr.StringParameter;
033import ptolemy.kernel.CompositeEntity;
034import ptolemy.kernel.util.IllegalActionException;
035import ptolemy.kernel.util.NameDuplicationException;
036
037///////////////////////////////////////////////////////////////////
038//// SubscriptionAggregator
039
040/**
041 Aggregate data produced by multiple publishers.
042 This is a generalization of the Subscriber (the base class)
043 where the channel name is interpreted as a regular expression.
044 Data produced by all publishers that publish on a channel name
045 that matches the regular expression are aggregated using the
046 operation given by the <i>operation</i> parameter.
047
048 <p>Note that the {@link ptolemy.actor.lib.Subscriber#channel <i>channel</i>}
049 parameter of the superclass is now a regular expression in this class.
050 Thus, this class is usually slower than the superclass.  One thing
051 to watch out for is using <code>.</code> instead of <code>\.</code>
052 and <code>*</code> instead of <code>.+</code>.
053 For example, <code>channel.foo.*</code> does not mean the same thing as
054 <code>channel\.foo.+</code>. The latter requires a dot between channel and
055 foo, where the former does not.
056
057 @author Edward A. Lee, Raymond A. Cardillo, contributor: Christopher Brooks, Bert Rodiers
058 @version $Id$
059 @since Ptolemy II 5.2
060 @Pt.ProposedRating Green (cxh)
061 @Pt.AcceptedRating Red (cxh)
062 */
063public class SubscriptionAggregator extends Subscriber {
064
065    // NOTE: This cannot extend Subscriber because it needs
066    // a different kind of input port.
067
068    /** Construct a subscriber with the specified container and name.
069     *  @param container The container actor.
070     *  @param name The name of the actor.
071     *  @exception IllegalActionException If the actor is not of an acceptable
072     *   class for the container.
073     *  @exception NameDuplicationException If the name coincides with
074     *   an actor already in the container.
075     */
076    public SubscriptionAggregator(CompositeEntity container, String name)
077            throws IllegalActionException, NameDuplicationException {
078        super(container, name);
079
080        operation = new StringParameter(this, "operation");
081        operation.addChoice("add");
082        operation.addChoice("multiply");
083        operation.setExpression("add");
084
085        // Set the operation attribute of the input port to inherit
086        // the value of the operation parameter of this actor.
087        ((SubscriptionAggregatorPort) input).operation
088                .setExpression("$operation");
089    }
090
091    ///////////////////////////////////////////////////////////////////
092    ////                   ports and parameters                    ////
093
094    /** The operation used to aggregate the data produced by
095     *  matching publishers. The choices are "add" and "multiply".
096     *  Note that "multiply" is a poor choice if the data type
097     *  has a non-commutative multiplication operation (e.g.
098     *  matrix types) because the result will be nondeterministic.
099     *  This is a string that defaults to "add".
100     */
101    public StringParameter operation;
102
103    ///////////////////////////////////////////////////////////////////
104    ////                         public methods                    ////
105
106    /** Read at most one input token from each input
107     *  channel, aggregate them, and send the result to the output.
108     *  @exception IllegalActionException If there is no director, or
109     *   if there is no input connection.
110     */
111    @Override
112    public void fire() throws IllegalActionException {
113        // Do not call super.fire() here because that does the wrong thing.
114        if (_debugging) {
115            _debug("Called fire()");
116        }
117        int width = input.getWidth();
118        if (width == 0) {
119            throw new IllegalActionException(this,
120                    "SubscriptionAggregator could not find a matching Publisher "
121                            + "with channel \"" + channel.stringValue() + "\"");
122
123        }
124        if (input.hasToken(0)) {
125            // This will do the aggregation.
126            Token token = input.get(0);
127            output.send(0, token);
128        }
129    }
130
131    ///////////////////////////////////////////////////////////////////
132    ////                         protected methods                 ////
133
134    /** Create an input port. This overrides the base class to create
135     *  a SubscriptionAggregatorPort.
136     *  @exception IllegalActionException If creating the input port fails.
137     *  @exception NameDuplicationException If there is already a port named "input".
138     */
139    @Override
140    protected void _createInputPort()
141            throws IllegalActionException, NameDuplicationException {
142        input = new SubscriptionAggregatorPort(this, "input");
143    }
144}