001/* An actor that assembles multiple inputs to an [Ordered]RecordToken. 002 003 Copyright (c) 2009-2015 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 java.util.LinkedHashMap; 031import java.util.Map; 032import java.util.Map.Entry; 033import java.util.Set; 034 035import ptolemy.actor.TypedIOPort; 036import ptolemy.data.OrderedRecordToken; 037import ptolemy.data.RecordToken; 038import ptolemy.data.Token; 039import ptolemy.kernel.CompositeEntity; 040import ptolemy.kernel.util.IllegalActionException; 041import ptolemy.kernel.util.NameDuplicationException; 042 043/////////////////////////////////////////////////////////////////// 044//// OrderedRecordAssembler 045 046/** 047 On each firing, read one token from each input port and assemble them 048 into a RecordToken. The labels for the RecordToken are the names of the 049 input ports. To use this class, instantiate it, and then add input ports 050 (instances of TypedIOPort). This actor is polymorphic. The type constraint 051 is that the type of each record field is no less than the type of the 052 corresponding input port. 053 <p> 054 This actor differs from the RecordAssembler in that it ensures that the 055 order of the fields of the record is preserved, matching the order 056 of the input ports. This is probably not relevant unless you are writing 057 Java code that iterates over the fields of the record.</p> 058 059 <p>Note that if the display name of a port is set, display name is used in 060 the type constraints instead of name. This is useful in case fields to 061 add to the record contain a period, because periods are not allowed in 062 port names.</p> 063 064 @author Ben Leinfelder, Christopher Brooks 065 @version $Id$ 066 @since Ptolemy II 8.0 067 @Pt.ProposedRating Red (cxh) 068 @Pt.AcceptedRating Red (cxh) 069 @see ptolemy.actor.lib.RecordAssembler 070 @see ptolemy.actor.lib.RecordDisassembler 071 */ 072public class OrderedRecordAssembler extends RecordAssembler { 073 /** Construct an OrderedRecordAssembler with the given container and name. 074 * @param container The container. 075 * @param name The name of this actor. 076 * @exception IllegalActionException If this actor cannot be contained 077 * by the proposed container. 078 * @exception NameDuplicationException If the container already has an 079 * actor with this name. 080 */ 081 public OrderedRecordAssembler(CompositeEntity container, String name) 082 throws NameDuplicationException, IllegalActionException { 083 super(container, name); 084 } 085 086 /////////////////////////////////////////////////////////////////// 087 //// public methods //// 088 089 /** Read one token from each input port, assemble them into a RecordToken, 090 * and send the RecordToken to the output. 091 * @exception IllegalActionException If there is no director. 092 */ 093 @Override 094 public void fire() throws IllegalActionException { 095 int i = 0; 096 Set<Entry<String, TypedIOPort>> entries = _portMap.entrySet(); 097 String[] labels = new String[entries.size()]; 098 Token[] values = new Token[entries.size()]; 099 100 for (Entry<String, TypedIOPort> entry : entries) { 101 labels[i] = entry.getKey(); 102 values[i] = entry.getValue().get(0); 103 i++; 104 } 105 106 RecordToken result = new OrderedRecordToken(labels, values); 107 108 output.send(0, result); 109 } 110 111 /////////////////////////////////////////////////////////////////// 112 //// protected methods //// 113 114 /** Return a new _portMap, which is a map between 115 * port names and strings. Derived classes 116 * like OrderedRecordAssembler return 117 * a map with a different ordering. 118 * @return a Map from port names to TypedIOPorts. 119 */ 120 @Override 121 protected Map<String, TypedIOPort> _newPortMap() { 122 // RecordToken._initializeStorage() should probably 123 // use a similar Collection class. 124 return new LinkedHashMap<String, TypedIOPort>(); 125 } 126}