001/* A PN process actor object.
002
003 Copyright (c) 1999-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.domains.pn.demo.QR;
029
030import java.util.Vector;
031
032import ptolemy.actor.TypedAtomicActor;
033import ptolemy.actor.TypedIOPort;
034import ptolemy.data.DoubleToken;
035import ptolemy.data.IntToken;
036import ptolemy.data.expr.Parameter;
037import ptolemy.data.type.BaseType;
038import ptolemy.kernel.CompositeEntity;
039import ptolemy.kernel.util.IllegalActionException;
040import ptolemy.kernel.util.NameDuplicationException;
041
042///////////////////////////////////////////////////////////////////
043//// ND_3
044
045/**
046
047 This class defines a PN actor object. This actor is automatically
048 generated, as part of the <A
049 HREF="http://www.gigascale.org/compaan">Compaan</A> project. Although
050 most of the actor is generated automatically, some parts have been
051 manually tuned for this demo especially when reading and writing of
052 matrices is involved; they may change in future releases.
053
054 @author Bart Kienhuis
055 @version $Id$
056 @since Ptolemy II 1.0
057 @Pt.ProposedRating Red (kienhuis)
058 @Pt.AcceptedRating Red (kienhuis)
059 */
060public class ND_3 extends TypedAtomicActor {
061    /** Construct an actor that is an SBF object with the given container
062     *  and name.
063     *  @param container The container.
064     *  @param name The name of this actor.
065     *  @exception IllegalActionException If the actor cannot be contained
066     *   by the proposed container.
067     *  @exception NameDuplicationException If the container already has an
068     *   actor with this name.
069     */
070    public ND_3(CompositeEntity container, String name)
071            throws IllegalActionException, NameDuplicationException {
072        super(container, name);
073
074        RP_1 = new TypedIOPort(this, "RP_1", true, false);
075        RP_2 = new TypedIOPort(this, "RP_2", true, false);
076        RP_3 = new TypedIOPort(this, "RP_3", true, false);
077        RP_4 = new TypedIOPort(this, "RP_4", true, false);
078
079        WP_1 = new TypedIOPort(this, "WP_1", false, true);
080        WP_11 = new TypedIOPort(this, "WP_11", false, true);
081        WP_10 = new TypedIOPort(this, "WP_10", false, true);
082
083        RP_1.setTypeEquals(BaseType.DOUBLE);
084        RP_2.setTypeEquals(BaseType.DOUBLE);
085        RP_3.setTypeEquals(BaseType.DOUBLE);
086        RP_4.setTypeEquals(BaseType.DOUBLE);
087
088        WP_1.setTypeEquals(BaseType.DOUBLE);
089        WP_11.setTypeEquals(BaseType.DOUBLE);
090        WP_10.setTypeEquals(BaseType.DOUBLE);
091
092        // The Type of these Parameters is set by the First
093        // Token placed in the parameters when created.
094        parameter_N = new Parameter(this, "N", new IntToken(6));
095        parameter_K = new Parameter(this, "K", new IntToken(6));
096    }
097
098    ///////////////////////////////////////////////////////////////////
099    ////                     ports and parameters                  ////
100    // -- Part of the Actor
101    public TypedIOPort RP_1;
102
103    public TypedIOPort RP_2;
104
105    public TypedIOPort RP_3;
106
107    public TypedIOPort RP_4;
108
109    public TypedIOPort WP_1;
110
111    public TypedIOPort WP_11;
112
113    public TypedIOPort WP_10;
114
115    // -- Public interface of the Actor
116    public Parameter parameter_N;
117
118    public Parameter parameter_K;
119
120    ///////////////////////////////////////////////////////////////////
121    ////                         public methods                    ////
122
123    /** Initialize the PN actor.
124     *  @exception IllegalActionException If the parent class throws it.
125     */
126    @Override
127    public void initialize() throws IllegalActionException {
128        super.initialize();
129        //_returnValue = true;
130
131        // Get the correct value from the parameters
132        N = ((IntToken) parameter_N.getToken()).intValue();
133        K = ((IntToken) parameter_K.getToken()).intValue();
134    }
135
136    /** Fire the actor.
137     *  @exception IllegalActionException If there is no director.
138     */
139    @Override
140    public void fire() throws IllegalActionException {
141        super.fire();
142        for (int k = 1; k <= 1 * K; k += 1) {
143            for (int j = 1; j <= 1 * N; j += 1) {
144                if (k - 2 >= 0) {
145                    r_2.add(Double.valueOf(
146                            ((DoubleToken) RP_1.get(0)).doubleValue()));
147                    in_0 = ((Double) r_2.elementAt(w_r_2++)).doubleValue();
148                }
149
150                if (k - 1 == 0) {
151                    r_1.add(Double.valueOf(
152                            ((DoubleToken) RP_2.get(0)).doubleValue()));
153                    in_0 = ((Double) r_1.elementAt(w_r_1++)).doubleValue();
154                }
155
156                if (j - 2 >= 0) {
157                    x_3.add(Double.valueOf(
158                            ((DoubleToken) RP_3.get(0)).doubleValue()));
159                    in_1 = ((Double) x_3.elementAt(w_x_3++)).doubleValue();
160                }
161
162                if (j - 1 == 0) {
163                    x_1.add(Double.valueOf(
164                            ((DoubleToken) RP_4.get(0)).doubleValue()));
165                    in_1 = ((Double) x_1.elementAt(w_x_1++)).doubleValue();
166                }
167
168                _Vectorize(in_0, in_1);
169                out_0 = _argOut0;
170                //out_1 = _argOut1;
171                out_2 = _argOut2;
172
173                if (K - k - 1 >= 0) {
174                    WP_1.broadcast(new DoubleToken(out_0));
175                }
176
177                if (-K + k == 0) {
178                    WP_11.broadcast(new DoubleToken(out_0));
179                }
180
181                if (N - j - 1 >= 0) {
182                    WP_10.broadcast(new DoubleToken(out_2));
183                }
184            }
185        }
186    }
187
188    /** Post fire the actor. Return false to indicated that the
189     *  process has finished. If it returns true, the process will
190     *  continue indefinitely.
191     */
192    @Override
193    public boolean postfire() {
194        return false;
195    }
196
197    ///////////////////////////////////////////////////////////////////
198    ////                         private methods                   ////
199    private void _Vectorize(double arg0, double arg1) {
200        _argOut2 = -1 * Math.atan2(arg1, arg0);
201        _argOut0 = Math.cos(_argOut2) * arg0 - Math.sin(_argOut2) * arg1;
202        //_argOut1 = 0.0;
203    }
204
205    ///////////////////////////////////////////////////////////////////
206    ////                         private variables                 ////
207    // -- Get private copies of the parameters
208    private int N;
209
210    private int K;
211
212    private double in_0;
213
214    private double in_1;
215
216    private double out_0;
217
218    //private double out_1;
219
220    private double out_2;
221
222    // Needed to communicate results from the function
223    private double _argOut0;
224
225    //private double _argOut1;
226
227    private double _argOut2;
228
229    private Vector r_2 = new Vector();
230
231    private Vector r_1 = new Vector();
232
233    private Vector x_3 = new Vector();
234
235    private Vector x_1 = new Vector();
236
237    private int w_r_2 = 0;
238
239    private int w_r_1 = 0;
240
241    private int w_x_3 = 0;
242
243    private int w_x_1 = 0;
244
245    //private boolean _returnValue = true;
246}