27 const std::map<std::string, std::string> json_path_regex_replacement_map = {
28 {
"/star/subblock_types/([A-Za-z0-9_]*)/",
"/\\1_type/"},
29 {
"[A-Za-z0-9_]*/types/([A-Za-z0-9_]*)/",
"\\1_type/"},
30 {
"/actions/[A-Za-z0-9_]*/parameters/",
"/"},
31 {
"/parameters/",
"/"},
32 {
"/subblocks/",
"/"}};
34 for (
const auto & el : root[
"global"][
"associated_types"].items())
36 const auto & type = el.key();
37 for (
const auto & el_path : el.value().items())
39 std::string path = el_path.value();
40 for (
const auto & map_iter : json_path_regex_replacement_map)
41 pcrecpp::RE(map_iter.first).GlobalReplace(map_iter.second, &path);
49 for (
const auto & el : root[
"blocks"].items())
69 const nlohmann::json & block,
71 const std::string & parent_name,
72 const nlohmann::json & parameters_in,
73 const nlohmann::json & subblocks_in)
77 addLine(
"'" + block_name + (is_typeblock ?
"_type" :
"") +
"'{");
84 std::string block_decl = (is_typeblock ? parent_name : block_name);
85 bool is_starblock = (block_decl ==
"*" ? true :
false);
93 addLine(
"InputTmpl=MooseBlock");
94 addLine(
"InputName=\"" + block_name +
"\"");
96 addLine(
_level == 1 ?
"InputType=normal_top" :
"InputType=normal_sub");
98 addLine(
_level == 1 ?
"InputType=type_top" :
"InputType=type_sub");
100 addLine(
"InputDefault=\"" + block_decl +
"\"");
102 addLine(
"InputDefault=\"insert_name_here\"");
105 std::string description = block.contains(
"description") ? block[
"description"] :
"";
106 pcrecpp::RE(
"\"").GlobalReplace(
"'", &description);
107 pcrecpp::RE(
"[\r\n]").GlobalReplace(
" ", &description);
108 if (!description.empty())
109 addLine(
"Description=\"" + description +
"\"");
117 if (is_typeblock && !is_starblock)
118 addLine(
"ValEnums=[ \"" + block_decl +
"\" ]");
123 if (block_name ==
"GlobalParams")
139 for (
const auto & el : parameters_in.items())
140 parameters[el.key()] = el.value();
141 if (block.contains(
"parameters"))
143 for (
const auto & el : block[
"parameters"].items())
144 parameters[el.key()] = el.value();
147 if (block.contains(
"actions"))
149 for (
const auto & el : block[
"actions"].items())
150 if (el.value().contains(
"parameters"))
151 for (
const auto & param_el : el.value()[
"parameters"].items())
152 parameters[param_el.key()] = param_el.value();
159 nlohmann::json subblocks = subblocks_in;
160 if (block.contains(
"subblocks"))
162 for (
const auto & el : block[
"subblocks"].items())
163 subblocks[el.key()] = el.value();
165 if (block.contains(
"star"))
166 subblocks[
"*"] = block[
"star"];
171 nlohmann::json typeblocks = block.contains(
"types") ? block[
"types"] : nlohmann::json();
172 if (block.contains(
"subblock_types"))
173 for (
const auto & el : block[
"subblock_types"].items())
174 typeblocks[el.key()] = el.value();
184 if (parameters.contains(
"type") && parameters[
"type"].contains(
"default") &&
185 parameters[
"type"][
"default"].is_string() &&
186 typeblocks.contains(parameters[
"type"][
"default"].get<std::string>()))
188 std::string type_default = parameters[
"type"][
"default"].get<std::string>();
189 const nlohmann::json & default_block = typeblocks[type_default];
190 if (default_block.contains(
"parameters"))
192 nlohmann::json default_child_params = default_block[
"parameters"];
193 if (default_block.contains(
"actions"))
195 const nlohmann::json & default_actions = default_block[
"actions"];
196 for (
const auto & el : default_actions.items())
198 if (el.value().contains(
"parameters"))
199 for (
const auto & param_el : el.value()[
"parameters"].items())
200 default_child_params[param_el.key()] = param_el.value();
206 if (block_name ==
"Mesh" && default_child_params.contains(
"file") &&
207 default_child_params[
"file"].contains(
"required") &&
208 default_child_params[
"file"][
"required"].is_boolean())
209 default_child_params[
"file"][
"required"] =
false;
211 for (
const auto & el : parameters.items())
212 default_child_params[el.key()] = el.value();
220 for (
const auto & el : subblocks.items())
225 addLine(
"} % end block " + block_name + (is_typeblock ?
"_type" :
""));
232 for (
const auto & el : typeblocks.items())
233 addBlock(el.key(), el.value(),
true, block_name, parameters, subblocks);
259 std::vector<std::string> action_set_params;
260 if (params.contains(
"_object_params_set_by_action") &&
261 params[
"_object_params_set_by_action"].contains(
"default"))
263 std::string opsba = nlohmann::to_string(params[
"_object_params_set_by_action"][
"default"]);
264 if (opsba.front() ==
'"' && opsba.back() ==
'"')
266 opsba.erase(opsba.begin());
272 for (
const auto & el : params.items())
274 auto &
name = el.key();
275 auto & param = el.value();
278 if (
name ==
"_object_params_set_by_action")
282 auto backtrack = [](
int level)
284 std::string backtrack_path;
285 for (
int i = 0; i < level; ++i)
286 backtrack_path +=
"../";
287 return backtrack_path;
291 std::string cpp_type = param[
"cpp_type"];
292 std::string basic_type = param[
"basic_type"];
293 bool is_array =
false;
294 if (cpp_type ==
"FunctionExpression" || cpp_type ==
"FunctionName" ||
295 basic_type.compare(0, 6,
"Array:") == 0 || cpp_type.compare(0, 13,
"Eigen::Matrix") == 0)
297 pcrecpp::RE(
".+<([A-Za-z0-9_' ':]*)>.*").GlobalReplace(
"\\1", &cpp_type);
298 pcrecpp::RE(
"(Array:)*(.*)").GlobalReplace(
"\\2", &basic_type);
303 auto def_ptr = param.find(
"default");
305 if (def_ptr != param.end())
306 def = def_ptr->is_string() ? def_ptr->get<std::string>() : nlohmann::to_string(*def_ptr);
308 if (param.contains(
"required") &&
309 std::find(action_set_params.begin(), action_set_params.end(),
name) ==
310 action_set_params.end())
312 bool required = param[
"required"];
313 if (required && def.empty())
315 "/value\" \"" +
name +
"\" ]");
323 addLine(
"InputTmpl=MooseParam");
327 addLine(
"InputType=key_array");
329 addLine(
"InputType=key_value");
335 if (param.contains(
"description"))
337 std::string description = param[
"description"];
338 pcrecpp::RE(
"\"").GlobalReplace(
"'", &description);
339 pcrecpp::RE(
"[\r\n]").GlobalReplace(
" ", &description);
340 if (!description.empty())
341 addLine(
"Description=\"" + description +
"\"");
360 if (basic_type ==
"Integer")
362 else if (basic_type ==
"Real")
366 if (basic_type.find(
"Boolean") != std::string::npos)
367 addLine(
"ValEnums=[ true false 1 0 on off ]");
370 std::string options = param[
"options"];
371 if (!options.empty())
373 pcrecpp::RE(
" ").GlobalReplace(
"\" \"", &options);
374 if (!param[
"out_of_range_allowed"])
375 addLine(
"ValEnums=[ \"" + options +
"\" ]");
377 addLine(
"InputChoices=[ \"" + options +
"\" ]");
386 if (param.contains(
"reserved_values"))
388 for (
const auto & reserved : param[
"reserved_values"])
389 choices += nlohmann::to_string(reserved) +
" ";
393 choices +=
"PATH:\"" + backtrack(
_level) + path +
"/decl\" ";
394 if (!choices.empty())
395 addLine(
"InputChoices=[ " + choices +
"]");
398 if (cpp_type.compare(0, 8,
"unsigned") == 0 && basic_type ==
"Integer")
403 addLine(
"InputDefault=\"" + def +
"\"");
std::string name(const ElemQuality q)
std::vector< std::string > split(const std::string &str, const std::string &delimiter, std::size_t max_count=std::numeric_limits< std::size_t >::max())
Python like split functions for strings.
std::string trim(const std::string &str, const std::string &white_space=" \\\)
Standard scripting language trim function.