Line data Source code
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 : // 9 : /// \file mcrl2/utilities/detail/transform_tool.h 10 : /// \brief add your file description here. 11 : 12 : #ifndef MCRL2_UTILITIES_DETAIL_TRANSFORM_TOOL_H 13 : #define MCRL2_UTILITIES_DETAIL_TRANSFORM_TOOL_H 14 : 15 : #include "mcrl2/utilities/detail/command.h" 16 : #include "mcrl2/utilities/tool.h" 17 : 18 : namespace mcrl2 { 19 : 20 : namespace utilities { 21 : 22 : namespace detail { 23 : 24 : template <typename Tool> 25 : class transform_tool: public Tool 26 : { 27 : protected: 28 : typedef Tool super; 29 : 30 : std::string algorithm_and_options; 31 : int algorithm_number = -1; 32 : bool print_algorithms = false; 33 : std::map<std::string, std::shared_ptr<utilities::detail::command>> commands; 34 : 35 0 : void parse_options(const utilities::command_line_parser& parser) 36 : { 37 0 : super::parse_options(parser); 38 0 : algorithm_and_options = parser.option_argument("algorithm"); 39 0 : algorithm_number = parser.option_argument_as<int>("number"); 40 0 : print_algorithms = parser.options.count("print-algorithms") > 0; 41 0 : } 42 : 43 0 : void add_options(utilities::interface_description& desc) 44 : { 45 0 : super::add_options(desc); 46 0 : desc.add_option("algorithm", utilities::make_optional_argument<std::string>("NAME", ""), "the algorithm that is to be applied", 'a'); 47 0 : desc.add_option("number", utilities::make_optional_argument<int>("NAME", "-1"), "the number of the algorithm that is to be applied", 'n'); 48 0 : desc.add_option("print-algorithms", "print the available algorithms", 'p'); 49 0 : } 50 : 51 : inline 52 0 : void add_command(const std::shared_ptr<utilities::detail::command>& command) 53 : { 54 0 : commands[command->name] = command; 55 0 : } 56 : 57 : virtual void add_commands(const std::vector<std::string>& options) = 0; 58 : 59 : public: 60 0 : 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 0 : : Tool(name, author, what_is, tool_description, known_issues) 67 0 : {} 68 : 69 0 : bool run() 70 : { 71 0 : std::vector<std::string> options; 72 0 : std::set<std::string> algorithms; 73 0 : std::string algorithm; 74 : 75 0 : if (algorithm_number < 0) 76 : { 77 0 : options = utilities::regex_split(algorithm_and_options, "\\s+"); 78 0 : if (!options.empty()) 79 : { 80 0 : algorithm = options[0]; 81 0 : options.erase(options.begin()); 82 : } 83 : } 84 : 85 0 : add_commands(options); 86 : 87 0 : for (auto i = commands.begin(); i != commands.end(); ++i) 88 : { 89 0 : algorithms.insert(i->first); 90 : } 91 : 92 0 : if (algorithm_number >= 0 && !algorithm_and_options.empty()) 93 : { 94 0 : throw mcrl2::runtime_error("It is not allowed to set both number and algorithm!"); 95 : } 96 : 97 : // print the algorithms 98 0 : if (print_algorithms || (algorithm_number < 0 && algorithm_and_options.empty())) 99 : { 100 0 : int index = 1; 101 0 : std::cout << "The following algorithms are available:" << std::endl; 102 0 : for (auto const& algorithm: algorithms) 103 : { 104 0 : std::cout << index++ << ") " << algorithm << std::endl; 105 : } 106 0 : return true; 107 : } 108 : 109 : // if a number was specified, lookup the corresponding algorithm 110 0 : if (algorithm_number >= 0) 111 : { 112 0 : int index = 1; 113 0 : for (auto const& algo: algorithms) 114 : { 115 0 : if (index++ == algorithm_number) 116 : { 117 0 : algorithm = algo; 118 : } 119 : } 120 : } 121 : 122 : // run the algorithm 123 0 : auto i = commands.find(algorithm); 124 0 : if (i == commands.end()) 125 : { 126 0 : throw std::runtime_error("Unknown algorithm " + algorithm); 127 : } 128 0 : i->second->execute(); 129 : 130 0 : return true; 131 0 : } 132 : }; 133 : 134 : } // namespace detail 135 : 136 : } // namespace utilities 137 : 138 : } // namespace mcrl2 139 : 140 : #endif // MCRL2_UTILITIES_DETAIL_TRANSFORM_TOOL_H