00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #if !defined(ALIZE_MixtureServer_cpp)
00056 #define ALIZE_MixtureServer_cpp
00057
00058
00059
00060 #include <ctime>
00061 #include "MixtureServer.h"
00062 #include "MixtureFileReader.h"
00063 #include "MixtureServerFileReader.h"
00064 #include "MixtureServerFileWriter.h"
00065 #include "MixtureGD.h"
00066 #include "MixtureGF.h"
00067 #include "DistribGD.h"
00068 #include "DistribGF.h"
00069 #include "Exception.h"
00070 #include "XLine.h"
00071 #include "ULongVector.h"
00072
00073 using namespace alize;
00074 typedef MixtureServer S;
00075
00076
00077 S::MixtureServer(const Config& c)
00078 :Object(), _config(c) { reset(); }
00079
00080 S::MixtureServer(const FileName& f, const Config& c)
00081 :Object(), _config(c)
00082 {
00083 reset();
00084 load(f);
00085 }
00086
00087 void S::reset()
00088 {
00089 _mixtureDict.clear();
00090 _distribDict.clear();
00091 _lastMixtureId = 0;
00092 _vectSizeDefined = false;
00093 }
00094
00095 Distrib& S::createDistrib()
00096 {
00097 unsigned long vectSize;
00098 if (_vectSizeDefined)
00099 vectSize = _vectSize;
00100 else
00101 vectSize = _config.getParam_vectSize();
00102 return createDistrib(_config.getParam_distribType(), vectSize);
00103 }
00104
00105 DistribGD& S::createDistribGD()
00106 {
00107 unsigned long vectSize;
00108 if (_vectSizeDefined)
00109 vectSize = _vectSize;
00110 else
00111 vectSize = _config.getParam_vectSize();
00112 return static_cast<DistribGD&>(createDistrib(DistribType_GD, vectSize));
00113 }
00114
00115 DistribGF& S::createDistribGF()
00116 {
00117 unsigned long vectSize;
00118 if (_vectSizeDefined)
00119 vectSize = _vectSize;
00120 else
00121 vectSize = _config.getParam_vectSize();
00122 return static_cast<DistribGF&>(createDistrib(DistribType_GF, vectSize));
00123 }
00124
00125 Distrib& S::createDistrib(const DistribType type, unsigned long vectSize)
00126 {
00127 if (_vectSizeDefined && vectSize != _vectSize)
00128 throw Exception("Incompatible vectSize", __FILE__, __LINE__);
00129 Distrib& d = Distrib::create(K::k, type, vectSize);
00130 addDistribToDict(d);
00131 _vectSize = vectSize;
00132 _vectSizeDefined = true;
00133 return d;
00134 }
00135
00136 void S::addDistribToDict(Distrib& d)
00137 { d.dictIndex(K::k) = _distribDict.addDistrib(d); }
00138
00139 void S::addMixtureToDict(Mixture& m)
00140 { _mixtureDict.addMixture(m); }
00141
00142 Mixture& S::createMixture()
00143 {
00144 return createMixture(_config.getParam_mixtureDistribCount(),
00145 _config.getParam_distribType());
00146 }
00147
00148 Mixture& S::createMixture(unsigned long dc)
00149 { return createMixture(dc, _config.getParam_distribType()); }
00150
00151 MixtureGD& S::createMixtureGD()
00152 {
00153 Mixture& m = createMixture(_config.getParam_mixtureDistribCount(),
00154 DistribType_GD);
00155 return static_cast<MixtureGD&>(m);
00156 }
00157
00158 MixtureGF& S::createMixtureGF()
00159 {
00160 Mixture& m = createMixture(_config.getParam_mixtureDistribCount(),
00161 DistribType_GF);
00162 return static_cast<MixtureGF&>(m);
00163 }
00164
00165 MixtureGD& S::createMixtureGD(unsigned long dc)
00166 { return static_cast<MixtureGD&>(createMixture(dc, DistribType_GD)); }
00167
00168 MixtureGF& S::createMixtureGF(unsigned long dc)
00169 { return static_cast<MixtureGF&>(createMixture(dc, DistribType_GF)); }
00170
00171 Mixture& S::createMixture(unsigned long dc, DistribType type)
00172 {
00173 unsigned long vectSize;
00174 if (_vectSizeDefined)
00175 vectSize = _vectSize;
00176 else
00177 vectSize = _config.getParam_vectSize();
00178 Mixture& m = Mixture::create(K::k, dc, type, newId(), vectSize);
00179 addMixtureToDict(m);
00180 const unsigned long n = m.getDistribCount();
00181 for (unsigned long c=0; c<n; c++)
00182 { addDistribToDict(m.getDistrib(c)); }
00183 _vectSize = vectSize;
00184 _vectSizeDefined = true;
00185 return m;
00186 }
00187
00188 String S::newId()
00189 {
00190 String id;
00191 do { id = "#" + String::valueOf(++_lastMixtureId); }
00192 while (_mixtureDict.getIndexOfId(id) != -1);
00193 return id;
00194 }
00195
00196 void S::setMixtureId(Mixture& mixture, const String& id)
00197 {
00198
00199
00200
00201 _mixtureDict.setMixtureId(mixture, id);
00202 }
00203
00204 Mixture& S::duplicateMixture(const Mixture& mixture, DuplDistrib d)
00205 {
00206 Mixture& m = mixture.duplicate(K::k, d);
00207 String id(mixture.getId());
00208 unsigned long cpt = 1;
00209 while (_mixtureDict.getIndexOfId(id) != -1)
00210 id = mixture.getId() + " #" + String::valueOf(++cpt);
00211 m.setId(K::k, id);
00212 addMixtureToDict(m);
00213
00214 if (d == DUPL_DISTRIB)
00215 {
00216 const unsigned long n= m.getDistribCount();
00217 for (unsigned long c=0; c<n; c++)
00218 { addDistribToDict(m.getDistrib(c)); }
00219 }
00220 return m;
00221 }
00222
00223 MixtureGD& S::duplicateMixture(const MixtureGD& m, DuplDistrib d)
00224 {
00225 return static_cast<MixtureGD&>(duplicateMixture(
00226 static_cast<const Mixture&>(m), d));
00227 }
00228
00229 MixtureGF& S::duplicateMixture(const MixtureGF& m, DuplDistrib d)
00230 {
00231 return static_cast<MixtureGF&>(duplicateMixture(
00232 static_cast<const Mixture&>(m), d));
00233 }
00234
00235 MixtureGD& S::duplicateMixtureGD(const Mixture& m, DuplDistrib d)
00236 { return static_cast<MixtureGD&>(duplicateMixture(m, d)); }
00237
00238 MixtureGF& S::duplicateMixtureGF(const Mixture& m, DuplDistrib d)
00239 { return static_cast<MixtureGF&>(duplicateMixture(m, d)); }
00240
00241 DistribGD& S::duplicateDistrib(const DistribGD& d)
00242 {
00243 return static_cast<DistribGD&>(duplicateDistrib(
00244 static_cast<const Distrib&>(d)));
00245 }
00246
00247 DistribGF& S::duplicateDistrib(const DistribGF& d)
00248 {
00249 return static_cast<DistribGF&>(duplicateDistrib(
00250 static_cast<const Distrib&>(d)));
00251 }
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261 Distrib& S::duplicateDistrib(const Distrib& distrib)
00262 {
00263 Distrib& d = distrib.duplicate(K::k);
00264 addDistribToDict(d);
00265 return d;
00266 }
00267
00268 long S::getMixtureIndex(const String& id) const
00269 { return _mixtureDict.getIndexOfId(id); }
00270
00271 void S::setDistribToMixture(Mixture& m, Distrib& d, weight_t w,
00272 unsigned long i)
00273 {
00274 m.setDistrib(K::k, d, i);
00275 m.weight(i) = w;
00276 }
00277
00278 void S::addDistribToMixture(Mixture& m, Distrib& d, weight_t w)
00279 { m.addDistrib(K::k, d, w); }
00280
00281 MixtureGD& S::loadMixtureGD(const FileName& f)
00282 { return static_cast<MixtureGD&>(loadMixture(f, DistribType_GD)); }
00283
00284 MixtureGF& S::loadMixtureGF(const FileName& f)
00285 { return static_cast<MixtureGF&>(loadMixture(f, DistribType_GF)); }
00286
00287 Mixture& S::loadMixture(const FileName& f)
00288 {
00289 MixtureFileReader r(f, _config);
00290 const Mixture& m0 = r.readMixture();
00291 if (!_config.existsParam_vectSize)
00292 const_cast<Config&>(_config)
00293 .setParam("vectSize", String::valueOf(m0.getVectSize()));
00294 Mixture& m = createMixture(m0.getDistribCount(), m0.getType());
00295 m = m0;
00296 autoSetMixtureId(m, f);
00297 return m;
00298 }
00299
00300 void S::loadMixture(Mixture& m, const FileName& f)
00301 {
00302 MixtureFileReader r(f, _config);
00303 m = r.readMixture();
00304 autoSetMixtureId(m, f);
00305 }
00306
00307 Mixture& S::loadMixture(const FileName& f, DistribType type)
00308 {
00309 MixtureFileReader r(f, _config);
00310 const Mixture& m0 = r.readMixture(type);
00311 if (!_config.existsParam_vectSize)
00312 const_cast<Config&>(_config)
00313 .setParam("vectSize", String::valueOf(m0.getVectSize()));
00314 Mixture& m = createMixture(m0.getDistribCount(), type);
00315 m = m0;
00316 autoSetMixtureId(m, f);
00317 return m;
00318 }
00319
00320 void S::autoSetMixtureId(Mixture& m, String id)
00321 {
00322 const String f = id;
00323 unsigned long cpt = 1;
00324 while (_mixtureDict.getIndexOfId(id) != -1)
00325 id = f + " #" + String::valueOf(++cpt);
00326 _mixtureDict.setMixtureId(m, id);
00327 }
00328
00329 unsigned long S::loadMixture(const XLine& l)
00330 {
00331 unsigned long first = getMixtureCount();
00332 for (unsigned long i=0; i<l.getElementCount(); i++)
00333 loadMixture(l.getElement(i));
00334 return first;
00335 }
00336
00337 unsigned long S::getDistribCount() const { return _distribDict.size(); }
00338
00339 unsigned long S::getVectSize() const
00340 {
00341 if (_vectSizeDefined)
00342 return _vectSize;
00343 throw Exception("Unknown vectSize", __FILE__, __LINE__);
00344 }
00345
00346 Distrib& S::getDistrib(unsigned long i) const
00347 { return _distribDict.getDistrib(i); }
00348
00349 DistribGD& S::getDistribGD(unsigned long i) const
00350 {
00351 DistribGD* p = dynamic_cast<DistribGD*>(&_distribDict.getDistrib(i));
00352 if (p == NULL)
00353 throw Exception("The distrib found is not a GD", __FILE__, __LINE__);
00354 return *p;
00355 }
00356
00357 DistribGF& S::getDistribGF(unsigned long i) const
00358 {
00359 DistribGF* p = dynamic_cast<DistribGF*>(&_distribDict.getDistrib(i));
00360 if (p == NULL)
00361 throw Exception("The distrib found is not a GF", __FILE__, __LINE__);
00362 return *p;
00363 }
00364
00365 unsigned long S::getMixtureCount() const { return _mixtureDict.size(); }
00366
00367 Mixture& S::getMixture(unsigned long i) const
00368 { return _mixtureDict.getMixture(i); }
00369
00370 MixtureGD& S::getMixtureGD(unsigned long i) const
00371 {
00372 MixtureGD* p = dynamic_cast<MixtureGD*>(&_mixtureDict.getMixture(i));
00373 if (p == NULL)
00374 throw Exception("The mixture found is not a GD", __FILE__, __LINE__);
00375 return *p;
00376 }
00377
00378 MixtureGF& S::getMixtureGF(unsigned long i) const
00379 {
00380 MixtureGF* p = dynamic_cast<MixtureGF*>(&_mixtureDict.getMixture(i));
00381 if (p == NULL)
00382 throw Exception("The mixture found is not a GF", __FILE__, __LINE__);
00383 return *p;
00384 }
00385
00386 const String& S::getServerName() const { return _serverName; }
00387
00388 void S::setServerName(const String& s) { _serverName = s; }
00389
00390 void S::load(const FileName& f)
00391 { MixtureServerFileReader(f, _config).readMixtureServer(*this); }
00392
00393 void S::deleteMixtures(unsigned long first, unsigned long last)
00394 {
00395 _mixtureDict.deleteMixtures(first, last);
00396 if (getDistribCount() == 0 && getMixtureCount() == 0)
00397 _vectSizeDefined = false;
00398 }
00399
00400 void S::deleteMixture(const Mixture& m)
00401 {
00402 _mixtureDict.deleteMixture(m);
00403 if (getDistribCount() == 0 && getMixtureCount() == 0)
00404 _vectSizeDefined = false;
00405 }
00406
00407 void S::deleteUnusedDistribs()
00408 {
00409 _distribDict.deleteUnreferencedDistribs();
00410 if (getDistribCount() == 0 && getMixtureCount() == 0)
00411 _vectSizeDefined = false;
00412 }
00413
00414 void S::save(const FileName& f) const
00415 { MixtureServerFileWriter(f, _config).writeMixtureServer(*this); }
00416
00417 String S::toString() const
00418 {
00419 String s = Object::toString()
00420 + "\n serverName = '" + _serverName + "'"
00421 + "\n distribCount = " + String::valueOf(getDistribCount())
00422 + "\n mixtureCount = " + String::valueOf(getMixtureCount());
00423 for (unsigned long i=0; i<getMixtureCount(); i++)
00424 s += "\n mixture #" + String::valueOf(i)
00425 + " Id = " + getMixture(i).getId();
00426 return s;
00427 }
00428
00429 String S::getClassName() const { return "MixtureServer"; }
00430
00431 S::~MixtureServer() {}
00432
00433
00434 #endif // !defined(ALIZE_MixtureServer_cpp)
00435