001/* A mirror transformations on graphs. 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 ptolemy.graph.Graph; 028import ptolemy.graph.analysis.analyzer.Analyzer; 029import ptolemy.graph.analysis.analyzer.MirrorTransformer; 030import ptolemy.graph.analysis.analyzer.Transformer; 031import ptolemy.graph.analysis.strategy.MirrorTransformerStrategy; 032 033/////////////////////////////////////////////////////////////////// 034//// MirrorTransformation 035 036/** 037 A mirror transformations on graphs. Creates a mirror of this graph 038 in the form of the type of the associated graph. The mirror and 039 original graphs are isomorphic(of same topology). However, node and 040 edge objects of the mirror are newly created and therefore not 041 "equal" to those of the original graph. 042 <p> 043 To relate nodes and edges from the original and the mirrored graph 044 the {@link #transformedVersionOf} and {@link #originalVersionOf} methods are 045 provided. 046 Labels can also be used to relate mirror and original 047 nodes(edges). 048 <p> 049 In the {@link #cloneWeight} method, users can also specify whether 050 to clone node and edge weights. For non cloneable weights a {@link 051 java.lang.CloneNotSupportedException} will be thrown by the virtual 052 machine. 053 054 @since Ptolemy II 4.0 055 @Pt.ProposedRating Red (shahrooz) 056 @Pt.AcceptedRating Red (ssb) 057 @author Shahrooz Shahparnia 058 @version $Id$ 059 */ 060public class MirrorTransformation extends Analysis { 061 /** Construct a transformation for a given graph with a default analyzer. 062 * The default constructor runs in O(N+E) in which N is the number of 063 * nodes in the graph and E is the number of edges in the graph. 064 * 065 * @param graph The given graph. 066 */ 067 public MirrorTransformation(Graph graph) { 068 super(new MirrorTransformerStrategy(graph)); 069 } 070 071 /** Construct a transformation for a given graph and a given analyzer. 072 * 073 * @param analyzer The default Analyzer. 074 */ 075 public MirrorTransformation(MirrorTransformer analyzer) { 076 super(analyzer); 077 } 078 079 /////////////////////////////////////////////////////////////////// 080 //// public methods //// 081 082 /** Changes the status of the graph returned by the {@link #mirror} method. 083 * If true, the weights will also be cloned in the next calls to the 084 * {@link #mirror} method. 085 * 086 * @param status If true, the weights will also be cloned. 087 */ 088 public void cloneWeight(boolean status) { 089 ((MirrorTransformer) analyzer()).cloneWeight(status); 090 } 091 092 /** Specify if this transformation has a mapping from the transformed 093 * version to the original version or not. 094 * 095 * @return True if the implementation of the transformer supports backward 096 * mapping. 097 */ 098 public boolean hasBackwardMapping() { 099 return ((MirrorTransformer) analyzer()).hasBackwardMapping(); 100 } 101 102 /** Specify if this transformation has a mapping from the original 103 * version to the transformed version or not. 104 * 105 * @return True if the implementation of the transformer supports forward 106 * mapping. 107 */ 108 public boolean hasForwardMapping() { 109 return ((MirrorTransformer) analyzer()).hasForwardMapping(); 110 } 111 112 /** Create a mirror of the graph associated with this analyzer with the 113 * same runtime class. 114 * 115 * @return The mirror graph. 116 */ 117 public Graph mirror() { 118 return ((MirrorTransformer) analyzer()).mirror(); 119 } 120 121 /** Return a mirror of this graph in the form of the argument graph type 122 * (i.e., the run-time type of the returned graph is that of the 123 * argument graph). 124 * <p> 125 * In this method, users can also specify whether to clone node and 126 * edge weights. 127 * 128 * @param graph The graph. 129 * @param cloneWeights True if the weights will also be cloned. 130 * @return The mirror graph. 131 */ 132 public Graph mirror(Graph graph, boolean cloneWeights) { 133 return ((MirrorTransformer) analyzer()).mirror(graph, cloneWeights); 134 } 135 136 /** Return the original version of given object in the transformed graph. 137 * 138 * @param transformedObject The given object in the transformed graph. 139 * @return Return the original version the given object. 140 */ 141 public Object originalVersionOf(Object transformedObject) { 142 return ((Transformer) analyzer()).originalVersionOf(transformedObject); 143 } 144 145 /** Return a description of the analysis and the associated analyzer. 146 * 147 * @return A description of the analysis and the associated analyzer. 148 */ 149 @Override 150 public String toString() { 151 return "Mirror transformation using the following analyzer:\n" 152 + analyzer().toString(); 153 } 154 155 /** Return the transformed version of a given object in the original graph. 156 * 157 * @param originalObject The given object in the original graph. 158 * @return Return the transformed version of the given object. 159 */ 160 public Object transformedVersionOf(Object originalObject) { 161 return ((Transformer) analyzer()).transformedVersionOf(originalObject); 162 } 163 164 /** Check if a given analyzer is compatible with this analysis. 165 * In other words if it is possible to use it to compute the computation 166 * associated with this analysis. 167 * 168 * @param analyzer The given analyzer. 169 * @return True if the given analyzer is valid for this analysis. 170 */ 171 @Override 172 public boolean validAnalyzerInterface(Analyzer analyzer) { 173 return analyzer instanceof MirrorTransformer; 174 } 175}