summary refs log tree commit diff stats
path: root/generator/group.cpp
blob: cebe2b9fbb88f0ba476d9ec6e53c46fb24256491 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "group.h"
#include <stdexcept>
#include <list>
#include <json.hpp>
#include "database.h"
#include "field.h"
#include "frame.h"

namespace verbly {
  namespace generator {

    int group::nextId_ = 0;

    group::group() : id_(nextId_++)
    {
    }

    void group::setParent(const group& parent)
    {
      // Adding a group to itself is nonsensical.
      assert(&parent != this);

      parent_ = &parent;
    }

    void group::addRole(role r)
    {
      std::string name = r.getName();
      roles_[name] = std::move(r);
      roleNames_.insert(std::move(name));
    }

    void group::addFrame(const frame& f)
    {
      frames_.insert(&f);
    }

    std::set<std::string> group::getRoles() const
    {
      std::set<std::string> fullRoles = roleNames_;

      if (hasParent())
      {
        for (std::string name : getParent().getRoles())
        {
          fullRoles.insert(name);
        }
      }

      return fullRoles;
    }

    const role& group::getRole(std::string name) const
    {
      if (roles_.count(name))
      {
        return roles_.at(name);
      } else if (hasParent())
      {
        return getParent().getRole(name);
      } else {
        throw std::invalid_argument("Specified role not found in verb group");
      }
    }

    std::set<const frame*> group::getFrames() const
    {
      std::set<const frame*> fullFrames = frames_;

      if (hasParent())
      {
        for (const frame* f : getParent().getFrames())
        {
          fullFrames.insert(f);
        }
      }

      return fullFrames;
    }

    database& operator<<(database& db, const group& arg)
    {
      // Serialize the group first
      {
        std::list<field> fields;
        fields.emplace_back("group_id", arg.getId());

        nlohmann::json jsonRoles;
        for (std::string name : arg.getRoles())
        {
          const role& r = arg.getRole(name);

          nlohmann::json jsonRole;
          jsonRole["type"] = name;
          jsonRole["selrestrs"] = r.getSelrestrs().toJson();

          jsonRoles.emplace_back(std::move(jsonRole));
        }

        fields.emplace_back("data", jsonRoles.dump());

        db.insertIntoTable("groups", std::move(fields));
      }

      // Then, serialize the group/frame relationship
      for (const frame* f : arg.getFrames())
      {
        std::list<field> fields;

        fields.emplace_back("group_id", arg.getId());
        fields.emplace_back("frame_id", f->getId());

        db.insertIntoTable("groups_frames", std::move(fields));
      }

      return db;
    }

  };
};