001/* Construct a test hierarchal graph using the ptolemy.kernel classes. 002 003 Copyright (c) 1997-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 */ 027package ptolemy.kernel.test; 028 029import java.io.Serializable; 030import java.util.Iterator; 031 032import ptolemy.kernel.ComponentEntity; 033import ptolemy.kernel.ComponentPort; 034import ptolemy.kernel.ComponentRelation; 035import ptolemy.kernel.CompositeEntity; 036import ptolemy.kernel.util.IllegalActionException; 037import ptolemy.kernel.util.NameDuplicationException; 038 039/////////////////////////////////////////////////////////////////// 040//// ExampleSystem 041 042/** 043 ExampleSystem constructs a hierarchal graph as shown in 044 Ptolemy II design document, Figure 8. 045 The graph has 10 entities, 14 ports, and 12 relations. 046 The main function also returns the results of some key functions of 047 ComponentRelation and ComponentPort. 048 See Ptolemy 2 design document, Figure 11 049 050 @author Jie Liu 051 @version $Id$ 052 @since Ptolemy II 0.2 053 @Pt.ProposedRating Red 054 @Pt.AcceptedRating Red 055 */ 056@SuppressWarnings("serial") 057public class ExampleSystem implements Serializable { 058 /** Construct the graph. 059 * @exception NameDuplicationException if the example system cannot 060 * be built because of a duplicate name 061 * @exception IllegalActionException if the example system cannot 062 * be built. 063 */ 064 public ExampleSystem() 065 throws IllegalActionException, NameDuplicationException { 066 super(); 067 068 // Create composite entities 069 e0 = new CompositeEntity(); 070 e0.setName("E0"); 071 e3 = new CompositeEntity(e0, "E3"); 072 e4 = new CompositeEntity(e3, "E4"); 073 e7 = new CompositeEntity(e0, "E7"); 074 e10 = new CompositeEntity(e0, "E10"); 075 076 // Create component entities 077 e1 = new ComponentEntity(e4, "E1"); 078 e2 = new ComponentEntity(e4, "E2"); 079 e5 = new ComponentEntity(e3, "E5"); 080 e6 = new ComponentEntity(e3, "E6"); 081 e8 = new ComponentEntity(e7, "E8"); 082 e9 = new ComponentEntity(e10, "E9"); 083 084 // Create ports 085 p0 = (ComponentPort) e4.newPort("P0"); 086 p1 = (ComponentPort) e1.newPort("P1"); 087 p2 = (ComponentPort) e2.newPort("P2"); 088 p3 = (ComponentPort) e2.newPort("P3"); 089 p4 = (ComponentPort) e4.newPort("P4"); 090 p5 = (ComponentPort) e5.newPort("P5"); 091 p6 = (ComponentPort) e5.newPort("P6"); 092 p7 = (ComponentPort) e3.newPort("P7"); 093 p8 = (ComponentPort) e7.newPort("P8"); 094 p9 = (ComponentPort) e8.newPort("P9"); 095 p10 = (ComponentPort) e8.newPort("P10"); 096 p11 = (ComponentPort) e7.newPort("P11"); 097 p12 = (ComponentPort) e10.newPort("P12"); 098 p13 = (ComponentPort) e10.newPort("P13"); 099 p14 = (ComponentPort) e9.newPort("P14"); 100 101 // Create links 102 r1 = e4.connect(p1, p0, "R1"); 103 r2 = e4.connect(p1, p4, "R2"); 104 p3.link(r2); 105 r3 = e4.connect(p1, p2, "R3"); 106 r4 = e3.connect(p4, p7, "R4"); 107 r5 = e3.connect(p4, p5, "R5"); 108 e3.allowLevelCrossingConnect(true); 109 110 r6 = e3.connect(p3, p6, "R6"); 111 r7 = e0.connect(p7, p13, "R7"); 112 r8 = e7.connect(p9, p8, "R8"); 113 r9 = e7.connect(p10, p11, "R9"); 114 r10 = e0.connect(p8, p12, "R10"); 115 r11 = e10.connect(p12, p13, "R11"); 116 r12 = e10.connect(p14, p13, "R12"); 117 p11.link(r7); 118 } 119 120 /////////////////////////////////////////////////////////////////// 121 //// public methods //// 122 123 /** Return the results as a String. */ 124 @Override 125 public String toString() { 126 return "----Methods of ComponentRelation----\n" + "linkedPorts:\n" 127 + printLinkedPorts(r1) + printLinkedPorts(r2) 128 + printLinkedPorts(r3) + printLinkedPorts(r4) 129 + printLinkedPorts(r5) + printLinkedPorts(r6) 130 + printLinkedPorts(r7) + printLinkedPorts(r8) 131 + printLinkedPorts(r9) + printLinkedPorts(r10) 132 + printLinkedPorts(r11) + printLinkedPorts(r12) 133 + "\ndeepLinkedPorts:\n" + printDeepLinkedPorts(r1) 134 + printDeepLinkedPorts(r2) + printDeepLinkedPorts(r3) 135 + printDeepLinkedPorts(r4) + printDeepLinkedPorts(r5) 136 + printDeepLinkedPorts(r6) + printDeepLinkedPorts(r7) 137 + printDeepLinkedPorts(r8) + printDeepLinkedPorts(r9) 138 + printDeepLinkedPorts(r10) + printDeepLinkedPorts(r11) 139 + printDeepLinkedPorts(r12) 140 + "\n----Methods of ComponentPort----\n" + "connectedPorts:\n" 141 + printConnectedPorts(p0) + printConnectedPorts(p1) 142 + printConnectedPorts(p2) + printConnectedPorts(p3) 143 + printConnectedPorts(p4) + printConnectedPorts(p5) 144 + printConnectedPorts(p6) + printConnectedPorts(p7) 145 + printConnectedPorts(p8) + printConnectedPorts(p9) 146 + printConnectedPorts(p10) + printConnectedPorts(p11) 147 + printConnectedPorts(p12) + printConnectedPorts(p13) 148 + printConnectedPorts(p14) + "\ndeepConnectedPorts:\n" 149 + printDeepConnectedPorts(p0) + printDeepConnectedPorts(p1) 150 + printDeepConnectedPorts(p2) + printDeepConnectedPorts(p3) 151 + printDeepConnectedPorts(p4) + printDeepConnectedPorts(p5) 152 + printDeepConnectedPorts(p6) + printDeepConnectedPorts(p7) 153 + printDeepConnectedPorts(p8) + printDeepConnectedPorts(p9) 154 + printDeepConnectedPorts(p10) + printDeepConnectedPorts(p11) 155 + printDeepConnectedPorts(p12) + printDeepConnectedPorts(p13) 156 + printDeepConnectedPorts(p14); 157 } 158 159 /** Print the linked ports for a given ComponentRelation. The ports 160 * are restricted in the same level of hierarchy 161 * @see ptolemy.kernel.Relation#linkedPortList() 162 * @param r Print the linked ports for this relation. 163 */ 164 public String printLinkedPorts(ComponentRelation r) { 165 StringBuffer st = new StringBuffer(r.getName() + ": "); 166 ComponentPort po; 167 Iterator ports = r.linkedPortList().iterator(); 168 169 while (ports.hasNext()) { 170 po = (ComponentPort) ports.next(); 171 st.append(po.getName() + " "); 172 } 173 174 return st.toString() + "\n"; 175 } 176 177 /** Print the deeply linked ports for a given 178 * ComponentRelation. Look through all transparent ports and return 179 * only non transparent ports (those with no inside links). 180 * @param r Print the deeply linked ports for this 181 * relation. 182 * @see ptolemy.kernel.ComponentRelation#deepLinkedPortList() 183 */ 184 public String printDeepLinkedPorts(ComponentRelation r) { 185 StringBuffer st = new StringBuffer(r.getName() + ": "); 186 ComponentPort po; 187 Iterator ports = r.deepLinkedPortList().iterator(); 188 189 while (ports.hasNext()) { 190 po = (ComponentPort) ports.next(); 191 st.append(po.getName() + " "); 192 } 193 194 return st.toString() + "\n"; 195 } 196 197 /** Print the connected ports for a given ComponentPort. Restricted 198 * to the same level of hierarchy. 199 * @param p Print the connected ports for this Port. 200 * @see ptolemy.kernel.Port#connectedPortList() 201 */ 202 public String printConnectedPorts(ComponentPort p) { 203 StringBuffer st = new StringBuffer(p.getName() + ": "); 204 ComponentPort po; 205 Iterator ports = p.connectedPortList().iterator(); 206 207 while (ports.hasNext()) { 208 po = (ComponentPort) ports.next(); 209 st.append(po.getName() + " "); 210 } 211 212 return st.toString() + "\n"; 213 } 214 215 /** Print the deeply connected ports for a given 216 * ComponentPort. Look through all transparent ports and return 217 * only non transparent ports (those with no inside links). 218 * @param p Print the deeply connected ports for this Port. 219 * @see ptolemy.kernel.ComponentPort#deepConnectedPortList() 220 */ 221 public String printDeepConnectedPorts(ComponentPort p) { 222 StringBuffer st = new StringBuffer(p.getName() + ": "); 223 ComponentPort po; 224 Iterator ports = p.deepConnectedPortList().iterator(); 225 226 while (ports.hasNext()) { 227 po = (ComponentPort) ports.next(); 228 st.append(po.getName() + " "); 229 } 230 231 return st.toString() + "\n"; 232 } 233 234 /** Create an Example System, then print it out. 235 * @exception NameDuplicationException if the example system cannot 236 * be built because of a duplicate name 237 * @exception IllegalActionException if the example system cannot 238 * be built. 239 */ 240 public static void main(String[] args) 241 throws NameDuplicationException, IllegalActionException { 242 ExampleSystem exsys = new ExampleSystem(); 243 System.out.println(exsys.toString()); 244 } 245 246 /////////////////////////////////////////////////////////////////// 247 //// private variables //// 248 // Components of the system. 249 250 /** @serial Composite Entities that make up the Example System. */ 251 public CompositeEntity e0; 252 253 /////////////////////////////////////////////////////////////////// 254 //// private variables //// 255 // Components of the system. 256 257 /** @serial Composite Entities that make up the Example System. */ 258 public CompositeEntity e3; 259 260 /////////////////////////////////////////////////////////////////// 261 //// private variables //// 262 // Components of the system. 263 264 /** @serial Composite Entities that make up the Example System. */ 265 public CompositeEntity e4; 266 267 /////////////////////////////////////////////////////////////////// 268 //// private variables //// 269 // Components of the system. 270 271 /** @serial Composite Entities that make up the Example System. */ 272 public CompositeEntity e7; 273 274 /////////////////////////////////////////////////////////////////// 275 //// private variables //// 276 // Components of the system. 277 278 /** @serial Composite Entities that make up the Example System. */ 279 public CompositeEntity e10; 280 281 /** @serial Component Entities that make up the Example System. */ 282 public ComponentEntity e1; 283 284 /** @serial Component Entities that make up the Example System. */ 285 public ComponentEntity e2; 286 287 /** @serial Component Entities that make up the Example System. */ 288 public ComponentEntity e5; 289 290 /** @serial Component Entities that make up the Example System. */ 291 public ComponentEntity e6; 292 293 /** @serial Component Entities that make up the Example System. */ 294 public ComponentEntity e8; 295 296 /** @serial Component Entities that make up the Example System. */ 297 public ComponentEntity e9; 298 299 /** @serial Component Ports that make up the Example System. */ 300 public ComponentPort p0; 301 302 /** @serial Component Ports that make up the Example System. */ 303 public ComponentPort p1; 304 305 /** @serial Component Ports that make up the Example System. */ 306 public ComponentPort p2; 307 308 /** @serial Component Ports that make up the Example System. */ 309 public ComponentPort p3; 310 311 /** @serial Component Ports that make up the Example System. */ 312 public ComponentPort p4; 313 314 /** @serial Component Ports that make up the Example System. */ 315 public ComponentPort p5; 316 317 /** @serial Component Ports that make up the Example System. */ 318 public ComponentPort p6; 319 320 /** @serial Component Ports that make up the Example System. */ 321 public ComponentPort p7; 322 323 /** @serial Component Ports that make up the Example System. */ 324 public ComponentPort p8; 325 326 /** @serial Component Ports that make up the Example System. */ 327 public ComponentPort p9; 328 329 /** @serial Component Ports that make up the Example System. */ 330 public ComponentPort p10; 331 332 /** @serial Component Ports that make up the Example System. */ 333 public ComponentPort p11; 334 335 /** @serial Component Ports that make up the Example System. */ 336 public ComponentPort p12; 337 338 /** @serial Component Ports that make up the Example System. */ 339 public ComponentPort p13; 340 341 /** @serial Component Ports that make up the Example System. */ 342 public ComponentPort p14; 343 344 /** @serial Component Relations that make up the Example System. */ 345 public ComponentRelation r1; 346 347 /** @serial Component Relations that make up the Example System. */ 348 public ComponentRelation r2; 349 350 /** @serial Component Relations that make up the Example System. */ 351 public ComponentRelation r3; 352 353 /** @serial Component Relations that make up the Example System. */ 354 public ComponentRelation r4; 355 356 /** @serial Component Relations that make up the Example System. */ 357 public ComponentRelation r5; 358 359 /** @serial Component Relations that make up the Example System. */ 360 public ComponentRelation r6; 361 362 /** @serial Component Relations that make up the Example System. */ 363 public ComponentRelation r7; 364 365 /** @serial Component Relations that make up the Example System. */ 366 public ComponentRelation r8; 367 368 /** @serial Component Relations that make up the Example System. */ 369 public ComponentRelation r9; 370 371 /** @serial Component Relations that make up the Example System. */ 372 public ComponentRelation r10; 373 374 /** @serial Component Relations that make up the Example System. */ 375 public ComponentRelation r11; 376 377 /** @serial Component Relations that make up the Example System. */ 378 public ComponentRelation r12; 379}