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 ptolemy.actor.TypedAtomicActor;
031import ptolemy.actor.TypedIOPort;
032import ptolemy.data.DoubleToken;
033import ptolemy.data.IntToken;
034import ptolemy.data.expr.Parameter;
035import ptolemy.data.type.BaseType;
036import ptolemy.kernel.CompositeEntity;
037import ptolemy.kernel.util.IllegalActionException;
038import ptolemy.kernel.util.NameDuplicationException;
039
040///////////////////////////////////////////////////////////////////
041//// ND_1
042
043/**
044
045 This class defines a PN actor object. This actor is automatically
046 generated, as part of the <A
047 HREF="http://www.gigascale.org/compaan">Compaan</A> project. Although
048 most of the actor is generated automatically, some parts have been
049 manually tuned for this demo especially when reading and writing of
050 matrices is involved; they may change in future releases.
051
052 @author Bart Kienhuis
053 @version $Id$
054 @since Ptolemy II 1.0
055 @Pt.ProposedRating Red (kienhuis)
056 @Pt.AcceptedRating Red (kienhuis)
057 */
058public class ND_1 extends TypedAtomicActor {
059    /** Construct an actor that is an SBF object with the given container
060     *  and name.
061     *  @param container The container.
062     *  @param name The name of this actor.
063     *  @exception IllegalActionException If the actor cannot be contained
064     *   by the proposed container.
065     *  @exception NameDuplicationException If the container already has an
066     *   actor with this name.
067     */
068    public ND_1(CompositeEntity container, String name)
069            throws IllegalActionException, NameDuplicationException {
070        super(container, name);
071
072        WP_2 = new TypedIOPort(this, "WP_2", false, true);
073        WP_6 = new TypedIOPort(this, "WP_6", false, true);
074
075        WP_2.setTypeEquals(BaseType.DOUBLE);
076        WP_6.setTypeEquals(BaseType.DOUBLE);
077
078        // The Type of these Parameters is set by the First
079        // Token placed in the parameters when created.
080        parameter_N = new Parameter(this, "N", new IntToken(6));
081        parameter_K = new Parameter(this, "K", new IntToken(6));
082
083        x_1.readMatrix("Zeros_64x64");
084    }
085
086    ///////////////////////////////////////////////////////////////////
087    ////                     ports and parameters                  ////
088    // -- Part of the Actor
089    public TypedIOPort WP_2;
090
091    public TypedIOPort WP_6;
092
093    // -- Public interface of the Actor
094    public Parameter parameter_N;
095
096    public Parameter parameter_K;
097
098    ///////////////////////////////////////////////////////////////////
099    ////                         public methods                    ////
100
101    /** Initialize the PN actor.
102     *  @exception IllegalActionException If the parent class throws it.
103     */
104    @Override
105    public void initialize() throws IllegalActionException {
106        super.initialize();
107
108        // Get the correct value from the parameters
109        N = ((IntToken) parameter_N.getToken()).intValue();
110        K = ((IntToken) parameter_K.getToken()).intValue();
111    }
112
113    /** Fire the actor.
114     *  @exception IllegalActionException If there is no director.
115     */
116    @Override
117    public void fire() throws IllegalActionException {
118        super.fire();
119        for (int j = 1; j <= 1 * N; j += 1) {
120            for (int i = 1 * j; i <= 1 * N; i += 1) {
121                out_0 = x_1.retrieve(x_1.atKey(j, i));
122                _debug(" Broadcast from ND_1: " + out_0);
123
124                if (-i + j == 0 && K - 1 >= 0) {
125                    WP_2.broadcast(new DoubleToken(out_0));
126                }
127
128                if (i - j - 1 >= 0 && K - 1 >= 0) {
129                    WP_6.broadcast(new DoubleToken(out_0));
130                }
131            }
132        }
133    }
134
135    /** Post fire the actor. Return false to indicated that the
136     *  process has finished. If it returns true, the process will
137     *  continue indefinitely.
138     *  @exception IllegalActionException If thrown by the parent
139     *  class.
140     */
141    @Override
142    public boolean postfire() throws IllegalActionException {
143        // We intentially ignore the return value of super.postfire()
144        // here.
145        super.postfire();
146        return false;
147    }
148
149    ///////////////////////////////////////////////////////////////////
150    ////                         private variables                 ////
151    // -- Get private copies of the parameters
152    private int N;
153
154    private int K;
155
156    private double out_0;
157
158    private ArrayIndex x_1 = new ArrayIndex();
159}