OpenTREP Logo  0.6.0
C++ Open Travel Request Parsing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OPENTREP_Service.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 #include <ostream>
7 // Boost
8 #include <boost/date_time/gregorian/gregorian.hpp>
9 #include <boost/date_time/posix_time/ptime.hpp>
10 // OpenTrep
23 
24 namespace OPENTREP {
25 
26  // //////////////////////////////////////////////////////////////////////
27  OPENTREP_Service::
28  OPENTREP_Service (std::ostream& ioLogStream, const PORFilePath_T& iPORFilepath,
29  const TravelDBFilePath_T& iTravelDBFilePath,
30  const SQLiteDBFilePath_T& iSQLiteDBFilePath)
31  : _opentrepServiceContext (NULL) {
32  init (ioLogStream, iPORFilepath, iTravelDBFilePath, iSQLiteDBFilePath);
33  }
34 
35  // //////////////////////////////////////////////////////////////////////
36  OPENTREP_Service::
37  OPENTREP_Service (std::ostream& ioLogStream,
38  const TravelDBFilePath_T& iTravelDBFilePath,
39  const SQLiteDBFilePath_T& iSQLiteDBFilePath)
40  : _opentrepServiceContext (NULL) {
41  init (ioLogStream, iTravelDBFilePath, iSQLiteDBFilePath);
42  }
43 
44  // //////////////////////////////////////////////////////////////////////
45  OPENTREP_Service::OPENTREP_Service() : _opentrepServiceContext (NULL) {
46  assert (false);
47  }
48 
49  // //////////////////////////////////////////////////////////////////////
50  OPENTREP_Service::OPENTREP_Service (const OPENTREP_Service& iService) {
51  assert (false);
52  }
53 
54  // //////////////////////////////////////////////////////////////////////
56  // Delete/Clean all the objects from memory
57  finalise();
58  }
59 
60  // //////////////////////////////////////////////////////////////////////
61  void logInit (const LOG::EN_LogLevel iLogLevel,
62  std::ostream& ioLogOutputFile) {
63  Logger::instance().setLogParameters (iLogLevel, ioLogOutputFile);
64  }
65 
66  // //////////////////////////////////////////////////////////////////////
67  void OPENTREP_Service::init (std::ostream& ioLogStream,
68  const TravelDBFilePath_T& iTravelDBFilePath,
69  const SQLiteDBFilePath_T& iSQLiteDBFilePath) {
70  // Set the log file
71  logInit (LOG::DEBUG, ioLogStream);
72 
73  // Initialise the context
74  OPENTREP_ServiceContext& lOPENTREP_ServiceContext =
75  FacOpenTrepServiceContext::instance().create (iTravelDBFilePath, iSQLiteDBFilePath);
76  _opentrepServiceContext = &lOPENTREP_ServiceContext;
77 
78  // Instanciate an empty World object
79  World& lWorld = FacWorld::instance().create();
80  lOPENTREP_ServiceContext.setWorld (lWorld);
81  }
82 
83  // //////////////////////////////////////////////////////////////////////
84  void OPENTREP_Service::init (std::ostream& ioLogStream,
85  const PORFilePath_T& iPORFilepath,
86  const TravelDBFilePath_T& iTravelDBFilePath,
87  const SQLiteDBFilePath_T& iSQLiteDBFilePath) {
88  // Set the log file
89  logInit (LOG::DEBUG, ioLogStream);
90 
91  // Initialise the context
92  OPENTREP_ServiceContext& lOPENTREP_ServiceContext =
94  iTravelDBFilePath, iSQLiteDBFilePath);
95  _opentrepServiceContext = &lOPENTREP_ServiceContext;
96 
97  // Instanciate an empty World object
98  World& lWorld = FacWorld::instance().create();
99  lOPENTREP_ServiceContext.setWorld (lWorld);
100  }
101 
102  // //////////////////////////////////////////////////////////////////////
103  void OPENTREP_Service::finalise() {
104  }
105 
106  // //////////////////////////////////////////////////////////////////////
108  if (_opentrepServiceContext == NULL) {
109  throw NonInitialisedServiceException ("The OpenTREP service has not been"
110  " initialised");
111  }
112  assert (_opentrepServiceContext != NULL);
113  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
114 
115  // Retrieve the file-path of the POR (points of reference) file
116  const PORFilePath_T& lPORFilePath = lOPENTREP_ServiceContext.getPORFilePath();
117 
118  // Retrieve the Xapian database name (directorty of the index)
119  const TravelDBFilePath_T& lTravelDBFilePath =
120  lOPENTREP_ServiceContext.getTravelDBFilePath();
121 
122  // Retrieve the SQLite3 database file-path
123  const SQLiteDBFilePath_T& lSQLiteDBFilePath =
124  lOPENTREP_ServiceContext.getSQLiteDBFilePath();
125 
126  const DBFilePathPair_T lDBFilePathPair (lTravelDBFilePath, lSQLiteDBFilePath);
127  FilePathSet_T oFilePathSet (lPORFilePath, lDBFilePathPair);
128  return oFilePathSet;
129  }
130 
131  // //////////////////////////////////////////////////////////////////////
133  NbOfDBEntries_T oNbOfEntries = 0;
134 
135  if (_opentrepServiceContext == NULL) {
136  throw NonInitialisedServiceException ("The OpenTREP service has not been"
137  " initialised");
138  }
139  assert (_opentrepServiceContext != NULL);
140  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
141 
142  // Retrieve the Xapian database name (directorty of the index)
143  const TravelDBFilePath_T& lTravelDBFilePath =
144  lOPENTREP_ServiceContext.getTravelDBFilePath();
145 
146  // Delegate the query execution to the dedicated command
147  BasChronometer lIndexSizeChronometer; lIndexSizeChronometer.start();
148  oNbOfEntries = XapianIndexManager::getSize (lTravelDBFilePath);
149  const double lIndexSizeMeasure = lIndexSizeChronometer.elapsed();
150 
151  // DEBUG
152  OPENTREP_LOG_DEBUG ("Size retrieval of the Xapian database (index): "
153  << lIndexSizeMeasure << " - "
154  << lOPENTREP_ServiceContext.display());
155 
156  return oNbOfEntries;
157  }
158 
159  // //////////////////////////////////////////////////////////////////////
162  LocationList_T& ioLocationList) {
163  NbOfMatches_T oNbOfMatches = 0;
164 
165  if (_opentrepServiceContext == NULL) {
166  throw NonInitialisedServiceException ("The OpenTREP service has not been"
167  " initialised");
168  }
169  assert (_opentrepServiceContext != NULL);
170  OPENTREP_ServiceContext& lOPENTREP_ServiceContext= *_opentrepServiceContext;
171 
172  // Retrieve the Xapian database name (directorty of the index)
173  const TravelDBFilePath_T& lTravelDBFilePath =
174  lOPENTREP_ServiceContext.getTravelDBFilePath();
175 
176  // Delegate the query execution to the dedicated command
177  BasChronometer lRandomGetChronometer; lRandomGetChronometer.start();
178  oNbOfMatches = XapianIndexManager::drawRandomLocations (lTravelDBFilePath,
179  iNbOfDraws,
180  ioLocationList);
181  const double lRandomGetMeasure = lRandomGetChronometer.elapsed();
182 
183  // DEBUG
184  OPENTREP_LOG_DEBUG ("Random retrieval of locations (index): "
185  << lRandomGetMeasure << " - "
186  << lOPENTREP_ServiceContext.display());
187 
188  return oNbOfMatches;
189  }
190 
191  // //////////////////////////////////////////////////////////////////////
193  NbOfDBEntries_T oNbOfEntries = 0;
194 
195  if (_opentrepServiceContext == NULL) {
196  throw NonInitialisedServiceException ("The OpenTREP service has not been"
197  " initialised");
198  }
199  assert (_opentrepServiceContext != NULL);
200  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
201 
202  // Retrieve the file-path of the POR (points of reference) file
203  const PORFilePath_T& lPORFilePath= lOPENTREP_ServiceContext.getPORFilePath();
204 
205  // Retrieve the SQLite3 database file-path
206  const SQLiteDBFilePath_T& lSQLiteDBFilePath =
207  lOPENTREP_ServiceContext.getSQLiteDBFilePath();
208 
209  // Delegate the database creation to the dedicated command
210  BasChronometer lDBCreationChronometer;
211  lDBCreationChronometer.start();
212  oNbOfEntries = DBManager::buildSQLDB (lPORFilePath, lSQLiteDBFilePath);
213  const double lDBCreationMeasure = lDBCreationChronometer.elapsed();
214 
215  // DEBUG
216  OPENTREP_LOG_DEBUG ("Created the SQLite3 database: " << lDBCreationMeasure
217  << " - " << lOPENTREP_ServiceContext.display());
218 
219  return oNbOfEntries;
220  }
221 
222  // //////////////////////////////////////////////////////////////////////
224  NbOfDBEntries_T oNbOfEntries = 0;
225 
226  if (_opentrepServiceContext == NULL) {
227  throw NonInitialisedServiceException ("The OpenTREP service has not been"
228  " initialised");
229  }
230  assert (_opentrepServiceContext != NULL);
231  OPENTREP_ServiceContext& lOPENTREP_ServiceContext = *_opentrepServiceContext;
232 
233  // Retrieve the file-path of the POR (points of reference) file
234  const PORFilePath_T& lPORFilePath= lOPENTREP_ServiceContext.getPORFilePath();
235 
236  // Retrieve the Xapian database name (directorty of the index)
237  const TravelDBFilePath_T& lTravelDBFilePath =
238  lOPENTREP_ServiceContext.getTravelDBFilePath();
239 
240  // Retrieve the Unicode transliterator
241  const OTransliterator& lTransliterator =
242  lOPENTREP_ServiceContext.getTransliterator();
243 
244  // Delegate the index building to the dedicated command
245  BasChronometer lBuildSearchIndexChronometer;
246  lBuildSearchIndexChronometer.start();
247  oNbOfEntries = IndexBuilder::buildSearchIndex (lPORFilePath,
248  lTravelDBFilePath,
249  lTransliterator);
250  const double lBuildSearchIndexMeasure =
251  lBuildSearchIndexChronometer.elapsed();
252 
253  // DEBUG
254  OPENTREP_LOG_DEBUG ("Built Xapian database (index): "
255  << lBuildSearchIndexMeasure << " - "
256  << lOPENTREP_ServiceContext.display());
257 
258  return oNbOfEntries;
259  }
260 
261  // //////////////////////////////////////////////////////////////////////
263  interpretTravelRequest (const std::string& iTravelQuery,
264  LocationList_T& ioLocationList,
265  WordList_T& ioWordList) {
266  NbOfMatches_T nbOfMatches = 0;
267 
268  if (_opentrepServiceContext == NULL) {
269  throw NonInitialisedServiceException ("The OpenTREP service has not been"
270  " initialised");
271  }
272  assert (_opentrepServiceContext != NULL);
273  OPENTREP_ServiceContext& lOPENTREP_ServiceContext= *_opentrepServiceContext;
274 
275  // Get the date-time for the present time
276  boost::posix_time::ptime lNowDateTime =
277  boost::posix_time::second_clock::local_time();
278  // boost::gregorian::date lNowDate = lNowDateTime.date();
279 
280  // DEBUG
281  OPENTREP_LOG_DEBUG (std::endl
282  << "==================================================="
283  << std::endl
284  << lNowDateTime << " - Match query '" << iTravelQuery
285  << "' on Xapian database (index)");
286 
287  // Check that the travel request is not empty
288  if (iTravelQuery.empty() == true) {
289  std::ostringstream errorStr;
290  errorStr << "The travel request is empty.";
291  OPENTREP_LOG_ERROR (errorStr.str());
292  throw TravelRequestEmptyException (errorStr.str());
293  }
294 
295  // Retrieve the Xapian database name (directorty of the index)
296  const TravelDBFilePath_T& lTravelDBFilePath =
297  lOPENTREP_ServiceContext.getTravelDBFilePath();
298 
299  // Delegate the query execution to the dedicated command
300  BasChronometer lRequestInterpreterChronometer;
301  lRequestInterpreterChronometer.start();
302  nbOfMatches = RequestInterpreter::interpretTravelRequest (lTravelDBFilePath,
303  iTravelQuery,
304  ioLocationList,
305  ioWordList);
306  const double lRequestInterpreterMeasure =
307  lRequestInterpreterChronometer.elapsed();
308 
309  // DEBUG
310  OPENTREP_LOG_DEBUG ("Match query on Xapian database (index): "
311  << lRequestInterpreterMeasure << " - "
312  << lOPENTREP_ServiceContext.display());
313 
314  return nbOfMatches;
315  }
316 
317 }
std::pair< const TravelDBFilePath_T, const SQLiteDBFilePath_T > DBFilePathPair_T
Structure allowing measuring the time elapsed between two events.
#define OPENTREP_LOG_ERROR(iToBeLogged)
Definition: Logger.hpp:23
#define OPENTREP_LOG_DEBUG(iToBeLogged)
Definition: Logger.hpp:32
unsigned short NbOfMatches_T
const TravelDBFilePath_T & getTravelDBFilePath() const
const PORFilePath_T & getPORFilePath() const
static NbOfDBEntries_T buildSQLDB(const PORFilePath_T &, const SQLiteDBFilePath_T &)
Definition: DBManager.cpp:516
const SQLiteDBFilePath_T & getSQLiteDBFilePath() const
Class holding the context of the OpenTrep services.
const OTransliterator & getTransliterator() const
static FacWorld & instance()
Definition: FacWorld.cpp:26
void setLogParameters(const LOG::EN_LogLevel iLogLevel, std::ostream &ioLogStream)
Definition: Logger.cpp:48
unsigned int NbOfDBEntries_T
World & create()
Definition: FacWorld.cpp:38
std::list< Word_T > WordList_T
static FacOpenTrepServiceContext & instance()
NbOfMatches_T drawRandomLocations(const NbOfMatches_T &iNbOfDraws, LocationList_T &)
std::list< Location > LocationList_T
OPENTREP_ServiceContext & create(const TravelDBFilePath_T &, const SQLiteDBFilePath_T &)
NbOfDBEntries_T buildSearchIndex()
NbOfMatches_T interpretTravelRequest(const std::string &iTravelQuery, LocationList_T &, WordList_T &)
std::pair< const PORFilePath_T, const DBFilePathPair_T > FilePathSet_T
static Logger & instance()
Definition: Logger.cpp:55
FilePathSet_T getFilePaths() const
void logInit(const LOG::EN_LogLevel iLogLevel, std::ostream &ioLogOutputFile)