001/* Computation of clusters 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;
026
027import java.util.Collection;
028
029import ptolemy.graph.Graph;
030import ptolemy.graph.Node;
031import ptolemy.graph.analysis.analyzer.Analyzer;
032import ptolemy.graph.analysis.analyzer.ClusterNodesTransformer;
033import ptolemy.graph.analysis.strategy.ClusterNodesTransformerStrategy;
034
035///////////////////////////////////////////////////////////////////
036//// ClusterNodesAnalysis
037
038/**
039 Given a collection of nodes in a graph, replace the subgraph induced by
040 the nodes with a single node N. Each edge that connects a node Z
041 outside the subgraph a node inside the subgraph is replaced by
042 an edge (with the same edge weight, if there is one) that connects
043 Z to N. Return the subgraph that is replaced;
044 that is, return the subgraph induced by the given collection of nodes.
045
046 @since Ptolemy II 4.0
047 @Pt.ProposedRating Red (shahrooz)
048 @Pt.AcceptedRating Red (ssb)
049 @version $Id$
050 @author Shahrooz Shahparnia based on a file by Shuvra S. Bhattacharyya and
051 Ming-Yung Ko
052 @see ptolemy.graph.Graph
053 */
054public class ClusterNodesAnalysis extends Analysis {
055    /** Construct an instance of this class for a given graph.
056     *  Given a collection of nodes in a graph, replace the subgraph induced by
057     *  the nodes with a single node N. Each edge that connects a node Z
058     *  outside the subgraph a node inside the subgraph is replaced by
059     *  an edge (with the same edge weight, if there is one) that connects
060     *  Z to N. Return the subgraph that is replaced;
061     *  that is, return the subgraph induced by the given collection of nodes.
062     *  @param graph The graph.
063     *  @param nodeCollection The collection of nodes.
064     *  @param superNode The node that replaces the subgraph.
065     */
066    public ClusterNodesAnalysis(Graph graph, Collection nodeCollection,
067            Node superNode) {
068        super(new ClusterNodesTransformerStrategy(graph, nodeCollection,
069                superNode));
070    }
071
072    /** Construct an instance of this class with a given analyzer.
073     *
074     *  @param analyzer The default Analyzer.
075     */
076    public ClusterNodesAnalysis(ClusterNodesTransformer analyzer) {
077        super(analyzer);
078        ;
079    }
080
081    ///////////////////////////////////////////////////////////////////
082    ////                         public methods                    ////
083
084    /** Return the clustered Graph.
085     *
086     *  @return Return the clustered Graph.
087     */
088    public Graph clusterNodes() {
089        return ((ClusterNodesTransformer) analyzer()).clusterNodes();
090    }
091
092    /** Return a description of the analysis and the associated analyzer.
093     *
094     *  @return A description of the analysis and the associated analyzer.
095     */
096    @Override
097    public String toString() {
098        return "Cluster node analysis using the following analyzer:\n"
099                + analyzer().toString();
100    }
101
102    /** Check if a given analyzer is compatible with this analysis.
103     *  In other words if it is possible to use it to compute the computation
104     *  associated with this analysis.
105     *
106     *  @param analyzer The given analyzer.
107     *  @return True if the given analyzer is valid for this analysis.
108     */
109    @Override
110    public boolean validAnalyzerInterface(Analyzer analyzer) {
111        return analyzer instanceof ClusterNodesTransformer;
112    }
113}