tools.hxx

00001 // tools.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2005, 2006 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 #ifndef VCSN_XML_TOOLS_HXX
00018 # define VCSN_XML_TOOLS_HXX
00019 
00030 namespace vcsn
00031 {
00032   namespace xml
00033   {
00034     /*----------------.
00035     | Converter tools |
00036     `----------------*/
00037 
00038     /*---------.
00039     | Automata |
00040     `---------*/
00041     namespace tools
00042     {
00043       // Add the label as a string attribute
00044       template <class S, class T, class U>
00045       void add_label(xercesc::DOMElement* elt,
00046                      const Element<S, T>&,
00047                      const U& series)
00048       {
00049         std::string label = get_label(series);
00050 
00051         if (label.size())
00052           elt->setAttribute(STR2XML("label"),
00053                             STR2XML(label.c_str()));
00054       }
00055 
00056       // Add the label as an xml node
00057       template <class S, class T, class U>
00058       void add_xml_label(xercesc::DOMDocument* doc,
00059                           xercesc::DOMElement* elt,
00060                           const Element<S, T>& a,
00061                           const U& series)
00062       {
00063         typedef Element<S,T> automaton_t;
00064         typedef typename
00065           rat::exp<typename automaton_t::monoid_elt_value_t,
00066           typename automaton_t::semiring_elt_value_t> krat_exp_impl_t;
00067         typedef Element<typename automaton_t::series_set_t, krat_exp_impl_t> krat_exp_t;
00068 
00069 
00070         std::string label = get_label(series);
00071         if (label.size())
00072         {
00073           krat_exp_t res (a.structure().series());
00074           parse(label, res);
00075           elt->appendChild(res.value().xml_tree(doc, "label"));
00076         }
00077       }
00078 
00079 
00080     /*----------------.
00081     | FMP Transducers |
00082     `----------------*/
00083 
00084       // Add the label as a string attribute
00085       template <class S, class T, class U, class M1, class M2>
00086       void add_label(xercesc::DOMElement* elt,
00087                      const FMPtype&,
00088                      const U& series)
00089       {
00090         std::string out;
00091         if (series.supp().size() > 1)
00092         {
00093           out = get_label(series);
00094           if (out.size())
00095             elt->setAttribute(STR2XML("label"),
00096                               STR2XML(out.c_str()));
00097         }
00098         else
00099         {
00100           std::string in_word =
00101             get_label((*(series.supp().begin())).first);
00102           std::string out_word =
00103             get_label((*(series.supp().begin())).second);
00104           std::string mult =
00105             get_label(series.get(*(series.supp().begin())));
00106           if (mult.size())
00107             out = mult;
00108           if (out != "" && out_word.size())
00109             out += " ";
00110           if (out_word.size())
00111             out += out_word;
00112 
00113           if (in_word.size())
00114             elt->setAttribute(STR2XML("in"),
00115                               STR2XML(in_word.c_str()));
00116           if (out.size())
00117             elt->setAttribute(STR2XML("out"),
00118                               STR2XML(out.c_str()));
00119         }
00120       }
00121 
00122       // Add the label as an xml node
00123       template <class S, class T, class U, class M1, class M2>
00124       void add_xml_label(xercesc::DOMDocument* doc,
00125                          xercesc::DOMElement* elt,
00126                          const FMPtype& a,
00127                          const U& series)
00128       {
00129         std::string out;
00130         if (series.supp().size() > 1)
00131         {
00132           out = get_label(series);
00133           if (out.size())
00134             elt->setAttribute(STR2XML("label"),
00135                               STR2XML(out.c_str()));
00136         }
00137         else
00138         {
00139           std::string in_word =
00140             get_label((*(series.supp().begin())).first);
00141           std::string out_word =
00142             get_label((*(series.supp().begin())).second);
00143           std::string mult =
00144             get_label(series.get(*(series.supp().begin())));
00145 
00146           if (in_word.size() && in_word != "1")
00147           {
00148             using namespace vcsn::r_automaton;
00149             automaton_t bin = make_automaton(
00150               a.structure().series().monoid().first_monoid().alphabet());
00151             rat_exp_t res_in(bin.structure().series());
00152             parse(in_word, res_in);
00153             elt->appendChild(res_in.value().xml_tree(doc, "in"));
00154           }
00155           if (out_word.size() && out_word != "1")
00156           {
00157             using namespace vcsn::r_automaton;
00158             automaton_t bout = make_automaton(
00159               a.structure().series().monoid().second_monoid().alphabet());
00160             rat_exp_t res_out(bout.structure().series());
00161             parse(out_word, res_out);
00162             xercesc::DOMElement* out_node =
00163               res_out.value().xml_tree(doc, "out");
00164             if (mult.size())
00165               out_node->setAttribute(STR2XML("weight"), STR2XML(mult.c_str()));
00166             elt->appendChild(out_node);
00167           }
00168         }
00169       }
00170 
00171 
00172     /*---------------------.
00173     | Transducers on P(B*) |
00174     `---------------------*/
00175 
00176       // Add the label as a string attribute
00177       template <class S, class T, class U>
00178       void add_label(xercesc::DOMElement* elt,
00179                      const Element<Transducer<S>, T>&,
00180                      const U& series)
00181       {
00182         std::string in = get_label(*(series.supp().begin()));
00183         std::string out = get_label((series.get(*(series.supp().begin()))));
00184 
00185         if (in.size() && in != "1")
00186           elt->setAttribute(STR2XML("in"),
00187                             STR2XML(in.c_str()));
00188         if (out.size() && out != "1")
00189           elt->setAttribute(STR2XML("out"),
00190                             STR2XML(out.c_str()));
00191       }
00192 
00193       // Add the label as an xml node
00194       template <class S, class T, class U>
00195       void add_xml_label(xercesc::DOMDocument* doc,
00196                          xercesc::DOMElement* elt,
00197                          const Element<Transducer<S>, T>& a,
00198                          const U& series)
00199       {
00200         std::string in = get_label(*(series.supp().begin()));
00201         std::string out = get_label((series.get(*(series.supp().begin()))));
00202 
00203         if (in.size() && in != "1")
00204         {
00205           using namespace vcsn::r_automaton;
00206           automaton_t bin = make_automaton(
00207             a.structure().series().monoid().alphabet());
00208           rat_exp_t res_in(bin.structure().series());
00209           parse(in, res_in);
00210           elt->appendChild(res_in.value().xml_tree(doc, "in"));
00211         }
00212         if (out.size() && out != "1")
00213         {
00214           using namespace vcsn::r_automaton;
00215           automaton_t bout = make_automaton(
00216             a.structure().series().semiring().monoid().alphabet());
00217           rat_exp_t res_out(bout.structure().series());
00218           parse(out, res_out);
00219           elt->appendChild(res_out.value().xml_tree(doc, "out"));
00220         }
00221       }
00222 
00223 
00224       template <class L>
00225       const std::string get_label(const L& l)
00226       {
00227         std::ostringstream os;
00228         os << l;
00229         return os.str();
00230       }
00231 
00232       inline const std::string get_label(const bool& l)
00233       {
00234         if (l)
00235           return "";
00236         return "0";
00237       }
00238 
00239 
00240       inline const std::string get_label(const std::pair<std::string, std::string>& l)
00241       {
00242         std::ostringstream os;
00243         os << "(" << l.first << ", " << l.second << ")";
00244         return os.str().c_str();
00245       }
00246 
00247       template <class S, class Series>
00248       const std::string get_label(const Element<Series,
00249                                   vcsn::algebra::polynom<S, bool> >& l)
00250       {
00251         std::ostringstream os;
00252         os << l;
00253         if (os.str() == "1")
00254           return "";
00255         return os.str();
00256       }
00257 
00258 
00259       template <class A>
00260       void create_alphabet(const A& alphabet, xercesc::DOMDocument* doc,
00261                            xercesc::DOMElement* root)
00262       {
00263         typedef typename A::const_iterator alphabet_iterator;
00264         for_each_letter(l, alphabet)
00265         {
00266           std::ostringstream letter;
00267           xercesc::DOMElement* gen =
00268             doc->createElement(STR2XML("generator"));
00269           letter << *l;
00270           gen->setAttribute(STR2XML("value"), STR2XML(letter.str().c_str()));
00271           root->appendChild(gen);
00272         }
00273       }
00274 
00275       template <class M>
00276       xercesc::DOMElement* create_monoid(const M& monoid,
00277                                          xercesc::DOMDocument* doc,
00278                                          xercesc::DOMElement* elt)
00279       {
00280         xercesc::DOMElement* m = doc->createElement(STR2XML("monoid"));
00281         m->setAttribute(STR2XML("type"), STR2XML(get_monoid_type(monoid)));
00282         m->setAttribute(STR2XML("generators"), STR2XML("letters"));
00283         elt->appendChild(m);
00284 
00285         return m;
00286       }
00287 
00288       template <class A, class S>
00289       xercesc::DOMElement* create_semiring(const A&,
00290                                            const S& semiring,
00291                                            xercesc::DOMDocument* doc,
00292                                            xercesc::DOMElement* elt)
00293       {
00294         typedef typename A::series_set_elt_t::semiring_elt_t::value_t value_t;
00295 
00296         xercesc::DOMElement* s = doc->createElement(STR2XML("semiring"));
00297 
00298         if (get_semiring_set(semiring, value_t()) != "ratSeries")
00299           s->setAttribute(STR2XML("operations"),
00300                           STR2XML(get_semiring_operations(semiring)));
00301         s->setAttribute(STR2XML("set"),
00302                         STR2XML(get_semiring_set(semiring, value_t())));
00303         elt->appendChild(s);
00304 
00305         return s;
00306       }
00307 
00308       template <class S, class T>
00309       xercesc::DOMElement*
00310       create_semiring(const Element<Transducer<S>, T>&,
00311                       const vcsn::algebra::NumericalSemiring& semiring,
00312                       xercesc::DOMDocument* doc,
00313                       xercesc::DOMElement* elt)
00314       {
00315         typedef typename
00316           Element<Transducer<S>, T>::series_set_elt_t::semiring_elt_t::semiring_elt_t::value_t value_t;
00317 
00318         xercesc::DOMElement* s = doc->createElement(STR2XML("semiring"));
00319         s->setAttribute(STR2XML("operations"), STR2XML("numerical"));
00320         s->setAttribute(STR2XML("set"),
00321                         STR2XML(get_semiring_set(semiring, value_t())));
00322         elt->appendChild(s);
00323 
00324         return s;
00325       }
00326 
00327 
00333 # define GET_SEMIRING_SET(T, Value)                     \
00334       template <class S>                                \
00335       const char* get_semiring_set(const S&, const T&)  \
00336       { return Value; }
00337 
00338       // Default.
00339       template <class S, class T>
00340       const char* get_semiring_set(const S&, const T&)
00341       { return "undefined"; }
00342 
00343       // Transducer on P(B*).
00344       template <class S, class U, class V>
00345       const char* get_semiring_set(const S&,
00346                                    const vcsn::rat::exp<U, V>&)
00347       { return "ratSeries"; }
00348 
00349       GET_SEMIRING_SET(bool, "B")
00350       GET_SEMIRING_SET(double, "R")
00351       GET_SEMIRING_SET(float, "R")
00352       GET_SEMIRING_SET(int, "Z")
00353 
00354       // Deals with the "operation" attribute of <semiring> tag.
00355 # define GET_SEMIRING_OPERATIONS(S, Value)              \
00356       template <>                                       \
00357       inline const char* get_semiring_operations<S>(const S&)   \
00358       { return Value; }
00359 
00360       // Default.
00361       template <class S>
00362       const char* get_semiring_operations(const S&)
00363       { return "undefined"; }
00364 
00365       GET_SEMIRING_OPERATIONS(vcsn::algebra::NumericalSemiring, "numerical")
00366       GET_SEMIRING_OPERATIONS(vcsn::z_max_plus_automaton::semiring_t, "tropicalMax")
00367       GET_SEMIRING_OPERATIONS(vcsn::z_min_plus_automaton::semiring_t, "tropicalMin")
00368 
00369 
00370       // Deal with the "type" attribute of <monoid> tag.
00371       // Default.
00372       template <class S>
00373       const char* get_monoid_type(const S&)
00374       { return "undefined"; }
00375 
00376       template <class S>
00377       const char* get_monoid_type(const vcsn::algebra::FreeMonoid<S>&)
00378       { return "free"; }
00379 
00380       template <class S1, class S2>
00381       const char*
00382       get_monoid_type(const vcsn::algebra::FreeMonoidProduct<S1, S2>&)
00383       { return "product"; }
00384 
00385 
00386 
00387 # define VCSN_XML_NO_TYPE ""
00388 # define VCSN_XML_SUM_TYPE "+"
00389 # define VCSN_XML_PRODUCT_TYPE "."
00390 
00391       template <class T>
00392       std::string
00393       get_rec_xml_series(xercesc::DOMNode* n, T& aut,
00394                          std::string op_type = VCSN_XML_NO_TYPE,
00395                          std::string res = "")
00396       {
00397         xercesc::DOMElement* element_n;
00398         std::string next_op_type;
00399 
00400         if (!n)
00401           return res;
00402         for (; n; n = n->getNextSibling())
00403           {
00404             if (n->getNodeType() == xercesc::DOMNode::ELEMENT_NODE)
00405               {
00406                 element_n = (static_cast<xercesc::DOMElement*>(n));
00407 
00408                 if (xml2str(n->getNodeName()) == "sum")
00409                   next_op_type = VCSN_XML_SUM_TYPE;
00410                 if (xml2str(n->getNodeName()) == "product")
00411                   next_op_type = VCSN_XML_PRODUCT_TYPE;
00412 
00413                 // Add weight (and opening brace if complex expression)
00414                 if (element_n->hasAttribute(STR2XML("weight")))
00415                   res += xml2str(element_n->getAttribute(STR2XML("weight")))
00416                     + " ";
00417 
00418                 // Add opening prace if attribute or complex starified/weighted
00419                 // expression or sum in product
00420                 if ((element_n->hasAttribute(STR2XML("parenthesis")) &&
00421                      xml2str(element_n->getAttribute(STR2XML("parenthesis")))
00422                      == "true")
00423                     || ((((element_n->hasAttribute(STR2XML("star")))
00424                           && (xml2str(element_n->getAttribute(STR2XML("star")))
00425                               == "true"))
00426                          ||(element_n->hasAttribute(STR2XML("weight"))))
00427                         && (xml2str(n->getNodeName()) != "word"))
00428                     || ((xml2str(n->getNodeName()) == "sum")
00429                         && (op_type == VCSN_XML_PRODUCT_TYPE)))
00430                   res += "(";
00431 
00432                 // Word, zero or identity
00433                 if (xml2str(n->getNodeName()) == "word")
00434                   res += xml2str(element_n->getAttribute(STR2XML("value")));
00435                 if (xml2str(n->getNodeName()) == "zero")
00436                   res += "1";
00437                 if (xml2str(n->getNodeName()) == "identity")
00438                   res += "0";
00439 
00440                 // Recursive call
00441                 if (n->hasChildNodes())
00442                   res += get_rec_xml_series(n->getFirstChild(),
00443                                             aut, next_op_type);
00444 
00445                 // Add closing brace if parenthesis attribute
00446                 if ((element_n->hasAttribute(STR2XML("parenthesis")) &&
00447                      xml2str(element_n->getAttribute(STR2XML("parenthesis")))
00448                      == "true")
00449                     // Or star/weight...
00450                     || ((((element_n->hasAttribute(STR2XML("star")))
00451                           && (xml2str(element_n->getAttribute(STR2XML("star")))
00452                               == "true"))
00453                          || (element_n->hasAttribute(STR2XML("weight"))))
00454                         // ...in a complex expression
00455                         && (xml2str(n->getNodeName()) != "word"))
00456                     // Or a sum in a product
00457                     || ((xml2str(n->getNodeName()) == "sum")
00458                         && (op_type == VCSN_XML_PRODUCT_TYPE))
00459                     // Or a product in a sum
00460                     || ((xml2str(n->getNodeName()) == "product")
00461                         && (op_type == VCSN_XML_SUM_TYPE)))
00462                   res += ")";
00463 
00464                 // Add star
00465                 if (element_n->hasAttribute(STR2XML("star"))
00466                     && (xml2str(element_n->getAttribute(STR2XML("star")))
00467                         == "true"))
00468                   res += "*";
00469 
00470                 // Add operator
00471                 if ((n->getNextSibling())
00472                     && (n->getNextSibling()->getNextSibling()))
00473                   res += op_type;
00474               }
00475           }
00476         return res;
00477       }
00478 
00479 
00491       template <class T>
00492       typename T::series_set_elt_t
00493       get_series(xercesc::DOMElement* node, T& aut)
00494       {
00495         typedef typename
00496           rat::exp<typename T::monoid_elt_value_t,
00497           typename T::semiring_elt_value_t> krat_exp_impl_t;
00498         typedef Element<typename T::series_set_t, krat_exp_impl_t> krat_exp_t;
00499         std::string str_res;
00500         krat_exp_t res (aut.structure().series());
00501         xercesc::DOMNode* n = node->getFirstChild();
00502         if (n && n->getNextSibling()
00503             && (xml2str(n->getNextSibling()->getNodeName()) == "label"))
00504           str_res = get_rec_xml_series(n, aut);
00505         if (str_res == "")
00506           {
00507             if (xml2str(node->getAttribute(STR2XML("label"))) == "")
00508               return
00509                 vcsn::algebra::identity_as<typename T::series_set_elt_t::value_t>
00510 		::of(aut.structure().series());
00511             else
00512               parse(xml2str(node->getAttribute(STR2XML("label"))), res);
00513           }
00514         else
00515           parse(str_res, res);
00516 
00517         return res;
00518       }
00519 
00520 
00521       TParmFMP
00522       typename FMPtype::series_set_elt_t
00523       get_series(xercesc::DOMElement* node, FMPtype& a)
00524       {
00525         typename FMPtype::series_set_elt_t res(a.structure().series());
00526 
00527         parse_label(node, a,
00528                     a.structure().series().monoid().first_monoid().alphabet(),
00529                     a.structure().series().monoid().second_monoid().alphabet(),
00530                     res);
00531 
00532         return res;
00533       }
00534 
00535 
00536       TParm
00537       typename TRANStype::series_set_elt_t
00538       get_series(xercesc::DOMElement* node,
00539                  TRANStype& a)
00540       {
00541         typename TRANStype::series_set_elt_t res(a.structure().series());
00542 
00543         parse_label(node, a,
00544                     a.structure().series().monoid().alphabet(),
00545                     a.structure().series().semiring().monoid().alphabet(),
00546                     res);
00547 
00548         return res;
00549       }
00550 
00551 
00552       template <class S, class T, class E1, class E2, class R>
00553       void assoc_exp(TRANStype& a, E1& i_exp, E2& o_exp, R& res,
00554                      bool i_res, bool o_res)
00555       {
00556         typedef typename TRANStype::monoid_elt_t::value_t md_value_t;
00557         typedef typename TRANStype::semiring_elt_t::value_t sg_value_t;
00558         typename TRANStype::monoid_elt_t m(a.structure().series().monoid());
00559 
00560         if (! i_res && i_exp.supp().size())
00561           m = *(i_exp.supp().begin());
00562         else
00563           m = vcsn::algebra::identity_as<md_value_t>
00564 	    ::of(a.structure().series().monoid());
00565 
00566         if (! o_res && ! o_exp.supp().size())
00567           res.assoc(m,
00568                     vcsn::algebra::identity_as<sg_value_t>
00569                     ::of(a.structure().series().semiring()));
00570         else
00571           res.assoc(m, o_exp);
00572       }
00573 
00574 
00575       template <class S, class T, class M1, class M2,
00576                 class E1, class E2, class R>
00577       void assoc_exp(FMPtype& a, E1& i_exp, E2& o_exp, R& res,
00578                      bool i_res, bool o_res)
00579       {
00580         typename FMPtype::monoid_elt_t m(a.structure().series().monoid());
00581         typename FMPtype::monoid_elt_t::first_monoid_elt_value_t m1("");
00582         typename FMPtype::monoid_elt_t::second_monoid_elt_value_t m2("");
00583         typename FMPtype::semiring_elt_value_t sem = 1;
00584         typedef typename FMPtype::monoid_elt_t::first_monoid_elt_t::value_t
00585           md_value_t;
00586         typedef typename FMPtype::semiring_elt_value_t sg_value_t;
00587 
00588         if (! i_res && i_exp.supp().size())
00589           m1 = *(i_exp.supp().begin());
00590         else
00591           m1 = vcsn::algebra::identity_as<md_value_t>
00592 	    ::of(a.structure().series().monoid().first_monoid()).value();
00593 
00594         if (! o_res && o_exp.supp().size())
00595         {
00596           m2 = *(o_exp.supp().begin());
00597           sem = o_exp.get(m2);
00598         }
00599         else
00600         {
00601           m2 = vcsn::algebra::identity_as<md_value_t>
00602 	    ::of(a.structure().series().monoid().second_monoid()).value();
00603           sem = vcsn::algebra::identity_as<sg_value_t>
00604 	    ::of(a.structure().series().semiring()).value();
00605         }
00606         m = std::make_pair(m1, m2);
00607         res.assoc(m, sem);
00608       }
00609 
00610 
00611       template <class A, class A1, class A2, class S>
00612       void parse_label(xercesc::DOMElement* node, A& a,
00613                        const A1& alphabet1, const A2& alphabet2, S& res)
00614       {
00615         using namespace vcsn::r_automaton;
00616         automaton_t bin = make_automaton(alphabet1);
00617         automaton_t bout = make_automaton(alphabet2);
00618         rat_exp_t i_exp(bin.structure().series());
00619         rat_exp_t o_exp(bout.structure().series());
00620         std::string in, out;
00621 
00622         std::pair<bool, std::string> i_res;
00623         std::pair<bool, std::string> o_res;
00624 
00625         xercesc::DOMNode* n = node->getFirstChild();
00626 
00627         if (n)
00628           for (; n; n = n->getNextSibling())
00629           {
00630             if (xml2str(n->getNodeName()) == "in" && n->getFirstChild())
00631             {
00632               in = get_rec_xml_series(n->getFirstChild(), a);
00633               i_res = parse(in, i_exp);
00634             }
00635             if (xml2str(n->getNodeName()) == "out" && n->getFirstChild())
00636             {
00637               out = get_rec_xml_series(n->getFirstChild(), a);
00638               o_res = parse(out, o_exp);
00639             }
00640           }
00641         // No expression tag
00642         else
00643         {
00644           if (node->hasAttribute(STR2XML("label")))
00645           {
00646             std::string label = xml2str(node->getAttribute(STR2XML("label")));
00647             unsigned int pos = label.find("|");
00648             if (pos != std::string::npos)
00649             {
00650               in = label.substr(0, pos);
00651               out = label.substr(pos + 1);
00652               i_res = parse(in, i_exp);
00653               o_res = parse(out, o_exp);
00654             }
00655             else
00656               i_res = parse(label, i_exp);
00657             if (node->hasAttribute(STR2XML("weight")))
00658               o_res = parse(xml2str(node->getAttribute(STR2XML("weight"))),
00659                             o_exp);
00660           }
00661           // No expression tag, no label attribute.
00662           else
00663           {
00664             if (node->hasAttribute(STR2XML("in")))
00665               i_res = parse(xml2str(node->getAttribute(STR2XML("in"))),
00666                             i_exp);
00667             if (node->hasAttribute(STR2XML("out")))
00668               o_res = parse(xml2str(node->getAttribute(STR2XML("out"))),
00669                             o_exp);
00670           }
00671         }
00672         assoc_exp(a, i_exp, o_exp, res, i_res.first, o_res.first);
00673       }
00674 
00675 
00686       template <class U, class V>
00687       void insert_letter(Element<vcsn::algebra::AlphabetSet<U>, V>&,
00688                          const std::string&)
00689       {
00690         FAIL("No available implementation for this type of letter");
00691       }
00692 
00693       template <class V>
00694       void insert_letter(Element<vcsn::algebra::AlphabetSet<char>, V>& a,
00695                          const std::string& str)
00696       {
00697         a.insert(str[0]);
00698       }
00699 
00700 
00712       template <class T, class U>
00713       void ensure_semiring_type(const xercesc::DOMElement* node,
00714                                 const T&, const U& param)
00715       {
00716         typedef typename T::series_set_elt_t::semiring_elt_t::value_t value_t;
00717 
00718         std::string set(tools::get_semiring_set(param, value_t()));
00719         std::string set_ref;
00720 
00721         if (node && node->hasAttribute(STR2XML("set")))
00722           set_ref = xml2str(node->getAttribute(STR2XML("set")));
00723         else
00724           set_ref = "B";
00725         if (set_ref != set)
00726           FAIL("Bad semiring");
00727       }
00728 
00729 
00730       template <class S, class T>
00731       void ensure_semiring_type(const xercesc::DOMElement* node,
00732                                 const Element<Transducer<S>, T>&,
00733                                 const typename Element<Transducer<S>, T>
00734                                 ::semiring_t::semiring_t& param)
00735       {
00736         typedef Element<Transducer<S>, T> trans_t;
00737         typedef typename
00738           trans_t::series_set_elt_t::semiring_elt_t::semiring_elt_t::value_t
00739           value_t;
00740 
00741         std::string set(tools::get_semiring_set(param, value_t()));
00742         std::string set_ref;
00743 
00744         if (node && node->hasAttribute(STR2XML("set")))
00745           set_ref = xml2str(node->getAttribute(STR2XML("set")));
00746         else
00747           set_ref = "B";
00748         if (set_ref != set)
00749           FAIL("Bad semiring");
00750       }
00751 
00752 
00753       template <class S, class T, class U>
00754       void ensure_semiring_type(const xercesc::DOMElement* node,
00755                                 const Element<Transducer<S>, T>&,
00756                                 const typename Element<Transducer<S>, T>
00757                                 ::semiring_t& param)
00758       {
00759         typedef Element<Transducer<S>, T> trans_t;
00760         typedef typename
00761           trans_t::series_set_elt_t::semiring_elt_t::value_t value_t;
00762 
00763         std::string set(tools::get_semiring_set(param, value_t()));
00764         std::string set_ref;
00765 
00766         if (node && node->hasAttribute(STR2XML("set")))
00767           set_ref = xml2str(node->getAttribute(STR2XML("set")));
00768         else
00769           set_ref = "ratSeries";
00770         if (set_ref != set)
00771           FAIL("Bad semiring");
00772       }
00773 
00774 
00784       template <class U>
00785       void ensure_monoid_type(const xercesc::DOMElement* node, const U& param)
00786       {
00787         std::string monoid_type = tools::get_monoid_type(param);
00788         std::string monoid_type_ref;
00789 
00790         if (! node || ! node->hasAttribute(STR2XML("type")))
00791           monoid_type_ref = "free";
00792         else
00793           monoid_type_ref = xml2str(node->getAttribute(STR2XML("type")));
00794         if (monoid_type_ref != monoid_type)
00795           FAIL("Bad monoid type");
00796       }
00797 
00798 
00799       template <class S, class T, class U, class M1, class M2>
00800       void ensure_monoid_type(const xercesc::DOMElement* node,
00801                               const FMPtype& param)
00802       {
00803         std::string monoid_type = tools::get_monoid_type(param);
00804         std::string monoid_type_ref;
00805 
00806         if (! node || ! node->hasAttribute(STR2XML("type")))
00807           monoid_type_ref = "product";
00808         else
00809           monoid_type_ref = xml2str(node->getAttribute(STR2XML("type")));
00810         if (monoid_type_ref != monoid_type)
00811           FAIL("Bad monoid type");
00812       }
00813 
00814 
00815       template <class OStream>
00816       void print_transition(const xercesc::DOMElement* n,
00817                             OStream& os,
00818                             std::string& spacing)
00819       {
00820         os << spacing << "<" << xml2str(n->getNodeName());
00821         if (n->hasAttribute(STR2XML("src")))
00822           os << " src=\""
00823              << xml2str(n->getAttribute(STR2XML("src"))) << "\"";
00824         if (n->hasAttribute(STR2XML("dst")))
00825           os << " dst=\""
00826              << xml2str(n->getAttribute(STR2XML("dst"))) << "\"";
00827         if (n->hasAttribute(STR2XML("label")))
00828           os << " label=\""
00829              << xml2str(n->getAttribute(STR2XML("label"))) << "\"";
00830         if (n->hasAttribute(STR2XML("weight")))
00831           os << " weight=\""
00832              << xml2str(n->getAttribute(STR2XML("weight"))) << "\"";
00833         if (n->hasAttribute(STR2XML("in")))
00834           os << " in=\""
00835              << xml2str(n->getAttribute(STR2XML("in"))) << "\"";
00836         if (n->hasAttribute(STR2XML("out")))
00837           os << " out=\""
00838              << xml2str(n->getAttribute(STR2XML("out"))) << "\"";
00839       }
00840 
00841 
00842       template <class OStream>
00843       void print_tree(const xercesc::DOMElement* node,
00844                       OStream& os,
00845                       std::string spacing)
00846       {
00847         using namespace xercesc;
00848         unsigned i;
00849         DOMNamedNodeMap* m;
00850 
00851         if (xml2str(node->getNodeName()) == "transition")
00852           print_transition(static_cast<const DOMElement*>(node), os, spacing);
00853         else
00854         {
00855           os << spacing << "<" << xml2str(node->getNodeName());
00856           for (m = node->getAttributes(), i = 0;
00857                i < m->getLength(); ++i)
00858           {
00859             os << " " << xml2str(m->item(i)->getNodeName())
00860                << "=\"" << xml2str(m->item(i)->getNodeValue())
00861                << "\"";
00862           }
00863         }
00864         if (node->hasChildNodes())
00865           os << ">";
00866         else
00867           os << "/>";
00868         os << std::endl;
00869         for (DOMNode* n = node->getFirstChild(); n; n = n->getNextSibling())
00870           if (n->getNodeType() == DOMNode::ELEMENT_NODE)
00871             print_tree(static_cast<const DOMElement*>(n), os, spacing + "  ");
00872         if (node->hasChildNodes())
00873           os << spacing << "</" << xml2str(node->getNodeName()) << ">"
00874              << std::endl;
00875       }
00876 
00877 
00887       template <class OStream>
00888       void print_document(xercesc::DOMElement* node, OStream& os)
00889       {
00890         node->setAttribute(STR2XML("xmlns"),
00891                            STR2XML("http://vaucanson.lrde.epita.fr"));
00892         print_tree(node, os, "");
00893       }
00894 
00895 
00896     } // ! tools
00897 
00898   } // ! xml
00899 
00900 } // ! vcsn
00901 
00902 
00903 # undef GET_FREEMONOID_TYPE
00904 # undef GET_SEMIRING_OPERATIONS
00905 # undef GET_SEMIRING_SET
00906 # undef AUTtype
00907 # undef TRANStype
00908 # undef FMPtype
00909 # undef TParm
00910 # undef TParmFMP
00911 
00912 #endif // ! VCSN_XML_TOOLS_HXX

Generated on Fri Jul 28 12:18:54 2006 for Vaucanson by  doxygen 1.4.6