001/* 002 * Copyright (c) 2009-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: crawl $' 006 * '$Date: 2014-07-14 22:26:04 +0000 (Mon, 14 Jul 2014) $' 007 * '$Revision: 32837 $' 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.date; 031 032import java.util.Date; 033 034import ptolemy.actor.TypedIOPort; 035import ptolemy.actor.lib.RandomSource; 036import ptolemy.data.DateToken; 037import ptolemy.data.type.BaseType; 038import ptolemy.kernel.CompositeEntity; 039import ptolemy.kernel.util.IllegalActionException; 040import ptolemy.kernel.util.NameDuplicationException; 041 042/** 043 * Generate a random date between upper and lower dates. 044 * 045 * @author Daniel Crawl 046 * @version $Id: RandomDate.java 32837 2014-07-14 22:26:04Z crawl $ 047 */ 048 049public class RandomDate extends RandomSource { 050 051 /** 052 * Construct a RandomDate with the given container and name. 053 * 054 * @param name 055 * The name of this actor. 056 * @exception IllegalActionException 057 * If the entity cannot be contained by the proposed 058 * container. 059 * @exception NameDuplicationException 060 * If the container already has an actor with this name. 061 */ 062 public RandomDate(CompositeEntity container, String name) 063 throws NameDuplicationException, IllegalActionException { 064 super(container, name); 065 066 lowerBound = new TypedIOPort(this, "lowerBound", true, false); 067 lowerBound.setTypeEquals(BaseType.DATE); 068 069 upperBound = new TypedIOPort(this, "upperBound", true, false); 070 upperBound.setTypeEquals(BaseType.DATE); 071 072 output.setTypeEquals(BaseType.DATE); 073 output.setMultiport(true); 074 output.setDefaultWidth(1); 075 076 _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" " 077 + "width=\"60\" height=\"20\" " + "style=\"fill:white\"/>\n" 078 + "</svg>\n"); 079 } 080 081 // ///////////////////////////////////////////////////////////////// 082 // // ports and parameters //// 083 084 /** The lower bound of the date. */ 085 public TypedIOPort lowerBound; 086 087 /** The upper bound of the date. */ 088 public TypedIOPort upperBound; 089 090 // ///////////////////////////////////////////////////////////////// 091 // // public methods //// 092 093 /** Output the most recently generated random date. */ 094 @Override 095 public void fire() throws IllegalActionException { 096 super.fire(); 097 output.broadcast(new DateToken(_currentDate.getTime())); 098 } 099 100 /** Generate a new random date. */ 101 @Override 102 protected void _generateRandomNumber() throws IllegalActionException { 103 long lower = ((DateToken) lowerBound.get(0)).getValue(); 104 long upper = ((DateToken) upperBound.get(0)).getValue(); 105 106 if (lower > upper) { 107 throw new IllegalActionException(this, 108 "Invalid bounds: lowerBound is later than upperBound."); 109 } 110 111 double rawNum = _random.nextDouble(); 112 double num = (rawNum * (upper - lower)) + lower; 113 _currentDate = new Date(new Double(num).longValue()); 114 } 115 116 /** The most recently generated random date. */ 117 private Date _currentDate; 118}