Building Controls Virtual Test Bed
ADInterfaceMCCWriter.java
Go to the documentation of this file.
1 // Write to a ADInterfaceMCC device
2 
3 /*
4 ********************************************************************
5 Copyright Notice
6 ----------------
7 
8 Building Controls Virtual Test Bed (BCVTB) Copyright (c) 2008-2009, The
9 Regents of the University of California, through Lawrence Berkeley
10 National Laboratory (subject to receipt of any required approvals from
11 the U.S. Dept. of Energy). All rights reserved.
12 
13 If you have questions about your rights to use or distribute this
14 software, please contact Berkeley Lab's Technology Transfer Department
15 at TTD@lbl.gov
16 
17 NOTICE. This software was developed under partial funding from the U.S.
18 Department of Energy. As such, the U.S. Government has been granted for
19 itself and others acting on its behalf a paid-up, nonexclusive,
20 irrevocable, worldwide license in the Software to reproduce, prepare
21 derivative works, and perform publicly and display publicly. Beginning
22 five (5) years after the date permission to assert copyright is obtained
23 from the U.S. Department of Energy, and subject to any subsequent five
24 (5) year renewals, the U.S. Government is granted for itself and others
25 acting on its behalf a paid-up, nonexclusive, irrevocable, worldwide
26 license in the Software to reproduce, prepare derivative works,
27 distribute copies to the public, perform publicly and display publicly,
28 and to permit others to do so.
29 
30 
31 Modified BSD License agreement
32 ------------------------------
33 
34 Building Controls Virtual Test Bed (BCVTB) Copyright (c) 2008-2009, The
35 Regents of the University of California, through Lawrence Berkeley
36 National Laboratory (subject to receipt of any required approvals from
37 the U.S. Dept. of Energy). All rights reserved.
38 
39 Redistribution and use in source and binary forms, with or without
40 modification, are permitted provided that the following conditions are met:
41 
42  1. Redistributions of source code must retain the above copyright
43  notice, this list of conditions and the following disclaimer.
44  2. Redistributions in binary form must reproduce the above copyright
45  notice, this list of conditions and the following disclaimer in
46  the documentation and/or other materials provided with the
47  distribution.
48  3. Neither the name of the University of California, Lawrence
49  Berkeley National Laboratory, U.S. Dept. of Energy nor the names
50  of its contributors may be used to endorse or promote products
51  derived from this software without specific prior written permission.
52 
53 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
54 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
56 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
57 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
60 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
61 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
62 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.%
64 
65 You are under no obligation whatsoever to provide any bug fixes,
66 patches, or upgrades to the features, functionality or performance of
67 the source code ("Enhancements") to anyone; however, if you choose to
68 make your Enhancements available either publicly, or directly to
69 Lawrence Berkeley National Laboratory, without imposing a separate
70 written license agreement for such Enhancements, then you hereby grant
71 the following license: a non-exclusive, royalty-free perpetual license
72 to install, use, modify, prepare derivative works, incorporate into
73 other computer software, distribute, and sublicense such enhancements or
74 derivative works thereof, in binary and source code form.
75 
76 ********************************************************************
77 */
78 
79 
80 package adInterfaceMCC.actor;
81 
84 
85 import ptolemy.kernel.CompositeEntity;
86 import ptolemy.kernel.util.IllegalActionException;
87 import ptolemy.kernel.util.NameDuplicationException;
88 import ptolemy.kernel.util.Settable;
89 import ptolemy.actor.TypedIOPort;
90 import ptolemy.data.type.BaseType;
91 import ptolemy.data.Token;
92 import ptolemy.data.IntToken;
93 import ptolemy.data.expr.Parameter;
94 import java.util.ArrayList;
95 
106 public class ADInterfaceMCCWriter extends ADInterfaceMCC {
107 
116  public ADInterfaceMCCWriter(CompositeEntity container, String name)
117  throws IllegalActionException,NameDuplicationException {
118  super(container, name);
119  /*Use input port to receive Ptolemy input tokens
120  */
121  input = new TypedIOPort(this, "input", true, false);
122  input.setMultiport(true);
123 
124  /*Use input token consumption rate to check whether there are tokens
125  * in the input port, used in prefire()
126  */
127  input_tokenConsumptionRate = new Parameter(input, "tokenConsumptionRate");
128  input_tokenConsumptionRate.setExpression("1");
129  input_tokenConsumptionRate.setVisibility(Settable.NOT_EDITABLE);
130  input_tokenConsumptionRate.setTypeEquals(BaseType.INT);
131  input_tokenConsumptionRate.setPersistent(false);
132 
133 
134  }
135 
138 
139 
146  public boolean prefire() throws IllegalActionException {
147  Token rateToken = input_tokenConsumptionRate.getToken();
148  int required = ((IntToken) rateToken).intValue();
149 
150  // Derived classes may convert the input port to a multiport.
151 
152  for (int i = 0; i < input.getWidth(); i++) {
153  if (!input.hasToken(i, required)) {
154  if (_debugging) {
155  _debug("Called prefire(), "
156  + " input tokenConsumptionRate = " + required
157  + ", input.hasToken(" + i + ", " + required
158  + ") is false, prefire() returning false");
159  }
160 
161  return false;
162  }
163  }
164  return super.prefire();
165  }
166 
175  public void checkNumberOfInput() throws IllegalActionException
176  {
177  final int size = input.getWidth();
178  int new_size = 0;
179  int old_size = prop_arr.size();
180  for (int i = 0; i < prop_arr.size(); i++)
181  {
182  if (prop_arr.get(i).getApplicationTag().equals("READ")){
183  old_size = old_size -1;
184  }
185  new_size = old_size;
186  }
187  if(size!= new_size){
188  String em = "The number of input '" + size + "' is ";
189  if(size > new_size)
190  em += "bigger ";
191  else
192  em += "less ";
193  em += "than the number"
194  + LS + "of properties found in the configuration file '" + new_size +"'.";
195  throw new IllegalActionException(em);
196  }
197  }
198 
205  public Token[] consumeToken() throws IllegalActionException
206  {
207  final int n = input.getWidth();
208  Token inp[] = new Token[n];
209  for(int i=0;i<n;i++)
210  inp[i]= input.get(i);
211  return inp;
212  }
213 
219  public ArrayList<ADInterfaceMCCObjectType> addPropertyValue(Token[] inp) throws IllegalActionException
220  {
221  ArrayList<ADInterfaceMCCObjectType> proparr = new ArrayList<ADInterfaceMCCObjectType>();
222  ArrayList<ADInterfaceMCCObjectType> proparr_write = new ArrayList<ADInterfaceMCCObjectType>();
223 
224  for (int i = 0; i < prop_arr.size(); i++)
225  {
226  if (prop_arr.get(i).getApplicationTag().equals("WRITE")){
227  proparr_write.add(prop_arr.get(i));
228  }
229  }
230  for (int i = 0; i < proparr_write.size(); i++) {
231 
232  ADInterfaceMCCObjectType bcp = proparr_write.get(i);
233  String boardNumber = bcp.getBoardNumber();
234  String channelNumber = bcp.getChannelNumber();
235  String channelGain = bcp.getChannelGain();
236  String channelValue = inp[i].toString();
237  String channelOptions = bcp.getChannelOptions();
238  String apptag = bcp.getApplicationTag();
239 
240  ADInterfaceMCCObjectType bcp_new = new ADInterfaceMCCObjectType(boardNumber,channelNumber,channelGain,channelValue,channelOptions, apptag);
241  proparr.add(bcp_new);
242  }
243  return proparr;
244  }
245 
255  public void initialize() throws IllegalActionException
256  {
257  super.initialize();
258 
259  /* Check if application tag is part of the standard,
260  if priority and index are integers*/
261 
263 
264  /* Check if number of tokens from Ptolemy is equal to
265  the number of properties read from the configuration file
266  */
268  }
269 
277  public void fire() throws IllegalActionException {
278  super.fire();
279  /*get input tokens from Ptolemy
280  */
281  Token[] inp = consumeToken();
282  /*add these input tokens to ADInterfaceMCCObjectType
283  * as read from configuration file
284  */
286  /*Prepare processes to be executed
287  */
288  proc_arr = adDevMan.getProcesses(prop_add_arr);
289  /*Prepare the tokens to be sent to outlet port
290  */
292  /*Send tokens to outlet port
293  */
294  sendToken();
295 
296  /*Remove token in last time step*/
297  removeToken();
298  }
299 
304  protected void setSchemaFileName(){
306  bcvtbhome + FS + "lib" + FS + "ptII" + FS + "adInterfaceMCC" + FS + "ADInterfaceMCC.xsd";
307  }
308 
311 
312  /* The input port. This base class imposes no type constraints except
313  * that the type of the input cannot be greater than the type of the
314  * output.
315  */
316  public TypedIOPort input;
317 
318  /* The rate parameter for the input port.
319  */
320  public Parameter input_tokenConsumptionRate;
321 
325  public static ArrayList<ADInterfaceMCCObjectType> prop_add_arr;
326 }
This class reads the XML configuration file and stores the data in the ADInterfaceMCCObjectType data ...
for i
Definition: compile.m:69
static final String bcvtbhome
String that points to root directory of the BCVTB.
ArrayList< ProcessBuilder > proc_arr
Arraylist containing processes to be executed in console.
void prepareTokensToSend()
Get tokens to send for the next timestep.
static ArrayList< ADInterfaceMCCObjectType > prop_add_arr
ADInterfaceMCCObjectType after adding the value from Ptolemy.
static final String LS
Line Seperator.
void setSchemaFileName()
Sets the schema file name.
ArrayList< ADInterfaceMCCObjectType > prop_arr
Arraylist containing ADInterfaceMCCObjectType read from configuration file.
static final String FS
File seperator.
boolean prefire()
Return true if the number of available tokens on the input port is at least the declared consumption ...
ADInterfaceMCCWriter(CompositeEntity container, String name)
Construct an actor with the given container and name.
This file compiles the BSD socket interface for Simulink It is called by ant On it requires the Microsoft compiler since matlab requires the size of output arrays to be fixed If more elements are required
Definition: compile.m:5
void checkNumberOfInput()
Check if the number of input tokens of the actor is equal to the number of properties read from the c...
String schemaFileName
Schema file path.
ArrayList< ADInterfaceMCCObjectType > addPropertyValue(Token[] inp)
Replace the property value in ADInterfaceMCCObjectType with tokens from Ptolemy.
Token[] consumeToken()
consume token, store the token in an array.
void validateAttributes()
Validate application tag.
This actor writes to ADInterfaceMCC devices.
void removeToken()
Remove tokens in previous time step.
void initialize()
Initialize variables and check for errors.
This is an abstract base class for actors that generates an output stream.
void fire()
At each timestep write properties to ADInterfaceMCC devices.
void sendToken()
Sends the error signal to the errorSignal port, the error message to the errorMessage port...