001/* 002 * Copyright (c) 2005-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: welker $' 006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 007 * '$Revision: 24234 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.kepler.sms; 031 032import java.util.Collection; 033import java.util.Iterator; 034 035import ptolemy.actor.IOPort; 036import ptolemy.kernel.Entity; 037import ptolemy.kernel.util.IllegalActionException; 038import ptolemy.kernel.util.NameDuplicationException; 039import ptolemy.kernel.util.NamedObj; 040import ptolemy.kernel.util.Settable; 041import ptolemy.kernel.util.StringAttribute; 042import ptolemy.kernel.util.Workspace; 043 044/** 045 * An IO Port Reference refers to a ptolemy Port within its containing entity. 046 * The port is referenced by name within the context of the entity (actor). 047 * 048 * @author Shawn Bowers 049 * @created June 14, 2005 050 * @see ptolemy.kernel.Port 051 */ 052public class KeplerIOPortReference extends StringAttribute { 053 054 /** Constructor */ 055 public KeplerIOPortReference() { 056 super(); 057 } 058 059 /** 060 * Constructor 061 * 062 * @param container 063 * Description of the Parameter 064 * @param name 065 * The value of the property 066 * @exception IllegalActionException 067 * Description of the Exception 068 * @exception NameDuplicationException 069 * Description of the Exception 070 */ 071 public KeplerIOPortReference(NamedObj container, String name) 072 throws IllegalActionException, NameDuplicationException { 073 super(container, name); 074 } 075 076 /** 077 * Constructor 078 * 079 * @param workspace 080 * Description of the Parameter 081 */ 082 public KeplerIOPortReference(Workspace workspace) { 083 super(workspace); 084 } 085 086 /** 087 * Return the default expression which is null 088 * 089 * @return The defaultExpression value. Currently if not given, then 090 * unknown. 091 */ 092 public String getDefaultExpression() { 093 return null; 094 } 095 096 /** 097 * Return the corresponding IOPort for the reference. 098 * 099 * @return The IOPort referred to by this reference. 100 */ 101 public Object getPort() { 102 // get the container, etc. 103 String portName = getExpression(); 104 105 // iterate to first entity container 106 NamedObj container = getContainer(); 107 while (container != null && !(container instanceof Entity)) 108 container = container.getContainer(); 109 110 // couldn't find a container 111 if (container == null) 112 return null; 113 114 // find the corresponding port 115 Entity actor = (Entity) container; 116 for (Iterator ports = actor.portList().iterator(); ports.hasNext();) { 117 IOPort port = (IOPort) ports.next(); 118 if (portName == null && port.getName() == null) 119 return port; 120 else if (portName.equals(port.getName())) 121 return port; 122 } 123 return null; 124 } 125 126 /** 127 * Create a reference from the given port. 128 * 129 * @param port 130 * The port that this reference refers to 131 */ 132 public void setPort(IOPort port) throws IllegalActionException { 133 setExpression(port.getName()); 134 } 135 136 /** 137 * Set visibility to NOT_EDITABLE 138 * 139 * @return The visibility value 140 */ 141 public Settable.Visibility getVisibility() { 142 // return NONE; // this should be changed? 143 // return ptolemy.kernel.util.Settable.FULL; 144 return ptolemy.kernel.util.Settable.NOT_EDITABLE; 145 } 146 147 /** 148 * The visibility cannot be changed. This method does nothing. 149 * 150 * @param visibility 151 * The new visibility value 152 */ 153 public void setVisibility(Settable.Visibility visibility) { 154 // do nothing....we don't want the visibility getting changed 155 } 156 157 /** 158 * Validate the expression. The expression is valid if it has a 159 * corresponding port. 160 */ 161 public Collection validate() throws IllegalActionException { 162 // want to make sure the port exists ... 163 if (getPort() == null) { 164 String msg = "Could not find port '" + getExpression() + "'"; 165 throw new IllegalActionException(this, msg); 166 } 167 return null; 168 } 169 170 /** 171 * References are equal when they refer to the same port. 172 */ 173 public boolean equals(Object obj) { 174 if (!(obj instanceof KeplerIOPortReference)) 175 return false; 176 KeplerIOPortReference ref = (KeplerIOPortReference) obj; 177 if (getPort() == null) 178 return false; 179 return getPort().equals(ref.getPort()); 180 } 181 182} // KeplerIOPortReference