001/* An actor that synchronizes tokens. 002 003 Copyright (c) 1997-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.kernel.CompositeEntity; 031import ptolemy.kernel.util.IllegalActionException; 032import ptolemy.kernel.util.NameDuplicationException; 033 034/////////////////////////////////////////////////////////////////// 035//// Synchronizer 036 037/** 038 This actor implements a token synchronizer. It has one input port and 039 one output port, both of which are multiports. When at least one token 040 exists on every input channel, exactly one token is consumed from each 041 input channel, and the tokens are output on the corresponding output 042 channels. If any input channel is missing a token, then no output is 043 produced. 044 <p> 045 Note that the ordering of channels of a multiport is determined by the 046 order of connection. Thus the n<sup>th</sup> input channel connected 047 corresponds to the n<sup>th</sup> output channel connected. 048 049 @author Paul Whitaker 050 @version $Id$ 051 @since Ptolemy II 1.0 052 @Pt.ProposedRating Yellow (pwhitake) 053 @Pt.AcceptedRating Yellow (pwhitake) 054 */ 055public class Synchronizer extends Transformer { 056 /** Construct an actor in the specified container with the specified 057 * name. 058 * @param container The container. 059 * @param name The name of this actor within the container. 060 * @exception IllegalActionException If the actor cannot be contained 061 * by the proposed container. 062 * @exception NameDuplicationException If the name coincides with 063 * an actor already in the container. 064 */ 065 public Synchronizer(CompositeEntity container, String name) 066 throws IllegalActionException, NameDuplicationException { 067 super(container, name); 068 input.setMultiport(true); 069 output.setMultiport(true); 070 071 input.setWidthEquals(output, true); 072 073 _attachText("_iconDescription", 074 "<svg>\n" + "<polygon points=\"-10,20 10,10 10,-10, -10,-20\" " 075 + "style=\"fill:yellow\"/>\n" + "</svg>\n"); 076 } 077 078 /////////////////////////////////////////////////////////////////// 079 //// public methods //// 080 081 /** Consume exactly one token from each input channel and output 082 * the tokens on the corresponding output channels. 083 * @exception IllegalActionException If there is no director or 084 * if the number of input channels does not equal the number of 085 * output channels. 086 */ 087 @Override 088 public void fire() throws IllegalActionException { 089 super.fire(); 090 int outWidth = output.getWidth(); 091 int inWidth = input.getWidth(); 092 093 if (inWidth != outWidth) { 094 throw new IllegalActionException(this, 095 "Unequal synchronizer channels: " + inWidth + " inputs and " 096 + outWidth + " outputs."); 097 } else { 098 for (int i = 0; i < inWidth; i++) { 099 output.send(i, input.get(i)); 100 } 101 } 102 } 103 104 /** If all of the input channels have at least one token, return 105 * what the superclass returns (presumably true). Otherwise return 106 * false. 107 * @exception IllegalActionException If there is no director. 108 */ 109 @Override 110 public boolean prefire() throws IllegalActionException { 111 for (int i = 0; i < input.getWidth(); i++) { 112 if (!input.hasToken(i)) { 113 return false; 114 } 115 } 116 117 return super.prefire(); 118 } 119}