001/* A scheduler for fixed point directors.
002
003 Copyright (c) 2006-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.actor.sched;
029
030import java.util.List;
031
032import ptolemy.actor.Actor;
033import ptolemy.actor.CompositeActor;
034import ptolemy.actor.util.CausalityInterfaceForComposites;
035import ptolemy.kernel.util.IllegalActionException;
036import ptolemy.kernel.util.NameDuplicationException;
037
038///////////////////////////////////////////////////////////////////
039//// FixedPointScheduler
040
041/**
042 A scheduler for the FixedPointDirector.  This scheduler constructs
043 a static schedule for a model by performing a topological sort on
044 actors. Currently, in this class, each actor appears only once, but
045 a more sophisticated scheduler may mention an actor more
046 than once if the dependencies require it.
047
048 @author Haiyang Zheng and Edward A. Lee
049 @version $Id$
050 @since Ptolemy II 5.2
051 @Pt.ProposedRating Yellow (hyzheng)
052 @Pt.AcceptedRating Red (reviewModerator)
053 */
054public class FixedPointScheduler extends Scheduler {
055    /** Construct a scheduler in the given container with the given name.
056     *  The container argument must not be null, or a
057     *  NullPointerException will be thrown.
058     *  If the name argument is null, then the name is set to the empty string.
059     *  Increment the version of the workspace.
060     *  @param container The container.
061     *  @param name The name of this scheduler.
062     *  @exception IllegalActionException If the scheduler is not of an
063     *   acceptable class for the container, or if the name contains a period.
064     *  @exception NameDuplicationException If the name coincides with
065     *   an attribute already in the container.
066     */
067    public FixedPointScheduler(StaticSchedulingDirector container, String name)
068            throws IllegalActionException, NameDuplicationException {
069        super(container, name);
070    }
071
072    ///////////////////////////////////////////////////////////////////
073    ////                         protected methods                 ////
074
075    /** Return the schedule. This method attempts to construct a schedule based
076     *  on a topological sort of the graph (which uses causality interfaces).
077     *  If there are cycles, no such sort is possible, and this method simply
078     *  returns a schedule that lists the actors in their natural order in the
079     *  container, which is the order in which they were created (unless that
080     *  order has been modified through "bring to front" or "send to back").
081     *  This method should not be called directly, but rather the getSchedule()
082     *  method (which is defined in the superclass) will call it when the
083     *  schedule is invalid.  This method is not synchronized on the workspace.
084     *  @return A schedule.
085     */
086    @Override
087    protected Schedule _getSchedule() {
088        StaticSchedulingDirector director = (StaticSchedulingDirector) getContainer();
089        if (director == null) {
090            throw new NotSchedulableException(this, "No director.  ");
091        }
092        CompositeActor compositeActor = (CompositeActor) director
093                .getContainer();
094        if (compositeActor == null) {
095            throw new NotSchedulableException(this, "No container.");
096        }
097        CausalityInterfaceForComposites causality = (CausalityInterfaceForComposites) compositeActor
098                .getCausalityInterface();
099        List<Actor> sortedActors;
100        try {
101            sortedActors = causality.topologicalSort();
102        } catch (IllegalActionException ex) {
103            sortedActors = compositeActor.deepEntityList();
104        }
105        Schedule schedule = new Schedule();
106        if (_debugging) {
107            _debug("## Schedule generated:");
108        }
109        for (Actor actor : sortedActors) {
110            Firing firing = new Firing(actor);
111            schedule.add(firing);
112            if (_debugging) {
113                _debug(" - " + actor.getFullName());
114            }
115        }
116        if (_debugging) {
117            _debug("## End of schedule.");
118        }
119        setValid(true);
120        return schedule;
121    }
122}