Vespucci  1.0.0
fileinterprocess.cpp
Go to the documentation of this file.
1 #include "fileinterprocess.h"
2 #include <QDir>
3 #include <QDirIterator>
4 #include <QProcess>
5 #include <QProcessEnvironment>
6 
7 Vespucci::External::FileInterprocess::FileInterprocess(const arma::field<std::string> invar_names, const arma::field<arma::mat> invars, const arma::field<std::string> outvar_names)
8 {
9  invar_names_ = invar_names;
10  invars_ = invars;
11  outvar_names_ = outvar_names;
12  outvars_.set_size(outvar_names.n_elem);
13 
14  invar_names_.save("interprocess/invar_names.tmp");
15  invars_.save("interprocess/invars.tmp");
16  outvar_names_.save("interprocess/outvar_names.tmp");
17 }
18 
20 {
21  DeleteTemporaryFiles();
22 }
23 
25 {
26  return outvars_;
27 }
28 
29 int Vespucci::External::FileInterprocess::RunR(const std::string &script, const QString &R_HOME)
30 {
31  QProcess R_process;
32  QStringList env_vars = QProcess::systemEnvironment();
33  env_vars << "R_HOME="+R_HOME;//set R_HOME environment variable before starting process
34  R_process.setEnvironment(env_vars);
35  std::ofstream cmd_stream("interprocess/commands.tmp");
36  cmd_stream << script;
37  cmd_stream.close();
38 
39  QStringList args;
40  args << "interprocess/invar_names.tmp"
41  << "interprocess/invars.tmp"
42  << "interprocess/outvar_names.tmp"
43  << "interprocess/outvars.tmp"
44  << "interprocess/commands.tmp";
45 
46  R_process.start("VespucciR", args);
47  R_process.waitForFinished(-1);
48  return R_process.exitCode();
49 }
50 
52 {
53  QProcess Octave_process;
54  QStringList env_vars = QProcess::systemEnvironment();
55  Octave_process.setEnvironment(env_vars);
56  std::ofstream cmd_stream("interprocess/commands_tmp.m");
57  //create wrapper function for octave to call
58  cmd_stream << "function ";
59  if (outvar_names_.n_elem){
60  cmd_stream << "[";
61  for (arma::uword i = 0; i < outvar_names_.n_elem - 1; ++i){
62  cmd_stream << outvar_names_(i) << ",";
63  }
64  cmd_stream << outvar_names_(outvar_names_.n_elem - 1) << "] = ";
65  }
66  cmd_stream << "VespucciOctave(";
67  for (arma::uword i = 0; i < invar_names_.n_elem - 1; ++i){
68  cmd_stream << invar_names_(i) << ",";
69  }
70  cmd_stream << invar_names_(invar_names_.n_elem - 1) << ")" << std::endl;
71 
72  cmd_stream << script << std::endl << "endfunction";
73  cmd_stream.close();
74 
75  QStringList args;
76  args << "interprocess/invar_names.tmp"
77  << "interprocess/invars.tmp"
78  << "interprocess/outvar_names.tmp"
79  << "interprocess/outvars.tmp"
80  << "interprocess/commands_tmp.m";
81 
82  Octave_process.start("VespucciOctave", args);
83  Octave_process.waitForFinished(-1);
84  return Octave_process.exitCode();
85 }
86 
88 {
89 
90 }
91 
92 void Vespucci::External::FileInterprocess::GetOutvars()
93 {
94  outvars_.load("interprocess/outvars.tmp");
95 }
96 
97 void Vespucci::External::FileInterprocess::DeleteTemporaryFiles()
98 {
99  QDir dir("interprocess");
100  QDirIterator it("interprocess", QDirIterator::NoIteratorFlags);
101  while(it.hasNext()){
102  if (it.fileInfo().suffix() == "tmp" || it.fileInfo().suffix() == "m"){
103  dir.remove(it.filePath());
104  }
105  }
106 }
107 
arma::field< arma::mat > outvars()
int RunOctave(const std::string &script)
FileInterprocess(const arma::field< std::string > invar_names, const arma::field< arma::mat > invars, const arma::field< std::string > outvar_names)
int RunR(const std::string &script, const QString &R_HOME)