LCOV - code coverage report
Current view: top level - cltools - GenJson.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 179 185 96.8 %
Date: 2025-04-08 18:07:56 Functions: 9 9 100.0 %

          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](www.plumed-nest.org), [plumed tutorials](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       16260 : PLUMED_REGISTER_CLTOOL(GenJson,"gen_json")
      71             : 
      72        5418 : void GenJson::registerKeywords( Keywords& keys ) {
      73        5418 :   CLTool::registerKeywords( keys );
      74        5418 :   keys.add("compulsory","--actions","a file containing one line descriptions of the various actions");
      75        5418 : }
      76             : 
      77           6 : GenJson::GenJson(const CLToolOptions& co ):
      78             :   CLTool(co),
      79           6 :   version("master") {
      80           6 :   inputdata=commandline;
      81          12 :   if( config::getVersionLong().find("dev")==std::string::npos ) {
      82           0 :     version="v"+config::getVersion();
      83             :   }
      84           6 : }
      85             : 
      86         504 : void GenJson::printHyperlink( const std::string& action ) {
      87        1512 :   std::cout<<"    \"hyperlink\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/"<<action<<"\","<<std::endl;
      88         504 : }
      89             : 
      90        5307 : void GenJson::printKeywordDocs( const std::string& k, const std::string& action, const Keywords& keys ) {
      91        5307 :   std::string defa = "", desc = keys.getKeywordDescription( k );
      92        5307 :   if( desc.find("default=")!=std::string::npos ) {
      93        1779 :     std::size_t defstart = desc.find_first_of("="), brac=desc.find_first_of(")");
      94        1779 :     defa = desc.substr(defstart+1,brac-defstart-2);
      95        3558 :     desc = desc.substr(brac+1);
      96             :   }
      97        5307 :   std::size_t dot=desc.find_first_of(".");
      98        5307 :   std::string mydescrip = desc.substr(0,dot);
      99        5307 :   if( mydescrip.find("\\")!=std::string::npos ) {
     100           0 :     error("found invalid backslash character documentation for keyword " + k + " in action " + action );
     101             :   }
     102       31842 :   std::cout<<"       \""<<k<<"\" : { \"type\": \""<<keys.getStyle(k)<<"\", \"description\": \""<<mydescrip<<"\", \"multiple\": "<<keys.numbered(k)<<", \"actionlink\": \""<<keys.getLinkedActions(k)<<"\"";
     103        5307 :   std::string argtype = keys.getArgumentType( k );
     104        5307 :   if( argtype.length()>0 ) {
     105         444 :     std::cout<<", \"argtype\": \""<<argtype<<"\"}";
     106        5085 :   } else if( defa.length()>0 ) {
     107        3546 :     std::cout<<", \"default\": \""<<defa<<"\"}";
     108             :   } else {
     109        3312 :     std::cout<<"}";
     110             :   }
     111        5307 : }
     112             : 
     113           1 : int GenJson::main(FILE* in, FILE*out,Communicator& pc) {
     114           1 :   std::string line(""), actionfile;
     115           1 :   parse("--actions",actionfile);
     116           1 :   IFile myfile;
     117           1 :   myfile.open(actionfile);
     118             :   bool stat;
     119             :   std::map<std::string,std::string> action_map;
     120         458 :   while((stat=myfile.getline(line))) {
     121         457 :     std::size_t col = line.find_first_of(":");
     122         457 :     std::string docs = line.substr(col+1);
     123         457 :     if( docs.find("\\")!=std::string::npos ) {
     124           0 :       error("found invalid backslash character in first line of documentation for action " + line.substr(0,col) );
     125             :     }
     126         914 :     action_map.insert(std::pair<std::string,std::string>( line.substr(0,col), docs ) );
     127             :   }
     128           1 :   myfile.close();
     129             : 
     130             :   // Cycle over all the action names
     131           1 :   std::cout<<"{"<<std::endl;
     132             :   // Get the vimlink
     133           2 :   std::cout<<"  \"vimlink\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/vim\","<<std::endl;
     134             :   // And the replicas link
     135           2 :   std::cout<<"  \"replicalink\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/parsing.html\","<<std::endl;
     136             :   // Get the names of all the actions
     137           1 :   std::vector<std::string> action_names( actionRegister().getActionNames() );
     138             :   std::vector<std::string> allmodules;
     139         442 :   for(unsigned i=0; i<action_names.size(); ++i) {
     140         882 :     std::cout<<"  \""<<action_names[i]<<'"'<<": {"<<std::endl;
     141         441 :     std::string action=action_names[i];
     142             :     // Handle conversion of action names to links
     143         441 :     printHyperlink( action );
     144         882 :     std::cout<<"    \"description\" : \""<<action_map[action_names[i]]<<"\",\n";
     145             :     bool found=false;
     146         882 :     plumed_massert( getModuleMap().find(action)!=getModuleMap().end(), "could not find action named " + action + " in module map");
     147         882 :     std::string thismodule = getModuleMap().find(action_names[i])->second;
     148        5816 :     for(unsigned i=0; i<allmodules.size(); ++i) {
     149        5772 :       if( allmodules[i]==thismodule ) {
     150             :         found=true;
     151             :         break;
     152             :       }
     153             :     }
     154         441 :     if( !found ) {
     155          44 :       allmodules.push_back( thismodule );
     156             :     }
     157         882 :     std::cout<<"    \"module\" : \""<<thismodule<<"\",\n";
     158             :     // Now output keyword information
     159         441 :     Keywords keys;
     160         441 :     actionRegister().getKeywords( action_names[i], keys );
     161         882 :     std::cout<<"    \"displayname\" : \""<<keys.getDisplayName()<<"\",\n";
     162             : // This is used for noting actions that have been deprecated
     163         441 :     std::string replacement = keys.getReplacementAction();
     164         441 :     if( replacement!="none" ) {
     165             :       bool found_replacement=false;
     166        1202 :       for(unsigned j=0; j<action_names.size(); ++j) {
     167        1202 :         if( action_names[j]==replacement ) {
     168             :           found_replacement=true;
     169             :           break;
     170             :         }
     171             :       }
     172           5 :       if( !found_replacement ) {
     173           0 :         error("could not find action named " + replacement + " that is supposed to be used to replace " + action_names[i] );
     174             :       }
     175          10 :       std::cout<<"    \"replacement\" : \""<<replacement<<"\",\n";
     176             :     }
     177         441 :     std::cout<<"     \"dois\" : [";
     178         441 :     unsigned ndoi = keys.getDOIList().size();
     179         441 :     if( ndoi>0 ) {
     180         156 :       std::cout<<"\"" + keys.getDOIList()[0] + "\"";
     181         120 :       for(unsigned j=1; j<ndoi; ++j) {
     182          84 :         std::cout<<", \"" + keys.getDOIList()[j] + "\"";
     183             :       }
     184             :     }
     185         441 :     std::cout<<"],\n";
     186         441 :     std::cout<<"    \"syntax\" : {"<<std::endl;
     187        5512 :     for(unsigned j=0; j<keys.size(); ++j) {
     188        5071 :       printKeywordDocs( keys.getKeyword(j), action, keys );
     189        5071 :       if( j==keys.size()-1 && !keys.exists("HAS_VALUES") ) {
     190             :         std::cout<<std::endl;
     191             :       } else {
     192        4999 :         std::cout<<","<<std::endl;
     193             :       }
     194             :     }
     195         441 :     if( keys.exists("HAS_VALUES") ) {
     196         369 :       std::cout<<"       \"output\" : {"<<std::endl;
     197         369 :       std::vector<std::string> components( keys.getOutputComponents() );
     198             :       // Check if we have a value
     199             :       bool hasvalue=true;
     200         815 :       for(unsigned k=0; k<components.size(); ++k) {
     201        1606 :         if( keys.getOutputComponentFlag( components[k] )=="default" ) {
     202             :           hasvalue=false;
     203             :           break;
     204             :         }
     205             :       }
     206        1687 :       for(unsigned k=0; k<components.size(); ++k) {
     207        1318 :         std::string compname=components[k];
     208        1318 :         if( components[k]==".#!value" ) {
     209             :           hasvalue=false;
     210             :           compname="value";
     211             :         }
     212        2636 :         std::cout<<"         \""<<compname<<"\" : {"<<std::endl;
     213        2636 :         std::cout<<"           \"flag\": \""<<keys.getOutputComponentFlag( components[k] )<<"\","<<std::endl;
     214        3954 :         std::cout<<"           \"type\": \""<<keys.getOutputComponentType( components[k] )<<"\","<<std::endl;
     215        1318 :         std::string desc=keys.getOutputComponentDescription( components[k] );
     216        1318 :         std::size_t dot=desc.find_first_of(".");
     217        1318 :         std::string mydescrip = desc.substr(0,dot);
     218        1318 :         if( mydescrip.find("\\")!=std::string::npos ) {
     219           0 :           error("found invalid backslash character documentation for output component " + compname + " in action " + action_names[i] );
     220             :         }
     221        2636 :         std::cout<<"           \"description\": \""<<mydescrip<<"\""<<std::endl;
     222        1318 :         if( k==components.size()-1 ) {
     223         369 :           std::cout<<"         }"<<std::endl;
     224             :         } else {
     225         949 :           std::cout<<"         },"<<std::endl;
     226             :         }
     227             :       }
     228         369 :       if( hasvalue && components.size()==0 ) {
     229             :         printf("WARNING: no components have been registered for action %s \n", action_names[i].c_str() );
     230             :       }
     231         369 :       std::cout<<"       }"<<std::endl;
     232             : 
     233         369 :     }
     234         441 :     std::cout<<"    },"<<std::endl;
     235         441 :     if( keys.getNeededKeywords().size()>0 ) {
     236         123 :       std::vector<std::string> neededActions( keys.getNeededKeywords() );
     237         246 :       std::cout<<"    \"needs\" : ["<<"\""<<neededActions[0]<<"\"";
     238        1008 :       for(unsigned j=1; j<neededActions.size(); ++j) {
     239        1770 :         std::cout<<", \""<<neededActions[j]<<"\"";
     240             :       }
     241         123 :       std::cout<<"],"<<std::endl;
     242         123 :     }
     243             :     // This ensures that \n is replaced by \\n
     244         441 :     std::string unsafen="\n", safen="\\n", helpstr = keys.getHelpString();
     245         441 :     for( std::size_t pos = helpstr.find("\n");
     246       11110 :          pos != std::string::npos;
     247       10669 :          pos = helpstr.find("\n", pos)
     248             :        ) {
     249             :       helpstr.replace(pos, unsafen.size(), safen);
     250       10669 :       pos += safen.size();
     251             :     }
     252         882 :     std::cout<<"    \"help\" : \""<<helpstr<<"\"\n";
     253         441 :     std::cout<<"  },"<<std::endl;
     254         441 :   }
     255             :   // Get all the special groups
     256           1 :   std::cout<<"  \"groups\" : {"<<std::endl;
     257           1 :   std::cout<<"    \"@allatoms\" : { \n"<<std::endl;
     258           1 :   std::cout<<"        \"description\" : \"refers to all the MD codes atoms and PLUMEDs vatoms\","<<std::endl;
     259           2 :   std::cout<<"        \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/specifying_atoms\""<<std::endl;
     260           1 :   std::cout<<"    },"<<std::endl;
     261           1 :   std::cout<<"    \"@mdatoms\" : { \n"<<std::endl;
     262           1 :   std::cout<<"        \"description\" : \"refers to all the MD codes atoms but not PLUMEDs vatoms\","<<std::endl;
     263           2 :   std::cout<<"        \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/specifying_atoms\""<<std::endl;
     264           1 :   std::cout<<"    },"<<std::endl;
     265           1 :   std::cout<<"    \"@ndx:\" : { \n"<<std::endl;
     266           1 :   std::cout<<"        \"description\" : \"load a group from a GROMACS index file\","<<std::endl;
     267           2 :   std::cout<<"        \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/specifying_atoms.html\""<<std::endl;
     268             :   // Now print all the special keywords in molinfo
     269           1 :   std::map<std::string,std::string> specials( GenericMolInfo::getSpecialKeywords() );
     270          36 :   for(auto const& s : specials ) {
     271          35 :     std::cout<<"    },"<<std::endl;
     272          70 :     std::cout<<"    \""<<s.first<<"\" : { \n"<<std::endl;
     273          70 :     std::cout<<"        \"description\" : \""<<s.second<<"\","<<std::endl;
     274          70 :     std::cout<<"        \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/MOLINFO\""<<std::endl;
     275             :   }
     276           1 :   std::cout<<"        }"<<std::endl;
     277           1 :   std::cout<<"  },"<<std::endl;
     278           1 :   std::cout<<"  \"modules\" : {"<<std::endl;
     279           2 :   std::cout<<"    \""<<allmodules[0]<<"\" : { "<<std::endl;
     280           1 :   printHyperlink( allmodules[0] );
     281           1 :   std::cout<<"        \"description\" : \"A module that will be used for something\""<<std::endl;
     282          44 :   for(unsigned i=1; i<allmodules.size(); ++i) {
     283          43 :     std::cout<<"    },"<<std::endl;
     284          86 :     std::cout<<"    \""<<allmodules[i]<<"\" : { "<<std::endl;
     285          43 :     printHyperlink( allmodules[i] );
     286          43 :     std::cout<<"        \"description\" : \"A module that will be used for something\""<<std::endl;
     287             :   }
     288           1 :   std::cout<<"        }"<<std::endl;
     289           1 :   std::cout<<"  },"<<std::endl;
     290           1 :   std::cout<<"  \"cltools\" : {"<<std::endl;
     291           1 :   std::vector<std::string> cltool_names( cltoolRegister().list() );
     292          20 :   for(unsigned i=0; i<cltool_names.size(); ++i) {
     293          38 :     std::cout<<"  \""<<cltool_names[i]<<'"'<<": {"<<std::endl;
     294          19 :     std::string cltool=cltool_names[i];
     295             :     // Handle converstion to link
     296          19 :     printHyperlink( cltool );
     297          38 :     plumed_massert( getModuleMap().find(cltool)!=getModuleMap().end(), "could not find cltool named " + cltool + " in module map" );
     298          38 :     std::string thismodule = getModuleMap().find(cltool_names[i])->second;
     299          38 :     std::cout<<"    \"module\" : \""<<thismodule<<"\",\n";
     300          19 :     auto mytool = cltoolRegister().create( CLToolOptions(cltool) );
     301          57 :     std::cout<<"    \"description\" : \""<<mytool->description()<<"\",\n";
     302          19 :     if( mytool->inputdata==commandline ) {
     303          16 :       std::cout<<"    \"inputtype\" : \"command line args\",\n";
     304           3 :     } else if( mytool->inputdata==ifile ) {
     305           3 :       std::cout<<"    \"inputtype\" : \"file\",\n";
     306             :     } else {
     307           0 :       error("input type for cltool was not specified");
     308             :     }
     309          19 :     std::cout<<"    \"syntax\" : {"<<std::endl;
     310         255 :     for(unsigned j=0; j<cltoolRegister().get(cltool).keys.size(); ++j) {
     311         236 :       std::string k = cltoolRegister().get(cltool).keys.getKeyword(j);
     312         236 :       printKeywordDocs( k, cltool, cltoolRegister().get(cltool).keys );
     313         236 :       if( j==cltoolRegister().get(cltool).keys.size()-1 ) {
     314             :         std::cout<<std::endl;
     315             :       } else {
     316         217 :         std::cout<<","<<std::endl;
     317             :       }
     318             :     }
     319          19 :     std::cout<<"       }"<<std::endl;
     320          19 :     std::cout<<"     }";
     321          19 :     if( i==cltool_names.size()-1 ) {
     322             :       std::cout<<std::endl;
     323             :     } else {
     324          18 :       std::cout<<","<<std::endl;
     325             :     }
     326          19 :   }
     327           1 :   std::cout<<"  }"<<std::endl;
     328           1 :   std::cout<<"}"<<std::endl;
     329           1 :   return 0;
     330           3 : }
     331             : 
     332             : } // End of namespace
     333             : }

Generated by: LCOV version 1.16