001/* Computation of self-loops 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.Edge; 033import ptolemy.graph.Graph; 034import ptolemy.graph.analysis.analyzer.SelfLoopAnalyzer; 035 036/////////////////////////////////////////////////////////////////// 037//// SelfLoopAnalyzer 038 039/** 040 Computation of self-loops in a graph. 041 The returned collection cannot be modified. 042 <p> 043 This analysis requires <em>O</em>(<em>E</em>) time, where <em>E</em> is the 044 number of edges in the graph. 045 <p> 046 @see ptolemy.graph.analysis.SelfLoopAnalysis 047 @since Ptolemy II 4.0 048 @Pt.ProposedRating Red (ssb) 049 @Pt.AcceptedRating Red (ssb) 050 @author Shuvra S. Bhattacharyya, Shahrooz Shahparnia 051 @version $Id$ 052 */ 053public class SelfLoopStrategy extends CachedStrategy 054 implements SelfLoopAnalyzer { 055 /** Construct an instance of this strategy for a given graph. 056 * 057 * @param graph The given graph. 058 */ 059 public SelfLoopStrategy(Graph graph) { 060 super(graph); 061 } 062 063 /////////////////////////////////////////////////////////////////// 064 //// public methods //// 065 066 /** Compute the self-loop edges in the graph in the form of 067 * a collection. Each element of the collection is an {@link Edge}. 068 * @return The self-loop edges. 069 */ 070 @Override 071 public List edges() { 072 return (List) _result(); 073 } 074 075 /** Return a description of the analyzer. 076 * 077 * @return Return a description of the analyzer.. 078 */ 079 @Override 080 public String toString() { 081 return "Ordinary Self-loop analyzer.\n"; 082 } 083 084 /** Check for validity of this strategy. 085 * 086 * @return True since this strategy is always valid. 087 */ 088 @Override 089 public boolean valid() { 090 return true; 091 } 092 093 /////////////////////////////////////////////////////////////////// 094 //// protected methods //// 095 096 /** Compute the self-loop edges in the graph in the form of 097 * a collection. Each element of the collection is an {@link Edge}. 098 * 099 * @return The self-loop edges. 100 */ 101 @Override 102 protected Object _compute() { 103 ArrayList selfLoopEdges = new ArrayList(); 104 Iterator edges = graph().edges().iterator(); 105 106 while (edges.hasNext()) { 107 Edge edge = (Edge) edges.next(); 108 109 if (edge.isSelfLoop()) { 110 selfLoopEdges.add(edge); 111 } 112 } 113 114 return selfLoopEdges; 115 } 116 117 /** Return the result of this analysis (collection of self-loop edges) 118 * in a form that cannot be modified. 119 * 120 * @return The analysis result in unmodifiable form. 121 */ 122 protected Object _convertResult() { 123 return Collections.unmodifiableList((List) _result()); 124 } 125}