diff options
Diffstat (limited to 'generator/group.cpp')
| -rw-r--r-- | generator/group.cpp | 119 |
1 files changed, 119 insertions, 0 deletions
| diff --git a/generator/group.cpp b/generator/group.cpp new file mode 100644 index 0000000..7cbd4c8 --- /dev/null +++ b/generator/group.cpp | |||
| @@ -0,0 +1,119 @@ | |||
| 1 | #include "group.h" | ||
| 2 | #include <stdexcept> | ||
| 3 | #include <list> | ||
| 4 | #include <json.hpp> | ||
| 5 | #include "database.h" | ||
| 6 | #include "field.h" | ||
| 7 | #include "frame.h" | ||
| 8 | |||
| 9 | namespace verbly { | ||
| 10 | namespace generator { | ||
| 11 | |||
| 12 | int group::nextId_ = 0; | ||
| 13 | |||
| 14 | group::group() : id_(nextId_++) | ||
| 15 | { | ||
| 16 | } | ||
| 17 | |||
| 18 | void group::setParent(const group& parent) | ||
| 19 | { | ||
| 20 | // Adding a group to itself is nonsensical. | ||
| 21 | assert(&parent != this); | ||
| 22 | |||
| 23 | parent_ = &parent; | ||
| 24 | } | ||
| 25 | |||
| 26 | void group::addRole(std::string name, role r) | ||
| 27 | { | ||
| 28 | roleNames_.insert(name); | ||
| 29 | roles_[name] = std::move(r); | ||
| 30 | } | ||
| 31 | |||
| 32 | void group::addFrame(const frame& f) | ||
| 33 | { | ||
| 34 | frames_.insert(&f); | ||
| 35 | } | ||
| 36 | |||
| 37 | std::set<std::string> group::getRoles() const | ||
| 38 | { | ||
| 39 | std::set<std::string> fullRoles = roleNames_; | ||
| 40 | |||
| 41 | if (hasParent()) | ||
| 42 | { | ||
| 43 | for (std::string name : getParent().getRoles()) | ||
| 44 | { | ||
| 45 | fullRoles.insert(name); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | return fullRoles; | ||
| 50 | } | ||
| 51 | |||
| 52 | const role& group::getRole(std::string name) const | ||
| 53 | { | ||
| 54 | if (roles_.count(name)) | ||
| 55 | { | ||
| 56 | return roles_.at(name); | ||
| 57 | } else if (hasParent()) | ||
| 58 | { | ||
| 59 | return getParent().getRole(name); | ||
| 60 | } else { | ||
| 61 | throw std::invalid_argument("Specified role not found in verb group"); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | std::set<const frame*> group::getFrames() const | ||
| 66 | { | ||
| 67 | std::set<const frame*> fullFrames = frames_; | ||
| 68 | |||
| 69 | if (hasParent()) | ||
| 70 | { | ||
| 71 | for (const frame* f : getParent().getFrames()) | ||
| 72 | { | ||
| 73 | fullFrames.insert(f); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | return fullFrames; | ||
| 78 | } | ||
| 79 | |||
| 80 | database& operator<<(database& db, const group& arg) | ||
| 81 | { | ||
| 82 | // Serialize the group first | ||
| 83 | { | ||
| 84 | std::list<field> fields; | ||
| 85 | fields.emplace_back("group_id", arg.getId()); | ||
| 86 | |||
| 87 | nlohmann::json jsonRoles; | ||
| 88 | for (std::string name : arg.getRoles()) | ||
| 89 | { | ||
| 90 | const role& r = arg.getRole(name); | ||
| 91 | |||
| 92 | nlohmann::json jsonRole; | ||
| 93 | jsonRole["type"] = name; | ||
| 94 | jsonRole["selrestrs"] = r.getSelrestrs().toJson(); | ||
| 95 | |||
| 96 | jsonRoles.emplace_back(std::move(jsonRole)); | ||
| 97 | } | ||
| 98 | |||
| 99 | fields.emplace_back("data", jsonRoles.dump()); | ||
| 100 | |||
| 101 | db.insertIntoTable("groups", std::move(fields)); | ||
| 102 | } | ||
| 103 | |||
| 104 | // Then, serialize the group/frame relationship | ||
| 105 | for (const frame* f : arg.getFrames()) | ||
| 106 | { | ||
| 107 | std::list<field> fields; | ||
| 108 | |||
| 109 | fields.emplace_back("group_id", arg.getId()); | ||
| 110 | fields.emplace_back("frame_id", f->getId()); | ||
| 111 | |||
| 112 | db.insertIntoTable("groups_frames", std::move(fields)); | ||
| 113 | } | ||
| 114 | |||
| 115 | return db; | ||
| 116 | } | ||
| 117 | |||
| 118 | }; | ||
| 119 | }; | ||
