Issue a custom error message when Lemon is not found.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #include <new_map_win.h>
21 #include <mapstorage.h>
23 bool NewMapWin::closeIfEscapeIsPressed(GdkEventKey* e)
25 if(e->keyval==GDK_Escape)
32 NewMapWin::NewMapWin(const std::string& title, NoteBookTab & mw, bool itisarc, bool arcnode, MapType type):Gtk::Dialog(title, true, true),mytab(mw),node("Create NodeMap"),arc("Create ArcMap"),map_type(type)
34 set_default_size(200, 50);
36 signal_key_press_event().connect(sigc::mem_fun(*this, &NewMapWin::closeIfEscapeIsPressed));
38 Gtk::VBox * vbox=get_vbox();
41 table=new Gtk::Table(5, 2, false);
44 label->set_text("Name of new map:");
47 (*table).attach(*label,0,1,0,1,Gtk::SHRINK,Gtk::SHRINK,10,3);
48 (*table).attach(name,1,2,0,1,Gtk::SHRINK,Gtk::SHRINK,10,3);
50 lblType.set_label("Element type:");
52 cbType.append_text("Numeric");
54 cbType.append_text("String");
57 (*table).attach(lblType,0,1,1,2,Gtk::SHRINK,Gtk::SHRINK,10,3);
58 (*table).attach(cbType, 1,2,1,2,Gtk::SHRINK,Gtk::SHRINK,10,3);
61 label->set_text("Default value in the map:");
62 default_value.set_text("0");
64 (*table).attach(*label,0,1,2,3,Gtk::SHRINK,Gtk::SHRINK,10,3);
65 (*table).attach(default_value,1,2,2,3,Gtk::SHRINK,Gtk::SHRINK,10,3);
67 //node vs. arc map selector
68 Gtk::RadioButton::Group group = node.get_group();
73 (*table).attach(node,0,1,3,4,Gtk::SHRINK,Gtk::SHRINK,10,3);
74 (*table).attach(arc,1,2,3,4,Gtk::SHRINK,Gtk::SHRINK,10,3);
88 (*table).attach(lblErrorMsg,0,2,4,5,Gtk::SHRINK,Gtk::SHRINK,10,3);
90 vbox->pack_start(*table);
93 add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
99 void NewMapWin::setErrorMsg(const Glib::ustring& msg)
101 lblErrorMsg.set_markup("<i><small>" + msg + "</small></i>");
104 std::vector<double>* NewMapWin::evaluate_expr(const std::string polishform, bool itisarc)
106 MapStorage& ms = *mytab.mapstorage;
108 std::vector<double>* ret = new std::vector<double>;
109 std::stack<double> polishstack;
113 for(ArcIt k(ms.digraph); k!=INVALID; ++k)
115 for(int i=0;i<(int)polishform.size();i++)
119 switch(polishform[i])
125 op1=polishstack.top();
127 op2=polishstack.top();
131 //substitute variable
132 std::vector<std::string> maps = ms.getArcMapList(NUM);
133 bool itisvar=(std::find(maps.begin(), maps.end(), ch2var[ polishform[i] ]) != maps.end());
136 polishstack.push(ms.get(ch2var[ polishform[i] ], k));
140 polishstack.push(atof(ch2var[ polishform[i] ].c_str()));
148 switch(polishform[i])
163 std::cout << "How could we get here?" << std::endl;
166 polishstack.push(res);
168 }//foreach letter in polishform
169 ret->push_back(polishstack.top());
174 for(NodeIt k(ms.digraph); k!=INVALID; ++k)
176 for(int i=0;i<(int)polishform.size();i++)
180 switch(polishform[i])
186 op1=polishstack.top();
188 op2=polishstack.top();
192 //substitute variable
193 std::vector<std::string> maps = ms.getNodeMapList(NUM);
194 bool itisvar=(std::find(maps.begin(), maps.end(), ch2var[ polishform[i] ]) != maps.end());
197 polishstack.push(ms.get(ch2var[ polishform[i] ], k));
201 polishstack.push(atof(ch2var[ polishform[i] ].c_str()));
209 switch(polishform[i])
224 std::cout << "How could we get here?" << std::endl;
227 polishstack.push(res);
229 }//foreach letter in polishform
230 ret->push_back(polishstack.top());
236 void NewMapWin::on_response(int response_id)
238 MapStorage& ms = *mytab.mapstorage;
240 if(response_id==Gtk::RESPONSE_OK)
242 std::string map_name = name.get_text();
243 std::string def_val = default_value.get_text();
245 if (map_name.empty())
247 setErrorMsg("No map name given.");
251 // check whether the map already exists
252 if (arc.get_active())
254 if (ms.arcMapExists(map_name))
256 setErrorMsg("Map '" + map_name + "' already exists.");
262 if (ms.nodeMapExists(map_name))
264 setErrorMsg("Map '" + map_name + "' already exists.");
269 Glib::ustring text = cbType.get_active_text();
270 if (text == "Numeric")
274 d = strtod(def_val.c_str(), &endptr);
275 if (def_val.c_str() + def_val.length() == endptr)
277 // the full string was a number
278 if (arc.get_active())
279 ms.createArcMap(map_name, MapValue::NUMERIC,
282 ms.createNodeMap(map_name, MapValue::NUMERIC,
287 // let't try to evaluate the string as an arithmetic expression
288 std::string polishform =
289 string2Polishform(def_val, arc.get_active());
290 if (polishform.empty())
292 std::vector<double>* values =
293 evaluate_expr(polishform, arc.get_active());
294 if (arc.get_active())
296 ms.createArcMap(map_name, MapValue::NUMERIC,
298 std::vector<double>::const_iterator vit = values->begin();
299 for (ArcIt it(ms.digraph); it != INVALID; ++it)
301 ms.set(map_name, it, MapValue(*vit));
307 ms.createNodeMap(map_name, MapValue::NUMERIC,
309 std::vector<double>::const_iterator vit = values->begin();
310 for (NodeIt it(ms.digraph); it != INVALID; ++it)
312 ms.set(map_name, it, MapValue(*vit));
319 else if (text == "String")
321 if (arc.get_active())
322 ms.createArcMap(map_name, MapValue::STRING,
325 ms.createNodeMap(map_name, MapValue::STRING,
330 default_value.set_text("0");
338 std::string NewMapWin::string2Polishform(std::string rawcommand, bool itisarc)
340 bool valid_entry=true;
342 std::map<std::string, int> str2i;
346 std::string variable;
350 for(int i=0;(valid_entry&&(i<(int)rawcommand.size()));i++)
352 switch(rawcommand[i])
360 if(!variable.empty())
362 valid_entry=validVariable(variable, itisarc);
363 ch2var[index]=variable;
366 variable.erase(0,variable.size());
368 command+=rawcommand[i];
371 variable+=rawcommand[i];
376 if(!variable.empty()&&valid_entry)
378 valid_entry=validVariable(variable, itisarc);
379 ch2var[index]=variable;
382 variable.erase(0,variable.size());
387 unsigned int pr=10000;
389 unsigned int prev_change=pr;
390 unsigned int prev_br=pr;
392 std::string comm_nobr="";
393 std::vector<unsigned int> p;
397 //6 brackets embedded
398 //100 operation in a row from the same priority
400 for(int i=0;i<(int)command.size();i++)
402 bool put_in_string=true;
448 comm_nobr=comm_nobr+command[i];
452 tree_node * root=weightedString2Tree(comm_nobr, p, 0);
454 std::string polishform=postOrder(root);
463 void NewMapWin::deleteTree(NewMapWin::tree_node * node)
465 if(node->left_child!=NULL)
467 deleteTree(node->left_child);
469 if(node->right_child!=NULL)
471 deleteTree(node->right_child);
476 NewMapWin::tree_node * NewMapWin::weightedString2Tree(std::string to_tree, std::vector<unsigned int> & p, int offset)
478 unsigned int min=p[offset];
480 for(int i=0;i<(int)to_tree.size();i++)
488 tree_node * act_node=new tree_node;
489 act_node->ch=to_tree[minplace];
490 if(to_tree.size()>=3)
492 act_node->left_child=weightedString2Tree(to_tree.substr(0,minplace), p, offset);
493 act_node->right_child=weightedString2Tree(to_tree.substr(minplace+1,to_tree.size()-minplace-1), p, offset+minplace+1);
497 act_node->left_child=NULL;
498 act_node->right_child=NULL;
503 std::string NewMapWin::postOrder(tree_node * subtree)
505 std::string subtree_to_string;
506 if(subtree->left_child)
508 subtree_to_string=postOrder(subtree->left_child);
510 if(subtree->right_child)
512 subtree_to_string=subtree_to_string+postOrder(subtree->right_child);
514 subtree_to_string=subtree_to_string+subtree->ch;
515 return subtree_to_string;
518 bool NewMapWin::validVariable(std::string variable, bool itisarc)
520 MapStorage& ms = *mytab.mapstorage;
526 std::vector<std::string> arc_maps =
527 ms.getArcMapList(NUM);
528 cancel=(std::find(arc_maps.begin(), arc_maps.end(), variable)==arc_maps.end());
532 std::vector<std::string> node_maps =
533 ms.getNodeMapList(NUM);
534 cancel=(std::find(node_maps.begin(), node_maps.end(), variable)==node_maps.end());
541 for(int j=0;(!cancel)&&(j<(int)variable.size());j++)
543 if(((variable[j]<'0')||(variable[j]>'9'))&&(variable[j]!='.'))