CellModules
SchemeSQL.hpp
Go to the documentation of this file.
1//'''
2#pragma once
3
4#include <string>
5#include <sstream>
6
7/*isigen:eval@utilityDeclarations'''
8 attributesToIgnore = ['type', 'state']
9 correctAttribute = [a for a in builderData['CellAttributesRecorded'] if a['value'] not in attributesToIgnore]
10 correctGetters = builderData['CellFunctionsRecorded'] if 'CellFunctionsRecorded' in builderData else []
11 nbDouble = len([a for a in correctAttribute if a['type'] == 'double']) + len([a for a in correctGetters if a['type'] == 'double'])
12 nbInt = len([a for a in correctAttribute if (a['type'] == 'int') or ((a['type'] in builderData['enums']) if 'enums' in builderData else False)])
13 nbInt += len([a for a in correctGetters if (a['type'] == 'int') or ((a['type'] in builderData['enums']) if 'enums' in builderData else False)])
14 nbBool = len([a for a in correctAttribute if a['type'] == 'bool']) + len([a for a in correctGetters if a['type'] == 'bool'])
15 def convert(type) :
16 if type == 'bool':
17 return "BOOLEAN"
18 elif type == 'double':
19 return "DOUBLE PRECISION"
20 elif (type == "int") | ((type in builderData['enums']) if 'enums' in builderData else False):
21 return "INTEGER"
22 else:
23 raise Exception(f'Unknown type {type} for cell attribute')
24
25 def typeToFunction(type):
26 if type == 'bool':
27 return 'appendBool'
28 elif type == 'double':
29 return 'appendDouble'
30 elif (type == 'int') or ((type in builderData['enums']) if 'enums' in builderData else False):
31 return 'appendInt'
32 else:
33 raise Exception(f'Unknown type {type} for cell attribute')
34
35 def cast_enum(a):
36 if a['type'] not in builderData['enums']:
37 return 'c->'+a['value']
38 else:
39 return 'static_cast<unsigned int>(c->'+a['value']+')'
40 '''*/
41
42namespace PluginSQL {
43
44 struct Scheme {
45
46 public:
47 const std::string WorldTable;
48 const std::string CellTable;
49
52
53 const std::string WorldTableDataLayout;
54 const std::string CellTableDataLayout;
55
56
57 Scheme(const std::string &simulation_id) :
58 WorldTable(simulation_id + "_world"),
59 CellTable(simulation_id + "_cells"),
60 WorldTableCreationStatement("CREATE UNLOGGED TABLE " + simulation_id + "_world (" +
61 "step integer NOT NULL," +
62 "num_cells integer NOT NULL," +
63 "hours DOUBLE PRECISION NOT NULL" +
64 "); "),
65 CellTableCreationStatement("CREATE UNLOGGED TABLE " + simulation_id + "_cells (" +
66 /*isigen:insert@CreateTableCells'''
67 print('\n'.join(['"{value}\t{type}\t\tNOT NULL," +'.format(value=a['value'], type=convert(a['type'])) for a in correctAttribute]))
68 print('\n'.join(['"{value}\t{type}\t\tNOT NULL," +'.format(value=a['value'][:-2], type=convert(a['type'])) for a in correctGetters]))
69 '''*/
70 "step integer NOT NULL," +
71 "cell_id integer NOT NULL," +
72 "type integer NOT NULL," +
73 "state integer NOT NULL," +
74 "x DOUBLE PRECISION NOT NULL," +
75 "y DOUBLE PRECISION NOT NULL," +
76 "z DOUBLE PRECISION NOT NULL," +
77 "radius DOUBLE PRECISION NOT NULL);"),
78 CellTableDataLayout(std::string("(") +
79 /*isigen:insert@CellTableDataLayout'''
80 print(('"'+', '.join([a['value'] for a in correctAttribute])+'," +') if len(correctAttribute)>0 else '')
81 print(('"'+', '.join([a['value'][:-2] for a in correctGetters])+'," +') if len(correctGetters)>0 else '')
82 '''*/
83 "step, cell_id, type, state, x, y, z, radius)"),
84 WorldTableDataLayout("(step, num_cells, hours)")
85 {}
86
87 /*isigen:insert@CellValueStatement'''
88 print(f'static constexpr size_t CellRowSize = {2+(nbDouble+4)*12+(nbInt+4)*8+nbBool*5};')
89 '''*/
90 static constexpr size_t WorldRowSize = 2 + 2*8 + 1*12;
91
92 template<typename cell_t, typename world_t>
93 static void CellValueBinary(Connection &conn, cell_t* c, world_t* w) {
94 /*isigen:insert@CellValueStatement'''
95 print(f'conn.beginRow({8+len(correctAttribute)+len(correctGetters)});')
96 print('\n'.join(['conn.{addFunc}(c->{value});'.format(value=a['value'], addFunc=typeToFunction(a['type'])) for a in correctAttribute]))
97 print('\n'.join(['conn.{addFunc}(c->{value});'.format(value=a['value'], addFunc=typeToFunction(a['type'])) for a in correctGetters]))
98 '''*/
99 conn.appendInt(w->getNbUpdates());
100 conn.appendInt(c->getId());
101 conn.appendInt(c->getType());
102 conn.appendInt(c->getState());
103 auto pos = c->getBody().getPosition();
104 conn.appendDouble(pos.x());
105 conn.appendDouble(pos.y());
106 conn.appendDouble(pos.z());
107 conn.appendDouble(c->getBody().getBoundingBoxRadius());
108 }
109
110 template<typename world_t>
111 static void WorldValueBinary(Connection &conn, world_t *w) {
112 conn.beginRow(3);
113 conn.appendInt(w->getNbUpdates());
114 conn.appendInt(w->cells.size());
115 conn.appendDouble(w->getNbUpdates() * w->dt / 3600.);
116 }
117
118 template<typename cell_t, typename world_t>
119 static void CellValueStatement(std::stringstream& ss, cell_t *c, world_t *w) {
120 auto pos = c->getBody().getPosition();
121 ss << "(";
122 /*isigen:insert@CellValueStatement'''
123 print('\n'.join(['ss << "CAST(" << {value} << " AS {type})" << ", ";'.format(value=cast_enum(a), type=convert(a['type'])) for a in correctAttribute]))
124 print('\n'.join(['ss << "CAST(" << {value} << " AS {type})" << ", ";'.format(value=cast_enum(a), type=convert(a['type'])) for a in correctGetters]))
125 '''*/
126 ss << w->getNbUpdates() << ", ";
127 ss << c->getId() << ", ";
128 ss << static_cast<unsigned int>(c->getType()) << ",";
129 ss << static_cast<unsigned int>(c->getState()) << ",";
130 ss << "CAST(" << pos.x() << " AS DOUBLE PRECISION), ";
131 ss << "CAST(" << pos.y() << " AS DOUBLE PRECISION), ";
132 ss << "CAST(" << pos.z() << " AS DOUBLE PRECISION), ";
133 ss << "CAST(" << c->getBody().getBoundingBoxRadius() << " AS DOUBLE PRECISION)";
134 ss << " )";
135 }
136 template<typename world_t>
137 static std::string WorldValueStatement(world_t *w) {
138 std::stringstream ss;
139 ss << "(";
140 ss << w->getNbUpdates() << ", ";
141 ss << w->cells.size() << ", ";
142 ss << w->getNbUpdates() * w->dt / 3600;
143 ss << " )";
144
145 return ss.str();
146 }
147
148 };
149}
150//'''
MecaCell::Vec pos
Definition: CellBody.hpp:34
double x() const
Definition: vector3D.h:94
double y() const
Definition: vector3D.h:100
double z() const
Definition: vector3D.h:106
void appendDouble(double d)
Definition: Connection.hpp:140
void beginRow(int16_t nCols)
Definition: Connection.hpp:111
Provides common mathematical functions and vector operations.
Definition: std.hpp:4
const std::string WorldTable
Definition: SchemeSQL.hpp:47
const std::string WorldTableDataLayout
Definition: SchemeSQL.hpp:53
static void CellValueStatement(std::stringstream &ss, cell_t *c, world_t *w)
Definition: SchemeSQL.hpp:119
const std::string CellTable
Definition: SchemeSQL.hpp:48
const std::string CellTableDataLayout
Definition: SchemeSQL.hpp:54
std::string WorldTableCreationStatement
Definition: SchemeSQL.hpp:50
std::string CellTableCreationStatement
Definition: SchemeSQL.hpp:51
static std::string WorldValueStatement(world_t *w)
Definition: SchemeSQL.hpp:137
Scheme(const std::string &simulation_id)
Definition: SchemeSQL.hpp:57
static void CellValueBinary(Connection &conn, cell_t *c, world_t *w)
Definition: SchemeSQL.hpp:93
static constexpr size_t WorldRowSize
Definition: SchemeSQL.hpp:90
static void WorldValueBinary(Connection &conn, world_t *w)
Definition: SchemeSQL.hpp:111