001/* 002 * Copyright (c) 2003-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.objectmanager.cache; 031 032import ptolemy.actor.TypedAtomicActor; 033import ptolemy.actor.TypedIOPort; 034import ptolemy.data.IntToken; 035import ptolemy.data.StringToken; 036import ptolemy.data.expr.Parameter; 037import ptolemy.data.expr.StringParameter; 038import ptolemy.data.type.BaseType; 039import ptolemy.kernel.CompositeEntity; 040import ptolemy.kernel.Port; 041import ptolemy.kernel.util.IllegalActionException; 042import ptolemy.kernel.util.InternalErrorException; 043import ptolemy.kernel.util.NameDuplicationException; 044 045/** 046 * 047 */ 048public class DataCachePutActor extends TypedAtomicActor { 049 public StringParameter fileType = new StringParameter(this, "fileType"); 050 // EXTERNAL or COPY_FILE_TO_CACHE 051 public TypedIOPort inputFilename = new TypedIOPort(this, "inputFilename", 052 true, false); 053 public TypedIOPort inputObjectName = new TypedIOPort(this, 054 "inputObjectName", true, false); 055 public TypedIOPort inputObjectType = new TypedIOPort(this, 056 "inputObjectType", true, false); 057 public TypedIOPort trigger = new TypedIOPort(this, "trigger", true, false); 058 public TypedIOPort outputCacheID = new TypedIOPort(this, "outputCacheID", 059 false, true); 060 public Parameter outputCacheIDTokenProductionRate; 061 062 /** 063 * 064 */ 065 public DataCachePutActor(CompositeEntity container, String name) 066 throws NameDuplicationException, IllegalActionException { 067 super(container, name); 068 inputFilename.setTypeEquals(BaseType.STRING); 069 inputFilename.setMultiport(true); 070 inputObjectName.setTypeEquals(BaseType.STRING); 071 inputObjectName.setMultiport(true); 072 inputObjectType.setTypeEquals(BaseType.STRING); 073 inputObjectType.setMultiport(true); 074 outputCacheID.setTypeEquals(BaseType.STRING); 075 outputCacheIDTokenProductionRate = new Parameter(outputCacheID, 076 "tokenProductionRate"); 077 outputCacheIDTokenProductionRate.setExpression("0"); 078 } 079 080 /** 081 * Notify this entity that the links to the specified port have been 082 * altered. This sets the production rate of the output port and notifies 083 * the director that the schedule is invalid, if there is a director. 084 * 085 * @param port 086 */ 087 public void connectionsChanged(Port port) { 088 super.connectionsChanged(port); 089 if (port == inputFilename) { 090 try { 091 outputCacheIDTokenProductionRate.setToken(new IntToken( 092 inputFilename.getWidth())); 093 // NOTE: schedule is invalidated automatically already 094 // by the changed connections. 095 } catch (IllegalActionException ex) { 096 throw new InternalErrorException(this, ex, 097 "Failed to set the token production rate for the outputCacheID port."); 098 } 099 } 100 } 101 102 /** 103 * 104 */ 105 public void initialize() throws IllegalActionException { 106 } 107 108 /** 109 * 110 */ 111 public boolean prefire() throws IllegalActionException { 112 return super.prefire(); 113 } 114 115 /** 116 * 117 */ 118 public void fire() throws IllegalActionException { 119 super.fire(); 120 if (trigger.getWidth() > 0 && !trigger.hasToken(0)) { // make sure we're 121 // ready to fire 122 return; 123 } 124 125 String fileTypeStr = fileType.stringValue(); 126 // find out if we are moving the file to the cache dir or not 127 int fileTypeInt = DataCacheFileObject.EXTERNAL; // default to external 128 if (fileTypeStr.equals("Copy the file to the cache directory")) { 129 fileTypeInt = DataCacheFileObject.COPY_FILE_TO_CACHE; 130 } 131 132 // find out how many files we're putting in the cache 133 int inputFilenameWidth = inputFilename.getWidth(); 134 135 for (int i = 0; i < inputFilenameWidth; i++) { // loop through each 136 // input on the port 137 // get the input file name 138 StringToken inputFilenameToken = (StringToken) inputFilename.get(i); 139 // get the resource name and type of the file 140 StringToken inputObjectTypeToken = (StringToken) inputObjectType 141 .get(i); 142 StringToken inputObjectNameToken = (StringToken) inputObjectName 143 .get(i); 144 145 try { 146 DataCacheFileObject fileItem = DataCacheManager.putFile( 147 inputFilenameToken.stringValue(), inputObjectNameToken 148 .stringValue(), inputObjectTypeToken 149 .stringValue(), fileTypeInt); 150 outputCacheID.broadcast(new StringToken(fileItem.getFile() 151 .getAbsolutePath())); 152 } catch (Exception e) { 153 throw new IllegalActionException( 154 "Error getting putting item in cache: " 155 + e.getMessage()); 156 } 157 } 158 } 159}