LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
MediaProcessors.cpp
Go to the documentation of this file.
1// Copyright 2002-2021 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6#include "MediaProcessors.h"
7
8#include "MediaProcessUnit.h"
10
15#include <iostream>
16
17
18using namespace std;
20using namespace Lima::Common::MediaticData;
21
22namespace Lima {
23
24class MediaProcessorsPrivate
25{
26 friend class MediaProcessors;
27
28 MediaProcessorsPrivate();
29 MediaProcessorsPrivate(const MediaProcessorsPrivate& /*mpp*/) {}
30 virtual ~MediaProcessorsPrivate() {}
31
32 std::map<MediaId,MediaProcessUnit::Manager*> m_pipelineManagers;
33 std::map<std::string,std::map<MediaId,std::shared_ptr<MediaProcessUnitPipeline>> > m_pipelines;
34
35// std::map<MediaId,MediaAnalysisDumper::Manager*> m_dumperManagers;
36 std::map<std::string,std::map<MediaId,std::shared_ptr<MediaAnalysisDumper> > > m_analysisDumpers;
37
38 // The pipelines of the medias re-initialized by initMedia, to instantiate
39 // again in initPipelines
40 std::map<MediaId,std::set<std::string> > m_pipelinesToRestore;
41
45 std::set<std::string> release(MediaId med);
46
49 bool initPipeline(const std::string& id,
50 const std::map<std::string,std::string>& pipelineMapping,
51 MediaId med,
53
54 // recursive read of configuration files
57
58
59};
60
61MediaProcessorsPrivate::MediaProcessorsPrivate() :
62 m_pipelineManagers(), m_pipelines(), m_analysisDumpers()
63{
64}
65
66MediaProcessors::MediaProcessors() :
68 m_d(new MediaProcessorsPrivate())
69{
70}
71
72MediaProcessors::MediaProcessors(const MediaProcessors& mp) :
74 m_d(new MediaProcessorsPrivate(*mp.m_d))
75{
76}
77
79{
80 for (auto it = m_d->m_pipelineManagers.begin();
81 it != m_d->m_pipelineManagers.end(); it++ )
82 {
83 delete it->second;
84 it->second=0;
85 }
86 // for (std::map<MediaId,MediaAnalysisDumper::Manager*>::iterator it=m_dumperManagers.begin();
87 // it!=m_dumperManagers.end();
88 // it++)
89 // {
90 // delete it->second;
91 // it->second=0;
92 // }
93 delete m_d;
94}
95
96const std::map<MediaId,MediaProcessUnit::Manager*>& MediaProcessors::managers() const
97{
98 return m_d->m_pipelineManagers;
99}
100
102 MediaId med,
104{
105#ifdef DEBUG_CD
107 LDEBUG << "MediaProcessors::initMedia" << med;
108#endif
109
110 // A media initialized again (e.g. by a new analyzer, possibly with other
111 // models): release its former process units before building the new ones,
112 // otherwise their models would never be freed. Its former pipelines are
113 // instantiated again by initPipelines.
114 if (m_d->m_pipelineManagers.find(med) != m_d->m_pipelineManagers.end())
115 {
116 auto previous = m_d->release(med);
117 m_d->m_pipelinesToRestore[med].insert(previous.begin(), previous.end());
118 }
119
121 lpuinit.media=med;
122 m_d->m_pipelineManagers[med] = new MediaProcessUnit::Manager(confProc,
123 lpuinit);
124
125 // MediaAnalysisDumperInitializationParameters ladinit;
126 // ladinit.media=med;
127 // m_dumperManagers[lang]=new MediaAnalysisDumper::Manager(confDump,ladinit);
128
129 // Recursive init for 'Processors'
130 auto& module = m_d->m_pipelineManagers[med]->getModuleConfigurationStructure();
131 m_d->includeProcessors(module, module);
132}
133
136 const std::deque<std::string>& pipelines )
137{
138#ifdef DEBUG_CD
140#endif
141 std::deque<std::string> pipelinesToInit ( pipelines );
142 std::map<std::string, std::map<std::string, std::string> > allpipelines;
143 try
144 {
145 allpipelines = confGroup.getMaps();
146 }
147 catch ( NoSuchGroup& e )
148 {
150 LERROR << "no group 'declaration' to configure linguistic pipelines ";
151 throw InvalidConfiguration(e.what());
152 }
153
154 // if pipelines is empty, then initialize all pipelines
155 if ( pipelinesToInit.empty() )
156 {
157 for (auto mapItr = allpipelines.cbegin();
158 mapItr != allpipelines.cend(); mapItr++ )
159 {
160 pipelinesToInit.push_back( mapItr->first );
161 }
162 }
163
164#ifdef DEBUG_CD
165 LDEBUG << "MediaProcessors::initPipelines ";
166 for (auto pipItr = pipelinesToInit.cbegin();
167 pipItr != pipelinesToInit.cend(); pipItr++ )
168 {
169 LDEBUG << "\t" << *pipItr << ", ";
170 }
171#endif
172 // init pipelines for all available medias
173 for (auto pipItr = pipelinesToInit.cbegin();
174 pipItr != pipelinesToInit.cend(); pipItr++ )
175 {
176 // std::cout << "initialize pipeline type '" << *pipItr << "'" << std::endl;
177 const map<string,string>& pipelineMapping=allpipelines[*pipItr];
178 if ( pipelineMapping.empty() )
179 {
181 LERROR << "no pipeline '" << *pipItr
182 << "' defined in pipeline declaration module!";
183 continue;
184 }
185 for (auto mapItr = m_d->m_pipelineManagers.cbegin();
186 mapItr != m_d->m_pipelineManagers.cend(); mapItr++ )
187 {
188 m_d->initPipeline(*pipItr, pipelineMapping, mapItr->first, mapItr->second);
189 }
190 }
191
192 // Restore, for the medias initialized again, the pipelines they had and
193 // that were not requested here (only for them: not to load them for the
194 // other medias)
195 std::set<std::string> initialized(pipelinesToInit.begin(), pipelinesToInit.end());
196 for (const auto& [med, ids]: m_d->m_pipelinesToRestore)
197 {
198 auto managerItr = m_d->m_pipelineManagers.find(med);
199 if (managerItr == m_d->m_pipelineManagers.end())
200 continue;
201 for (const auto& id: ids)
202 {
203 if (initialized.find(id) != initialized.end())
204 continue;
205 auto mappingItr = allpipelines.find(id);
206 if (mappingItr != allpipelines.end())
207 m_d->initPipeline(id, mappingItr->second, med, managerItr->second);
208 }
209 }
210 m_d->m_pipelinesToRestore.clear();
211}
212
213bool MediaProcessorsPrivate::initPipeline(
214 const std::string& id,
215 const std::map<std::string,std::string>& pipelineMapping,
216 MediaId med,
218{
219 const std::string& mediaStr=MediaticData::single().getMediaId ( med );
220 auto entryItr = pipelineMapping.find ( mediaStr );
221 if ( entryItr == pipelineMapping.end() )
222 {
224 LINFO << "no pipeline '" << id << "' for media " << mediaStr;
225 return false;
226 }
227#ifdef DEBUG_CD
229 LDEBUG << "MediaProcessors::initPipelines instanciating"
230 << entryItr->second;
231#endif
232 std::shared_ptr<MediaProcessUnit> pu = nullptr;
233 try
234 {
235 pu = manager->getObject( entryItr->second );
236 }
237 catch (const InvalidConfiguration& e)
238 {
239 }
240 if (pu == nullptr)
241 {
243 LERROR << "no process unit '" << entryItr->second << "' for media " << mediaStr;
244 return false;
245 }
246 auto pipeline = std::dynamic_pointer_cast<MediaProcessUnitPipeline> ( pu );
247 if ( pipeline==nullptr )
248 {
250 LERROR << "pipeline '" << id << "' for media "
251 << mediaStr << " is not of type ProcessUnitPipeline!";
252 return false;
253 }
254 m_pipelines[id][med]=pipeline;
255 return true;
256}
257
258std::set<std::string> MediaProcessorsPrivate::release(MediaId med)
259{
260 std::set<std::string> ids;
261 for (auto& [id, pipelines]: m_pipelines)
262 {
263 if (pipelines.erase(med) > 0)
264 ids.insert(id);
265 }
266 for (auto& [type, dumpers]: m_analysisDumpers)
267 dumpers.erase(med);
268 auto managerItr = m_pipelineManagers.find(med);
269 if (managerItr != m_pipelineManagers.end())
270 {
271 delete managerItr->second;
272 m_pipelineManagers.erase(managerItr);
273 }
274 return ids;
275}
276
278{
280 LINFO << "MediaProcessors::releaseMedia" << med;
281 m_d->release(med);
282 m_d->m_pipelinesToRestore.erase(med);
283}
284
285
286std::shared_ptr<MediaProcessUnitPipeline> MediaProcessors::getPipelineForId (
287 MediaId med,
288 const std::string& id ) const
289{
290 auto pipItr = m_d->m_pipelines.find ( id );
291 if ( pipItr == m_d->m_pipelines.end() )
292 {
294 LERROR << "no pipeline type '" << id << "' defined in media " << med
295 << " configuration file !";
296 return nullptr;
297 }
298 auto medItr = pipItr->second.find ( med );
299 if ( medItr==pipItr->second.end() )
300 {
302 LERROR << "no media [" << med << ":"
303 << MediaticData::single().getMediaId(med).c_str()
304 << "] defined for pipeline '" << id.c_str()
305 << "' in the configuration file!";
306 return nullptr;
307 }
308 return medItr->second;
309}
310
311std::shared_ptr<MediaAnalysisDumper> MediaProcessors::getAnalysisDumperForId (
312 MediaId med,
313 const std::string& type ) const
314{
316 auto dumpItr = m_d->m_analysisDumpers.find ( type );
317 if ( dumpItr == m_d->m_analysisDumpers.end() )
318 {
319 LWARN << "no dumper type '" << type << "' defined in media " << med
320 << " configuration file ";
321 return nullptr;
322 }
323 auto medItr = dumpItr->second.find ( med );
324 if ( medItr==dumpItr->second.end() )
325 {
326 LWARN << "no media [" << med << ":"
327 << MediaticData::single().getMediaId(med) << "] defined for dumper '"
328 << type << "' in the configuration file !";
329 return nullptr;
330 }
331 return medItr->second;
332}
333
334void MediaProcessors::getAvailableMedia ( std::set<MediaId>& ids ) const
335{
336 ids.clear();
337 for ( auto & kv : m_d->m_pipelines )
338 {
339 for ( auto & id2pipeline : kv.second )
340 {
341 ids.insert(id2pipeline.first);
342 }
343 }
344}
345
346void MediaProcessors::getAvailablePipelinesForMedia ( MediaId id, std::set<std::string>& pipelines ) const
347{
348 pipelines.clear();
349 for ( auto & kv : m_d->m_pipelines )
350 {
351 if (kv.second.cend() != kv.second.find(id))
352 {
353 pipelines.insert(kv.first);
354 }
355 }
356}
357
358// two arguments: first is the module to modify, second is the module in which
359// the 'include' group can be found : both arguments can be the same
360void MediaProcessorsPrivate::includeProcessors(
363{
364 try
365 {
366 auto includeList = includeModule.getListValuesAtKeyOfGroupNamed("includeList",
367 "include");
368 for (auto it = includeList.cbegin(); it!=includeList.cend(); it++)
369 {
370 string::size_type i=(*it).find("/");
371 if ( i== string::npos)
372 {
373 //PROCESSORSLOGINIT;
374 //LERROR << "Cannot include processors " << *it
375 // << ": must specify file and module name";
376 continue;
377 }
378 QString fileName;
379 std::string moduleName("");
380 try
381 {
382 //PROCESSORSLOGINIT;
383 //LDEBUG << "i="<< i;
385 std::string((*it),0,i).c_str());
386 //LDEBUG << "filename="<< fileName;
387 moduleName = std::string((*it),i+1);
388 //LDEBUG << "moduleName="<< moduleName;
389 XMLConfigurationFileParser parser(fileName);
390 ModuleConfigurationStructure& newMod = parser.getModuleConfiguration(moduleName);
391 module.addModule(newMod);
392 //LDEBUG << "added module";
393 //ostringstream oss;
394 //for (auto it = module.cbegin(); it != module.cend(); it++)
395 //{
396 // oss << (*it).first << ";";
397 //}
398 //LDEBUG << "module contain following groups: " << oss.str();
399 // recursive inclusions
400
401 // 'include' group is not inserted in module by addModule function because a group
402 // with same name was already there : use the one in newMod
403 includeProcessors(module,newMod);
404 }
405 catch (NoSuchModule& )
406 {
407 //PROCESSORSLOGINIT;
408 //LERROR << "Cannot find module " << moduleName
409 // << " in file " << fileName;
410 }
411 catch (std::exception& )
412 {
413 //PROCESSORSLOGINIT;
414 //LERROR << "Error trying to find module " << moduleName
415 // << " in file " << fileName << ":" << e.what();
416 }
417 }
418 }
419 catch (NoSuchList& ) { } // do nothing: optional
420 catch (NoSuchGroup& ) { } // do nothing: optional
421}
422
423
424} // Lima
#define LWARN
Definition LimaCommon.h:160
#define LDEBUG
Definition LimaCommon.h:157
#define LINFO
Definition LimaCommon.h:158
#define LERROR
Definition LimaCommon.h:161
#define PROCESSORSLOGINIT
Definition LimaCommon.h:211
const std::map< std::string, std::map< std::string, std::string > > & getMaps()
std::deque< std::string > & getListValuesAtKeyOfGroupNamed(const std::string &key, const std::string &name)
return a message when a 'group' was not found
Parser class for the lima's xml configuration files.
InitializableObjectManager< MediaProcessUnit, MediaProcessUnitInitializationParameters > Manager
Use this exception to signal an error in one of the configuration files.
Definition LimaCommon.h:345
virtual const char * what() const override
Definition LimaCommon.h:280
void getAvailableMedia(std::set< MediaId > &ids) const
void initPipelines(Common::XMLConfigurationFiles::GroupConfigurationStructure &confModule, const std::deque< std::string > &pipelines)
void initMedia(MediaId, Common::XMLConfigurationFiles::ModuleConfigurationStructure &procConf)
(re)initialize the process units manager of a media.
void getAvailablePipelinesForMedia(MediaId id, std::set< std::string > &pipelines) const
std::shared_ptr< MediaAnalysisDumper > getAnalysisDumperForId(MediaId med, const std::string &type) const
get the dumper for the given id and media
const std::map< MediaId, MediaProcessUnit::Manager * > & managers() const
std::shared_ptr< MediaProcessUnitPipeline > getPipelineForId(MediaId med, const std::string &id) const
get the pipeline for the given id and media
void releaseMedia(MediaId med)
release the pipelines and the process units (and thus the models they loaded) of a media.
virtual std::shared_ptr< Object > getObject(const std::string &id)
If object doesn't exists, call the create method.
Defines access method for the singleton pattern.
Definition Singleton.h:22
static const MediaticData & single()
Definition Singleton.h:51
originally detectModuleConfigurationStructure.h in detectlibraries
QString findFileInPaths(const QString &paths, const QString &fileName, const QChar &separator)
Find the given file in the given paths.
NAUTITIA.
STL namespace.
launch exception related to the configuration file parsing