001/* This is a helper class for execution aspects.
002
003@Copyright (c) 2008-2018 The Regents of the University of California.
004All rights reserved.
005
006Permission is hereby granted, without written agreement and without
007license or royalty fees, to use, copy, modify, and distribute this
008software and its documentation for any purpose, provided that the
009above copyright notice and the following two paragraphs appear in all
010copies of this software.
011
012IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
013FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
014ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
015THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
016SUCH DAMAGE.
017
018THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
019INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
021PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023ENHANCEMENTS, OR MODIFICATIONS.
024
025                                                PT_COPYRIGHT_VERSION_2
026                                                COPYRIGHTENDKEY
027
028
029 */
030package ptolemy.actor;
031
032import java.util.ArrayList;
033import java.util.List;
034
035import ptolemy.actor.ExecutionAspectListener.ExecutionEventType;
036import ptolemy.actor.util.Time;
037import ptolemy.kernel.ComponentEntity;
038import ptolemy.kernel.CompositeEntity;
039import ptolemy.kernel.util.IllegalActionException;
040import ptolemy.kernel.util.NamedObj;
041
042/**
043  This is a helper class for execution aspects used in AtomicExecutionAspect and
044  CompositeExecutionAspect.
045  @author Patricia Derler
046  @version $Id$
047  @since Ptolemy II 10.0
048  @Pt.ProposedRating Red (derler)
049  @Pt.AcceptedRating Red (derler)
050 */
051public class ExecutionAspectHelper {
052
053    /** Execution aspects are decorators and this method recursively computes
054     *  all entities inside a given container that are decorated by execution aspects.
055     *  @param container The container.
056     *  @return All entities to decorate.
057     */
058    public static List<NamedObj> getEntitiesToDecorate(
059            CompositeEntity container) {
060        List<NamedObj> toDecorate = new ArrayList<NamedObj>();
061        for (Object entity : container.entityList(ComponentEntity.class)) {
062            if (!(entity instanceof ActorExecutionAspect)) {
063                toDecorate.add((NamedObj) entity);
064                if (entity instanceof CompositeEntity) {
065                    toDecorate.addAll(ExecutionAspectHelper
066                            .getEntitiesToDecorate((CompositeEntity) entity));
067                }
068            }
069        }
070        return toDecorate;
071    }
072
073    /** Schedule an actor for execution on an aspect and return the next time
074     *  this aspect has to perform an action. Derived classes
075     *  must implement this method to actually schedule actors, this
076     *  base class implementation just creates events for aspect
077     *  activity that is displayed in the plotter. This
078     *  base class implementation just creates events for aspect
079     *  activity that is displayed in the plotter.
080     *  @param aspect The aspect.
081     *  @param actor The actor to be scheduled.
082     *  @param environmentTime The current platform time.
083     *  @param deadline The deadline timestamp of the event to be scheduled.
084     *  This can be the same as the environmentTime.
085     *  @return Relative time when this aspect has to be executed
086     *    again to perform rescheduling actions.
087     *  @exception IllegalActionException Thrown if actor parameters such
088     *    as execution time or priority cannot be read.
089     */
090    public static Time schedule(ActorExecutionAspect aspect, NamedObj actor,
091            Time environmentTime, Time deadline) throws IllegalActionException {
092        Director director = ((CompositeActor) ((ComponentEntity) aspect)
093                .getContainer()).getDirector();
094        double executionTime = aspect.getExecutionTime(actor);
095        aspect.notifyExecutionListeners(((NamedObj) aspect),
096                environmentTime.getDoubleValue(), ExecutionEventType.START);
097        aspect.notifyExecutionListeners(((NamedObj) aspect),
098                environmentTime.getDoubleValue(), ExecutionEventType.STOP);
099        return aspect.schedule(actor, environmentTime, deadline,
100                new Time(director, executionTime));
101    }
102
103}