001/* Relation linking TypedIOPorts. 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 027 028 */ 029package ptolemy.actor; 030 031import ptolemy.kernel.CompositeEntity; 032import ptolemy.kernel.Port; 033import ptolemy.kernel.Relation; 034import ptolemy.kernel.util.IllegalActionException; 035import ptolemy.kernel.util.NameDuplicationException; 036import ptolemy.kernel.util.Workspace; 037 038/////////////////////////////////////////////////////////////////// 039//// TypedIORelation 040 041/** 042 This class overrides some of the methods in IORelation to ensure that 043 TypedIOPorts are only connected to TypedIOPorts. I.e., Instances of 044 TypedIORelation can only be linked to instances of TypedIOPort. 045 Derived classes may further constrain this to subclasses of TypedIOPort. 046 Such derived classes should override the protected method _checkPort() 047 to throw an exception. 048 <p> 049 To link a TypedIOPort to a TypedIORelation, use the link() or 050 liberalLink() method in the TypedIOPort class. To remove a link, 051 use the unlink() method. 052 <p> 053 The container for instances of this class can only be instances of 054 TypedCompositeActor. Derived classes may wish to further constrain the 055 container to subclasses of TypedComponentEntity. To do this, they should 056 override the _checkContainer() method. 057 058 @author Yuhong Xiong 059 @version $Id$ 060 @since Ptolemy II 0.2 061 @Pt.ProposedRating Green (yuhong) 062 @Pt.AcceptedRating Green (liuxj) 063 */ 064public class TypedIORelation extends IORelation { 065 // all the constructors are wrappers of the super class constructors. 066 067 /** Construct a relation in the default workspace with an empty string 068 * as its name. Add the relation to the directory of the workspace. 069 */ 070 public TypedIORelation() { 071 super(); 072 } 073 074 /** Construct a relation in the specified workspace with an empty 075 * string as a name. You can then change the name with setName(). 076 * If the workspace argument is null, then use the default workspace. 077 * Add the relation to the workspace directory. 078 * 079 * @param workspace The workspace that will list the relation. 080 */ 081 public TypedIORelation(Workspace workspace) { 082 super(workspace); 083 } 084 085 /** Construct a relation with the given name contained by the specified 086 * entity. The container argument must not be null, or a 087 * NullPointerException will be thrown. This relation will use the 088 * workspace of the container for synchronization and version counts. 089 * If the name argument is null, then the name is set to the empty string. 090 * This constructor write-synchronizes on the workspace. 091 * 092 * @param container The container. 093 * @param name The name of the relation. 094 * @exception IllegalActionException If the container is incompatible 095 * with this relation. 096 * @exception NameDuplicationException If the name coincides with 097 * a relation already in the container. 098 */ 099 public TypedIORelation(CompositeEntity container, String name) 100 throws IllegalActionException, NameDuplicationException { 101 super(container, name); 102 } 103 104 /////////////////////////////////////////////////////////////////// 105 //// protected methods //// 106 107 /** Override the method in the super class to constrain the 108 * container to be an instance of TypedCompositeActor, or to be null. 109 * 110 * @param container The proposed container. 111 * @exception IllegalActionException If the container is not a 112 * TypedCompositeActor or null, or this relation and the container 113 * are not in the same workspace. 114 */ 115 @Override 116 protected void _checkContainer(CompositeEntity container) 117 throws IllegalActionException { 118 if (!(container instanceof TypedCompositeActor) && container != null) { 119 throw new IllegalActionException(this, container, 120 "TypedIORelation can only be contained by " 121 + "TypedCompositeActor."); 122 } 123 } 124 125 /** Throw an exception if the specified port cannot be linked to this 126 * relation (is not of class TypedIOPort). 127 * @param port The candidate port to link to. 128 * @exception IllegalActionException If the port is not an 129 * TypedIOPort. 130 */ 131 @Override 132 protected void _checkPort(Port port) throws IllegalActionException { 133 if (!(port instanceof TypedIOPort)) { 134 throw new IllegalActionException(this, port, 135 "TypedIORelation can only link to a TypedIOPort."); 136 } 137 } 138 139 /** Throw an exception if the specified relation is not an instance 140 * of TypedIORelation. 141 * @param relation The relation to link to. 142 * @param symmetric If true, the call _checkRelation on the specified 143 * relation with this as an argument. 144 * @exception IllegalActionException If this port has no container, 145 * or if this port is not an acceptable port for the specified 146 * relation. 147 */ 148 @Override 149 protected void _checkRelation(Relation relation, boolean symmetric) 150 throws IllegalActionException { 151 if (!(relation instanceof TypedIORelation)) { 152 throw new IllegalActionException(this, relation, 153 "TypedIORelation can only link to a TypedIORelation."); 154 } 155 156 super._checkRelation(relation, symmetric); 157 } 158}