mCRL2
Loading...
Searching...
No Matches
transform_tool.h
Go to the documentation of this file.
1// Author(s): Wieger Wesselink
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
11
12#ifndef MCRL2_UTILITIES_DETAIL_TRANSFORM_TOOL_H
13#define MCRL2_UTILITIES_DETAIL_TRANSFORM_TOOL_H
14
17
18namespace mcrl2 {
19
20namespace utilities {
21
22namespace detail {
23
24template <typename Tool>
25class transform_tool: public Tool
26{
27 protected:
28 typedef Tool super;
29
32 bool print_algorithms = false;
33 std::map<std::string, std::shared_ptr<utilities::detail::command>> commands;
34
35 void parse_options(const utilities::command_line_parser& parser)
36 {
37 super::parse_options(parser);
38 algorithm_and_options = parser.option_argument("algorithm");
39 algorithm_number = parser.option_argument_as<int>("number");
40 print_algorithms = parser.options.count("print-algorithms") > 0;
41 }
42
43 void add_options(utilities::interface_description& desc)
44 {
45 super::add_options(desc);
46 desc.add_option("algorithm", utilities::make_optional_argument<std::string>("NAME", ""), "the algorithm that is to be applied", 'a');
47 desc.add_option("number", utilities::make_optional_argument<int>("NAME", "-1"), "the number of the algorithm that is to be applied", 'n');
48 desc.add_option("print-algorithms", "print the available algorithms", 'p');
49 }
50
51 inline
52 void add_command(const std::shared_ptr<utilities::detail::command>& command)
53 {
55 }
56
57 virtual void add_commands(const std::vector<std::string>& options) = 0;
58
59 public:
60 transform_tool(const std::string& name,
61 const std::string& author,
62 const std::string& what_is,
63 const std::string& tool_description,
64 std::string known_issues = ""
65 )
66 : Tool(name, author, what_is, tool_description, known_issues)
67 {}
68
69 bool run()
70 {
71 std::vector<std::string> options;
72 std::set<std::string> algorithms;
73 std::string algorithm;
74
75 if (algorithm_number < 0)
76 {
78 if (!options.empty())
79 {
80 algorithm = options[0];
81 options.erase(options.begin());
82 }
83 }
84
85 add_commands(options);
86
87 for (auto i = commands.begin(); i != commands.end(); ++i)
88 {
89 algorithms.insert(i->first);
90 }
91
92 if (algorithm_number >= 0 && !algorithm_and_options.empty())
93 {
94 throw mcrl2::runtime_error("It is not allowed to set both number and algorithm!");
95 }
96
97 // print the algorithms
99 {
100 int index = 1;
101 std::cout << "The following algorithms are available:" << std::endl;
102 for (auto const& algorithm: algorithms)
103 {
104 std::cout << index++ << ") " << algorithm << std::endl;
105 }
106 return true;
107 }
108
109 // if a number was specified, lookup the corresponding algorithm
110 if (algorithm_number >= 0)
111 {
112 int index = 1;
113 for (auto const& algo: algorithms)
114 {
115 if (index++ == algorithm_number)
116 {
117 algorithm = algo;
118 }
119 }
120 }
121
122 // run the algorithm
123 auto i = commands.find(algorithm);
124 if (i == commands.end())
125 {
126 throw std::runtime_error("Unknown algorithm " + algorithm);
127 }
128 i->second->execute();
129
130 return true;
131 }
132};
133
134} // namespace detail
135
136} // namespace utilities
137
138} // namespace mcrl2
139
140#endif // MCRL2_UTILITIES_DETAIL_TRANSFORM_TOOL_H
Standard exception class for reporting runtime errors.
Definition exception.h:27
void parse_options(const utilities::command_line_parser &parser)
virtual void add_commands(const std::vector< std::string > &options)=0
std::map< std::string, std::shared_ptr< utilities::detail::command > > commands
void add_command(const std::shared_ptr< utilities::detail::command > &command)
void add_options(utilities::interface_description &desc)
transform_tool(const std::string &name, const std::string &author, const std::string &what_is, const std::string &tool_description, std::string known_issues="")
add your file description here.
std::vector< std::string > regex_split(const std::string &text, const std::string &sep)
Split a string using a regular expression separator.
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
Definition indexed_set.h:72
add your file description here.