Changeset - 6d2e1692ec10
[Not reviewed]
default
0 5 0
Roeland Merks - 15 years ago 2010-05-26 10:47:32
roeland.merks@cwi.nl
I've repaired VLeaf's batch mode to work with dynamic model loading. There is a new option now "-m" that allows you to supply a model name.

For example:

./VirtualLeaf -l ../data/leaves/auxin_growth.xml -m libauxingrowth.so -b

or

./VirtualLeaf --leaffile ../data/leaves/auxin_growth.xml --model libauxingrowth.so --batch


user: Roeland Merks <roeland.merks@cwi.nl>
branch 'default'
changed src/VirtualLeaf.cpp
changed src/VirtualLeaf.pro
changed src/modelcatalogue.cpp
changed src/modelcatalogue.h
changed src/warning.cpp
5 files changed with 92 insertions and 27 deletions:
0 comments (0 inline, 0 general)
src/VirtualLeaf.cpp
Show inline comments
 
@@ -385,26 +385,27 @@ int main(int argc,char **argv) {
 
						
 

	
 
    int c;
 

	
 
						
 
    char *leaffile=0;
 

	
 
    char *modelfile=0;
 
						
 
    while (1) {
 
							
 
      //int this_option_optind = optind ? optind : 1;
 
      int option_index = 0;
 
      static struct option long_options[] = {
 
	{"batch", 0, 0, 0},
 
	{"leaffile", 2, 0, 0}
 
	{"batch", no_argument, NULL, 'b'},
 
	{"leaffile", required_argument, NULL, 'l'},
 
	{"model", required_argument, NULL, 'm'} 
 
      };
 
		
 
      // short option 'p' creates trouble for non-commandline usage on MacOSX. Option -p changed to -P (capital)
 
      static char *short_options = "bl";
 
      c = getopt_long (argc, argv, "bl:",
 
      static char *short_options = "blm";
 
      c = getopt_long (argc, argv, "bl:m:",
 
		       long_options, &option_index);
 
      if (c == -1)
 
	break;
 
		
 
		
 
      if (c==0) {
 
@@ -426,13 +427,20 @@ int main(int argc,char **argv) {
 
	leaffile=strdup(optarg);
 
	if (!leaffile) {
 
	  throw("Out of memory");
 
	}
 
	printf("Reading leaf state file '%s'\n", leaffile);
 
	break;
 
				
 

	
 
      case 'm':
 
	modelfile=strdup(optarg);
 
	if (!modelfile) {
 
	  throw("Out of memory");
 
	}
 
	break;
 
					
 
      case '?':
 
	break;
 
				
 
      default:
 
	printf ("?? getopt returned character code 0%o ??\n", c);
 
      }
 
@@ -491,25 +499,25 @@ int main(int argc,char **argv) {
 
	  foreach (SimPluginInterface *p, plugins) {
 
		  cerr << p->ModelID().toStdString() << endl;
 
	  }
 
	  */
 

	
 
	 	  
 

	
 
	  ModelCatalogue model_catalogue(&mesh, (Main *)main_window);
 
	  model_catalogue.PopulateModelMenu();
 
	  model_catalogue.InstallFirstModel();
 
    // Install model or read catalogue of models
 
    ModelCatalogue model_catalogue(&mesh, useGUI?(Main *)main_window:0,modelfile);
 
    if (useGUI)
 
      model_catalogue.PopulateModelMenu();
 
    model_catalogue.InstallFirstModel();
 
	  
 
	  //main_window->Init(leaffile);
 
    main_window->Init(leaffile);
 
	  
 
    Cell::SetMagnification(1);
 
    Cell::setOffset(0,0);
 
						
 
    main_window->FitLeafToCanvas();
 
						
 
				
 
							
 
						
 
    main_window->Plot();
 

	
 
						
 

	
 
    if (batch) {
src/VirtualLeaf.pro
Show inline comments
 
@@ -16,14 +16,14 @@
 
#  You should have received a copy of the GNU General Public License
 
#  along with the Virtual Leaf.  If not, see <http://www.gnu.org/licenses/>.
 
#
 
#  Copyright 2010 Roeland Merks.
 
#
 

	
 
CONFIG += release
 
CONFIG -= debug
 
CONFIG -= release
 
CONFIG += debug
 
CONFIG += qt
 

	
 
QMAKE_CXXFLAGS += -fexceptions
 
QMAKE_CXXFLAGS_DEBUG += -g3
 
QMAKE_CXXFLAGS_DEBUG += -DQDEBUG
 

	
src/modelcatalogue.cpp
Show inline comments
 
@@ -22,16 +22,22 @@
 
#include <string>
 
#include "modelcatalogue.h"
 
#include <QVariant>
 

	
 
static const std::string _module_id("$Id$");
 

	
 
ModelCatalogue::ModelCatalogue(Mesh *_mesh, Main *_mainwin) {
 
ModelCatalogue::ModelCatalogue(Mesh *_mesh, Main *_mainwin, const char *model=0) {
 
	mesh = _mesh;
 
	mainwin = _mainwin;
 
	LoadPlugins();
 
	if (model) {
 
	  cerr << "Loading model: " << model << endl;
 
		LoadPlugin(model);
 
	} else {
 
	  cerr << "Loading all models." << endl;
 
		LoadPlugins();
 
	}
 
}
 

	
 
void ModelCatalogue::LoadPlugins() {
 
	
 
	QDir pluginDir(QApplication::applicationDirPath()); 
 
	QStringList plugin_filters; // filter for plugins, i.e "*.dll", "*.dylib"
 
@@ -52,24 +58,71 @@ void ModelCatalogue::LoadPlugins() {
 

	
 
	if (!pluginDir.cd("models")) {
 
		MyWarning::error("Directory 'models' not found!");
 
	}
 
	
 
	
 
	QVector<SimPluginInterface *> plugins;
 
	//QVector<SimPluginInterface *> plugins;
 
	foreach (QString fileName, pluginDir.entryList(QDir::Files)){ 
 
		QPluginLoader loader(pluginDir.absoluteFilePath(fileName)); 
 
		if (SimPluginInterface *plugin = 
 
			qobject_cast<SimPluginInterface *>(loader.instance())) {
 
			models.append(plugin); 
 
		} else {
 
			MyWarning::warning("Could not load plugin %s",fileName.toStdString().c_str());
 
		}
 
	}
 
}
 

	
 
void ModelCatalogue::LoadPlugin(const char *model) {
 

	
 
	
 
  QDir pluginDir(QApplication::applicationDirPath()); 
 
  QStringList plugin_filters; // filter for plugins, i.e "*.dll", "*.dylib"
 
	
 
	
 
#if defined(Q_OS_WIN) 
 
  if (pluginDir.dirName().toLower() =="debug" 
 
      ||pluginDir.dirName().toLower() =="release") 
 
    pluginDir.cdUp(); 
 
  //plugin_filters << "*.dll";
 
#elif defined(Q_OS_MAC) 
 
  if (pluginDir.dirName() =="MacOS"){ 
 
    pluginDir.cdUp(); 
 
    pluginDir.cdUp(); 
 
    pluginDir.cdUp(); 
 
  } 
 
  //plugin_filters << "*.dylib";
 
#endif
 
  plugin_filters << model;
 
  pluginDir.setNameFilters(plugin_filters);
 
	
 
  if (!pluginDir.cd("models")) {
 
    MyWarning::error("Directory 'models' not found!");
 
  }
 
	
 
	
 
  //QVector<SimPluginInterface *> plugins;
 
	
 
  QStringList modelnames=pluginDir.entryList(QDir::Files);
 
  if (modelnames.empty()) {
 
    MyWarning::error("Model %s not found - hint: do not include path in filename.",model);
 
  }
 
  foreach (QString fileName, modelnames){ 
 
    QPluginLoader loader(pluginDir.absoluteFilePath(fileName)); 
 
		
 
    if (SimPluginInterface *plugin = 
 
	qobject_cast<SimPluginInterface *>(loader.instance())) {
 
      models.append(plugin); 
 
      MyWarning::warning("Successfully loaded model %s",fileName.toStdString().c_str());
 
    } else {
 
      MyWarning::warning("Could not load plugin %s",fileName.toStdString().c_str());
 
    }
 
  }
 
}
 

	
 
void ModelCatalogue::InstallFirstModel() {
 
	InstallModel(models[0]);
 
}
 
void ModelCatalogue::PopulateModelMenu() {
 
	foreach (SimPluginInterface *model, models) {
 
		QAction *modelaction = new QAction(model->ModelID(), mainwin); 
 
@@ -96,10 +149,13 @@ void ModelCatalogue::InstallModel(SimPlu
 
	plugin->SetCellsStaticDatamembers(CellBase::GetStaticDataMemberPointer());
 
	
 
	mesh->SetSimPlugin(plugin);
 
	Cell::SetNChem(plugin->NChem());
 
	plugin->SetParameters(&par);
 

	
 
	mainwin->RefreshInfoBar();
 
//	mesh->StandardInit();
 
	mainwin->Init(0);
 
	if (mainwin) {
 
		mainwin->RefreshInfoBar();
 
		//mainwin->Init(0);
 
	}
 
	//	mesh->StandardInit();
 
	
 
}
src/modelcatalogue.h
Show inline comments
 
@@ -38,14 +38,15 @@
 
#include <QAction>
 
#include <QMenu>
 

	
 
class ModelCatalogue : public QObject {
 
Q_OBJECT
 
public:
 
	ModelCatalogue(Mesh *mesh, Main *mainwin); 	
 
	ModelCatalogue(Mesh *mesh, Main *mainwin, const char *model); 	
 
	void LoadPlugins(); 
 
	void LoadPlugin(const char *model);
 
	
 
	void InstallFirstModel();
 
	void PopulateModelMenu();	
 

	
 
public slots:
 
	void InstallModel(SimPluginInterface *model);	
 
@@ -53,7 +54,7 @@ public slots:
 
private:
 
	QVector<SimPluginInterface *> models;
 
	Mesh *mesh;
 
	Main *mainwin;
 

	
 
};
 
#endif
 
\ No newline at end of file
 
#endif
src/warning.cpp
Show inline comments
 
@@ -63,15 +63,15 @@ void MyWarning::error(const char *fmt, .
 
  va_start(ap, fmt);
 
  vsnprintf(message, 999, fmt, ap);		/* invoke interface to printf       */
 
  va_end(ap);
 
  
 
  QString qmess(message);
 
  
 
  bool batch = false;
 
  //  bool batch = false;
 
  
 
  if (batch) {
 
  if (qApp->type()==QApplication::Tty) {
 
    // batch mode: print the message to stderr
 
    fprintf(stderr, "Fatal error: %s\n",qmess.toStdString().c_str());
 
    exit(1);
 
  } else { // issue a dialog box
 
    /* Solve this with signal and slot...! */
 
	  //extern MainBase *main_window;
 
@@ -120,15 +120,15 @@ void MyWarning::warning(const char *fmt,
 
  va_start(ap, fmt);
 
  vsnprintf(message, 999, fmt, ap);		/* invoke interface to printf       */
 
  va_end(ap);
 
  
 
  QString qmess(message);
 
  
 
  bool batch = false;
 
  //  bool batch = false;
 
  
 
  if (batch) {
 
  if (qApp->type()==QApplication::Tty) {
 
    // batch mode: print the message to stderr
 
    fprintf(stderr, "Warning: %s\n",qmess.toStdString().c_str());
 
  } else { // issue a dialog box
 
	  UniqueMessageBox msgBox( QString("Warning"), qmess );
 
	  msgBox.exec();
 
  }
0 comments (0 inline, 0 general)