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/GenericMolInfo.h"
28 : #include "core/ModuleMap.h"
29 : #include <cstdio>
30 : #include <string>
31 : #include <iostream>
32 :
33 : namespace PLMD {
34 : namespace cltools {
35 :
36 : //+PLUMEDOC TOOLS gen_json
37 : /*
38 : 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
39 :
40 : \par Examples
41 :
42 : The following command generates the json file
43 : \verbatim
44 : plumed gen_json
45 : \endverbatim
46 :
47 :
48 : */
49 : //+ENDPLUMEDOC
50 :
51 : class GenJson : public CLTool {
52 : private:
53 : std::string version;
54 : public:
55 : static void registerKeywords( Keywords& keys );
56 : explicit GenJson(const CLToolOptions& co );
57 : int main(FILE* in, FILE*out,Communicator& pc) override;
58 4 : std::string description()const override {
59 4 : return "print out a json file that contains the pluemd syntax";
60 : }
61 : };
62 :
63 16331 : PLUMED_REGISTER_CLTOOL(GenJson,"gen_json")
64 :
65 5442 : void GenJson::registerKeywords( Keywords& keys ) {
66 5442 : CLTool::registerKeywords( keys );
67 10884 : keys.add("compulsory","--actions","a file containing one line descriptions of the various actions");
68 5442 : }
69 :
70 5 : GenJson::GenJson(const CLToolOptions& co ):
71 : CLTool(co),
72 5 : version("master") {
73 5 : inputdata=commandline;
74 10 : if( config::getVersionLong().find("dev")==std::string::npos ) {
75 10 : version="v"+config::getVersion();
76 : }
77 5 : }
78 :
79 1 : int GenJson::main(FILE* in, FILE*out,Communicator& pc) {
80 1 : std::string line(""), actionfile;
81 1 : parse("--actions",actionfile);
82 1 : IFile myfile;
83 1 : myfile.open(actionfile);
84 : bool stat;
85 : std::map<std::string,std::string> action_map;
86 463 : while((stat=myfile.getline(line))) {
87 462 : std::size_t col = line.find_first_of(":");
88 462 : std::string docs = line.substr(col+1);
89 462 : if( docs.find("\\")!=std::string::npos ) {
90 0 : error("found invalid backslash character in first line of documentation for action " + line.substr(0,col) );
91 : }
92 924 : action_map.insert(std::pair<std::string,std::string>( line.substr(0,col), docs ) );
93 : }
94 1 : myfile.close();
95 :
96 : // Cycle over all the action names
97 1 : std::cout<<"{"<<std::endl;
98 : // Get the vimlink
99 2 : std::cout<<" \"vimlink\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/_vim_syntax.html\","<<std::endl;
100 : // And the replicas link
101 2 : std::cout<<" \"replicalink\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/special-replica-syntax.html\","<<std::endl;
102 : // Get the names of all the actions
103 1 : std::vector<std::string> action_names( actionRegister().getActionNames() );
104 440 : for(unsigned i=0; i<action_names.size(); ++i) {
105 878 : std::cout<<" \""<<action_names[i]<<'"'<<": {"<<std::endl;
106 439 : std::string action=action_names[i];
107 : // Handle conversion of action names to links
108 878 : std::cout<<" \"hyperlink\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/";
109 439 : std::transform(action.begin(), action.end(), action.begin(), [](unsigned char c) {
110 5271 : return std::tolower(c);
111 : });
112 : while(true) {
113 759 : std::size_t und=action.find_first_of("_");
114 759 : if( und==std::string::npos ) {
115 : break;
116 : }
117 320 : std::string first=action.substr(0,und);
118 2256 : for(auto c : first ) {
119 1936 : if( isdigit(c) ) {
120 0 : std::cout<<c;
121 : } else {
122 3872 : std::cout<<"_"<<c;
123 : }
124 : }
125 320 : std::cout<<"_";
126 640 : action=action.substr(und+1);
127 320 : }
128 3454 : for(auto c : action ) {
129 3015 : if( isdigit(c) ) {
130 12 : std::cout<<c;
131 : } else {
132 6006 : std::cout<<"_"<<c;
133 : }
134 : }
135 439 : std::cout<<".html\","<<std::endl;
136 878 : std::cout<<" \"description\" : \""<<action_map[action_names[i]]<<"\",\n";
137 878 : std::cout<<" \"module\" : \""<<getModuleMap().find(action_names[i])->second<<"\",\n";
138 : // Now output keyword information
139 439 : Keywords keys;
140 439 : actionRegister().getKeywords( action_names[i], keys );
141 878 : std::cout<<" \"displayname\" : \""<<keys.getDisplayName()<<"\",\n";
142 439 : std::cout<<" \"syntax\" : {"<<std::endl;
143 5502 : for(unsigned j=0; j<keys.size(); ++j) {
144 5063 : std::string desc = keys.getKeywordDescription( keys.getKeyword(j) );
145 5063 : if( desc.find("default=")!=std::string::npos ) {
146 1641 : std::size_t brac=desc.find_first_of(")");
147 3282 : desc = desc.substr(brac+1);
148 : }
149 5063 : std::size_t dot=desc.find_first_of(".");
150 5063 : std::string mydescrip = desc.substr(0,dot);
151 5063 : if( mydescrip.find("\\")!=std::string::npos ) {
152 0 : error("found invalid backslash character documentation for keyword " + keys.getKeyword(j) + " in action " + action_names[i] );
153 : }
154 25315 : std::cout<<" \""<<keys.getKeyword(j)<<"\" : { \"type\": \""<<keys.getStyle(keys.getKeyword(j))<<"\", \"description\": \""<<mydescrip<<"\", \"multiple\": "<<keys.numbered( keys.getKeyword(j) )<<"}";
155 5869 : if( j==keys.size()-1 && !keys.exists("HAS_VALUES") ) {
156 : std::cout<<std::endl;
157 : } else {
158 4991 : std::cout<<","<<std::endl;
159 : }
160 : }
161 878 : if( keys.exists("HAS_VALUES") ) {
162 367 : std::cout<<" \"output\" : {"<<std::endl;
163 367 : std::vector<std::string> components( keys.getOutputComponents() );
164 : // Check if we have a value
165 : bool hasvalue=true;
166 915 : for(unsigned k=0; k<components.size(); ++k) {
167 1734 : if( keys.getOutputComponentFlag( components[k] )=="default" ) {
168 : hasvalue=false;
169 : break;
170 : }
171 : }
172 1640 : for(unsigned k=0; k<components.size(); ++k) {
173 1273 : std::string compname=components[k];
174 1273 : if( components[k]==".#!value" ) {
175 : hasvalue=false;
176 : compname="value";
177 : }
178 2546 : std::cout<<" \""<<compname<<"\" : {"<<std::endl;
179 3819 : std::cout<<" \"flag\": \""<<keys.getOutputComponentFlag( components[k] )<<"\","<<std::endl;
180 1273 : std::string desc=keys.getOutputComponentDescription( components[k] );
181 1273 : std::size_t dot=desc.find_first_of(".");
182 1273 : std::string mydescrip = desc.substr(0,dot);
183 1273 : if( mydescrip.find("\\")!=std::string::npos ) {
184 0 : error("found invalid backslash character documentation for output component " + compname + " in action " + action_names[i] );
185 : }
186 2546 : std::cout<<" \"description\": \""<<mydescrip<<"\""<<std::endl;
187 1273 : if( k==components.size()-1 ) {
188 367 : std::cout<<" }"<<std::endl;
189 : } else {
190 906 : std::cout<<" },"<<std::endl;
191 : }
192 : }
193 367 : if( hasvalue && components.size()==0 ) {
194 : printf("WARNING: no components have been registered for action %s \n", action_names[i].c_str() );
195 : }
196 367 : std::cout<<" }"<<std::endl;
197 :
198 367 : }
199 439 : std::cout<<" },"<<std::endl;
200 439 : if( keys.getNeededKeywords().size()>0 ) {
201 122 : std::vector<std::string> neededActions( keys.getNeededKeywords() );
202 244 : std::cout<<" \"needs\" : ["<<"\""<<neededActions[0]<<"\"";
203 1006 : for(unsigned j=1; j<neededActions.size(); ++j) {
204 1768 : std::cout<<", \""<<neededActions[j]<<"\"";
205 : }
206 122 : std::cout<<"],"<<std::endl;
207 122 : }
208 : // This ensures that \n is replaced by \\n
209 439 : std::string unsafen="\n", safen="\\n", helpstr = keys.getHelpString();
210 439 : for( std::size_t pos = helpstr.find("\n");
211 13776 : pos != std::string::npos;
212 13337 : pos = helpstr.find("\n", pos)
213 : ) {
214 : helpstr.replace(pos, unsafen.size(), safen);
215 13337 : pos += safen.size();
216 : }
217 878 : std::cout<<" \"help\" : \""<<helpstr<<"\"\n";
218 439 : std::cout<<" },"<<std::endl;
219 439 : }
220 : // Get all the special groups
221 1 : std::cout<<" \"groups\" : {"<<std::endl;
222 1 : std::cout<<" \"@allatoms\" : { \n"<<std::endl;
223 1 : std::cout<<" \"description\" : \"refers to all the MD codes atoms and PLUMEDs vatoms\","<<std::endl;
224 2 : std::cout<<" \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/_group.html\""<<std::endl;
225 1 : std::cout<<" },"<<std::endl;
226 1 : std::cout<<" \"@mdatoms\" : { \n"<<std::endl;
227 1 : std::cout<<" \"description\" : \"refers to all the MD codes atoms but not PLUMEDs vatoms\","<<std::endl;
228 2 : std::cout<<" \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/_group.html\""<<std::endl;
229 1 : std::cout<<" },"<<std::endl;
230 1 : std::cout<<" \"@ndx:\" : { \n"<<std::endl;
231 1 : std::cout<<" \"description\" : \"load a group from a GROMACS index file\","<<std::endl;
232 2 : std::cout<<" \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/_group.html\""<<std::endl;
233 : // Now print all the special keywords in molinfo
234 1 : std::map<std::string,std::string> specials( GenericMolInfo::getSpecialKeywords() );
235 36 : for(auto const& s : specials ) {
236 35 : std::cout<<" },"<<std::endl;
237 70 : std::cout<<" \""<<s.first<<"\" : { \n"<<std::endl;
238 70 : std::cout<<" \"description\" : \""<<s.second<<"\","<<std::endl;
239 70 : std::cout<<" \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/_m_o_l_i_n_f_o.html\""<<std::endl;
240 : }
241 1 : std::cout<<" }"<<std::endl;
242 1 : std::cout<<" }"<<std::endl;
243 1 : std::cout<<"}"<<std::endl;
244 1 : return 0;
245 2 : }
246 :
247 : } // End of namespace
248 : }
|