Skip to content
Snippets Groups Projects
Commit 2dcdc107 authored by Davide Branduardi's avatar Davide Branduardi
Browse files

first try with regexp for arguments

parent dc10b7ea
No related branches found
No related tags found
No related merge requests found
......@@ -48,5 +48,15 @@ std::string ActionSet::getLabelList() const{
return outlist;
}
std::vector<std::string> ActionSet::getLabelVector() const{
std::vector<std::string> outlist;
for(const_iterator p=begin();p!=end();++p){
outlist.push_back(dynamic_cast<Action*>(*p)->getLabel());
};
return outlist;
}
}
......@@ -62,6 +62,8 @@ public:
T selectWithLabel(const std::string&s)const;
/// get the labels in the list of actions in form of a string (useful to debug)
std::string getLabelList() const;
/// get the labels in the form of a vector of strings
std::vector<std::string> getLabelVector() const;
};
/////
......
......@@ -24,6 +24,12 @@
#include "tools/PDB.h"
#include "PlumedMain.h"
#include "ActionSet.h"
#include <iostream>
#define CREGEX
#ifdef CREGEX
#include <cstring>
#include "regex.h"
#endif
using namespace std;
namespace PLMD{
......@@ -37,75 +43,147 @@ void ActionWithArguments::parseArgumentList(const std::string&key,std::vector<Va
}
void ActionWithArguments::interpretArgumentList(const std::vector<std::string>& c, std::vector<Value*>&arg){
for(unsigned i=0;i<c.size();i++){
std::size_t dot=c[i].find_first_of('.');
string a=c[i].substr(0,dot);
string name=c[i].substr(dot+1);
if(c[i].find(".")!=string::npos){ // if it contains a dot:
if(a=="*" && name=="*"){
// Take all values from all actions
std::vector<ActionWithValue*> all=plumed.getActionSet().select<ActionWithValue*>();
if( all.empty() ) error("your input file is not telling plumed to calculate anything");
for(unsigned j=0;j<all.size();j++){
for(int k=0;k<all[j]->getNumberOfComponents();++k) arg.push_back(all[j]->copyOutput(k));
}
} else if ( name=="*"){
// Take all the values from an action with a specific name
ActionWithValue* action=plumed.getActionSet().selectWithLabel<ActionWithValue*>(a);
if(!action){
std::string str=" (hint! the actions in this ActionSet are: ";
str+=plumed.getActionSet().getLabelList()+")";
error("cannot find action named " + a + str);
}
for(int k=0;k<action->getNumberOfComponents();++k) arg.push_back(action->copyOutput(k));
} else if ( a=="*" ){
// Take components from all actions with a specific name
std::vector<ActionWithValue*> all=plumed.getActionSet().select<ActionWithValue*>();
if( all.empty() ) error("your input file is not telling plumed to calculate anything");
unsigned nval=0;
for(unsigned j=0;j<all.size();j++){
std::string flab; flab=all[j]->getLabel() + "." + name;
if( all[j]->exists(flab) ){ arg.push_back(all[j]->copyOutput(flab)); nval++; }
}
if(nval==0) error("found no actions with a component called " + name );
} else {
// Take values with a specific name
ActionWithValue* action=plumed.getActionSet().selectWithLabel<ActionWithValue*>(a);
if(!action){
std::string str=" (hint! the actions in this ActionSet are: ";
str+=plumed.getActionSet().getLabelList()+")";
error("cannot find action named " + a +str);
}
if( !(action->exists(c[i])) ){
std::string str=" (hint! the components in this actions are: ";
str+=action->getComponentsList()+")";
error("action " + a + " has no component named " + name + str);
} ;
arg.push_back(action->copyOutput(c[i]));
}
} else { // if it doesn't contain a dot
if(c[i]=="*"){
// Take all values from all actions
std::vector<ActionWithValue*> all=plumed.getActionSet().select<ActionWithValue*>();
if( all.empty() ) error("your input file is not telling plumed to calculate anything");
for(unsigned j=0;j<all.size();j++){
for(int k=0;k<all[j]->getNumberOfComponents();++k) arg.push_back(all[j]->copyOutput(k));
}
} else {
ActionWithValue* action=plumed.getActionSet().selectWithLabel<ActionWithValue*>(c[i]);
if(!action){
std::string str=" (hint! the actions in this ActionSet are: ";
str+=plumed.getActionSet().getLabelList()+")";
error("cannot find action named " + c[i] + str );
}
if( !(action->exists(c[i])) ){
std::string str=" (hint! the components in this actions are: ";
str+=action->getComponentsList()+")";
error("action " + c[i] + " has no component named " + c[i] +str);
};
arg.push_back(action->copyOutput(c[i]));
}
}
// is a regex? then just interpret it. The signal is ()
std::size_t found1 = c[i].find("(");
std::size_t found2 ;
if(found1!=std::string::npos){
found2=c[i].find(")",found1+1,1); // find it again
if(found2!=std::string::npos){
// start regex parsing
#ifdef CREGEX
// take the string enclosed in quotes and put in round brackets
std::string myregex=c[i].substr(found1,found2-found1+1);
log.printf(" Evaluating regexp for this action: %s \n",myregex.c_str());
int errcode;
regex_t *preg = (regex_t*)malloc(sizeof(regex_t)); // pointer to the regular expression
regmatch_t *pmatch;
if ((errcode=regcomp(preg, myregex.c_str() ,REG_EXTENDED|REG_NEWLINE))) { // compile the regular expression
char* errbuf;
size_t errbuf_size;
// one can check the errors asking to regerror
errbuf_size = regerror(errcode, preg, NULL, 0);
if (!(errbuf=(char*)malloc(errbuf_size))) {
plumed_merror("cannot allocate the buffer for error detection in regexp!");
};
regerror(errcode, preg, errbuf, errbuf_size);
error(errbuf);
}
plumed_massert(preg->re_nsub==1,"I can parse with only one subexpression");
pmatch = (regmatch_t*)malloc(sizeof(regmatch_t)*preg->re_nsub);
// select all the actions that have a value
std::vector<ActionWithValue*> all=plumed.getActionSet().select<ActionWithValue*>();
if( all.empty() ) error("your input file is not telling plumed to calculate anything");
for(unsigned j=0;j<all.size();j++){
std::string thisargument=all[j]->getLabel();
std::vector<std::string> ss=all[j]->getComponentsVector();
for(unsigned k=0;k<ss.size();++k){
thisargument=ss[k];
unsigned ll=strlen(ss[k].c_str())+1;
char str[ll];strcpy(str,ss[k].c_str());
char *ppstr=str;
if(!regexec(preg, ppstr , preg->re_nsub, pmatch, 0)) {
log.printf(" Something matched with \"%s\" : ",ss[k].c_str());
do {
if (pmatch[0].rm_so != -1) { /* The regex is matching part of a string */
char *submatch;
size_t matchlen = pmatch[0].rm_eo - pmatch[0].rm_so;
submatch = (char*)malloc(matchlen+1);
strncpy(submatch, ppstr+pmatch[0].rm_so, matchlen+1);
submatch[matchlen]='\0';
log.printf(" subpattern %s\n", submatch);
// this is the match: try to see if it is a valid action
std::string putativeVal(submatch);
if( all[j]->exists(putativeVal) ){
arg.push_back(all[j]->copyOutput(putativeVal));
log.printf(" Action %s added! \n",putativeVal.c_str());
}
free(submatch);
};
ppstr += pmatch[0].rm_eo; /* Restart from last match */
} while(!regexec(preg,ppstr,preg->re_nsub,pmatch,0));
}
}
};
regfree(preg);
free(pmatch);
#else
plumed_merror("Regexp support not compiled!");
#endif
}else{
plumed_merror("did you want to use regexp to input arguments? enclose it between two round braces (...) with no spaces!");
}
}else{
std::size_t dot=c[i].find_first_of('.');
string a=c[i].substr(0,dot);
string name=c[i].substr(dot+1);
if(c[i].find(".")!=string::npos){ // if it contains a dot:
if(a=="*" && name=="*"){
// Take all values from all actions
std::vector<ActionWithValue*> all=plumed.getActionSet().select<ActionWithValue*>();
if( all.empty() ) error("your input file is not telling plumed to calculate anything");
for(unsigned j=0;j<all.size();j++){
for(int k=0;k<all[j]->getNumberOfComponents();++k) arg.push_back(all[j]->copyOutput(k));
}
} else if ( name=="*"){
// Take all the values from an action with a specific name
ActionWithValue* action=plumed.getActionSet().selectWithLabel<ActionWithValue*>(a);
if(!action){
std::string str=" (hint! the actions in this ActionSet are: ";
str+=plumed.getActionSet().getLabelList()+")";
error("cannot find action named " + a + str);
}
for(int k=0;k<action->getNumberOfComponents();++k) arg.push_back(action->copyOutput(k));
} else if ( a=="*" ){
// Take components from all actions with a specific name
std::vector<ActionWithValue*> all=plumed.getActionSet().select<ActionWithValue*>();
if( all.empty() ) error("your input file is not telling plumed to calculate anything");
unsigned nval=0;
for(unsigned j=0;j<all.size();j++){
std::string flab; flab=all[j]->getLabel() + "." + name;
if( all[j]->exists(flab) ){ arg.push_back(all[j]->copyOutput(flab)); nval++; }
}
if(nval==0) error("found no actions with a component called " + name );
} else {
// Take values with a specific name
ActionWithValue* action=plumed.getActionSet().selectWithLabel<ActionWithValue*>(a);
if(!action){
std::string str=" (hint! the actions in this ActionSet are: ";
str+=plumed.getActionSet().getLabelList()+")";
error("cannot find action named " + a +str);
}
if( !(action->exists(c[i])) ){
std::string str=" (hint! the components in this actions are: ";
str+=action->getComponentsList()+")";
error("action " + a + " has no component named " + name + str);
} ;
arg.push_back(action->copyOutput(c[i]));
}
} else { // if it doesn't contain a dot
if(c[i]=="*"){
// Take all values from all actions
std::vector<ActionWithValue*> all=plumed.getActionSet().select<ActionWithValue*>();
if( all.empty() ) error("your input file is not telling plumed to calculate anything");
for(unsigned j=0;j<all.size();j++){
for(int k=0;k<all[j]->getNumberOfComponents();++k) arg.push_back(all[j]->copyOutput(k));
}
} else {
ActionWithValue* action=plumed.getActionSet().selectWithLabel<ActionWithValue*>(c[i]);
if(!action){
std::string str=" (hint! the actions in this ActionSet are: ";
str+=plumed.getActionSet().getLabelList()+")";
error("cannot find action named " + c[i] + str );
}
if( !(action->exists(c[i])) ){
std::string str=" (hint! the components in this actions are: ";
str+=action->getComponentsList()+")";
error("action " + c[i] + " has no component named " + c[i] +str);
};
arg.push_back(action->copyOutput(c[i]));
}
}
}
}
}
......
......@@ -142,13 +142,22 @@ int ActionWithValue::getComponent( const std::string& name ) const {
std::string ActionWithValue::getComponentsList( ) const {
std::string complist;
plumed_massert( !exists( getLabel() ), "You should not be calling this routine if you are using a value");
//plumed_massert( !exists( getLabel() ), "You should not be calling this routine if you are using a value");
for(unsigned i=0;i<values.size();++i){
complist+=values[i]->name+" ";
}
return complist;
}
std::vector<std::string> ActionWithValue::getComponentsVector( ) const {
std::vector<std::string> complist;
//plumed_massert( !exists( getLabel() ), "You should not be calling this routine if you are using a value");
for(unsigned i=0;i<values.size();++i){
complist.push_back(values[i]->name);
}
return complist;
}
void ActionWithValue::componentIsNotPeriodic( const std::string& name ){
int kk=getComponent(name);
values[kk]->min=0; values[kk]->max=0;
......
......@@ -134,6 +134,8 @@ public:
Value* copyOutput( const unsigned& n ) const;
/// get a string that contains all the available components
std::string getComponentsList( ) const ;
/// get a vector that contains the label for all the components
std::vector<std::string> getComponentsVector( ) const ;
// -- Routines for everything else -- //
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment