您可以将融合与精神特征一起使用(参见例如解析成几个向量成员),但我会考虑使用语义动作。
这是设计难题:
vector
用 trait分隔s
Live On Coliru
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/repository/include/qi_seek.hpp>
#include <boost/utility/string_view.hpp>
#include <cstring> // strlen
using It = char const*;
struct BaseEvent {
int driver;
int sequence;
double time;
double vel;
double km;
boost::string_view date;
boost::string_view road;
};
struct Sequence : BaseEvent{};
struct Clutch : BaseEvent{};
struct Gear : BaseEvent{};
BOOST_FUSION_ADAPT_STRUCT(::Sequence, date, time, driver, vel, road, km, sequence)
BOOST_FUSION_ADAPT_STRUCT(::Clutch, date, time, driver, vel, road, km, sequence)
BOOST_FUSION_ADAPT_STRUCT(::Gear, date, time, driver, vel, road, km, sequence)
struct LogEvents {
std::vector<Sequence> sequence;
std::vector<Clutch> clutch;
std::vector<Gear> gear;
void add(Sequence const& s) { sequence.push_back(s); }
void add(Clutch const& c) { clutch.push_back(c); }
void add(Gear const& g) { gear.push_back(g); }
};
namespace qi = boost::spirit::qi;
namespace boost { namespace spirit { namespace traits {
template <typename It>
struct assign_to_attribute_from_iterators<boost::string_view, It, void> {
static inline void call(It f, It l, boost::string_view& attr) { attr = boost::string_view { &*f, size_t(std::distance(f,l)) }; }
};
template <> struct is_container<LogEvents> : std::true_type {};
template <> struct container_value<LogEvents> {
using type = boost::variant<::Sequence, ::Clutch, ::Gear>;
};
template <typename T> struct push_back_container<LogEvents, T> {
struct Visitor {
LogEvents& _log;
template <typename U> void operator()(U const& ev) const { _log.add(ev); }
using result_type = void;
};
template <typename... U>
static bool call(LogEvents& log, boost::variant<U...> const& attribute) {
boost::apply_visitor(Visitor{log}, attribute);
return true;
}
};
} } }
namespace QiParsers {
template <typename It, typename Attribute>
struct BaseEventParser : qi::grammar<It, Attribute()> {
BaseEventParser(std::string const& event_type) : BaseEventParser::base_type(start) {
using namespace qi;
auto date_time = copy(
repeat(4)[digit] >> '-' >> repeat(3)[alpha] >> '-' >> repeat(2)[digit] >> ' ' >>
repeat(2)[digit] >> ':' >> repeat(2)[digit] >> ':' >> repeat(2)[digit] >> '.' >> +digit);
start
= '[' >> raw[date_time] >> "] - "
>> double_ >> " s"
>> " => Driver: " >> int_
>> " - Speed: " >> double_
>> " - Road: " >> raw[+graph]
>> " - Km: " >> double_
>> " - " >> lit(event_type) >> ": " >> int_
>> (eol|eoi);
}
private:
qi::rule<It, Attribute()> start;
};
}
LogEvents parse_spirit(It b, It e) {
QiParsers::BaseEventParser<It, ::Sequence> sequence("SEQUENCE");
QiParsers::BaseEventParser<It, ::Clutch> clutch("CLUTCH");
QiParsers::BaseEventParser<It, ::Gear> gear("GEAR");
LogEvents events;
assert(parse(b, e, *boost::spirit::repository::qi::seek[sequence|clutch|gear], events));
return events;
}
static char input[] = /* see question */;
static const size_t len = strlen(input);
int main() {
auto events = parse_spirit(input, input+len);
std::cout << "Events: "
<< events.sequence.size() << " sequence, "
<< events.clutch.size() << " clutch, "
<< events.gear.size() << " gear events\n";
using boost::fusion::operator<<;
for (auto& s : events.sequence) { std::cout << "SEQUENCE: " << s << "\n"; }
for (auto& c : events.clutch) { std::cout << "CLUTCH: " << c << "\n"; }
for (auto& g : events.gear) { std::cout << "GEAR: " << g << "\n"; }
}
翻转它:1vector<variant<>>
有一个变体向量不是更有意义吗?
Live On Coliru
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/repository/include/qi_seek.hpp>
#include <boost/utility/string_view.hpp>
#include <cstring> // strlen
using It = char const*;
namespace MyEvents {
struct BaseEvent {
int driver;
int sequence;
double time;
double vel;
double km;
boost::string_view date;
boost::string_view road;
};
struct Sequence : BaseEvent{};
struct Clutch : BaseEvent{};
struct Gear : BaseEvent{};
using LogEvent = boost::variant<Sequence, Clutch, Gear>;
using LogEvents = std::vector<LogEvent>;
}
BOOST_FUSION_ADAPT_STRUCT(MyEvents::Sequence, date, time, driver, vel, road, km, sequence)
BOOST_FUSION_ADAPT_STRUCT(MyEvents::Clutch, date, time, driver, vel, road, km, sequence)
BOOST_FUSION_ADAPT_STRUCT(MyEvents::Gear, date, time, driver, vel, road, km, sequence)
namespace qi = boost::spirit::qi;
namespace boost { namespace spirit { namespace traits {
template <typename It>
struct assign_to_attribute_from_iterators<boost::string_view, It, void> {
static inline void call(It f, It l, boost::string_view& attr) { attr = boost::string_view { &*f, size_t(std::distance(f,l)) }; }
};
} } }
namespace QiParsers {
template <typename It, typename Attribute>
struct BaseEventParser : qi::grammar<It, Attribute()> {
BaseEventParser(std::string const& event_type) : BaseEventParser::base_type(start) {
using namespace qi;
auto date_time = copy(
repeat(4)[digit] >> '-' >> repeat(3)[alpha] >> '-' >> repeat(2)[digit] >> ' ' >>
repeat(2)[digit] >> ':' >> repeat(2)[digit] >> ':' >> repeat(2)[digit] >> '.' >> +digit);
start
= '[' >> raw[date_time] >> "] - "
>> double_ >> " s"
>> " => Driver: " >> int_
>> " - Speed: " >> double_
>> " - Road: " >> raw[+graph]
>> " - Km: " >> double_
>> " - " >> lit(event_type) >> ": " >> int_
>> (eol|eoi);
}
private:
qi::rule<It, Attribute()> start;
};
template <typename It>
struct LogParser : qi::grammar<It, MyEvents::LogEvents()> {
LogParser() : LogParser::base_type(start) {
using namespace qi;
using boost::spirit::repository::qi::seek;
event = sequence | clutch | gear ; // TODO add types
start = *seek[event];
}
private:
qi::rule<It, MyEvents::LogEvents()> start;
qi::rule<It, MyEvents::LogEvent()> event;
BaseEventParser<It, MyEvents::Sequence> sequence{"SEQUENCE"};
BaseEventParser<It, MyEvents::Clutch> clutch{"CLUTCH"};
BaseEventParser<It, MyEvents::Gear> gear{"GEAR"};
};
}
MyEvents::LogEvents parse_spirit(It b, It e) {
static QiParsers::LogParser<It> const parser {};
MyEvents::LogEvents events;
parse(b, e, parser, events);
return events;
}
static char input[] = /* see question */;
static const size_t len = strlen(input);
namespace MyEvents { // for debug/demo
using boost::fusion::operator<<;
static inline char const* kind(Sequence const&) { return "SEQUENCE"; }
static inline char const* kind(Clutch const&) { return "CLUTCH"; }
static inline char const* kind(Gear const&) { return "GEAR"; }
struct KindVisitor : boost::static_visitor<char const*> {
template <typename T> char const* operator()(T const& ev) const { return kind(ev); }
};
static inline char const* kind(LogEvent const& ev) {
return boost::apply_visitor(KindVisitor{}, ev);
}
}
int main() {
auto events = parse_spirit(input, input+len);
std::cout << "Parsed: " << events.size() << " events\n";
for (auto& e : events)
std::cout << kind(e) << ": " << e << "\n";
}
概括:常见字段和其他事件
特别是如果您继续概括:
Live On Coliru
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/repository/include/qi_seek.hpp>
#include <boost/utility/string_view.hpp>
#include <cstring> // strlen
using It = char const*;
namespace MyEvents {
enum class Kind { Sequence, Clutch, Gear, Slope, Other };
struct CommonFields {
boost::string_view date;
double duration;
};
struct BaseEvent {
CommonFields common;
int driver;
int event_id;
double vel;
double km;
boost::string_view road;
Kind kind;
};
struct OtherEvent {
CommonFields common;
std::string message;
};
using LogEvent = boost::variant<BaseEvent, OtherEvent>;
using LogEvents = std::vector<LogEvent>;
}
BOOST_FUSION_ADAPT_STRUCT(MyEvents::CommonFields, date, duration)
BOOST_FUSION_ADAPT_STRUCT(MyEvents::BaseEvent, common, driver, vel, road, km, kind, event_id)
BOOST_FUSION_ADAPT_STRUCT(MyEvents::OtherEvent, common, message)
namespace qi = boost::spirit::qi;
namespace boost { namespace spirit { namespace traits {
template <typename It>
struct assign_to_attribute_from_iterators<boost::string_view, It, void> {
static inline void call(It f, It l, boost::string_view& attr) { attr = boost::string_view { &*f, size_t(std::distance(f,l)) }; }
};
} } }
namespace QiParsers {
template <typename It>
struct LogParser : qi::grammar<It, MyEvents::LogEvents()> {
using Kind = MyEvents::Kind;
LogParser() : LogParser::base_type(start) {
using namespace qi;
kind.add
("SEQUENCE", Kind::Sequence)
("CLUTCH", Kind::Clutch)
("GEAR", Kind::Gear)
("SLOPE", Kind::Slope)
;
common_fields
= '[' >> raw[
repeat(4)[digit] >> '-' >> repeat(3)[alpha] >> '-' >> repeat(2)[digit] >> ' ' >>
repeat(2)[digit] >> ':' >> repeat(2)[digit] >> ':' >> repeat(2)[digit] >> '.' >> +digit
] >> "]"
>> " - " >> double_ >> " s";
base_event
= common_fields
>> " => Driver: " >> int_
>> " - Speed: " >> double_
>> " - Road: " >> raw[+graph]
>> " - Km: " >> double_
>> " - " >> kind >> ": " >> int_;
other_event
= common_fields
>> " => " >> *~char_("\r\n");
event
= (base_event | other_event)
>> (eol|eoi);
start = *boost::spirit::repository::qi::seek[event];
}
private:
qi::rule<It, MyEvents::LogEvents()> start;
qi::rule<It, MyEvents::LogEvent()> event;
qi::rule<It, MyEvents::CommonFields()> common_fields;
qi::rule<It, MyEvents::BaseEvent()> base_event;
qi::rule<It, MyEvents::OtherEvent()> other_event;
qi::symbols<char, MyEvents::Kind> kind;
};
}
MyEvents::LogEvents parse_spirit(It b, It e) {
static QiParsers::LogParser<It> const parser {};
MyEvents::LogEvents events;
parse(b, e, parser, events);
return events;
}
static char input[] = /* see question */;
static const size_t len = strlen(input);
namespace MyEvents { // for debug/demo
using boost::fusion::operator<<;
static inline Kind getKind(BaseEvent const& be) { return be.kind; }
static inline Kind getKind(OtherEvent const&) { return Kind::Other; }
struct KindVisitor : boost::static_visitor<Kind> {
template <typename T> Kind operator()(T const& ev) const { return getKind(ev); }
};
static inline Kind getKind(LogEvent const& ev) {
return boost::apply_visitor(KindVisitor{}, ev);
}
static inline std::ostream& operator<<(std::ostream& os, Kind k) {
switch(k) {
case Kind::Sequence: return os << "SEQUENCE";
case Kind::Clutch: return os << "CLUTCH";
case Kind::Gear: return os << "GEAR";
case Kind::Slope: return os << "SLOPE";
case Kind::Other: return os << "(Other)";
}
return os;
}
}
int main() {
auto events = parse_spirit(input, input+len);
std::cout << "Parsed: " << events.size() << " events\n";
for (auto& e : events)
std::cout << getKind(e) << ": " << e << "\n";
}
打印例如
Parsed: 37 events
SLOPE: ((2018-Mar-13 13:13:59.580482 0.2) 0 0 A-11 90 SLOPE 0)
GEAR: ((2018-Mar-13 13:14:01.170203 1.79) 0 0 A-11 90 GEAR 0)
GEAR: ((2018-Mar-13 13:14:01.170203 1.79) 0 0.1 A-11 90 GEAR 1)
SEQUENCE: ((2018-Mar-13 13:14:01.819966 2.44) 0 0.1 A-11 90 SEQUENCE 1)
CLUTCH: ((2018-Mar-13 13:14:01.819966 2.44) 0 0.2 A-11 90 CLUTCH 1)
(Other): ((2018-Mar-13 13:14:01.819966 2.54) Backup to regestry)
[...]
奖励:多索引
如果你使用多索引容器,你也可以吃蛋糕。
这是一个示例定义,允许您根据一些相当随意选择的特征来索引向量:
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/global_fun.hpp>
namespace Indexing {
namespace bmi = boost::multi_index;
using MyEvents::LogEvent;
double getDuration(LogEvent const& ev) { return getCommon(ev).duration; }
using Table = bmi::multi_index_container<
std::reference_wrapper<LogEvent const>, //LogEvent,
bmi::indexed_by<
bmi::ordered_non_unique<
bmi::tag<struct primary>,
bmi::composite_key<
LogEvent,
bmi::global_fun<LogEvent const&, MyEvents::Kind, MyEvents::getKind>,
bmi::global_fun<LogEvent const&, int, MyEvents::getEventId>
>
>,
bmi::ordered_non_unique<
bmi::tag<struct duration>,
bmi::global_fun<LogEvent const&, double, getDuration>
>
>
>;
}
现在你可以做一些有趣的事情,比如:
Indexing::Table idx(events.begin(), events.end());
/*
* // To print all events, grouped by by kind and event id:
* for (MyEvents::LogEvent const& e : idx)
* std::cout << getKind(e) << ": " << e << "\n";
*
* // Ordered by duration:
* for (MyEvents::LogEvent const& e : idx.get<Indexing::duration>())
* std::cout << getKind(e) << ": " << e << "\n";
*/
std::cout << "\nAll GEAR events ordered by event id:\n";
for (MyEvents::LogEvent const& e : make_iterator_range(idx.equal_range(make_tuple(Kind::Gear))))
std::cout << getKind(e) << ": " << e << "\n";
std::cout << "\nOnly the SLOPE events with id 10:\n";
for (MyEvents::LogEvent const& e : make_iterator_range(idx.equal_range(make_tuple(Kind::Slope, 10))))
std::cout << getKind(e) << ": " << e << "\n";
std::cout << "\nEvents with durations in [2s..3s):\n";
auto& by_dur = idx.get<Indexing::duration>();
for (MyEvents::LogEvent const& e : make_iterator_range(by_dur.lower_bound(2), by_dur.upper_bound(3)))
std::cout << getKind(e) << ": " << e << "\n";
Live On Coliru
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/repository/include/qi_seek.hpp>
#include <boost/utility/string_view.hpp>
#include <cstring> // strlen
using It = char const*;
namespace MyEvents {
enum class Kind { Sequence, Clutch, Gear, Slope, Other };
struct CommonFields {
boost::string_view date;
double duration;
};
struct BaseEvent {
CommonFields common;
int driver;
int event_id;
double vel;
double km;
boost::string_view road;
Kind kind;
};
struct OtherEvent {
CommonFields common;
std::string message;
};
using LogEvent = boost::variant<BaseEvent, OtherEvent>;
using LogEvents = std::vector<LogEvent>;
}
BOOST_FUSION_ADAPT_STRUCT(MyEvents::CommonFields, date, duration)
BOOST_FUSION_ADAPT_STRUCT(MyEvents::BaseEvent, common, driver, vel, road, km, kind, event_id)
BOOST_FUSION_ADAPT_STRUCT(MyEvents::OtherEvent, common, message)
namespace qi = boost::spirit::qi;
namespace boost { namespace spirit { namespace traits {
template <typename It>
struct assign_to_attribute_from_iterators<boost::string_view, It, void> {
static inline void call(It f, It l, boost::string_view& attr) { attr = boost::string_view { &*f, size_t(std::distance(f,l)) }; }
};
} } }
namespace QiParsers {
template <typename It>
struct LogParser : qi::grammar<It, MyEvents::LogEvents()> {
using Kind = MyEvents::Kind;
LogParser() : LogParser::base_type(start) {
using namespace qi;
kind.add
("SEQUENCE", Kind::Sequence)
("CLUTCH", Kind::Clutch)
("GEAR", Kind::Gear)
("SLOPE", Kind::Slope)
;
common_fields
= '[' >> raw[
repeat(4)[digit] >> '-' >> repeat(3)[alpha] >> '-' >> repeat(2)[digit] >> ' ' >>
repeat(2)[digit] >> ':' >> repeat(2)[digit] >> ':' >> repeat(2)[digit] >> '.' >> +digit
] >> "]"
>> " - " >> double_ >> " s";
base_event
= common_fields
>> " => Driver: " >> int_
>> " - Speed: " >> double_
>> " - Road: " >> raw[+graph]
>> " - Km: " >> double_
>> " - " >> kind >> ": " >> int_;
other_event
= common_fields
>> " => " >> *~char_("\r\n");
event
= (base_event | other_event)
>> (eol|eoi);
start = *boost::spirit::repository::qi::seek[event];
}
private:
qi::rule<It, MyEvents::LogEvents()> start;
qi::rule<It, MyEvents::LogEvent()> event;
qi::rule<It, MyEvents::CommonFields()> common_fields;
qi::rule<It, MyEvents::BaseEvent()> base_event;
qi::rule<It, MyEvents::OtherEvent()> other_event;
qi::symbols<char, MyEvents::Kind> kind;
};
}
MyEvents::LogEvents parse_spirit(It b, It e) {
static QiParsers::LogParser<It> const parser {};
MyEvents::LogEvents events;
parse(b, e, parser, events);
return events;
}
static char input[] = /* see question */;
static const size_t len = strlen(input);
namespace MyEvents { // for debug/demo
using boost::fusion::operator<<;
static inline CommonFields const& getCommon(BaseEvent const& be) { return be.common; }
static inline CommonFields const& getCommon(OtherEvent const& oe) { return oe.common; }
static inline Kind getKind(BaseEvent const& be) { return be.kind; }
static inline Kind getKind(OtherEvent const&) { return Kind::Other; }
static inline int getEventId(BaseEvent const& be) { return be.event_id; }
static inline int getEventId(OtherEvent const&) { return 0; }
#define IMPL_DISPATCH(name, T) \
struct name##Visitor : boost::static_visitor<T> { \
template <typename E> T operator()(E const &ev) const { return name(ev); } \
}; \
static inline T name(LogEvent const &ev) { return boost::apply_visitor(name##Visitor{}, ev); }
IMPL_DISPATCH(getCommon, CommonFields const&)
IMPL_DISPATCH(getKind, Kind)
IMPL_DISPATCH(getEventId, int)
static inline std::ostream& operator<<(std::ostream& os, Kind k) {
switch(k) {
case Kind::Sequence: return os << "SEQUENCE";
case Kind::Clutch: return os << "CLUTCH";
case Kind::Gear: return os << "GEAR";
case Kind::Slope: return os << "SLOPE";
case Kind::Other: return os << "(Other)";
}
return os;
}
}
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/global_fun.hpp>
namespace Indexing {
namespace bmi = boost::multi_index;
using MyEvents::LogEvent;
double getDuration(LogEvent const& ev) { return getCommon(ev).duration; }
using Table = bmi::multi_index_container<
std::reference_wrapper<LogEvent const>, //LogEvent,
bmi::indexed_by<
bmi::ordered_non_unique<
bmi::tag<struct primary>,
bmi::composite_key<
LogEvent,
bmi::global_fun<LogEvent const&, MyEvents::Kind, MyEvents::getKind>,
bmi::global_fun<LogEvent const&, int, MyEvents::getEventId>
>
>,
bmi::ordered_non_unique<
bmi::tag<struct duration>,
bmi::global_fun<LogEvent const&, double, getDuration>
>
>
>;
}
using boost::make_iterator_range;
using boost::make_tuple;
int main() {
using MyEvents::LogEvent;
using MyEvents::Kind;
auto events = parse_spirit(input, input+len);
std::cout << "Parsed: " << events.size() << " events\n";
Indexing::Table idx(events.begin(), events.end());
/*
* // To print all events, grouped by by kind and event id:
* for (MyEvents::LogEvent const& e : idx)
* std::cout << getKind(e) << ": " << e << "\n";
*
* // Ordered by duration:
* for (MyEvents::LogEvent const& e : idx.get<Indexing::duration>())
* std::cout << getKind(e) << ": " << e << "\n";
*/
std::cout << "\nAll GEAR events ordered by event id:\n";
for (MyEvents::LogEvent const& e : make_iterator_range(idx.equal_range(make_tuple(Kind::Gear))))
std::cout << getKind(e) << ": " << e << "\n";
std::cout << "\nOnly the SLOPE events with id 10:\n";
for (MyEvents::LogEvent const& e : make_iterator_range(idx.equal_range(make_tuple(Kind::Slope, 10))))
std::cout << getKind(e) << ": " << e << "\n";
std::cout << "\nEvents with durations in [2s..3s):\n";
auto& by_dur = idx.get<Indexing::duration>();
for (MyEvents::LogEvent const& e : make_iterator_range(by_dur.lower_bound(2), by_dur.upper_bound(3)))
std::cout << getKind(e) << ": " << e << "\n";
}
印刷:
Parsed: 37 events
All GEAR events ordered by event id:
GEAR: ((2018-Mar-13 13:14:01.170203 1.79) 0 0 A-11 90 GEAR 0)
GEAR: ((2018-Mar-13 13:14:01.170203 1.79) 0 0 A-11 90 GEAR 0)
GEAR: ((2018-Mar-13 13:14:01.170203 1.79) 0 0.1 A-11 90 GEAR 1)
GEAR: ((2018-Mar-13 13:14:01.170203 1.79) 0 0.1 A-11 90 GEAR 1)
GEAR: ((2018-Mar-13 13:14:03.250451 3.87) 0 1.2 B-302 90.2 GEAR 2)
GEAR: ((2018-Mar-13 13:14:03.250451 3.87) 0 1.2 B-302 90.2 GEAR 2)
GEAR: ((2018-Mar-13 13:14:04.510025 5.13) 0 4.9 B-302 91.1 GEAR 3)
Only the SLOPE events with id 10:
SLOPE: ((2018-Mar-13 13:14:04.300160 4.92) 0 4.2 B-302 90.9 SLOPE 10)
SLOPE: ((2018-Mar-13 13:14:04.300160 4.92) 0 4.2 B-302 90.9 SLOPE 10)
Events with durations in [2s..3s):
SEQUENCE: ((2018-Mar-13 13:14:01.819966 2.44) 0 0.1 A-11 90 SEQUENCE 1)
CLUTCH: ((2018-Mar-13 13:14:01.819966 2.44) 0 0.2 A-11 90 CLUTCH 1)
SEQUENCE: ((2018-Mar-13 13:14:01.819966 2.44) 0 0.1 A-11 90 SEQUENCE 1)
CLUTCH: ((2018-Mar-13 13:14:01.819966 2.44) 0 0.2 A-11 90 CLUTCH 1)
(Other): ((2018-Mar-13 13:14:01.819966 2.54) Backup to regestry)
(Other): ((2018-Mar-13 13:14:01.819966 2.54) Backup to regestry)