00001 #ifndef FI_TREE_CONVERTER_IMPL_H
00002 #define FI_TREE_CONVERTER_IMPL_H
00003
00004
00005
00006
00007 template <class T>
00008 void FIPatternImpl<T>::SaveIfLabeled(
00009 const TInNode& node,
00010 TInIterator first,
00011 TInIterator last,
00012 TSourceLabelMaps* lmap,
00013 bool asAttrs)
00014 {
00015 if (label!=0 && lmap){
00016
00017
00018
00019 if (asAttrs)
00020 lmap->attrs[label]= TLabeledInfo(node, first, last);
00021 else
00022 lmap->values[label]= TLabeledInfo(node, first, last);
00023 }
00024 }
00025
00026
00027 template <class T>
00028 void FIPatternImpl<T>::Generate(TOutAttributes& attrs, const TTargetLabelMaps* lmap)
00029 {
00030 if (label!=0){
00031
00032 TTargetValueLabelMap::const_iterator vit;
00033 TTargetAttributeLabelMap::const_iterator ait;
00034 if ((vit= lmap->values.find(label))!=(lmap->values.end()))
00035 GenerateLabeled(attrs, vit->second);
00036
00037 else if ((ait= lmap->attrs.find(label))!=(lmap->attrs.end()))
00038 Add(attrs, ait->second);
00039 }else{
00040 return GenerateSelf(attrs, lmap);
00041 }
00042 }
00043
00044
00045 template <class T>
00046 void FIPatternImpl<T>::GenerateLabeled(
00047 TOutAttributes& attrs, const TOutValues& values)
00048 {
00049 for (std::size_t i= 0; i<values.size(); ++i){
00050 TOutAttribute attr;
00051 CreateAttribute(attr, GetMatchingKey(), values[i]);
00052 Add(attrs, attr);
00053 }
00054 }
00055
00056
00057 template <class T>
00058 inline FIPattern<T> FIPattern<T>::operator!()
00059 {
00060 return FIPattern(new FIOptionalPatternImpl<T>(*this));
00061 }
00062
00063
00064
00065 template <class T>
00066 inline FIPattern<T> operator>>(
00067 const FIPattern<T>& firstPattern, const FIPattern<T>& secondPattern){
00068 return FIPattern<T>(new FIConjunctionPatternImpl<T>(
00069 firstPattern, secondPattern));
00070 }
00071
00072
00073 template <class T>
00074 std::pair<bool, typename T::TInIterator>
00075 FINodePatternImpl<T>::Match(
00076 const TInNode& pnode,
00077 TInIterator first,
00078 TInIterator last,
00079 TSourceLabelMaps* lmap)
00080 {
00081 if (first==last) return make_pair(false, TInIterator());
00082 TInAttribute attr= GetAttribute(pnode, first);
00083 TInValue value= GetValue(attr);
00084 if (GetKey(attr)!=GetMatchingKey() || IsString(value))
00085 return make_pair(false, TInIterator());
00086 TInNode node= GetNode(value);
00087 std::pair<bool, TInIterator> result
00088 = childrenPattern.Match(node, GetBegin(node), GetEnd(node), lmap);
00089 if (result.first && result.second==GetEnd(node)){
00090 SaveIfLabeled(pnode, first, first+1, lmap);
00091 return make_pair(true, first+1);
00092 }else{
00093 return make_pair(false, TInIterator());
00094 }
00095 }
00096
00097
00098 template <class T>
00099 void FINodePatternImpl<T>::GenerateSelf(
00100 TOutAttributes& attrs, const TTargetLabelMaps* lmap)
00101 {
00102 TOutAttributes cattrs;
00103 childrenPattern.Generate(cattrs, lmap);
00104 TOutNode node;
00105 CreateNode(node, cattrs);
00106 TOutValue value;
00107 CreateValue(value, node);
00108 TOutAttribute attr;
00109 CreateAttribute(attr, GetMatchingKey(), value);
00110 Add(attrs, attr);
00111 }
00112
00113
00114
00115 template <class T>
00116 void FINodePatternImpl<T>::SetMatchingKey(const TKey& k)
00117 {
00118 assert(key==TKey());
00119 key= k;
00120 }
00121
00122
00123 template <class T>
00124 std::pair<bool, typename T::TInIterator>
00125 FIStringPatternImpl<T>::Match(
00126 const TInNode& pnode,
00127 TInIterator first,
00128 TInIterator last,
00129 TSourceLabelMaps* lmap)
00130 {
00131 if (first==last) return make_pair(false, TInIterator());
00132 TInAttribute attr= GetAttribute(pnode, first);
00133 TInValue value= GetValue(attr);
00134 if (GetKey(attr)==GetMatchingKey() && IsString(value) && GetString(value)==str){
00135 SaveIfLabeled(pnode, first, first+1, lmap);
00136 return std::make_pair(true, first+1);
00137 }else{
00138 return std::make_pair(false, TInIterator());
00139 }
00140 }
00141
00142 template <class T>
00143 void FIStringPatternImpl<T>::GenerateSelf(
00144 TOutAttributes& attrs, const TTargetLabelMaps* lmap)
00145 {
00146 TOutValue value;
00147 CreateValue(value, str);
00148 TOutAttribute attr;
00149 CreateAttribute(attr, GetMatchingKey(), value);
00150 Add(attrs, attr);
00151 }
00152
00153
00154
00155 template <class T>
00156 void FIStringPatternImpl<T>::SetMatchingKey(const TKey& k)
00157 {
00158 assert(key==TKey());
00159 key= k;
00160 }
00161
00162
00163 template <class T>
00164 std::pair<bool, typename T::TInIterator>
00165 FIAnyValuePatternImpl<T>::Match(
00166 const TInNode& pnode,
00167 TInIterator first,
00168 TInIterator last,
00169 TSourceLabelMaps* lmap)
00170 {
00171
00172 if (first!=last && GetKey(GetAttribute(pnode, first))==GetMatchingKey()){
00173 SaveIfLabeled(pnode, first, first+1, lmap);
00174 return std::make_pair(true, first+1);
00175 }else{
00176 return std::make_pair(false, TInIterator());
00177 }
00178 }
00179
00180
00181 template <class T>
00182 void FIAnyValuePatternImpl<T>::GenerateSelf(
00183 TOutAttributes& attrs, const TTargetLabelMaps* lmap)
00184 {
00185 assert(false);
00186 }
00187
00188
00189 template <class T>
00190 std::pair<bool, typename T::TInIterator>
00191 FIConjunctionPatternImpl<T>::Match(const TInNode& pnode, TInIterator first, TInIterator last,
00192 TSourceLabelMaps* lmap)
00193 {
00194 pair<bool, TInIterator> result
00195 = firstPattern.Match(pnode, first, last, lmap);
00196 if (!result.first) return result;
00197 result= secondPattern.Match(pnode, result.second, last, lmap);
00198 if (result.first) SaveIfLabeled(pnode, first, result.second, lmap);
00199 return result;
00200 }
00201
00202 template <class T>
00203 void FIConjunctionPatternImpl<T>::GenerateSelf(
00204 TOutAttributes& attrs, const TTargetLabelMaps* lmap)
00205 {
00206 firstPattern.Generate(attrs, lmap);
00207 secondPattern.Generate(attrs, lmap);
00208 }
00209
00210
00211
00212 template <class T>
00213 typename FIConjunctionPatternImpl<T>::TKey
00214 FIConjunctionPatternImpl<T>::GetMatchingKey()const
00215 {
00216
00217
00218 TKey firstKey= firstPattern.GetImpl()->GetMatchingKey();
00219 TKey secondKey= secondPattern.GetImpl()->GetMatchingKey();
00220 return (firstKey==secondKey)? firstKey : TKey();
00221 }
00222
00223
00224
00225 template <class T>
00226 void FIConjunctionPatternImpl<T>::SetMatchingKey(const TKey& k)
00227 {
00228 firstPattern.GetImpl()->SetMatchingKey(k);
00229 secondPattern.GetImpl()->SetMatchingKey(k);
00230 }
00231
00232
00233 template <class T>
00234 std::pair<bool, typename T::TInIterator>
00235 FIOptionalPatternImpl<T>::Match(
00236 const TInNode& pnode,
00237 TInIterator first,
00238 TInIterator last,
00239 TSourceLabelMaps* lmap)
00240 {
00241 std::pair<bool, TInIterator> result
00242 = pattern.Match(pnode, first, last, lmap);
00243 if (result.first) return result;
00244 else return std::make_pair(true, first);
00245 }
00246
00247
00248 template <class T>
00249 void FIOptionalPatternImpl<T>::GenerateSelf(
00250 TOutAttributes& attrs, const TTargetLabelMaps* lmap)
00251 {
00252 pattern.Generate(attrs, lmap);
00253 }
00254
00255
00256
00257 template <class T>
00258 typename FIOptionalPatternImpl<T>::TKey
00259 FIOptionalPatternImpl<T>::GetMatchingKey()const
00260 {
00261 return pattern.GetImpl()->GetMatchingKey();
00262 }
00263
00264
00265
00266 template <class T>
00267 void FIOptionalPatternImpl<T>::SetMatchingKey(const TKey& k)
00268 {
00269 pattern.GetImpl()->SetMatchingKey(k);
00270 }
00271
00272
00273 template <class T>
00274 std::pair<bool, typename T::TInIterator>
00275 FIAnyPatternImpl<T>::Match(
00276 const TInNode& pnode,
00277 TInIterator first,
00278 TInIterator last,
00279 TSourceLabelMaps* lmap)
00280 {
00281 TInIterator it;
00282 for (it= first; it!=last; ++it){
00283 if (GetKey(GetAttribute(pnode, it))!=key) break;
00284 }
00285 SaveIfLabeled(pnode, first, it, lmap);
00286 return std::make_pair(true, it);
00287 }
00288
00289
00290 template <class T>
00291 void FIAnyPatternImpl<T>::GenerateSelf(
00292 TOutAttributes& attrs, const TTargetLabelMaps* lmap)
00293 {
00294 assert(false);
00295 }
00296
00297
00298 template <class T>
00299 std::pair<bool, typename T::TInIterator>
00300 FIAnyAttributesPatternImpl<T>::Match(
00301 const TInNode& pnode,
00302 TInIterator first,
00303 TInIterator last,
00304 TSourceLabelMaps* lmap)
00305 {
00306 SaveIfLabeled(pnode, first, last, lmap, true);
00307
00308 return std::make_pair(true, last);
00309 }
00310
00311 template <class T>
00312 void FIAnyAttributesPatternImpl<T>::GenerateSelf(
00313 TOutAttributes& attrs, const TTargetLabelMaps* lmap)
00314 {
00315 assert(false);
00316 }
00317
00318
00319 template <class T>
00320 std::pair<bool, typename T::TInIterator>
00321 FIAASPatternImpl<T>::Match(
00322 const TInNode& pnode,
00323 TInIterator first,
00324 TInIterator last,
00325 TSourceLabelMaps* lmap)
00326 {
00327 TInIterator it = first;
00328 for(;it != last; ++it){
00329 if (end.Match(pnode, it, last, lmap).first) break;
00330 }
00331 SaveIfLabeled(pnode, first, it, lmap, true);
00332
00333 return std::make_pair(true, it);
00334 }
00335
00336 template <class T>
00337 void FIAASPatternImpl<T>::GenerateSelf(
00338 TOutAttributes& attrs, const TTargetLabelMaps* lmap)
00339 {
00340 assert(false);
00341 }
00342
00343
00344 template <class T, class S>
00345 void FIHalfTreeConverter<T, S>::Add(const typename TL::TPattern& lpattern,
00346 const typename TR::TPattern& rpattern)
00347 {
00348 rules.push_back(std::make_pair(lpattern, rpattern));
00349 }
00350
00351
00352
00353 template <class T, class S>
00354 void FIHalfTreeConverter<T, S>::Convert(
00355 typename TR::TOutAttributes& outAttrs,
00356 const typename TL::TInNode& parent,
00357 typename TL::TInIterator first,
00358 typename TL::TInIterator last)
00359 {
00360 typedef typename TL::TSourceLabelMap::const_iterator TSourceLabelIterator;
00361 typename TL::TInIterator ait= first;
00362 typename TL::TSourceLabelMaps lmap;
00363 std::pair<bool, typename TL::TInIterator> result;
00364 TRules::const_iterator rit;
00365
00366 while (ait!=last){
00367
00368 for (rit= rules.begin(); rit!=rules.end(); ++rit){
00369 result= rit->first.Match(parent, ait, last, &lmap);
00370 if (result.first) break;
00371 }
00372 if (rit!=rules.end()){
00373
00374 typename TR::TTargetLabelMaps tmap;
00375 for (TSourceLabelIterator lit= lmap.values.begin(); lit!=lmap.values.end(); ++lit){
00376 int label= lit->first;
00377 TL::TLabeledInfo linfo= lit->second;
00378 for (typename TL::TInIterator lait= linfo.first; lait!=linfo.last; ++lait){
00379 typename TL::TInValue svalue= TL::GetValue(TL::GetAttribute(linfo.node, lait));
00380 typename TR::TOutValue tvalue;
00381 ConvertValue(tvalue, svalue);
00382 tmap.values[label].push_back(tvalue);
00383 }
00384 }
00385
00386 for (TSourceLabelIterator lit= lmap.attrs.begin(); lit!=lmap.attrs.end(); ++lit){
00387 int label= lit->first;
00388 TL::TLabeledInfo linfo= lit->second;
00389 Convert(tmap.attrs[label], linfo.node, linfo.first, linfo.last);
00390 }
00391
00392 rit->second.Generate(outAttrs, &tmap);
00393
00394 ait= result.second;
00395 }else{
00396
00397
00398 typename TL::TInAttribute sattr= TL::GetAttribute(parent, ait);
00399 typename TL::TKey skey= TL::GetKey(sattr);
00400 typename TL::TInValue svalue= TL::GetValue(sattr);
00401 typename TR::TKey tkey= FIConvertationTraits<T, S>::ConvertKey(skey);
00402 typename TR::TOutValue tvalue;
00403 ConvertValue(tvalue, svalue);
00404 typename TR::TOutAttribute tattr;
00405 TR::CreateAttribute(tattr, tkey, tvalue);
00406 TR::Add(outAttrs, tattr);
00407 ++ait;
00408 }
00409 }
00410 }
00411
00412
00413
00414 template <class T, class S>
00415 void FIHalfTreeConverter<T, S>::ConvertValue(
00416 typename TR::TOutValue& outValue,
00417 typename const TL::TInValue& inValue)
00418 {
00419 if (TL::IsString(inValue)){
00420
00421 TR::CreateValue(outValue,
00422 FIConvertationTraits<T, S>::ConvertString(TL::GetString(inValue)));
00423 }else{
00424
00425 typename TL::TInNode snode= TL::GetNode(inValue);
00426 typename TR::TOutAttributes tattrs;
00427 Convert(tattrs, snode, TL::GetBegin(snode), TL::GetEnd(snode));
00428 typename TR::TOutNode tnode;
00429 TR::CreateNode(tnode, tattrs);
00430 TR::CreateValue(outValue, tnode);
00431 }
00432 }
00433
00434
00435 #endif