LCOV - code coverage report
Current view: top level - cltools - GenJson.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 125 129 96.9 %
Date: 2026-03-30 11:13:23 Functions: 7 7 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/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       17655 : PLUMED_REGISTER_CLTOOL(GenJson,"gen_json")
      64             : 
      65        5883 : void GenJson::registerKeywords( Keywords& keys ) {
      66        5883 :   CLTool::registerKeywords( keys );
      67       11766 :   keys.add("compulsory","--actions","a file containing one line descriptions of the various actions");
      68        5883 : }
      69             : 
      70           6 : GenJson::GenJson(const CLToolOptions& co ):
      71             :   CLTool(co),
      72           6 :   version("master") {
      73           6 :   inputdata=commandline;
      74          12 :   if( config::getVersionLong().find("dev")==std::string::npos ) {
      75          12 :     version="v"+config::getVersion();
      76             :   }
      77           6 : }
      78             : 
      79           2 : int GenJson::main(FILE* in, FILE*out,Communicator& pc) {
      80           2 :   std::string line(""), actionfile;
      81           2 :   parse("--actions",actionfile);
      82           2 :   IFile myfile;
      83           2 :   myfile.open(actionfile);
      84             :   bool stat;
      85             :   std::map<std::string,std::string> action_map;
      86         926 :   while((stat=myfile.getline(line))) {
      87         924 :     std::size_t col = line.find_first_of(":");
      88         924 :     std::string docs = line.substr(col+1);
      89         924 :     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        1848 :     action_map.insert(std::pair<std::string,std::string>( line.substr(0,col), docs ) );
      93             :   }
      94           2 :   myfile.close();
      95             : 
      96             :   // Cycle over all the action names
      97           2 :   std::cout<<"{"<<std::endl;
      98             :   // Get the vimlink
      99           4 :   std::cout<<"  \"vimlink\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/_vim_syntax.html\","<<std::endl;
     100             :   // And the replicas link
     101           4 :   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           2 :   std::vector<std::string> action_names( actionRegister().getActionNames() );
     104         880 :   for(unsigned i=0; i<action_names.size(); ++i) {
     105        1756 :     std::cout<<"  \""<<action_names[i]<<'"'<<": {"<<std::endl;
     106         878 :     std::string action=action_names[i];
     107             :     // Handle conversion of action names to links
     108        1756 :     std::cout<<"    \"hyperlink\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/";
     109         878 :     std::transform(action.begin(), action.end(), action.begin(), [](unsigned char c) {
     110       10542 :       return std::tolower(c);
     111             :     });
     112             :     while(true) {
     113        1518 :       std::size_t und=action.find_first_of("_");
     114        1518 :       if( und==std::string::npos ) {
     115             :         break;
     116             :       }
     117         640 :       std::string first=action.substr(0,und);
     118        4512 :       for(auto c : first ) {
     119        3872 :         if( isdigit(c) ) {
     120           0 :           std::cout<<c;
     121             :         } else {
     122        7744 :           std::cout<<"_"<<c;
     123             :         }
     124             :       }
     125         640 :       std::cout<<"_";
     126        1280 :       action=action.substr(und+1);
     127         640 :     }
     128        6908 :     for(auto c : action ) {
     129        6030 :       if( isdigit(c) ) {
     130          24 :         std::cout<<c;
     131             :       } else {
     132       12012 :         std::cout<<"_"<<c;
     133             :       }
     134             :     }
     135         878 :     std::cout<<".html\","<<std::endl;
     136        1756 :     std::cout<<"    \"description\" : \""<<action_map[action_names[i]]<<"\",\n";
     137        1756 :     std::cout<<"    \"module\" : \""<<getModuleMap().find(action_names[i])->second<<"\",\n";
     138             :     // Now output keyword information
     139         878 :     Keywords keys;
     140         878 :     actionRegister().getKeywords( action_names[i], keys );
     141        1756 :     std::cout<<"    \"displayname\" : \""<<keys.getDisplayName()<<"\",\n";
     142         878 :     std::cout<<"    \"syntax\" : {"<<std::endl;
     143       11004 :     for(unsigned j=0; j<keys.size(); ++j) {
     144       10126 :       std::string desc = keys.getKeywordDescription( keys.getKeyword(j) );
     145       10126 :       if( desc.find("default=")!=std::string::npos ) {
     146        3282 :         std::size_t brac=desc.find_first_of(")");
     147        6564 :         desc = desc.substr(brac+1);
     148             :       }
     149       10126 :       std::size_t dot=desc.find_first_of(".");
     150       10126 :       std::string mydescrip = desc.substr(0,dot);
     151       10126 :       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       50630 :       std::cout<<"       \""<<keys.getKeyword(j)<<"\" : { \"type\": \""<<keys.getStyle(keys.getKeyword(j))<<"\", \"description\": \""<<mydescrip<<"\", \"multiple\": "<<keys.numbered( keys.getKeyword(j) )<<"}";
     155       11738 :       if( j==keys.size()-1 && !keys.exists("HAS_VALUES") ) {
     156             :         std::cout<<std::endl;
     157             :       } else {
     158        9982 :         std::cout<<","<<std::endl;
     159             :       }
     160             :     }
     161        1756 :     if( keys.exists("HAS_VALUES") ) {
     162         734 :       std::cout<<"       \"output\" : {"<<std::endl;
     163         734 :       std::vector<std::string> components( keys.getOutputComponents() );
     164             :       // Check if we have a value
     165             :       bool hasvalue=true;
     166        1830 :       for(unsigned k=0; k<components.size(); ++k) {
     167        3468 :         if( keys.getOutputComponentFlag( components[k] )=="default" ) {
     168             :           hasvalue=false;
     169             :           break;
     170             :         }
     171             :       }
     172        3280 :       for(unsigned k=0; k<components.size(); ++k) {
     173        2546 :         std::string compname=components[k];
     174        2546 :         if( components[k]==".#!value" ) {
     175             :           hasvalue=false;
     176             :           compname="value";
     177             :         }
     178        5092 :         std::cout<<"         \""<<compname<<"\" : {"<<std::endl;
     179        7638 :         std::cout<<"           \"flag\": \""<<keys.getOutputComponentFlag( components[k] )<<"\","<<std::endl;
     180        2546 :         std::string desc=keys.getOutputComponentDescription( components[k] );
     181        2546 :         std::size_t dot=desc.find_first_of(".");
     182        2546 :         std::string mydescrip = desc.substr(0,dot);
     183        2546 :         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        5092 :         std::cout<<"           \"description\": \""<<mydescrip<<"\""<<std::endl;
     187        2546 :         if( k==components.size()-1 ) {
     188         734 :           std::cout<<"         }"<<std::endl;
     189             :         } else {
     190        1812 :           std::cout<<"         },"<<std::endl;
     191             :         }
     192             :       }
     193         734 :       if( hasvalue && components.size()==0 ) {
     194             :         printf("WARNING: no components have been registered for action %s \n", action_names[i].c_str() );
     195             :       }
     196         734 :       std::cout<<"       }"<<std::endl;
     197             : 
     198         734 :     }
     199         878 :     std::cout<<"    },"<<std::endl;
     200         878 :     if( keys.getNeededKeywords().size()>0 ) {
     201         244 :       std::vector<std::string> neededActions( keys.getNeededKeywords() );
     202         488 :       std::cout<<"    \"needs\" : ["<<"\""<<neededActions[0]<<"\"";
     203        2012 :       for(unsigned j=1; j<neededActions.size(); ++j) {
     204        3536 :         std::cout<<", \""<<neededActions[j]<<"\"";
     205             :       }
     206         244 :       std::cout<<"],"<<std::endl;
     207         244 :     }
     208             :     // This ensures that \n is replaced by \\n
     209         878 :     std::string unsafen="\n", safen="\\n", helpstr = keys.getHelpString();
     210         878 :     for( std::size_t pos = helpstr.find("\n");
     211       27558 :          pos != std::string::npos;
     212       26680 :          pos = helpstr.find("\n", pos)
     213             :        ) {
     214             :       helpstr.replace(pos, unsafen.size(), safen);
     215       26680 :       pos += safen.size();
     216             :     }
     217        1756 :     std::cout<<"    \"help\" : \""<<helpstr<<"\"\n";
     218         878 :     std::cout<<"  },"<<std::endl;
     219         878 :   }
     220             :   // Get all the special groups
     221           2 :   std::cout<<"  \"groups\" : {"<<std::endl;
     222           2 :   std::cout<<"    \"@allatoms\" : { \n"<<std::endl;
     223           2 :   std::cout<<"        \"description\" : \"refers to all the MD codes atoms and PLUMEDs vatoms\","<<std::endl;
     224           4 :   std::cout<<"        \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/_group.html\""<<std::endl;
     225           2 :   std::cout<<"    },"<<std::endl;
     226           2 :   std::cout<<"    \"@mdatoms\" : { \n"<<std::endl;
     227           2 :   std::cout<<"        \"description\" : \"refers to all the MD codes atoms but not PLUMEDs vatoms\","<<std::endl;
     228           4 :   std::cout<<"        \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/_group.html\""<<std::endl;
     229           2 :   std::cout<<"    },"<<std::endl;
     230           2 :   std::cout<<"    \"@ndx:\" : { \n"<<std::endl;
     231           2 :   std::cout<<"        \"description\" : \"load a group from a GROMACS index file\","<<std::endl;
     232           4 :   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           2 :   std::map<std::string,std::string> specials( GenericMolInfo::getSpecialKeywords() );
     235          72 :   for(auto const& s : specials ) {
     236          70 :     std::cout<<"    },"<<std::endl;
     237         140 :     std::cout<<"    \""<<s.first<<"\" : { \n"<<std::endl;
     238         140 :     std::cout<<"        \"description\" : \""<<s.second<<"\","<<std::endl;
     239         140 :     std::cout<<"        \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/_m_o_l_i_n_f_o.html\""<<std::endl;
     240             :   }
     241           2 :   std::cout<<"        }"<<std::endl;
     242           2 :   std::cout<<"  }"<<std::endl;
     243           2 :   std::cout<<"}"<<std::endl;
     244           2 :   return 0;
     245           4 : }
     246             : 
     247             : } // End of namespace
     248             : }

Generated by: LCOV version 1.16