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