WeightGen.hpp

Go to the documentation of this file.
00001 #ifndef wali_nwa_WeightGen_GUARD
00002 #define wali_nwa_WeightGen_GUARD 1
00003 
00004 /**
00005  * @author Amanda Burton
00006  */
00007 
00008 #include "opennwa/ClientInfo.hpp"
00009 #include "wali/Reach.hpp"
00010 #include "wali/ShortestPathSemiring.hpp"
00011 
00012 
00013 namespace opennwa
00014 {
00015   using wali::Key; // TODO remove
00016   using wali::sem_elem_t;
00017   
00018     /**
00019      *
00020      * This class is used in NWA::NWAtoPDS and NWA::NWAtoBackwardsPDS.  It is used to
00021      * compute the weights for the rules added to the WPDS and must be extended to 
00022      * incorporate the desired semiring in order to use either of these methods.
00023      *  
00024      */
00025     //TODO: we may want to split getWeight into getCallWeight/getInternalWeight/getReturnWeight
00026     class WeightGen
00027     {
00028     public:
00029       //The kinds of edges that need to be considered.
00030       //  INTRA:  corresponds to an Internal transition in the NWA
00031       //  CALL_TO_ENTRY:  corresponds to a Call transition in the NWA
00032       //  EXIT_TO_RET:  corresponds to the exit-to-return part of a Return transition in the NWA
00033       //  CALL_TO_RET:  corresponds to the call-to-return part of a Return transition in the NWA
00034       enum Kind {INTRA, CALL_TO_ENTRY, EXIT_TO_RET, CALL_TO_RET}; 
00035       typedef ref_ptr<ClientInfo> ClientInfoRefPtr;
00036       
00037       //
00038       // Methods
00039       //
00040       
00041     public:
00042       
00043       //Constructors and Destructor
00044       WeightGen( ) { }
00045       virtual ~WeightGen() {}
00046         
00047       /**
00048        *  
00049        * @brief access the One of the desired semiring
00050        *
00051        * This method returns the One of the desired semiring.  
00052        *
00053        * @return the One of the desired semiring
00054        *
00055        */
00056       virtual sem_elem_t getOne( ) const  = 0;
00057 
00058       /**
00059        *
00060        * @brief computes the weight(in the desired semiring) for a rule of the WPDS from 
00061        *        the given edge
00062        *
00063        * This method computes the weight(in the desired semiring) for a WPDS rule from 
00064        * the (kind) edge between 'src' and 'tgt' labeled with symbol 'sym'.
00065        * Note: The kind will never be CALL_TO_RET, this case is never needed.
00066        *
00067        * @param - src: the source of the edge
00068        * @param - srcInfo: the clientInfo associated with src
00069        * @param - sym: the symbol labeling the edge
00070        * @param - kind: the variety of edge
00071        * @param - tgt: the target of the edge
00072        * @param - tgtInfo: the clientInfo associated with tgt
00073        * @return the weight to put on the rule corresponding to the given edge
00074        *         
00075        */
00076       virtual sem_elem_t getWeight( Key src, ClientInfoRefPtr srcInfo, Key sym, Kind kind, Key tgt, ClientInfoRefPtr tgtInfo ) const
00077       {
00078         (void) src;
00079         (void) srcInfo;
00080         (void) sym;
00081         (void) kind;
00082         (void) tgt;
00083         (void) tgtInfo;
00084         return getOne();
00085       }
00086         
00087       /** 
00088        * 
00089        * @brief computes the weight(in the desired semiring) for the return rule of the 
00090        *        WPDS associated with the given exit
00091        *
00092        * This method computes the weight(in the desired semiring) for the return rule of
00093        * the WPDS corresponding to the exit 'src'.
00094        * Note: This value is generally the same as getOne().
00095        * 
00096        * @param - src: the source of the exit edge
00097        * @param - srcInfo: the clientInfo associated with src
00098        * @return the weight to put on the return rule corresponding to the given exit 
00099        *
00100        */
00101       virtual sem_elem_t getExitWeight( Key src, ClientInfoRefPtr srcInfo ) const
00102       {
00103         (void) src;
00104         (void) srcInfo;
00105         return getOne();
00106       }
00107 
00108       /**
00109        *
00110        * @brief computes the weight(in the desired semiring) for a rule when the symbol
00111        *        labeling the corresponding edge in the NWA is the wild symbol
00112        *
00113        * This method computes the weight(in the desired semiring) for a WPDS rule from 
00114        * the edge between 'src' and 'tgt' labeled with the wild symbol.
00115        *
00116        * @param - src: the source of the edge
00117        * @param - srcInfo: the clientInfo associated with src
00118        * @param - tgt: the target of the edge
00119        * @param - tgtInfo: the clientInfo associated with tgt
00120        * @return the weight to put on the rule corresponding to the given edge
00121        *
00122        */
00123       virtual sem_elem_t getWildWeight( Key src, ClientInfoRefPtr srcInfo, Key tgt, ClientInfoRefPtr tgtInfo ) const
00124       {
00125         (void) src;
00126         (void) srcInfo;
00127         (void) tgt;
00128         (void) tgtInfo;
00129         return getOne();
00130       }
00131     };
00132 
00133 
00134 
00135     class ReachGen : public WeightGen
00136     {
00137     public:
00138       //
00139       // Methods
00140       //
00141           
00142     public:
00143       sem_elem_t getOne( ) const
00144       {
00145         static const Reach r(true);
00146         return r.one();
00147       }
00148     };
00149   
00150 
00151     class ShortestPathGen : public WeightGen
00152     {
00153     public:
00154       sem_elem_t getOne( ) const
00155       {
00156         static const wali::ShortestPathSemiring r;
00157         return r.one();
00158       }
00159 
00160 
00161       sem_elem_t getUnitWeight() const
00162       {
00163         static const sem_elem_t r = new wali::ShortestPathSemiring(1);
00164         return r;
00165       }
00166 
00167       virtual sem_elem_t getWeight( Key src, ClientInfoRefPtr srcInfo, Key sym, Kind kind, Key tgt, ClientInfoRefPtr tgtInfo ) const
00168       {
00169         (void) src;
00170         (void) srcInfo;
00171         (void) sym;
00172         (void) kind;
00173         (void) tgt;
00174         (void) tgtInfo;
00175         return getUnitWeight();
00176       }
00177         
00178       virtual sem_elem_t getExitWeight( Key src, ClientInfoRefPtr srcInfo ) const
00179       {
00180         (void) src;
00181         (void) srcInfo;
00182         return getUnitWeight();
00183       }
00184 
00185       virtual sem_elem_t getWildWeight( Key src, ClientInfoRefPtr srcInfo, Key tgt, ClientInfoRefPtr tgtInfo ) const
00186       {
00187         (void) src;
00188         (void) srcInfo;
00189         (void) tgt;
00190         (void) tgtInfo;
00191         return getUnitWeight();
00192       }
00193       
00194     };
00195 
00196 
00197     class ShortestWordGen : public WeightGen
00198     {
00199     public:
00200       // Semiring 1 is length 0
00201       sem_elem_t getOne( ) const
00202       {
00203         static const wali::ShortestPathSemiring r;
00204         return r.one();
00205       }
00206 
00207 
00208       sem_elem_t getUnitWeight() const
00209       {
00210         static const sem_elem_t r = new wali::ShortestPathSemiring(1);
00211         return r;
00212       }
00213 
00214       virtual sem_elem_t getWeight( Key src, ClientInfoRefPtr srcInfo, Key sym, Kind kind, Key tgt, ClientInfoRefPtr tgtInfo ) const
00215       {
00216         (void) src;
00217         (void) srcInfo;
00218         (void) kind;
00219         (void) tgt;
00220         (void) tgtInfo;
00221         if (sym != EPSILON) {
00222           return getUnitWeight();
00223         }
00224         else {
00225           return getOne();
00226         }
00227       }
00228         
00229       virtual sem_elem_t getExitWeight( Key src, ClientInfoRefPtr srcInfo ) const
00230       {
00231         (void) src;
00232         (void) srcInfo;
00233         return getUnitWeight();
00234       }
00235 
00236       virtual sem_elem_t getWildWeight( Key src, ClientInfoRefPtr srcInfo, Key tgt, ClientInfoRefPtr tgtInfo ) const
00237       {
00238         (void) src;
00239         (void) srcInfo;
00240         (void) tgt;
00241         (void) tgtInfo;
00242         return getUnitWeight();
00243       }
00244       
00245     };
00246     
00247 }
00248 
00249 // Yo, Emacs!
00250 // Local Variables:
00251 //   c-file-style: "ellemtel"
00252 //   c-basic-offset: 2
00253 // End:
00254 
00255 #endif