001/* 002 * $RCSfile$ 003 * 004 * $Author: crawl $ 005 * $Date: 2015-08-24 22:44:14 +0000 (Mon, 24 Aug 2015) $ 006 * $Revision: 33630 $ 007 * 008 * For Details: http://kepler-project.org 009 * 010 * Copyright (c) 2007 The Regents of the University of California. 011 * All rights reserved. 012 * 013 * Permission is hereby granted, without written agreement and without 014 * license or royalty fees, to use, copy, modify, and distribute this 015 * software and its documentation for any purpose, provided that the 016 * above copyright notice and the following two paragraphs appear in 017 * all copies of this software. 018 * 019 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 020 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 021 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 022 * IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY 023 * OF SUCH DAMAGE. 024 * 025 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 026 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 027 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 028 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY 029 * OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, 030 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 031 */ 032package org.kepler.monitor.test; 033 034import java.awt.Font; 035import java.awt.geom.Rectangle2D; 036 037import javax.swing.SwingConstants; 038 039import org.kepler.monitor.MonitorIcon; 040 041import diva.canvas.CompositeFigure; 042import diva.canvas.toolbox.LabelFigure; 043import ptolemy.actor.lib.Transformer; 044import ptolemy.data.expr.StringParameter; 045import ptolemy.kernel.CompositeEntity; 046import ptolemy.kernel.util.IllegalActionException; 047import ptolemy.kernel.util.NameDuplicationException; 048 049/** 050 * This actor demonstrates the association of monitoring icons that show the 051 * number of tokens being received/produced in each port. 052 * 053 * @see org.kepler.monitor.figure 054 * 055 * @author Carlos Rueda 056 * @version $Id: ActorWithTokenCounters.java 33630 2015-08-24 22:44:14Z crawl $ 057 */ 058public class ActorWithTokenCounters extends Transformer { 059 public StringParameter maxParam; 060 061 public ActorWithTokenCounters(CompositeEntity container, String name) 062 throws IllegalActionException, NameDuplicationException { 063 super(container, name); 064 065 _icon = new MonitorIcon(this, "_icon"); 066 _icon.setText("ActorWithTokenCounters"); 067 068 _inputCounterFigure = _createFigure("in=0"); 069 _outputCounterFigure = _createFigure("out=0"); 070 071 CompositeFigure _figure = new CompositeFigure(); 072 _figure.add(_inputCounterFigure); 073 _figure.add(_outputCounterFigure); 074 075 // TODO: proper location of figures is still unclear 076 077 Rectangle2D inpBounds = _inputCounterFigure.getBounds(); 078 double h = 25 + inpBounds.getHeight(); 079 double inX = 20; 080 double outX = inX + 5 * inpBounds.getWidth(); 081 _inputCounterFigure.translate(inX, h); 082 _outputCounterFigure.translate(outX, h); 083 084 _icon.setFigure(_figure); 085 } 086 087 /** 088 * Starts the counters. 089 */ 090 public void initialize() throws IllegalActionException { 091 super.initialize(); 092 _inputCounterFigure.setString("in=" + (_inputCounter = 0)); 093 _outputCounterFigure.setString("out=" + (_outputCounter = 0)); 094 } 095 096 /** 097 * Broadcasts every other input token. Updates the input and output 098 * counters. 099 */ 100 public void fire() throws IllegalActionException { 101 super.fire(); 102 if (input.hasToken(0)) { 103 _inputCounterFigure.setString("in=" + (++_inputCounter)); 104 if (_inputCounter % 2 == 0) { 105 output.broadcast(input.get(0)); 106 _outputCounterFigure.setString("out=" + (++_outputCounter)); 107 } 108 } 109 } 110 111 // ///////////////////////////////////////////////////////////////// 112 // // private members //// 113 114 /** Creates a figure for a counter */ 115 private static LabelFigure _createFigure(String str) { 116 LabelFigure fig = new LabelFigure(str); 117 fig.setAnchor(SwingConstants.LEFT); 118 fig.setFont(new Font("monospaced", Font.PLAIN, 8)); 119 return fig; 120 } 121 122 private int _inputCounter = 0; 123 private LabelFigure _inputCounterFigure; 124 125 private int _outputCounter = 0; 126 private LabelFigure _outputCounterFigure; 127 128 private MonitorIcon _icon; 129 130 private static final long serialVersionUID = 1L; 131}