00001 #include "Translate/TranslateCore.hpp"
00002 #include "AML/AMLUtilities.hpp"
00003
00004 using namespace std;
00005 using namespace BasicUtilities;
00006
00007
00008
00009
00010 TranslateCore::TranslateCore() {
00011
00012 aml_ignore_these_attribs.clear();
00013 attribute_list.clear();
00014 x_class_to_aml_map.clear();
00015
00016 aml_class_names << "beambeam";
00017 aml_class_names << "bend";
00018 aml_class_names << "electric_kicker";
00019 aml_class_names << "field_table";
00020 aml_class_names << "kicker";
00021 aml_class_names << "marker";
00022 aml_class_names << "match";
00023 aml_class_names << "multipole";
00024 aml_class_names << "octupole";
00025 aml_class_names << "patch";
00026 aml_class_names << "quadrupole";
00027 aml_class_names << "rf_cavity";
00028 aml_class_names << "sextupole";
00029 aml_class_names << "solenoid";
00030 aml_class_names << "taylor_map";
00031 aml_class_names << "wiggler";
00032 aml_class_names << "aperture";
00033
00034 }
00035
00036
00037
00038
00039 bool TranslateCore::ModifyIncludedFileNames (UAPNode* root,
00040 const string& replacement_suffix) {
00041
00042
00043
00044 string replace_str = replacement_suffix;
00045 if (replace_str[0] != '.') replace_str = "." + replace_str;
00046
00047
00048
00049 bool ok = true;
00050 if (!modify_file_names ("{http://www.w3.org/2001/XInclude}include", "href",
00051 root, replace_str)) ok = false;
00052 if (!modify_file_names ("AML_representation", "filename", root, replace_str)) ok = false;
00053 if (!modify_file_names (language + "_representation",
00054 "filename", root, replace_str)) ok = false;
00055 if (!modify_file_names ("call", "filename", root, replace_str)) ok = false;
00056
00057 return ok;
00058
00059 }
00060
00061
00062
00063
00064 bool TranslateCore::modify_file_names (const string& node_name,
00065 const string& attrib_name, UAPNode* root, const string& replace_str) {
00066
00067 bool ok = true;
00068
00069 string dir, file;
00070 NodeVec incs = root->getSubNodesByName (node_name);
00071 for (NodeVecIter it = incs.begin(); it != incs.end(); it++) {
00072 UAPAttribute* attrib = (*it)->getAttribute(attrib_name);
00073 if (!attrib) {
00074 info_out.error ("No " + attrib_name + " attribute found for <" +
00075 node_name + "> node: " + (*it)->toString());
00076 ok = false;
00077 continue;
00078 }
00079 string name = attrib->getValue();
00080 split_file_name (name, dir, file);
00081 size_t ip = file.rfind(".");
00082 if (ip != string::npos) file.erase(ip);
00083 attrib->setValue(dir + file + replace_str);
00084 }
00085
00086 return ok;
00087
00088 }
00089
00090
00091
00092
00093 UAPNode* TranslateCore::XFileToAMLRep (const string& filename, UAPNode* uap_root) {
00094
00095
00096
00097 UAPNode* root = uap_root;
00098 if (!root) root = new UAPNode("UAP_root");
00099
00100
00101
00102 UAPNode* x_rep = XFileToXRep (filename, root);
00103
00104
00105
00106 XRepToAMLRep (root);
00107
00108 return root;
00109
00110 }
00111
00112
00113
00114
00115 UAPNode* TranslateCore::XFileToXRep (const string& filename, UAPNode* root) {
00116
00117
00118
00119 name_to_x_class_map.clear();
00120
00121
00122
00123 UAPNode* this_root = root;
00124 if (!this_root) this_root = new UAPNode("UAP_root");
00125 UAPNode* x_rep = this_root->addChild(language + "_representation");
00126 x_rep->addAttribute ("source_language", language);
00127
00128
00129
00130 FileStackList file_stack;
00131 info_out.ix_line = -1;
00132 info_out.parsing_status = "Parsing " + language + " file.";
00133 info_out.statement = "";
00134 x_rep->addAttribute("filename", filename);
00135 x_file_to_x (x_rep, filename, file_stack);
00136
00137 info_out.file_name = "";
00138
00139 return x_rep;
00140
00141 }
00142
00143
00144
00145
00146 void TranslateCore::x_file_to_x (UAPNode* x_rep_root,
00147 string file_name, FileStackList file_stack) {
00148
00149
00150
00151
00152 StrList word_list;
00153
00154 file_stack.addFile(file_name);
00155 string this_file = file_stack.file_list.back().full_name;
00156
00157
00158
00159 ifstream x_input_file(this_file.c_str());
00160 if (!x_input_file.good()) {
00161 info_out.error("Cannot open file: " + this_file);
00162 return;
00163 }
00164 bool close_file = false;
00165
00166 in_sequence = false;
00167 string comment = "";
00168 info_out.statement = "";
00169
00170
00171
00172
00173
00174 string line, word;
00175 bool line_continue_found;
00176 char quote_delim = ' ';
00177 int ix_line = 0;
00178 bool continued_line = false;
00179 bool in_extended_comment = false;
00180 bool init_for_new_command = false;
00181
00182 while (!x_input_file.eof()) {
00183
00184
00185
00186 getline(x_input_file, line);
00187 if (word_list.size() == 0)
00188 info_out.statement = line;
00189 else {
00190 info_out.statement += '\n';
00191 info_out.statement += line;
00192 }
00193 line_continue_found = false;
00194 bool delim_found = true;
00195 bool space_found = false;
00196 bool blank_line = true;
00197 ix_line++;
00198
00199 if (in_extended_comment) comment += '\n';
00200
00201
00202
00203
00204 if (init_for_new_command) {
00205 word_list.clear();
00206 delim_found = true;
00207 info_out.statement = line; comment = "";
00208 continued_line = false;
00209 init_for_new_command = false;
00210 }
00211
00212
00213
00214
00215 for (size_t ix_char = 0; ix_char < line.length(); ix_char++) {
00216
00217
00218
00219 if (init_for_new_command) {
00220 word_list.clear();
00221 delim_found = true;
00222 info_out.statement = line; comment = "";
00223 continued_line = false;
00224 init_for_new_command = false;
00225 }
00226
00227
00228
00229 char ch = line[ix_char];
00230 char ch2 = ' ';
00231 if (ix_char+1 != line.length()) ch2 = line[ix_char+1];
00232
00233
00234
00235 if (c_style_format && ch == '/' && ch2 == '*') {
00236 in_extended_comment = true;
00237 ix_char++;
00238 continue;
00239 }
00240
00241 if (in_extended_comment) {
00242 if (ch == '*' && ch2 == '/') {
00243 ix_char++;
00244 in_extended_comment = false;
00245 x_statement_to_x (word_list, comment, x_rep_root,
00246 quote_delim, close_file, ix_line, file_stack);
00247 if (close_file) return;
00248 init_for_new_command = true;
00249 } else {
00250 comment += ch;
00251 }
00252 continue;
00253 }
00254
00255
00256 if (ch != ' ' && ch != '\t' && ch != '!') blank_line = false;
00257
00258
00259
00260 if (ch == '\'' || ch == '\"') {
00261 if (quote_delim == ' ') {
00262 add_word_to_list (word, word_list);
00263 quote_delim = ch;
00264 word += ch;
00265 continue;
00266 }
00267 if (quote_delim == ch) {
00268 quote_delim = ' ';
00269 word += ch;
00270 add_word_to_list (word, word_list);
00271 continue;
00272 }
00273 }
00274
00275
00276
00277 if (quote_delim != ' ') {
00278 word += ch;
00279 continue;
00280 }
00281
00282
00283
00284
00285 if (ch == ' ' || ch == '\t') {
00286 space_found = true;
00287 continue;
00288 }
00289
00290
00291
00292
00293 if (ch == ';') {
00294 add_word_to_list (word, word_list);
00295 ix_char++;
00296 while (ix_char != line.length() && line[ix_char] == ' ') ix_char++;
00297
00298 if (ix_char != line.length()) {
00299 if (line[ix_char] == '!' ||
00300 (c_style_format && line[ix_char] == '/' && line[ix_char+1] == '/')) {
00301 ix_char++;
00302 if (line[ix_char] == '/') ix_char++;
00303 comment = line.substr(ix_char);
00304 ix_char = line.length();
00305 } else if (c_style_format && line[ix_char] == '/' && line[ix_char+1] == '*') {
00306 in_extended_comment = true;
00307 ix_char++;
00308 continue;
00309 } else {
00310 ix_char--;
00311 }
00312 }
00313
00314 x_statement_to_x (word_list, comment, x_rep_root,
00315 quote_delim, close_file, ix_line, file_stack);
00316 if (close_file) return;
00317 init_for_new_command = true;
00318 continue;
00319 }
00320
00321
00322
00323 if (ch == '&' && !c_style_format) {
00324 line_continue_found = true;
00325 break;
00326 }
00327
00328
00329
00330 if (ch == '!' || (c_style_format && ch == '/' && ch2 == '/')) {
00331 add_word_to_list (word, word_list);
00332 if (ch == '/') ix_char++;
00333 comment = line.substr(ix_char+1);
00334 break;
00335 }
00336
00337
00338
00339 if (ch == '=' || ch == ',' || ch == ':' || ch == '(' || ch == ')' ||
00340 ch == '-' || ch == '*' || ch == '{' || ch == '}' || ch == '/' ||
00341 ch == '[' || ch == ']') {
00342 add_word_to_list (word, word_list);
00343 word = ch;
00344 if (ch == '-' && ch2 == '>') {
00345 word = "->";
00346 ix_char++;
00347 }
00348 if (ch == ':' && ch2 == '=') {
00349 word = "=";
00350 ix_char++;
00351 }
00352 add_word_to_list (word, word_list);
00353 delim_found = true;
00354 continue;
00355 }
00356
00357 if (!delim_found && space_found) add_word_to_list (word, word_list);
00358 word += ch;
00359 space_found = false;
00360 delim_found = false;
00361
00362 }
00363
00364
00365
00366 add_word_to_list (word, word_list);
00367
00368 if (line_continue_found || c_style_format) {
00369 continued_line = true;
00370
00371 } else if (!blank_line || !continued_line) {
00372 x_statement_to_x (word_list, comment, x_rep_root,
00373 quote_delim, close_file, ix_line, file_stack);
00374 if (close_file) return;
00375 init_for_new_command = true;
00376 }
00377
00378 }
00379
00380
00381
00382 if (c_style_format && word_list.size() > 0) {
00383 info_out.error ("FINAL \";\" NOT FOUND IN INPUT FILE.");
00384 }
00385
00386 if (in_extended_comment) {
00387 info_out.error ("OPENING '/*' DOES NOT HAVE A CLOSING '*/' IN INPUT FILE");
00388 }
00389
00390 return;
00391
00392 }
00393
00394
00395
00396
00397 void TranslateCore::add_word_to_list (string& word, StrList& word_list) {
00398
00399
00400
00401 if (word.length() == 0) return;
00402
00403
00404
00405
00406 if (word[0] == '\'' || word[0] == '\"') {
00407 word.erase(0,1);
00408 word.erase(word.length()-1,1);
00409 }
00410
00411
00412
00413 word_list.push_back(word);
00414 word.clear();
00415
00416 return;
00417 }
00418
00419
00420
00421
00422 void TranslateCore::x_statement_to_x (StrList& word_list,
00423 string comment, UAPNode* x_rep_root, char& quote_delim,
00424 bool& close_file, int ix_line, FileStackList file_stack) {
00425
00426
00427
00428
00429
00430
00431 info_out.file_name = x_rep_root->getAttributeString("filename");
00432 info_out.ix_line = ix_line;
00433
00434
00435
00436 if (quote_delim != ' ') {
00437 info_out.error ("String not terminated with quotation mark");
00438 quote_delim = ' ';
00439 return;
00440 }
00441
00442
00443
00444 comment = trim(comment);
00445
00446
00447
00448 if (word_list.size() == 0) {
00449 if (comment != "") {
00450 UAPNode* node = x_rep_root->addChild("comment");
00451 node->addAttribute("text", comment);
00452 }
00453 return;
00454 }
00455
00456
00457
00458 if (custom_x_statement_to_x (word_list, comment, x_rep_root)) return;
00459
00460
00461
00462 string word1 = next_word(word_list);
00463
00464
00465
00466 if (word1 == "endsequence") {
00467 if (!in_sequence) info_out.error ("endsequence found without beginning sequence line.");
00468 in_sequence = false;
00469 return;
00470 }
00471
00472
00473
00474 if (in_sequence) {
00475
00476 if (word_list.size() < 4) {
00477 info_out.error("Malformed line in sequence construct");
00478 return;
00479 }
00480
00481 string word2 = next_word(word_list);
00482
00483 if (word2 == ":") {
00484 in_sequence = false;
00485 word_list.push_front(word2);
00486 word_list.push_front(word1);
00487 x_statement_to_x (word_list, comment, seq_node,
00488 quote_delim, close_file, ix_line, file_stack);
00489 in_sequence = true;
00490 return;
00491 }
00492
00493 string at, from;
00494 while (true) {
00495 string word3 = next_word(word_list);
00496 string word4 = next_word(word_list);
00497 if (word2 != "," || (word3 != "at" && word3 != "from") || word4 != "=") {
00498 info_out.error ("Bad line in sequence construct");
00499 return;
00500 }
00501 if (word3 == "at") {
00502 while (word_list.size() > 0 && word_list.front() != ",") at += next_word(word_list);
00503 } else {
00504 while (word_list.size() > 0 && word_list.front() != ",") from += next_word(word_list);
00505 }
00506
00507 if (word_list.size() == 0) break;
00508
00509 if (word_list.size() < 4) {
00510 info_out.error("Malformed sub-sequence or element in sequence construct");
00511 return;
00512 }
00513 word2 = next_word(word_list);
00514
00515 }
00516
00517
00518
00519 if (!found(name_to_x_class_map, word1)) {
00520 info_out.error ("UNKNOWN ELEMENT OR SEQUENCE: " + word1);
00521 return;
00522 }
00523
00524 UAPNode* node;
00525 if (name_to_x_class_map[word1] == "sequence")
00526 node = seq_node->addChild("sequence");
00527 else
00528 node = seq_node->addChild("element");
00529
00530 node->addAttribute("ref", word1);
00531 node->addAttribute("at", at);
00532 if (from != "") node->addAttribute("from", from);
00533
00534 return;
00535 }
00536
00537
00538
00539 if (word_list.size() == 0) {
00540
00541 if (word1 == "return" || word1 == "end_file") {
00542 UAPNode* node = x_rep_root->addChild(word1);
00543 close_file = true;
00544 if (comment != "") node->addAttribute("comment", comment);
00545 return;
00546 }
00547 info_out.error ("Invalid or incomplete statement");
00548 return;
00549
00550 }
00551
00552
00553
00554 if (word_list.size() < 2) {
00555 info_out.error ("Invalid or incomplete statement");
00556 return;
00557 }
00558
00559 if (word1 == "beam" || word1 == "twiss") {
00560 UAPNode* node = x_rep_root->addChild(word1);
00561 if (comment != "") node->addAttribute("comment", comment);
00562 x_add_attributes (word_list, word1, node);
00563 return;
00564 }
00565
00566 string word2 = next_word(word_list);
00567 string word3 = next_word(word_list);
00568
00569 if (word2 == ",") {
00570
00571 if (word1 == "call") {
00572 if (word_list.size() < 2) {
00573 info_out.error ("Malformed call statement.");
00574 return;
00575 }
00576 string word4 = next_word(word_list);
00577 if (word3 != string("filename").substr(0,word3.size()) || word4 != "=") {
00578 info_out.error ("\"Filename =\" missing or misplaced in call statement");
00579 return;
00580 }
00581 string file_name = next_word(word_list, NO_CONVERT);
00582 while (word_list.size() > 0) {
00583 file_name += next_word(word_list, NO_CONVERT);
00584 }
00585 UAPNode* call_root = x_rep_root->addChild("call");
00586 if (comment != "") x_rep_root->addAttribute("comment", comment);
00587 call_root->addAttribute("filename", file_name);
00588 x_file_to_x (call_root, file_name, file_stack);
00589 return;
00590 }
00591
00592
00593
00594
00595
00596 string x_class = ele_name_to_x_class(word1);
00597 if (x_class != "") {
00598 UAPNode* node0 = x_rep_root->addChild("temporary");
00599 word_list.push_front(word3);
00600 word_list.push_front(",");
00601 x_add_attributes (word_list, x_class, node0);
00602 AttributeVec attribs = node0->getAttributes();
00603 for (AttributeVecIter ia = attribs.begin(); ia != attribs.end(); ia++) {
00604 UAPNode* set = x_rep_root->addChild("set");
00605 set->addAttribute("element", word1);
00606 set->addAttribute("attribute", ia->getName());
00607 set->addAttribute("value", ia->getValue());
00608 }
00609 node0->deleteNode();
00610 return;
00611 }
00612
00613
00614
00615 UAPNode* node = x_rep_root->addChild(word1);
00616 if (comment != "") node->addAttribute("comment", comment);
00617
00618 if (word1 == "title") {
00619 node->addAttribute ("text", word3);
00620 return;
00621 }
00622
00623
00624
00625 info_out.error ("Unrecognized statement");
00626 return;
00627 }
00628
00629
00630
00631 if (word2 == "=") {
00632 UAPNode* node = x_rep_root->addChild("constant");
00633 node->addAttribute("name", word1);
00634 while (word_list.size() != 0) word3 += next_word(word_list);
00635 node->addAttribute("design", word3);
00636 if (comment != "") node->addAttribute("comment", comment);
00637 return;
00638 }
00639
00640
00641
00642 string args;
00643
00644 if (word2 == "(") {
00645 word_list.push_front(word3);
00646 if (!get_args (word_list, args)) return;
00647 if (word_list.size() < 6) {
00648 info_out.error ("Malformed replacement list");
00649 return;
00650 }
00651 word2 = next_word(word_list);
00652 word3 = next_word(word_list);
00653 }
00654
00655 if (word2 != ":") {
00656 info_out.error ("Missing or misplaced \":\"");
00657 return;
00658 }
00659
00660 bool constant = false;
00661 if (word3 == "constant") {
00662 if (word_list.size() < 2) {
00663 info_out.error ("Malformed constant definition statement");
00664 return;
00665 }
00666 if (word_list.front() != "=") {
00667 info_out.error ("Missing or misplaced \"=\"");
00668 return;
00669 }
00670 constant = true;
00671 word3 = next_word(word_list);
00672 }
00673
00674
00675
00676 if (word3 == "=") {
00677 UAPNode* node = x_rep_root->addChild("constant");
00678 node->addAttribute("name", word1);
00679 string wrd = next_word(word_list);
00680 while (word_list.size() != 0) wrd += next_word(word_list);
00681 node->addAttribute("design", wrd);
00682 if (constant) node->addAttribute("type", "constant");
00683 if (comment != "") node->addAttribute("comment", comment);
00684 return;
00685 }
00686
00687
00688
00689 if (!valid_variable(word1)) {
00690 info_out.error ("Malformed statement first word: " + word1);
00691 return;
00692 }
00693
00694
00695
00696 string x_class = ele_name_to_x_class(word3);
00697 if (x_class == "") {
00698 x_class = named_x_class(word3);
00699 if (x_class == "") {
00700 info_out.error ("Unrecognized element class: " + word3);
00701 return;
00702 }
00703 word3 = x_class;
00704 }
00705
00706 if (args != "") {
00707 if (word3 != "line") {
00708 info_out.error ("Only lines can have arguments");
00709 return;
00710 }
00711 }
00712
00713
00714
00715 if (word3 == "sequence") {
00716 seq_node = x_rep_root->addChild("sequence");
00717 seq_node->addAttribute("name", word1);
00718 name_to_x_class_map[word1] = "sequence";
00719
00720 x_add_attributes (word_list, "sequence", seq_node);
00721 if (comment != "") seq_node->addAttribute("comment", comment);
00722 in_sequence = true;
00723
00724 while(true) {
00725 if (word_list.size() == 0) break;
00726 string word4 = next_word(word_list);
00727 string word5 = next_word(word_list);
00728 string word6 = next_word(word_list);
00729 string word7 = next_word(word_list);
00730 if (word4 != "," || word6 != "=" || word7 == "") {
00731 info_out.error ("Malformed SEQUENCE");
00732 return;
00733 }
00734 seq_node->addAttribute(word5, word7);
00735 }
00736 return;
00737 }
00738
00739
00740
00741 if (word3 == "line" || word3 == "list") {
00742 UAPNode* class_node = x_rep_root->addChild(word3);
00743 class_node->addAttribute("name", word1);
00744 if (comment != "") class_node->addAttribute("comment", comment);
00745 if (word_list.size() < 4) {
00746 info_out.error ("Malformed line or list of elements");
00747 return;
00748 }
00749
00750 string word4 = next_word(word_list);
00751 string word5 = next_word(word_list);
00752
00753 if (word3 == "line" && word4 == "[") {
00754 if (word5 != "multipass" || next_word(word_list) != "]") {
00755 info_out.error ("Malformed line statement");
00756 return;
00757 }
00758 class_node->addAttribute("multipass", "TRUE");
00759 word4 = next_word(word_list);
00760 word5 = next_word(word_list);
00761 }
00762
00763 if (word4 != "=" || word5 != "(") {
00764 info_out.error ("Expecting an \"= (\" after line or list");
00765 return;
00766 }
00767
00768 word_list.push_front("(");
00769 if (!parse_this_line_or_list (word_list, class_node)) return;
00770 if (word_list.size() > 0) {
00771 string rest = next_word(word_list);
00772 while (word_list.size() != 0) rest += next_word(word_list);
00773 info_out.error ("Statement did not end after final \")\" in line or list: " + rest);
00774 return;
00775 }
00776 if (args != "") class_node->addAttribute("args", args);
00777 return;
00778 }
00779
00780
00781
00782 UAPNode* element_node = x_rep_root->addChild("element");
00783 element_node->addAttribute("name", word1);
00784 name_to_x_class_map[word1] = x_class;
00785
00786 if (found (name_to_x_class_map, word3)) {
00787 element_node->addAttribute("inherit", word3);
00788 } else {
00789 element_node->addAttribute("class", word3);
00790 }
00791 if (comment != "") element_node->addAttribute("comment", comment);
00792
00793 x_add_attributes (word_list, x_class, element_node);
00794 return;
00795
00796 }
00797
00798
00799
00800
00801 string TranslateCore::named_x_class (const string& name) {
00802
00803
00804
00805 string x_class = ele_name_to_x_class(name);
00806 if (x_class != "") return x_class;
00807
00808
00809
00810 size_t isize = name.size();
00811 if (isize < 2) return "";
00812
00813 for (StrMapCIter im = x_class_to_aml_map.begin();
00814 im != x_class_to_aml_map.end(); im++) {
00815 string x_class = im->first;
00816 if (isize > x_class.size()) continue;
00817 if (name.substr(0,isize) == x_class.substr(0,isize)) return x_class;
00818 }
00819
00820 return "";
00821
00822 }
00823
00824
00825
00826
00827 string TranslateCore::ele_name_to_x_class (const string& name, bool include_param_names) {
00828
00829 if (found(name_to_x_class_map, name)) return name_to_x_class_map[name];
00830 if (include_param_names && found(x_param_names, name)) return name;
00831 return "";
00832
00833 }
00834
00835
00836
00837
00838 string TranslateCore::next_word (StrList& word_list, CaseConvert this_case) {
00839
00840 if (this_case == TO_LOWER)
00841 return str_to_lower(str_pop(word_list));
00842 if (this_case == NO_CONVERT)
00843 return str_pop(word_list);
00844 if (this_case == TO_UPPER)
00845 return str_to_upper(str_pop(word_list));
00846
00847 info_out.error("Bad this_case argument");
00848 return str_pop(word_list);
00849
00850 }
00851
00852
00853
00854
00855 bool TranslateCore::valid_variable (const string& var) {
00856 if (!isalpha(var[0])) return false;
00857 for (size_t i = 1; i < var.length(); i++) {
00858 if (!isalpha(var[i]) && !isdigit(var[i]) && var[i] != '_' && var[i] != '.') return false;
00859 }
00860 return true;
00861 }
00862
00863
00864
00865
00866 bool TranslateCore::parse_this_line_or_list (StrList& word_list, UAPNode* seq_root) {
00867
00868
00869
00870 while (true) {
00871 if (word_list.size() == 0) {
00872 info_out.error ("Line or list ended without final \"\"");
00873 return false;
00874 }
00875 string delim = next_word(word_list, NO_CONVERT);
00876 if (delim == ")") return true;
00877 if (delim == "," || delim == "(") {
00878 bool good = parse_this_line_or_list_element (word_list, seq_root);
00879 if (!good) return false;
00880 } else {
00881 info_out.error ("Expecting \"(\", \")\" or \",\" in line expansion but got: " + delim);
00882 return false;
00883 }
00884 }
00885
00886 }
00887
00888
00889
00890
00891 bool TranslateCore::parse_this_line_or_list_element (StrList& word_list, UAPNode* seq_root) {
00892
00893
00894
00895 if (word_list.size() < 2) {
00896 info_out.error ("Malformed end of line or list");
00897 return false;
00898 }
00899
00900 string w1 = next_word(word_list);
00901
00902 string negative = "";
00903 if (w1 == "-") {
00904 negative = "-";
00905 w1 = next_word(word_list);
00906 }
00907
00908
00909
00910 string repeat("1");
00911 if (isdigit(w1[0])) {
00912 if (!is_int(w1)) {
00913 info_out.error ("Malformed integer: " + w1);
00914 return false;
00915 }
00916 repeat = negative + w1;
00917 if (word_list.size() < 3) {
00918 info_out.error ("Incomplete line or list");
00919 return false;
00920 }
00921 string times = next_word(word_list, NO_CONVERT);
00922 if (times != "*") {
00923 info_out.error ("Missing \"*\"");
00924 return false;
00925 }
00926 w1 = next_word(word_list);
00927 } else {
00928 if (negative == "-") repeat = "-1";
00929 }
00930
00931 if (word_list.size() < 1) {
00932 info_out.error ("Incomplete line or list");
00933 return false;
00934 }
00935
00936
00937
00938
00939 if (w1 == "(") {
00940 UAPNode* line = seq_root->addChild("line");
00941 if (repeat != "1") line->addAttribute("repeat", repeat);
00942 word_list.push_front("(");
00943 return parse_this_line_or_list (word_list, line);
00944 }
00945
00946
00947
00948 string tag;
00949 string delim = next_word(word_list);
00950
00951 if (delim == "[") {
00952 if (word_list.size() < 3) {
00953 info_out.error ("Incomplete line or list");
00954 return false;
00955 }
00956 tag = next_word(word_list);
00957 delim = next_word(word_list);
00958 if (delim != "]") {
00959 info_out.error("Matching \"]\" to end tag not found");
00960 return false;
00961 }
00962 delim = next_word(word_list);
00963 }
00964
00965
00966
00967 if (delim == "(") {
00968 UAPNode* line = seq_root->addChild("line");
00969 line->addAttribute("ref", w1);
00970 if (repeat != "1") line->addAttribute("repeat", repeat);
00971 if (tag != "") line->addAttribute("tag", tag);
00972 string args;
00973 bool good = get_args (word_list, args);
00974 if (!good) return false;
00975 line->addAttribute("args", args);
00976 return true;
00977 }
00978
00979
00980
00981
00982 UAPNode* ele = seq_root->addChild("item");
00983 ele->addAttribute("ref", w1);
00984 if (repeat != "1") ele->addAttribute("repeat", repeat);
00985 if (tag != "") ele->addAttribute("tag", tag);
00986
00987
00988
00989 word_list.push_front(delim);
00990 return true;
00991
00992 }
00993
00994
00995
00996
00997 bool TranslateCore::get_args(StrList& word_list, string& args) {
00998
00999 args.clear();
01000
01001 while (true) {
01002 if (word_list.size() < 3) {
01003 info_out.error ("End of statement encountered while parsing argument list");
01004 return false;
01005 }
01006 string arg = next_word(word_list);
01007 if (!valid_variable(arg)) {
01008 info_out.error ("Malformed argument variable name: " + arg);
01009 return false;
01010 }
01011 args += arg;
01012 string wrd = next_word(word_list, NO_CONVERT);
01013 if (wrd == ")") return true;
01014 if (wrd != ",") {
01015 info_out.error ("Malformed argument list");
01016 return false;
01017 }
01018 args += ",";
01019 }
01020 }
01021
01022
01023
01024
01025 void TranslateCore::x_add_attributes (StrList& word_list, string ele_class, UAPNode* x_ele_root) {
01026
01027
01028
01029
01030
01031
01032
01033
01034 while (true) {
01035
01036 if (word_list.size() == 0) return;
01037 string w = next_word(word_list, NO_CONVERT);
01038 if (w != ",") {
01039 info_out.error ("Expected \",\" in element attribute list but got: " + w);
01040 return;
01041 }
01042
01043 if (word_list.size() == 0) {
01044 info_out.error ("Statement ended with a \",\".");
01045 return;
01046 }
01047
01048 string attrib_name = next_word(word_list);
01049 string value, delim;
01050
01051
01052
01053 if (custom_x_add_attributes (word_list, ele_class, attrib_name, x_ele_root)) continue;
01054
01055
01056
01057 if (!valid_attribute (ele_class, attrib_name)) {
01058 info_out.error ("Attribute not recognized: " + attrib_name);
01059
01060 while (true) {
01061 if (word_list.size() == 0) return;
01062 if (next_word(word_list) == ",") break;
01063 }
01064 word_list.push_front(",");
01065 continue;
01066 }
01067
01068
01069
01070 if (word_list.size() == 0) {
01071 if (!x_attribute_can_have_default(attrib_name)) return;
01072 x_ele_root->addAttribute(attrib_name, "");
01073 return;
01074 }
01075
01076 w = next_word(word_list);
01077 if (w == ",") {
01078 if (!x_attribute_can_have_default(attrib_name)) return;
01079 x_ele_root->addAttribute(attrib_name, "");
01080 word_list.push_front(",");
01081 continue;
01082 }
01083 if (!(w == "=" || (c_style_format && w == ":="))) {
01084 info_out.error ("Confused attribute: " + attrib_name);
01085 return;
01086 }
01087 if (word_list.size() == 0) {
01088 info_out.error ("Statement ended with a \"=\"");
01089 return;
01090 }
01091
01092 delim = concat_value_expression (word_list, x_attribute_case(attrib_name), value);
01093 x_ele_root->addAttribute(attrib_name, value);
01094 if (delim == "") return;
01095
01096 }
01097
01098 }
01099
01100
01101
01102
01103 string TranslateCore::concat_value_expression (StrList& word_list,
01104 CaseConvert this_case, string& value) {
01105
01106
01107
01108 int ix_parens = 0;
01109 int ix_curly = 0;
01110 int ix_square = 0;
01111 value = "";
01112
01113 while (true) {
01114 if (word_list.size() == 0) return "";
01115 string word = next_word(word_list, this_case);
01116 if (word == "(") ix_parens++;
01117 if (word == ")") ix_parens--;
01118 if (word == "{") ix_curly++;
01119 if (word == "}") ix_curly--;
01120 if (word == "{") ix_square++;
01121 if (word == "}") ix_square--;
01122 if (word == "," && ix_parens == 0 && ix_curly == 0 && ix_square == 0) {
01123 word_list.push_front(",");
01124 return ",";
01125 }
01126 value += word;
01127 }
01128
01129 }
01130
01131
01132
01133
01134 TranslateCore::CaseConvert TranslateCore::x_attribute_case(const string& attrib_name) {
01135
01136 CaseConvert this_case = TO_LOWER;
01137 if (found (x_attributes_to_upper_case, attrib_name)) this_case = TO_UPPER;
01138 if (found (x_attributes_no_case_change, attrib_name)) this_case = NO_CONVERT;
01139
01140 return this_case;
01141
01142 }
01143
01144
01145
01146
01147 bool TranslateCore::valid_attribute (const string& ele_class, string& attrib) {
01148
01149
01150
01151 int nl = attrib.length();
01152 int n_match = 0;
01153
01154 StrVec attribs = map_element_to_attribs[ele_class];
01155 for (StrVecIter ia = attribs.begin(); ia != attribs.end(); ia++) {
01156 string this_attrib = *ia;
01157 if (attrib == this_attrib) return true;
01158 if (abbreviations_permitted && attrib == this_attrib.substr(0, nl)) n_match++;
01159 }
01160
01161 if (n_match == 1) return true;
01162 return false;
01163
01164 }
01165
01166
01167
01168
01169 bool TranslateCore::x_attribute_can_have_default (string attrib_name) {
01170
01171 if (found(x_attribs_that_can_have_default, attrib_name)) return true;
01172
01173 info_out.error ("Attribute does not have a default value: " + attrib_name);
01174 return false;
01175
01176 }
01177
01178
01179
01180
01181 UAPNode* TranslateCore::XRepToAMLRep (UAPNode* x_root, UAPNode* aml_root) {
01182
01183
01184
01185 use_parent = NULL;
01186 name_to_x_class_map.clear();
01187
01188 info_out.ix_line = -1;
01189 info_out.statement = "";
01190 info_out.parsing_status = "Translating from " + language + " rep to AML rep.";
01191
01192 string str;
01193
01194
01195
01196 UAPNode* x_rep;
01197 UAPNode* uap_root = NULL;
01198
01199 if (x_root->getName() == "UAP_root") {
01200 uap_root = x_root;
01201 x_rep = x_root->getChildByName(language + "_representation");
01202 if (!x_rep) {
01203 info_out.error ("<UAP_root> does not have a <" + language + "_representation> child.");
01204 return NULL;
01205 }
01206 } else if (x_root->getName() == language + "_representation") {
01207 x_rep = x_root;
01208 uap_root = x_root->getParent();
01209 } else {
01210 info_out.error ("argument is not a <UAP_root> node nor a <" + language + "_representation> node",
01211 x_root->toString());
01212 return NULL;
01213 }
01214
01215
01216
01217 UAPNode* aml_rep = aml_root;
01218
01219 if (!aml_rep) {
01220 if (uap_root) {
01221 NodeVec reps = uap_root->getChildrenByName("AML_representation");
01222 for (NodeVecIter ir = reps.begin(); ir != reps.end(); ir++) (*ir)->deleteNode();
01223 aml_rep = uap_root->addChild("AML_representation");
01224 } else {
01225 aml_rep = new UAPNode("AML_representation");
01226 }
01227
01228 } else if (aml_rep->getName() == "UAP_root") {
01229 NodeVec reps = aml_rep->getChildrenByName("AML_representation");
01230 for (NodeVecIter ir = reps.begin(); ir != reps.end(); ir++) (*ir)->deleteNode();
01231 aml_rep = aml_rep->addChild("AML_representation");
01232
01233 } else if (aml_rep->getName() == "AML_representation") {
01234 UAPNode* root = aml_rep->getParent();
01235 aml_rep->deleteNode();
01236 if (root) aml_rep = root->addChild("AML_representation");
01237 else aml_rep = new UAPNode("AML_representation");
01238
01239 } else {
01240 info_out.error ("aml_root argument is not a <UAP_root> node nor a <AML_representation> node",
01241 aml_root->toString());
01242 return NULL;
01243 }
01244
01245
01246
01247 aml_rep->addAttribute ("source_language", language);
01248
01249 if(x_rep->getAttribute("filename"))
01250 aml_rep->addAttribute("filename", x_rep->getAttributeString("filename"));
01251
01252 aml_lab_root = aml_rep->addChild("laboratory");
01253 aml_lab_root->addAttribute("xmlns:xi", "http://www.w3.org/2001/XInclude");
01254 aml_lab_root->addAttribute("aml_version", "1");
01255
01256
01257
01258 x_rep_to_aml_init (x_rep);
01259 x_rep_to_aml (x_rep, aml_lab_root);
01260
01261
01262
01263 if (use_parent) {
01264 UAPNode* machine_node = use_parent->addChild("machine");
01265 machine_node->addChild("sector")-> addAttribute("ref", use_line);
01266 }
01267
01268
01269
01270 aml_lab_root->addXMLNSAttributes();
01271
01272
01273
01274 return aml_rep;
01275
01276 }
01277
01278
01279
01280
01281 void TranslateCore::x_rep_to_aml_init (UAPNode* x_rep) {
01282
01283
01284
01285 x_set_nodes.clear();
01286 name_to_aml_type_map.clear();
01287 name_to_x_type_map.clear();
01288 name_to_x_node_map.clear();
01289 name_to_aml_node_map.clear();
01290 x_rep_make_name_lists (x_rep);
01291
01292 }
01293
01294
01295
01296
01297 void TranslateCore::x_rep_make_name_lists (UAPNode* x_rep) {
01298
01299
01300
01301 NodeVec list = x_rep->getChildren();
01302 for (NodeVecCIter it = list.begin(); it != list.end(); it++) {
01303 UAPNode* node = *it;
01304
01305
01306
01307 string node_type = node->getName();
01308 if (node_type == "return") return;
01309
01310
01311
01312 if (node_type == "set") {
01313 x_set_nodes.push_back(node);
01314 continue;
01315 }
01316
01317
01318
01319 string ele_name = node->getAttributeString("name");
01320 x_rep_make_name_lists (node);
01321
01322 if (ele_name == "") continue;
01323 name_to_x_node_map[ele_name] = node;
01324
01325 if (node_type == "line" || node_type == "list" || node_type == "sequence") {
01326 name_to_x_type_map[ele_name] = node_type;
01327
01328 } else if (node_type == "overlay" || node_type == "group"){
01329 name_to_x_type_map[ele_name] = node_type;
01330 name_to_x_class_map[ele_name] = node_type;
01331
01332 } else if (node_type == "element") {
01333 name_to_x_type_map[ele_name] = node_type;
01334 string inherit = node->getAttributeString("inherit");
01335 string x_class;
01336 if (inherit != "") {
01337 x_class = ele_name_to_x_class(inherit);
01338 } else {
01339 x_class = node->getAttributeString("class");
01340 }
01341 name_to_x_class_map[ele_name] = x_class;
01342 }
01343
01344 }
01345
01346 }
01347
01348
01349
01350
01351 void TranslateCore::x_rep_to_aml (UAPNode* x_rep_root, UAPNode* lab) {
01352
01353
01354
01355 NodeVec list = x_rep_root->getChildren();
01356 for (NodeVecCIter it = list.begin(); it != list.end(); it++) {
01357
01358 if ((*it)->getName() == "return") return;
01359 x_node_to_aml (*it, lab, lab);
01360 }
01361
01362 return;
01363
01364 }
01365
01366
01367
01368
01369 void TranslateCore::x_node_to_aml (UAPNode* x_ele, UAPNode* aml_root, UAPNode* aml_ele,
01370 const string& ignore_this1, const string& ignore_this2) {
01371
01372 UAPNode* aml_node;
01373 string x_node_type = x_ele->getName();
01374 string aml_class, ele_name;
01375
01376
01377
01378 string com_str = x_ele->getAttributeString("comment");
01379 if (com_str != "") {
01380 UAPNode* x_ele = aml_ele->addChild("comment");
01381 x_ele->addAttribute("text", com_str);
01382 }
01383
01384
01385
01386 if (custom_x_node_to_aml (x_ele, aml_root, aml_ele)) return;
01387
01388
01389
01390 if (x_node_type == "sequence") {
01391
01392 if (x_ele->getAttribute("refpos"))
01393 info_out.error (
01394 "Translation for a sequence with a \"refpos\" attribute not currently implemented: " +
01395 x_ele->toString());
01396
01397
01398
01399 string length = x_ele->getAttributeString("l");
01400 if (length == "") length = x_ele->getAttributeString("length");
01401 if (length == "") {
01402 UAPNode* last_ele = x_ele->getChildren().back();
01403
01404
01405 if (last_ele->getName() == "element") {
01406 if (x_node_to_x_class(last_ele) != "marker")
01407 info_out.error ("Last element in sequence is not a marker: " +
01408 last_ele->toString());
01409 } else if (last_ele->getName() == "class") {
01410 if (last_ele->getAttributeString("name") != "drift")
01411 info_out.error ("Last element in sequence is not a marker: " +
01412 last_ele->toString());
01413 } else {
01414 info_out.error ("Last element in sequence is a sub-sequence but should be a drift.");
01415 }
01416
01417 length = last_ele->getAttributeString("at");
01418 }
01419
01420 string refer = x_ele->getAttributeString("refer");
01421 string ele_origin;
01422 if (refer == "entry")
01423 ele_origin = "ENTRANCE";
01424 else if (refer == "centre")
01425 ele_origin = "CENTER";
01426 else if (refer == "exit")
01427 ele_origin = "EXIT";
01428 else if (refer != "")
01429 info_out.error ("Bad \"refer\" attribute for sequence: " + x_ele->toString());
01430
01431 string seq_name = x_ele->getAttributeString("name");
01432
01433
01434
01435
01436
01437
01438 NodeVec eles = x_ele->getChildren();
01439 StrVec at_const_vec(eles.size());
01440 StrMap at_const_map;
01441 int n(-1);
01442
01443 for (NodeVecCIter ie = eles.begin(); ie != eles.end(); ie++) {
01444 UAPNode* x_seq_ele = *ie;
01445 n++;
01446
01447 string ele_name = x_seq_ele->getAttributeString("name");
01448 if (ele_name == "") ele_name = x_seq_ele->getAttributeString("ref");
01449
01450 string at_str = x_seq_ele->getAttributeString("at");
01451 if (at_str == "") {
01452 info_out.error ("No \"at\" attribute for sequence element: " + x_seq_ele->toString());
01453 return;
01454 }
01455
01456 if (at_str.find_first_of("+-/*^") == string::npos) {
01457 at_const_vec[n] = at_str;
01458 } else {
01459 at_const_vec[n] = "at" + int_to_string(n) + "_" + seq_name;
01460 aml_ele->addChild("constant")->addAttribute("name", at_const_vec[n])->addAttribute("design", at_str);
01461 }
01462 at_const_map[ele_name] = at_const_vec[n];
01463 }
01464
01465
01466
01467 UAPNode* seq_node = aml_ele->addChild("sector");
01468 seq_node->addAttribute("name", seq_name);
01469 seq_node->addAttribute("length", length);
01470
01471 n = -1;
01472 for (NodeVecCIter ie = eles.begin(); ie != eles.end(); ie++) {
01473 UAPNode* x_seq_ele = *ie;
01474 n++;
01475
01476 string type = x_seq_ele->getName();
01477 string name = x_seq_ele->getAttributeString("name");
01478 string ref = x_seq_ele->getAttributeString("ref");
01479 string at = at_const_vec[n];
01480
01481
01482
01483
01484
01485
01486
01487
01488 UAPNode* aml_seq_ele;
01489
01490 if (ref != "") {
01491 aml_seq_ele = seq_node->addChild("element");
01492 if (type == "sequence") aml_seq_ele->setName("sector");
01493 aml_seq_ele->addAttribute("ref", ref);
01494
01495 } else {
01496 x_node_to_aml (x_seq_ele, aml_root, seq_node, "at", "from");
01497 aml_seq_ele = seq_node->getChildren().back();
01498 }
01499
01500
01501
01502 UAPNode* super_node = aml_seq_ele->addChild("superimpose");
01503 string from = x_seq_ele->getAttributeString("from");
01504 if (from != "") at = at + " + " + at_const_map[from];
01505 super_node->addAttribute("offset", at);
01506 if (ele_origin != "") super_node->addAttribute("ele_origin", ele_origin);
01507 }
01508
01509 return;
01510
01511 }
01512
01513
01514
01515 if (x_node_type == "set") {
01516
01517 string ele_name = x_ele->getAttributeString("element");
01518 string x_attrib = x_ele->getAttributeString("attribute");
01519 string value = x_ele->getAttributeString("value");
01520
01521 string x_class = ele_name_to_x_class(ele_name) ;
01522 if (x_class == "") {
01523 info_out.error ("Element class name cannot be resolved in for: " + ele_name);
01524 }
01525
01526 attribute_convert_struct attrib_info;
01527 if (!found_x_attrib(x_class, x_attrib, attrib_info)) {
01528 info_out.error ("Unknown set attribute: " + x_attrib, "Of: " + x_ele->toString());
01529 return;
01530 }
01531
01532 if (attrib_info.aml_twig.target_attribute == "design")
01533 attrib_info.aml_twig.target_attribute = "";
01534
01535 attrib_info.aml_twig.name = ele_name;
01536
01537 value = x_expression_to_aml(value, attrib_info, ele_name);
01538
01539
01540
01541 bool bend_calc = (x_class == "rbend" || x_class == "sbend");
01542 if (x_attrib != "l" && x_attrib != "angle" && x_attrib != "rho" && x_attrib != "g" &&
01543 x_attrib != "e1" && x_attrib != "e2") bend_calc = false;
01544 if (x_class == "sbend" && (x_attrib == "e1" || x_attrib == "e2")) bend_calc = false;
01545
01546
01547
01548 if (!bend_calc) {
01549 if (attrib_info.aml_twig.target_attribute.find("string") == string::npos) {
01550 UAPNode* aml_node = aml_ele->addChild("set");
01551 aml_node->addAttribute("attribute", attrib_info.aml_twig.toString());
01552 aml_node->addAttribute("value", value);
01553 } else {
01554 UAPNode* aml_node = aml_ele->addChild("string_set");
01555 aml_node->addAttribute("attribute", attrib_info.aml_twig.toString());
01556 aml_node->addAttribute("string", value);
01557 }
01558
01559 return;
01560 }
01561
01562
01563
01564
01565
01566
01567
01568
01569 bend_struct& bend = bend_map[ele_name];
01570
01571 if (x_attrib == "rho") bend.rho = value;
01572 if (x_attrib == "g") bend.g = value;
01573 if (x_attrib == "l") bend.l = value;
01574 if (x_attrib == "angle") bend.angle = value;
01575 if (x_attrib == "e1") bend.e1 = value;
01576 if (x_attrib == "e2") bend.e2 = value;
01577
01578
01579
01580 int n_defined(0);
01581 if (bend.l != "") n_defined++;
01582 if (bend.g != "") n_defined++;
01583 if (bend.rho != "") n_defined++;
01584 if (bend.angle != "") n_defined++;
01585 if (n_defined > 2 || (bend.g != "" && bend.rho != "")) {
01586 info_out.error ("Bend is overspecified on set! " + x_ele->toStringTree());
01587 return;
01588 }
01589
01590 if (n_defined == 2) bend.complete = true;
01591
01592
01593
01594
01595 if (!bend.complete) return;
01596
01597 if (x_class == "sbend") {
01598 UAPNode* aml_node = aml_ele->addChild("set");
01599
01600 if (x_attrib == "l") {
01601 aml_node->addAttribute("attribute", ele_name + "[length]");
01602 aml_node->addAttribute("value", bend.l);
01603 if (bend.angle != "") {
01604 UAPNode* aml_node2 = aml_ele->addChild("set");
01605 aml_node2->addAttribute("attribute", ele_name + "[bend:g]");
01606 aml_node2->addAttribute("value",
01607 add_parens(bend.angle, 1) + "/" + add_parens(bend.l, 2));
01608 }
01609
01610 } else if (x_attrib == "g" || x_attrib == "rho") {
01611 aml_node->addAttribute("attribute", ele_name + "[bend:g]");
01612 if (x_attrib == "g")
01613 aml_node->addAttribute("value", bend.g);
01614 else
01615 aml_node->addAttribute("value", "1/" + add_parens(x_attrib, 1));
01616
01617 if (bend.angle != "") {
01618 UAPNode* aml_node2 = aml_ele->addChild("set");
01619 aml_node2->addAttribute("attribute", ele_name + "[length]");
01620 if (bend.g != "")
01621 aml_node2->addAttribute("value", add_parens(bend.g, 1) + "*" + add_parens(bend.angle, 1));
01622 else
01623 aml_node2->addAttribute("value", add_parens(bend.angle, 1) + "/" + add_parens(bend.rho, 2));
01624 }
01625
01626 } else {
01627 if (bend.l != "") {
01628 aml_node->addAttribute("attribute", ele_name + "[bend:g]");
01629 aml_node->addAttribute("value",
01630 add_parens(bend.angle, 1) + "/" + add_parens(bend.l, 2));
01631 } else {
01632 aml_node->addAttribute("attribute", ele_name + "[length]");
01633 if (bend.g != "")
01634 aml_node->addAttribute("value", add_parens(bend.angle, 1) + "/" + add_parens(bend.g, 2));
01635 else
01636 aml_node->addAttribute("value", add_parens(bend.angle, 1) + "*" + add_parens(bend.rho, 2));
01637 }
01638 }
01639 }
01640
01641
01642
01643
01644 if (x_class == "rbend" && x_attrib != "e1" && x_attrib != "e2") {
01645
01646 string l_str, g_str;
01647
01648 if (bend.l == "") {
01649 if (bend.g == "")
01650 l_str = add_parens(bend.angle, 1) + "*" + add_parens(bend.rho, 2);
01651 else
01652 l_str = add_parens(bend.angle, 1) + "/" + add_parens(bend.g, 2);
01653
01654 if (x_attrib == "g")
01655 g_str = bend.g;
01656 else if (x_attrib == "rho")
01657 g_str = "1/" + add_parens(bend.rho, 2);
01658
01659 } else if (bend.angle == "") {
01660 if (bend.g == "")
01661 l_str = "2*" + add_parens(bend.rho, 1) + "*asin(" + add_parens(bend.l, 1) +
01662 "/(2*" + add_parens(bend.rho, 1) + "))";
01663 else
01664 l_str = "2*asin(" + add_parens(bend.l, 1) + "*" + add_parens(bend.g, 1) +
01665 "/2)/(2*" + add_parens(bend.g, 1) + ")";
01666
01667 if (x_attrib == "g")
01668 g_str = bend.g;
01669 else if (x_attrib == "rho")
01670 g_str = "1/" + add_parens(bend.rho, 2);
01671
01672 } else {
01673 l_str = add_parens(bend.angle, 1) + "*" + add_parens(bend.l, 1) +
01674 "/(2*sin(" + add_parens(bend.angle, 1) + "/2))";
01675 g_str = "2*sin(" + add_parens(bend.angle, 1) + "/2)/" + add_parens(bend.l, 2);
01676
01677 }
01678
01679 UAPNode* l_node = aml_ele->addChild("set");
01680 l_node->addAttribute("attribute", ele_name + "[length]");
01681 l_node->addAttribute("value", l_str);
01682
01683 if (g_str != "") {
01684 UAPNode* g_node = aml_ele->addChild("set");
01685 g_node->addAttribute("attribute", ele_name + "[bend:g]");
01686 g_node->addAttribute("value", g_str);
01687 }
01688
01689 }
01690
01691
01692
01693 if (x_class == "rbend") {
01694
01695 bool angle_change = (x_attrib != "e1" && x_attrib != "e2" &&
01696 (x_attrib == "angle" || bend.angle == ""));
01697
01698 if (angle_change || x_attrib == "e1" || x_attrib == "e2") {
01699
01700 string angle2;
01701 if (bend.angle != "")
01702 angle2 = add_parens(bend.angle, 1) + "/2";
01703 else if (bend.g != "")
01704 angle2 = "asin(" + add_parens(bend.g, 1) + "*" + add_parens(bend.l, 1) + "/2)";
01705 else
01706 angle2 = "asin(" + add_parens(bend.l, 1) + "/(2*" + add_parens(bend.rho, 1) + ")";
01707
01708 if (angle_change || x_attrib == "e1") {
01709 UAPNode* aml_set_e1 = aml_ele->addChild("set");
01710 aml_set_e1->addAttribute("attribute", ele_name + "[bend:e1]");
01711 if (bend.e1 == "")
01712 aml_set_e1->addAttribute("value", angle2);
01713 else
01714 aml_set_e1->addAttribute("value", bend.e1 + " + " + angle2);
01715 }
01716
01717 if (angle_change || x_attrib == "e2") {
01718 UAPNode* aml_set_e2 = aml_ele->addChild("set");
01719 aml_set_e2->addAttribute("attribute", ele_name + "[bend:e2]");
01720 if (bend.e2 == "")
01721 aml_set_e2->addAttribute("value", angle2);
01722 else
01723 aml_set_e2->addAttribute("value", bend.e2 + " + " + angle2);
01724 }
01725
01726 }
01727
01728 }
01729
01730 return;
01731
01732 }
01733
01734
01735
01736
01737 if (x_node_type == "use") {
01738 use_parent = aml_ele;
01739 use_line = x_ele->getAttributeString("line");
01740 return;
01741 }
01742
01743
01744
01745 if (x_node_type == "line" || x_node_type == "list") {
01746 string str = "sector";
01747 if (x_node_type == "list") str = "list";
01748 UAPNode* aml_node = aml_ele->addChild(str);
01749 aml_node->addAttribute("name", x_ele->getAttributeString("name"));
01750 StrList arg_list;
01751 AU.getSectorArgList (x_ele, arg_list);
01752 x_to_aml_sector (x_ele, aml_node, arg_list);
01753 return;
01754 }
01755
01756
01757
01758 if (x_node_type == "title") {
01759 aml_node = aml_ele->addChild("comment");
01760 aml_node->addAttribute("type", "title");
01761 aml_node->addAttribute("text", x_ele->getAttributeString("text"));
01762 return;
01763 }
01764
01765
01766
01767 if (x_node_type == "constant") {
01768 aml_node = aml_ele->addChild("constant");
01769 aml_node->addAttribute ("name", x_ele->getAttributeString("name"));
01770 aml_node->addAttribute ("design",
01771 x_expression_to_aml(x_ele->getAttributeString("design")));
01772 string type = x_ele->getAttributeString("type");
01773 if (type != "") aml_node->addAttribute ("type", type);
01774 return;
01775 }
01776
01777
01778
01779 if (x_node_type == "call") {
01780 aml_node = aml_ele->addChild("{http://www.w3.org/2001/XInclude}xi:include");
01781 string str = x_ele->getAttributeString("filename");
01782 if (str != "") aml_node->addAttribute("href", str);
01783 x_rep_to_aml (x_ele, aml_node);
01784 return;
01785 }
01786
01787
01788
01789 if (x_node_type == "comment") {
01790 aml_node = aml_ele->addChild("comment");
01791 aml_node->addAttribute("text", x_ele->getAttributeString("text"));
01792 return;
01793 }
01794
01795
01796
01797 if (x_node_type == "parameter" || x_node_type == "beam" ||
01798 x_node_type == "beginning" || x_node_type == "beam_start") {
01799 x_param_to_aml (x_ele, aml_ele);
01800 return;
01801 }
01802
01803
01804
01805 UAPNode* aml_class_node;
01806
01807 if (x_node_type == "element") {
01808 string inherit;
01809 aml_node = aml_ele->addChild("element");
01810 ele_name = x_ele->getAttributeString("name");
01811 aml_node->addAttribute ("name", ele_name);
01812 name_to_aml_node_map[ele_name] = aml_node;
01813 string x_class = x_ele->getAttributeString("class");
01814
01815 if (x_class != "") {
01816
01817
01818 aml_class = x_class_to_aml_map[x_class];
01819 Twig twig;
01820 twig.fromString(aml_class);
01821 for (NodeIDVecCIter ni = twig.nodeid_vec.begin(); ni != twig.nodeid_vec.end(); ni++) {
01822 aml_class_node = aml_node->addChild(ni->name);
01823 for (AttributeIDVecCIter ai = ni->attribute.begin(); ai != ni->attribute.end(); ai++)
01824 aml_class_node->addAttribute(ai->name, ai->value);
01825 }
01826
01827 } else {
01828 inherit = x_ele->getAttributeString("inherit");
01829 aml_node->addAttribute ("inherit", inherit);
01830 }
01831
01832 x_element_attributes_to_aml (x_ele, aml_root, aml_node, ignore_this1, ignore_this2);
01833
01834
01835
01836
01837
01838 x_class = ele_name_to_x_class(ele_name);
01839
01840 if (x_class == "rbend" || x_class == "sbend") {
01841
01842 bend_struct& bend = bend_map[ele_name];
01843
01844 if (!found(bend_map, inherit)) inherit = "";
01845 bend_struct bend_base;
01846 if (inherit != "") {
01847 bend_base = bend_map[inherit];
01848 if (bend.l == "") bend.l = bend_base.l;
01849 if (bend.g == "") bend.g = bend_base.g;
01850 if (bend.rho == "") bend.rho = bend_base.rho;
01851 if (bend.angle == "") bend.angle = bend_base.angle;
01852 if (bend.e1 == "") bend.e1 = bend_base.e1;
01853 if (bend.e2 == "") bend.e2 = bend_base.e2;
01854 }
01855
01856 int n_defined(0);
01857 if (bend.l != "") n_defined++;
01858 if (bend.g != "") n_defined++;
01859 if (bend.rho != "") n_defined++;
01860 if (bend.angle != "") n_defined++;
01861 if (n_defined > 2 || (bend.rho != "" && bend.g != "")) {
01862 info_out.error ("Bend is overspecified! " + x_ele->toStringTree());
01863 return;
01864 }
01865
01866 UAPNode* bend_node = aml_node->getChildByName("bend", true);
01867
01868 if (bend_base.complete || n_defined == 2) bend.complete = true;
01869
01870
01871
01872 if (bend.complete) {
01873
01874 if (x_class == "sbend") {
01875 UAPNode* last_child = aml_root->getChildren().back();
01876
01877 string l_str = bend.l, g_str = bend.g;
01878
01879 if (bend.l == "") {
01880 if (bend.g == "") {
01881 l_str = add_parens(bend.angle, 1) + "*" + add_parens(bend.rho, 2);
01882 g_str = "1/" + add_parens(bend.rho, 2);
01883 } else {
01884 l_str = add_parens(bend.angle, 1) + "/" + add_parens(bend.g, 2);
01885 }
01886
01887 } else if (bend.angle == "") {
01888 if (bend.g == "") g_str = "1/" + add_parens(bend.rho, 2);
01889
01890 } else {
01891 g_str = add_parens(bend.angle, 1) + "/" + add_parens(bend.l, 2);
01892 }
01893
01894 aml_node->addChild("length")->addAttribute("design", l_str);
01895 bend_node->addChild("g")->addAttribute("design", g_str);
01896 }
01897
01898
01899
01900 if (x_class == "rbend") {
01901
01902 string l_str, g_str;
01903
01904 if (bend.l == "") {
01905 if (bend.g == "") {
01906 l_str = add_parens(bend.angle, 1) + "*" + add_parens(bend.rho, 2);
01907 g_str = "1/" + add_parens(bend.rho, 2);
01908 } else {
01909 l_str = add_parens(bend.angle, 1) + "/" + add_parens(bend.g, 2);
01910 g_str = bend.g;
01911 }
01912
01913 } else if (bend.angle == "") {
01914 if (bend.g == "") {
01915 l_str = "2*" + add_parens(bend.rho, 1) + "*asin(" + add_parens(bend.l, 1) +
01916 "/(2*" + add_parens(bend.rho, 1) + "))";
01917 g_str = "1/" + add_parens(bend.rho, 2);
01918 } else {
01919 l_str = "2*asin(" + add_parens(bend.l, 1) + "*" + add_parens(bend.g, 1) +
01920 "/2)/(2*" + add_parens(bend.g, 1) + ")";
01921 g_str = bend.g;
01922 }
01923
01924 } else {
01925 l_str = add_parens(bend.angle, 1) + "*" + add_parens(bend.l, 1) +
01926 "/(2*sin(" + add_parens(bend.angle, 1) + "/2))";
01927 g_str = "2*sin(" + add_parens(bend.angle, 1) + "/2)/" + add_parens(bend.l, 2);
01928
01929 }
01930
01931 bend_node->addChild("g")->addAttribute("design", g_str);
01932 aml_node->addChild("length")->addAttribute("design", l_str);
01933
01934
01935
01936 string angle2;
01937 if (bend.angle != "")
01938 angle2 = add_parens(bend.angle, 1) + "/2";
01939 else if (bend.g != "")
01940 angle2 = "asin(" + add_parens(bend.g, 1) + "*" + add_parens(bend.l, 1) + "/2)";
01941 else
01942 angle2 = "asin(" + add_parens(bend.l, 1) + "/(2*" + add_parens(bend.rho, 1) + "))";
01943
01944 if (bend.e1 == "")
01945 bend_node->addChild("e1")->addAttribute("design", angle2);
01946 else
01947 bend_node->addChild("e1")->addAttribute("design", bend.e1 + " + " + angle2);
01948
01949 if (bend.e2 == "")
01950 bend_node->addChild("e2")->addAttribute("design", angle2);
01951 else
01952 bend_node->addChild("e2")-> addAttribute("design", bend.e2 + " + " + angle2);
01953
01954 }
01955
01956 }
01957
01958 }
01959
01960 return;
01961
01962 }
01963
01964
01965
01966 info_out.error (language + " node not recognized: " + x_ele->toString());
01967 return;
01968
01969 }
01970
01971
01972
01973
01974 void TranslateCore::x_param_to_aml (UAPNode* x_param_node, UAPNode* aml_root) {
01975
01976
01977
01978 string x_param_class = x_param_node->getName();
01979
01980 AttributeVec alist = x_param_node->getAttributes();
01981 for (AttributeVecCIter ia = alist.begin(); ia != alist.end(); ia++) {
01982 UAPAttribute attrib = *ia;
01983
01984 string x_param_name = attrib.getName();
01985 string param_value = attrib.getValue();
01986
01987
01988
01989 if (x_param_name == "comment") continue;
01990
01991
01992
01993 for (ParamValueStructVecIter ip = param_values_list.begin();
01994 ip != param_values_list.end(); ip++) {
01995 if (ip->x_value == param_value) {
01996 param_value = ip->aml_value;
01997 break;
01998 }
01999 }
02000
02001
02002
02003 attribute_convert_struct attrib_info;
02004 if (!found_x_attrib(x_param_class, x_param_name, attrib_info)) {
02005 info_out.error ("Unknown parameter attribute: " + x_param_name, " Of: " + x_param_node->toString());
02006 continue;
02007 }
02008
02009 string aml_value = x_expression_to_aml(param_value, attrib_info);
02010
02011
02012
02013
02014
02015 string aml_param_class = attrib_info.aml_twig.name;
02016 Twig twig;
02017 twig.fromString(aml_param_class + "(name = '" + aml_param_class + "')");
02018 UAPNode* aml_class_node = twig.getSubNode (aml_lab_root);
02019 if (!(aml_class_node)) aml_class_node = aml_root->addChild(aml_param_class);
02020 aml_class_node->addAttribute("name", aml_param_class);
02021
02022
02023
02024 UAPNode* node = attrib_info.aml_twig.getLocalSubNode(aml_class_node, true);
02025 node->addAttribute(attrib_info.aml_twig.target_attribute, aml_value);
02026 }
02027
02028 }
02029
02030
02031
02032
02033 void TranslateCore::x_element_attributes_to_aml (UAPNode* x_node, UAPNode* aml_root,
02034 UAPNode* aml_node, const string& ignore_this1, const string& ignore_this2) {
02035
02036 bool ok;
02037 string ele_name = x_node->getAttributeString("name");
02038 string x_class = x_node_to_x_class (x_node);
02039
02040
02041
02042
02043 bool e1_found = false, e2_found = false, fintx_found = false, hgapx_found = false;
02044 string fint = "", hgap = "";
02045
02046 AttributeVec a_list = x_node->getAttributes();
02047 for (AttributeVecCIter ia = a_list.begin(); ia != a_list.end(); ia++) {
02048
02049 string x_attrib = ia->getName();
02050 if (x_attrib == ignore_this1 || x_attrib == ignore_this2) continue;
02051
02052 if (custom_x_element_attribute_to_aml (x_node, x_class, *ia, aml_node)) continue;
02053
02054 if (x_attrib == "name") continue;
02055 if (x_attrib == "comment") continue;
02056 if (x_attrib == "inherit") continue;
02057 if (x_attrib == "attribute") continue;
02058 if (x_attrib == "class") continue;
02059
02060 string value0 = ia->getValue();
02061
02062
02063
02064
02065 string a1 = x_attrib.substr(0,1);
02066 string a2 = x_attrib.substr(0,2);
02067 string a4 = x_attrib.substr(0,4);
02068
02069 if (x_class == "matrix" && (a2 == "rm" || a2 == "tm" || a4 == "kick")) {
02070
02071 int i, j, k;
02072 string i_str, exp = "0 0 0 0 0 0";
02073 bool ok1, ok2, ok3;
02074
02075 if (a4 == "kick") {
02076
02077 i_str = x_attrib.substr(4,1);
02078 i = string_to_int(i_str, ok1);
02079 if (!ok1 || i < 1 || i > 6) {
02080 info_out.error ("Malformed matrix kick attribute: " + x_attrib,
02081 "In: " + x_node->toString());
02082 return;
02083 }
02084 }
02085
02086 if (a2 == "rm") {
02087 i_str = x_attrib.substr(2,1);
02088 i = string_to_int(i_str, ok1);
02089 j = string_to_int(x_attrib.substr(3,1), ok2);
02090 if (!ok1 || i < 1 || i > 6 || !ok2 || j < 1 || j > 6) {
02091 info_out.error ("Malformed matrix rm attribute: " + x_attrib,
02092 "In: " + x_node->toString());
02093 return;
02094 }
02095 exp.replace(2*j-2, 1, "1");
02096 }
02097
02098 if (a2 == "tm") {
02099 i_str = x_attrib.substr(2,1);
02100 i = string_to_int(i_str, ok1);
02101 j = string_to_int(x_attrib.substr(3,1), ok2);
02102 k = string_to_int(x_attrib.substr(4,1), ok3);
02103 if (!ok1 || i < 1 || i > 6 || !ok2 || j < 1 || j > 6 || !ok3 || k < 0 || k > 6) {
02104 info_out.error ("Malformed matrix tm attribute: " + x_attrib,
02105 "In: " + x_node->toString());
02106 return;
02107 }
02108 exp.replace(2*j-2, 1, "1");
02109 if (k == j)
02110 exp.replace(2*k-2, 1, "2");
02111 else
02112 exp.replace(2*k-2, 1, "1");
02113
02114 }
02115
02116 string value = x_expression_to_aml(value0);
02117
02118 UAPNode* term = aml_node->getChildByName("taylor_map")->addChild("term");
02119 term->addAttribute("i_out", i_str);
02120 term->addAttribute("coef", value);
02121 term->addAttribute("exp", exp);
02122
02123 continue;
02124
02125 }
02126
02127
02128
02129
02130 attribute_convert_struct attrib_info;
02131 if (!found_x_attrib(x_class, x_attrib, attrib_info)) {
02132 info_out.error ("Unknown element attribute: " + x_attrib, "Of: " + x_node->toString());
02133 continue;
02134 }
02135
02136 if (attrib_info.ignore_x_to_aml) continue;
02137
02138 string value = x_expression_to_aml(value0, attrib_info, ele_name);
02139
02140
02141
02142 if (x_class == "sbend" || x_class == "rbend") {
02143
02144 UAPNode* last_child = aml_root->getChildren().back();
02145
02146 if (x_attrib == "l") {
02147 bend_map[ele_name].l = value;
02148 continue;
02149 }
02150
02151 if (x_attrib == "angle") {
02152 bend_map[ele_name].angle = value;
02153 continue;
02154 }
02155
02156 if (x_attrib == "g") {
02157 bend_map[ele_name].g = value;
02158 continue;
02159 }
02160
02161 if (x_attrib == "rho") {
02162 bend_map[ele_name].rho = value;
02163 continue;
02164 }
02165
02166 if (x_class == "rbend") {
02167
02168 if (x_attrib == "e1") {
02169 bend_map[ele_name].e1 = value;
02170 continue;
02171 }
02172
02173 if (x_attrib == "e2" ) {
02174 bend_map[ele_name].e2 = value;
02175 continue;
02176 }
02177
02178 }
02179
02180 }
02181
02182
02183
02184
02185 if (value == "") {
02186 if (x_attrib == "tilt") {
02187 if (x_class == "rbend" || x_class == "sbend") value = "pi/2";
02188 if (x_class == "quadrupole") value = "pi/4";
02189 if (x_class == "sextupole") value = "pi/6";
02190 if (x_class == "octupole") value = "pi/8";
02191 if (x_class == "sol_quad") value = "pi/4";
02192 }
02193
02194 if (x_class == "multipole") {
02195 bool ok;
02196 int n = string_to_int(x_attrib.substr(1), ok);
02197 value = "pi / " + int_to_string((n + 1) * 2, ok);
02198 }
02199 }
02200
02201 if (x_attrib == "fint") {
02202 if (value == "") value = "0.5";
02203 fint = value;
02204 }
02205
02206 if (x_attrib == "hgap") hgap = value;
02207
02208 if (x_attrib == "fintx") fintx_found = true;
02209 if (x_attrib == "hgapx") hgapx_found = true;
02210 if (x_attrib == "e1") e1_found = true;
02211 if (x_attrib == "e2") e2_found = true;
02212
02213
02214
02215
02216 UAPNode* a_node = attrib_info.aml_twig.getLocalSubNode(aml_node, true);
02217
02218
02219
02220 a_node->addAttribute(attrib_info.aml_twig.target_attribute, value);
02221
02222 }
02223
02224
02225
02226
02227 if (!fintx_found && fint != "") {
02228 UAPNode* bend_node = aml_node->getChildByName("bend", true);
02229 bend_node->addChild("f_int2")->addAttribute("design", fint);
02230 }
02231
02232 if (!hgapx_found && hgap != "") {
02233 UAPNode* bend_node = aml_node->getChildByName("bend", true);
02234 bend_node->addChild("h_gap2")->addAttribute("design", hgap);
02235 }
02236
02237
02238
02239 custom2_x_element_attribute_to_aml (x_node, aml_node);
02240
02241 return;
02242
02243 }
02244
02245
02246
02247
02248 string TranslateCore::x_node_to_x_class (UAPNode* x_ele) {
02249
02250
02251
02252
02253 if (x_ele->getName() != "element") return x_ele->getName();
02254
02255
02256
02257 if (x_ele->getAttribute("class")) return x_ele->getAttributeString("class");
02258
02259
02260
02261 string inherit = x_ele->getAttributeString("inherit");
02262
02263 string x_class = ele_name_to_x_class(inherit);
02264 if (x_class == "")
02265 info_out.error ("Class name cannot be resolved for element: " + x_ele->toString());
02266
02267 return x_class;
02268
02269 }
02270
02271
02272
02273
02274 bool TranslateCore::x_to_aml_sector (UAPNode* x_line, UAPNode* aml_sector, StrList& arg_list) {
02275
02276
02277
02278 UAPAttribute* attrib;
02279
02280 transfer_attribute(x_line, "args", aml_sector, "args");
02281 transfer_attribute(x_line, "multipass", aml_sector, "multipass");
02282
02283
02284
02285 NodeVec x_eles = x_line->getChildren();
02286 for (NodeVecCIter nt = x_eles.begin(); nt != x_eles.end(); nt++) {
02287 UAPNode* x_ele = *nt;
02288 string ref_str = x_ele->getAttributeString("ref");
02289 string ele_class = x_ele->getName();
02290 if (ele_class == "item") {
02291 if (found(name_to_x_type_map, ref_str)) {
02292 ele_class = name_to_x_type_map[ref_str];
02293 if (ele_class == "list") ele_class = "element";
02294 } else if (found(arg_list, ref_str)) {
02295 ele_class = "arg";
02296 } else {
02297 info_out.error ("CANNOT CONNECT NAME TO A LINE, LIST ELEMENT OR SEQUENCE: ",
02298 x_ele->toString());
02299 return false;
02300 }
02301 }
02302
02303 UAPNode* aml_ele;
02304 string args = x_ele->getAttributeString("args");
02305
02306
02307
02308 if (ele_class == "element" || ele_class == "arg") {
02309 aml_ele = aml_sector->addChild("element");
02310 aml_ele->addAttribute("ref", ref_str);
02311
02312
02313
02314
02315 } else if (ele_class == "line") {
02316 if (args == "") {
02317 aml_ele = aml_sector->addChild("sector");
02318 if (ref_str != "") aml_ele->addAttribute("ref", ref_str);
02319 x_to_aml_sector (x_ele, aml_ele, arg_list);
02320 } else {
02321 aml_ele = aml_sector->addChild("sector");
02322 aml_ele->addAttribute("ref", ref_str);
02323 aml_ele->addAttribute("args", args);
02324 }
02325
02326 } else {
02327 info_out.error ("Unknown class: " + x_ele->toString());
02328 return false;
02329 }
02330
02331 if (attrib = x_ele->getAttribute("tag"))
02332 aml_ele->addAttribute("tag", attrib->getValue());
02333
02334 string repeat = x_ele->getAttributeString("repeat");
02335 if (repeat != "") {
02336 if (repeat.substr(0,1) == "-") {
02337 aml_ele->addAttribute("reflection", "TRUE");
02338 repeat.erase(0,1);
02339 }
02340 if (repeat != "1") aml_ele->addAttribute("repeat", repeat);
02341 }
02342
02343 }
02344
02345 return true;
02346
02347 }
02348
02349
02350
02351
02352 string TranslateCore::x_expression_to_aml (const string& x_expression,
02353 attribute_convert_struct attrib_info, const string& ele_name) {
02354
02355
02356
02357
02358 for (ParamValueStructVecIter ip = param_values_list.begin();
02359 ip != param_values_list.end(); ip++) {
02360 if (ip->x_value == x_expression) return ip->aml_value;
02361 }
02362
02363
02364
02365
02366
02367
02368 size_t i0, i1, i2, n;
02369 string aml_exp = x_expression;
02370
02371
02372
02373 if (c_style_format) {
02374 while (true) {
02375 if ((i1 = aml_exp.find("->")) == string::npos) break;
02376 aml_exp.replace(i1, 2, "[");
02377 }
02378 }
02379
02380
02381
02382 i0 = 0;
02383 while (true) {
02384 string token = next_token_in_expression(aml_exp, c_style_format, i0, n);
02385 if (i0 == aml_exp.size()) break;
02386
02387
02388
02389 if ((i1 = token.find("[")) != string::npos) {
02390
02391
02392 if (c_style_format) {
02393 i2 = token.size();
02394 } else {
02395 if (token.find("]") != token.size() - 1) {
02396 info_out.error ("No matching ']' for opening '[' in expression: " + x_expression);
02397 return "";
02398 }
02399 i2 = token.size() - 1;
02400 }
02401
02402 string this_ele_name = token.substr(0, i1);
02403 string x_attrib = token.substr(i1+1, i2-i1-1);
02404
02405 string x_class = ele_name_to_x_class(this_ele_name, true);
02406 if (x_class == "") {
02407 info_out.error ("Class name cannot be resolved for: " + this_ele_name,
02408 "In expression: " + x_expression);
02409 return aml_exp;
02410 }
02411
02412 attribute_convert_struct at_info;
02413 if (!found_x_attrib(x_class, x_attrib, at_info)) {
02414 info_out.error ("Unknown attribute: " + x_attrib, "In expression: " + x_expression);
02415 return aml_exp;
02416 }
02417
02418
02419
02420
02421 if (at_info.aml_twig.name != "")
02422 this_ele_name = at_info.aml_twig.name;
02423
02424
02425
02426
02427
02428 aml_exp.erase(i0, n);
02429 string aml_attrib = this_ele_name + "[" + at_info.aml_twig.toNodeString() + "]";
02430 aml_attrib = convert_single_attribute (aml_attrib,
02431 at_info.aml_to_x_template, at_info.x_to_aml_template, this_ele_name);
02432 aml_attrib = add_parens(aml_attrib, aml_exp, i0);
02433 aml_exp.insert(i0, aml_attrib);
02434 i0 += aml_attrib.size();
02435
02436
02437
02438
02439 } else {
02440
02441 bool found = false;
02442 for (ConstantsVecIter ic = constants_list.begin(); ic != constants_list.end(); ic++) {
02443 if (ic->ignore_x_to_aml) continue;
02444 if (ic->x_const == token) {
02445 string aml_const = ic->x_to_aml_template;
02446 if (aml_const == "") aml_const = ic->aml_const;
02447 aml_exp.erase(i0, n);
02448 aml_const = add_parens(aml_const, aml_exp, i0);
02449 aml_exp.insert(i0, aml_const);
02450 i0 += aml_const.size();
02451 found = true;
02452 break;
02453 }
02454 }
02455
02456 if (!found) i0 += token.size();
02457
02458 }
02459 }
02460
02461
02462
02463
02464 return convert_single_attribute (aml_exp, attrib_info.x_to_aml_template,
02465 attrib_info.aml_to_x_template, ele_name);
02466 }
02467
02468
02469
02470
02471 UAPNode* TranslateCore::AMLRepToXFile(UAPNode* aml_root,
02472 const string& file_name, const Str& out_suffix, bool one_file ) {
02473
02474 UAPNode* x_rep = AMLRepToXRep (aml_root);
02475 if (!x_rep) return false;
02476 return XRepToXFile (x_rep, file_name, out_suffix, one_file);
02477
02478 }
02479
02480
02481
02482
02483 UAPNode* TranslateCore::AMLRepToXRep(UAPNode* aml_root, UAPNode* x_root) {
02484
02485
02486
02487 name_to_x_class_map.clear();
02488 info_out.ix_line = -1;
02489 info_out.statement = "";
02490 info_out.parsing_status = "Translating from AML rep to " + language + " rep.";
02491
02492
02493
02494 UAPNode* aml_rep;
02495 UAPNode* uap_root = NULL;
02496
02497 if (aml_root->getName() == "UAP_root") {
02498 uap_root = aml_root;
02499 aml_rep = aml_root->getChildByName("AML_representation");
02500 if (!aml_rep) {
02501 info_out.error ("<UAP_root> does not have a <AML_representation> child.");
02502 return NULL;
02503 }
02504 } else if (aml_root->getName() == "AML_representation") {
02505 aml_rep = aml_root;
02506 uap_root = aml_root->getParent();
02507 } else {
02508 info_out.error ("aml_root argument is not a <UAP_root> node nor a <AML_representation> node",
02509 aml_root->toString());
02510 return NULL;
02511 }
02512
02513
02514
02515 UAPNode* x_rep = x_root;
02516
02517 if (!x_rep) {
02518 if (uap_root) {
02519 NodeVec reps = uap_root->getChildrenByName(language + "_representation");
02520 for (NodeVecIter ir = reps.begin(); ir != reps.end(); ir++) (*ir)->deleteNode();
02521 x_rep = uap_root->addChild(language + "_representation");
02522 } else {
02523 x_rep = new UAPNode(language + "_representation");
02524 }
02525
02526 } else if (x_rep->getName() == "UAP_root") {
02527 NodeVec reps = x_rep->getChildrenByName(language + "_representation");
02528 for (NodeVecIter ir = reps.begin(); ir != reps.end(); ir++) (*ir)->deleteNode();
02529 x_rep = x_rep->addChild(language + "_representation");
02530
02531 } else if (x_rep->getName() == language + "_representation") {
02532 UAPNode* root = x_rep->getParent();
02533 x_rep->deleteNode();
02534 if (root) x_rep = root->addChild(language + "_representation");
02535 else x_rep = new UAPNode(language + "_representation");
02536
02537 } else {
02538 info_out.error ("Argument is not a <UAP_root> node nor a <" + language + "_representation> node",
02539 x_root->toString());
02540 return NULL;
02541 }
02542
02543
02544
02545 x_rep->addAttribute ("source_language", "AML");
02546
02547 if (aml_rep->getAttribute("filename"))
02548 x_rep->addAttribute("filename", aml_rep->getAttributeString("filename"));
02549
02550
02551
02552 UAPNode* lab = aml_rep->getChildByName("laboratory");
02553 if (!lab) {
02554 info_out.error ("no <laboratory> child found for " + aml_rep->getName() + " node.");
02555 return NULL;
02556 }
02557
02558
02559
02560 aml_rep_to_x_init (lab);
02561 aml_rep_to_x (lab, x_rep);
02562 custom_aml_rep_to_x (lab, x_rep);
02563
02564 return x_rep;
02565
02566 }
02567
02568
02569
02570
02571 void TranslateCore::aml_rep_to_x_init (UAPNode* aml_rep) {
02572
02573
02574
02575 name_to_aml_type_map.clear();
02576 name_to_x_type_map.clear();
02577 name_to_x_node_map.clear();
02578 name_to_aml_node_map.clear();
02579 aml_set_nodes.clear();
02580 aml_k_map.clear(); aml_ks_map.clear(); aml_tilt_map.clear();
02581
02582 aml_rep_make_name_lists (aml_rep);
02583
02584 }
02585
02586
02587
02588
02589 void TranslateCore::aml_rep_make_name_lists (UAPNode* aml_rep) {
02590
02591
02592
02593 NodeVec list = aml_rep->getChildren();
02594 for (NodeVecCIter it = list.begin(); it != list.end(); it++) {
02595 UAPNode* node = *it;
02596
02597 string node_name = node->getName();
02598
02599 if (node_name == "set") {
02600 aml_set_nodes.push_back(node);
02601 continue;
02602 }
02603
02604 string name = node->getAttributeString("name");
02605 aml_rep_make_name_lists (node);
02606 if (name == "") continue;
02607 name_to_aml_node_map[name] = node;
02608
02609 if (node_name == "sector" || node_name == "list" || node_name == "sequence") {
02610 name_to_aml_type_map[name] = node_name;
02611 continue;
02612 }
02613
02614 if (node_name == "lattice" || node_name == "beam" || node_name == "parameter") {
02615 name_to_aml_type_map[name] = node_name;
02616 continue;
02617 }
02618
02619 if (node_name == "controller") {
02620 string x_class;
02621 aml_ele_to_x_class (node, x_class);
02622 name_to_aml_type_map[name] = x_class;
02623 name_to_x_class_map[name] = x_class;
02624 continue;
02625 }
02626
02627 if (node_name == "element") {
02628 name_to_aml_type_map[name] = node_name;
02629 string inherit = node->getAttributeString("inherit");
02630 string x_class;
02631 if (inherit != "") {
02632 x_class = ele_name_to_x_class(inherit);
02633 } else {
02634 if (!aml_ele_to_x_class(node, x_class)) continue;
02635 }
02636 name_to_x_class_map[name] = x_class;
02637 continue;
02638 }
02639
02640 }
02641
02642 }
02643
02644
02645
02646
02647 void TranslateCore::aml_rep_to_x (UAPNode* lab, UAPNode* x_rep) {
02648
02649
02650
02651 NodeVec eles = lab->getChildren();
02652 for (NodeVecCIter ie = eles.begin(); ie != eles.end(); ie++) {
02653 UAPNode* aml_ele = *ie;
02654 aml_node_to_x (aml_ele, x_rep);
02655 }
02656
02657 }
02658
02659
02660
02661
02662 void TranslateCore::aml_node_to_x (UAPNode* aml_ele, UAPNode* x_root, const Str& ignore_this_attribute) {
02663
02664 string aml_name = aml_ele->getName();
02665 bool ok;
02666
02667
02668
02669 if (custom_aml_node_to_x (aml_ele, x_root)) return;
02670
02671
02672
02673 if (aml_name == "include") {
02674 string file = aml_ele->getAttributeString("href");
02675 if (file == "") {
02676 info_out.error ("No href attribute associated with <include> node.");
02677 }
02678 UAPNode* x_ele = x_root->addChild("call");
02679 x_ele->addAttribute ("filename", file);
02680 UAPNode* sub_lab = aml_ele->getChildByName("laboratory");
02681 if (sub_lab) aml_ele = sub_lab;
02682 aml_rep_to_x (aml_ele, x_ele);
02683 return;
02684 }
02685
02686
02687
02688 if (aml_name == "comment") {
02689 UAPNode* x_ele = x_root->addChild("comment");
02690 x_ele->addAttribute ("text", aml_ele->getAttributeString("text"));
02691 return;
02692 }
02693
02694
02695
02696 if (aml_name == "doc") return;
02697
02698
02699
02700 if (aml_name == "constant") {
02701 UAPNode* x_ele = x_root->addChildCopy(aml_ele);
02702 UAPAttribute* design_attrib = x_ele->getAttribute("design");
02703 if (!design_attrib) {
02704 info_out.error ("No associated 'design' attribute in <constant> node.", x_ele->toString());
02705 return;
02706 }
02707 string value = aml_expression_to_x(design_attrib->getValue());
02708 design_attrib->setValue(value);
02709 return;
02710 }
02711
02712
02713
02714 if (aml_name == "set" || aml_name == "string_set") {
02715 Twig twig;
02716 if (!twig.fromString(aml_ele->getAttributeString("attribute"))) return;
02717 string ele_name = twig.name;
02718 string aml_attribute = twig.toNodeString(true);
02719
02720 string x_class = ele_name_to_x_class(ele_name);
02721
02722
02723
02724 if (twig.nodeid_vec.size() == 2) {
02725 string n0 = twig.nodeid_vec[0].name, n1 = twig.nodeid_vec[1].name;
02726 if ((n0 == "quadrupole" || n0 == "sextupole" || n0 == "octupole") &&
02727 knl_tn_style_multipoles && (n1 == "k" || n1 == "ks" || n1 == "tilt")) {
02728 string k_set, ks_set, tilt_set, n_str, value;
02729 value = aml_ele->getAttributeString("value");
02730 if (n1 == "k") k_set = value;
02731 else if (n1 == "ks") ks_set = value;
02732 else if (n1 == "tilt") tilt_set = value;
02733
02734 if (n0 == "quadrupole") n_str = "1";
02735 else if (n0 == "sextupole") n_str = "2";
02736 else if (n0 == "octupole") n_str = "3";
02737
02738 string k_ele, ks_ele, tilt_ele;
02739 if (found (aml_k_map, ele_name)) {
02740 k_ele = aml_k_map[ele_name];
02741 ks_ele = aml_ks_map[ele_name];
02742 tilt_ele = aml_tilt_map[ele_name];
02743 required_multipole_transfer ("tilt", k_ele, ks_ele, tilt_ele,
02744 k_set, ks_set, tilt_set);
02745 }
02746
02747 string k, tilt;
02748 multipole_to_k_tilt (k_set, ks_set, tilt_set, n_str, k, tilt);
02749
02750
02751 if (k != "") {
02752 UAPNode* x_set = x_root->addChild("set");
02753 x_set->addAttribute("element", ele_name);
02754 x_set->addAttribute("attribute", "k" + n_str);
02755 x_set->addAttribute("value", k);
02756 }
02757 if (tilt != "") {
02758 UAPNode* x_set = x_root->addChild("set");
02759 x_set->addAttribute("element", ele_name);
02760 x_set->addAttribute("attribute", "tilt");
02761 x_set->addAttribute("value", tilt);
02762 }
02763
02764
02765 if (n1 == "kl") aml_k_map[ele_name] = value;
02766 else if (n1 == "ksl") aml_ks_map[ele_name] = value;
02767 else if (n1 == "tilt") aml_tilt_map[ele_name] = value;
02768
02769 return;
02770 }
02771 }
02772
02773
02774
02775 if (twig.nodeid_vec.size() == 2) {
02776 string n0 = twig.nodeid_vec[0].name, n1 = twig.nodeid_vec[1].name;
02777 if (n0 == "multipole" &&
02778 knl_tn_style_multipoles && (n1 == "kl" || n1 == "ksl" || n1 == "tilt")) {
02779 string n = twig.nodeid_vec[1].attribute[0].value;
02780 int n_int = string_to_int(n, ok);
02781 if (!ok) {
02782 info_out.error ("Bad multipole integer order: " + n,
02783 "In AML element:", aml_ele->toStringTree());
02784 return;
02785 }
02786 string kl_set, ksl_set, tilt_set, n_str, value;
02787 value = aml_ele->getAttributeString("value");
02788 if (n1 == "kl") kl_set = value;
02789 else if (n1 == "ksl") ksl_set = value;
02790 else if (n1 == "tilt") tilt_set = value;
02791
02792 string kl_ele, ksl_ele, tilt_ele;
02793 if (found (aml_multi_tilt_map, ele_name)) {
02794 kl_ele = aml_multi_kl_map[ele_name][n_int];
02795 ksl_ele = aml_multi_ksl_map[ele_name][n_int];
02796 tilt_ele = aml_multi_tilt_map[ele_name][n_int];
02797 required_multipole_transfer ("tilt", kl_ele, ksl_ele, tilt_ele,
02798 kl_set, ksl_set, tilt_set);
02799 } else {
02800 aml_multi_kl_map[ele_name] = StrVec(N_MULTIPOLE);
02801 aml_multi_ksl_map[ele_name] = StrVec(N_MULTIPOLE);
02802 aml_multi_tilt_map[ele_name] = StrVec(N_MULTIPOLE);
02803 }
02804
02805
02806 string knl, tilt;
02807 multipole_to_k_tilt (kl_set, ksl_set, tilt_set, n, knl, tilt);
02808
02809 if (knl != "") {
02810 UAPNode* x_set = x_root->addChild("set");
02811 x_set->addAttribute("element", ele_name);
02812 x_set->addAttribute("attribute", "k" + n + "l");
02813 x_set->addAttribute("value", knl);
02814 }
02815
02816 if (tilt != "") {
02817 UAPNode* x_set = x_root->addChild("set");
02818 x_set->addAttribute("element", ele_name);
02819 x_set->addAttribute("attribute", "t" + n);
02820 x_set->addAttribute("value", tilt);
02821 }
02822
02823
02824 if (n1 == "kl") aml_multi_kl_map[ele_name][n_int] = value;
02825 else if (n1 == "ksl") aml_multi_ksl_map[ele_name][n_int] = value;
02826 else if (n1 == "tilt") aml_multi_tilt_map[ele_name][n_int] = value;
02827
02828 return;
02829 }
02830
02831 }
02832
02833
02834
02835
02836 attribute_convert_struct attrib_info;
02837
02838 if (!found_aml_attrib(x_class, aml_attribute, attrib_info)) {
02839 if (AMLUtilities::inAMLNameSpace(twig.nodeid_vec.back().xml_uri))
02840 info_out.error ("Unknown attribute: " + aml_attribute, "Of: " + aml_ele->toString());
02841 return;
02842 }
02843 if (attrib_info.ignore_aml_to_x) return;
02844
02845 string value;
02846 if (aml_name == "set")
02847 value = aml_ele->getAttributeString("value");
02848 else
02849 value = aml_ele->getAttributeString("string");
02850
02851 value = aml_expression_to_x(value, attrib_info, ele_name);
02852 UAPNode* x_ele = x_root->addChild("set");
02853 x_ele->addAttribute("element", ele_name);
02854 x_ele->addAttribute("attribute", attrib_info.x_name);
02855 x_ele->addAttribute("value", value);
02856 return;
02857 }
02858
02859
02860
02861
02862 if (aml_name == "machine") {
02863 NodeVec ch = aml_ele->getChildren();
02864 string name;
02865 for (NodeVecCIter ic = ch.begin(); ic != ch.end(); ic++) {
02866 UAPNode* sector = *ic;
02867 if (sector->getName() != "sector") continue;
02868 if (sector->getAttributeString("ref") != "") {
02869 name = sector->getAttributeString("ref");
02870 } else if (sector->getAttributeString("name") != "") {
02871 name = sector->getAttributeString("name");
02872 } else {
02873 info_out.error("Sector without ref or name attribute found in machine: " +
02874 aml_ele->toString());
02875 continue;
02876 }
02877 x_root->addChild("use")->addAttribute("line", name);
02878 break;
02879 }
02880
02881 if (name == "") {
02882 info_out.error("No sector found for machine: " + aml_ele->toString());
02883 return;
02884 }
02885
02886 aml_rep_to_x (aml_ele, x_root);
02887 return;
02888 }
02889
02890
02891
02892 if (aml_name == "beam" || aml_name == "floor" || aml_name == "twiss" ||
02893 aml_name == "geometry" || aml_name == "parameter" || aml_name == "lattice") {
02894 aml_param_to_x (aml_ele, aml_ele, x_root);
02895 return;
02896 }
02897
02898
02899
02900 if (aml_name == "sector" || aml_name == "list") {
02901 if (aml_ele->getAttribute("name"))
02902 aml_sector_or_element_to_x (aml_ele, x_root);
02903 return;
02904 }
02905
02906
02907
02908 if (aml_name == "element") {
02909
02910 if (!aml_ele->getAttribute("name")) return;
02911
02912 UAPNode* x_ele = x_root->addChild("element");
02913 string ele_name = aml_ele->getAttributeString("name");
02914 x_ele->addAttribute("name", ele_name);
02915 string inherit = aml_ele->getAttributeString("inherit");
02916 name_to_x_node_map[ele_name] = x_ele;
02917 if (inherit == "") {
02918 x_ele->addAttribute("class", name_to_x_class_map[ele_name]);
02919 } else {
02920 x_ele->addAttribute("inherit", inherit);
02921 }
02922
02923 aml_element_attributes_to_x (aml_ele, aml_ele, x_ele, ignore_this_attribute);
02924 string x_class = name_to_x_class_map[ele_name];
02925
02926
02927
02928 string fint = x_ele->getAttributeString("fint");
02929 string fintx = x_ele->getAttributeString("fintx");
02930 if (fint == fintx && fint != "") x_ele->removeAttribute("fintx");
02931
02932 string hgap = x_ele->getAttributeString("hgap");
02933 string hgapx = x_ele->getAttributeString("hgapx");
02934 if (hgap == hgapx && hgap != "") x_ele->removeAttribute("hgapx");
02935
02936
02937
02938
02939
02940 if (knl_tn_style_multipoles) {
02941
02942 if ((found(aml_k_map, ele_name) || found(aml_ks_map, ele_name) || found(aml_tilt_map, ele_name)) &&
02943 (x_class == "quadrupole" || x_class == "sextupole" || x_class == "octupole")) {
02944
02945
02946
02947
02948 string inherit = x_ele->getAttributeString("inherit");
02949 if (found(aml_ks_map, inherit)) inherit = "";
02950
02951 if (inherit != "")
02952 required_multipole_transfer("ks",
02953 aml_k_map[inherit], aml_ks_map[inherit], aml_tilt_map[inherit],
02954 aml_k_map[ele_name], aml_ks_map[ele_name], aml_tilt_map[ele_name]);
02955
02956 string k, tilt, n;
02957
02958 if (x_class == "quadrupole") n = "1";
02959 else if (x_class == "sextupole") n = "2";
02960 else if (x_class == "octupole") n = "3";
02961
02962 multipole_to_k_tilt (aml_k_map[ele_name], aml_ks_map[ele_name],
02963 aml_tilt_map[ele_name], n, k, tilt);
02964 if (k != "") x_ele->addAttribute("k" + n, k);
02965 if (tilt != "") x_ele->addAttribute("tilt", tilt);
02966
02967
02968
02969 if (inherit != "") {
02970 if (aml_k_map[ele_name] == "") aml_k_map[ele_name] = aml_k_map[inherit];
02971 if (aml_ks_map[ele_name] == "") aml_ks_map[ele_name] = aml_ks_map[inherit];
02972 if (aml_tilt_map[ele_name] == "") aml_tilt_map[ele_name] = aml_tilt_map[inherit];
02973 }
02974
02975 }
02976
02977
02978
02979 if (x_class == "multipole" && (found(aml_multi_kl_map, ele_name) ||
02980 found(aml_multi_ksl_map, ele_name) || found(aml_multi_tilt_map, ele_name))) {
02981
02982 string inherit = x_ele->getAttributeString("inherit");
02983 if (aml_multi_kl_map.find(inherit) == aml_multi_kl_map.end()) inherit = "";
02984
02985
02986 for (int n = 0; n < N_MULTIPOLE; n++) {
02987 string kl_base, ksl_base, tilt_base, kl_ele, ksl_ele, tilt_ele;
02988 if (inherit != "") {
02989 kl_base = aml_multi_kl_map[inherit][n];
02990 ksl_base = aml_multi_ksl_map[inherit][n];
02991 tilt_base = aml_multi_tilt_map[inherit][n];
02992 }
02993 kl_ele = aml_multi_kl_map[ele_name][n];
02994 ksl_ele = aml_multi_ksl_map[ele_name][n];
02995 tilt_ele = aml_multi_tilt_map[ele_name][n];
02996 string kl, tilt, n_str = int_to_string(n);
02997 if (inherit != "") required_multipole_transfer ("ks",
02998 kl_base, ksl_base, tilt_base, kl_ele, ksl_ele, tilt_ele);
02999
03000 multipole_to_k_tilt (kl_ele, ksl_ele, tilt_ele, n_str, kl, tilt);
03001 if (kl != "") x_ele->addAttribute("k" + n_str + "l", kl);
03002 if (tilt != "") x_ele->addAttribute("t" + n_str, tilt);
03003
03004 if (inherit != "") {
03005 if (kl_ele == "") aml_multi_kl_map[ele_name][n] = kl_base;
03006 if (ksl_ele == "") aml_multi_ksl_map[ele_name][n] = ksl_base;
03007 if (tilt_ele == "") aml_multi_tilt_map[ele_name][n] = tilt_base;
03008 }
03009
03010 }
03011
03012 }
03013 }
03014
03015
03016
03017
03018
03019
03020
03021
03022 AttributeVec& attribs = x_ele->getAttributes();
03023 for (AttributeVecIter ia = attribs.begin(); ia != attribs.end(); ia++) {
03024 string value = ia->getValue();
03025 size_t i0(0), n, ;
03026 int ns = ele_name.size();
03027 bool changed(false);
03028 while (true) {
03029 string token = next_token_in_expression(value, c_style_format, i0, n);
03030 if (i0 == value.size()) break;
03031
03032 string attrib;
03033 if (c_style_format) {
03034 if (token.substr(0,ns) == ele_name && token.substr(ns,2) == "->")
03035 attrib = token.substr(ns+2);
03036 } else {
03037 if (token.substr(0,ns) == ele_name && token.substr(ns,1) == "[") {
03038 attrib = token.substr(ns+1);
03039 attrib.erase(attrib.size()-1);
03040 }
03041 }
03042
03043 if (attrib == "") {
03044 i0 += n;
03045 continue;
03046 }
03047
03048 string sub_value = x_ele->getAttributeString(attrib);
03049 if (sub_value == "") continue;
03050 value.erase(i0, n);
03051 sub_value = add_parens(sub_value, value, i0);
03052 value.insert(i0, sub_value);
03053 i0 += sub_value.size();
03054 changed = true;
03055 }
03056
03057 if (changed) ia->setValue(value);
03058
03059 }
03060
03061 return;
03062 }
03063
03064
03065
03066 if (AMLUtilities::inAMLNameSpace(aml_ele))
03067 info_out.error ("AML node not recognized: " + aml_ele->toString());
03068
03069 return;
03070
03071 }
03072
03073
03074
03075
03076 void TranslateCore::aml_param_to_x (UAPNode* aml_root, UAPNode* aml_param, UAPNode* x_rep) {
03077
03078 UAPNode* x_param_root;
03079
03080
03081
03082
03083
03084
03085
03086 NodeVec children = aml_param->getChildren();
03087 if (children.size() > 0) {
03088
03089 for (NodeVecCIter ib = children.begin(); ib != children.end(); ib++)
03090 aml_param_to_x (aml_root, *ib, x_rep);
03091
03092 return;
03093
03094
03095
03096 } else {
03097
03098
03099
03100 if (aml_param == aml_root) return;
03101
03102
03103
03104 attribute_convert_struct attrib_info;
03105 if (!found_aml_attrib("", aml_root, aml_param, "", attrib_info)) {
03106 if (AMLUtilities::inAMLNameSpace(aml_param))
03107 info_out.error ("No corresponding " + language + " parameter for AML parameter: " +
03108 aml_param->getName(),
03109 "For: " + aml_param->getParent()->toStringTree());
03110
03111 return;
03112 }
03113 if (attrib_info.ignore_aml_to_x) return;
03114
03115 string value = aml_param->getAttributeString(attrib_info.aml_twig.target_attribute);
03116 if (value == "")
03117 info_out.error ("For AML parameter: " + aml_param->toString(),
03118 "expected AML attribute: " + attrib_info.aml_twig.target_attribute);
03119
03120 value = aml_expression_to_x (value, attrib_info);
03121
03122
03123
03124 x_param_root = x_rep->addChild(attrib_info.x_parent);
03125
03126 for (ParamValueStructVecIter ip = param_values_list.begin();
03127 ip != param_values_list.end(); ip++) {
03128 if (ip->aml_value == value) {
03129 value = ip->x_value;
03130 break;
03131 }
03132 }
03133
03134 x_param_root->addAttribute(attrib_info.x_name, value);
03135
03136 }
03137
03138 return;
03139 }
03140
03141
03142
03143
03144
03145 void TranslateCore::aml_element_attributes_to_x (UAPNode* aml_root, UAPNode* aml_ele,
03146 UAPNode* x_ele, const string& ignore_this_attribute) {
03147
03148
03149
03150
03151 string aml_parent_name = aml_ele->getName();
03152 string aml_grand_p_name = aml_ele->getParent()->getName();
03153 string ele_name = x_ele->getAttributeString("name");
03154 string x_class = name_to_x_class_map[ele_name];
03155 bool ok;
03156
03157 StrVec tilt_vec(N_MULTIPOLE), ksl_vec(N_MULTIPOLE), kl_vec(N_MULTIPOLE);
03158
03159
03160
03161 NodeVec c = aml_ele->getChildren();
03162 for (NodeVecCIter ic = c.begin(); ic != c.end(); ic++) {
03163
03164 UAPNode* aml_attrib_node = *ic;
03165 string aml_attrib_name = aml_attrib_node->getName();
03166 if (aml_attrib_name == ignore_this_attribute) continue;
03167
03168
03169
03170 if (aml_attrib_node->getChildren().size() > 0) {
03171 aml_element_attributes_to_x (aml_root, aml_attrib_node, x_ele);
03172
03173 }
03174
03175
03176
03177 if (custom_aml_element_attribute_to_x(aml_root, aml_ele, aml_attrib_node, x_ele)) continue;
03178
03179
03180
03181 if (aml_attrib_name == "description") continue;
03182 if (aml_attrib_name == "doc") continue;
03183 if (aml_attrib_name == "floor") continue;
03184 if (aml_attrib_name == "s") continue;
03185
03186
03187
03188
03189 if (aml_parent_name == "multipole" && (aml_attrib_name == "kl" || aml_attrib_name == "ksl" ||
03190 aml_attrib_name == "tilt")) {
03191 int n = string_to_int(aml_attrib_node->getAttributeString("n"), ok);
03192 string design = aml_attrib_node->getAttributeString("design");
03193 if (aml_attrib_name == "kl") kl_vec[n] = design;
03194 else if (aml_attrib_name == "ksl") ksl_vec[n] = design;
03195 else if (aml_attrib_name == "tilt") tilt_vec[n] = design;
03196 continue;
03197 }
03198
03199
03200
03201 if (aml_parent_name == "quadrupole" || aml_parent_name == "sextupole" || aml_parent_name == "octupole") {
03202
03203 if (aml_attrib_name == "orientation") {
03204 info_out.error ("Conversion with an <orientation> child of a <" + aml_parent_name + "> not allowed.",
03205 "For: " + aml_root->toString());
03206 continue;
03207 }
03208
03209 if (aml_attrib_name == "k") {
03210 aml_k_map[ele_name] = aml_attrib_node->getAttributeString("design");
03211 continue;
03212 } else if (aml_attrib_name == "ks") {
03213 aml_ks_map[ele_name] = aml_attrib_node->getAttributeString("design");
03214 continue;
03215 }
03216
03217 }
03218
03219 if (x_class == "quadrupole" || x_class == "sextupole" || x_class == "octupole") {
03220 if (aml_attrib_name == "tilt") {
03221 aml_tilt_map[ele_name] = aml_attrib_node->getAttributeString("design");
03222 continue;
03223 }
03224 }
03225
03226
03227
03228 if (aml_parent_name == "taylor_map" && aml_attrib_name == "term") {
03229 bool ok;
03230 int i_out = string_to_int(aml_attrib_node->getAttributeString("i_out"), ok);
03231 if (i_out < 1 || i_out > 6) {
03232 info_out.error ("Taylor term has bad \"i_out\" index: ", aml_ele->toString(),
03233 "In Taylor_map: " + aml_ele->toString());
03234 continue;;
03235 }
03236 string coef = aml_attrib_node->getAttributeString("coef");
03237 if (coef == "") {
03238 info_out.error ("Bad Taylor series term coef:", aml_attrib_node->toString(),
03239 "In Taylor_map: " + aml_ele->toString());
03240 continue;;
03241 }
03242 string exp = aml_attrib_node->getAttributeString("exp");
03243 IntVec e(6);
03244 if (!get_taylor_exponents (exp, e)) return;
03245 int sum = e[0] + e[1] + e[2] + e[3] + e[4] + e[5];
03246 if (sum > 2) {
03247 info_out.error ("Taylor series term has order > 2:", aml_attrib_node->toString(),
03248 "In Taylor_map: " + aml_ele->toString());
03249 continue;;
03250 }
03251 string term;
03252 if (sum == 0)
03253 term = "kick" + int_to_string(i_out, ok);
03254 else if (sum == 1)
03255 term = "rm" + int_to_string(i_out, ok);
03256 else
03257 term = "tm" + int_to_string(i_out, ok);
03258
03259 for (int i = 0; i < 6; i++) {
03260 if (e[i] == 1) term += int_to_string(i+1, ok);
03261 if (e[i] == 2) term += int_to_string(i+1, ok) + int_to_string(i+1, ok);
03262 }
03263
03264 x_ele->addAttribute(term, coef);
03265 continue;
03266 }
03267
03268
03269
03270 AttributeVec att_list = aml_attrib_node->getAttributes();
03271 for (AttributeVecCIter design = att_list.begin(); design != att_list.end(); design++) {
03272
03273 string design_name = design->getName();
03274 attribute_convert_struct attrib_info;
03275
03276
03277
03278 if (design_name == "actual") continue;
03279
03280
03281
03282
03283 string full_name = aml_attrib_name + "(" + design_name + "='" + design->getValue() + "')";
03284 if (found(aml_ignore_these_attribs, full_name)) continue;
03285 if (found_value(x_class_to_aml_map, full_name)) continue;
03286 if (aml_parent_name != "element") full_name = aml_parent_name + ":" + full_name;
03287 if (found_aml_attrib (x_class, full_name, attrib_info)) continue;
03288 if (aml_parent_name != "element") {
03289 string aml_grand_parent = aml_ele->getParent()->getName();
03290 if (aml_grand_parent != "element") {
03291 full_name = aml_grand_parent + ":" + full_name;
03292 if (found_aml_attrib (x_class, full_name, attrib_info)) continue;
03293 }
03294 }
03295
03296
03297
03298
03299 if (!found_aml_attrib(x_class, aml_root, aml_attrib_node, design_name, attrib_info)) {
03300 if (aml_attrib_node->getXMLURI() == "") info_out.error (
03301 "No corresponding " + language + " attribute",
03302 "For AML " + design_name + " attribute: " + aml_attrib_node->toString(),
03303 "Of: " + aml_root->toString());
03304 continue;
03305 }
03306 if (attrib_info.ignore_aml_to_x) continue;
03307
03308
03309
03310 string value = design->getValue();
03311 value = aml_expression_to_x (value, attrib_info, ele_name);
03312 x_ele->addAttribute(attrib_info.x_name, value);
03313
03314 }
03315
03316 }
03317
03318
03319
03320 if (aml_parent_name == "multipole") {
03321 aml_multi_kl_map[ele_name] = kl_vec;
03322 aml_multi_ksl_map[ele_name] = ksl_vec;
03323 aml_multi_tilt_map[ele_name] = tilt_vec;
03324 }
03325
03326
03327
03328 custom2_aml_element_attribute_to_x (aml_ele, x_ele);
03329
03330 return;
03331
03332 }
03333
03334
03335
03336
03337 void TranslateCore::aml_sector_or_element_to_x (UAPNode* aml_sec_or_ele, UAPNode* x_rep) {
03338
03339
03340
03341 if (custom_aml_sector_or_element_to_x(aml_sec_or_ele, x_rep)) return;
03342
03343 string aml_name = aml_sec_or_ele->getName();
03344 string x_name = aml_name;
03345 bool ok;
03346
03347
03348
03349
03350
03351 if (aml_name == "sector" && aml_sec_or_ele->getAttribute("length")) {
03352
03353 string sec_name = aml_sec_or_ele->getAttributeString("name");
03354
03355 string sec_length = aml_sec_or_ele->getAttributeString("length");
03356 x_rep->addChild("constant")->addAttribute("name", "len_" + sec_name)->
03357 addAttribute("design", sec_length);
03358
03359 int n_drift = 0;
03360
03361
03362
03363 UAPNode* x_line = x_rep->addChild("line");
03364 x_line->addAttribute("name", sec_name);
03365
03366
03367
03368 UAPNode* positions = new UAPNode("temp");
03369 UAPNode* last_p_child = positions->addChild("zero!");
03370 last_p_child->addAttribute("EXIT", "");
03371
03372
03373
03374
03375
03376
03377
03378
03379
03380
03381 NodeVec eles = aml_sec_or_ele->getChildren();
03382 for (NodeVecCIter ie = eles.begin(); ie != eles.end(); ie++) {
03383 UAPNode* aml_seq_ele = *ie;
03384
03385
03386
03387 string ele_name = aml_seq_ele->getAttributeString("name");
03388 if (ele_name == "") {
03389 ele_name = aml_seq_ele->getAttributeString("ref");
03390 } else if (!found(name_to_x_node_map, ele_name)) {
03391 aml_node_to_x (aml_seq_ele, x_rep, "superimpose");
03392 }
03393
03394 }
03395
03396
03397
03398 for (NodeVecCIter ie = eles.begin(); ie != eles.end(); ie++) {
03399 UAPNode* aml_seq_ele = *ie;
03400
03401 string d_name = "drft" + int_to_string(n_drift++, ok) + "_" + sec_name;
03402 UAPNode* drift_node = x_rep->addChild("element");
03403
03404 string ele_name = aml_seq_ele->getAttributeString("name");
03405 if (ele_name == "") ele_name = aml_seq_ele->getAttributeString("ref");
03406
03407
03408
03409 UAPNode* superimpose = aml_seq_ele->getChildByName("superimpose");
03410 string offset = superimpose->getAttributeString("offset");
03411 string ele_origin = superimpose->getAttributeString("ele_origin");
03412 if (ele_origin == "") ele_origin = "CENTER";
03413 string ref_origin = superimpose->getAttributeString("ref_origin");
03414 if (ref_origin == "") ref_origin = "CENTER";
03415 string ref_element = superimpose->getAttributeString("ref_element");
03416
03417 string s_ele("");
03418
03419 if (ref_element == "") {
03420 s_ele = offset;
03421 if (offset == "") s_ele = "0";
03422
03423 } else {
03424 UAPNode* this_ref = NULL;
03425 NodeVec refs = positions->getChildren();
03426 for (NodeVecCIter ir = refs.begin(); ir != refs.end(); ie++) {
03427 UAPNode* ref = *ir;
03428 if (ref->getName() == ref_element) {
03429 this_ref = ref;
03430 break;
03431 }
03432 }
03433
03434 if (!this_ref) {
03435 info_out.error ("Cannot file reference element for: " + aml_seq_ele->toString(),
03436 "In Sector:", aml_sec_or_ele->toStringTree());
03437 return;
03438 }
03439
03440 s_ele = this_ref->getAttributeString(ref_origin);
03441 if (offset != "") s_ele = s_ele + " + " + offset;
03442 }
03443
03444
03445
03446 string ele_len;
03447 if (aml_seq_ele->getName() == "element") {
03448 attribute_convert_struct attrib_info;
03449 if (!found_x_attrib (name_to_x_class_map[ele_name], "l", attrib_info)) {
03450 info_out.error ("Information not found for: " + aml_seq_ele->toString());
03451 return;
03452 }
03453 if (!attrib_info.ignore_aml_to_x) ele_len = ele_name + "[length]";
03454 } else {
03455 ele_len = "len_" + ele_name;
03456 }
03457
03458 UAPNode* p_child = positions->addChild(ele_name);
03459 if (ele_len == "") {
03460 p_child->addAttribute("ENTRANCE", s_ele);
03461 p_child->addAttribute("CENTER", s_ele);
03462 p_child->addAttribute("EXIT", s_ele);
03463 } else if (ele_origin == "ENTRANCE") {
03464 p_child->addAttribute("ENTRANCE", s_ele);
03465 p_child->addAttribute("CENTER", s_ele + " + " + ele_len + "/2");
03466 p_child->addAttribute("EXIT", s_ele + " + " + ele_len);
03467 } else if (ele_origin == "CENTER") {
03468 p_child->addAttribute("ENTRANCE", s_ele + " - " + ele_len + "/2");
03469 p_child->addAttribute("CENTER", s_ele);
03470 p_child->addAttribute("EXIT", s_ele + " + " + ele_len + "/2");
03471 } else {
03472 p_child->addAttribute("ENTRANCE", s_ele + " - " + ele_len);
03473 p_child->addAttribute("CENTER", s_ele + " - " + ele_len + "/2");
03474 p_child->addAttribute("EXIT", s_ele);
03475 }
03476
03477
03478
03479 drift_node->addAttribute("name", d_name);
03480 drift_node->addAttribute("class", "drift");
03481 string len = p_child->getAttributeString("ENTRANCE");
03482 string base = add_parens(last_p_child->getAttributeString("EXIT"), 1);
03483 if (base != "") len = len + " - " + base;
03484
03485 drift_node->addAttribute("l", aml_expression_to_x(len));
03486
03487
03488
03489 x_line->addChild("element")->addAttribute("ref", d_name);
03490
03491 if (aml_seq_ele->getName() == "element")
03492 x_line->addChild("element")->addAttribute("ref", ele_name);
03493 else
03494 x_line->addChild("line")->addAttribute("ref", ele_name);
03495
03496
03497
03498 last_p_child = p_child;
03499
03500 }
03501
03502
03503
03504 bool ok;
03505 string d_name = "drft" + int_to_string(n_drift++, ok) + "_" + sec_name;
03506 UAPNode* drift_node = x_rep->addChild("element");
03507 drift_node->addAttribute("name", d_name);
03508 drift_node->addAttribute("class", "drift");
03509 string len = sec_length;
03510 string base = last_p_child->getAttributeString("EXIT");
03511 if (base != "") len = len + " - " + add_parens(base, 1);
03512 drift_node->addAttribute("l", aml_expression_to_x(len));
03513 x_line->addChild("element")->addAttribute("ref", d_name);
03514
03515
03516
03517 x_line->detachNode();
03518 x_rep->addChild(x_line);
03519
03520 positions->deleteNode();
03521
03522 return;
03523 }
03524
03525
03526
03527
03528
03529
03530 if (aml_name == "sector") x_name = "line";
03531 if (aml_name == "element") x_name = "element";
03532
03533 UAPNode* x_ele = x_rep->addChild(x_name);
03534
03535
03536
03537 transfer_attribute(aml_sec_or_ele, "name", x_ele, "name");
03538 transfer_attribute(aml_sec_or_ele, "ref", x_ele, "ref");
03539 transfer_attribute(aml_sec_or_ele, "tag", x_ele, "tag");
03540 transfer_attribute(aml_sec_or_ele, "args", x_ele, "args");
03541
03542 string repeat = aml_sec_or_ele->getAttributeString("repeat");
03543 string refl = aml_sec_or_ele->getAttributeString("reflection");
03544 if (refl == "TRUE") {
03545 if (repeat == "") repeat = "-1";
03546 else repeat = "-" + repeat;
03547 }
03548 if (repeat != "") x_ele->addAttribute("repeat", repeat);
03549
03550
03551
03552 NodeVec c = aml_sec_or_ele->getChildren();
03553 for (NodeVecCIter ic = c.begin(); ic != c.end(); ic++) {
03554 aml_sector_or_element_to_x (*ic, x_ele);
03555 }
03556
03557 return;
03558
03559 }
03560
03561
03562
03563
03564 bool TranslateCore::aml_ele_to_x_class (UAPNode* aml_ele, string& x_class) {
03565
03566
03567
03568
03569 x_class = "";
03570
03571
03572
03573 StrList names;
03574 NodeVec children = aml_ele->getChildren();
03575 for (NodeVecCIter it = children.begin(); it != children.end(); it++) {
03576 string name = (*it)->getName();
03577 if (found(aml_class_names, name)) names << name;
03578 }
03579
03580 UAPNode* instrument = aml_ele->getChildByName("instrument");
03581
03582
03583
03584 if (names.size() == 0) {
03585 x_class = "drift";
03586 if (instrument) {
03587 x_class = "instrument";
03588 string i_type = instrument->getAttributeString("type");
03589 if (i_type == "hmonitor" || i_type == "vmonitor" || i_type == "monitor")
03590 x_class = i_type;
03591 }
03592 return true;
03593 }
03594
03595
03596
03597 if (names.size() > 1) names.remove("scaled_multipole");
03598 if (names.size() > 1) names.remove("aperture");
03599
03600
03601
03602 if (names.size() == 1) {
03603
03604 x_class = names.front();
03605
03606 if (x_class == "taylor_map") x_class = "matrix";
03607 if (x_class == "bend") x_class = "sbend";
03608 if (x_class == "rf_cavity") x_class = "rfcavity";
03609 if (x_class == "aperture") {
03610 x_class = "rcollimator";
03611 string shape = aml_ele->getChildByName("aperture")->getAttributeString("shape");
03612 if (shape == "ELLIPTICAL") x_class = "ecollimator";
03613 }
03614
03615 if (x_class == "patch" || x_class == "wiggler" || x_class == "match") {
03616 x_class = "BAD!";
03617 info_out.error ("AML element does not have a corresponding " + language + " type",
03618 aml_ele->toStringTree());
03619 return false;
03620 }
03621 }
03622
03623
03624
03625 if (found(names, "bend")) x_class = "sbend";
03626
03627 if (x_class == "") {
03628 info_out.error ("AML element does not have a corresponding " + language + " type",
03629 aml_ele->toStringTree());
03630 return false;
03631 }
03632
03633 return true;
03634
03635 }
03636
03637
03638
03639
03640 string TranslateCore::aml_expression_to_x (const string& aml_expression,
03641 attribute_convert_struct attrib_info, const string& ele_name) {
03642
03643
03644
03645
03646 for (ParamValueStructVecIter ip = param_values_list.begin();
03647 ip != param_values_list.end(); ip++) {
03648 if (ip->aml_value == aml_expression) return ip->x_value;
03649 }
03650
03651
03652
03653
03654 size_t i0, i1, i2, n;
03655 string x_exp = aml_expression;
03656
03657 i0 = 0;
03658 while (true) {
03659 string token = next_token_in_expression(x_exp, false, i0, n);
03660
03661 if (i0 == x_exp.size()) break;
03662
03663
03664
03665 if ((i1 = token.find("[")) != string::npos) {
03666
03667
03668
03669 if (token.find("]") != token.size() - 1) {
03670 info_out.error ("No matching ']' for opening '[' in expression: " + aml_expression);
03671 return "";
03672 }
03673 i2 = token.size();
03674
03675 string this_ele_name = token.substr(0, i1);
03676 string this_aml_attrib = token.substr(i1+1, i2-i1-2);
03677
03678 string this_x_class = ele_name_to_x_class(this_ele_name);
03679
03680 attribute_convert_struct this_attrib_info;
03681 if (!found_aml_attrib(this_x_class, this_aml_attrib, this_attrib_info)) {
03682 info_out.error ("Unknown attribute: " + this_aml_attrib,
03683 "In AML expression: " + aml_expression);
03684 return x_exp;
03685 }
03686 if (this_attrib_info.ignore_aml_to_x) {
03687 i0 = i0 + i2;
03688 continue;
03689 }
03690
03691
03692
03693
03694 if (this_attrib_info.aml_twig.name != "") this_ele_name = this_attrib_info.x_parent;
03695
03696
03697
03698
03699
03700 x_exp.erase(i0, n);
03701
03702 string x_attrib;
03703 if (c_style_format) {
03704 x_attrib = this_ele_name + "->" + this_attrib_info.x_name;
03705 } else {
03706 x_attrib = this_ele_name + "[" + this_attrib_info.x_name + "]";
03707 }
03708 x_attrib = convert_single_attribute (x_attrib, this_attrib_info.x_to_aml_template,
03709 this_attrib_info.aml_to_x_template, this_ele_name);
03710 x_attrib = add_parens(x_attrib, x_exp, i0);
03711 x_exp.insert(i0, x_attrib);
03712 i0 += x_attrib.size();
03713
03714
03715
03716
03717 } else {
03718
03719 bool found_it = false;
03720 for (ConstantsVecIter ic = constants_list.begin(); ic != constants_list.end(); ic++) {
03721 if (ic->ignore_aml_to_x) continue;
03722 if (ic->aml_const == token) {
03723 string x_const = ic->aml_to_x_template;
03724 if (x_const == "") x_const = ic->x_const;
03725 x_exp.erase(i0, n);
03726 x_const = add_parens(x_const, x_exp, i0);
03727 x_exp.insert(i0, x_const);
03728 i0 += x_const.size();
03729 found_it = true;
03730 break;
03731 }
03732 }
03733
03734 if (!found_it) i0 += token.size();
03735
03736 }
03737 }
03738
03739
03740
03741 return convert_single_attribute (x_exp, attrib_info.aml_to_x_template,
03742 attrib_info.x_to_aml_template, ele_name);
03743
03744 return x_exp;
03745
03746 }
03747
03748
03749
03750
03751 string TranslateCore::convert_single_attribute (string expression, string forward_template,
03752 string back_template, string ele_name) {
03753
03754
03755
03756 if (forward_template == "") return expression;
03757
03758
03759
03760
03761
03762
03763
03764
03765
03766
03767
03768
03769 if (back_template != "") {
03770
03771 while (true) {
03772 size_t ix = back_template.find("!");
03773 if (ix == string::npos) break;
03774 back_template.replace(ix, 1, ele_name);
03775 }
03776
03777
03778
03779 size_t n_e = expression.size() - 1;
03780 size_t n_b = back_template.size() - 1;
03781 size_t ix_h = back_template.find("#");
03782 size_t ix_e = (ix_h + n_e) - n_b;
03783 size_t n2 = n_b - ix_h;
03784
03785 bool found_back = true;
03786 if (ix_h != 1) {
03787 if (expression.substr(0,ix_h-1) != back_template.substr(0,ix_h-1)) found_back = false;
03788 }
03789 if (n_e < n2) found_back = false;
03790 if (found_back && ix_h != n_b) {
03791 if (expression.substr(ix_e+1, n2) != back_template.substr(ix_h+1,n2)) found_back = false;
03792 }
03793
03794
03795
03796 if (found_back) {
03797 expression.erase(ix_e+1, n2);
03798 expression.erase(0,ix_h);
03799 return expression;
03800 }
03801 }
03802
03803
03804
03805
03806 while (true) {
03807 size_t ix = forward_template.find("!");
03808 if (ix == string::npos) break;
03809 forward_template.replace(ix, 1, ele_name);
03810 }
03811
03812
03813
03814 size_t ix_h = forward_template.find("#");
03815 forward_template.erase(ix_h, 1);
03816 expression = add_parens (expression, forward_template, ix_h);
03817 forward_template.insert(ix_h, expression);
03818 return forward_template;
03819
03820 }
03821
03822
03823
03824
03825
03826
03827 bool TranslateCore::transfer_attribute(UAPNode* ele_in, const string& attrib_in,
03828 UAPNode* ele_out, const string& attrib_out) {
03829
03830 if (UAPAttribute* attrib = ele_in->getAttribute(attrib_in))
03831 ele_out->addAttribute(attrib_out, attrib->getValue());
03832
03833 return true;
03834 }
03835
03836
03837
03838
03839 UAPNode* TranslateCore::XRepToXFile (UAPNode* x_root,
03840 const string& file_name, const Str& out_suffix, bool one_file) {
03841
03842
03843
03844 name_to_x_class_map.clear();
03845 info_out.ix_line = -1;
03846 info_out.statement = "";
03847 info_out.parsing_status = "Creating a " + language + " lattice file";
03848
03849 UAPNode* x_rep = x_root;
03850
03851 if (x_rep->getName() == "UAP_root")
03852 x_rep = x_rep->getChildByName(language + "_representation");
03853
03854 if (!x_rep || (x_rep->getName() != language + "_representation")) {
03855 info_out.error ("Node argument is not <" + language + "_representation> or its parent.");
03856 return NULL;
03857 }
03858
03859
03860
03861 string this_file;
03862
03863 if (file_name == "*") {
03864 string old_name = x_rep->getAttributeString("filename");
03865 size_t n = old_name.find_last_of(".");
03866 if (n == string::npos)
03867 this_file = old_name + out_suffix;
03868 else
03869 this_file = old_name.substr(0,n) + out_suffix;
03870 } else if (file_name != "") {
03871 this_file = file_name;
03872 }
03873
03874 x_rep->addAttribute("filename", this_file);
03875
03876
03877
03878 if (out_suffix != "") {
03879 Twig tinfo(include_file_string);
03880 include_filename_replace_suffix (x_rep, out_suffix, tinfo);
03881 }
03882
03883
03884
03885 StreamStruct x_out;
03886 FileStackList file_stack;
03887 x_rep_to_x_file (x_rep, this_file, one_file, x_out, file_stack);
03888 x_out.close_file();
03889
03890 return x_rep;
03891
03892 }
03893
03894
03895
03896
03897 void TranslateCore::include_filename_replace_suffix (UAPNode* node,
03898 const string& out_suffix, const Twig& info) {
03899
03900 if (node->getName() == info.nodeid_vec[0].name &&
03901 node->getXMLURI() == info.nodeid_vec[0].xml_uri) {
03902 UAPAttribute* attrib = node->getAttribute(info.nodeid_vec[1].name);
03903 if (attrib) {
03904 string file_name = attrib->getValue();
03905 size_t n = file_name.find_last_of(".");
03906 if (n == string::npos)
03907 file_name = file_name + out_suffix;
03908 else
03909 file_name = file_name.substr(0,n) + out_suffix;
03910
03911 attrib->setValue(file_name);
03912 }
03913 }
03914
03915 for (NodeVecIter in = node->getChildren().begin(); in != node->getChildren().end(); in++) {
03916 include_filename_replace_suffix (*in, out_suffix, info);
03917 }
03918
03919 }
03920
03921
03922
03923
03924 bool TranslateCore::x_rep_to_x_file (UAPNode* x_rep, const string& file_name,
03925 bool one_file, StreamStruct& x_output, FileStackList file_stack) {
03926
03927
03928
03929 StreamStruct x_out;
03930
03931 if (!one_file || !x_output.is_open()) {
03932 if (file_name == "") {
03933 info_out.error ("No file name given for creating a " + language + " file.");
03934 return false;
03935 }
03936 file_stack.addFile(file_name);
03937 string f_name = file_stack.file_list.back().full_name;
03938 x_out.continuation_char = continuation_char;
03939 x_out.open_file(f_name);
03940 if (!x_out.is_open()) {
03941 info_out.error ("Cannot open file: " + f_name);
03942 return false;
03943 }
03944 } else {
03945 x_out = x_output;
03946 }
03947
03948
03949
03950 NodeVec children = x_rep->getChildren();
03951 for (NodeVecCIter it = children.begin(); it != children.end(); it++) {
03952
03953 UAPNode* x_node = *it;
03954 AttributeVec x_attribs = x_node->getAttributes();
03955 string x_name = x_node->getName();
03956
03957
03958
03959 string end = "";
03960 string comment = x_node->getAttributeString("comment");
03961 if (comment != "") comment = " " + add_comment_token (comment);
03962 if (c_style_format) {
03963 comment = ";" + comment;
03964 end = ";";
03965 }
03966
03967
03968
03969 if (custom_x_element_to_x_file (x_node, comment, x_out)) continue;
03970
03971
03972
03973 if (x_name == "sequence") {
03974
03975 x_out << x_node->getAttributeString("name") << ": sequence";
03976 for (AttributeVecCIter ia = x_attribs.begin(); ia != x_attribs.end(); ia++) {
03977 if (ia->getName() == "comment") continue;
03978 if (ia->getName() == "name") continue;
03979 x_out << ", " << ia->getName() << " = " << ia->getValue();
03980 }
03981 x_out << comment << fini;
03982
03983 NodeVec childs = x_node->getChildren();
03984 for (NodeVecCIter ic = childs.begin(); ic != childs.end(); ic++) {
03985 UAPNode* ele = *ic;
03986
03987 if (ele->getName() == "sequence") {
03988 x_out << " " << ele->getAttributeString("ref") << ", at = "
03989 << ele->getAttributeString("at") << end << fini;
03990
03991 } else if (ele->getName() == "element" && ele->getAttribute("ref")) {
03992 x_out << " " << ele->getAttributeString("ref") << ", at = "
03993 << ele->getAttributeString("at") << end << fini;
03994
03995 } else if (ele->getName() == "element" && ele->getAttribute("name")) {
03996 string str = ele->getAttributeString("class");
03997 if (str == "") str = ele->getAttributeString("inherit");
03998 x_out << " " << ele->getAttributeString("name") << ": "
03999 << str << ", at = " << ele->getAttributeString("at");
04000 AttributeVec atts = ele->getAttributes();
04001 for (AttributeVecCIter ia = atts.begin(); ia != atts.end(); ia++) {
04002 string a_name = ia->getName();
04003 string a_value = ia->getValue();
04004 if (a_name == "name") continue;
04005 if (a_name == "class") continue;
04006 if (a_name == "inherit") continue;
04007 if (a_name == "at") continue;
04008 custom_x_attrib_to_x_file_translate (a_name, a_value);
04009 x_out << ", " << a_name << " = " << a_value;
04010 }
04011 x_out << end << fini;
04012
04013 }
04014 }
04015
04016 x_out << "endsequence" << end << fini;
04017 continue;
04018
04019 }
04020
04021
04022
04023 if (x_name == "call") {
04024 string this_file = x_node->getAttributeString("filename");
04025 x_out << "call, filename = \"" << this_file << "\"" << comment << fini;
04026 x_rep_to_x_file (x_node, this_file, one_file, x_output, file_stack);
04027 continue;
04028 }
04029
04030 if (x_name == "comment") {
04031 x_out << add_comment_token(x_node->getAttributeString("text")) << fini;
04032 continue;
04033 }
04034
04035 if (x_name == "constant") {
04036 string type = x_node->getAttributeString("type");
04037 string equal = " = ";
04038 if (type != "") equal = " : " + type + " = ";
04039 x_out << x_node->getAttributeString("name") <<
04040 equal << x_node->getAttributeString("design") << comment << fini;
04041 continue;
04042 }
04043
04044 if (x_name == "beam") {
04045 x_out << x_name;
04046 for (AttributeVecCIter it = x_attribs.begin(); it != x_attribs.end(); it++) {
04047 if (it->getName() == "comment") continue;
04048 x_out << ", " << it->getName() << " = " << it->getValue();
04049 }
04050 x_out << comment << fini;
04051 continue;
04052 }
04053
04054 if (x_name == "line" || x_name == "list") {
04055 x_out << x_node->getAttributeString("name");
04056
04057 string tag = x_node->getAttributeString("tag");
04058 if (tag != "") x_out << "[" << tag << "]";
04059
04060 string args = x_node->getAttributeString("args");
04061 if (args != "") x_out << "(" << args << ")";
04062
04063 x_out << ": " << x_name;
04064
04065 bool ok;
04066 bool multipass = string_to_bool(x_node->getAttributeString("multipass"), ok);
04067 if (ok && multipass) x_out << "[multipass]";
04068
04069 x_out << " = ";
04070
04071 write_line_or_list_to_x_file (x_node, ", ", x_out);
04072 x_out << comment << fini;
04073 continue;
04074 }
04075
04076 if (x_name == "return") {
04077 x_out << "return" << comment << fini;
04078 continue;
04079 }
04080
04081
04082
04083 if (x_name == "set") {
04084 string a_name = x_node->getAttributeString("attribute");
04085 string a_value = x_node->getAttributeString("value");
04086 x_add_quote_marks (a_name, a_value);
04087 x_out << x_node->getAttributeString("element") << ", "
04088 << a_name << " = " << a_value << comment << fini;
04089 continue;
04090 }
04091
04092
04093
04094 if (x_name == "element") {
04095 x_out << x_node->getAttributeString("name") << ": ";
04096 string ele_class = x_node->getAttributeString("class");
04097 if (ele_class == "") ele_class = x_node->getAttributeString("inherit");
04098 x_out << ele_class;
04099 for (AttributeVecCIter att = x_attribs.begin(); att != x_attribs.end(); att++) {
04100 string a_name = att->getName();
04101 string a_value = att->getValue();
04102 if (a_name == "name") continue;
04103 if (a_name == "comment") continue;
04104 if (a_name == "class") continue;
04105 if (a_name == "inherit") continue;
04106
04107 custom_x_attrib_to_x_file_translate (a_name, a_value);
04108 x_out << ", " << a_name;
04109 x_add_quote_marks (a_name, a_value);
04110 if (a_value != "") x_out << " = " << a_value;
04111 }
04112
04113 custom_x_ele_attributes_to_x_file (x_node, ele_class, x_out);
04114
04115 x_out << comment << fini;
04116 continue;
04117 }
04118
04119
04120
04121 info_out.error (language + " Node not recognized: " + x_node->toString());
04122
04123 }
04124
04125 if (!one_file) x_out.close_file();
04126 return true;
04127
04128 }
04129
04130
04131
04132
04133 string TranslateCore::next_token_in_expression (string expression, bool c_style,
04134 size_t& i0, size_t& n) {
04135
04136
04137
04138
04139
04140
04141
04142 bool last_was_digit_or_period = false;
04143 for ( ; ; i0++) {
04144 if (i0 == expression.size()) return "";
04145 char a = expression[i0];
04146 if (isalpha(a) && !((a == 'e' || a == 'E') && last_was_digit_or_period)) break;
04147 if (isdigit(a) || a == '.')
04148 last_was_digit_or_period = true;
04149 else
04150 last_was_digit_or_period = false;
04151 }
04152
04153
04154
04155 bool found_attrib = false;
04156 size_t i2 = i0;
04157
04158 for ( ; ; i2++) {
04159 if (i2 == expression.size()) break;
04160 char a = expression[i2];
04161 if (a == '[') found_attrib = true;
04162 if (found_attrib && !c_style && a != ']') continue;
04163 if (a == ']') {
04164 i2++;
04165 break;
04166 }
04167 if (c_style && a == '-' && expression[i2+1] == '>') {
04168 found_attrib = true;
04169 i2++;
04170 continue;
04171 }
04172 if (a == '+' || a == '-' || a == '*' || a == '/' || a == '^') break;
04173 if (a == '(' || a == ')' || a == ',' || a == ' ') break;
04174 }
04175
04176 n = i2 - i0;
04177 return expression.substr(i0, n);
04178
04179 }
04180
04181
04182
04183
04184 void TranslateCore::x_add_quote_marks (const string& name, string& value) {
04185
04186
04187
04188
04189
04190 if (value == "") return;
04191 if (!found(x_attributes_needing_quote_marks, name)) return;
04192
04193
04194
04195
04196 if (value.find("\'") != string::npos && value.find("\"") != string::npos) {
04197 info_out.error("String has both \' and \" marks within it: " + value);
04198 return;
04199 }
04200
04201
04202
04203 if (value.find("\"") != string::npos)
04204 value = "\'" + value + "\'";
04205 else
04206 value = "\"" + value + "\"";
04207
04208 return;
04209
04210 }
04211
04212
04213
04214
04215 string TranslateCore::add_comment_token (const string& str) {
04216 if (c_style_format) {
04217 if (str.find("\n") == string::npos) return "// " + str;
04218 return "/*" + str + "*/";
04219 } else {
04220 return "! " + str;
04221 }
04222 }
04223
04224
04225
04226
04227 void TranslateCore::write_line_or_list_to_x_file(UAPNode* line_node,
04228 string separator_str, StreamStruct& x_out) {
04229
04230 x_out << "(";
04231
04232 NodeVec children = line_node->getChildren();
04233 for (NodeVecCIter nt = children.begin(); nt != children.end(); nt++) {
04234 UAPNode* ele = *nt;
04235
04236
04237
04238 string repeat = ele->getAttributeString("repeat");
04239 if (repeat != "") {
04240 repeat += "*";
04241 if (repeat == "-1*") repeat = "-";
04242 x_out << repeat;
04243 }
04244
04245
04246
04247 string name = ele->getAttributeString("ref");
04248 string who = ele->getName();
04249 x_out << name;
04250
04251
04252
04253 string tag = ele->getAttributeString("tag");
04254 if (tag != "") x_out << "[" << tag << "]";
04255
04256
04257
04258
04259 if (who == "line") {
04260 string args = ele->getAttributeString("args");
04261 if (args != "") {
04262 x_out << "(" << args << ")";
04263 } else if (ele->getChildren().size() > 0) {
04264 write_line_or_list_to_x_file (ele, separator_str, x_out);
04265 }
04266
04267 } else if (who == "girder") {
04268 continue;
04269 } else if (who != "element" && who != "item") {
04270 info_out.error ("Node is not a \"line\" nor a \"element\": " + ele->toString());
04271 }
04272
04273 if (ele != children.back()) x_out << separator_str;
04274
04275 }
04276
04277 x_out << ")";
04278 return;
04279 }
04280
04281
04282
04283
04284
04285
04286
04287
04288
04289
04290
04291
04292
04293 void TranslateCore::register_attribute (string x_attribute, string aml_attribute,
04294 string x_to_aml_template, string aml_to_x_template, bool ignore_x_to_aml, bool ignore_aml_to_x) {
04295
04296
04297
04298 attribute_convert_struct at;
04299
04300 at.x_to_aml_template = x_to_aml_template;
04301 at.aml_to_x_template = aml_to_x_template;
04302 at.ignore_x_to_aml = ignore_x_to_aml;
04303 at.ignore_aml_to_x = ignore_aml_to_x;
04304
04305 if (x_to_aml_template != "" && !ignore_x_to_aml && x_to_aml_template.find('#') == string::npos) {
04306 info_out.error ("NO '#' FOUND IN X_TO_AML_TEMPLATE: " + x_to_aml_template,
04307 "FOR ATTRIBUTE: " + aml_attribute);
04308 throw;
04309 }
04310
04311 if (aml_to_x_template != "" && !ignore_aml_to_x && aml_to_x_template.find('#') == string::npos) {
04312 info_out.error ("NO '#' FOUND IN AML_TO_X_TEMPLATE: " + aml_to_x_template,
04313 "FOR ATTRIBUTE: " + aml_attribute);
04314 throw;
04315 }
04316
04317
04318
04319 size_t i = x_attribute.find(":");
04320 if (i == string::npos) {
04321 at.x_name = x_attribute;
04322 at.x_parent = "";
04323 } else {
04324 at.x_name = x_attribute.substr(i+1);
04325 at.x_parent = x_attribute.substr(0, i);
04326 }
04327
04328
04329
04330 string element_dummy;
04331 if (!at.aml_twig.fromString(aml_attribute)) {
04332 info_out.error ("Bad AML attribute in register_attribute: " + aml_attribute);
04333 throw;
04334 }
04335
04336 if (at.aml_twig.target_attribute == "") at.aml_twig.target_attribute = "design";
04337
04338
04339
04340 attribute_list.push_back(at);
04341
04342
04343
04344 for (NodeIDVecCIter ni = at.aml_twig.nodeid_vec.begin();
04345 ni != at.aml_twig.nodeid_vec.end(); ni++) {
04346 if (ni->attribute.size() > 0) aml_ignore_these_attribs << ni->toString();
04347 }
04348
04349 return;
04350
04351 }
04352
04353
04354
04355
04356 void TranslateCore::register_constant (const string& x, const string& aml,
04357 const string& x_to_aml_template, const string& aml_to_x_template,
04358 bool ignore_x_to_aml, bool ignore_aml_to_x) {
04359
04360 constants_struct cs;
04361 cs.x_const = x;
04362 cs.aml_const = aml;
04363 cs.x_to_aml_template = x_to_aml_template;
04364 cs.aml_to_x_template = aml_to_x_template;
04365 cs.ignore_x_to_aml = ignore_x_to_aml;
04366 cs.ignore_aml_to_x = ignore_aml_to_x;
04367
04368 constants_list.push_back(cs);
04369
04370 }
04371
04372
04373
04374
04375 void TranslateCore::register_param_value_conversion (const string& x_value, const string& aml_value) {
04376
04377 param_value_struct pvs;
04378 pvs.x_value = x_value;
04379 pvs.aml_value = aml_value;
04380
04381 param_values_list.push_back(pvs);
04382
04383 return;
04384 }
04385
04386
04387
04388
04389 bool TranslateCore::found_x_attrib (const string& x_class, const string& x_attrib,
04390 attribute_convert_struct& at) {
04391
04392
04393
04394
04395 int nl = x_attrib.length();
04396 int n_match = 0;
04397
04398 for (AttributeConvertVecIter im = attribute_list.begin(); im != attribute_list.end(); im++) {
04399 if (im->x_parent != x_class) continue;
04400 if (x_attrib == im->x_name) {
04401 at = *im;
04402 return true;
04403 }
04404 if (abbreviations_permitted && x_attrib == im->x_name.substr(0, nl)) {
04405 at = *im;
04406 n_match++;
04407 }
04408 }
04409
04410 if (n_match == 1) return true;
04411 if (n_match > 1) return false;
04412
04413
04414
04415 n_match = 0;
04416
04417 for (AttributeConvertVecIter im = attribute_list.begin(); im != attribute_list.end(); im++) {
04418 if (im->x_parent != "" && x_class != "" && im->x_parent != x_class) continue;
04419 if (x_attrib == im->x_name) {
04420 at = *im;
04421 return true;
04422 }
04423 if (abbreviations_permitted && x_attrib == im->x_name.substr(0, nl)) {
04424 at = *im;
04425 n_match++;
04426 }
04427 }
04428
04429 if (n_match == 1) return true;
04430 return false;
04431
04432 }
04433
04434
04435
04436
04437 bool TranslateCore::found_aml_attrib (const string& x_class, const string& aml_attrib,
04438 attribute_convert_struct& attrib_info) {
04439 Twig aml_twig;
04440 if (!aml_twig.fromString(aml_attrib)) {
04441 info_out.error ("Cannot parse: " + aml_attrib);
04442 return false;
04443 }
04444 if (aml_twig.target_attribute == "") aml_twig.target_attribute = "design";
04445
04446
04447
04448 for (AttributeConvertVecIter im = attribute_list.begin();
04449 im != attribute_list.end(); im++) {
04450 attribute_convert_struct& this_info = *im;
04451
04452 if (x_class != "" && this_info.x_parent != "" && x_class != this_info.x_parent) continue;
04453 if (this_info.aml_twig.nodeid_vec == aml_twig.nodeid_vec &&
04454 this_info.aml_twig.target_attribute == aml_twig.target_attribute) {
04455 attrib_info = this_info;
04456 return true;
04457 }
04458 }
04459
04460 return false;
04461
04462 }
04463
04464
04465
04466
04467 bool TranslateCore::found_aml_attrib (const string& x_class, UAPNode* aml_root,
04468 UAPNode* aml_attrib_node, const string& design_name, attribute_convert_struct& attrib_info) {
04469
04470 string aml_parent = aml_attrib_node->getParent()->getName();
04471 string aml_attrib = aml_attrib_node->getName();
04472
04473
04474
04475
04476 for (AttributeConvertVecIter im = attribute_list.begin();
04477 im != attribute_list.end(); im++) {
04478 attribute_convert_struct& this_info = *im;
04479 if (x_class != "" && this_info.x_parent != "" && x_class != this_info.x_parent) continue;
04480 if (design_name != "" && design_name != this_info.aml_twig.target_attribute) continue;
04481 if (aml_attrib_node != this_info.aml_twig.getLocalSubNode(aml_root)) continue;
04482 attrib_info = this_info;
04483 return true;
04484 }
04485
04486
04487
04488 return false;
04489
04490 }
04491
04492
04493
04494
04495 void TranslateCore::multipole_to_k_tilt (string k_in, string ks_in, string tilt_in, string n,
04496 string& k, string& tilt) {
04497
04498
04499
04500
04501 if (k_in.find_first_not_of("0. ") == string::npos) k_in = "";
04502 if (ks_in.find_first_not_of("0. ") == string::npos) ks_in = "";
04503 if (tilt_in.find_first_not_of("0. ") == string::npos) tilt_in = "";
04504
04505
04506
04507 if (ks_in == "") {
04508 k = k_in;
04509 tilt = tilt_in;
04510 } else if (k_in == "" && tilt_in == "") {
04511 k = ks_in;
04512 int n2 = 2 * (string_to_int(n) + 1);
04513 tilt = "-pi/" + int_to_string(n2);
04514 } else {
04515 k_in = add_parens(k_in, 2);
04516 ks_in = add_parens(ks_in, 1);
04517 k = k_in + " * sqrt (1 + (" + ks_in + "/" + k_in + ")^2)";
04518 int n1 = string_to_int(n) + 1;
04519 if (tilt_in == "")
04520 tilt = "-atan(" + ks_in + " / " + k_in + ") / " + int_to_string(n1);
04521 else
04522 tilt = tilt_in + "- atan(" + ks_in + " / " + k_in + ") / " + int_to_string(n1);
04523 }
04524
04525 return;
04526
04527 }
04528
04529
04530
04531
04532 void TranslateCore::multipole_to_k_ks (string k_in, string ks_in, string tilt_in, string n,
04533 string& k, string& ks) {
04534
04535
04536
04537
04538 if (k_in.find_first_not_of("0. ") == string::npos) k_in = "";
04539 if (ks_in.find_first_not_of("0. ") == string::npos) ks_in = "";
04540 if (tilt_in.find_first_not_of("0. ") == string::npos) tilt_in = "";
04541
04542
04543
04544 if (tilt_in == "") {
04545 k = k_in;
04546 ks = ks_in;
04547 return;
04548 }
04549
04550 tilt_in = add_parens(tilt_in, 1);
04551 int n1 = string_to_int(n) + 1;
04552 string arg_str = "(" + tilt_in + "/" + int_to_string(n1) + ")";
04553
04554 if (k_in == "") {
04555 ks_in = add_parens(ks_in, 1);
04556 k = ks_in + " * sin" + arg_str;
04557 ks = ks_in + " * cos" + arg_str;
04558 } else if (ks_in == "") {
04559 k_in = add_parens(k_in, 1);
04560 k = k_in + " * cos" + arg_str;
04561 ks = "-" + k_in + " * sin" + arg_str;
04562 } else {
04563 k_in = add_parens(k_in, 1);
04564 ks_in = add_parens(ks_in, 1);
04565 ks = ks_in + " * cos" + arg_str + " - " + k_in + " * sin" + arg_str;
04566 k = ks_in + " * sin" + arg_str + " + " + k_in + " * cos" + arg_str;
04567 }
04568
04569 return;
04570
04571 }
04572
04573
04574
04575
04576 void TranslateCore::required_multipole_transfer (const Str& odd_man_out, const Str& k_base,
04577 const Str& ks_base, const Str& tilt_base, Str& k_ele, Str& ks_ele, Str& tilt_ele) {
04578
04579
04580
04581 if (k_ele == "" && ks_ele == "" && tilt_ele == "") return;
04582
04583
04584
04585 if (odd_man_out == "ks")
04586 if (ks_base == "" && ks_ele == "") return;
04587 else if (odd_man_out == "tilt")
04588 if (tilt_base == "" && tilt_ele == "") return;
04589 else
04590 throw;
04591
04592
04593
04594 if (k_ele == "") k_ele = k_base;
04595 if (ks_ele == "") ks_ele = ks_base;
04596 if (tilt_ele == "") tilt_ele = tilt_base;
04597
04598 return;
04599
04600 }
04601
04602
04603
04604
04605 bool TranslateCore::custom_x_statement_to_x (StrList word_list, string comment,
04606 UAPNode* x_rep_root) {
04607 return false;
04608 }
04609
04610
04611
04612
04613 bool TranslateCore::custom_x_add_attributes (StrList& word_list, string ele_class,
04614 string& attrib_name, UAPNode* x_ele_root) {
04615 return false;
04616 }
04617
04618
04619
04620
04621 bool TranslateCore::custom_x_node_to_aml (UAPNode* x_node, UAPNode* aml_root, UAPNode* aml_ele) {
04622
04623 return false;
04624 }
04625
04626
04627
04628
04629 void TranslateCore::custom2_x_element_attribute_to_aml (UAPNode* x_node, UAPNode* aml_node) {
04630 return;
04631 }
04632
04633
04634
04635
04636 void TranslateCore::custom_aml_rep_to_x (UAPNode* lab, UAPNode* x_rep) {
04637 return;
04638 }
04639
04640
04641
04642
04643 bool TranslateCore::custom_aml_node_to_x (UAPNode* aml_ele, UAPNode* x_rep) {
04644 return false;
04645 }
04646
04647
04648
04649
04650 void TranslateCore::custom2_aml_element_attribute_to_x (UAPNode* aml_ele, UAPNode* x_ele) {
04651 return;
04652 }
04653
04654
04655
04656
04657 bool TranslateCore::custom_aml_element_attribute_to_x(UAPNode* aml_root, UAPNode* aml_ele,
04658 UAPNode* aml_attrib, UAPNode* x_ele) {
04659 return false;
04660 }
04661
04662
04663
04664
04665 bool TranslateCore::custom_x_element_to_x_file (UAPNode* x_node, string& comment,
04666 StreamStruct& x_out) {
04667 return false;
04668 }
04669
04670
04671
04672
04673 bool TranslateCore::custom_aml_sector_or_element_to_x (UAPNode* aml_sector,
04674 UAPNode* x_root) {
04675 return false;
04676 }
04677
04678
04679
04680
04681 void TranslateCore::custom_x_ele_attributes_to_x_file (UAPNode* x_node,
04682 const string& ele_class,StreamStruct& x_out) {
04683 return;
04684 }
04685
04686
04687
04688
04689 bool TranslateCore::custom_x_element_attribute_to_aml (UAPNode* x_node, string x_class,
04690 UAPAttribute x_attrib, UAPNode* aml_node) {
04691 return false;
04692 }
04693
04694
04695
04696
04697 void TranslateCore::custom_x_attrib_to_x_file_translate (string& attrib_name,
04698 string& attrib_value) {
04699 return;
04700 }
04701
04702
04703
04704
04705
04706
04707
04708
04709 void TranslateCore::init_lists_core() {
04710
04711
04712
04713 x_attributes_to_upper_case << "particle";
04714
04715
04716
04717 x_attributes_no_case_change << "type";
04718
04719
04720
04721 aml_param_names << "beam" << "lattice" << "global";
04722
04723
04724
04725 x_class_to_aml_map["line"] = "sector";
04726 x_class_to_aml_map["list"] = "list";
04727 x_class_to_aml_map["marker"] = "marker";
04728 x_class_to_aml_map["drift"] = "";
04729 x_class_to_aml_map["sbend"] = "bend";
04730 x_class_to_aml_map["rbend"] = "bend";
04731 x_class_to_aml_map["quadrupole"] = "quadrupole";
04732 x_class_to_aml_map["sextupole"] = "sextupole";
04733 x_class_to_aml_map["octupole"] = "octupole";
04734 x_class_to_aml_map["multipole"] = "multipole";
04735 x_class_to_aml_map["solenoid"] = "solenoid";
04736 x_class_to_aml_map["hkicker"] = "kicker";
04737 x_class_to_aml_map["vkicker"] = "kicker";
04738 x_class_to_aml_map["kicker"] = "kicker";
04739 x_class_to_aml_map["rfcavity"] = "rf_cavity";
04740 x_class_to_aml_map["lcavity"] = "rf_cavity(type=LINAC)";
04741 x_class_to_aml_map["elseparator"] = "electric_kicker";
04742 x_class_to_aml_map["monitor"] = "instrument";
04743 x_class_to_aml_map["ecollimator"] = "aperture(shape=ELLIPTICAL)";
04744 x_class_to_aml_map["rcollimator"] = "aperture";
04745 x_class_to_aml_map["instrument"] = "instrument";
04746 x_class_to_aml_map["beambeam"] = "beambeam";
04747
04748
04749
04750 for (int i = 0; i < 20; i++) {
04751 ostringstream oss;
04752 oss << i;
04753 string k = "k" + oss.str() + "l";
04754 string t = "t" + oss.str();
04755 x_attribs_that_can_have_default << t;
04756 }
04757
04758
04759
04760
04761
04762
04763
04764
04765
04766
04767
04768
04769
04770
04771 if (knl_tn_style_multipoles) {
04772 for (int i = 0; i < 20; i++) {
04773 ostringstream order;
04774 order << i;
04775
04776 string k = "multipole:k" + order.str() + "l";
04777 string t = "multipole:t" + order.str();
04778 string n = "(n=" + order.str() + ")";
04779
04780 register_attribute(k, "multipole:kl" + n);
04781 register_attribute(t, "multipole:tilt" + n);
04782 }
04783 }
04784
04785
04786
04787 register_attribute ("marker:l", "length", "", "", false, true);
04788 register_attribute ("multipole:l", "length", "", "", false, true);
04789 register_attribute ("rbend:l", "length", "", "", false, true);
04790
04791 register_attribute("type", "description:type@string");
04792
04793 register_attribute("l", "length");
04794 register_attribute("k1", "quadrupole:k");
04795 register_attribute("k2", "sextupole:k");
04796 register_attribute("k3", "octupole:k");
04797 register_attribute("ks", "solenoid:ksol");
04798
04799 register_attribute("tilt", "orientation:tilt");
04800
04801 register_attribute("rfcavity:harmon", "rf_cavity:harmonic");
04802
04803 register_attribute("sbend:k1", "quadrupole:k");
04804 register_attribute("sbend:k2", "sextupole:k");
04805 register_attribute("rbend:k1", "quadrupole:k");
04806 register_attribute("rbend:k2", "sextupole:k");
04807
04808 register_attribute("rbend:e1", "bend:e1", "", "", false, true);
04809 register_attribute("rbend:e2", "bend:e2", "", "", false, true);
04810 register_attribute("rbend:angle", "bend:g", "", "", false, true);
04811
04812 register_attribute("sbend:e1", "bend:e1");
04813 register_attribute("sbend:e2", "bend:e2");
04814
04815 register_attribute("h1", "bend:h1");
04816 register_attribute("h2", "bend:h2");
04817 register_attribute("fint", "bend:f_int1");
04818 register_attribute("fintx", "bend:f_int2");
04819 register_attribute("hgap", "bend:h_gap1");
04820 register_attribute("hgapx", "bend:h_gap2");
04821
04822 register_attribute("hkicker:kick", "kicker:x_kick");
04823 register_attribute("vkicker:kick", "kicker:y_kick");
04824 register_attribute("hkick", "kicker:x_kick");
04825 register_attribute("vkick", "kicker:y_kick");
04826
04827 register_attribute("beambeam:charge", "beambeam:charge");
04828
04829 register_attribute("xxx", "lattice[geometry@type]", "", "", false, true);
04830
04831 }
04832
04833
04834
04835
04836
04837
04838
04839
04840 void TranslateCore::init_lists_mad () {
04841
04842
04843
04844 x_attributes_needing_quote_marks << "type";
04845
04846
04847
04848 x_param_names << "twiss" << "beam";
04849
04850
04851
04852 x_class_to_aml_map["hmonitor"] = "instrument";
04853 x_class_to_aml_map["vmonitor"] = "instrument";
04854 x_class_to_aml_map["srotation"] = "patch";
04855 x_class_to_aml_map["yrotation"] = "patch";
04856 x_class_to_aml_map["sequence"] = "sequence";
04857 x_class_to_aml_map["matrix"] = "taylor_map";
04858
04859
04860
04861 StrVec seq_attrib;
04862 seq_attrib << "l" << "refer";
04863 map_element_to_attribs["sequence"] << seq_attrib;
04864
04865 StrVec drift_attrib;
04866 drift_attrib << "l" << "type";
04867 map_element_to_attribs["drift"] << drift_attrib;
04868
04869 StrVec bend_attrib;
04870 bend_attrib << "l" << "tilt" << "angle" << "k1" << "k2"
04871 << "e1" << "e2" << "fint" << "fintx" << "hgap" << "hgapx"
04872 << "h1" << "h2" << "type" << "at";
04873 map_element_to_attribs["sbend"] << bend_attrib;
04874 map_element_to_attribs["rbend"] << bend_attrib;
04875
04876 StrVec quadrupole_attrib;
04877 quadrupole_attrib << "l" << "tilt" << "k1" << "type" << "at";
04878 map_element_to_attribs["quadrupole"] << quadrupole_attrib;
04879
04880 StrVec sextupole_attrib;
04881 sextupole_attrib << "l" << "tilt" << "k2" << "type" << "at";
04882 map_element_to_attribs["sextupole"] << sextupole_attrib;
04883
04884 StrVec rfcavity_attrib;
04885 rfcavity_attrib << "l" << "harmon" << "volt" << "lag"
04886 << "freq" << "betrf" << "gp" << "shunt" << "tfill" << "type" << "at";
04887 map_element_to_attribs["rfcavity"] << rfcavity_attrib;
04888
04889 StrVec elseparator_attrib;
04890 elseparator_attrib << "l" << "tilt" << "e" << "type" << "at";
04891 map_element_to_attribs["elseparator"] << elseparator_attrib;
04892
04893 StrVec beambeam_attrib;
04894 beambeam_attrib << "sigx" << "sigy" << "xma" << "yma"
04895 << "charge" << "angle" << "slices" << "type" << "at";
04896 map_element_to_attribs["beambeam"] << beambeam_attrib;
04897
04898 StrVec marker_attrib;
04899 marker_attrib << "tilt" << "type" << "at";
04900 map_element_to_attribs["marker"] << marker_attrib;
04901
04902 StrVec kicker_attrib;
04903 kicker_attrib << "l" << "tilt" << "hkick" << "vkick" << "type" << "at";
04904 map_element_to_attribs["kicker"] << kicker_attrib;
04905
04906 StrVec octupole_attrib;
04907 octupole_attrib << "l" << "tilt" << "k3" << "type" << "at";
04908 map_element_to_attribs["octupole"] << octupole_attrib;
04909
04910 StrVec beam_attrib;
04911 beam_attrib << "particle" << "mass" << "charge" << "energy" << "pc"
04912 << "gamma" << "ex" << "exn" << "ey" << "eyn" << "et" << "sigt" << "sige"
04913 << "kbunch" << "npart" << "bcurrent" << "bunched" << "radiate";
04914 map_element_to_attribs["beam"] << beam_attrib;
04915
04916 StrVec twiss_attrib;
04917 twiss_attrib << "betx" << "alfx" << "mux" << "bety" << "alfy" << "muy" << "dx"
04918 << "dpx" << "dy" << "dpy" << "x" << "px" << "y" << "dpy" << "wx"
04919 << "phix" << "dmux" << "wy" << "phiy" << "dmuy" << "ddx" << "ddy"
04920 << "ddpx" << "ddpy" << "tape" << "save" << "deltap";
04921 map_element_to_attribs["twiss"] << twiss_attrib;
04922
04923 StrVec solenoid_attrib;
04924 solenoid_attrib << "l" << "ks" << "type" << "at";
04925 map_element_to_attribs["solenoid"] << solenoid_attrib;
04926
04927 StrVec lcavity_attrib;
04928 lcavity_attrib << "l" << "tilt" << "gradient_err" << "coupler_angle"
04929 << "phi0_err" << "lrad" << "dphi0" << "rf_frequency" << "e_loss"
04930 << "gradient" << "phi0" << "delta_e" << "type" << "at";
04931 map_element_to_attribs["lcavity"] << lcavity_attrib;
04932
04933 StrVec monitor_attrib;
04934 monitor_attrib << "l" << "tilt" << "type" << "at";
04935 map_element_to_attribs["monitor"] << monitor_attrib;
04936 map_element_to_attribs["hmonitor"] << monitor_attrib;
04937 map_element_to_attribs["vmonitor"] << monitor_attrib;
04938 map_element_to_attribs["instrument"] << monitor_attrib;
04939
04940 StrVec hvkicker_attrib;
04941 hvkicker_attrib << "l" << "tilt" << "kick" << "type" << "at";
04942 map_element_to_attribs["hkicker"] << hvkicker_attrib;
04943 map_element_to_attribs["vkicker"] << hvkicker_attrib;
04944
04945 StrVec collimator_attrib;
04946 collimator_attrib << "l" << "xsize" << "ysize" << "type" << "at";
04947 map_element_to_attribs["rcollimator"] << collimator_attrib;
04948 map_element_to_attribs["ecollimator"] << collimator_attrib;
04949
04950
04951
04952 x_attribs_that_can_have_default << "tilt" << "fint"
04953 << "fintx" << "charge" << "radiate" << "bunched";
04954
04955
04956
04957
04958
04959
04960
04961
04962
04963
04964
04965
04966
04967
04968
04969 register_attribute("beam:particle", "beam[particle@type]");
04970 register_attribute("beam:energy", "beam[total_energy]", "# * 1e9", "# / 1e9");
04971 register_attribute("beam:mass", "beam[mass]", "# * 1e9", "# / 1e9");
04972 register_attribute("beam:charge", "beam[charge]");
04973 register_attribute("beam:pc", "beam[pc]");
04974 register_attribute("beam:gamma", "beam[gamma]");
04975 register_attribute("beam:ex", "beam[emittance_a]");
04976 register_attribute("beam:exn", "beam[norm_emittance_a]");
04977 register_attribute("beam:ey", "beam[emittance_b]");
04978 register_attribute("beam:eyn", "beam[norm_emittance_b]");
04979 register_attribute("beam:et", "beam[emittance_z]");
04980 register_attribute("beam:sigt", "beam[sig_t]");
04981 register_attribute("beam:sige", "beam[sig_e]");
04982 register_attribute("beam:kbunch", "beam[n_bunches]");
04983 register_attribute("beam:npart", "beam[n_particles]");
04984 register_attribute("beam:bcurrent", "beam[bunch_current]");
04985 register_attribute("beam:bunched", "beam[bunched@logical]");
04986 register_attribute("beam:radiate", "beam[radiate@logical]");
04987
04988
04989
04990 register_attribute("twiss:betx", "lattice[twiss:beta_a]");
04991 register_attribute("twiss:alfx", "lattice[twiss:alpha_a]");
04992 register_attribute("twiss:mux", "lattice[twiss:phase_a]");
04993 register_attribute("twiss:bety", "lattice[twiss:beta_b]");
04994 register_attribute("twiss:alfy", "lattice[twiss:alpha_b]");
04995 register_attribute("twiss:muy", "lattice[twiss:phase_b]");
04996 register_attribute("twiss:dx", "lattice[twiss:eta_x]");
04997 register_attribute("twiss:dpx", "lattice[twiss:etap_x]");
04998 register_attribute("twiss:dy", "lattice[twiss:eta_y]");
04999 register_attribute("twiss:dpy", "lattice[twiss:etap_y]");
05000 register_attribute("twiss:x", "beam[position:x]");
05001 register_attribute("twiss:px", "beam[position:p_x]");
05002 register_attribute("twiss:y", "beam[position:y]");
05003 register_attribute("twiss:dy", "beam[position:p_y]");
05004
05005
05006
05007 register_attribute("multipole:lrad", "length");
05008
05009 register_attribute("yrot:angle", "patch:x_pitch");
05010 register_attribute("srot:angle", "patch:tilt");
05011
05012 register_attribute("beambeam:xma", "beambeam:orientation:x_offset");
05013 register_attribute("beambeam:yma", "beambeam:orientation:y_offset");
05014 register_attribute("beambeam:angle", "beambeam:orientation:x_pitch");
05015 register_attribute("beambeam:slices", "methods:n_steps");
05016
05017 register_attribute("sbend:angle", "bend:angle");
05018
05019 register_attribute("beambeam:sigx", "beambeam:sig_x");
05020 register_attribute("beambeam:sigy", "beambeam:sig_y");
05021
05022 register_attribute("xsize", "aperture:x_limit");
05023 register_attribute("ysize", "aperture:y_limit");
05024
05025
05026
05027
05028
05029 register_constant("degrad", "pi", "pi / 180", "", false, true);
05030 register_constant("raddeg", "degrees");
05031 register_constant("emass", "m_electron", "m_electron * 1e-6", "emass / 1e-6");
05032 register_constant("pmass", "m_proton", "m_proton * 1e-6", "pmass / 1e-6");
05033 register_constant("clight", "c_light");
05034
05035 register_constant("twopi", "hz", "", "", true, false);
05036 register_constant("sqrt(2)", "sqrt_2", "", "");
05037 register_constant("2.8179380e-15", "r_e", "", "");
05038 register_constant("1.5346980e-18", "r_p", "", "");
05039 register_constant("1.6021892e-19", "e_charge", "", "");
05040 register_constant("6.6021892e-34", "h_plank", "", "");
05041 register_constant("1.054591e-34", "h_bar_plank", "", "");
05042
05043
05044 }