-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add ntta builder that works on strings
- Loading branch information
Showing
9 changed files
with
195 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#include "ntta_builder_2.h" | ||
#include "ntta_builder.h" | ||
|
||
namespace aaltitoad { | ||
|
||
auto tta_builder2::set_instance_name(const std::string& name) -> tta_builder2& { | ||
instance_name = name; | ||
return *this; | ||
} | ||
|
||
auto tta_builder2::set_symbol_declarations(const std::string& decls) -> tta_builder2& { | ||
symbol_declarations = decls; | ||
return *this; | ||
} | ||
|
||
auto tta_builder2::add_sub_tta(const std::string& tta_name, const std::string& arguments) -> tta_builder2& { | ||
sub_tta_instances.push_back({tta_name, arguments}); | ||
return *this; | ||
} | ||
|
||
auto tta_builder2::add_location(const std::string &name) -> tta_builder2 & { | ||
locations.insert(name); | ||
return *this; | ||
} | ||
|
||
auto tta_builder2::set_start_location(const std::string &name) -> tta_builder2 & { | ||
initial_location = name; | ||
return *this; | ||
} | ||
|
||
auto tta_builder2::set_main() -> tta_builder2 & { | ||
is_main = true; | ||
return *this; | ||
} | ||
|
||
auto tta_builder2::add_edge(const edge_builder2 &edge) -> tta_builder2 & { | ||
edges.push_back(edge); | ||
return *this; | ||
} | ||
|
||
auto tta_builder2::build(expr::symbol_table_t& symbols, expr::symbol_table_t& external_symbols) -> tta_builder { | ||
tta_builder b{symbols, external_symbols}; | ||
for(auto& loc : locations) | ||
b.add_location(loc); | ||
b.set_starting_location(initial_location); | ||
for(auto& edge : edges) | ||
b.add_edge({edge.source, edge.target, edge.guard, edge.update}); | ||
return b; | ||
} | ||
|
||
auto ntta_builder2::add_declarations(const std::string& decls) -> ntta_builder2& { | ||
symbol_declarations.push_back(decls); | ||
return *this; | ||
} | ||
|
||
auto ntta_builder2::add_external_declarations(const std::string& decls) -> ntta_builder2& { | ||
external_symbol_declarations.push_back(decls); | ||
return *this; | ||
} | ||
|
||
auto ntta_builder2::add_tta(const tta_builder2& builder) -> ntta_builder2& { | ||
tta_builders.insert({builder.instance_name, builder}); | ||
return *this; | ||
} | ||
|
||
auto ntta_builder2::build() -> ntta_builder { | ||
expr::symbol_table_t s{}, e{}; | ||
expr::interpreter i{s}; | ||
for(auto& tta : tta_builders) | ||
s += i.interpret_declarations(tta.second.symbol_declarations); | ||
for(auto& d : symbol_declarations) | ||
s += i.interpret_declarations(d); | ||
for(auto& d : external_symbol_declarations) | ||
e += i.interpret_declarations(d); | ||
// TODO: Implement support for declarations that are initialized with other variables. | ||
// One way of doing this could be: | ||
// - remember which decl-strings fail | ||
// - try once more to load previously failed decl-strings (throw if fails on this second try) | ||
// Or we could do some dependency-graph detection (read up on static analysis tools) | ||
ntta_builder b{}; | ||
b.add_symbols(s).add_external_symbols(e); | ||
// TODO: Check for dependency loops with tarjans SCC algorithm | ||
auto main_component = std::find_if(tta_builders.begin(), tta_builders.end(), | ||
[](const auto& t){ return t.second.is_main; }); | ||
if(main_component == tta_builders.end()) | ||
throw std::logic_error("no main tta"); | ||
build_recursive(b, main_component->first, main_component, s, e); | ||
return b; | ||
} | ||
|
||
// TODO: Sequential composition | ||
void ntta_builder2::build_recursive(ntta_builder &builder, const std::string& name, const tta_builder2_it &it, expr::symbol_table_t& s, expr::symbol_table_t& e) { | ||
auto bb = it->second.build(s,e); | ||
builder.add_tta(name, bb); | ||
for(auto& subtta : it->second.sub_tta_instances) { | ||
auto itt = tta_builders.find(subtta.filename); | ||
if(itt == tta_builders.end()) | ||
spdlog::error("no such tta: {}", subtta.filename); | ||
build_recursive(builder, subtta.parameterization, itt, s, e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#ifndef AALTITOAD_NTTA_BUILDER_2_H | ||
#define AALTITOAD_NTTA_BUILDER_2_H | ||
#include <map> | ||
#include <vector> | ||
#include <string> | ||
#include <set> | ||
#include <ntta/tta.h> | ||
#include "ntta_builder.h" | ||
|
||
// TODO: This two-layer builder structure should be refactored... | ||
namespace aaltitoad { | ||
// TODO: Rename to something else | ||
struct edge_builder2 { | ||
std::string source, target, guard{}, update{}; | ||
}; | ||
|
||
struct sub_tta { | ||
std::string filename, parameterization; | ||
}; | ||
|
||
// TODO: Rename to something else | ||
struct tta_builder2 { | ||
auto set_instance_name(const std::string& name) -> tta_builder2&; | ||
auto set_symbol_declarations(const std::string& decls) -> tta_builder2&; | ||
auto add_sub_tta(const std::string& tta_name, const std::string& arguments) -> tta_builder2&; | ||
auto add_location(const std::string& name) -> tta_builder2&; | ||
auto set_start_location(const std::string& name) -> tta_builder2&; | ||
auto set_main() -> tta_builder2&; | ||
auto add_edge(const edge_builder2& edge) -> tta_builder2&; | ||
auto build(expr::symbol_table_t& symbols, expr::symbol_table_t& external_symbols) -> tta_builder; | ||
std::string instance_name{}; | ||
std::string symbol_declarations{}; | ||
std::string initial_location{}; | ||
bool is_main = false; | ||
std::set<std::string> locations{}; | ||
std::vector<edge_builder2> edges{}; | ||
std::vector<sub_tta> sub_tta_instances{}; | ||
}; | ||
|
||
// TODO: Rename to something else | ||
struct ntta_builder2 { | ||
using tta_builder2_it = std::map<std::string, tta_builder2>::iterator; | ||
auto add_declarations(const std::string& decls) -> ntta_builder2&; | ||
auto add_external_declarations(const std::string& decls) -> ntta_builder2&; | ||
auto add_tta(const tta_builder2& builder) -> ntta_builder2&; | ||
auto build() -> ntta_builder; | ||
void build_recursive(ntta_builder& builder, const std::string& name, const tta_builder2_it& it, expr::symbol_table_t& s, expr::symbol_table_t& e); | ||
std::map<std::string, tta_builder2> tta_builders; | ||
std::vector<std::string> symbol_declarations; | ||
std::vector<std::string> external_symbol_declarations; | ||
}; | ||
} | ||
|
||
#endif //AALTITOAD_NTTA_BUILDER_2_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
project(parser_plugins VERSION 1.0.0) | ||
add_library(huppaal_parser SHARED huppaal_parser.cpp) | ||
target_link_libraries(huppaal_parser PUBLIC aaltitoad expr) | ||
if(${CODE_COVERAGE}) | ||
target_link_options(huppaal_parser PUBLIC --coverage) | ||
target_compile_options(huppaal_parser PUBLIC --coverage) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
#ifndef AALTITOAD_HUPPAAL_PARSER_H | ||
#define AALTITOAD_HUPPAAL_PARSER_H | ||
#include "plugin_system/plugin_system.h" | ||
#include "ntta/ntta_builder.h" | ||
#include "ntta/builder/ntta_builder.h" | ||
#include "ntta/builder/ntta_builder_2.h" | ||
#include <nlohmann/json.hpp> | ||
|
||
namespace aaltitoad::huppaal { | ||
auto load_declarations(const nlohmann::json& json_file, const expr::symbol_table_t& symbols) -> expr::symbol_table_t; | ||
auto load_part(const nlohmann::json& json_file) -> expr::symbol_table_t; | ||
void load_declarations(ntta_builder2& b, const nlohmann::json& json_file); | ||
auto load_part(const nlohmann::json& json_file) -> std::string; | ||
auto load(const std::vector<std::string>& filepaths, const std::vector<std::string> &ignore_list) -> aaltitoad::ntta_t*; | ||
auto load_tta(const nlohmann::json& json_file, expr::symbol_table_t& symbols) -> aaltitoad::tta_builder; | ||
auto load_tta(const nlohmann::json& json_file) -> aaltitoad::tta_builder2; | ||
} | ||
|
||
#endif //AALTITOAD_HUPPAAL_PARSER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters