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
25{
26 friend class MediaProcessors;
27
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 // recursive read of configuration files
41
42
43};
44
45MediaProcessorsPrivate::MediaProcessorsPrivate() :
46 m_pipelineManagers(), m_pipelines(), m_analysisDumpers()
47{
48}
49
50MediaProcessors::MediaProcessors() :
52 m_d(new MediaProcessorsPrivate())
53{
54}
55
56MediaProcessors::MediaProcessors(const MediaProcessors& mp) :
58 m_d(new MediaProcessorsPrivate(*mp.m_d))
59{
60}
61
63{
64 for (auto it = m_d->m_pipelineManagers.begin();
65 it != m_d->m_pipelineManagers.end(); it++ )
66 {
67 delete it->second;
68 it->second=0;
69 }
70 // for (std::map<MediaId,MediaAnalysisDumper::Manager*>::iterator it=m_dumperManagers.begin();
71 // it!=m_dumperManagers.end();
72 // it++)
73 // {
74 // delete it->second;
75 // it->second=0;
76 // }
77 delete m_d;
78}
79
80const std::map<MediaId,MediaProcessUnit::Manager*>& MediaProcessors::managers() const
81{
82 return m_d->m_pipelineManagers;
83}
84
86 MediaId med,
88{
89#ifdef DEBUG_CD
91 LDEBUG << "MediaProcessors::initMedia" << med;
92#endif
93
95 lpuinit.media=med;
96 m_d->m_pipelineManagers[med] = new MediaProcessUnit::Manager(confProc,
97 lpuinit);
98
99 // MediaAnalysisDumperInitializationParameters ladinit;
100 // ladinit.media=med;
101 // m_dumperManagers[lang]=new MediaAnalysisDumper::Manager(confDump,ladinit);
102
103 // Recursive init for 'Processors'
104 auto& module = m_d->m_pipelineManagers[med]->getModuleConfigurationStructure();
105 m_d->includeProcessors(module, module);
106}
107
110 const std::deque<std::string>& pipelines )
111{
112#ifdef DEBUG_CD
114#endif
115 std::deque<std::string> pipelinesToInit ( pipelines );
116 std::map<std::string, std::map<std::string, std::string> > allpipelines;
117 try
118 {
119 allpipelines = confGroup.getMaps();
120 }
121 catch ( NoSuchGroup& e )
122 {
124 LERROR << "no group 'declaration' to configure linguistic pipelines ";
125 throw InvalidConfiguration(e.what());
126 }
127
128 // if pipelines is empty, then initialize all pipelines
129 if ( pipelinesToInit.empty() )
130 {
131 for (auto mapItr = allpipelines.cbegin();
132 mapItr != allpipelines.cend(); mapItr++ )
133 {
134 pipelinesToInit.push_back( mapItr->first );
135 }
136 }
137
138#ifdef DEBUG_CD
139 LDEBUG << "MediaProcessors::initPipelines ";
140 for (auto pipItr = pipelinesToInit.cbegin();
141 pipItr != pipelinesToInit.cend(); pipItr++ )
142 {
143 LDEBUG << "\t" << *pipItr << ", ";
144 }
145#endif
146 // init pipelines for all available medias
147 for (auto pipItr = pipelinesToInit.cbegin();
148 pipItr != pipelinesToInit.cend(); pipItr++ )
149 {
150 // std::cout << "initialize pipeline type '" << *pipItr << "'" << std::endl;
151 const map<string,string>& pipelineMapping=allpipelines[*pipItr];
152 if ( pipelineMapping.empty() )
153 {
155 LERROR << "no pipeline '" << *pipItr
156 << "' defined in pipeline declaration module!";
157 continue;
158 }
159 for (auto mapItr = m_d->m_pipelineManagers.cbegin();
160 mapItr != m_d->m_pipelineManagers.cend(); mapItr++ )
161 {
162 const std::string& mediaStr=MediaticData::single().getMediaId ( mapItr->first );
163 auto entryItr = pipelineMapping.find ( mediaStr );
164 if ( entryItr == pipelineMapping.end() )
165 {
167 LINFO << "no pipeline '" << *pipItr << "' for media " << mediaStr;
168 continue;
169 }
170#ifdef DEBUG_CD
171 LDEBUG << "MediaProcessors::initPipelines instanciating"
172 << entryItr->second;
173#endif
174 std::shared_ptr<MediaProcessUnit> pu = nullptr;
175 try
176 {
177 pu = mapItr->second->getObject( entryItr->second );
178 }
179 catch (const InvalidConfiguration& e)
180 {
181 }
182 if (pu == nullptr)
183 {
185 LERROR << "no process unit '" << entryItr->second << "' for media " << mediaStr;
186 continue;
187 }
188 auto pipeline = std::dynamic_pointer_cast<MediaProcessUnitPipeline> ( pu );
189 if ( pipeline==nullptr )
190 {
192 LERROR << "pipeline '" << *pipItr << "' for media "
193 << mediaStr << " is not of type ProcessUnitPipeline!";
194 continue;
195 }
196 m_d->m_pipelines[*pipItr][mapItr->first]=pipeline;
197 }
198 }
199}
200
201
202std::shared_ptr<MediaProcessUnitPipeline> MediaProcessors::getPipelineForId (
203 MediaId med,
204 const std::string& id ) const
205{
206 auto pipItr = m_d->m_pipelines.find ( id );
207 if ( pipItr == m_d->m_pipelines.end() )
208 {
210 LERROR << "no pipeline type '" << id << "' defined in media " << med
211 << " configuration file !";
212 return nullptr;
213 }
214 auto medItr = pipItr->second.find ( med );
215 if ( medItr==pipItr->second.end() )
216 {
218 LERROR << "no media [" << med << ":"
219 << MediaticData::single().getMediaId(med).c_str()
220 << "] defined for pipeline '" << id.c_str()
221 << "' in the configuration file!";
222 return nullptr;
223 }
224 return medItr->second;
225}
226
227std::shared_ptr<MediaAnalysisDumper> MediaProcessors::getAnalysisDumperForId (
228 MediaId med,
229 const std::string& type ) const
230{
232 auto dumpItr = m_d->m_analysisDumpers.find ( type );
233 if ( dumpItr == m_d->m_analysisDumpers.end() )
234 {
235 LWARN << "no dumper type '" << type << "' defined in media " << med
236 << " configuration file ";
237 return nullptr;
238 }
239 auto medItr = dumpItr->second.find ( med );
240 if ( medItr==dumpItr->second.end() )
241 {
242 LWARN << "no media [" << med << ":"
243 << MediaticData::single().getMediaId(med) << "] defined for dumper '"
244 << type << "' in the configuration file !";
245 return nullptr;
246 }
247 return medItr->second;
248}
249
250void MediaProcessors::getAvailableMedia ( std::set<MediaId>& ids ) const
251{
252 ids.clear();
253 for ( auto & kv : m_d->m_pipelines )
254 {
255 for ( auto & id2pipeline : kv.second )
256 {
257 ids.insert(id2pipeline.first);
258 }
259 }
260}
261
262void MediaProcessors::getAvailablePipelinesForMedia ( MediaId id, std::set<std::string>& pipelines ) const
263{
264 pipelines.clear();
265 for ( auto & kv : m_d->m_pipelines )
266 {
267 if (kv.second.cend() != kv.second.find(id))
268 {
269 pipelines.insert(kv.first);
270 }
271 }
272}
273
274// two arguments: first is the module to modify, second is the module in which
275// the 'include' group can be found : both arguments can be the same
276void MediaProcessorsPrivate::includeProcessors(
279{
280 try
281 {
282 auto includeList = includeModule.getListValuesAtKeyOfGroupNamed("includeList",
283 "include");
284 for (auto it = includeList.cbegin(); it!=includeList.cend(); it++)
285 {
286 string::size_type i=(*it).find("/");
287 if ( i== string::npos)
288 {
289 //PROCESSORSLOGINIT;
290 //LERROR << "Cannot include processors " << *it
291 // << ": must specify file and module name";
292 continue;
293 }
294 QString fileName;
295 std::string moduleName("");
296 try
297 {
298 //PROCESSORSLOGINIT;
299 //LDEBUG << "i="<< i;
301 std::string((*it),0,i).c_str());
302 //LDEBUG << "filename="<< fileName;
303 moduleName = std::string((*it),i+1);
304 //LDEBUG << "moduleName="<< moduleName;
305 XMLConfigurationFileParser parser(fileName);
306 ModuleConfigurationStructure& newMod = parser.getModuleConfiguration(moduleName);
307 module.addModule(newMod);
308 //LDEBUG << "added module";
309 //ostringstream oss;
310 //for (auto it = module.cbegin(); it != module.cend(); it++)
311 //{
312 // oss << (*it).first << ";";
313 //}
314 //LDEBUG << "module contain following groups: " << oss.str();
315 // recursive inclusions
316
317 // 'include' group is not inserted in module by addModule function because a group
318 // with same name was already there : use the one in newMod
319 includeProcessors(module,newMod);
320 }
321 catch (NoSuchModule& )
322 {
323 //PROCESSORSLOGINIT;
324 //LERROR << "Cannot find module " << moduleName
325 // << " in file " << fileName;
326 }
327 catch (std::exception& )
328 {
329 //PROCESSORSLOGINIT;
330 //LERROR << "Error trying to find module " << moduleName
331 // << " in file " << fileName << ":" << e.what();
332 }
333 }
334 }
335 catch (NoSuchList& ) { } // do nothing: optional
336 catch (NoSuchGroup& ) { } // do nothing: optional
337}
338
339
340} // 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
return a message when a 'module' is not found
Parser class for the lima's xml configuration files.
InitializableObjectManager< Base, InitializationParameters > Manager
Manager is the type of the Manager associated to the initializableObject.
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)
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
Defines access method for the singleton pattern.
Definition Singleton.h:22
static const MediaticData & single()
const singleton accessor
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