001/*
002 * Copyright (c) 1998-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.surge;
031
032import ptolemy.actor.IOPort;
033import ptolemy.actor.TypedAtomicActor;
034import ptolemy.actor.TypedIOPort;
035import ptolemy.data.RecordToken;
036import ptolemy.kernel.CompositeEntity;
037import ptolemy.kernel.util.IllegalActionException;
038import ptolemy.kernel.util.NameDuplicationException;
039
040//////////////////////////////////////////////////////////////////////////
041//// RecordMerger class name
042/**
043 * This actor merges a RecordToken from each input. More than two RecordTokens
044 * can be merged at each firing by adding input ports.
045 * 
046 * @author Daniel Crawl
047 * @version $Id: RecordMerger.java 24234 2010-05-06 05:21:26Z welker $
048 */
049
050public class RecordMerger extends TypedAtomicActor {
051
052        /**
053         * Construct a RecordMerger with the given container and name.
054         * 
055         * @param name
056         *            The name of this actor.
057         * @exception IllegalActionException
058         *                If the entity cannot be contained by the proposed
059         *                container.
060         * @exception NameDuplicationException
061         *                If the container already has an actor with this name.
062         */
063        public RecordMerger(CompositeEntity container, String name)
064                        throws NameDuplicationException, IllegalActionException {
065                super(container, name);
066
067                input1 = new TypedIOPort(this, "input1", true, false);
068                input2 = new TypedIOPort(this, "input2", true, false);
069
070                output = new TypedIOPort(this, "output", false, true);
071
072                _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" "
073                                + "width=\"60\" height=\"20\" " + "style=\"fill:white\"/>\n"
074                                + "</svg>\n");
075        }
076
077        // /////////////////////////////////////////////////////////////////
078        // // ports and parameters ////
079
080        // at least two inputs
081        public TypedIOPort input1 = null;
082        public TypedIOPort input2 = null;
083
084        // the output
085        public TypedIOPort output = null;
086
087        // /////////////////////////////////////////////////////////////////
088        // // public methods ////
089
090        /**
091         * Merge RecordTokens from all input ports.
092         * 
093         * @exception IllegalActionException
094         *                If it is thrown by the send() method sending out the
095         *                token.
096         */
097        public void fire() throws IllegalActionException {
098                super.fire();
099
100                RecordToken out = RecordToken.merge((RecordToken) input1.get(0),
101                                (RecordToken) input2.get(0));
102
103                Object[] ports = inputPortList().toArray();
104
105                for (int i = 0; i < ports.length; i++) {
106                        IOPort p = (IOPort) ports[i];
107                        if (!p.getName().equals("input1") && !p.getName().equals("input2")) {
108                                out = RecordToken.merge(out, (RecordToken) p.get(0));
109                        }
110                }
111                output.broadcast(out);
112        }
113}