LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
ExternalProcessUnit.cpp
Go to the documentation of this file.
1// Copyright 2002-2013 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6/************************************************************************
7 * @author besancon (besanconr@zoe.cea.fr)
8 * @date Mon Jan 17 2011
9 ***********************************************************************/
10
11#include "ExternalProcessUnit.h"
12
13#include "LinguisticMetaData.h"
22
23//#include "boost/process.hpp"
24#include <QtCore/QProcess>
25
26#include <fstream>
27
28//namespace bp = ::boost::process;
29
30using namespace std;
31
32namespace Lima {
33namespace LinguisticProcessing {
34
36
39m_dumper(),
40m_loader(),
41m_commandLine(),
42m_inputSuffix(),
43m_outputSuffix()
44{
45}
46
50
51
54 Manager* manager)
55
56{
57 LOGINIT("LP::External");
58 LDEBUG << "Initialization";
59
60 MediaId language=manager->getInitializationParameters().media;
61 try {
62 string dumperName=unitConfiguration.getParamsValueAtKey("dumper");
63 // create the dumper
64 m_dumper=manager->getObject(dumperName);
65 }
67 LERROR << "Missing 'dumper' parameter in ExternalProcessUnit group for language "
68 << (int)language << " !";
70 }
71
72 try {
73 string loaderName=unitConfiguration.getParamsValueAtKey("loader");
74 // create the loader
75 m_loader=manager->getObject(loaderName);
76 }
77 catch (InvalidConfiguration& ) {
78 m_loader = 0;
79 }
81 LERROR << "Missing 'loader' parameter in ExternalProcessUnit group for language "
82 << (int)language << " !";
84 }
85
86 try {
87 m_inputSuffix=QString::fromUtf8(unitConfiguration.getParamsValueAtKey("inputSuffix").c_str());
88 }
90 // optional parameter: keep default value
91 }
92
93 try {
94 m_outputSuffix=QString::fromUtf8(unitConfiguration.getParamsValueAtKey("outputSuffix").c_str());
95 }
97 // optional parameter: keep default value
98 }
99
100 try {
101 m_commandLine=QString::fromUtf8(unitConfiguration.getParamsValueAtKey("command").c_str());
102 }
104 LERROR << "Missing 'command' parameter in ExternalProcessUnit group for language "
105 << (int)language << " !";
106 throw InvalidConfiguration();
107 }
108}
109
111{
113 LOGINIT("LP::External");
114 LINFO << "ExternalProcessUnit: start";
115
116 auto metadata = std::dynamic_pointer_cast<LinguisticMetaData>(analysis.getData("LinguisticMetaData"));
117 if (metadata == 0) {
118 LERROR << "no LinguisticMetaData ! abort";
119 return MISSING_DATA;
120 }
121
122 LimaStatusCode returnCode(SUCCESS_ID);
123
124 // produce temporary file with the given dumper
125 LDEBUG << "ExternalProcessUnit: write tmp file";
126 returnCode=m_dumper->process(analysis);
127 if (returnCode!=SUCCESS_ID) {
128 LERROR << "ExternalProcessUnit: failed to dump data to temporary file";
129 return returnCode;
130 }
131
132 QString fileName = QString::fromUtf8(metadata->getMetaData("FileName").c_str());
133 QString inputFilename, outputFilename;
134 // apply command line
135 LDEBUG << "ExternalProcessUnit: apply external program";
136 QString commandLine = m_commandLine;
137 if (!m_inputSuffix.isEmpty())
138 {
139 inputFilename = fileName+ m_inputSuffix;
140 }
141 if (!m_outputSuffix.isEmpty())
142 {
143 outputFilename = fileName + m_outputSuffix;
144 }
145 commandLine = commandLine.arg(inputFilename, outputFilename);
146 auto args = commandLine.split(" ");
147 auto command = args.front();
148 args.pop_front();
149 LDEBUG << "Launching " << commandLine;
150 int processResult = QProcess::execute(command, args);
151 switch (processResult) {
152 case -2 :
153 LERROR << "ExternalProcessUnit: Was not able to start '" << commandLine << "'" ;
154 return returnCode;
155 case -1 :
156 LERROR << "ExternalProcessUnit: '" << commandLine << "' crashed!";
157 return returnCode;
158 case 0 :
159 break;
160 default:
161 LERROR << "ExternalProcessUnit: '" << commandLine << "' returned error status:" << processResult;
162 return returnCode;
163 }
164
165 if (m_loader != 0) {
166 // load results from the external program with the given loader
167 LDEBUG << "ExternalProcessUnit: read results";
168 returnCode=m_loader->process(analysis);
169 if (returnCode!=SUCCESS_ID) {
170 LERROR << "ExternalProcessUnit: failed to load data from temporary file";
171 return returnCode;
172 }
173 }
174 else {
175 LWARN << "ExternalProcessUnit: no loader defined for the current external process unit";
176 }
177
178 TimeUtils::logElapsedTime("ExternalProcessUnit");
179 return returnCode;
180}
181
182
183} // end namespace
184} // end namespace
This file is the main header file for the data related to annotation graphs.
#define EXTERNALPROCESSUNIT_CLASSID
#define LWARN
Definition LimaCommon.h:160
#define LOGINIT(X)
Definition LimaCommon.h:187
#define LDEBUG
Definition LimaCommon.h:157
#define LINFO
Definition LimaCommon.h:158
#define LERROR
Definition LimaCommon.h:161
Defines a Factory to create Object of type Base.
Holds all data that pass through the ProcessUnits Analysis data are shared pointers,...
std::shared_ptr< AnalysisData > getData(const QString &id)
return AnalysisData by id
return a message when a 'param' was not found
Manage initialization of InitializableObjects using configuration module and parameters.
const InitializationParameters & getInitializationParameters() const
get Initialization Parameters
Use this exception to signal an error in one of the configuration files.
Definition LimaCommon.h:345
LimaStatusCode process(AnalysisContent &analysis) const override
Process on data in analysisContent.
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
virtual std::shared_ptr< Object > getObject(const std::string &id)
If object doesn't exists, call the create method.
static void logElapsedTime(const std::string &mess, const std::string &taskCategory=std::string(""))
log the number of microseconds since last UpdateCurrentTime
static void updateCurrentTime(const std::string &taskCategory=std::string(""))
store current time for new elapsed time computation
SimpleFactory< MediaProcessUnit, ExternalProcessUnit > ExternalProcessUnitFactory(EXTERNALPROCESSUNIT_CLASSID)
NAUTITIA.
LimaStatusCode
Definition LimaCommon.h:236
@ SUCCESS_ID
Definition LimaCommon.h:237
@ MISSING_DATA
Definition LimaCommon.h:243
STL namespace.
launch exception related to the configuration file parsing