001/* A merge actor for the DE domain. 002 003 Copyright (c) 1997-2018 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.domains.de.lib; 029 030import java.util.LinkedList; 031import java.util.Queue; 032 033import ptolemy.data.BooleanToken; 034import ptolemy.data.Token; 035import ptolemy.data.expr.Parameter; 036import ptolemy.data.type.BaseType; 037import ptolemy.kernel.CompositeEntity; 038import ptolemy.kernel.util.IllegalActionException; 039import ptolemy.kernel.util.NameDuplicationException; 040import ptolemy.kernel.util.Workspace; 041 042/////////////////////////////////////////////////////////////////// 043//// Merge 044 045/** 046 A timed merge actor for the DE domain. It merges a set of input signals 047 into a single output signal based on the order of the tags 048 associated with the events of signals. A tag is a tuple of a timestamp 049 (as an instance of Time) and a microstep or index (as non-negative integer). 050 Tags have a lexicographic order. 051 <p> 052 This actor has an input port (a multiport) and an output port 053 (a single port). The types of the ports are undeclared and will be 054 resolved by the type resolution mechanism, with the constraint that 055 the output type must be greater than or equal to the input type. 056 <p> 057 There is a boolean parameter <i>discardEvents</i> associated 058 with this actor, which decides how to handle simultaneously 059 available inputs. 060 <p> 061 If the <i>discardEvents</i> parameter is configured to true, 062 then each time this actor fires, it reads the first 063 available token from an input channel and send it to the output 064 port. Then this actor discards all the remaining inputs in the rest of 065 channels. 066 <p> 067 If the <i>discardEvents</i> parameter is configured to false (the default), 068 then the handling of simultaneous events is a bit more subtle. 069 On each firing, it reads at most one input from each input channel 070 in the order of the channels and puts them on a queue to be produced 071 as an output. It then takes the first token (the oldest one) from the queue 072 and produces it on the output. If after this firing the queue is not empty, 073 then it requests a refiring at the current time. 074 075 @author Edward A. Lee, Haiyang Zheng 076 @version $Id$ 077 @since Ptolemy II 0.4 078 @Pt.ProposedRating Green (hyzheng) 079 @Pt.AcceptedRating Green (hyzheng) 080 */ 081public class Merge extends DETransformer { 082 /** Construct an actor in the specified container with the specified 083 * name. Create ports and make the input port a multiport. 084 * @param container The container. 085 * @param name The name. 086 * @exception NameDuplicationException If an actor 087 * with an identical name already exists in the container. 088 * @exception IllegalActionException If the actor cannot be contained 089 * by the proposed container. 090 */ 091 public Merge(CompositeEntity container, String name) 092 throws NameDuplicationException, IllegalActionException { 093 super(container, name); 094 input.setMultiport(true); 095 096 discardEvents = new Parameter(this, "discardEvents"); 097 discardEvents.setExpression("false"); 098 discardEvents.setTypeEquals(BaseType.BOOLEAN); 099 100 _attachText("_iconDescription", 101 "<svg>\n" + "<polygon points=\"-10,20 10,10 10,-10, -10,-20\" " 102 + "style=\"fill:green\"/>\n" + "</svg>\n"); 103 } 104 105 /////////////////////////////////////////////////////////////////// 106 //// ports and parameters //// 107 108 /** A flag to indicate whether the input events can be discarded. 109 * Its default value is false. 110 */ 111 public Parameter discardEvents; 112 113 /////////////////////////////////////////////////////////////////// 114 //// public methods //// 115 116 /** Clone this actor into the specified workspace. 117 * @param workspace The workspace for the cloned object. 118 * @exception CloneNotSupportedException If cloned ports cannot have 119 * as their container the cloned entity (this should not occur), or 120 * if one of the attributes cannot be cloned. 121 * @return A new ComponentEntity. 122 */ 123 @Override 124 public Object clone(Workspace workspace) throws CloneNotSupportedException { 125 Merge newObject = (Merge) super.clone(workspace); 126 newObject._queue = null; 127 return newObject; 128 } 129 130 /** Read the first available tokens from an input channel and 131 * send them to the output port. If the discardEvents parameter 132 * is true, consume all the available tokens of the other channels 133 * and discard them. Otherwise, if the other channels have tokens, 134 * request a refiring at the current time to process them. 135 * @exception IllegalActionException If there is no director, or 136 * the input can not be read, or the output can not be sent. 137 */ 138 @Override 139 public void fire() throws IllegalActionException { 140 super.fire(); 141 boolean discard = ((BooleanToken) discardEvents.getToken()) 142 .booleanValue(); 143 boolean foundInput = false; 144 145 // If tokens can be discarded, this actor sends 146 // out the first available tokens only. It discards all 147 // remaining tokens from other input channels. 148 // Otherwise, this actor handles one channel at each firing 149 // and requests refiring at the current time to handle the 150 // the remaining channels that have tokens. 151 for (int i = 0; i < input.getWidth(); i++) { 152 if (input.hasNewToken(i)) { 153 // This output will be produced regardless of discard status. 154 if (!discard || !foundInput) { 155 _queue.offer(input.get(i)); 156 } 157 foundInput = true; 158 } 159 } 160 if (!_queue.isEmpty()) { 161 output.send(0, _queue.poll()); 162 if (!_queue.isEmpty()) { 163 // Refiring the actor to handle the other tokens. 164 getDirector().fireAt(this, getDirector().getModelTime()); 165 } 166 } 167 } 168 169 /** Initialize this actor by creating a new queue for pending outputs. 170 * @exception IllegalActionException If a superclass throws it. 171 */ 172 @Override 173 public void initialize() throws IllegalActionException { 174 super.initialize(); 175 _queue = new LinkedList<Token>(); 176 } 177 178 /////////////////////////////////////////////////////////////////// 179 // private variables // 180 181 // Queue of outputs to be produced. 182 private Queue<Token> _queue = null; 183}