CASM  1.1.0
A Clusters Approach to Statistical Mechanics
jsonPropertiesDatabase.cc
Go to the documentation of this file.
2 
3 #include <boost/filesystem.hpp>
4 
7 #include "casm/global/errors.hh"
8 
9 namespace CASM {
10 namespace DB {
11 
13  std::string calc_type,
14  fs::path location)
15  : PropertiesDatabase(_primclex),
16  m_is_open(false),
17  m_calc_type(calc_type),
18  m_location(location) {}
19 
21  if (m_is_open || m_location.empty() || !fs::exists(m_location)) {
22  m_is_open = true;
23  return *this;
24  }
25 
26  jsonParser json{m_location};
27  this->from_json(json);
28  m_is_open = true;
29  return *this;
30 }
31 
33  if (!m_is_open || m_location.empty()) {
34  return;
35  }
36 
37  jsonParser json;
38  this->to_json(json);
39 
40  SafeOfstream file;
41  fs::create_directories(m_location.parent_path());
42  file.open(m_location);
43  // json.print(file.ofstream());
44  int indent = 0;
45  int prec = 12;
46  json_spirit::write_stream((json_spirit::mValue &)json, file.ofstream(),
47  indent, prec);
48  file.close();
49 }
50 
52  m_data.clear();
53  m_origins.clear();
54  m_is_open = false;
55 }
56 
58  m_data.clear();
59  m_origins.clear();
60 
61  CASM::from_json(m_default_score, json["default_conflict_score"]);
62 
63  {
64  auto it = json["conflict_score"].begin();
65  auto end = json["conflict_score"].end();
66  for (; it != end; ++it) {
67  set_score_method(it.name(), it->get<ScoreMappedProperties>());
68  }
69  }
70 
71  {
72  MappedProperties obj;
73  auto it = json["data"].begin();
74  auto end = json["data"].end();
75  for (; it != end; ++it) {
76  CASM::from_json(obj, *it);
77  insert(obj);
78  }
79  }
80 }
81 
83  if (!json.is_obj()) {
85  "Error in jsonPropertiesDatabase::to_json: Not a JSON object.");
86  }
87  json["data"] = m_data;
88  json["default_conflict_score"] = m_default_score;
89 
90  json["conflict_score"].put_obj();
91  jsonParser &j = json["conflict_score"];
92  for (const auto &val : m_origins) {
93  if (val.second.key_comp().score_method() != m_default_score) {
94  j[val.first] = val.second.key_comp().score_method();
95  }
96  }
97  return json;
98 }
99 
102  return _iterator(m_data.begin());
103 }
104 
107  return _iterator(m_data.end());
108 }
109 
111  return m_data.size();
112 }
113 
119  std::string to_configname) const {
120  auto it = m_origins.find(to_configname);
121  if (it == m_origins.end()) {
122  return end();
123  }
124  // it->second is set of all 'origin' -> 'to'
125  return find_via_origin(*it->second.begin());
126 }
127 
130  std::string origin) const {
131  // m_key.from = from_configname;
132  return _iterator(m_data.find(origin));
133 }
134 
136 std::set<std::string, PropertiesDatabase::Compare>
138  auto it = m_origins.find(to_configname);
139  if (it == m_origins.end()) {
141  } else {
142  return it->second;
143  }
144 }
145 
148  std::string to_configname, const ScoreMappedProperties &score) {
149  auto it = m_origins.find(to_configname);
150  if (it == m_origins.end()) {
151  // do nothing if default score
152  if (score == m_default_score) {
153  return;
154  }
155 
156  auto tmp = _make_set(to_configname, score);
157  m_origins.insert({to_configname, tmp});
158  } else {
159  // if no change, return
160  if (it->second.value_comp().score_method() == score) {
161  return;
162  }
163 
164  // construct new set and copy from old set
165  auto tmp = _make_set(to_configname, score);
166  for (const auto &origin : it->second) {
167  tmp.insert(origin);
168  }
169  it->second = tmp;
170  }
171 }
172 
176 }
177 
179 std::pair<jsonPropertiesDatabase::iterator, bool>
181  auto res = m_data.emplace(value.origin, value);
182  return std::make_pair(_iterator(res.first), res.second);
183 }
184 
187  auto base_it =
188  static_cast<jsonPropertiesDatabaseIterator *>(pos.get())->base();
189  return _iterator(m_data.erase(base_it));
190 }
191 
194  std::string to_configname, const std::set<std::string, Compare> &_set) {
195  auto it = m_origins.find(to_configname);
196  if (it == m_origins.end()) {
197  if (_set.size()) {
198  m_origins.insert({to_configname, _set});
199  }
200  } else {
201  if (!_set.size()) {
202  m_origins.erase(it);
203  } else {
204  it->second = _set;
205  }
206  }
207 }
208 
209 std::set<std::string, PropertiesDatabase::Compare>
211  const ScoreMappedProperties &score) const {
212  return std::set<std::string, Compare>(Compare(this, to_configname, score));
213 }
214 
215 } // namespace DB
216 } // namespace CASM
Fully generic database interface for use by DatabaseHandler.
Definition: Database.hh:25
Compare type for std::set<std::string, Compare>, which sorts to determine the 'origin' configname for...
PropertiesDatabaseIterator iterator
double score(std::string origin) const
Score mapping 'origin'->'to'.
std::pair< iterator, bool > insert(const MappedProperties &value)
Insert data.
Wrapper class for specializations PropertiesDatabaseIteratorBase.
PropertiesDatabaseIteratorBase * get() const
std::set< std::string, Compare > _make_set(std::string to_configname, const ScoreMappedProperties &score) const
jsonPropertiesDatabase(const PrimClex &_primclex, std::string calc_type, fs::path location)
void set_score_method(std::string to_configname, const ScoreMappedProperties &score) override
Change the score method for a single configuration.
iterator end() const override
End iterator.
std::map< std::string, std::set< std::string, Compare > > m_origins
std::set< std::string, Compare > all_origins(std::string to_configname) const override
Names of all configurations that relaxed 'origin'->'to'.
iterator find_via_origin(std::string origin) const override
Return iterator to MappedProperties that is from the specified config.
iterator begin() const override
Begin iterator.
iterator find_via_to(std::string to_configname) const override
Return iterator to MappedProperties that is the best mapping to specified config.
std::map< std::string, MappedProperties > m_data
iterator _erase(iterator pos) override
Private _erase MappedProperties, without modifying 'relaxed_from'.
std::pair< iterator, bool > _insert(const MappedProperties &value) override
Private _insert MappedProperties, without modifying 'relaxed_from'.
void from_json(jsonParser const &json)
Clear all and read from JSON.
void _set_all_origins(std::string to_configname, const std::set< std::string, Compare > &_set) override
Names of all configurations that relaxed 'from'->'to'.
jsonParser & to_json(jsonParser &json) const
Export all to JSON.
iterator _iterator(jsonPropertiesDatabaseIterator::base_iterator _it) const
std::map< std::string, MappedProperties >::const_iterator base_iterator
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:55
Write to a temporary file to ensure a good write, then rename.
Definition: SafeOfstream.hh:31
void close()
Closes stream, and if not a failed write, removes "file" and renames "file.tmp" to "file".
Definition: SafeOfstream.hh:84
void open(fs::path name, std::string tmp_ext="tmp")
Opens "file.tmp" for writing, with intended final target "file".
Definition: SafeOfstream.hh:65
fs::ofstream & ofstream()
Access underlying stream.
Definition: SafeOfstream.hh:80
iterator begin()
Returns const_iterator to beginning of JSON object or JSON array.
Definition: jsonParser.cc:497
iterator end()
Returns iterator to end of JSON object or JSON array.
Definition: jsonParser.cc:520
jsonParser & put_obj()
Puts new empty JSON object.
Definition: jsonParser.hh:354
bool is_obj() const
Check if object type.
Definition: jsonParser.cc:272
GenericDatumFormatter< double, Result > score()
GenericDatumFormatter< std::string, Result > to_configname()
Definition: ConfigData.cc:92
Main CASM namespace.
Definition: APICommand.hh:8
void from_json(ClexDescription &desc, const jsonParser &json)