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 "core/CLToolRegister.h"
24 : #include "tools/Tools.h"
25 : #include "config/Config.h"
26 : #include "core/ActionRegister.h"
27 : #include "core/CLToolRegister.h"
28 : #include "core/GenericMolInfo.h"
29 : #include "core/ModuleMap.h"
30 : #include <cstdio>
31 : #include <string>
32 : #include <iostream>
33 :
34 : namespace PLMD {
35 : namespace cltools {
36 :
37 : //+PLUMEDOC TOOLS gen_json
38 : /*
39 : 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
40 :
41 : This command is used during the build process of plumed. It constructs a json file that contains documentation information on all actions and cltools. This json
42 : file is then used by the python package [PlumedToHTML](https://github.com/plumed/PlumedToHTML). This package is used to construct the example input files that
43 : appear in [the plumed nest](https://www.plumed-nest.org), [plumed tutorials](https://www.plumed-tutorials.org) and this manual.
44 :
45 : You will likely not ever need to run this command. When it runs during the build proess we use the following command:
46 :
47 : ```plumed
48 : plumed --no-mpi gen_json --actions action_list > syntax.json
49 : ```
50 :
51 : You can find the syntax json file that is generated by this comamnd as `plumed2/json/syntax.json`.
52 :
53 : */
54 : //+ENDPLUMEDOC
55 :
56 : class GenJson : public CLTool {
57 : private:
58 : std::string version;
59 : void printHyperlink( const std::string& action );
60 : void printKeywordDocs( const std::string& k, const std::string& action, const Keywords& keys );
61 : public:
62 : static void registerKeywords( Keywords& keys );
63 : explicit GenJson(const CLToolOptions& co );
64 : int main(FILE* in, FILE*out,Communicator& pc) override;
65 5 : std::string description()const override {
66 5 : return "print out a json file that contains the pluemd syntax";
67 : }
68 : };
69 :
70 17055 : PLUMED_REGISTER_CLTOOL(GenJson,"gen_json")
71 :
72 5683 : void GenJson::registerKeywords( Keywords& keys ) {
73 5683 : CLTool::registerKeywords( keys );
74 5683 : keys.add("compulsory","--actions","a file containing one line descriptions of the various actions");
75 5683 : }
76 :
77 6 : GenJson::GenJson(const CLToolOptions& co ):
78 : CLTool(co),
79 6 : version("master") {
80 6 : inputdata=inputType::commandline;
81 12 : if( config::getVersionLong().find("dev")==std::string::npos ) {
82 0 : version="v"+config::getVersion();
83 : }
84 6 : }
85 :
86 518 : void GenJson::printHyperlink( const std::string& action ) {
87 1554 : std::cout<<" \"hyperlink\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/"<<action<<"\","<<std::endl;
88 518 : }
89 :
90 5563 : void GenJson::printKeywordDocs( const std::string& k, const std::string& action, const Keywords& keys ) {
91 5563 : std::string defa = "", desc = keys.getKeywordDescription( k );
92 5563 : if( desc.find("default=")!=std::string::npos ) {
93 1834 : std::size_t defstart = desc.find_first_of("="), brac=desc.find_first_of(")");
94 1834 : defa = desc.substr(defstart+1,brac-defstart-2);
95 3668 : desc = desc.substr(brac+1);
96 : }
97 5563 : std::size_t dot=desc.find_first_of(".");
98 5563 : std::string mydescrip = desc.substr(0,dot);
99 5563 : if( mydescrip.find("\\")!=std::string::npos ) {
100 0 : error("found invalid backslash character documentation for keyword " + k + " in action " + action );
101 : }
102 22252 : std::cout<<" \""<<k<<"\" : { \"type\": \""<<keys.getStyle(k)<<"\", \"description\": \""<<mydescrip<<"\", \"multiple\": "<<keys.numbered(k);
103 5563 : std::string actlink=keys.getLinkedActions(k);
104 5563 : if( actlink.length()>0 ) {
105 11126 : std::cout<<", \"actionlink\": \""<<actlink<<"\"";
106 : }
107 5563 : std::string doclink=keys.getLinkedPages(k);
108 5563 : if( doclink.length()>0 ) {
109 356 : std::cout<<", \"pagelink\": \""<<doclink<<"\"";
110 : }
111 5563 : std::string argtype = keys.getArgumentType( k );
112 5563 : if( argtype.length()>0 ) {
113 604 : std::cout<<", \"argtype\": \""<<argtype<<"\"}";
114 5261 : } else if( defa.length()>0 ) {
115 3656 : std::cout<<", \"default\": \""<<defa<<"\"}";
116 : } else {
117 3433 : std::cout<<"}";
118 : }
119 5563 : }
120 :
121 1 : int GenJson::main(FILE* in, FILE*out,Communicator& pc) {
122 1 : std::string line(""), actionfile;
123 1 : parse("--actions",actionfile);
124 1 : IFile myfile;
125 1 : myfile.open(actionfile);
126 : bool stat;
127 : std::map<std::string,std::string> action_map;
128 391 : while((stat=myfile.getline(line))) {
129 390 : std::size_t col = line.find_first_of(":");
130 390 : std::string docs = line.substr(col+1);
131 390 : if( docs.find("\\")!=std::string::npos ) {
132 0 : error("found invalid backslash character in first line of documentation for action " + line.substr(0,col) );
133 : }
134 780 : action_map.insert(std::pair<std::string,std::string>( line.substr(0,col), docs ) );
135 : }
136 1 : myfile.close();
137 :
138 : // Cycle over all the action names
139 1 : std::cout<<"{"<<std::endl;
140 : // Get the vimlink
141 2 : std::cout<<" \"vimlink\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/vim\","<<std::endl;
142 : // And the replicas link
143 2 : std::cout<<" \"replicalink\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/parsing.html\","<<std::endl;
144 : // Get the names of all the actions
145 1 : std::vector<std::string> action_names( actionRegister().getActionNames() );
146 : std::vector<std::string> allmodules;
147 455 : for(const auto & action : action_names) {
148 908 : std::cout<<" \""<<action<<'"'<<": {"<<std::endl;
149 : // Handle conversion of action names to links
150 454 : printHyperlink( action );
151 908 : std::cout<<" \"description\" : \""<<action_map[action]<<"\",\n";
152 : bool found=false;
153 908 : plumed_massert( getModuleMap().find(action)!=getModuleMap().end(), "could not find action named " + action + " in module map");
154 908 : std::string thismodule = getModuleMap().find(action)->second;
155 454 : found = std::find(allmodules.begin(),allmodules.end(),thismodule)!=allmodules.end();
156 454 : if( !found ) {
157 45 : allmodules.push_back( thismodule );
158 : }
159 908 : std::cout<<" \"module\" : \""<<thismodule<<"\",\n";
160 : // Now output keyword information
161 454 : Keywords keys;
162 454 : actionRegister().getKeywords( action, keys );
163 908 : std::cout<<" \"displayname\" : \""<<keys.getDisplayName()<<"\",\n";
164 : // This is used for noting actions that have been deprecated
165 454 : std::string replacement = keys.getReplacementAction();
166 454 : if( replacement!="none" ) {
167 : bool found_replacement=false;
168 6616 : for(unsigned j=0; j<action_names.size(); ++j) {
169 6616 : if( action_names[j]==replacement ) {
170 : found_replacement=true;
171 : break;
172 : }
173 : }
174 31 : if( !found_replacement ) {
175 0 : error("could not find action named " + replacement + " that is supposed to be used to replace " + action );
176 : }
177 62 : std::cout<<" \"replacement\" : \""<<replacement<<"\",\n";
178 : }
179 454 : std::cout<<" \"dois\" : [";
180 454 : unsigned ndoi = keys.getDOIList().size();
181 454 : if( ndoi>0 ) {
182 282 : std::cout<<"\"" + keys.getDOIList()[0] + "\"";
183 267 : for(unsigned j=1; j<ndoi; ++j) {
184 252 : std::cout<<", \"" + keys.getDOIList()[j] + "\"";
185 : }
186 : }
187 454 : std::cout<<"],\n";
188 454 : std::cout<<" \"syntax\" : {"<<std::endl;
189 5763 : for(unsigned j=0; j<keys.size(); ++j) {
190 5309 : printKeywordDocs( keys.getKeyword(j), action, keys );
191 5309 : if( j==keys.size()-1 && !keys.exists("HAS_VALUES") ) {
192 : std::cout<<std::endl;
193 : } else {
194 5237 : std::cout<<","<<std::endl;
195 : }
196 : }
197 454 : if( keys.exists("HAS_VALUES") ) {
198 382 : std::cout<<" \"output\" : {"<<std::endl;
199 382 : std::vector<std::string> components( keys.getOutputComponents() );
200 : // Check if we have a value
201 : bool hasvalue=true;
202 826 : for(unsigned k=0; k<components.size(); ++k) {
203 1628 : if( keys.getOutputComponentFlag( components[k] )=="default" ) {
204 : hasvalue=false;
205 : break;
206 : }
207 : }
208 1712 : for(unsigned k=0; k<components.size(); ++k) {
209 1330 : std::string compname=components[k];
210 1330 : if( components[k]==".#!value" ) {
211 : hasvalue=false;
212 : compname="value";
213 : }
214 2660 : std::cout<<" \""<<compname<<"\" : {"<<std::endl;
215 2660 : std::cout<<" \"flag\": \""<<keys.getOutputComponentFlag( components[k] )<<"\","<<std::endl;
216 3990 : std::cout<<" \"type\": \""<<keys.getOutputComponentType( components[k] )<<"\","<<std::endl;
217 1330 : std::string desc=keys.getOutputComponentDescription( components[k] );
218 1330 : std::size_t dot=desc.find_first_of(".");
219 1330 : std::string mydescrip = desc.substr(0,dot);
220 1330 : if( mydescrip.find("\\")!=std::string::npos ) {
221 0 : error("found invalid backslash character documentation for output component " + compname + " in action " + action );
222 : }
223 2660 : std::cout<<" \"description\": \""<<mydescrip<<"\""<<std::endl;
224 1330 : if( k==components.size()-1 ) {
225 382 : std::cout<<" }"<<std::endl;
226 : } else {
227 948 : std::cout<<" },"<<std::endl;
228 : }
229 : }
230 382 : if( hasvalue && components.size()==0 ) {
231 : printf("WARNING: no components have been registered for action %s \n", action.c_str() );
232 : }
233 382 : std::cout<<" }"<<std::endl;
234 :
235 382 : }
236 454 : std::cout<<" },"<<std::endl;
237 454 : if( keys.getNeededKeywords().size()>0 ) {
238 128 : std::vector<std::string> neededActions( keys.getNeededKeywords() );
239 256 : std::cout<<" \"needs\" : ["<<"\""<<neededActions[0]<<"\"";
240 1042 : for(unsigned j=1; j<neededActions.size(); ++j) {
241 1828 : std::cout<<", \""<<neededActions[j]<<"\"";
242 : }
243 128 : std::cout<<"],"<<std::endl;
244 128 : }
245 : // This ensures that \n is replaced by \\n
246 454 : std::string unsafen="\n", safen="\\n", helpstr = keys.getHelpString();
247 454 : for( std::size_t pos = helpstr.find("\n");
248 10059 : pos != std::string::npos;
249 9605 : pos = helpstr.find("\n", pos)
250 : ) {
251 : helpstr.replace(pos, unsafen.size(), safen);
252 9605 : pos += safen.size();
253 : }
254 908 : std::cout<<" \"help\" : \""<<helpstr<<"\"\n";
255 454 : std::cout<<" },"<<std::endl;
256 454 : }
257 : // Get all the special groups
258 1 : std::cout<<" \"groups\" : {"<<std::endl;
259 1 : std::cout<<" \"@allatoms\" : { \n"<<std::endl;
260 1 : std::cout<<" \"description\" : \"refers to all the MD codes atoms and PLUMEDs vatoms\","<<std::endl;
261 2 : std::cout<<" \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/specifying_atoms\""<<std::endl;
262 1 : std::cout<<" },"<<std::endl;
263 1 : std::cout<<" \"@mdatoms\" : { \n"<<std::endl;
264 1 : std::cout<<" \"description\" : \"refers to all the MD codes atoms but not PLUMEDs vatoms\","<<std::endl;
265 2 : std::cout<<" \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/specifying_atoms\""<<std::endl;
266 1 : std::cout<<" },"<<std::endl;
267 1 : std::cout<<" \"@ndx:\" : { \n"<<std::endl;
268 1 : std::cout<<" \"description\" : \"load a group from a GROMACS index file\","<<std::endl;
269 2 : std::cout<<" \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/specifying_atoms.html\""<<std::endl;
270 : // Now print all the special keywords in molinfo
271 1 : std::map<std::string,std::string> specials( GenericMolInfo::getSpecialKeywords() );
272 36 : for(auto const& s : specials ) {
273 35 : std::cout<<" },"<<std::endl;
274 70 : std::cout<<" \""<<s.first<<"\" : { \n"<<std::endl;
275 70 : std::cout<<" \"description\" : \""<<s.second<<"\","<<std::endl;
276 70 : std::cout<<" \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/MOLINFO\""<<std::endl;
277 : }
278 1 : std::cout<<" }"<<std::endl;
279 1 : std::cout<<" },"<<std::endl;
280 1 : std::cout<<" \"modules\" : {"<<std::endl;
281 2 : std::cout<<" \""<<allmodules[0]<<"\" : { "<<std::endl;
282 1 : printHyperlink( "module_" + allmodules[0] );
283 1 : std::cout<<" \"description\" : \"A module that will be used for something\""<<std::endl;
284 45 : for(unsigned i=1; i<allmodules.size(); ++i) {
285 44 : std::cout<<" },"<<std::endl;
286 88 : std::cout<<" \""<<allmodules[i]<<"\" : { "<<std::endl;
287 44 : printHyperlink( "module_" + allmodules[i] );
288 44 : std::cout<<" \"description\" : \"A module that will be used for something\""<<std::endl;
289 : }
290 1 : std::cout<<" }"<<std::endl;
291 1 : std::cout<<" },"<<std::endl;
292 1 : std::cout<<" \"cltools\" : {"<<std::endl;
293 1 : std::vector<std::string> cltool_names( cltoolRegister().list() );
294 20 : for(unsigned i=0; i<cltool_names.size(); ++i) {
295 38 : std::cout<<" \""<<cltool_names[i]<<'"'<<": {"<<std::endl;
296 19 : std::string cltool=cltool_names[i];
297 : // Handle converstion to link
298 19 : printHyperlink( cltool );
299 38 : plumed_massert( getModuleMap().find(cltool)!=getModuleMap().end(), "could not find cltool named " + cltool + " in module map" );
300 38 : std::string thismodule = getModuleMap().find(cltool_names[i])->second;
301 38 : std::cout<<" \"module\" : \""<<thismodule<<"\",\n";
302 19 : auto mytool = cltoolRegister().create( CLToolOptions(cltool) );
303 57 : std::cout<<" \"description\" : \""<<mytool->description()<<"\",\n";
304 19 : if( mytool->inputdata==inputType::commandline ) {
305 16 : std::cout<<" \"inputtype\" : \"command line args\",\n";
306 3 : } else if( mytool->inputdata==inputType::ifile ) {
307 3 : std::cout<<" \"inputtype\" : \"file\",\n";
308 : } else {
309 0 : error("input type for cltool was not specified");
310 : }
311 19 : std::cout<<" \"syntax\" : {"<<std::endl;
312 273 : for(unsigned j=0; j<cltoolRegister().get(cltool).keys.size(); ++j) {
313 254 : std::string k = cltoolRegister().get(cltool).keys.getKeyword(j);
314 254 : printKeywordDocs( k, cltool, cltoolRegister().get(cltool).keys );
315 254 : if( j==cltoolRegister().get(cltool).keys.size()-1 ) {
316 : std::cout<<std::endl;
317 : } else {
318 235 : std::cout<<","<<std::endl;
319 : }
320 : }
321 19 : std::cout<<" }"<<std::endl;
322 19 : std::cout<<" }";
323 19 : if( i==cltool_names.size()-1 ) {
324 : std::cout<<std::endl;
325 : } else {
326 18 : std::cout<<","<<std::endl;
327 : }
328 19 : }
329 1 : std::cout<<" }"<<std::endl;
330 1 : std::cout<<"}"<<std::endl;
331 1 : return 0;
332 3 : }
333 :
334 : } // End of namespace
335 : }
|