1 // Copyright (C) 2006-2008 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "yacsconfig.h"
20 #include "RuntimeSALOME.hxx"
23 #include "Exception.hxx"
24 #include "Executor.hxx"
25 #include "parsers.hxx"
26 #include "VisitorSaveState.hxx"
27 #include "VisitorSaveSalomeSchema.hxx"
28 #include "LoadState.hxx"
29 #include "Dispatcher.hxx"
30 #include "LinkInfo.hxx"
33 #include "SALOME_NamingService.hxx"
34 #include "SALOME_ModuleCatalog.hh"
41 using YACS::YACSLoader;
42 using namespace YACS::ENGINE;
46 // --- use of glibc argp interface for parsing unix-style arguments
48 const char *argp_program_version ="driver V0.1";
49 const char *argp_program_bug_address ="<nepal@nepal.edf.fr>";
50 static char doc[] ="driver -- a SALOME YACS graph executor";
51 static char args_doc[] = "graph.xml";
53 static struct argp_option options[] =
55 {"display", 'd', "level", 0, "Display dot files: 0=never to 3=very often"},
56 {"verbose", 'v', 0, 0, "Produce verbose output" },
57 {"stop-on-error", 's', 0, 0, "Stop on first error" },
58 {"dump-on-error", 'e', "file", OPTION_ARG_OPTIONAL, "Stop on first error and dump state"},
59 {"dump-final", 'f', "file", OPTION_ARG_OPTIONAL, "dump final state"},
60 {"load-state", 'l', "file", 0, "Load State from a previous partial execution"},
61 {"save-xml-schema", 'x', "file", OPTION_ARG_OPTIONAL, "dump xml schema"},
78 parse_opt (int key, char *arg, struct argp_state *state)
80 // Get the input argument from argp_parse, which we
81 // know is a pointer to our arguments structure.
82 struct arguments *myArgs = (arguments*)state->input;
87 myArgs->display = atoi(arg);
98 myArgs->dumpErrorFile = arg;
100 myArgs->dumpErrorFile = "dumpErrorState.xml";
104 myArgs->finalDump = arg;
106 myArgs->finalDump = "finalDumpState.xml";
109 myArgs->loadState = arg;
113 myArgs->xmlSchema = arg;
115 myArgs->xmlSchema = "saveSchema.xml";
119 if (state->arg_num >=1) // Too many arguments.
121 myArgs->args[state->arg_num] = arg;
125 if (state->arg_num < 1) // Not enough arguments.
130 return ARGP_ERR_UNKNOWN;
136 static struct argp argp = { options, parse_opt, args_doc, doc };
138 void timer(std::string msg)
141 gettimeofday(&tv,NULL);
142 long t=tv.tv_sec*1000+tv.tv_usec/1000;
144 gettimeofday(&tv,NULL);
145 std::cerr << msg << tv.tv_sec*1000+tv.tv_usec/1000-t0 << " ms" << std::endl;
148 main (int argc, char* argv[])
150 struct arguments myArgs;
156 myArgs.dumpErrorFile= "";
157 myArgs.finalDump = "";
158 myArgs.loadState = "";
159 myArgs.xmlSchema = "";
161 // Parse our arguments; every option seen by parse_opt will be reflected in arguments.
162 argp_parse (&argp, argc, argv, 0, 0, &myArgs);
163 cerr << "graph = " << myArgs.args[0]
164 << " options: display=" << myArgs.display
165 << " verbose="<<myArgs.verbose
166 << " stop-on-error=" << myArgs.stop;
168 cerr << " dumpErrorFile=" << myArgs.dumpErrorFile << endl;
173 RuntimeSALOME::setRuntime();
175 // Try to load the session catalog if it exists
178 YACS::ENGINE::RuntimeSALOME* runTime = YACS::ENGINE::getSALOMERuntime();
179 CORBA::ORB_ptr orb = runTime->getOrb();
182 SALOME_NamingService namingService(orb);
183 CORBA::Object_var obj = namingService.Resolve("/Kernel/ModulCatalog");
184 SALOME_ModuleCatalog::ModuleCatalog_var aModuleCatalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj);
185 if (! CORBA::is_nil(aModuleCatalog))
187 CORBA::String_var anIOR = orb->object_to_string( aModuleCatalog );
188 YACS::ENGINE::Catalog* aCatalog = runTime->loadCatalog( "session", anIOR.in() );
189 runTime->addCatalog(aCatalog);
193 catch(ServiceUnreachable& e)
195 //Naming service unreachable don't add catalog
203 timer("Elapsed time before load: ");
204 Proc* p=loader.load(myArgs.args[0]);
207 std::cerr << "The imported file is probably not a YACS schema file" << std::endl;
210 //Get the parser logger
211 Logger* logger=p->getLogger("parser");
212 //Print errors logged if any
213 if(!logger->isEmpty())
215 std::cerr << "The imported file has errors" << std::endl;
216 std::cerr << logger->getStr() << std::endl;
218 //Don't execute if there are errors
219 if(logger->hasErrors())
222 Runtime* r=YACS::ENGINE::getRuntime();
223 Dispatcher* disp=Dispatcher::getDispatcher();
229 timer("Elapsed time after load: ");
233 std::string report=p->getErrorReport();
234 std::cerr << "The schema is not valid and can not be executed" << std::endl;
235 std::cerr << report << std::endl;
236 Runtime* r=YACS::ENGINE::getRuntime();
237 Dispatcher* disp=Dispatcher::getDispatcher();
243 timer("Elapsed time after validation: ");
246 LinkInfo info(LinkInfo::ALL_DONT_STOP);
247 p->checkConsistency(info);
248 if(info.areWarningsOrErrors())
250 std::cerr << "The schema is not consistent and can not be executed" << std::endl;
251 std::cerr << info.getGlobalRepr() << std::endl;
252 Runtime* r=YACS::ENGINE::getRuntime();
253 Dispatcher* disp=Dispatcher::getDispatcher();
259 timer("Elapsed time after check consistency: ");
262 bool isXmlSchema = (strlen(myArgs.xmlSchema) != 0);
265 YACS::ENGINE::VisitorSaveSalomeSchema vss(p);
266 vss.openFileSchema(myArgs.xmlSchema);
268 vss.closeFileSchema();
271 bool fromScratch = (strlen(myArgs.loadState) == 0);
276 stateParser* rootParser = new stateParser();
277 stateLoader myStateLoader(rootParser, p);
278 myStateLoader.parse(myArgs.loadState);
282 if (strlen(myArgs.dumpErrorFile) >0)
283 executor.setStopOnError(true, myArgs.dumpErrorFile);
285 executor.setStopOnError(false, myArgs.dumpErrorFile);
287 std::ofstream f("toto");
291 cerr << "+++++++++++++++++++ start calculation +++++++++++++++++++" << endl;
292 executor.RunW(p,myArgs.display, fromScratch);
293 cerr << "+++++++++++++++++++ end calculation +++++++++++++++++++" << endl;
294 cerr << "Proc state : " << p->getEffectiveState() << endl;
295 timer("Elapsed time after execution: ");
297 if(p->getEffectiveState() != YACS::DONE)
299 std::string report=p->getErrorReport();
300 std::cerr << "Execution has ended in error" << std::endl;
301 std::cerr << report << std::endl;
304 std::ofstream g("titi");
308 bool isFinalDump = (strlen(myArgs.finalDump) != 0);
311 YACS::ENGINE::VisitorSaveState vst(p);
312 vst.openFileDump(myArgs.finalDump);
317 Runtime* r=YACS::ENGINE::getRuntime();
318 Dispatcher* disp=Dispatcher::getDispatcher();
324 catch (YACS::Exception& e)
326 cerr << "Caught a YACS exception" << endl;
327 cerr << e.what() << endl;
328 Runtime* r=YACS::ENGINE::getRuntime();
329 Dispatcher* disp=Dispatcher::getDispatcher();
335 catch (const std::ios_base::failure&)
337 cerr << "Caught an io failure exception" << endl;
340 catch(CORBA::SystemException& ex)
342 cerr << "Caught a CORBA::SystemException." ;
345 CORBA::TypeCode_var tc = tmp.type();
346 const char *p = tc->name();
354 catch(omniORB::fatalException& fe)
356 cerr << "Caught omniORB::fatalException:" << endl;
357 cerr << " file: " << fe.file() << endl;
358 cerr << " line: " << fe.line() << endl;
359 cerr << " mesg: " << fe.errmsg() << endl;
364 cerr << "Caught unknown exception." << endl;