souffle  2.0.2-371-g6315b36
Public Member Functions | Protected Member Functions | Protected Attributes
souffle::ReadStreamJSON Class Reference

#include <ReadStreamJSON.h>

Inheritance diagram for souffle::ReadStreamJSON:
Inheritance graph
Collaboration diagram for souffle::ReadStreamJSON:
Collaboration graph

Public Member Functions

 ReadStreamJSON (std::istream &file, const std::map< std::string, std::string > &rwOperation, SymbolTable &symbolTable, RecordTable &recordTable)
 
- Public Member Functions inherited from souffle::ReadStream
template<typename T >
void readAll (T &relation)
 
- Public Member Functions inherited from souffle::SerialisationStream< false >
virtual ~SerialisationStream ()=default
 

Protected Member Functions

RamDomain readNextElementList (const Json &source, const std::string &recordTypeName)
 
RamDomain readNextElementObject (const Json &source, const std::string &recordTypeName)
 
Own< RamDomain[]> readNextTuple () override
 
Own< RamDomain[]> readNextTupleList ()
 
Own< RamDomain[]> readNextTupleObject ()
 
- Protected Member Functions inherited from souffle::ReadStream
void consumeChar (const std::string &str, char c, size_t &pos)
 Read past given character, consuming any preceding whitespace. More...
 
void consumeWhiteSpace (const std::string &str, size_t &pos)
 Advance position in the string until first non-whitespace character. More...
 
RamDomain readADT (const std::string &source, const std::string &adtName, size_t pos=0, size_t *charactersRead=nullptr)
 
std::string readAlphanumeric (const std::string &source, size_t &pos)
 Read the next alphanumeric sequence (corresponding to IDENT). More...
 
RamDomain readRecord (const std::string &source, const std::string &recordTypeName, size_t pos=0, size_t *charactersRead=nullptr)
 Read a record from a string. More...
 
 ReadStream (const std::map< std::string, std::string > &rwOperation, SymbolTable &symTab, RecordTable &recTab)
 
std::string readUntil (const std::string &source, const std::string stopChars, const size_t pos, size_t *charactersRead)
 
- Protected Member Functions inherited from souffle::SerialisationStream< false >
 SerialisationStream (RO< SymbolTable > &symTab, RO< RecordTable > &recTab, const std::map< std::string, std::string > &rwOperation)
 
 SerialisationStream (RO< SymbolTable > &symTab, RO< RecordTable > &recTab, Json types)
 
 SerialisationStream (RO< SymbolTable > &symTab, RO< RecordTable > &recTab, Json types, std::vector< std::string > relTypes, size_t auxArity=0)
 

Protected Attributes

std::istream & file
 
bool isInitialized
 
Json jsonSource
 
std::map< const std::string, const size_t > paramIndex
 
Json params
 
size_t pos
 
bool useObjects
 
- Protected Attributes inherited from souffle::SerialisationStream< false >
size_t arity
 
size_t auxiliaryArity
 
RO< RecordTable > & recordTable
 
RO< SymbolTable > & symbolTable
 
std::vector< std::string > typeAttributes
 
Json types
 

Additional Inherited Members

- Protected Types inherited from souffle::SerialisationStream< false >
using RO = std::conditional_t< readOnlyTables, const A, A >
 

Detailed Description

Definition at line 46 of file ReadStreamJSON.h.

Constructor & Destructor Documentation

◆ ReadStreamJSON()

souffle::ReadStreamJSON::ReadStreamJSON ( std::istream &  file,
const std::map< std::string, std::string > &  rwOperation,
SymbolTable symbolTable,
RecordTable recordTable 
)
inline

Definition at line 48 of file ReadStreamJSON.h.

49  {
50  fatal("cannot get internal params: %s", err);
51  }
52  }
53 
54 protected:
55  std::istream& file;
56  size_t pos;

Member Function Documentation

◆ readNextElementList()

RamDomain souffle::ReadStreamJSON::readNextElementList ( const Json source,
const std::string &  recordTypeName 
)
inlineprotected

Definition at line 153 of file ReadStreamJSON.h.

153  : " + recordTypeName);
154  }
155 
156  // Handle null case
157  if (source.is_null()) {
158  return 0;
159  }
160 
161  assert(source.is_array() && "the input is not json array");
162  auto&& recordTypes = recordInfo["types"];
163  const size_t recordArity = recordInfo["arity"].long_value();
164  std::vector<RamDomain> recordValues(recordArity);
165  for (size_t i = 0; i < recordArity; ++i) {
166  const std::string& recordType = recordTypes[i].string_value();
167  switch (recordType[0]) {
168  case 's': {
169  recordValues[i] = symbolTable.unsafeLookup(source[i].string_value());
170  break;
171  }
172  case 'r': {
173  recordValues[i] = readNextElementList(source[i], recordType);
174  break;
175  }
176  case 'i': {
177  recordValues[i] = source[i].int_value();
178  break;
179  }
180  case 'u': {
181  recordValues[i] = source[i].int_value();
182  break;
183  }
184  case 'f': {
185  recordValues[i] = static_cast<RamDomain>(source[i].number_value());
186  break;
187  }
188  default: fatal("invalid type attribute");
189  }
190  }
191 
192  return recordTable.pack(recordValues.data(), recordValues.size());
193  }
194 
195  Own<RamDomain[]> readNextTupleObject() {
196  if (pos >= jsonSource.array_items().size()) {
197  return nullptr;

◆ readNextElementObject()

RamDomain souffle::ReadStreamJSON::readNextElementObject ( const Json source,
const std::string &  recordTypeName 
)
inlineprotected

Definition at line 249 of file ReadStreamJSON.h.

251  : params["records"][recordName]["params"].array_items()) {
252  recordIndex.insert(std::make_pair(param.string_value(), index_pos));
253  index_pos++;
254  }
255 
256  if (recordInfo.is_null()) {
257  throw std::invalid_argument("Missing record type information: " + recordTypeName);
258  }
259 
260  // Handle null case
261  if (source.is_null()) {
262  return 0;
263  }
264 
265  assert(source.is_object() && "the input is not json object");
266  auto&& recordTypes = recordInfo["types"];
267  const size_t recordArity = recordInfo["arity"].long_value();
268  std::vector<RamDomain> recordValues(recordArity);
269  recordValues.reserve(recordIndex.size());
270  for (auto readParam : source.object_items()) {
271  // get the corresponding position by parameter name
272  if (recordIndex.find(readParam.first) == recordIndex.end()) {
273  fatal("invalid parameter: %s", readParam.first);
274  }
275  size_t i = recordIndex.at(readParam.first);
276  auto&& type = recordTypes[i].string_value();
277  switch (type[0]) {
278  case 's': {
279  recordValues[i] = symbolTable.unsafeLookup(readParam.second.string_value());
280  break;
281  }
282  case 'r': {
283  recordValues[i] = readNextElementObject(readParam.second, type);
284  break;
285  }
286  case 'i': {
287  recordValues[i] = readParam.second.int_value();
288  break;
289  }
290  case 'u': {
291  recordValues[i] = readParam.second.int_value();
292  break;
293  }
294  case 'f': {
295  recordValues[i] = static_cast<RamDomain>(readParam.second.number_value());
296  break;
297  }
298  default: fatal("invalid type attribute: `%c`", type[0]);
299  }
300  }
301 
302  return recordTable.pack(recordValues.data(), recordValues.size());
303  }
304 };
305 
306 class ReadFileJSON : public ReadStreamJSON {
307 public:

◆ readNextTuple()

Own<RamDomain[]> souffle::ReadStreamJSON::readNextTuple ( )
inlineoverrideprotectedvirtual

Implements souffle::ReadStream.

Definition at line 67 of file ReadStreamJSON.h.

69  {});
70 
71  jsonSource = Json::parse(source, error);
72  // it should be wrapped by an extra array
73  if (error.length() > 0 || !jsonSource.is_array()) {
74  fatal("cannot deserialize json because %s:\n%s", error, source);
75  }
76 
77  // we only check the first one, since there are extra checks
78  // in readNextTupleObject/readNextTupleList
79  if (jsonSource[0].is_array()) {
80  useObjects = false;
81  } else if (jsonSource[0].is_object()) {
82  useObjects = true;
83  size_t index_pos = 0;
84  for (auto param : params["relation"]["params"].array_items()) {
85  paramIndex.insert(std::make_pair(param.string_value(), index_pos));
86  index_pos++;
87  }
88  } else {
89  fatal("the input is neither list nor object format");
90  }
91  }
92 
93  if (useObjects) {
94  return readNextTupleObject();
95  } else {
96  return readNextTupleList();
97  }
98  }
99 
100  Own<RamDomain[]> readNextTupleList() {
101  if (pos >= jsonSource.array_items().size()) {
102  return nullptr;

◆ readNextTupleList()

Own<RamDomain[]> souffle::ReadStreamJSON::readNextTupleList ( )
inlineprotected

Definition at line 104 of file ReadStreamJSON.h.

109  {
110  try {
111  auto&& ty = typeAttributes.at(i);
112  switch (ty[0]) {
113  case 's': {
114  tuple[i] = symbolTable.unsafeLookup(jsonObj[i].string_value());
115  break;
116  }
117  case 'r': {
118  tuple[i] = readNextElementList(jsonObj[i], ty);
119  break;
120  }
121  case 'i': {
122  tuple[i] = jsonObj[i].int_value();
123  break;
124  }
125  case 'u': {
126  tuple[i] = jsonObj[i].int_value();
127  break;
128  }
129  case 'f': {
130  tuple[i] = static_cast<RamDomain>(jsonObj[i].number_value());
131  break;
132  }
133  default: fatal("invalid type attribute: `%c`", ty[0]);
134  }
135  } catch (...) {
136  std::stringstream errorMessage;
137  if (jsonObj.is_array() && i < jsonObj.array_items().size()) {
138  errorMessage << "Error converting: " << jsonObj[i].dump();
139  } else {
140  errorMessage << "Invalid index: " << i;
141  }
142  throw std::invalid_argument(errorMessage.str());
143  }
144  }
145 
146  return tuple;
147  }
148 
149  RamDomain readNextElementList(const Json& source, const std::string& recordTypeName) {
150  auto&& recordInfo = types["records"][recordTypeName];
151 

◆ readNextTupleObject()

Own<RamDomain[]> souffle::ReadStreamJSON::readNextTupleObject ( )
inlineprotected

Definition at line 199 of file ReadStreamJSON.h.

204  : jsonObj.object_items()) {
205  try {
206  // get the corresponding position by parameter name
207  if (paramIndex.find(p.first) == paramIndex.end()) {
208  fatal("invalid parameter: %s", p.first);
209  }
210  size_t i = paramIndex.at(p.first);
211  auto&& ty = typeAttributes.at(i);
212  switch (ty[0]) {
213  case 's': {
214  tuple[i] = symbolTable.unsafeLookup(p.second.string_value());
215  break;
216  }
217  case 'r': {
218  tuple[i] = readNextElementObject(p.second, ty);
219  break;
220  }
221  case 'i': {
222  tuple[i] = p.second.int_value();
223  break;
224  }
225  case 'u': {
226  tuple[i] = p.second.int_value();
227  break;
228  }
229  case 'f': {
230  tuple[i] = static_cast<RamDomain>(p.second.number_value());
231  break;
232  }
233  default: fatal("invalid type attribute: `%c`", ty[0]);
234  }
235  } catch (...) {
236  std::stringstream errorMessage;
237  errorMessage << "Error converting: " << p.second.dump();
238  throw std::invalid_argument(errorMessage.str());
239  }
240  }
241 
242  return tuple;
243  }
244 
245  RamDomain readNextElementObject(const Json& source, const std::string& recordTypeName) {
246  auto&& recordInfo = types["records"][recordTypeName];
247  const std::string recordName = recordTypeName.substr(2);

Field Documentation

◆ file

std::istream& souffle::ReadStreamJSON::file
protected

Definition at line 59 of file ReadStreamJSON.h.

◆ isInitialized

bool souffle::ReadStreamJSON::isInitialized
protected

Definition at line 63 of file ReadStreamJSON.h.

◆ jsonSource

Json souffle::ReadStreamJSON::jsonSource
protected

Definition at line 61 of file ReadStreamJSON.h.

◆ paramIndex

std::map<const std::string, const size_t> souffle::ReadStreamJSON::paramIndex
protected

Definition at line 65 of file ReadStreamJSON.h.

◆ params

Json souffle::ReadStreamJSON::params
protected

Definition at line 62 of file ReadStreamJSON.h.

◆ pos

size_t souffle::ReadStreamJSON::pos
protected

Definition at line 60 of file ReadStreamJSON.h.

◆ useObjects

bool souffle::ReadStreamJSON::useObjects
protected

Definition at line 64 of file ReadStreamJSON.h.


The documentation for this class was generated from the following file:
souffle::SerialisationStream< false >::recordTable
RO< RecordTable > & recordTable
Definition: SerialisationStream.h:72
err
std::string & err
Definition: json11.h:664
souffle::ReadStreamJSON::readNextElementList
RamDomain readNextElementList(const Json &source, const std::string &recordTypeName)
Definition: ReadStreamJSON.h:153
souffle::ReadStreamJSON::file
std::istream & file
Definition: ReadStreamJSON.h:59
json11::Json::parse
static Json parse(const std::string &in, std::string &err, JsonParse strategy=JsonParse::STANDARD)
Definition: json11.h:1071
souffle::ReadStreamJSON::readNextTupleList
Own< RamDomain[]> readNextTupleList()
Definition: ReadStreamJSON.h:104
souffle::RamDomain
int32_t RamDomain
Definition: RamTypes.h:56
souffle::ReadStreamJSON::pos
size_t pos
Definition: ReadStreamJSON.h:60
souffle::ReadStreamJSON::jsonSource
Json jsonSource
Definition: ReadStreamJSON.h:61
souffle::SerialisationStream< false >::symbolTable
RO< SymbolTable > & symbolTable
Definition: SerialisationStream.h:71
souffle::SerialisationStream< false >::typeAttributes
std::vector< std::string > typeAttributes
Definition: SerialisationStream.h:74
array
souffle::SerialisationStream< false >::types
Json types
Definition: SerialisationStream.h:73
json11::Json::array_items
const array & array_items() const
Definition: json11.h:554
i
size_t i
Definition: json11.h:663
souffle::ReadStreamJSON::useObjects
bool useObjects
Definition: ReadStreamJSON.h:64
souffle::ast::DirectiveType::input
@ input
json11::Json::is_array
bool is_array() const
Definition: json11.h:147
souffle::ReadStreamJSON::paramIndex
std::map< const std::string, const size_t > paramIndex
Definition: ReadStreamJSON.h:65
souffle::ReadStreamJSON::ReadStreamJSON
ReadStreamJSON(std::istream &file, const std::map< std::string, std::string > &rwOperation, SymbolTable &symbolTable, RecordTable &recordTable)
Definition: ReadStreamJSON.h:48
souffle::ReadStreamJSON::params
Json params
Definition: ReadStreamJSON.h:62
souffle::fatal
void fatal(const char *format, const Args &... args)
Definition: MiscUtil.h:198
souffle::SerialisationStream< false >::arity
size_t arity
Definition: SerialisationStream.h:76
souffle::ReadStreamJSON::readNextElementObject
RamDomain readNextElementObject(const Json &source, const std::string &recordTypeName)
Definition: ReadStreamJSON.h:249
souffle::ReadStreamJSON::readNextTupleObject
Own< RamDomain[]> readNextTupleObject()
Definition: ReadStreamJSON.h:199
std::type
ElementType type
Definition: span.h:640
p
a horizontalBars(j=m=void 0===a.axisX.type?new c.AutoScaleAxis(c.Axis.units.x, b.normalized.series, o, c.extend({}, a.axisX,{highLow:d, referenceValue:0})):a.axisX.type.call(c, c.Axis.units.x, b.normalized.series, o, c.extend({}, a.axisX,{highLow:d, referenceValue:0})), l=n=void 0===a.axisY.type?new c.StepAxis(c.Axis.units.y, b.normalized.series, o,{ticks:k}):a.axisY.type.call(c, c.Axis.units.y, b.normalized.series, o, a.axisY)) var p
Definition: htmlJsChartistMin.h:15