2 #include <boost/python.hpp>
12 #include <boost/filesystem.hpp>
38 return getPathsImpl();
51 std::string
generate (
const std::string& iOutputFormatString,
56 return generateImpl (iNbOfDraws, lOutputFormatEnum);
62 std::string
search (
const std::string& iOutputFormatString,
63 const std::string& iTravelQuery) {
67 return searchImpl (iTravelQuery, lOutputFormatEnum);
74 std::string getPathsImpl() {
75 std::ostringstream oPythonLogStr;
78 if (_logOutputStream == NULL) {
79 oPythonLogStr <<
"The log filepath is not valid." << std::endl;
80 return oPythonLogStr.str();
82 assert (_logOutputStream != NULL);
87 *_logOutputStream <<
"Get the file-path details" << std::endl;
89 if (_opentrepService == NULL) {
90 oPythonLogStr <<
"The OpenTREP service has not been initialised, "
91 <<
"i.e., the init() method has not been called "
92 <<
"correctly on the OpenTrepSearcher object. Please "
93 <<
"check that all the parameters are not empty and "
94 <<
"point to actual files.";
95 *_logOutputStream << oPythonLogStr.str();
96 return oPythonLogStr.str();
98 assert (_opentrepService != NULL);
103 const PORFilePath_T& lPORFilePath = lFilePathSet.first;
106 const TravelDBFilePath_T& lTravelDBFilePath = lDBFilePathPair.first;
107 const SQLDBConnectionString_T& lSQLDBConnStr = lDBFilePathPair.second;
110 oPythonLogStr << lPORFilePath <<
";" << lTravelDBFilePath
111 <<
";" << lSQLDBConnStr;
114 *_logOutputStream <<
"OPTD-maintained list of POR: '" << lPORFilePath
116 *_logOutputStream <<
"Xapian travel database/index: '"
117 << lTravelDBFilePath <<
"'" << std::endl;
118 *_logOutputStream <<
"SQL database connection string: '"
119 << lSQLDBConnStr <<
"'" << std::endl;
121 }
catch (
const RootException& eOpenTrepError) {
122 *_logOutputStream <<
"OpenTrep error: " << eOpenTrepError.what()
125 }
catch (
const std::exception& eStdError) {
126 *_logOutputStream <<
"Error: " << eStdError.what() << std::endl;
129 *_logOutputStream <<
"Unknown error" << std::endl;
133 return oPythonLogStr.str();
139 std::string indexImpl() {
140 std::ostringstream oPythonLogStr;
143 if (_logOutputStream == NULL) {
144 oPythonLogStr <<
"The log filepath is not valid." << std::endl;
145 return oPythonLogStr.str();
147 assert (_logOutputStream != NULL);
152 *_logOutputStream <<
"Indexation by Xapian" << std::endl;
154 if (_opentrepService == NULL) {
155 oPythonLogStr <<
"The OpenTREP service has not been initialised, "
156 <<
"i.e., the init() method has not been called "
157 <<
"correctly on the OpenTrepSearcher object. Please "
158 <<
"check that all the parameters are not empty and "
159 <<
"point to actual files.";
160 *_logOutputStream << oPythonLogStr.str();
161 return oPythonLogStr.str();
163 assert (_opentrepService != NULL);
168 const PORFilePath_T& lPORFilePath = lFilePathSet.first;
171 const TravelDBFilePath_T& lTravelDBFilePath = lDBFilePathPair.first;
172 const SQLDBConnectionString_T& lSQLDBConnStr = lDBFilePathPair.second;
175 *_logOutputStream <<
"OPTD-maintained list of POR: '" << lPORFilePath
177 *_logOutputStream <<
"Xapian travel database/index: '"
178 << lTravelDBFilePath <<
"'" << std::endl;
179 *_logOutputStream <<
"SQL database connection string: '"
180 << lSQLDBConnStr <<
"'" << std::endl;
186 oPythonLogStr << lNbOfEntries;
189 *_logOutputStream <<
"Xapian indexation yielded " << lNbOfEntries
190 <<
" POR (points of reference) entries." << std::endl;
192 }
catch (
const RootException& eOpenTrepError) {
193 *_logOutputStream <<
"OpenTrep error: " << eOpenTrepError.what()
196 }
catch (
const std::exception& eStdError) {
197 *_logOutputStream <<
"Error: " << eStdError.what() << std::endl;
200 *_logOutputStream <<
"Unknown error" << std::endl;
204 return oPythonLogStr.str();
210 std::string searchImpl(
const std::string& iTravelQuery,
212 const std::string oEmptyStr (
"");
213 std::ostringstream oNoDetailedStr;
214 std::ostringstream oDetailedStr;
215 std::ostringstream oJSONStr;
216 std::ostringstream oProtobufStr;
219 if (_logOutputStream == NULL) {
220 oNoDetailedStr <<
"The log filepath is not valid." << std::endl;
221 return oNoDetailedStr.str();
223 assert (_logOutputStream != NULL);
228 *_logOutputStream <<
"Travel query ('" << iTravelQuery <<
"'"
229 <<
"') search" << std::endl;
231 if (_opentrepService == NULL) {
232 oNoDetailedStr <<
"The OpenTREP service has not been initialised, "
233 <<
"i.e., the init() method has not been called "
234 <<
"correctly on the OpenTrepSearcher object. Please "
235 <<
"check that all the parameters are not empty and "
236 <<
"point to actual files.";
237 *_logOutputStream << oNoDetailedStr.str();
238 return oNoDetailedStr.str();
240 assert (_opentrepService != NULL);
245 const PORFilePath_T& lPORFilePath = lFilePathSet.first;
248 const TravelDBFilePath_T& lTravelDBFilePath = lDBFilePathPair.first;
249 const SQLDBConnectionString_T& lSQLDBConnStr = lDBFilePathPair.second;
252 *_logOutputStream <<
"Xapian travel database/index: '"
254 <<
"' - SQL database connection string: '"
256 <<
"' - OPTD-maintained list of POR: '"
257 << lPORFilePath <<
"'" << std::endl;
264 lNonMatchedWordList);
267 *_logOutputStream <<
"Python search for '" << iTravelQuery <<
"' gave "
268 << nbOfMatches <<
" matches." << std::endl;
270 if (nbOfMatches != 0) {
273 for(LocationList_T::const_iterator itLocation = lLocationList.begin();
274 itLocation != lLocationList.end(); ++itLocation, ++idx) {
275 const Location& lLocation = *itLocation;
278 oNoDetailedStr <<
",";
282 oNoDetailedStr << lLocation.getIataCode();
283 oDetailedStr << idx+1 <<
". " << lLocation.toSingleLocationString()
289 lLocation.getExtraLocationList();
290 if (lExtraLocationList.empty() ==
false) {
291 oDetailedStr <<
" Extra matches: " << std::endl;
294 for (LocationList_T::const_iterator itLoc =
295 lExtraLocationList.begin();
296 itLoc != lExtraLocationList.end(); ++itLoc, ++idxExtra) {
297 oNoDetailedStr <<
":";
298 oDetailedStr <<
" " << idx+1 <<
"." << idxExtra+1 <<
". ";
300 const Location& lExtraLocation = *itLoc;
301 oNoDetailedStr << lExtraLocation.getIataCode();
302 oDetailedStr << lExtraLocation << std::endl;
308 oNoDetailedStr <<
"/" << lLocation.getPercentage();
313 lLocation.getAlternateLocationList();
314 if (lAlternateLocationList.empty() ==
false) {
315 oDetailedStr <<
" Alternate matches: " << std::endl;
318 for (LocationList_T::const_iterator itLoc =
319 lAlternateLocationList.begin();
320 itLoc != lAlternateLocationList.end(); ++itLoc, ++idxAlter) {
321 oNoDetailedStr <<
"-";
322 oDetailedStr <<
" " << idx+1 <<
"." << idxAlter+1 <<
". ";
324 const Location& lAlternateLocation = *itLoc;
325 oNoDetailedStr << lAlternateLocation.getIataCode()
326 <<
"/" << lAlternateLocation.getPercentage();
327 oDetailedStr << lAlternateLocation << std::endl;
333 if (lNonMatchedWordList.empty() ==
false) {
334 oNoDetailedStr <<
";";
335 oDetailedStr <<
"Not recognised words:" << std::endl;
337 for (WordList_T::const_iterator itWord = lNonMatchedWordList.begin();
338 itWord != lNonMatchedWordList.end(); ++itWord, ++idx) {
339 const Word_T& lWord = *itWord;
341 oNoDetailedStr <<
",";
342 oDetailedStr << idx+1 <<
"." << std::endl;
344 oNoDetailedStr << lWord;
345 oDetailedStr << lWord;
350 *_logOutputStream <<
"Python search for '" << iTravelQuery
351 <<
"' yielded:" << std::endl;
358 lNonMatchedWordList);
361 *_logOutputStream <<
"Short version: "
362 << oNoDetailedStr.str() << std::endl;
363 *_logOutputStream <<
"Long version: "
364 << oDetailedStr.str() << std::endl;
365 *_logOutputStream <<
"JSON version: "
366 << oJSONStr.str() << std::endl;
367 *_logOutputStream <<
"Protobuf version: "
368 << oProtobufStr.str() << std::endl;
370 }
catch (
const RootException& eOpenTrepError) {
371 *_logOutputStream <<
"OpenTrep error: " << eOpenTrepError.what()
374 }
catch (
const std::exception& eStdError) {
375 *_logOutputStream <<
"Error: " << eStdError.what() << std::endl;
378 *_logOutputStream <<
"Unknown error" << std::endl;
383 switch (iOutputFormat) {
385 return oNoDetailedStr.str();
389 return oDetailedStr.str();
393 return oJSONStr.str();
397 return oProtobufStr.str();
416 const std::string oEmptyStr (
"");
417 std::ostringstream oNoDetailedStr;
418 std::ostringstream oDetailedStr;
419 std::ostringstream oJSONStr;
420 std::ostringstream oProtobufStr;
423 if (_logOutputStream == NULL) {
424 oNoDetailedStr <<
"The log filepath is not valid." << std::endl;
425 return oNoDetailedStr.str();
427 assert (_logOutputStream != NULL);
432 *_logOutputStream <<
"Number of random draws: " << iNbOfDraws
435 if (_opentrepService == NULL) {
436 oNoDetailedStr <<
"The OpenTREP service has not been initialised, "
437 <<
"i.e., the init() method has not been called "
438 <<
"correctly on the OpenTrepSearcher object. Please "
439 <<
"check that all the parameters are not empty and "
440 <<
"point to actual files.";
441 *_logOutputStream << oNoDetailedStr.str();
442 return oNoDetailedStr.str();
444 assert (_opentrepService != NULL);
449 const PORFilePath_T& lPORFilePath = lFilePathSet.first;
452 const TravelDBFilePath_T& lTravelDBFilePath = lDBFilePathPair.first;
453 const SQLDBConnectionString_T& lSQLDBConnStr = lDBFilePathPair.second;
456 *_logOutputStream <<
"Xapian travel database/index: '"
458 <<
"' - SQL database connection string: '"
460 <<
"' - OPTD-maintained list of POR: '"
461 << lPORFilePath <<
"'" << std::endl;
469 *_logOutputStream <<
"Python generation of " << iNbOfDraws <<
" gave "
470 << nbOfMatches <<
" documents." << std::endl;
472 if (nbOfMatches != 0) {
475 for(LocationList_T::const_iterator itLocation = lLocationList.begin();
476 itLocation != lLocationList.end(); ++itLocation, ++idx) {
477 const Location& lLocation = *itLocation;
480 oNoDetailedStr <<
",";
484 oNoDetailedStr << lLocation.getIataCode();
485 oDetailedStr << idx+1 <<
". " << lLocation.toSingleLocationString()
491 lLocation.getExtraLocationList();
492 if (lExtraLocationList.empty() ==
false) {
493 oDetailedStr <<
" Extra matches: " << std::endl;
496 for (LocationList_T::const_iterator itLoc =
497 lExtraLocationList.begin();
498 itLoc != lExtraLocationList.end(); ++itLoc, ++idxExtra) {
499 oNoDetailedStr <<
":";
500 oDetailedStr <<
" " << idx+1 <<
"." << idxExtra+1 <<
". ";
502 const Location& lExtraLocation = *itLoc;
503 oNoDetailedStr << lExtraLocation.getIataCode();
504 oDetailedStr << lExtraLocation << std::endl;
510 oNoDetailedStr <<
"/" << lLocation.getPercentage();
515 lLocation.getAlternateLocationList();
516 if (lAlternateLocationList.empty() ==
false) {
517 oDetailedStr <<
" Alternate matches: " << std::endl;
520 for (LocationList_T::const_iterator itLoc =
521 lAlternateLocationList.begin();
522 itLoc != lAlternateLocationList.end(); ++itLoc, ++idxAlter) {
523 oNoDetailedStr <<
"-";
524 oDetailedStr <<
" " << idx+1 <<
"." << idxAlter+1 <<
". ";
526 const Location& lAlternateLocation = *itLoc;
527 oNoDetailedStr << lAlternateLocation.getIataCode()
528 <<
"/" << lAlternateLocation.getPercentage();
529 oDetailedStr << lAlternateLocation << std::endl;
536 *_logOutputStream <<
"Python generation of " << iNbOfDraws
537 <<
" yielded:" << std::endl;
545 lNonMatchedWordList);
548 *_logOutputStream <<
"Short version: "
549 << oNoDetailedStr.str() << std::endl;
550 *_logOutputStream <<
"Long version: "
551 << oDetailedStr.str() << std::endl;
552 *_logOutputStream <<
"JSON version: "
553 << oJSONStr.str() << std::endl;
554 *_logOutputStream <<
"Protobuf version: "
555 << oProtobufStr.str() << std::endl;
557 }
catch (
const RootException& eOpenTrepError) {
558 *_logOutputStream <<
"OpenTrep error: " << eOpenTrepError.what()
561 }
catch (
const std::exception& eStdError) {
562 *_logOutputStream <<
"Error: " << eStdError.what() << std::endl;
565 *_logOutputStream <<
"Unknown error" << std::endl;
570 switch (iOutputFormat) {
572 return oNoDetailedStr.str();
576 return oDetailedStr.str();
580 return oJSONStr.str();
584 return oProtobufStr.str();
609 : _opentrepService (iOpenTrepSearcher._opentrepService),
610 _logOutputStream (iOpenTrepSearcher._logOutputStream) {
617 _opentrepService = NULL;
618 _logOutputStream = NULL;
624 bool init (
const std::string& iTravelDBFilePath,
625 const std::string& iSQLDBTypeStr,
626 const std::string& iSQLDBConnStr,
628 const std::string& iLogFilePath) {
629 bool isEverythingOK =
true;
634 std::ostringstream oXFP;
635 oXFP << iTravelDBFilePath << iDeploymentNumber;
636 const std::string lXapianFP = oXFP.str();
639 boost::filesystem::path lXapianFilePath (lXapianFP.begin(),
641 if (!(boost::filesystem::exists (lXapianFilePath)
642 && boost::filesystem::is_directory (lXapianFilePath))) {
643 isEverythingOK =
false;
644 return isEverythingOK;
648 _logOutputStream =
new std::ofstream;
649 assert (_logOutputStream != NULL);
652 _logOutputStream->open (iLogFilePath.c_str());
653 _logOutputStream->clear();
656 *_logOutputStream <<
"Python wrapper initialisation" << std::endl;
666 lSQLDBType, lSQLDBConnStr,
670 *_logOutputStream <<
"Python wrapper initialised" << std::endl;
673 *_logOutputStream <<
"OpenTrep error: " << eOpenTrepError.
what()
676 }
catch (
const std::exception& eStdError) {
677 *_logOutputStream <<
"Error: " << eStdError.what() << std::endl;
680 *_logOutputStream <<
"Unknown error" << std::endl;
683 return isEverythingOK;
690 bool isEverythingOK =
true;
695 if (_opentrepService != NULL) {
696 delete _opentrepService; _opentrepService = NULL;
700 if (_logOutputStream != NULL) {
702 *_logOutputStream <<
"Python wrapper finalization" << std::endl;
703 _logOutputStream->close();
704 delete _logOutputStream; _logOutputStream = NULL;
710 return isEverythingOK;
718 std::ofstream* _logOutputStream;
725 boost::python::class_<OPENTREP::OpenTrepSearcher> (
"OpenTrepSearcher")