File: CAlgorithm.cc

package info (click to toggle)
gnuift 0.1.14%2Bds-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 5,632 kB
  • ctags: 2,973
  • sloc: cpp: 15,867; sh: 8,281; ansic: 1,812; perl: 1,007; php: 651; makefile: 483; lisp: 344
file content (303 lines) | stat: -rw-r--r-- 8,994 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// -*- mode: c++ -*- 
/* 

    GIFT, a flexible content based image retrieval system.
    Copyright (C) 1998, 1999, 2000, 2001, 2002, CUI University of Geneva

     Copyright (C) 2003, 2004 Bayreuth University
      2005 Bamberg University
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/
#include "libMRML/include/mrml_const.h"
#include "libMRML/include/my_throw.h"
#include <algorithm>
#include "libMRML/include/CQuery.h"
#include "libMRML/include/CAlgorithm.h"
#include "libMRML/include/CXMLHelper.h"
#include "libMRML/include/CQueryTreeBuilder.h"
#include "libMRML/include/CAlgorithmCollection.h"
/** clone this*/
CAlgorithm* CAlgorithm::clone()const{
  return new CAlgorithm(*this);
};
/** clone this*/
CAlgorithm::CAlgorithm(const CAlgorithm& inAlgorithm):
  CXMLElement(inAlgorithm){
  
};
/** clone this*/
CAlgorithm::CAlgorithm(const CXMLElement& inAlgorithm):
  CXMLElement(inAlgorithm){
  
};
/**
   Add a child with the given attributes
   to the current node, and MAKE 
   THIS CHILD THE NEW CURRENT NODE.
*/
void CAlgorithm::addChild(const string& inName,
			   const char* const* const inAttributeList){
  //initialize new child
  CXMLElement::addChild(static_cast
			<CXMLElement*>(new CAlgorithm(inName.c_str(),
						      inAttributeList)));
}
/**
   Add a child with the given attributes
   to the current node, and MAKE 
   THIS CHILD THE NEW CURRENT NODE.
*/
void CAlgorithm::addChild(CXMLElement* inChild){
    //initialize new child
    CXMLElement::addChild(inChild);
}
/**
*/
CAlgorithm::CAlgorithm(const char* inName,
		       const char* const* const inAttributeList):
  CXMLElement(inName,inAttributeList){
};
/**
*/
CAlgorithm::CAlgorithm(const string& inName,
		       const list<pair<string,string> >& inAttributeList):
  CXMLElement(inName,inAttributeList){
};
//----------------------------------------
//sending back the name of the collection treated by this
string CAlgorithm::getCollectionID()const{
  return stringReadAttribute(mrml_const::collection_id).second;
};
//----------------------------------------
//sending back the name of the algorithm
string CAlgorithm::getAlgorithmName()const{
  return stringReadAttribute(mrml_const::algorithm_name).second;
};
//----------------------------------------
//sending back the ID of the algorithm
string CAlgorithm::getID()const{
  return stringReadAttribute(mrml_const::algorithm_id).second;
};
//----------------------------------------
//sending back the relative weight of the algorithm
double CAlgorithm::getWeight()const{
  if(doubleReadAttribute(mrml_const::cui_weight).first){
    return (doubleReadAttribute(mrml_const::cui_weight).second);
  }else{
    return 1;
  }
};
//----------------------------------------
//sending back the Type of the algorithm
pair<bool,string> CAlgorithm::getType()const{
  return stringReadAttribute(mrml_const::algorithm_type);
};
//----------------------------------------
//sending back the Type of the algorithm
pair<bool,string> CAlgorithm::getInheritsFrom()const{
  return stringReadAttribute("cui-inherits-from");
};
//----------------------------------------
//sending back the BaseType of the algorithm
string CAlgorithm::getBaseType()const{
  return stringReadAttribute(mrml_const::cui_base_type).second;
};
//----------------------------------------
//making some old MRML for speaking with zoran
string CAlgorithm::toOldMRML()const{
  return string("<"
		+ mrml_const::algorithm 
		+ " " 
		+ mrml_const::algorithm_name
		+ "=\"")
    + getName()
    + "\" " 
    + mrml_const::algorithm_id
    + "=\""
    + getID()
    + "\"/>\n";
};


// //----------------------------------------
// /** reading attributes with defaults
//  */
// pair<bool,string> CAlgorithm::stringReadAttribute(const string& 
// 						  inAttribute)const{


//   return CXMLElement::stringReadAttribute(inAttribute);

//   ///code ignored from here ******************************

//   cout << "StringReadAttribute:"
//        << inAttribute
//        << endl;

//   CAttributeList::const_iterator lFoundPosition=mAttributes.find(inAttribute);
  
//   if(lFoundPosition != mAttributes.end()){
//     return make_pair(bool(1),
// 		     lFoundPosition->second);
//   }
//   if(mDefaultAlgorithm){
//     pair<bool,string> lResult=mDefaultAlgorithm->stringReadAttribute(inAttribute);

//     if(lResult.first){
//       return lResult;
//     }
//   }
//   if(getFather()){
//     return getFather()->stringReadAttribute(inAttribute);
//   }
//   cout << "..failed: the ID of the element: "
//        << getID()
//        << endl;
//   return make_pair(0,
// 		   string(""));
// };
//----------------------------------------
/** 
    Building a query tree out of the information we have gathered
    true indicates success...

    
*/
bool CAlgorithm::configure(CAlgorithmCollection& inBaseConfiguration){
  //
  // merge this with the default algorithm of this type
  //
  cout << this << endl;
  cout << "1" << flush;
  if((this->getType().first)
     &&(inBaseConfiguration.containsAlgorithmByType(this->getType().second))){
    mergeAttributes(inBaseConfiguration.getAlgorithmByType(this->getType().second));

    CAlgorithm& lDefault(inBaseConfiguration.getAlgorithmByType(this->getType().second));

    //HACK clone children from default if this childlist is empty or contains 
    //only text nodes.
    int lSize=0;
    for(lCChildren::const_iterator i=mChildren.begin();
	i!=mChildren.end();
	i++){
      if((*i)->getName()==mrml_const::algorithm){
	lSize++;
      }
    }
    if(!lSize){
	cout << ""
	     << endl;

	//	mCurrentChild=this;
	for(lCChildren::const_iterator i=lDefault.child_list_begin();
	    i!=lDefault.child_list_end();
	    i++){
	  cout << " || "
	       << mChildren.size()
	       << ((CAlgorithm*)(*i))->getBaseType()
	       << endl;
	  addChild((*i)->clone(true));
	  moveUp();
	}
    }
    cout << "NUMBER OF CHILDREN that are ALGORITHMS " 
	 << lSize
	 << endl;
    //mergeChildLists(inBaseConfiguration.getAlgorithmByType(this->getType().second));
  }
  cout << "2" << flush;
  // merge with the configuration of the father
  if(getFather()){
    mergeAttributes(*(CAlgorithm*)(getFather()));
  }
  cout << "3" << flush;


  // 
  // 3. Then build the query trees of the children and 
  // add each child both to the query finding structure
  // as to the query tree.
  //
  for(CXMLElement::lCChildren::const_iterator i=child_list_begin();
      i!=child_list_end();
      i++){
    CXMLElement* lDereferencedI=*i;
    
    if(lDereferencedI->getName()==mrml_const::algorithm){
      CAlgorithm*  iAlgorithm=(CAlgorithm*)(lDereferencedI);
      cout << "." << flush;
      iAlgorithm->configure(inBaseConfiguration);
    }
  }
  if(getFather()){
    //mergeChildLists(*(CAlgorithm*)(getFather()));
  }
  cout << "4" << flush;
}
/** merging two algorithms.

    general rule: if something is contained already in *this
    the value of *this will be kept.
    
    
    first: default values.
*/
void CAlgorithm::mergeAttributes(const CAlgorithm& inAlgorithm){
  if(&inAlgorithm){
    //STOOPID way of merging, takes n log n time instead of n
    for(CAttributeList::const_iterator
	  i=inAlgorithm.mAttributes.begin();
	i!=inAlgorithm.mAttributes.end();
	i++){
      //add attributes which are not yet found
      if(mAttributes.find(i->first)==mAttributes.end()){
#ifdef SINGLE
	mAttributes.add(i->first,i->second);
#else
	mAttributes.check();
	mAttributes.add(i->first,i->second.first);
#endif
      }
    }
  }
}

void CAlgorithm::mergeChildLists(const CAlgorithm& inAlgorithm){
  if(&inAlgorithm){
    //merge child lists
    copy(inAlgorithm.child_list_begin(),
	 inAlgorithm.child_list_end(),
	 inserter(mChildren,
		  child_list_end()));
  }
}

//----------------------------------------
//Sorting algorithms by their ID (coming from a config file)
bool CSortByID_CA::operator()(const CAlgorithm& l,
			      const CAlgorithm& t)const{
  return l.stringReadAttribute(mrml_const::algorithm_id)<l.stringReadAttribute(mrml_const::algorithm_id);
};

//----------------------------------------
//Sorting algorithms by their base (an integer defined in this source file)
bool CSortByBase_CA::operator()(const CAlgorithm& l,
				const CAlgorithm& t)const{
  return l.stringReadAttribute(mrml_const::cui_base_type)<l.stringReadAttribute(mrml_const::cui_base_type);
};