Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2022,2023 The plumed team
3 : (see the PEOPLE file at the root of the distribution for a list of names)
4 :
5 : See http://www.plumed.org for more information.
6 :
7 : This file is part of plumed, version 2.
8 :
9 : plumed is free software: you can redistribute it and/or modify
10 : it under the terms of the GNU Lesser General Public License as published by
11 : the Free Software Foundation, either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : plumed is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU Lesser General Public License for more details.
18 :
19 : You should have received a copy of the GNU Lesser General Public License
20 : along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 : #include "CLTool.h"
23 : #include "CLToolRegister.h"
24 : #include "tools/Tools.h"
25 : #include "config/Config.h"
26 : #include "core/ActionRegister.h"
27 : #include "core/GenericMolInfo.h"
28 : #include <cstdio>
29 : #include <string>
30 : #include <iostream>
31 :
32 : namespace PLMD {
33 : namespace cltools {
34 :
35 : //+PLUMEDOC TOOLS gen_json
36 : /*
37 : gen_json constructs a json file that includes a dictionary of actions, the keywords for those actions and the components and outputs this to standard output
38 :
39 : \par Examples
40 :
41 : The following command generates the json file
42 : \verbatim
43 : plumed gen_json
44 : \endverbatim
45 :
46 :
47 : */
48 : //+ENDPLUMEDOC
49 :
50 : class GenJson : public CLTool {
51 : private:
52 : std::string version;
53 : public:
54 : static void registerKeywords( Keywords& keys );
55 : explicit GenJson(const CLToolOptions& co );
56 : int main(FILE* in, FILE*out,Communicator& pc) override;
57 4 : std::string description()const override {
58 4 : return "print out a json file that contains the pluemd syntax";
59 : }
60 : };
61 :
62 13791 : PLUMED_REGISTER_CLTOOL(GenJson,"gen_json")
63 :
64 4595 : void GenJson::registerKeywords( Keywords& keys ) {
65 4595 : CLTool::registerKeywords( keys );
66 9190 : keys.add("compulsory","--actions","a file containing one line descriptions of the various actions");
67 4595 : }
68 :
69 6 : GenJson::GenJson(const CLToolOptions& co ):
70 : CLTool(co),
71 6 : version("master") {
72 6 : inputdata=commandline;
73 12 : if( config::getVersionLong().find("dev")==std::string::npos ) {
74 12 : version="v"+config::getVersion();
75 : }
76 6 : }
77 :
78 2 : int GenJson::main(FILE* in, FILE*out,Communicator& pc) {
79 2 : std::string line(""), actionfile;
80 2 : parse("--actions",actionfile);
81 2 : IFile myfile;
82 2 : myfile.open(actionfile);
83 : bool stat;
84 : std::map<std::string,std::string> action_map;
85 626 : while((stat=myfile.getline(line))) {
86 624 : std::size_t col = line.find_first_of(":");
87 624 : std::string docs = line.substr(col+1);
88 624 : if( docs.find("\\")!=std::string::npos ) {
89 0 : error("found invalid backslash character in first line of documentation for action " + line.substr(0,col) );
90 : }
91 1248 : action_map.insert(std::pair<std::string,std::string>( line.substr(0,col), docs ) );
92 : }
93 2 : myfile.close();
94 :
95 : // Cycle over all the action names
96 2 : std::cout<<"{"<<std::endl;
97 : // Get the vimlink
98 4 : std::cout<<" \"vimlink\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/_vim_syntax.html\","<<std::endl;
99 : // And the replicas link
100 4 : std::cout<<" \"replicalink\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/special-replica-syntax.html\","<<std::endl;
101 : // Get the names of all the actions
102 2 : std::vector<std::string> action_names( actionRegister().getActionNames() );
103 586 : for(unsigned i=0; i<action_names.size(); ++i) {
104 1168 : std::cout<<" \""<<action_names[i]<<'"'<<": {"<<std::endl;
105 584 : std::string action=action_names[i];
106 : // Handle conversion of action names to links
107 1168 : std::cout<<" \"hyperlink\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/";
108 584 : std::transform(action.begin(), action.end(), action.begin(), [](unsigned char c) {
109 6668 : return std::tolower(c);
110 : });
111 : while(true) {
112 958 : std::size_t und=action.find_first_of("_");
113 958 : if( und==std::string::npos ) {
114 : break;
115 : }
116 374 : std::string first=action.substr(0,und);
117 2424 : for(auto c : first ) {
118 2050 : if( isdigit(c) ) {
119 0 : std::cout<<c;
120 : } else {
121 4100 : std::cout<<"_"<<c;
122 : }
123 : }
124 374 : std::cout<<"_";
125 748 : action=action.substr(und+1);
126 374 : }
127 4828 : for(auto c : action ) {
128 4244 : if( isdigit(c) ) {
129 16 : std::cout<<c;
130 : } else {
131 8456 : std::cout<<"_"<<c;
132 : }
133 : }
134 584 : std::cout<<".html\","<<std::endl;
135 1168 : std::cout<<" \"description\" : \""<<action_map[action_names[i]]<<"\",\n";
136 : // Now output keyword information
137 584 : Keywords keys;
138 584 : actionRegister().getKeywords( action_names[i], keys );
139 584 : std::cout<<" \"syntax\" : {"<<std::endl;
140 9574 : for(unsigned j=0; j<keys.size(); ++j) {
141 8990 : std::string desc = keys.getKeywordDescription( keys.getKeyword(j) );
142 8990 : if( desc.find("default=")!=std::string::npos ) {
143 2984 : std::size_t brac=desc.find_first_of(")");
144 5968 : desc = desc.substr(brac+1);
145 : }
146 8990 : std::size_t dot=desc.find_first_of(".");
147 44950 : std::cout<<" \""<<keys.getKeyword(j)<<"\" : { \"type\": \""<<keys.getStyle(keys.getKeyword(j))<<"\", \"description\": \""<<desc.substr(0,dot)<<"\", \"multiple\": "<<keys.numbered( keys.getKeyword(j) )<<"}";
148 10018 : if( j==keys.size()-1 && !keys.exists("HAS_VALUES") ) {
149 : std::cout<<std::endl;
150 : } else {
151 8850 : std::cout<<","<<std::endl;
152 : }
153 : }
154 1168 : if( keys.exists("HAS_VALUES") ) {
155 444 : std::cout<<" \"output\" : {"<<std::endl;
156 444 : std::vector<std::string> components( keys.getOutputComponents() );
157 : // Check if we have a value
158 : bool hasvalue=true;
159 4558 : for(unsigned k=0; k<components.size(); ++k) {
160 8500 : if( keys.getOutputComponentFlag( components[k] )=="default" ) {
161 : hasvalue=false;
162 : break;
163 : }
164 : }
165 444 : if( hasvalue ) {
166 308 : std::cout<<" \"value\": {"<<std::endl;
167 308 : std::cout<<" \"flag\": \"value\","<<std::endl;
168 308 : std::cout<<" \"description\": \"a scalar quantity\""<<std::endl;
169 308 : if( components.size()==0 ) {
170 94 : std::cout<<" }"<<std::endl;
171 : } else {
172 214 : std::cout<<" },"<<std::endl;
173 : }
174 : }
175 5240 : for(unsigned k=0; k<components.size(); ++k) {
176 9592 : std::cout<<" \""<<components[k]<<"\" : {"<<std::endl;
177 14388 : std::cout<<" \"flag\": \""<<keys.getOutputComponentFlag( components[k] )<<"\","<<std::endl;
178 4796 : std::string desc=keys.getOutputComponentDescription( components[k] );
179 4796 : std::size_t dot=desc.find_first_of(".");
180 14388 : std::cout<<" \"description\": \""<<desc.substr(0,dot)<<"\""<<std::endl;
181 4796 : if( k==components.size()-1 ) {
182 350 : std::cout<<" }"<<std::endl;
183 : } else {
184 4446 : std::cout<<" },"<<std::endl;
185 : }
186 : }
187 444 : std::cout<<" }"<<std::endl;
188 :
189 444 : }
190 584 : std::cout<<" },"<<std::endl;
191 : // This ensures that \n is replaced by \\n
192 584 : std::string unsafen="\n", safen="\\n", helpstr = keys.getHelpString();
193 584 : for( std::size_t pos = helpstr.find("\n");
194 22632 : pos != std::string::npos;
195 22048 : pos = helpstr.find("\n", pos)
196 : ) {
197 : helpstr.replace(pos, unsafen.size(), safen);
198 22048 : pos += safen.size();
199 : }
200 1168 : std::cout<<" \"help\" : \""<<helpstr<<"\"\n";
201 584 : std::cout<<" },"<<std::endl;
202 584 : }
203 : // Get all the special groups
204 2 : std::cout<<" \"groups\" : {"<<std::endl;
205 2 : std::cout<<" \"@allatoms\" : { \n"<<std::endl;
206 2 : std::cout<<" \"description\" : \"refers to all the MD codes atoms and PLUMEDs vatoms\","<<std::endl;
207 4 : std::cout<<" \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/_group.html\""<<std::endl;
208 2 : std::cout<<" },"<<std::endl;
209 2 : std::cout<<" \"@mdatoms\" : { \n"<<std::endl;
210 2 : std::cout<<" \"description\" : \"refers to all the MD codes atoms but not PLUMEDs vatoms\","<<std::endl;
211 4 : std::cout<<" \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/_group.html\""<<std::endl;
212 : // Now print all the special keywords in molinfo
213 2 : std::map<std::string,std::string> specials( GenericMolInfo::getSpecialKeywords() );
214 72 : for(auto const& s : specials ) {
215 70 : std::cout<<" },"<<std::endl;
216 140 : std::cout<<" \""<<s.first<<"\" : { \n"<<std::endl;
217 140 : std::cout<<" \"description\" : \""<<s.second<<"\","<<std::endl;
218 140 : std::cout<<" \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/_m_o_l_i_n_f_o.html\""<<std::endl;
219 : }
220 2 : std::cout<<" }"<<std::endl;
221 2 : std::cout<<" }"<<std::endl;
222 2 : std::cout<<"}"<<std::endl;
223 2 : return 0;
224 4 : }
225 :
226 : } // End of namespace
227 : }
|