001/* Computation of source nodes in a graph.
002
003 Copyright (c) 2003-2014 The University of Maryland. All rights reserved.
004 Permission is hereby granted, without written agreement and without
005 license or royalty fees, to use, copy, modify, and distribute this
006 software and its documentation for any purpose, provided that the above
007 copyright notice and the following two paragraphs appear in all copies
008 of this software.
009
010 IN NO EVENT SHALL THE UNIVERSITY OF MARYLAND BE LIABLE TO ANY PARTY
011 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
012 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
013 THE UNIVERSITY OF MARYLAND HAS BEEN ADVISED OF THE POSSIBILITY OF
014 SUCH DAMAGE.
015
016 THE UNIVERSITY OF MARYLAND SPECIFICALLY DISCLAIMS ANY WARRANTIES,
017 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
018 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
019 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
020 MARYLAND HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
021 ENHANCEMENTS, OR MODIFICATIONS.
022
023
024 */
025package ptolemy.graph.analysis.strategy;
026
027import java.util.ArrayList;
028import java.util.Collections;
029import java.util.Iterator;
030import java.util.List;
031
032import ptolemy.graph.DirectedGraph;
033import ptolemy.graph.Graph;
034import ptolemy.graph.Node;
035import ptolemy.graph.analysis.analyzer.SourceNodeAnalyzer;
036
037///////////////////////////////////////////////////////////////////
038//// SourceNodeAnalysis
039
040/**
041 Computation of source nodes in a graph.
042 The collection returned cannot be modified.
043 <p>
044 This analysis requires <em>O</em>(<em>N</em>) time, where <em>N</em> is the
045 number of nodes in the graph.
046 <p>
047 @see ptolemy.graph.analysis.SourceNodeAnalysis
048 @since Ptolemy II 4.0
049 @Pt.ProposedRating Red (shahrooz)
050 @Pt.AcceptedRating Red (ssb)
051 @author Ming Yung Ko, Shahrooz Shahparnia
052 @version $Id$
053 */
054public class SourceNodeStrategy extends CachedStrategy
055        implements SourceNodeAnalyzer {
056    /** Construct an instance of this strategy for a given graph.
057     *
058     *  @param graph The given graph.
059     */
060    public SourceNodeStrategy(Graph graph) {
061        super(graph);
062    }
063
064    ///////////////////////////////////////////////////////////////////
065    ////                         public methods                    ////
066
067    /** Compute the source nodes in the graph in the form of
068     *  a collection. Each element of the collection is a {@link Node}.
069     *
070     *  @return The source nodes.
071     */
072    @Override
073    public List nodes() {
074        return (List) _result();
075    }
076
077    /** Return a description of the analyzer.
078     *
079     *  @return Return a description of the analyzer..
080     */
081    @Override
082    public String toString() {
083        return "Ordinary Source-loop analyzer.\n";
084    }
085
086    /** Check compatibility of the class of graph. The given graph
087     *  must be an instance of DirectedGraph.
088     *
089     *  @return True if the given graph is of class DirectedGraph.
090     */
091    @Override
092    public boolean valid() {
093        return graph() instanceof DirectedGraph;
094    }
095
096    ///////////////////////////////////////////////////////////////////
097    ////                         protected methods                 ////
098
099    /** Compute the source nodes in the graph in the form of
100     *  a collection. Each element of the collection is a {@link Node}.
101     *
102     *  @return The source nodes.
103     */
104    @Override
105    protected Object _compute() {
106        ArrayList sourceNodes = new ArrayList();
107        Iterator nodes = graph().nodes().iterator();
108
109        while (nodes.hasNext()) {
110            Node node = (Node) nodes.next();
111
112            if (((DirectedGraph) graph()).inputEdgeCount(node) == 0) {
113                sourceNodes.add(node);
114            }
115        }
116
117        return sourceNodes;
118    }
119
120    /** Return the result of this analysis (collection of source nodes)
121     *  in a form that cannot be modified.
122     *
123     *  @return The analysis result in unmodifiable form.
124     */
125    protected Object _convertResult() {
126        return Collections.unmodifiableList((List) _result());
127    }
128}