docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
transformation_schema.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4
5#include <nlohmann/json.hpp>
6
7namespace sdfg {
8namespace transformations {
9
19const std::string& transformation_schema_text();
20
25const nlohmann::json& transformation_schema();
26
45inline bool validate_transformation_schema(const nlohmann::json& j, std::string& error_out) {
46 if (!j.is_object()) {
47 error_out = "transformation description must be an object";
48 return false;
49 }
50
51 // transformation_type
52 if (!j.contains("transformation_type")) {
53 error_out = "missing 'transformation_type'";
54 return false;
55 }
56 if (!j["transformation_type"].is_string() || j["transformation_type"].get<std::string>().empty()) {
57 error_out = "'transformation_type' must be a non-empty string";
58 return false;
59 }
60
61 // parameters (optional, for backward compatibility; must be an object when present)
62 if (j.contains("parameters") && !j["parameters"].is_object()) {
63 error_out = "'parameters' must be an object";
64 return false;
65 }
66
67 // subgraph
68 if (!j.contains("subgraph")) {
69 error_out = "missing 'subgraph'";
70 return false;
71 }
72 const auto& subgraph = j["subgraph"];
73 if (!subgraph.is_object()) {
74 error_out = "'subgraph' must be an object";
75 return false;
76 }
77 for (std::size_t i = 0; i < subgraph.size(); ++i) {
78 const auto key = std::to_string(i);
79 if (!subgraph.contains(key)) {
80 error_out = "'subgraph' keys must be contiguous indices; missing '" + key + "'";
81 return false;
82 }
83 const auto& node = subgraph[key];
84 if (!node.is_object()) {
85 error_out = "'subgraph[" + key + "]' must be an object";
86 return false;
87 }
88 if (!node.contains("element_id")) {
89 error_out = "'subgraph[" + key + "]' must have an 'element_id'";
90 return false;
91 }
92 const auto& element_id = node["element_id"];
93 const bool non_negative_integer = element_id.is_number_unsigned() ||
94 (element_id.is_number_integer() && element_id.get<long long>() >= 0);
95 if (!non_negative_integer) {
96 error_out = "'subgraph[" + key + "]' must have a non-negative integer 'element_id'";
97 return false;
98 }
99 if (!node.contains("type") || !node["type"].is_string()) {
100 error_out = "'subgraph[" + key + "]' must have a string 'type'";
101 return false;
102 }
103 }
104
105 // Reject unknown top-level keys to keep the schema strict.
106 for (auto it = j.begin(); it != j.end(); ++it) {
107 if (it.key() != "transformation_type" && it.key() != "subgraph" && it.key() != "parameters") {
108 error_out = "unexpected top-level key '" + it.key() + "'";
109 return false;
110 }
111 }
112
113 return true;
114}
115
116} // namespace transformations
117} // namespace sdfg
Definition loop_parallelization.h:8
bool validate_transformation_schema(const nlohmann::json &j, std::string &error_out)
Validate that a transformation description respects the serialization schema.
Definition transformation_schema.h:45
const nlohmann::json & transformation_schema()
The transformation serialization schema, parsed once.
const std::string & transformation_schema_text()
The transformation serialization schema as a JSON Schema (2020-12) document.
Definition analysis.h:7