]> SALOME platform Git repositories - modules/yacs.git/blob - src/yacsloader/driver.cxx
Salome HOME
copy tag mergefrom_BR_V0_1_CC_Salome_04oct07
[modules/yacs.git] / src / yacsloader / driver.cxx
1
2 #include "RuntimeSALOME.hxx"
3 #include "Proc.hxx"
4 #include "Exception.hxx"
5 #include "Executor.hxx"
6 #include "parsers.hxx"
7 #include "VisitorSaveState.hxx"
8 #include "VisitorSaveSchema.hxx"
9 #include "LoadState.hxx"
10 #include "Dispatcher.hxx"
11
12 #include <iostream>
13 #include <fstream>
14 #include <argp.h>
15
16 using YACS::YACSLoader;
17 using namespace YACS::ENGINE;
18 using namespace std;
19
20
21 // --- use of glibc argp interface for parsing unix-style arguments
22
23 const char *argp_program_version ="driver V0.1";
24 const char *argp_program_bug_address ="<nepal@nepal.edf.fr>";
25 static char doc[] ="driver -- a SALOME YACS graph executor";
26 static char args_doc[] = "graph.xml";
27
28 static struct argp_option options[] =
29   {
30     {"display",         'd', "level", 0,                   "Display dot files: 0=never to 3=very often"},
31     {"verbose",         'v', 0,       0,                   "Produce verbose output" },
32     {"stop-on-error",   's', 0,       0,                   "Stop on first error" },
33     {"dump-on-error",   'e', "file",  OPTION_ARG_OPTIONAL, "Stop on first error and dump state"},
34     {"dump-final",      'f', "file",  OPTION_ARG_OPTIONAL, "dump final state"},
35     {"load-state",      'l', "file",  0,                   "Load State from a previous partial execution"},
36     {"save-xml-schema", 'x', "file",  OPTION_ARG_OPTIONAL, "dump xml schema"},
37     { 0 }
38   };
39
40 struct arguments
41 {
42   char *args[1];
43   int display;
44   int verbose;
45   int stop;
46   char *dumpErrorFile;
47   char *finalDump;
48   char *xmlSchema;
49   char *loadState;
50 };
51
52 static error_t
53 parse_opt (int key, char *arg, struct argp_state *state)
54 {
55   // Get the input argument from argp_parse, which we
56   // know is a pointer to our arguments structure. 
57   struct arguments *myArgs = (arguments*)state->input;
58   
59   switch (key)
60     {
61     case 'd':
62       myArgs->display = atoi(arg);
63       break;
64     case 'v':
65       myArgs->verbose = 1;
66       break;
67     case 's':
68       myArgs->stop = 1;
69       break;
70     case 'e':
71       myArgs->stop = 1;
72       if (arg)
73         myArgs->dumpErrorFile = arg;
74       else
75         myArgs->dumpErrorFile = "dumpErrorState.xml";
76       break;
77     case 'f':
78       if (arg)
79         myArgs->finalDump = arg;
80       else
81         myArgs->finalDump = "finalDumpState.xml";
82       break;      
83     case 'l':
84       myArgs->loadState = arg;
85       break;
86     case 'x':
87       if (arg)
88         myArgs->xmlSchema = arg;
89       else
90         myArgs->xmlSchema = "saveSchema.xml";
91       break;      
92
93     case ARGP_KEY_ARG:
94       if (state->arg_num >=1) // Too many arguments.
95         argp_usage (state);
96       myArgs->args[state->arg_num] = arg;
97       break;
98       
99     case ARGP_KEY_END:
100       if (state->arg_num < 1) // Not enough arguments.
101         argp_usage (state);
102       break;
103      
104     default:
105       return ARGP_ERR_UNKNOWN;
106     }
107   return 0;
108 }
109
110 // Our argp parser.
111 static struct argp argp = { options, parse_opt, args_doc, doc };
112
113
114 main (int argc, char* argv[])
115 {
116   struct arguments myArgs;
117      
118   // Default values.
119   myArgs.display = 0;
120   myArgs.verbose = 0;
121   myArgs.stop = 0;
122   myArgs.dumpErrorFile= "";
123   myArgs.finalDump = "";
124   myArgs.loadState = "";
125   myArgs.xmlSchema = "";
126
127   // Parse our arguments; every option seen by parse_opt will be reflected in arguments.
128   argp_parse (&argp, argc, argv, 0, 0, &myArgs);
129     cerr << "graph = " << myArgs.args[0] 
130          << " options: display=" << myArgs.display 
131          << " verbose="<<myArgs.verbose
132          << " stop-on-error=" << myArgs.stop;
133   if (myArgs.stop)
134     cerr << " dumpErrorFile=" << myArgs.dumpErrorFile << endl;
135   else
136     cerr << endl;
137
138   RuntimeSALOME::setRuntime();
139
140   YACSLoader loader;
141   Executor executor;
142
143   try
144     {
145       Proc* p=loader.load(myArgs.args[0]);
146
147       bool isXmlSchema = (strlen(myArgs.xmlSchema) != 0);
148       if (isXmlSchema)
149       {
150         YACS::ENGINE::VisitorSaveSchema vss(p);
151         vss.openFileSchema(myArgs.xmlSchema);
152         p->accept(&vss);
153         vss.closeFileSchema();
154       }
155
156       bool fromScratch = (strlen(myArgs.loadState) == 0);
157       if (!fromScratch)
158         {
159           p->init();
160           p->exUpdateState();
161           stateParser* rootParser = new stateParser();
162           stateLoader myStateLoader(rootParser, p);
163           myStateLoader.parse(myArgs.loadState);
164         }
165
166       if (myArgs.stop)
167         if (strlen(myArgs.dumpErrorFile) >0)
168           executor.setStopOnError(true, myArgs.dumpErrorFile);
169         else
170           executor.setStopOnError(false, myArgs.dumpErrorFile);
171
172       std::ofstream f("toto");
173       p->writeDot(f);
174       f.close();
175
176       cerr << "+++++++++++++++++++ start calculation +++++++++++++++++++" << endl;
177       executor.RunW(p,myArgs.display, fromScratch);
178       cerr << "+++++++++++++++++++  end calculation  +++++++++++++++++++" << endl;
179       cerr << "Proc state : " << p->getEffectiveState() << endl;
180
181       std::ofstream g("titi");
182       p->writeDot(g);
183       g.close();
184
185       bool isFinalDump = (strlen(myArgs.finalDump) != 0);
186       if (isFinalDump)
187         {
188           YACS::ENGINE::VisitorSaveState vst(p);
189           vst.openFileDump(myArgs.finalDump);
190           p->accept(&vst);
191           vst.closeFileDump();
192         }
193       delete p;
194       Runtime* r=YACS::ENGINE::getRuntime();
195       Dispatcher* disp=Dispatcher::getDispatcher();
196       r->fini();
197       delete r;
198       delete disp;
199       return 0;
200     }
201   catch (YACS::Exception& e)
202     {
203       cerr << "Caught a YACS exception" << endl;
204       cerr << e.what() << endl;
205       Runtime* r=YACS::ENGINE::getRuntime();
206       Dispatcher* disp=Dispatcher::getDispatcher();
207       r->fini();
208       delete r;
209       delete disp;
210       return 1;
211     }
212   catch (const std::ios_base::failure&)
213     {
214       cerr << "Caught an io failure exception" << endl;
215       return 1;
216     }
217   catch(CORBA::SystemException& ex) 
218     {
219       cerr << "Caught a CORBA::SystemException." ;
220       CORBA::Any tmp;
221       tmp <<= ex;
222       CORBA::TypeCode_var tc = tmp.type();
223       const char *p = tc->name();
224       if ( *p != '\0' ) 
225         cerr <<p;
226       else  
227         cerr  << tc->id();
228       cerr << endl;
229       return 1;
230     }
231   catch(omniORB::fatalException& fe) 
232     {
233       cerr << "Caught omniORB::fatalException:" << endl;
234       cerr << "  file: " << fe.file() << endl;
235       cerr << "  line: " << fe.line() << endl;
236       cerr << "  mesg: " << fe.errmsg() << endl;
237       return 1;
238     }
239   catch(...) 
240     {
241       cerr << "Caught unknown exception." << endl;
242       return 1;
243     }
244 }
245