LCOV - code coverage report
Current view: top level - cltools - GenJson.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 104 104 100.0 %
Date: 2024-10-18 14:00:25 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       15953 : PLUMED_REGISTER_CLTOOL(GenJson,"gen_json")
      64             : 
      65        5316 : void GenJson::registerKeywords( Keywords& keys ) {
      66        5316 :   CLTool::registerKeywords( keys );
      67       10632 :   keys.add("compulsory","--actions","a file containing one line descriptions of the various actions");
      68        5316 : }
      69             : 
      70           5 : GenJson::GenJson(const CLToolOptions& co ):
      71             :   CLTool(co),
      72           5 :   version("master")
      73             : {
      74           5 :   inputdata=commandline;
      75          15 :   if( config::getVersionLong().find("dev")==std::string::npos ) version="v"+config::getVersion();
      76           5 : }
      77             : 
      78           1 : int GenJson::main(FILE* in, FILE*out,Communicator& pc) {
      79           1 :   std::string line(""), actionfile; parse("--actions",actionfile);
      80           1 :   IFile myfile; myfile.open(actionfile); bool stat;
      81             :   std::map<std::string,std::string> action_map;
      82         464 :   while((stat=myfile.getline(line))) {
      83         463 :     std::size_t col = line.find_first_of(":"); std::string docs = line.substr(col+1);
      84         463 :     if( docs.find("\\")!=std::string::npos ) error("found invalid backslash character in first line of documentation for action " + line.substr(0,col) );
      85         926 :     action_map.insert(std::pair<std::string,std::string>( line.substr(0,col), docs ) );
      86             :   }
      87           1 :   myfile.close();
      88             : 
      89             :   // Cycle over all the action names
      90           1 :   std::cout<<"{"<<std::endl;
      91             :   // Get the vimlink
      92           2 :   std::cout<<"  \"vimlink\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/_vim_syntax.html\","<<std::endl;
      93             :   // And the replicas link
      94           2 :   std::cout<<"  \"replicalink\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/special-replica-syntax.html\","<<std::endl;
      95             :   // Get the names of all the actions
      96           1 :   std::vector<std::string> action_names( actionRegister().getActionNames() );
      97         439 :   for(unsigned i=0; i<action_names.size(); ++i) {
      98        1314 :     std::cout<<"  \""<<action_names[i]<<'"'<<": {"<<std::endl; std::string action=action_names[i];
      99             :     // Handle conversion of action names to links
     100         876 :     std::cout<<"    \"hyperlink\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/";
     101        5705 :     std::transform(action.begin(), action.end(), action.begin(), [](unsigned char c) { return std::tolower(c); });
     102             :     while(true) {
     103         758 :       std::size_t und=action.find_first_of("_");
     104         758 :       if( und==std::string::npos ) break;
     105         320 :       std::string first=action.substr(0,und);
     106        4192 :       for(auto c : first ) { if( isdigit(c) ) std::cout<<c; else std::cout<<"_"<<c; }
     107         640 :       std::cout<<"_"; action=action.substr(und+1);
     108         320 :     }
     109        6460 :     for(auto c : action ) { if( isdigit(c) ) std::cout<<c; else std::cout<<"_"<<c; }
     110         438 :     std::cout<<".html\","<<std::endl;
     111         876 :     std::cout<<"    \"description\" : \""<<action_map[action_names[i]]<<"\",\n";
     112         876 :     std::cout<<"    \"module\" : \""<<getModuleMap().find(action_names[i])->second<<"\",\n";
     113             :     // Now output keyword information
     114         438 :     Keywords keys; actionRegister().getKeywords( action_names[i], keys );
     115         876 :     std::cout<<"    \"displayname\" : \""<<keys.getDisplayName()<<"\",\n";
     116         438 :     std::cout<<"    \"syntax\" : {"<<std::endl;
     117        5482 :     for(unsigned j=0; j<keys.size(); ++j) {
     118        5044 :       std::string desc = keys.getKeywordDescription( keys.getKeyword(j) );
     119        5044 :       if( desc.find("default=")!=std::string::npos ) {
     120        3274 :         std::size_t brac=desc.find_first_of(")"); desc = desc.substr(brac+1);
     121             :       }
     122        5044 :       std::size_t dot=desc.find_first_of("."); std::string mydescrip = desc.substr(0,dot);
     123        5044 :       if( mydescrip.find("\\")!=std::string::npos ) error("found invalid backslash character documentation for keyword " + keys.getKeyword(j) + " in action " + action_names[i] );
     124       25220 :       std::cout<<"       \""<<keys.getKeyword(j)<<"\" : { \"type\": \""<<keys.getStyle(keys.getKeyword(j))<<"\", \"description\": \""<<mydescrip<<"\", \"multiple\": "<<keys.numbered( keys.getKeyword(j) )<<"}";
     125        5848 :       if( j==keys.size()-1 && !keys.exists("HAS_VALUES") ) std::cout<<std::endl; else std::cout<<","<<std::endl;
     126             :     }
     127         876 :     if( keys.exists("HAS_VALUES") ) {
     128         366 :       std::cout<<"       \"output\" : {"<<std::endl;
     129         366 :       std::vector<std::string> components( keys.getOutputComponents() );
     130             :       // Check if we have a value
     131             :       bool hasvalue=true;
     132         914 :       for(unsigned k=0; k<components.size(); ++k) {
     133        1732 :         if( keys.getOutputComponentFlag( components[k] )=="default" ) { hasvalue=false; break; }
     134             :       }
     135        1638 :       for(unsigned k=0; k<components.size(); ++k) {
     136        2544 :         std::string compname=components[k]; if( components[k]==".#!value" ) { hasvalue=false; compname="value"; }
     137        2544 :         std::cout<<"         \""<<compname<<"\" : {"<<std::endl;
     138        3816 :         std::cout<<"           \"flag\": \""<<keys.getOutputComponentFlag( components[k] )<<"\","<<std::endl;
     139        1272 :         std::string desc=keys.getOutputComponentDescription( components[k] );
     140        1272 :         std::size_t dot=desc.find_first_of("."); std::string mydescrip = desc.substr(0,dot);
     141        1272 :         if( mydescrip.find("\\")!=std::string::npos ) error("found invalid backslash character documentation for output component " + compname + " in action " + action_names[i] );
     142        2544 :         std::cout<<"           \"description\": \""<<mydescrip<<"\""<<std::endl;
     143        1272 :         if( k==components.size()-1 ) std::cout<<"         }"<<std::endl; else std::cout<<"         },"<<std::endl;
     144             :       }
     145         366 :       if( hasvalue && components.size()==0 ) printf("WARNING: no components have been registered for action %s \n", action_names[i].c_str() );
     146         366 :       std::cout<<"       }"<<std::endl;
     147             : 
     148         366 :     }
     149         438 :     std::cout<<"    },"<<std::endl;
     150         438 :     if( keys.getNeededKeywords().size()>0 ) {
     151         122 :       std::vector<std::string> neededActions( keys.getNeededKeywords() );
     152         244 :       std::cout<<"    \"needs\" : ["<<"\""<<neededActions[0]<<"\"";
     153        1006 :       for(unsigned j=1; j<neededActions.size(); ++j) std::cout<<", \""<<neededActions[j]<<"\"";
     154         122 :       std::cout<<"],"<<std::endl;
     155         122 :     }
     156             :     // This ensures that \n is replaced by \\n
     157         438 :     std::string unsafen="\n", safen="\\n", helpstr = keys.getHelpString();
     158         438 :     for( std::size_t pos = helpstr.find("\n");
     159       13735 :          pos != std::string::npos;
     160       13297 :          pos = helpstr.find("\n", pos)
     161       13297 :        ) { helpstr.replace(pos, unsafen.size(), safen); pos += safen.size(); }
     162         876 :     std::cout<<"    \"help\" : \""<<helpstr<<"\"\n";
     163         438 :     std::cout<<"  },"<<std::endl;
     164         438 :   }
     165             :   // Get all the special groups
     166           1 :   std::cout<<"  \"groups\" : {"<<std::endl;
     167           1 :   std::cout<<"    \"@allatoms\" : { \n"<<std::endl;
     168           1 :   std::cout<<"        \"description\" : \"refers to all the MD codes atoms and PLUMEDs vatoms\","<<std::endl;
     169           2 :   std::cout<<"        \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/_group.html\""<<std::endl;
     170           1 :   std::cout<<"    },"<<std::endl;
     171           1 :   std::cout<<"    \"@mdatoms\" : { \n"<<std::endl;
     172           1 :   std::cout<<"        \"description\" : \"refers to all the MD codes atoms but not PLUMEDs vatoms\","<<std::endl;
     173           2 :   std::cout<<"        \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/_group.html\""<<std::endl;
     174           1 :   std::cout<<"    },"<<std::endl;
     175           1 :   std::cout<<"    \"@ndx\" : { \n"<<std::endl;
     176           1 :   std::cout<<"        \"description\" : \"load a group from a GROMACS index file\","<<std::endl;
     177           2 :   std::cout<<"        \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/_group.html\""<<std::endl;
     178             :   // Now print all the special keywords in molinfo
     179           1 :   std::map<std::string,std::string> specials( GenericMolInfo::getSpecialKeywords() );
     180          36 :   for(auto const& s : specials ) {
     181          35 :     std::cout<<"    },"<<std::endl;
     182          70 :     std::cout<<"    \""<<s.first<<"\" : { \n"<<std::endl;
     183          70 :     std::cout<<"        \"description\" : \""<<s.second<<"\","<<std::endl;
     184          70 :     std::cout<<"        \"link\" : \"https://www.plumed.org/doc-"<<version<<"/user-doc/html/_m_o_l_i_n_f_o.html\""<<std::endl;
     185             :   }
     186           1 :   std::cout<<"        }"<<std::endl;
     187           1 :   std::cout<<"  }"<<std::endl;
     188           1 :   std::cout<<"}"<<std::endl;
     189           1 :   return 0;
     190           2 : }
     191             : 
     192             : } // End of namespace
     193             : }

Generated by: LCOV version 1.16