~ubuntu-branches/ubuntu/precise/kalzium/precise

« back to all changes in this revision

Viewing changes to libscience/moleculeparser.h

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-03 12:28:58 UTC
  • Revision ID: james.westby@ubuntu.com-20110703122858-q1yyxncs89e4w0hs
Tags: upstream-4.6.90+repack
Import upstream version 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
    copyright            : (C) 2005 by Inge Wallin
 
3
    email                : inge@lysator.liu.se
 
4
 ***************************************************************************/
 
5
/***************************************************************************
 
6
 *                                                                         *
 
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.                                   *
 
11
 *                                                                         *
 
12
 ***************************************************************************/
 
13
 
 
14
#ifndef MOLECULEPARSER_H
 
15
#define MOLECULEPARSER_H
 
16
 
 
17
#include "libkdeedu_science_export.h"
 
18
 
 
19
#include "element.h"
 
20
#include "parser.h"
 
21
 
 
22
#include <QtCore/QMap>
 
23
#include <QtCore/QList>
 
24
 
 
25
 
 
26
/**
 
27
 * @class ElementCount
 
28
 * @author Inge Wallin
 
29
 */
 
30
class SCIENCE_EXPORT ElementCount 
 
31
{
 
32
        public:
 
33
                /**
 
34
                 * Constructor
 
35
                 */
 
36
                ElementCount(Element *_element, int _count)
 
37
                {
 
38
                        m_element = _element;
 
39
                        m_count   = _count;
 
40
                }
 
41
                
 
42
                /**
 
43
                 * Constructor
 
44
                 */
 
45
                ElementCount(Element *_element)
 
46
                {
 
47
                        m_element = _element;
 
48
                        m_count   = 0;
 
49
                }
 
50
                /**
 
51
                 * Destructor
 
52
                 */
 
53
                ~ElementCount();
 
54
 
 
55
                /**
 
56
                 * @return the Element
 
57
                 */
 
58
                Element *element() const    { return m_element;   }
 
59
 
 
60
                /**
 
61
                 * @return the number of occurrences of the Element
 
62
                 */
 
63
                int   count()  const        { return m_count;     }
 
64
                
 
65
                /**
 
66
                 * Add @p _count occurrences of the Element
 
67
                 * @param _count The number of times the Element occurs
 
68
                 */
 
69
                void  add(int _count)       { m_count += _count;  }
 
70
                void  multiply(int _factor) { m_count *= _factor; }
 
71
 
 
72
                /**
 
73
                 * The Element of the object
 
74
                 */
 
75
                Element  *m_element;
 
76
                /**
 
77
                 * The number of occurrences
 
78
                 */
 
79
                int       m_count;
 
80
};
 
81
 
 
82
 
 
83
/**
 
84
 * This class is used to count the elements in the molecule
 
85
 * which is being calculated
 
86
 * 
 
87
 * @class ElementCountMap
 
88
 * @author Inge Wallin
 
89
 */
 
90
class SCIENCE_EXPORT ElementCountMap 
 
91
{
 
92
        public:
 
93
                /**
 
94
                 * Constructor
 
95
                 */
 
96
                ElementCountMap();
 
97
 
 
98
                /**
 
99
                 * Destructor
 
100
                 */
 
101
                ~ElementCountMap();
 
102
 
 
103
                /**
 
104
                 * Clear the map of ElementCount pointers
 
105
                 */
 
106
                void  clear(){ 
 
107
        m_map.clear(); 
 
108
    }
 
109
 
 
110
                /**
 
111
                 * @param _element the searched Element
 
112
                 * @return the Element which is searched
 
113
                 */
 
114
                ElementCount  *search(Element *_element);
 
115
 
 
116
                /**
 
117
                 * @param _map
 
118
                 */
 
119
                void           add(ElementCountMap &_map);
 
120
 
 
121
                /**
 
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
 
125
                 */
 
126
                QList<Element*> elements();
 
127
 
 
128
                /**
 
129
                 * @param _element
 
130
                 * @param _count
 
131
                 */
 
132
                void           add(Element *_element, int _count);
 
133
 
 
134
                /**
 
135
                 * @param _factor
 
136
                 */
 
137
                void           multiply(int _factor);
 
138
 
 
139
                QList<ElementCount*>  map(){
 
140
        return m_map;
 
141
    }
 
142
 
 
143
        private:
 
144
                QList<ElementCount*>  m_map;
 
145
};
 
146
 
 
147
 
 
148
 
 
149
/**
 
150
 * @class MoleculeParser
 
151
 *
 
152
 * Parse molecule formulas.
 
153
 *
 
154
 * Usage:
 
155
 * @code
 
156
 *   MoleculeParser  parser;
 
157
 *   QString         chemical_formula = "C2H5OH";
 
158
 *   double          weight;
 
159
 *
 
160
 *   if (parser.weight(chemical_formula, &weight))
 
161
 *     cout << "Weight of " << chemical_formula << " = " << weight << ".\n";
 
162
 *   else
 
163
 *     cout << "Parse error\n";
 
164
 * @endcode
 
165
 *
 
166
 * If a short form of a compound is specified, it will be expanded.
 
167
 * Example :- EtOH -> (C2H5OH)
 
168
 * @code
 
169
 *   MoleculeParser  parser;
 
170
 *   QString         chemical_formula = "EtOH";
 
171
 *   double          weight;
 
172
 *
 
173
 *   if (parser.weight(chemical_formula, &weight))
 
174
 *     cout << "Weight of " << chemical_formula << " = " << weight << ".\n";
 
175
 *   else
 
176
 *     cout << "Parse error\n";
 
177
 * @endcode 
 
178
 *
 
179
 * @author Inge Wallin
 
180
 * @author Kashyap R Puranik
 
181
 */
 
182
class SCIENCE_EXPORT MoleculeParser : public Parser {
 
183
 
 
184
public:
 
185
        /**
 
186
         * @param list This list of chemical elements will be used internally
 
187
         * for searching and matching with searched strings
 
188
         * Constructor
 
189
         */
 
190
    MoleculeParser( const QList<Element*>& list );
 
191
        
 
192
        /**
 
193
         * Constructor
 
194
         * 
 
195
         * @param _str @ref Parser::start the parsing with @p _str
 
196
         */
 
197
    MoleculeParser( const QString& _str);
 
198
        
 
199
        /**
 
200
         * Destructor
 
201
         */
 
202
    virtual ~MoleculeParser();
 
203
 
 
204
    /**
 
205
     * Try to parse the molecule @p molecule and get the weight of it.
 
206
     * The calculated weight is stored in @p _result.
 
207
         *
 
208
         * @param _moleculeString
 
209
         * @param _resultMass
 
210
         * @param _resultMap
 
211
     *
 
212
     * @return whether the parsing was successful or not
 
213
     */
 
214
    bool  weight(const QString&         _moleculeString,
 
215
                                 double          *_resultMass,
 
216
                                 ElementCountMap *_resultMap);
 
217
                                 
 
218
        QSet<QString> aliasList();
 
219
 private:
 
220
    // Helper functions
 
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);
 
231
        
 
232
        QList<Element*> m_elementList;
 
233
 
 
234
    static const int  ELEMENT_TOKEN = 300;
 
235
 
 
236
    Element  *lookupElement( const QString& _name );
 
237
 
 
238
    QMap<Element*, int> m_elementMap;
 
239
        
 
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
 
243
        bool             m_error;
 
244
 
 
245
protected:
 
246
 
 
247
    /**
 
248
     * Extends the standard tokenizer in Parser::getNextToken().
 
249
     */
 
250
    virtual int  getNextToken();
 
251
 
 
252
private:
 
253
    Element  *m_elementVal;     // Valid if m_nextToken == ELEMENT_TOKEN
 
254
};
 
255
 
 
256
#endif