| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * TremoloParser_ElementKeys.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: Mar 12, 2012
 | 
|---|
| 12 |  *      Author: heber
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | 
 | 
|---|
| 16 | // include config.h
 | 
|---|
| 17 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 18 | #include <config.h>
 | 
|---|
| 19 | #endif
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 22 | 
 | 
|---|
| 23 | #include "TremoloParser_ElementKeys.hpp"
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #include <boost/tokenizer.hpp>
 | 
|---|
| 26 | #include <iostream>
 | 
|---|
| 27 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 28 | 
 | 
|---|
| 29 | #include "Element/element.hpp"
 | 
|---|
| 30 | #include "Element/periodentafel.hpp"
 | 
|---|
| 31 | #include "Parser/Exceptions.hpp"
 | 
|---|
| 32 | #include "World.hpp"
 | 
|---|
| 33 | 
 | 
|---|
| 34 | /** Parses a .potentials file and creates from it the knownTypes file.
 | 
|---|
| 35 |  *
 | 
|---|
| 36 |  * @param file input stream of .potentials file
 | 
|---|
| 37 |  */
 | 
|---|
| 38 | void ElementKeys::parseKnownTypes(std::istream &file)
 | 
|---|
| 39 | {
 | 
|---|
| 40 |   const periodentafel *periode = World::getInstance().getPeriode();
 | 
|---|
| 41 |   // remove old mapping
 | 
|---|
| 42 |   clear();
 | 
|---|
| 43 | 
 | 
|---|
| 44 | //  LOG(3, "INFO: additionalAtomData contains: " << additionalAtomData);
 | 
|---|
| 45 | 
 | 
|---|
| 46 |   // parse in file
 | 
|---|
| 47 |   typedef boost::tokenizer<boost::char_separator<char> >
 | 
|---|
| 48 |       tokenizer;
 | 
|---|
| 49 |   boost::char_separator<char> tokensep(":\t ,;");
 | 
|---|
| 50 |   boost::char_separator<char> equalitysep("\t =");
 | 
|---|
| 51 |   std::string line;
 | 
|---|
| 52 |   while (file.good()) {
 | 
|---|
| 53 |     std::getline( file, line );
 | 
|---|
| 54 |     LOG(4, "INFO: full line of parameters is '" << line << "'");
 | 
|---|
| 55 |     if (line.find("particle:") != string::npos) {
 | 
|---|
| 56 |       LOG(3, "INFO: found line '" << line << "' containing keyword 'particle:'.");
 | 
|---|
| 57 |       tokenizer tokens(line, tokensep);
 | 
|---|
| 58 |       ASSERT(tokens.begin() != tokens.end(),
 | 
|---|
| 59 |           "FormatParser< tremolo >::parseKnownTypes() - line with 'particle:' but no particles separated by comma.");
 | 
|---|
| 60 |       // look for particle_type
 | 
|---|
| 61 |       std::string particle_type("NULL");
 | 
|---|
| 62 |       std::string element_type("NULL");
 | 
|---|
| 63 |       for (tokenizer::iterator tok_iter = tokens.begin();
 | 
|---|
| 64 |           tok_iter != tokens.end();
 | 
|---|
| 65 |           ++tok_iter) {
 | 
|---|
| 66 |         if ((*tok_iter).find("particle_type") != string::npos) {
 | 
|---|
| 67 |           LOG(3, "INFO: found token '" << *tok_iter << "' containing keyword 'particle_type'.");
 | 
|---|
| 68 |           tokenizer token((*tok_iter), equalitysep);
 | 
|---|
| 69 |           ASSERT(token.begin() != token.end(),
 | 
|---|
| 70 |                     "FormatParser< tremolo >::parseKnownTypes() - could not split particle_type by equality sign");
 | 
|---|
| 71 |           tokenizer::iterator particle_iter = token.begin();
 | 
|---|
| 72 |           particle_iter++;
 | 
|---|
| 73 |           particle_type = *particle_iter;
 | 
|---|
| 74 |         }
 | 
|---|
| 75 |         if ((*tok_iter).find("element_name") != string::npos) {
 | 
|---|
| 76 |           LOG(3, "INFO: found token '" << *tok_iter << "' containing keyword 'element_name'.");
 | 
|---|
| 77 |           tokenizer token((*tok_iter), equalitysep);
 | 
|---|
| 78 |           ASSERT(token.begin() != token.end(),
 | 
|---|
| 79 |                     "FormatParser< tremolo >::parseKnownTypes() - could not split particle_type by equality sign");
 | 
|---|
| 80 |           tokenizer::iterator element_iter = token.begin();
 | 
|---|
| 81 |           element_iter++;
 | 
|---|
| 82 |           element_type = *element_iter;
 | 
|---|
| 83 |         }
 | 
|---|
| 84 |       }
 | 
|---|
| 85 |       if ((particle_type != "NULL") && (element_type != "NULL")) {
 | 
|---|
| 86 |         if (periode->FindElement(element_type) != NULL) {
 | 
|---|
| 87 |           LOG(1, "INFO: Added Type " << particle_type << " as reference to element " << element_type << ".");
 | 
|---|
| 88 |           setType(particle_type, element_type);
 | 
|---|
| 89 |         } else {
 | 
|---|
| 90 |           ELOG(1, "INFO: Either Type " << particle_type << " or " << element_type << " could not be recognized." );
 | 
|---|
| 91 |         }
 | 
|---|
| 92 |       } else {
 | 
|---|
| 93 |         ELOG(3, "Line does not contain both 'particle_type' and 'element_name' as keys." );
 | 
|---|
| 94 |       }
 | 
|---|
| 95 |     }
 | 
|---|
| 96 |   }
 | 
|---|
| 97 | }
 | 
|---|
| 98 | 
 | 
|---|
| 99 | /** Creates knownTypes as the identity, e.g. H -> H, He -> He, ... .
 | 
|---|
| 100 |  *
 | 
|---|
| 101 |  */
 | 
|---|
| 102 | void ElementKeys::createKnownTypesByIdentity()
 | 
|---|
| 103 | {
 | 
|---|
| 104 |   // remove old mapping
 | 
|---|
| 105 |   clear();
 | 
|---|
| 106 |   // make knownTypes the identity mapping
 | 
|---|
| 107 |   const periodentafel *periode = World::getInstance().getPeriode();
 | 
|---|
| 108 |   for (periodentafel::const_iterator iter = periode->begin();
 | 
|---|
| 109 |       iter != periode->end();
 | 
|---|
| 110 |       ++iter) {
 | 
|---|
| 111 |     setType( iter->second->getSymbol(), iter->second->getSymbol());
 | 
|---|
| 112 |   }
 | 
|---|
| 113 | }
 | 
|---|
| 114 | 
 | 
|---|
| 115 | /** Getter for value indexed by \a type.
 | 
|---|
| 116 |  *
 | 
|---|
| 117 |  * @param type key for map
 | 
|---|
| 118 |  * @return value stored under \a type
 | 
|---|
| 119 |  */
 | 
|---|
| 120 | const std::string &ElementKeys::getType(const std::string &type) const throw (IllegalParserKeyException)
 | 
|---|
| 121 | {
 | 
|---|
| 122 |   const_iterator iter = find(type);
 | 
|---|
| 123 |   if (iter == end()) {
 | 
|---|
| 124 |     throw IllegalParserKeyException() << ParserMapLookup(type);
 | 
|---|
| 125 |   } else
 | 
|---|
| 126 |     return iter->second;
 | 
|---|
| 127 | }
 | 
|---|
| 128 | 
 | 
|---|
| 129 | /** Getter for value indexed by \a type.
 | 
|---|
| 130 |  *
 | 
|---|
| 131 |  * @param type key for map
 | 
|---|
| 132 |  * @return value stored under \a type
 | 
|---|
| 133 |  */
 | 
|---|
| 134 | void ElementKeys::setType(const std::string &type, const std::string &value) throw (IllegalParserKeyException)
 | 
|---|
| 135 | {
 | 
|---|
| 136 |   const_iterator iter = find(type);
 | 
|---|
| 137 |   if (iter != end()) {
 | 
|---|
| 138 |     throw IllegalParserKeyException() << ParserMapLookup(type);
 | 
|---|
| 139 |   } else {
 | 
|---|
| 140 |     insert( make_pair(type,value) );
 | 
|---|
| 141 |     LOG(3, "DEBUG: Key " << type << " refers to element " << at(type) << ".");
 | 
|---|
| 142 |   }
 | 
|---|
| 143 | }
 | 
|---|