1
/***************************************************************************
2
copyright : (C) 2005 by Inge Wallin
3
email : inge@lysator.liu.se
4
***************************************************************************/
5
/***************************************************************************
7
* This program is free software; you can redistribute it and/or modify *
8
* it under the terms of the GNU General Public License as published by *
9
* the Free Software Foundation; either version 2 of the License, or *
10
* (at your option) any later version. *
12
***************************************************************************/
14
#ifndef MOLECULEPARSER_H
15
#define MOLECULEPARSER_H
17
#include "libkdeedu_science_export.h"
22
#include <QtCore/QMap>
23
#include <QtCore/QList>
30
class SCIENCE_EXPORT ElementCount
36
ElementCount(Element *_element, int _count)
45
ElementCount(Element *_element)
58
Element *element() const { return m_element; }
61
* @return the number of occurrences of the Element
63
int count() const { return m_count; }
66
* Add @p _count occurrences of the Element
67
* @param _count The number of times the Element occurs
69
void add(int _count) { m_count += _count; }
70
void multiply(int _factor) { m_count *= _factor; }
73
* The Element of the object
77
* The number of occurrences
84
* This class is used to count the elements in the molecule
85
* which is being calculated
87
* @class ElementCountMap
90
class SCIENCE_EXPORT ElementCountMap
104
* Clear the map of ElementCount pointers
111
* @param _element the searched Element
112
* @return the Element which is searched
114
ElementCount *search(Element *_element);
119
void add(ElementCountMap &_map);
122
* Returns the elements in the molecule. For example, if the molecule
123
* is CO2, a list with C and O will be returned.
124
* @return the elements in the molecule
126
QList<Element*> elements();
132
void add(Element *_element, int _count);
137
void multiply(int _factor);
139
QList<ElementCount*> map(){
144
QList<ElementCount*> m_map;
150
* @class MoleculeParser
152
* Parse molecule formulas.
156
* MoleculeParser parser;
157
* QString chemical_formula = "C2H5OH";
160
* if (parser.weight(chemical_formula, &weight))
161
* cout << "Weight of " << chemical_formula << " = " << weight << ".\n";
163
* cout << "Parse error\n";
166
* If a short form of a compound is specified, it will be expanded.
167
* Example :- EtOH -> (C2H5OH)
169
* MoleculeParser parser;
170
* QString chemical_formula = "EtOH";
173
* if (parser.weight(chemical_formula, &weight))
174
* cout << "Weight of " << chemical_formula << " = " << weight << ".\n";
176
* cout << "Parse error\n";
179
* @author Inge Wallin
180
* @author Kashyap R Puranik
182
class SCIENCE_EXPORT MoleculeParser : public Parser {
186
* @param list This list of chemical elements will be used internally
187
* for searching and matching with searched strings
190
MoleculeParser( const QList<Element*>& list );
195
* @param _str @ref Parser::start the parsing with @p _str
197
MoleculeParser( const QString& _str);
202
virtual ~MoleculeParser();
205
* Try to parse the molecule @p molecule and get the weight of it.
206
* The calculated weight is stored in @p _result.
208
* @param _moleculeString
212
* @return whether the parsing was successful or not
214
bool weight(const QString& _moleculeString,
216
ElementCountMap *_resultMap);
218
QSet<QString> aliasList();
221
bool parseSubmolecule(double *_resultMass,
222
ElementCountMap *_resultMap);
223
bool parseTerm(double *_resultMass,
224
ElementCountMap *_resultMap);
225
// This function expands the molecule string
226
// eg expandFormula(EtOH) returns (C2H5)OH
227
QString expandFormula(const QString& _shortMolecularMass);
228
// This function expands a term
229
// eg expandTerm(Et) returns (C2H5)
230
QString expandTerm(const QString& _group);
232
QList<Element*> m_elementList;
234
static const int ELEMENT_TOKEN = 300;
236
Element *lookupElement( const QString& _name );
238
QMap<Element*, int> m_elementMap;
240
// Contains the list of aliases eg, { "Et - C2H5", "Me - CH3"}
241
QSet<QString> *m_aliasList;
242
//if this booloean is "true" the parser found an error
248
* Extends the standard tokenizer in Parser::getNextToken().
250
virtual int getNextToken();
253
Element *m_elementVal; // Valid if m_nextToken == ELEMENT_TOKEN