/airinv/command/vault/DCPParserHelper.hpp

https://github.com/airsim/airinv · C++ Header · 363 lines · 159 code · 38 blank · 166 comment · 0 complexity · e52b6cf8307a233e32e1746d2307b4ee MD5 · raw file

  1. #ifndef __AIRINV_CMD_DCPPARSERHELPER_HPP
  2. #define __AIRINV_CMD_DCPPARSERHELPER_HPP
  3. // //////////////////////////////////////////////////////////////////////
  4. // Import section
  5. // //////////////////////////////////////////////////////////////////////
  6. // StdAir
  7. // The stdair/basic/BasParserTypes.hpp header includes Boost.Spirit headers
  8. //#define BOOST_SPIRIT_DEBUG
  9. #include <stdair/basic/BasParserTypes.hpp>
  10. #include <stdair/command/CmdAbstract.hpp>
  11. // AirInv
  12. #include <airinv/AIRINV_Types.hpp>
  13. #include <airinv/bom/DCPRuleStruct.hpp>
  14. // Forward declarations
  15. namespace stdair {
  16. class BomRoot;
  17. }
  18. namespace AIRINV {
  19. namespace DCPParserHelper {
  20. // ///////////////////////////////////////////////////////////////////
  21. // Semantic actions
  22. // ////////////////////////////////////////////////////
  23. ///////////////
  24. /** Generic Semantic Action (Actor / Functor) for the DCP Parser. */
  25. struct ParserSemanticAction {
  26. /** Actor Constructor. */
  27. ParserSemanticAction (DCPRuleStruct&);
  28. /** Actor Context. */
  29. DCPRuleStruct& _DCPRule;
  30. };
  31. /** Store the parsed DCP Id. */
  32. struct storeDCPId : public ParserSemanticAction {
  33. /** Actor Constructor. */
  34. storeDCPId (DCPRuleStruct&);
  35. /** Actor Function (functor). */
  36. void operator() (unsigned int,
  37. boost::spirit::qi::unused_type,
  38. boost::spirit::qi::unused_type) const;
  39. };
  40. /** Store the parsed origin. */
  41. struct storeOrigin : public ParserSemanticAction {
  42. /** Actor Constructor. */
  43. storeOrigin (DCPRuleStruct&);
  44. /** Actor Function (functor). */
  45. void operator() (std::vector<char>,
  46. boost::spirit::qi::unused_type,
  47. boost::spirit::qi::unused_type) const;
  48. };
  49. /** Store the parsed destination. */
  50. struct storeDestination : public ParserSemanticAction {
  51. /** Actor Constructor. */
  52. storeDestination (DCPRuleStruct&);
  53. /** Actor Function (functor). */
  54. void operator() (std::vector<char>,
  55. boost::spirit::qi::unused_type,
  56. boost::spirit::qi::unused_type) const;
  57. };
  58. /** Store the parsed start of the date range. */
  59. struct storeDateRangeStart : public ParserSemanticAction {
  60. /** Actor Constructor. */
  61. storeDateRangeStart (DCPRuleStruct&);
  62. /** Actor Function (functor). */
  63. void operator() (boost::spirit::qi::unused_type,
  64. boost::spirit::qi::unused_type,
  65. boost::spirit::qi::unused_type) const;
  66. };
  67. /** Store the parsed end of the date range. */
  68. struct storeDateRangeEnd : public ParserSemanticAction {
  69. /** Actor Constructor. */
  70. storeDateRangeEnd (DCPRuleStruct&);
  71. /** Actor Function (functor). */
  72. void operator() (boost::spirit::qi::unused_type,
  73. boost::spirit::qi::unused_type,
  74. boost::spirit::qi::unused_type) const;
  75. };
  76. /** Store the parsed start range time. */
  77. struct storeStartRangeTime : public ParserSemanticAction {
  78. /** Actor Constructor. */
  79. storeStartRangeTime (DCPRuleStruct&);
  80. /** Actor Function (functor). */
  81. void operator() (boost::spirit::qi::unused_type,
  82. boost::spirit::qi::unused_type,
  83. boost::spirit::qi::unused_type) const;
  84. };
  85. /** Store the parsed end start range time. */
  86. struct storeEndRangeTime : public ParserSemanticAction {
  87. /** Actor Constructor. */
  88. storeEndRangeTime (DCPRuleStruct&);
  89. /** Actor Function (functor). */
  90. void operator() (boost::spirit::qi::unused_type,
  91. boost::spirit::qi::unused_type,
  92. boost::spirit::qi::unused_type) const;
  93. };
  94. /** Store the parsed customer position. */
  95. struct storePOS : public ParserSemanticAction {
  96. /** Actor Constructor. */
  97. storePOS (DCPRuleStruct&);
  98. /** Actor Function (functor). */
  99. void operator() (std::vector<char>,
  100. boost::spirit::qi::unused_type,
  101. boost::spirit::qi::unused_type) const;
  102. };
  103. /** Store the cabin code. */
  104. struct storeCabinCode : public ParserSemanticAction {
  105. /** Actor Constructor. */
  106. storeCabinCode (DCPRuleStruct&);
  107. /** Actor Function (functor). */
  108. void operator() (char,
  109. boost::spirit::qi::unused_type,
  110. boost::spirit::qi::unused_type) const;
  111. };
  112. /** Store the channel distribution. */
  113. struct storeChannel : public ParserSemanticAction {
  114. /** Actor Constructor. */
  115. storeChannel (DCPRuleStruct&);
  116. /** Actor Function (functor). */
  117. void operator() (std::vector<char>,
  118. boost::spirit::qi::unused_type,
  119. boost::spirit::qi::unused_type) const;
  120. };
  121. /** Store the parsed advance purchase days. */
  122. struct storeAdvancePurchase : public ParserSemanticAction {
  123. /** Actor Constructor. */
  124. storeAdvancePurchase (DCPRuleStruct&);
  125. /** Actor Function (functor). */
  126. void operator() (unsigned int,
  127. boost::spirit::qi::unused_type,
  128. boost::spirit::qi::unused_type) const;
  129. };
  130. /** Store the parsed saturday night. */
  131. struct storeSaturdayStay : public ParserSemanticAction {
  132. /** Actor Constructor. */
  133. storeSaturdayStay (DCPRuleStruct&);
  134. /** Actor Function (functor). */
  135. void operator() (char,
  136. boost::spirit::qi::unused_type,
  137. boost::spirit::qi::unused_type) const;
  138. };
  139. /** Store the parsed change fees. */
  140. struct storeChangeFees : public ParserSemanticAction {
  141. /** Actor Constructor. */
  142. storeChangeFees (DCPRuleStruct&);
  143. /** Actor Function (functor). */
  144. void operator() (char,
  145. boost::spirit::qi::unused_type,
  146. boost::spirit::qi::unused_type) const;
  147. };
  148. /** Store the parsed refundable option */
  149. struct storeNonRefundable : public ParserSemanticAction {
  150. /** Actor Constructor. */
  151. storeNonRefundable (DCPRuleStruct&);
  152. /** Actor Function (functor). */
  153. void operator() (char,
  154. boost::spirit::qi::unused_type,
  155. boost::spirit::qi::unused_type) const;
  156. };
  157. /** Store the parsed minimum stay. */
  158. struct storeMinimumStay : public ParserSemanticAction {
  159. /** Actor Constructor. */
  160. storeMinimumStay (DCPRuleStruct&);
  161. /** Actor Function (functor). */
  162. void operator() (unsigned int,
  163. boost::spirit::qi::unused_type,
  164. boost::spirit::qi::unused_type) const;
  165. };
  166. /** Store the parsed DCP value. */
  167. struct storeDCP : public ParserSemanticAction {
  168. /** Actor Constructor. */
  169. storeDCP (DCPRuleStruct&);
  170. /** Actor Function (functor). */
  171. void operator() (double,
  172. boost::spirit::qi::unused_type,
  173. boost::spirit::qi::unused_type) const;
  174. };
  175. /** Store the parsed airline code. */
  176. struct storeAirlineCode : public ParserSemanticAction {
  177. /** Actor Constructor. */
  178. storeAirlineCode (DCPRuleStruct&);
  179. /** Actor Function (functor). */
  180. void operator() (std::vector<char>,
  181. boost::spirit::qi::unused_type,
  182. boost::spirit::qi::unused_type) const;
  183. };
  184. /** Store the parsed class. */
  185. struct storeClass : public ParserSemanticAction {
  186. /** Actor Constructor. */
  187. storeClass (DCPRuleStruct&);
  188. /** Actor Function (functor). */
  189. void operator() (std::vector<char>,
  190. boost::spirit::qi::unused_type,
  191. boost::spirit::qi::unused_type) const;
  192. };
  193. /** Mark the end of the DCP-rule parsing. */
  194. struct doEndDCP : public ParserSemanticAction {
  195. /** Actor Constructor. */
  196. doEndDCP (stdair::BomRoot&, DCPRuleStruct&);
  197. /** Actor Function (functor). */
  198. void operator() (boost::spirit::qi::unused_type,
  199. boost::spirit::qi::unused_type,
  200. boost::spirit::qi::unused_type) const;
  201. /** Actor Specific Context. */
  202. stdair::BomRoot& _bomRoot;
  203. };
  204. /////////////////////////////////////////////////////////////////////////
  205. //
  206. // (Boost Spirit) Grammar Definition
  207. //
  208. /////////////////////////////////////////////////////////////////////////
  209. /**
  210. // DCP: DCPID; OriginCity; DestinationCity; DateRangeStart;
  211. DateRangeEnd; DepartureTimeRangeStart; DepartureTimeRangeEnd;
  212. POS; AdvancePurchase; SaturdayNight; ChangeFees; NonRefundable;
  213. MinimumStay; Price; AirlineCode; Class;
  214. DCPID
  215. OriginCity (3-char airport code)
  216. DestinationCity (3-char airport code)
  217. DateRangeStart (yyyy-mm-dd)
  218. DateRangeEnd (yyyy-mm-dd)
  219. DepartureTimeRangeStart (hh:mm)
  220. DepartureTimeRangeEnd (hh:mm)
  221. POS (3-char position city)
  222. Cabin Code (1-char cabin code)
  223. Channel (D=direct, I=indirect, N=oNline, F=oFfline)
  224. AdvancePurchase
  225. SaturdayNight (T=True, F=False)
  226. ChangeFees (T=True, F=False)
  227. NonRefundable (T=True, F=False)
  228. MinimumStay
  229. Price
  230. AirlineCode (2-char airline code)
  231. ClassList (List of 1-char class code)
  232. Grammar:
  233. Demand ::= PrefDepDate ';' Origin ';' Destination ';' PassengerType
  234. ';' DemandParams ';' PosDist ';' ChannelDist ';' TripDist
  235. ';' StayDist ';' FfDist ';' PrefDepTimeDist
  236. ';' minWTP ';' TimeValueDist ';' DtdDist
  237. EndOfDemand
  238. PrefDepDate ::= date
  239. PassengerType ::= 'T' | 'F'
  240. DemandParams ::= DemandMean ';' DemandStdDev
  241. PosDist ::= PosPair (',' PosPair)*
  242. PosPair ::= PosCode ':' PosShare
  243. PosCode ::= AirportCode | "row"
  244. PosShare ::= real
  245. ChannelDist ::= ChannelPair (',' ChannelPair)*
  246. ChannelPair ::= Channel_Code ':' ChannelShare
  247. ChannelCode ::= "DF" | "DN" | "IF" | "IN"
  248. ChannelShare ::= real
  249. TripDist ::= TripPair (',' TripPair)*
  250. TripPair ::= TripCode ':' TripShare
  251. TripCode ::= "RO" | "RI" | "OW"
  252. TripShare ::= real
  253. StayDist ::= StayPair (',' StayPair)*
  254. StayPair ::= [0;3]-digit-integer ':' stay_share
  255. StayShare ::= real
  256. FFDist ::= FF_Pair (',' FF_Pair)*
  257. FFPair ::= FFCode ':' FFShare
  258. FFCode ::= 'P' | 'G' | 'S' | 'M' | 'N'
  259. FFShare ::= real
  260. PrefDepTimeDist ::= PrefDepTimePair (',' PrefDepTimePair)*
  261. PrefDepTimePair ::= time ':' PrefDepTimeShare
  262. PrefDepTimeShare ::= real
  263. minWTP ::= real
  264. TimeValueDist ::= TimeValuePair (',' TimeValuePair)*
  265. TimeValuePair ::= [0;2]-digit-integer ':' TimeValueShare
  266. TimeValueShare ::= real
  267. DTDDist ::= DTDPair (',' DTDPair)*
  268. DTDPair ::= real ':' DTDShare
  269. DTDShare ::= real
  270. EndOfDemand ::= ';'
  271. */
  272. /** Grammar for the DCP-Rule parser. */
  273. struct DCPRuleParser :
  274. public boost::spirit::qi::grammar<stdair::iterator_t,
  275. boost::spirit::ascii::space_type> {
  276. DCPRuleParser (stdair::BomRoot&, DCPRuleStruct&);
  277. // Instantiation of rules
  278. boost::spirit::qi::rule<stdair::iterator_t,
  279. boost::spirit::ascii::space_type>
  280. start, comments, DCP_rule, DCP_rule_end, DCP_key, DCP_id, origin,
  281. destination, dateRangeStart, dateRangeEnd, date, timeRangeStart,
  282. timeRangeEnd, time, position, cabinCode, channel, advancePurchase,
  283. saturdayStay, changeFees, nonRefundable, minimumStay, DCP,
  284. segment, list_class;
  285. // Parser Context
  286. stdair::BomRoot& _bomRoot;
  287. DCPRuleStruct& _DCPRule;
  288. };
  289. }
  290. /////////////////////////////////////////////////////////////////////////
  291. //
  292. // Entry class for the file parser
  293. //
  294. /////////////////////////////////////////////////////////////////////////
  295. /** Class wrapping the
  296. initialisation and entry point of the parser.
  297. <br>The seemingly redundancy is used to force the instantiation of
  298. the actual parser, which is a templatised Boost Spirit grammar.
  299. Hence, the actual parser is instantiated within that class object
  300. code. */
  301. class DCPRuleFileParser : public stdair::CmdAbstract {
  302. public:
  303. /** Constructor. */
  304. DCPRuleFileParser (stdair::BomRoot& ioBomRoot,
  305. const stdair::Filename_T& iFilename);
  306. /** Parse the input file and generate the Inventories. */
  307. bool generateDCPRules ();
  308. private:
  309. /** Initialise. */
  310. void init();
  311. private:
  312. // Attributes
  313. /** File-name of the CSV-formatted schedule input file. */
  314. stdair::Filename_T _filename;
  315. /** stdair::BomRoot. */
  316. stdair::BomRoot& _bomRoot;
  317. /** DCP-Rule Structure. */
  318. DCPRuleStruct _DCPRule;
  319. };
  320. }
  321. #endif // __AIRINV_CMD_DCPPARSERHELPER_HPP