00001 #ifndef FI_TREE_CONVERTER_H
00002 #define FI_TREE_CONVERTER_H
00003
00004
00005
00006
00007 #include <utility>
00008 #include <map>
00009 #include <list>
00010 #include <vector>
00011 #include <boost/shared_ptr.hpp>
00012 #include <boost/spirit/core.hpp>
00013 #include <boost/spirit/tree/parse_tree.hpp>
00014
00015
00016 enum FILabeledType{
00017 filtNodes,
00018 filtValue
00019 };
00020
00021
00022
00023
00024 template <class TL, class TR>
00025 struct FIConvertationTraits;
00026
00027
00028 template <class TTraits>
00029 class FITreeDefs
00030 {
00031 public:
00032
00033 typedef typename TTraits::TKey TKey;
00034
00035 typedef typename TTraits::TString TString;
00036
00037
00038 typedef typename TTraits::TInNode TInNode;
00039
00040 typedef typename TTraits::TInValue TInValue;
00041
00042 typedef typename TTraits::TInAttribute TInAttribute;
00043
00044 typedef typename TTraits::TInIterator TInIterator;
00045
00046
00047 static TKey GetKey(const TInAttribute& attr){
00048 return TTraits::GetKey(attr);
00049 }
00050
00051 static TInValue GetValue(const TInAttribute& attr){
00052 return TTraits::GetValue(attr);
00053 }
00054
00055 static bool IsString(const TInValue& value){
00056 return TTraits::IsString(value);
00057 }
00058
00059 static TString GetString(const TInValue& value){
00060 return TTraits::GetString(value);
00061 }
00062
00063 static TInNode GetNode(const TInValue& value){
00064 return TTraits::GetNode(value);
00065 }
00066
00067 static TInIterator GetBegin(const TInNode& node){
00068 return TTraits::GetBegin(node);
00069 }
00070
00071 static TInIterator GetEnd(const TInNode& node){
00072 return TTraits::GetEnd(node);
00073 }
00074
00075 static TInAttribute GetAttribute(const TInNode& node, TInIterator i){
00076 return TTraits::GetAttribute(node, i);
00077 }
00078
00079
00080 typedef typename TTraits::TOutNode TOutNode;
00081
00082 typedef typename TTraits::TOutValue TOutValue;
00083
00084 typedef typename TTraits::TOutAttribute TOutAttribute;
00085
00086 typedef typename TTraits::TOutAttributes TOutAttributes;
00087
00088
00089 static void CreateNode(TOutNode& node, const TOutAttributes& attrs){
00090 TTraits::CreateNode(node, attrs);
00091 }
00092
00093 static void CreateValue(TOutValue& value, const TString& s){
00094 TTraits::CreateValue(value, s);
00095 }
00096
00097 static void CreateValue(TOutValue& value, const TOutNode& node){
00098 TTraits::CreateValue(value, node);
00099 }
00100
00101 static void CreateAttribute(TOutAttribute& attr, const TKey& key, const TOutValue& value){
00102 TTraits::CreateAttribute(attr, key, value);
00103 }
00104
00105 static void Add(TOutAttributes& attrs, const TOutAttribute& attr){
00106 TTraits::Add(attrs, attr);
00107 }
00108
00109 static void Add(TOutAttributes& attrs, const TOutAttributes& added){
00110 TTraits::Add(attrs, added);
00111 }
00112
00113
00114 struct TLabeledInfo{
00115 TInNode node;
00116 TInIterator first;
00117 TInIterator last;
00118 TLabeledInfo(){}
00119 TLabeledInfo(const TInNode& n, TInIterator f, TInIterator l)
00120 : node(n), first(f), last(l){}
00121 };
00122
00123 typedef std::vector<TOutValue> TOutValues;
00124
00125
00126 typedef std::map<int, TLabeledInfo> TSourceLabelMap;
00127 struct TSourceLabelMaps{
00128 TSourceLabelMap values;
00129 TSourceLabelMap attrs;
00130 };
00131
00132 typedef std::map<int, TOutValues> TTargetValueLabelMap;
00133 typedef std::map<int, TOutAttributes> TTargetAttributeLabelMap;
00134 struct TTargetLabelMaps{
00135 TTargetValueLabelMap values;
00136 TTargetAttributeLabelMap attrs;
00137 };
00138
00139 };
00140
00141
00142 template <class TTraits>
00143 class FIPatternImpl: public FITreeDefs<TTraits>
00144 {
00145 public:
00146 FIPatternImpl(): label(0){}
00147 virtual ~FIPatternImpl(){}
00148
00149 virtual std::pair<bool, TInIterator>
00150 Match(const TInNode& pnode, TInIterator first, TInIterator last, TSourceLabelMaps* lmap)= 0;
00151
00152
00153 void Generate(TOutAttributes& attrs, const TTargetLabelMaps* lmap);
00154
00155 void SetLabel(int l){ label= l; }
00156
00157 void SaveIfLabeled(
00158 const TInNode& node,
00159 TInIterator first,
00160 TInIterator last,
00161 TSourceLabelMaps* lmap,
00162 bool asAttrs= false);
00163
00164 virtual TKey GetMatchingKey()const= 0;
00165 virtual void SetMatchingKey(const TKey& k)= 0;
00166
00167 protected:
00168
00169 virtual void GenerateSelf(TOutAttributes& attrs, const TTargetLabelMaps* lmap)= 0;
00170
00171 virtual void GenerateLabeled(
00172 TOutAttributes& attrs, const TOutValues& values);
00173
00174 private:
00175 int label;
00176
00177 };
00178
00179
00180
00181 template <class TTraits>
00182 class FIPattern: public FITreeDefs<TTraits>
00183 {
00184 public:
00185 explicit FIPattern(FIPatternImpl<TTraits>* i): impl(i){}
00186
00187 std::pair<bool, TInIterator> Match(
00188 const TInNode& pnode,
00189 TInIterator first,
00190 TInIterator last,
00191 TSourceLabelMaps* lmap)const{
00192 return impl->Match(pnode, first, last, lmap);
00193 }
00194
00195
00196 void Generate(TOutAttributes& attrs, const TTargetLabelMaps* lmap)const{
00197 impl->Generate(attrs, lmap);
00198 }
00199
00200 FIPattern& operator[](int l){
00201 impl->SetLabel(l);
00202 return *this;
00203 }
00204
00205 FIPattern operator!();
00206
00207 boost::shared_ptr<FIPatternImpl<TTraits> > GetImpl()const{ return impl; }
00208
00209 private:
00210 boost::shared_ptr<FIPatternImpl<TTraits> > impl;
00211
00212 };
00213
00214
00215 template <class T>
00216 inline FIPattern<T> operator>>(
00217 const FIPattern<T>& firstPattern, const FIPattern<T>& secondPattern);
00218
00219
00220 template <class TTraits>
00221 class FINodePatternImpl: public FIPatternImpl<TTraits>
00222 {
00223 public:
00224
00225 FINodePatternImpl(const TKey& k, const FIPattern<TTraits>& p)
00226 : key(k), childrenPattern(p){}
00227
00228 virtual std::pair<bool, TInIterator>
00229 Match(const TInNode& pnode, TInIterator first, TInIterator last, TSourceLabelMaps* lmap);
00230
00231 protected:
00232
00233 virtual void GenerateSelf(TOutAttributes& attrs, const TTargetLabelMaps* lmap);
00234
00235 virtual TKey GetMatchingKey()const{ return key; }
00236 virtual void SetMatchingKey(const TKey& k);
00237
00238 private:
00239 TKey key;
00240 FIPattern<TTraits> childrenPattern;
00241
00242 };
00243
00244
00245 template <class TTraits>
00246 class FIStringPatternImpl: public FIPatternImpl<TTraits>
00247 {
00248 public:
00249 FIStringPatternImpl(const TKey& k, const TString& v)
00250 : key(k), str(v){}
00251
00252 virtual std::pair<bool, TInIterator>
00253 Match(const TInNode& pnode, TInIterator first, TInIterator last, TSourceLabelMaps* lmap);
00254
00255 protected:
00256
00257 virtual void GenerateSelf(TOutAttributes& attrs, const TTargetLabelMaps* lmap);
00258
00259 virtual TKey GetMatchingKey()const{ return key; }
00260 virtual void SetMatchingKey(const TKey& k);
00261
00262 private:
00263 TKey key;
00264 TString str;
00265
00266 };
00267
00268
00269 template <class TTraits>
00270 class FIAnyValuePatternImpl: public FIPatternImpl<TTraits>
00271 {
00272 public:
00273 FIAnyValuePatternImpl(const TKey& k)
00274 : key(k){}
00275
00276 virtual std::pair<bool, TInIterator>
00277 Match(const TInNode& pnode, TInIterator first, TInIterator last, TSourceLabelMaps* lmap);
00278
00279 protected:
00280
00281 virtual void GenerateSelf(TOutAttributes& attrs, const TTargetLabelMaps* lmap);
00282
00283 virtual TKey GetMatchingKey()const{ return key; }
00284 virtual void SetMatchingKey(const TKey& k){ key= k; }
00285
00286 private:
00287 TKey key;
00288
00289 };
00290
00291
00292
00293 template <class TTraits>
00294 class FIConjunctionPatternImpl: public FIPatternImpl<TTraits>
00295 {
00296 public:
00297 FIConjunctionPatternImpl(const FIPattern<TTraits>& f, const FIPattern<TTraits>& s)
00298 : firstPattern(f), secondPattern(s){}
00299
00300 virtual std::pair<bool, TInIterator>
00301 Match(const TInNode& pnode, TInIterator first, TInIterator last, TSourceLabelMaps* lmap);
00302
00303 protected:
00304
00305 virtual void GenerateSelf(TOutAttributes& attrs, const TTargetLabelMaps* lmap);
00306
00307 virtual TKey GetMatchingKey()const;
00308 virtual void SetMatchingKey(const TKey& k);
00309
00310 private:
00311 FIPattern<TTraits> firstPattern;
00312 FIPattern<TTraits> secondPattern;
00313
00314 };
00315
00316
00317
00318 template <class TTraits>
00319 class FIOptionalPatternImpl: public FIPatternImpl<TTraits>
00320 {
00321 public:
00322 FIOptionalPatternImpl(const FIPattern<TTraits>& p)
00323 : pattern(p){}
00324
00325 virtual std::pair<bool, TInIterator>
00326 Match(const TInNode& pnode, TInIterator first, TInIterator last, TSourceLabelMaps* lmap);
00327
00328 protected:
00329
00330 virtual void GenerateSelf(TOutAttributes& attrs, const TTargetLabelMaps* lmap);
00331
00332 virtual TKey GetMatchingKey()const;
00333 virtual void SetMatchingKey(const TKey& k);
00334
00335 private:
00336 FIPattern<TTraits> pattern;
00337
00338 };
00339
00340
00341 template <class TTraits>
00342 class FIAnyPatternImpl: public FIPatternImpl<TTraits>
00343 {
00344 public:
00345 FIAnyPatternImpl(const TKey& k)
00346 : key(k){}
00347
00348 virtual std::pair<bool, TInIterator>
00349 Match(const TInNode& pnode, TInIterator first, TInIterator last, TSourceLabelMaps* lmap);
00350
00351 virtual TKey GetMatchingKey()const{ return key; }
00352 virtual void SetMatchingKey(const TKey& k){ key= k; }
00353
00354 protected:
00355
00356 virtual void GenerateSelf(TOutAttributes& attrs, const TTargetLabelMaps* lmap);
00357
00358 private:
00359 TKey key;
00360
00361 };
00362
00363
00364
00365 template <class TTraits>
00366 class FIAnyAttributesPatternImpl: public FIPatternImpl<TTraits>
00367 {
00368 public:
00369
00370 virtual std::pair<bool, TInIterator>
00371 Match(const TInNode& pnode, TInIterator first, TInIterator last, TSourceLabelMaps* lmap);
00372
00373 virtual TKey GetMatchingKey()const{ assert(false); return TKey(); }
00374 virtual void SetMatchingKey(const TKey& k){ assert(false); }
00375
00376 protected:
00377
00378 virtual void GenerateSelf(TOutAttributes& attrs, const TTargetLabelMaps* lmap);
00379
00380 };
00381
00382
00383
00384
00385 template <class TTraits>
00386 class FIAASPatternImpl: public FIPatternImpl<TTraits>
00387 {
00388 FIPattern<TTraits> end;
00389 public:
00390 FIAASPatternImpl(const FIPattern<TTraits>& e):end(e){
00391 }
00392
00393 virtual std::pair<bool, TInIterator>
00394 Match(const TInNode& pnode, TInIterator first, TInIterator last, TSourceLabelMaps* lmap);
00395
00396 virtual TKey GetMatchingKey()const{ assert(false); return TKey(); }
00397 virtual void SetMatchingKey(const TKey& k){ assert(false); }
00398
00399 protected:
00400
00401 virtual void GenerateSelf(TOutAttributes& attrs, const TTargetLabelMaps* lmap);
00402 };
00403
00404
00405 template <class TTraits>
00406 struct FITreePatterns: public FITreeDefs<TTraits>
00407 {
00408
00409 typedef FIPattern<TTraits> TPattern;
00410
00411
00412 static TPattern Any(){
00413 return TPattern(new FIAnyPatternImpl<TTraits>(TKey()));
00414 }
00415
00416 static TPattern Node(const TPattern& childrenPattern){
00417 return TPattern(new FINodePatternImpl<TTraits>(TKey(), childrenPattern));
00418 }
00419
00420 static TPattern Value(){
00421 return TPattern(new FIAnyValuePatternImpl<TTraits>(TKey()));
00422 }
00423
00424 static TPattern String(const TString& str){
00425 return TPattern(new FIStringPatternImpl<TTraits>(TKey(), str));
00426 }
00427
00428 static TPattern Attribute(const TKey& key, const TPattern& pattern){
00429 pattern.GetImpl()->SetMatchingKey(key);
00430 return pattern;
00431 }
00432
00433 static TPattern AnyAttributes(){
00434 return TPattern(new FIAnyAttributesPatternImpl<TTraits>());
00435 }
00436 static TPattern AAS(TPattern& p){
00437 return TPattern(new FIAASPatternImpl<TTraits>(p));
00438 }
00439
00440 };
00441
00442
00443 template <class TLTraits, class TRTraits>
00444 class FIHalfTreeConverter
00445 {
00446 typedef FIHalfTreeConverter TThis;
00447 typedef FITreePatterns<TLTraits> TL;
00448 typedef FITreePatterns<TRTraits> TR;
00449
00450 public:
00451
00452 void Add(
00453 const typename TL::TPattern& lpattern,
00454 const typename TR::TPattern& rpattern);
00455
00456 void Convert(
00457 typename TR::TOutAttributes& outAttrs,
00458 const typename TL::TInNode& parent,
00459 typename TL::TInIterator first,
00460 typename TL::TInIterator last);
00461
00462 private:
00463 typedef std::list<std::pair<typename TL::TPattern, typename TR::TPattern> >
00464 TRules;
00465
00466 TRules rules;
00467
00468
00469 void ConvertValue(
00470 typename TR::TOutValue& outValue,
00471 typename const TL::TInValue& inValue);
00472
00473 };
00474
00475
00476
00477
00478
00479
00480 template <class TL, class TR>
00481 struct FIConvertationTraits
00482 {
00483 static typename TR::TKey ConvertKey(const typename TL::TKey& key){
00484 return key;
00485 }
00486 static typename TR::TString ConvertString(const typename TL::TString& str){
00487 return str;
00488 }
00489
00490 };
00491
00492
00493 #include "FITreeConverterImpl.h"
00494
00495 #endif