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.actors; 031 032import java.util.Iterator; 033 034import ptolemy.kernel.CompositeEntity; 035import ptolemy.kernel.util.Attribute; 036import ptolemy.kernel.util.IllegalActionException; 037import ptolemy.kernel.util.NameDuplicationException; 038import ptolemy.kernel.util.NamedObj; 039 040/** 041 * This is an attribute that gets attached to a configured merge actor instance. 042 * The attribute denotes a simple mapping from a source port to a target port. 043 * Also, a single conversion can be applied to the source port as part of the 044 * mapping (in particular, a simple unit conversion) 045 * 046 * @author Shawn Bowers 047 * @created October 17, 2005 048 */ 049 050public class SimpleMergeMapping extends Attribute { 051 052 /** 053 * Constructor 054 */ 055 public SimpleMergeMapping(String sourceActor, String sourcePort, 056 String targetPort) throws IllegalActionException, 057 NameDuplicationException { 058 super(); 059 setSourceActor(sourceActor); 060 setSourceActorPort(sourcePort); 061 setTargetPort(targetPort); 062 } 063 064 /** 065 * Constructor 066 */ 067 public SimpleMergeMapping(NamedObj container, String name) 068 throws IllegalActionException, NameDuplicationException { 069 super(container, name); 070 } 071 072 /** 073 * Constructor 074 */ 075 public SimpleMergeMapping(NamedObj container, String name, 076 String sourceActor, String sourcePort, String targetPort) 077 throws IllegalActionException, NameDuplicationException { 078 super(container, name); 079 setSourceActor(sourceActor); 080 setSourceActorPort(sourcePort); 081 setTargetPort(targetPort); 082 } 083 084 /** 085 * Constructor 086 */ 087 public SimpleMergeMapping(NamedObj container, String name, 088 String sourceActor, String sourcePort, String targetPort, 089 String conversion) throws IllegalActionException, 090 NameDuplicationException { 091 super(container, name); 092 setSourceActor(sourceActor); 093 setSourceActorPort(sourcePort); 094 setTargetPort(targetPort); 095 setConversion(conversion); 096 } 097 098 /** 099 * @param container 100 * The container for this attribute 101 */ 102 public void setContainer(NamedObj container) throws IllegalActionException, 103 NameDuplicationException { 104 if (container == null) { 105 super.setContainer(container); 106 return; 107 } 108 109 if (!(container instanceof MergeActor)) { 110 String msg = "This attribute can only be applied to " 111 + "org.Kepler.sms.actor.MergeActor instances."; 112 throw new IllegalActionException(this, msg); 113 } 114 super.setContainer(container); 115 } 116 117 /** 118 * @return The name of the source actor 119 */ 120 public String getSourceActor() { 121 Iterator iter = attributeList(SimpleMergeSourceActor.class).iterator(); 122 if (!iter.hasNext()) 123 return null; 124 SimpleMergeSourceActor a = (SimpleMergeSourceActor) iter.next(); 125 return a.getSourceActor(); 126 } 127 128 /** 129 * @return The name of the source actor port 130 */ 131 public String getSourceActorPort() { 132 Iterator iter = attributeList(SimpleMergeSourceActorPort.class) 133 .iterator(); 134 if (!iter.hasNext()) 135 return null; 136 SimpleMergeSourceActorPort p = (SimpleMergeSourceActorPort) iter.next(); 137 return p.getSourceActorPort(); 138 } 139 140 /** 141 * @return The name of the target port 142 */ 143 public String getTargetPort() { 144 Iterator iter = attributeList(SimpleMergeTargetPort.class).iterator(); 145 if (!iter.hasNext()) 146 return null; 147 SimpleMergeTargetPort p = (SimpleMergeTargetPort) iter.next(); 148 return p.getTargetPort(); 149 } 150 151 /** 152 * @return The name of the conversion 153 */ 154 public String getConversion() { 155 Iterator iter = attributeList(SimpleMergeConversion.class).iterator(); 156 if (!iter.hasNext()) 157 return null; 158 SimpleMergeConversion c = (SimpleMergeConversion) iter.next(); 159 return c.getConversion(); 160 } 161 162 /** 163 * Set the name of the source actor 164 */ 165 public void setSourceActor(String name) throws IllegalActionException, 166 NameDuplicationException { 167 if (name == null) 168 throw new IllegalActionException( 169 "setting source actor name to null"); 170 Iterator iter = attributeList(SimpleMergeSourceActor.class).iterator(); 171 SimpleMergeSourceActor a = null; 172 if (!iter.hasNext()) 173 a = new SimpleMergeSourceActor(this, "_sourceActor"); 174 else 175 a = (SimpleMergeSourceActor) iter.next(); 176 a.setSourceActor(name); 177 } 178 179 /** 180 * Set the name of the source actor port 181 */ 182 public void setSourceActorPort(String name) throws IllegalActionException, 183 NameDuplicationException { 184 if (name == null) 185 throw new IllegalActionException( 186 "setting source actor port to null"); 187 Iterator iter = attributeList(SimpleMergeSourceActorPort.class) 188 .iterator(); 189 SimpleMergeSourceActorPort p = null; 190 if (!iter.hasNext()) 191 p = new SimpleMergeSourceActorPort(this, "_sourceActorPort"); 192 else 193 p = (SimpleMergeSourceActorPort) iter.next(); 194 p.setSourceActorPort(name); 195 } 196 197 /** 198 * Set the name of the target port 199 */ 200 public void setTargetPort(String name) throws IllegalActionException, 201 NameDuplicationException { 202 if (name == null) 203 throw new IllegalActionException("setting target port name to null"); 204 Iterator iter = attributeList(SimpleMergeTargetPort.class).iterator(); 205 SimpleMergeTargetPort p = null; 206 if (!iter.hasNext()) 207 p = new SimpleMergeTargetPort(this, "_targetPort"); 208 else 209 p = (SimpleMergeTargetPort) iter.next(); 210 p.setTargetPort(name); 211 } 212 213 /** 214 * Set the name of the conversion 215 */ 216 public void setConversion(String name) throws IllegalActionException, 217 NameDuplicationException { 218 if (name == null) 219 throw new IllegalActionException( 220 "setting conversion function name to null"); 221 Iterator iter = attributeList(SimpleMergeConversion.class).iterator(); 222 SimpleMergeConversion c = null; 223 if (!iter.hasNext()) 224 c = new SimpleMergeConversion(this, "_conversion"); 225 else 226 c = (SimpleMergeConversion) iter.next(); 227 c.setConversion(name); 228 } 229 230 public boolean equals(Object obj) { 231 if (obj == null || !(obj instanceof SimpleMergeMapping)) 232 return false; 233 SimpleMergeMapping that = (SimpleMergeMapping) obj; 234 235 String thatActor = that.getSourceActor(); 236 String thisActor = this.getSourceActor(); 237 String thatActorPort = that.getSourceActorPort(); 238 String thisActorPort = this.getSourceActorPort(); 239 String thatTarget = that.getTargetPort(); 240 String thisTarget = this.getTargetPort(); 241 String thatConv = that.getConversion(); 242 String thisConv = this.getConversion(); 243 244 if (thatActor == null || thatActorPort == null || thatTarget == null) 245 return false; 246 247 if ((thatConv == null && thisConv != null) 248 || (thisConv == null && thatConv != null)) 249 return false; 250 251 if (!thatActor.equals(thisActor)) 252 return false; 253 if (!thatActorPort.equals(thisActorPort)) 254 return false; 255 if (!thatTarget.equals(thisTarget)) 256 return false; 257 if (thatConv != null && !thatConv.equals(thisConv)) 258 return false; 259 260 return true; 261 } 262 263 /** 264 * For testing 265 */ 266 public static void main(String[] args) { 267 try { 268 MergeActor a = new MergeActor(new CompositeEntity(), 269 "Merge Actor 1"); 270 SimpleMergeMapping m1 = new SimpleMergeMapping(a, "merge1", "a1", 271 "p1", "t1"); 272 SimpleMergeMapping m2 = new SimpleMergeMapping(a, "merge2", "a2", 273 "p1", "t1", "c1"); 274 275 System.out.println("merge1: <" + m1.getSourceActor() + ", " 276 + m1.getSourceActorPort() + ", " + m1.getTargetPort() 277 + ", " + m1.getConversion() + ">"); 278 279 System.out.println("merge2: <" + m2.getSourceActor() + ", " 280 + m2.getSourceActorPort() + ", " + m2.getTargetPort() 281 + ", " + m2.getConversion() + ">"); 282 283 } catch (Exception e) { 284 e.printStackTrace(); 285 } 286 } 287 288} // SimpleMergeMapping