Skip to content

Commit 08b24cd

Browse files
author
Asger Gitz-Johansen
committed
feat: trace as json
1 parent d443c07 commit 08b24cd

File tree

7 files changed

+67
-47
lines changed

7 files changed

+67
-47
lines changed

src/cli/verifier/cli_options.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ std::vector<option_t> get_options() {
4545
{"list-warn", 'W', argument_requirement::NO_ARG, "List all warnings available"},
4646
{"no-warn", 'm', argument_requirement::NO_ARG, "Disable all warnings"},
4747

48-
{"trace-file", 't', argument_requirement::REQUIRE_ARG, "Provide file to output result-traces to. Default is stdout"}, // TODO: We should output JSON formatted traces
48+
{"result-file", 't', argument_requirement::REQUIRE_ARG, "Provide file to output results to. Default is stdout"},
49+
{"result-json", 'j', argument_requirement::NO_ARG, "Output results as json. Useful for scripting"},
4950
};
5051
}
5152

src/cli/verifier/main.cpp

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
#include <aaltitoadpch.h>
1919
#include <ctl_syntax_tree.h>
20+
#include <ios>
2021
#include <ntta/tta.h>
2122
#include <timer>
2223
#include <plugin_system/plugin_system.h>
@@ -104,21 +105,39 @@ int main(int argc, char** argv) {
104105
auto results = frs.is_reachable(*n, queries);
105106
spdlog::info("reachability search took {0}ms", t.milliseconds_elapsed());
106107

107-
// gather and return results
108-
auto trace_file = cli_arguments["trace-file"].as_string_or_default("");
108+
// open the results file (std::cout by default)
109+
spdlog::trace("opening results file stream");
109110
auto* trace_stream = &std::cout;
111+
auto trace_file = cli_arguments["trace-file"].as_string_or_default("");
110112
if(trace_file != "")
111113
trace_stream = new std::ofstream{trace_file, std::ios::app};
112-
for(auto& result : results) {
113-
*trace_stream << result.query << ": " << std::boolalpha << result.solution.has_value() << "\n";
114-
if(result.solution.has_value())
115-
*trace_stream << result.solution.value(); // TODO: This should be json formatted
114+
115+
// write json to results file or non-json if not provided
116+
if(cli_arguments["result-json"]) {
117+
spdlog::trace("gathering results json data");
118+
auto json_results = "[]"_json;
119+
for(auto& result : results) {
120+
nlohmann::json res{};
121+
std::stringstream ss{}; ss << result.query;
122+
res["query"] = ss.str();
123+
if(result.solution.has_value())
124+
res["trace"] = to_json(result.solution.value());
125+
json_results.push_back(res);
126+
}
127+
*trace_stream << json_results << std::endl;
128+
} else {
129+
spdlog::trace("printing resuls data (non-json)");
130+
for(auto& result : results) {
131+
*trace_stream << result.query << ": " << std::boolalpha << result.solution.has_value();
132+
if(result.solution.has_value())
133+
*trace_stream << result.solution.value();
134+
}
116135
}
117136

118137
return 0;
119138
} catch (std::exception& any) {
120139
spdlog::error(any.what());
121-
std::cout.flush(); // Flush the output streams, just to be nice
140+
std::cout.flush();
122141
std::cerr.flush();
123142
return 1;
124143
}

src/ntta/ntta_state_json.h

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/ntta/tta.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,28 @@ namespace aaltitoad {
183183
ss << *this;
184184
return ss.str();
185185
}
186+
187+
auto ntta_t::to_json() const -> nlohmann::json {
188+
nlohmann::json result{};
189+
result["locations"] = "[]"_json;
190+
for(auto& component : components) {
191+
auto j = "{}"_json;
192+
j[component.first] = component.second.current_location->first;
193+
result["locations"].push_back(j);
194+
}
195+
result["symbols"] = "{}"_json;
196+
for(auto& symbol : symbols + external_symbols) {
197+
std::visit(ya::overload(
198+
[&result, &symbol](const int& v){ result["symbols"][symbol.first] = v; },
199+
[&result, &symbol](const float& v){ result["symbols"][symbol.first] = v; },
200+
[&result, &symbol](const bool& v){ result["symbols"][symbol.first] = v; },
201+
[&result, &symbol](const std::string& v){ result["symbols"][symbol.first] = v; },
202+
[&result, &symbol](const expr::clock_t& v){ result["symbols"][symbol.first] = v.time_units; },
203+
[&result, &symbol](auto&& v){ result["symbols"][symbol.first] = "undefined"; }
204+
), static_cast<const expr::underlying_symbol_value_t&>(symbol.second));
205+
}
206+
return result;
207+
}
186208
}
187209

188210
auto operator<<(std::ostream& os, const aaltitoad::ntta_t& state) -> std::ostream& {

src/ntta/tta.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef AALTITOAD_TTA_H
1919
#define AALTITOAD_TTA_H
2020
#include "expr-wrappers/interpreter.h"
21+
#include <nlohmann/json.hpp>
2122
#include <string>
2223
#include <graph>
2324
#include <symbol_table.h>
@@ -117,6 +118,7 @@ namespace aaltitoad {
117118
void apply(const std::vector<expr::symbol_table_t>& external_symbol_change_list);
118119
void apply_internal(const expr::symbol_table_t& symbol_changes);
119120
auto to_string() const -> std::string;
121+
auto to_json() const -> nlohmann::json;
120122
private:
121123
class tick_resolver {
122124
public:

src/verification/forward_reachability.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,16 @@ auto operator<<(std::ostream& o, const aaltitoad::forward_reachability_searcher:
112112
o << s->second.parent.value();
113113
return o << s->second.data;
114114
}
115+
116+
void to_json_helper(const aaltitoad::forward_reachability_searcher::solution_t& s, std::vector<nlohmann::json>& tail) {
117+
if(s->second.parent.has_value())
118+
to_json_helper(s->second.parent.value(), tail);
119+
tail.push_back(s->second.data.to_json());
120+
}
121+
122+
auto to_json(const aaltitoad::forward_reachability_searcher::solution_t& s) -> std::vector<nlohmann::json> {
123+
std::vector<nlohmann::json> states{};
124+
to_json_helper(s, states);
125+
return states;
126+
}
127+

src/verification/forward_reachability.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "ntta/tta.h"
2222
#include "traceable_multimap.h"
2323
#include <ctl_syntax_tree.h>
24+
#include <nlohmann/json.hpp>
2425
#include <vector>
2526
#include <utility>
2627

@@ -51,5 +52,6 @@ namespace aaltitoad {
5152
};
5253
}
5354
auto operator<<(std::ostream& o, const aaltitoad::forward_reachability_searcher::solution_t& s) -> std::ostream&;
55+
auto to_json(const aaltitoad::forward_reachability_searcher::solution_t& s) -> std::vector<nlohmann::json>;
5456

5557
#endif //AALTITOAD_FORWARD_REACHABILITY_H

0 commit comments

Comments
 (0)