Line data Source code
1 : /*******************************************************************************
2 : * *
3 : * Cosmos:(C)oncept et (O)utils (S)tatistique pour les (Mo)deles *
4 : * (S)tochastiques *
5 : * *
6 : * Copyright (C) 2009-2012 LSV & LACL *
7 : * Authors: Paolo Ballarini BenoƮt Barbot & Hilal Djafri *
8 : * Website: http://www.lsv.ens-cachan.fr/Software/cosmos *
9 : * *
10 : * This program is free software; you can redistribute it and/or modify *
11 : * it under the terms of the GNU General Public License as published by *
12 : * the Free Software Foundation; either version 3 of the License, or *
13 : * (at your option) any later version. *
14 : * *
15 : * This program is distributed in the hope that it will be useful, *
16 : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 : * GNU General Public License for more details. *
19 : * *
20 : * You should have received a copy of the GNU General Public License along *
21 : * with this program; if not, write to the Free Software Foundation, Inc., *
22 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
23 : * file Lha_gmlparser.cpp *
24 : * Created by Benoit Barbot on 03/11/11. *
25 : *******************************************************************************
26 : */
27 :
28 : #ifdef HAVE_CONFIG_H
29 : #include <config.h>
30 : #endif
31 :
32 : #include <fstream>
33 : #include <iostream>
34 : #include <vector>
35 : #include <map>
36 : #include <string>
37 : #include <algorithm>
38 : #include <stdlib.h>
39 : #include <cstdlib>
40 : #include <regex>
41 :
42 : #include "Lha_gmlparser.hpp"
43 : #include "expatmodelparser.hh"
44 : #include "modelhandler.hh"
45 :
46 : #include "Lha-Reader.hpp"
47 :
48 : //#include "tree.hh"
49 : #include "tree/tree_util.hh"
50 :
51 :
52 : using namespace std;
53 :
54 :
55 : /*
56 : Function sometime usefull for debuging
57 : void print_tree(const tree<string>& tr, tree<string>::pre_order_iterator it, tree<string>::pre_order_iterator end)
58 : {
59 : if(!tr.is_valid(it)) return;
60 : int rootdepth=tr.depth(it);
61 : while(it!=end) {
62 : cout << " ";
63 : for(int i=0; i<tr.depth(it)-rootdepth; ++i)
64 : cout << " ";
65 : cout << (*it) << endl << flush;
66 : ++it;
67 : }
68 : }
69 : */
70 :
71 221 : string MyLhaModelHandler::simplifyString(string str)
72 : {
73 221 : size_t n1 = str.find_first_not_of(" \n");
74 221 : size_t n2 = str.find_last_not_of(" \n");
75 221 : if(n1 ==std::string::npos )return "";
76 221 : return str.substr(n1, n2-n1+1);
77 : }
78 :
79 0 : bool MyLhaModelHandler::is_void(string str)
80 : {
81 : //int n1 = str.find_first_not_of(" \n");
82 : //int n2 = str.find_last_not_of(" \n");
83 0 : return str.compare("\n");
84 : }
85 :
86 68 : void MyLhaModelHandler::appendSimplify(string *st, string str)
87 : {
88 68 : size_t n1 = str.find_first_not_of(" \n");
89 68 : size_t n2 = str.find_last_not_of(" \n");
90 68 : if(n1 !=std::string::npos )st->append(str.substr(n1, n2-n1+1));
91 68 : }
92 :
93 : /*void evalinfix(string *st, tree<string>::pre_order_iterator it , string str){
94 : st->append("(");
95 : for (treeSI it2 = (it.begin()) ; it2 != (it.end()) ; ++it2 ) {
96 : if(it2!= it.begin()) st->append(str);
97 : eval_expr( st, it2);
98 : }
99 : st->append(")");
100 : }*/
101 :
102 428 : void MyLhaModelHandler::eval_expr(bool *is_mark_dep, string *st, tree<string>::pre_order_iterator it )
103 : {
104 :
105 428 : if((P.verbose-3)>2)cout << (*it) << endl;
106 428 : if((*it) == "expr"){
107 119 : eval_expr(is_mark_dep, st, it.begin());
108 309 : }else if((*it) == "boolExpr"){
109 56 : eval_expr(is_mark_dep, st, it.begin());
110 253 : }else if((*it) == "markingExpr"){
111 0 : string stmark;
112 0 : size_t colDomIndex = eval_marking_expr(stmark, it.begin());
113 0 : if (colDomIndex == UNCOLORED_DOMAIN){
114 0 : st->append(stmark);
115 : }else {
116 0 : st->append(stmark);
117 0 : st->append(".card()");
118 : //cerr << "Not uncolored type: " << stmark << endl;
119 : }
120 0 : *is_mark_dep = true;
121 253 : }else if((*it) == "function"){
122 19 : eval_expr(is_mark_dep, st, it.begin());
123 234 : }else if((*it) == "numValue"){
124 36 : if((P.verbose-3)>2)cout << "\t" << it.node->first_child->data<<endl;
125 36 : appendSimplify(st,it.node->first_child->data);
126 198 : }else if ((*it) == "boolValue") {
127 32 : appendSimplify(st,it.node->first_child->data);
128 166 : }else if ((*it) == "name") {
129 182 : string var = simplifyString(it.node->first_child->data);
130 174 : if(MyLHA->LhaIntConstant.count(var)>0 ||
131 97 : MyLHA->LhaRealConstant.count(var)>0){st->append(var);
132 : }else {
133 77 : vector<string>::const_iterator vn = find(MyLHA->Vars.label.begin(), MyLHA->Vars.label.end(), var);
134 77 : if(vn != MyLHA->Vars.label.end()){
135 52 : std::ostringstream s; s<<"Vars->"<< var;
136 26 : st->append(s.str());
137 :
138 51 : } else if(MyLHA->MyGspn->PlacesId.count(var)>0) {
139 34 : *is_mark_dep =true;
140 34 : st->append("Marking.P->_PL_");
141 34 : st->append(var.c_str());
142 34 : if(MyLHA->MyGspn->placeStruct[MyLHA->MyGspn->PlacesId[var]].colorDom != UNCOLORED_DOMAIN )
143 2 : st->append(".card()");
144 34 : st->append(" ");
145 : } else {
146 17 : vector<colorVariable>::const_iterator varit= MyLHA->MyGspn->colVars.begin();
147 27 : for ( ; varit != MyLHA->MyGspn->colVars.end() && varit->name.compare(var)!=0 ; ++varit) ;
148 17 : if (varit != MyLHA->MyGspn->colVars.end()) {
149 17 : st->append("b.P->");
150 17 : st->append(varit->name);
151 17 : st->append(".c0");
152 : }else {
153 0 : throw GrmlInputException("Unknown name:" + var );
154 : }
155 :
156 : }
157 : }
158 208 : }else if ( (*it) == "+" || (*it) == "*"
159 56 : || (*it) == "min" || (*it) == "max"
160 56 : || (*it) == "floor" || (*it) == "-"
161 56 : || (*it) == "/" || (*it) == "power"
162 56 : || (*it) == "or" || (*it) == "and"
163 40 : || (*it) == "not" || (*it) == "equal"
164 23 : || (*it) == "notEqual" || (*it) == "greater"
165 11 : || (*it) == "greaterEqual" || (*it) == "less"
166 81 : || (*it) == "lessEqual" ){
167 :
168 :
169 75 : if ((*it) == "min") st->append("min");
170 75 : if ((*it) == "max") st->append("max");
171 75 : if ((*it) == "floor" ) st->append("floor");
172 :
173 75 : st->append("(");
174 225 : for (treeSI it2 = (it.begin()) ; it2 != (it.end()) ; ++it2 ) {
175 150 : if(it2!= it.begin()) {
176 75 : if ((*it) == "+" ) { st->append("+"); }
177 58 : else if ((*it) == "*" ) { st->append("*"); }
178 56 : else if ((*it) == "-" ) { st->append("-"); }
179 56 : else if ((*it) == "/") { st->append("/"); }
180 56 : else if ((*it) == "power") { st->append("^"); }
181 56 : else if ((*it) == "or") { st->append(" || "); }
182 53 : else if ((*it) == "and") { st->append(" && "); }
183 40 : else if ((*it) == "not") { st->append(" !"); }
184 40 : else if ((*it) == "notEqual") { st->append(" !="); }
185 33 : else if ((*it) == "equal") { st->append("=="); }
186 16 : else if ((*it) == "greater") { st->append(">"); }
187 11 : else if ((*it) == "greaterEqual") { st->append(">="); }
188 10 : else if ((*it) == "less") { st->append("<"); }
189 6 : else if ((*it) == "lessEqual") { st->append("<="); }
190 0 : else st->append(",");
191 : }
192 150 : eval_expr(is_mark_dep, st, it2);
193 : }
194 75 : st->append(")");
195 : ;
196 0 : } else throw GrmlInputException("Unexpected:" + *it);
197 428 : }
198 :
199 0 : size_t MyLhaModelHandler::eval_marking_expr(string &st, tree<string>::pre_order_iterator it){
200 0 : if((P.verbose-3)>2)cout << "eval_marking_expr:"<< (*it) << endl;
201 0 : if(*it == "markingExpr")
202 0 : return eval_marking_expr(st, it.begin());
203 0 : else if(*it == "name"){
204 0 : string stpl = simplifyString(*(it.begin()));
205 0 : if(MyLHA->MyGspn->PlacesId.count(stpl)>0){
206 0 : st.append("Marking.P->_PL_");
207 0 : st.append(stpl);
208 0 : return MyLHA->MyGspn->placeStruct[MyLHA->MyGspn->PlacesId[stpl]].colorDom;
209 : } else {
210 0 : st.append(stpl);
211 0 : return UNCOLORED_DOMAIN;
212 : }
213 0 : } else if(*it == "selectColor"){
214 0 : st.append(" ( ");
215 0 : size_t colClassCounter =0;
216 0 : size_t colDomIndex = (size_t)-1;
217 0 : for (treeSI it2 = (it.begin()) ; it2 != (it.end()) ; ++it2 ) {
218 0 : if((P.verbose-3)>2)cout << (*it2) << endl;
219 0 : if(*it2 == "markingExpr") colDomIndex = eval_marking_expr(st, it2);
220 0 : else if(*it2 == "name"){
221 0 : string cname = simplifyString(*(it2.begin()));
222 0 : vector<color>::const_iterator itcc;
223 0 : colorClass cc = MyLHA->MyGspn->colClasses[MyLHA->MyGspn->colDoms[colDomIndex].colorClassIndex[colClassCounter]];
224 0 : for( itcc= cc.colors.begin(); itcc != cc.colors.end() && cname != itcc->name; ++itcc) ;
225 0 : if (itcc == cc.colors.end()) {
226 0 : cerr << "unknwon color:" << cname << "for color classe" << cc.name<< endl;
227 : }else{
228 0 : st.append("[ Color_" + cc.name + "_" + itcc->name +" ]");
229 : }
230 0 : colClassCounter++;
231 : }
232 : }
233 0 : st.append(" ) ");
234 0 : return colDomIndex;
235 : }
236 0 : throw GrmlInputException("Unexpected: "+ *it);
237 : }
238 :
239 :
240 1 : void MyLhaModelHandler::eval_term(vector<string> &CoeffsVector, tree<string>::pre_order_iterator it){
241 1 : if((P.verbose-3)>2)cout << (*it) << endl;
242 1 : if((*it) == "expr"){
243 0 : eval_term(CoeffsVector, it.begin());
244 1 : }else if((*it) == "name"){
245 2 : string var = simplifyString(*(it.begin()));
246 1 : if((P.verbose-3)>2)cout << "\t" << var << endl;
247 1 : vector<string>::const_iterator vi=find(MyLHA->Vars.label.begin(), MyLHA->Vars.label.end(), var);
248 1 : if(vi!= MyLHA->Vars.label.end())CoeffsVector[vi-MyLHA->Vars.label.begin()]= "1";
249 0 : else cout << "Unkown Variable " << var <<endl;
250 0 : }else cout << "fail eval tree : linexp" << endl;
251 :
252 1 : }
253 :
254 2 : void MyLhaModelHandler::eval_linexpr(vector<string> &CoeffsVector, tree<string>::pre_order_iterator it){
255 2 : if((P.verbose-3)>2)cout << (*it) << endl;
256 2 : if((*it) == "expr"){
257 1 : eval_linexpr(CoeffsVector, it.begin());
258 1 : }else if((*it) == "function" && (((*it.begin()) == "+") || ((*it.begin()) == "-"))){
259 0 : for (treeSI it2 = (it.begin()) ; it2 != (it.end()) ; ++it2 ) {
260 0 : eval_linexpr(CoeffsVector, it2);
261 : }
262 1 : }else eval_term(CoeffsVector, it);
263 :
264 2 : }
265 :
266 1 : void MyLhaModelHandler::eval_guard(vector<vector<string> > &CoeffsMatrix,vector<string> &CST, vector<string> &comp,tree<string>::pre_order_iterator it)
267 : {
268 1 : if((P.verbose-3)>2)cout << (*it) << endl;
269 1 : if((*it) == "boolExpr"){
270 0 : eval_guard(CoeffsMatrix,CST,comp,it.begin());
271 1 : }else if ((*it) == "boolValue") {
272 : //cout << "\tguard = true" << it.node->first_child->data << endl;
273 1 : }else if ((*it) == "and"){
274 : //cout << "(";
275 0 : for (treeSI it2 = (it.begin()) ; it2 != (it.end()) ; ++it2 ) {
276 0 : eval_guard(CoeffsMatrix,CST,comp,it2);
277 : }
278 : //cout << ")";
279 2 : }else if((*it) == "equal"
280 0 : || (*it) == "greaterEqual"
281 1 : || (*it) == "lessEqual" ){
282 1 : if((*it) == "equal")comp.push_back("==");
283 1 : if((*it) == "lessEqual")comp.push_back("<=");
284 1 : if((*it) == "greaterEqual")comp.push_back(">=");
285 :
286 2 : vector<string> CoeffsVector(MyLHA->NbVar,"");
287 1 : eval_linexpr(CoeffsVector,it.begin());
288 : //cout << ";";
289 1 : CoeffsMatrix.push_back(CoeffsVector);
290 1 : string* st= new string("");
291 1 : bool markdep = false;
292 1 : eval_expr(&markdep, st, it.begin().node->next_sibling);
293 1 : if((P.verbose-3)>1)cout<< "CST: " << *st << endl;
294 1 : CST.push_back(*st);
295 :
296 0 : } else cout << "failevaltree: guard" <<endl;
297 1 : }
298 :
299 :
300 4 : int MyLhaModelHandler::eval_str (string s){
301 8 : string val = simplifyString(s);
302 4 : int intval = atoi( val.c_str() );
303 8 : return intval;
304 : }
305 :
306 8 : int MyLhaModelHandler::eval_intFormula( map<std::string,int> intconst, tree<string>::pre_order_iterator it )
307 : {
308 8 : if((P.verbose-3)>2)cout << "intformula: " <<(*it) << endl;
309 8 : if((*it) == "expr"){
310 4 : return eval_intFormula(intconst,it.begin());
311 4 : }else if((*it) == "numValue"){
312 4 : return eval_str(it.node->first_child->data);
313 0 : }else if ((*it) == "name") {
314 0 : string val = simplifyString(it.node->first_child->data);
315 0 : int intval = intconst[(val).c_str()];
316 0 : return intval;
317 0 : }else if ((*it) == "+" || (*it) == "*"
318 0 : || (*it) == "min" || (*it) == "max"
319 0 : || (*it) == "ipower"|| (*it) == "-") {
320 :
321 0 : int v1=0;
322 0 : int v2=0;
323 0 : for (treeSI it2 = (it.begin()) ; it2 != (it.end()) ; ++it2 ) {
324 0 : if(it2!= it.begin()) { v1 = eval_intFormula(intconst, it2);
325 : } else {
326 0 : v2 = eval_intFormula(intconst, it2);
327 : }
328 :
329 : }
330 :
331 0 : if ((*it) == "+") { return v1+v2; }
332 0 : else if ((*it) == "*") { return v1*v2; }
333 0 : else if ((*it) == "-") { return v1-v2; }
334 0 : else if ((*it) == "min") { return min(v1,v2); }
335 0 : else if ((*it) == "max") { return max(v1,v2); }
336 0 : else if ((*it) == "power") { return v1^v2; }
337 0 : else throw GrmlInputException("Unexpected: "+ *it);
338 :
339 0 : }else throw GrmlInputException("Unexpected: "+ *it);
340 : }
341 :
342 :
343 :
344 6 : MyLhaModelHandler::MyLhaModelHandler(LhaType *MyLHA2,parameters &Q):MyLHA(MyLHA2), P(Q) {
345 : //Initialisation
346 6 : MyLHA->NbVar = 0;
347 6 : countLoc=0;
348 6 : ParseLoc=true;
349 : //ParseDecl=true;
350 6 : }
351 : //~MyModelHandler() { }
352 :
353 :
354 6 : void MyLhaModelHandler::on_read_model(const XmlString& formalismUrl) {
355 : // output used formalism
356 6 : ParseLoc=true;
357 : //ParseDecl=true;
358 6 : if((P.verbose-3)>0)cout << "read model : formalism = " << formalismUrl << endl;
359 6 : }
360 :
361 6 : HaslFormulasTop* MyLhaModelHandler::exportHASLTop(tree<string>::pre_order_iterator it){
362 :
363 6 : if((P.verbose-3)>2)cout << *it << ":" << endl;
364 6 : if((*it) == "AVG"){
365 12 : string lhafunc = exportHASL(it.begin());
366 6 : if((P.verbose-3)>1)cout << lhafunc << endl;
367 12 : stringstream ss;
368 6 : ss << "LhaFunc[" << MyLHA->LhaFunction[lhafunc] << "]";
369 6 : MyLHA->Algebraic.push_back( ss.str() );
370 6 : return (new HaslFormulasTop(MyLHA->Algebraic.size()-1));
371 0 : } else if((*it) == "PROB"){
372 0 : return (new HaslFormulasTop(PROBABILITY));
373 0 : } else if(*it == "PDF" || *it == "CDF"){
374 0 : string lhafunc;
375 0 : double deltab = 1;
376 0 : double minb =0;
377 0 : double maxb =0;
378 0 : for(treeSI it2 = it.begin(); it2 != it.end(); it2++){
379 0 : if (*it2 == "min")minb = atof(exportHASL(it2).c_str());
380 0 : else if (*it2 == "max")maxb = atof(exportHASL(it2).c_str());
381 0 : else if (*it2 == "delta")deltab = atof(exportHASL(it2).c_str());
382 0 : else lhafunc = exportHASL(it2);
383 : }
384 0 : if(lhafunc.empty()){
385 0 : throw GrmlInputException("Ill formed HASL formula");
386 : }
387 :
388 0 : if((P.verbose-3)>1)cout << lhafunc << endl;
389 0 : stringstream ss;
390 0 : ss << "LhaFunc[" << MyLHA->LhaFunction[lhafunc] << "]";
391 :
392 :
393 0 : for(double bucket = minb ; bucket < maxb ; bucket+= deltab){
394 0 : std::ostringstream algPDF;
395 0 : if(*it == "PDF")
396 0 : algPDF << "(("<< ss.str() <<" >= "<<bucket<<"&& "<<ss.str() <<"<"<<bucket+deltab<<") ? 1:0)";
397 : else
398 0 : algPDF << "(("<< ss.str() <<" <= "<<bucket<<") ? 1:0)";
399 :
400 0 : MyLHA->Algebraic.push_back(algPDF.str());
401 0 : MyLHA->HASLtop.push_back(new HaslFormulasTop((size_t)MyLHA->Algebraic.size()-1));
402 0 : MyLHA->HASLtop.back()->TypeOp = PDF_PART;
403 0 : std::ostringstream s; s<<"$_$: Value in ["<< bucket<< " , "<<bucket+deltab<<"]";
404 0 : MyLHA->HASLname.push_back(s.str());
405 : }
406 :
407 0 : return (NULL);
408 0 : } else throw GrmlInputException("Unexpected: "+ *it);
409 : }
410 :
411 :
412 24 : string MyLhaModelHandler::exportHASL(tree<string>::pre_order_iterator it){
413 24 : if((P.verbose-3)>2)cout << *it << ":" << endl;
414 24 : if(*it == "YHF"){
415 6 : return exportHASL(it.begin());
416 18 : }else if(*it == "delta"){
417 0 : return simplifyString(*(it.begin()));
418 18 : }else if(*it == "min"){
419 0 : return simplifyString(*(it.begin()));
420 18 : }else if(*it == "max"){
421 0 : return simplifyString(*(it.begin()));
422 18 : }else if(*it == "last"){
423 : //cout << *(it.begin()) << endl;
424 12 : string linForm = exportHASL(it.begin());
425 6 : const char* linformc = linForm.c_str();
426 6 : if(MyLHA->LinearForm.find(linformc)==MyLHA->LinearForm.end()){
427 6 : size_t i=MyLHA->LinearForm.size();
428 6 : MyLHA->LinearForm[linformc]=i;
429 : }
430 :
431 6 : MyLHA->LhaFuncArg.push_back(MyLHA->LinearForm[linformc]);
432 6 : MyLHA->LhaFuncType.push_back("Last");
433 12 : string ss="Last("; ss.append(linformc); ss.append(")");
434 6 : if(MyLHA->LhaFunction.find(ss)==MyLHA->LhaFunction.end()){
435 6 : size_t i=MyLHA->LhaFunction.size();
436 6 : MyLHA->LhaFunction[ss]=i;
437 : }
438 6 : return ss;
439 :
440 12 : }else if((*it) == "expr"){
441 6 : return exportHASL(it.begin());
442 6 : }else if((*it) == "name"){
443 12 : string var = simplifyString(it.node->first_child->data);
444 12 : stringstream ss;
445 6 : ss << "Vars->" << var;
446 :
447 6 : return ss.str();
448 :
449 0 : } else throw GrmlInputException("Unexpected: "+ *it);
450 :
451 : }
452 :
453 88 : treeSI MyLhaModelHandler::findbranchlha(treeSI t, string branch){
454 88 : if( branch == "")return t;
455 60 : size_t nextnode = branch.find_first_of("/");
456 102 : for (treeSI it = (t.begin()) ; it != (t.end()) ; ++it) {
457 100 : if((*it).compare(branch.substr(0,nextnode))==0){
458 58 : return findbranchlha(it, branch.substr(nextnode+1,branch.length()-nextnode-1));
459 : }
460 : }
461 2 : cerr << "Fail to find branch:" << branch << "in the file\n";
462 2 : return t.end();
463 : }
464 :
465 12 : void MyLhaModelHandler::on_read_model_attribute(const Attribute& attribute) {
466 : // read model attribute
467 24 : for(treeSI it = attribute.begin(); it != attribute.end(); ++it) {
468 12 : if((P.verbose-3)>1)cout << *it << ":" << endl;
469 12 : if((*it) == "declaration"){
470 : //cout<< "start int const" << endl;
471 6 : treeSI t1 = findbranchlha(it, "constants/intConsts/");
472 : //cout << "find branch" << endl;
473 6 : if (t1 != it.end())
474 12 : for (treeSI it2 = (t1.begin()) ; it2 != (t1.end()) ; ++it2 ) {
475 6 : if ((*it2) == "intConst") {
476 : //cout << "start intConst" << endl;
477 8 : string constname = simplifyString((find(it2.begin(),it2.end(),"name")).node->first_child->data);
478 : //cout << "finish name:"<< *constname << endl;
479 : //treeSI test = find(it2.begin(),it2.end(),"expr");
480 : //cout << "find expr" << endl;
481 4 : int constvalue = eval_intFormula(MyLHA->LhaIntConstant,find(it2.begin(),it2.end(),"expr"));
482 : //cout << "finish expr" << endl;
483 : //Evaluate_gml.parse(constvalue);
484 4 : if((P.verbose-3)>1)cout << "\tconst int " << constname << "=" << constvalue << endl;
485 4 : if (P.constants.count(constname)>0) {
486 0 : istringstream(P.constants[constname]) >> constvalue;
487 0 : if((P.verbose-3)>0)cout << "const int " << constname << " overwrite to " << constvalue << endl;;
488 : }
489 4 : MyLHA->LhaIntConstant[constname]=constvalue; //Evaluate_gml.IntResult;
490 4 : MyLHA->LhaRealConstant[constname]= constvalue; //Evaluate_gml.RealResult;
491 :
492 : }
493 : }
494 : //cout << "finished intconst" << endl;
495 6 : t1 = findbranchlha(it, "constants/realConsts/");
496 6 : if (t1 != it.end())
497 14 : for (treeSI it2 = (t1.begin()) ; it2 != (t1.end()) ; ++it2 ) {
498 : //cout << "test:" << *it2 << ":"<< endl;
499 8 : if ((*it2) == "realConst") {
500 6 : string constname = simplifyString((find(it2.begin(),it2.end(),"name")).node->first_child->data);
501 :
502 3 : bool ismarkdep=false;
503 6 : string constvalue;
504 3 : eval_expr( &ismarkdep, &constvalue, find(it2.begin(),it2.end(),"expr").begin());
505 3 : if(ismarkdep){
506 0 : throw GrmlInputException("Constant should not be marking dependant ");
507 : }
508 3 : if (P.constants.count(constname)>0) {
509 0 : constvalue = P.constants[constname];
510 0 : if((P.verbose-3)>0)cout << "const double " << constname << " overwrite to " << constvalue << endl;;
511 : }
512 3 : Evaluate_gml.parse(constvalue);
513 3 : if((P.verbose-3)>1)cout << "\tconst double " << constname << "=" << Evaluate_gml.RealResult << endl;
514 :
515 3 : MyLHA->LhaRealConstant[constname]=Evaluate_gml.RealResult;
516 :
517 : }
518 : }
519 : //cout << "finished realconst" << endl;
520 6 : t1 = findbranchlha(it, "variables/reals/");
521 6 : if (t1 != it.end())
522 18 : for (treeSI it2 = (t1.begin()) ; it2 != (t1.end()) ; ++it2 ) {
523 12 : if(*it2 == "real") {
524 24 : string constname = simplifyString((find(it2.begin(),it2.end(),"name")).node->first_child->data);
525 12 : MyLHA->Vars.label.push_back(constname);
526 12 : MyLHA->Vars.initialValue.push_back(0.0);
527 12 : treeSI dom = find(it2.begin(),it2.end(),"domain");
528 12 : if (dom != it2.end()) {
529 0 : string domname = simplifyString(*(dom.begin()));
530 0 : vector<colorDomain>::const_iterator domit;
531 0 : for(domit = MyLHA->MyGspn->colDoms.begin(); domit != MyLHA->MyGspn->colDoms.end() && domit->name != domname; ++domit) ;
532 0 : if(domit != MyLHA->MyGspn->colDoms.end())MyLHA->Vars.colorDomain.push_back(domit - MyLHA->MyGspn->colDoms.begin());
533 0 : else cerr << "Unknown color Domain " << domname << endl;
534 12 : }else MyLHA->Vars.colorDomain.push_back(UNCOLORED_DOMAIN);
535 12 : MyLHA->Vars.type.push_back(CONTINIOUS_VARIABLE);
536 12 : MyLHA->Vars.isTraced.push_back(true);
537 12 : MyLHA->NbVar++;
538 :
539 12 : if((P.verbose-3)>1)cout << "\tcontinuous var " << constname << " index: " << MyLHA->NbVar-1 << " domain: " << MyLHA->Vars.colorDomain[MyLHA->NbVar-1] <<endl;
540 0 : } else if(*it2 != "")cout << "Unknown variable Type" << *it2 << endl;
541 : }
542 : //cout << "finished realvar" << endl;
543 6 : t1 = findbranchlha(it, "variables/discretes/");
544 6 : if (t1 != it.end())
545 12 : for (treeSI it2 = (t1.begin()) ; it2 != (t1.end()) ; ++it2 ) {
546 6 : if ((*it2) == "discrete") {
547 0 : string constname = simplifyString((find(it2.begin(),it2.end(),"name")).node->first_child->data);
548 0 : MyLHA->Vars.label.push_back(constname);
549 0 : MyLHA->Vars.initialValue.push_back(0.0);
550 0 : treeSI dom = find(it2.begin(),it2.end(),"domain");
551 0 : if (dom != it2.end()) {
552 0 : string domname = simplifyString(*(dom.begin()));
553 0 : vector<colorDomain>::const_iterator domit;
554 0 : for(domit = MyLHA->MyGspn->colDoms.begin(); domit != MyLHA->MyGspn->colDoms.end() && domit->name != domname; ++domit) ;
555 0 : if(domit != MyLHA->MyGspn->colDoms.end())MyLHA->Vars.colorDomain.push_back(domit - MyLHA->MyGspn->colDoms.begin());
556 0 : else cerr << "Unknown color Domain " << domname << endl;
557 0 : }else MyLHA->Vars.colorDomain.push_back(UNCOLORED_DOMAIN);
558 :
559 0 : MyLHA->Vars.type.push_back(DISCRETE_VARIABLE);
560 0 : MyLHA->Vars.isTraced.push_back(true);
561 0 : MyLHA->NbVar++;
562 0 : if((P.verbose-3)>1)cout << "\tdiscrete var " << constname << " index: " << MyLHA->NbVar-1 << " domain: " << MyLHA->Vars.colorDomain[MyLHA->NbVar-1] <<endl;
563 : }
564 : }
565 6 : t1 = findbranchlha(it, "variables/colors/");
566 6 : if (t1 != it.end())
567 9 : for (treeSI it2 = (t1.begin()) ; it2 != (t1.end()) ; ++it2 ) {
568 5 : if ((*it2) == "color") {
569 4 : string constname = simplifyString((find(it2.begin(),it2.end(),"name")).node->first_child->data);
570 2 : MyLHA->Vars.label.push_back(constname);
571 2 : MyLHA->Vars.initialValue.push_back(0.0);
572 2 : treeSI dom = find(it2.begin(),it2.end(),"domain");
573 2 : if (dom != it2.end()) {
574 4 : string domname = simplifyString(*(dom.begin()));
575 2 : vector<colorClass>::const_iterator domit;
576 2 : for(domit = MyLHA->MyGspn->colClasses.begin(); domit != MyLHA->MyGspn->colClasses.end() && domit->name != domname; ++domit) ;
577 2 : if(domit != MyLHA->MyGspn->colClasses.end())MyLHA->Vars.colorDomain.push_back(domit - MyLHA->MyGspn->colClasses.begin());
578 0 : else cerr << "Unknown color Domain " << domname << endl;
579 0 : }else cerr << "No color class specify for color variable " << constname << endl;
580 :
581 2 : MyLHA->Vars.type.push_back(COLOR_VARIABLE);
582 2 : MyLHA->NbVar++;
583 2 : MyLHA->Vars.isTraced.push_back(true);
584 2 : if((P.verbose-3)>1)cout << "\tcolor var " << constname << " index: " << MyLHA->NbVar-1<< " domain: " << MyLHA->Vars.colorDomain[MyLHA->NbVar-1] << endl;
585 : }
586 : }
587 : //cout << "finished discrete var" << endl;
588 6 : } else if((*it) == "HASLFormula"){
589 : //cout << "export hasl formula" << endl;
590 6 : HaslFormulasTop* haslform = exportHASLTop(it.begin());
591 6 : if(haslform != NULL){
592 6 : MyLHA->HASLtop.push_back(haslform);
593 6 : MyLHA->HASLname.push_back("HASLFormula");
594 : }
595 0 : } else throw GrmlInputException("Unexpected: "+ *it);
596 : }
597 : //print_tree(attribute, attribute.begin(), attribute.end());
598 12 : }
599 :
600 24 : void MyLhaModelHandler::on_read_node(const XmlString& id,
601 : const XmlString& nodeType,
602 : const AttributeMap& attributes,
603 : const XmlStringList&) {
604 :
605 24 : if((P.verbose-3)>1)cout << "read node : " << id << ", " << nodeType << endl;
606 24 : if(nodeType == "state"){
607 : //cout << "location:"<<endl;
608 24 : flush(cout);
609 24 : int id2 = atoi(id.c_str());
610 24 : Gml2Loc[id2]=countLoc;
611 :
612 24 : bool markdep=false;
613 :
614 48 : string Plname = simplifyString(*(attributes.find("name")->second.begin().begin()));
615 24 : if((P.verbose-3)>1)cout << "name: " << Plname << endl;
616 24 : MyLHA->LocLabel.push_back(Plname);
617 24 : MyLHA->LocIndex[Plname]=countLoc;
618 :
619 24 : string* inv = new string("");
620 24 : eval_expr(&markdep, inv, attributes.find("invariant")->second.begin().begin());
621 24 : if((P.verbose-3)>1)cout << "invariant: " << *inv << endl;
622 24 : MyLHA->FuncLocProperty.push_back(*inv);
623 :
624 24 : treeSI itflow = attributes.find("flows")->second.begin();
625 48 : vector<string> v1(MyLHA->NbVar,"");
626 49 : for(treeSI it2 = itflow.begin(); it2!=itflow.end();++it2){
627 25 : if((*it2) == "flow"){
628 :
629 18 : string var = simplifyString((find(it2.begin(),it2.end(),"name")).node->first_child->data);
630 18 : string varflow;
631 :
632 9 : eval_expr( &markdep, &varflow, find(it2.begin(),it2.end(),"expr").begin());
633 :
634 9 : size_t vi = find(MyLHA->Vars.label.begin(), MyLHA->Vars.label.end(), var) - MyLHA->Vars.label.begin();
635 9 : if((P.verbose-3)>1)cout << "\tvar: " << var << " index: " << vi << " flow: " << varflow << endl;
636 9 : if(MyLHA->Vars.type[vi] == CONTINIOUS_VARIABLE){
637 9 : v1[vi]= varflow;
638 0 : } else cout << "Variable "<< var << " is continious, it as no flow"<< endl;
639 : }
640 : }
641 24 : MyLHA->StrFlow.push_back(v1);
642 24 : MyLHA->FuncFlow.push_back(v1);
643 :
644 48 : string type = simplifyString(*(attributes.find("type")->second.begin().begin()));
645 24 : if((P.verbose-3)>1)cout << "\ttype:" << type << endl;
646 24 : if ((type) == "Initial") {
647 6 : MyLHA->InitLoc.insert(countLoc);
648 18 : } else if ((type) == "Final") {
649 8 : MyLHA->FinalLoc.insert(countLoc);
650 : }
651 :
652 24 : countLoc++ ;
653 :
654 : }
655 :
656 :
657 : /*for(AttributeMap::const_iterator it = attributes.begin(); it != attributes.end(); ++it) {
658 : print_tree(it->second, it->second.begin(), it->second.end());
659 : }
660 : for(XmlStringList::const_iterator it = references.begin(); it != references.end(); ++it) {
661 : cout << " ref => " << *it << endl;
662 : }*/
663 24 : }
664 :
665 33 : void MyLhaModelHandler::on_read_arc(const XmlString& id,
666 : const XmlString& arcType,
667 : const XmlString& source,
668 : const XmlString& target,
669 : const AttributeMap& attributes,
670 : const XmlStringList&) {
671 33 : if(ParseLoc){
672 6 : ParseLoc=false;
673 6 : MyLHA->NbLoc= countLoc;
674 :
675 12 : vector<string> v1(MyLHA->NbVar,"");
676 :
677 12 : vector< vector<string> > vv(MyLHA->NbLoc,v1);
678 :
679 6 : CoeffsVector=v1;
680 12 : vector<string> v2(MyLHA->NbLoc,"");
681 : //int sz=MyLHA->TransitionIndex.size();
682 12 : set <string> Pt;
683 6 : PetriTransitions=Pt;
684 29 : for(map<string, int>::iterator it=MyLHA->TransitionIndex.begin();it!=MyLHA->TransitionIndex.end();it++)
685 23 : PetriTransitions.insert((*it).first);
686 :
687 6 : MyLHA->Out_S_Edges=vector < set<int> >(MyLHA->NbLoc);
688 6 : MyLHA->Out_A_Edges=vector < set<int> >(MyLHA->NbLoc);
689 :
690 :
691 : }
692 :
693 33 : if((P.verbose-3)>1)cout << "read arc : " << id << ", " << arcType << ", " << source << " -> " << target << endl;
694 :
695 33 : int sourceGML = atoi(source.c_str());
696 33 : int targetGML = atoi(target.c_str());
697 :
698 : LhaEdge edge;
699 33 : edge.Index=MyLHA->Edge.size();
700 33 : edge.Source= Gml2Loc[sourceGML];
701 33 : edge.Target= Gml2Loc[targetGML];
702 33 : MyLHA->Edge.push_back(edge);
703 :
704 66 : set <string> SubSet;
705 33 : AttributeMap::const_iterator attrf = attributes.find("label");
706 33 : if (attrf != attributes.end()) {
707 20 : tree<string> itaction = attrf->second.begin().begin();
708 20 : string actionstr = simplifyString(*(itaction.begin()));
709 10 : if((actionstr) == "ALL"){
710 10 : SubSet= PetriTransitions;
711 : }else{
712 0 : for(treeSI it2 = itaction.begin(); it2!=itaction.end();++it2){
713 0 : if ((*it2) == "actionName") {
714 0 : string actionstr2 = simplifyString(*(it2.begin()));
715 0 : if((P.verbose-3)>0)cout << "Action: " << actionstr2 << endl;
716 0 : regex actionreg(".*"+actionstr2+".*");
717 0 : size_t countmatch = 0;
718 0 : for(const auto &acs : PetriTransitions)
719 0 : if(regex_match(acs, actionreg)){
720 0 : countmatch++;
721 0 : SubSet.insert(acs);
722 0 : if((P.verbose-3)>0)cout << "action:" << acs << " matches " << actionstr2 << endl;
723 : }
724 0 : if(countmatch==0){
725 0 : cerr << "No match for action name: "<< actionstr2 <<endl;
726 : }
727 : }
728 : }
729 : }
730 : }
731 33 : attrf = attributes.find("action");
732 33 : if (attrf != attributes.end()) {
733 10 : treeSI itaction = attrf->second.begin();
734 20 : for(treeSI it2 = itaction.begin(); it2!=itaction.end();++it2){
735 10 : if ((*it2) == "actionName") {
736 18 : string actionstr2 = simplifyString(*(it2.begin()));
737 9 : if((P.verbose-3)>0)cout << "Action: " << actionstr2 << endl;
738 18 : regex actionreg(".*"+actionstr2+".*");
739 9 : size_t countmatch = 0;
740 81 : for(const auto &acs : PetriTransitions)
741 72 : if(regex_match(acs, actionreg)){
742 9 : countmatch++;
743 9 : SubSet.insert(acs);
744 9 : if((P.verbose-3)>0)cout << "action " << acs << " matches " << actionstr2 << endl;
745 : }
746 9 : if(countmatch==0){
747 0 : cerr << "No match for action name: "<< actionstr2 <<endl;
748 : }
749 : }
750 : }
751 : }
752 33 : attrf = attributes.find("allExcept");
753 33 : if (attrf != attributes.end()) {
754 13 : treeSI itaction = attrf->second.begin();
755 13 : SubSet = MyLHA->MyGspn->TransList;
756 26 : for(treeSI it2 = itaction.begin(); it2!=itaction.end();++it2){
757 13 : if (*it2 == "actionName") {
758 10 : string actionstr2 = simplifyString(*(it2.begin()));
759 5 : if((P.verbose-3)>0)cout << "All except: " << actionstr2 << endl;
760 10 : regex actionreg(".*"+actionstr2+".*");
761 5 : size_t countmatch = 0;
762 45 : for(const auto &acs : PetriTransitions)
763 40 : if(regex_match(acs, actionreg)){
764 5 : countmatch++;
765 5 : SubSet.erase(acs);
766 5 : if((P.verbose-3)>0)cout << "action " << acs << " matches " << actionstr2 << endl;
767 : }
768 5 : if(countmatch==0){
769 0 : cerr << "No match for action name: "<< actionstr2 <<endl;
770 : }
771 : }
772 : }
773 : }
774 :
775 :
776 :
777 33 : MyLHA->EdgeActions.push_back(SubSet);
778 33 : if(SubSet.size()>0) MyLHA->Out_S_Edges[edge.Source].insert(edge.Index);
779 1 : else MyLHA->Out_A_Edges[edge.Source].insert(edge.Index);
780 :
781 33 : bool markdep=false;
782 :
783 33 : treeSI itflow = attributes.find("updates")->second.begin();
784 66 : vector<string> v1(MyLHA->NbVar,"");
785 33 : if (*(itflow.begin()) == "update") {
786 28 : for(treeSI it2 = itflow.begin(); it2!=itflow.end();++it2){
787 30 : string var;
788 30 : string varflow;
789 : //cout << "var update:" << endl;
790 45 : for(treeSI it3 = it2.begin(); it3!=it2.end();++it3){
791 30 : if((*it3) == "name")var = simplifyString(*(it3.begin()));
792 30 : if((*it3) == "expr")eval_expr(&markdep, &varflow, it3.begin() );
793 : }
794 15 : size_t vi = find(MyLHA->Vars.label.begin(), MyLHA->Vars.label.end(), var) - MyLHA->Vars.label.begin();
795 15 : v1[vi]= varflow;
796 15 : if((P.verbose-3)>1 && !var.empty())
797 0 : cout << "\tvar: " << var << " index: " << vi << " update: " << varflow << endl;
798 :
799 : }
800 : }
801 33 : MyLHA->FuncEdgeUpdates.push_back(v1);
802 :
803 33 : treeSI itguard = attributes.find("guard")->second.begin();
804 33 : if((P.verbose-3)>2)cout << "guard:" << endl;
805 66 : vector<vector<string> > CoeffsMatrix;
806 66 : vector<string> CST;
807 66 : vector<string> comp;
808 33 : if(SubSet.size()==0){
809 1 : eval_guard(CoeffsMatrix,CST,comp,itguard.begin().begin());
810 2 : stringstream untime;
811 1 : untime << "true";
812 2 : for (size_t i=0; i< CoeffsMatrix.size(); i++) {
813 1 : size_t k=0;
814 6 : for (size_t j =0 ; j<CoeffsMatrix[0].size(); j++)
815 5 : if(CoeffsMatrix[i][j] !="" && MyLHA->Vars.type[j] != CONTINIOUS_VARIABLE)k++;
816 1 : if(k>0){
817 0 : untime << "&& (";
818 0 : for (size_t j =0 ; j<CoeffsMatrix[0].size(); j++) {
819 0 : if(CoeffsMatrix[i][j]!= "" && MyLHA->Vars.type[j] != CONTINIOUS_VARIABLE)
820 0 : untime << "+" << CoeffsMatrix[i][j]<< "* Vars->"<<MyLHA->Vars.label[j] <<" " ;
821 : }
822 0 : untime << comp[i] << CST[i]<<")";
823 : }
824 : }
825 1 : if((P.verbose-3)>1){
826 0 : cout << "guard:" << untime.str() << endl;
827 : }
828 1 : MyLHA->unTimeEdgeConstraints.push_back(untime.str());
829 : }else{
830 64 : string guard;
831 : bool markdep;
832 32 : eval_expr(&markdep, &guard, itguard.begin().begin());
833 32 : MyLHA->unTimeEdgeConstraints.push_back(guard);
834 : }
835 33 : MyLHA->ConstraintsCoeffs.push_back(CoeffsMatrix);
836 33 : MyLHA->ConstraintsConstants.push_back(CST);
837 33 : MyLHA->ConstraintsRelOp.push_back(comp);
838 :
839 :
840 138 : }
|