Seamly2D
Code documentation
calculator.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * Copyright (C) 2017 Seamly, LLC *
4  * *
5  * https://github.com/fashionfreedom/seamly2d *
6  * *
7  ***************************************************************************
8  **
9  ** Seamly2D 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 3 of the License, or
12  ** (at your option) any later version.
13  **
14  ** Seamly2D 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 Seamly2D. If not, see <http://www.gnu.org/licenses/>.
21  **
22  **************************************************************************
23 
24  ************************************************************************
25  **
26  ** @file calculator.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date November 15, 2013
29  **
30  ** @brief
31  ** @copyright
32  ** This source code is part of the Valentine project, a pattern making
33  ** program, whose allow create and modeling patterns of clothing.
34  ** Copyright (C) 2013-2015 Seamly2D project
35  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
36  **
37  ** Seamly2D is free software: you can redistribute it and/or modify
38  ** it under the terms of the GNU General Public License as published by
39  ** the Free Software Foundation, either version 3 of the License, or
40  ** (at your option) any later version.
41  **
42  ** Seamly2D is distributed in the hope that it will be useful,
43  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
44  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45  ** GNU General Public License for more details.
46  **
47  ** You should have received a copy of the GNU General Public License
48  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
49  **
50  *************************************************************************/
51 
52 #include "calculator.h"
53 
54 #include <QStaticStringData>
55 #include <QStringData>
56 #include <QStringDataPtr>
57 #include <QStringList>
58 
59 #include "../vmisc/def.h"
60 #include "../qmuparser/qmuparsererror.h"
62 #include <QSharedPointer>
63 //---------------------------------------------------------------------------------------------------------------------
64 /**
65  * @brief Calculator class wraper for QMuParser. Make easy initialization math parser.
66  *
67  * This constructor hide initialization variables, operators, character sets.
68  * Use this constuctor for evaluation formula. All formulas must be converted to internal look.
69  * Example:
70  *
71  * const QString formula = qApp->FormulaFromUser(edit->text());
72  * Calculator *cal = new Calculator(data, patternType);
73  * const qreal result = cal->EvalFormula(data->PlainVariables(), formula);
74  * delete cal;
75  *
76  */
78  :QmuFormulaBase()
79 {
80  InitCharSets();
81  setAllowSubexpressions(false);//Only one expression per time
82 
83  SetSepForEval();
84 }
85 
86 //---------------------------------------------------------------------------------------------------------------------
87 /**
88  * @brief eval calculate formula.
89  *
90  * First we try eval expression without adding variables. If it fail, we take tokens from expression and add variables
91  * to parser and try again.
92  *
93  * @param formula string of formula.
94  * @return value of formula.
95  */
96 qreal Calculator::EvalFormula(const QHash<QString, QSharedPointer<VInternalVariable>> *vars, const QString &formula)
97 {
98  // Parser doesn't know any variable on this stage. So, we just use variable factory that for each unknown variable
99  // set value to 0.
100  SetVarFactory(AddVariable, this);
101  SetSepForEval();//Reset separators options
102 
103  SetExpr(formula);
104 
105  qreal result = 0;
106  result = Eval();
107 
108  QMap<int, QString> tokens = this->GetTokens();
109 
110  // Remove "-" from tokens list if exist. If don't do that unary minus operation will broken.
111  RemoveAll(tokens, QStringLiteral("-"));
112 
113  for (int i = 0; i < builInFunctions.size(); ++i)
114  {
115  if (tokens.isEmpty())
116  {
117  break;
118  }
119  RemoveAll(tokens, builInFunctions.at(i));
120  }
121 
122  if (tokens.isEmpty())
123  {
124  return result; // We have found only numbers in expression.
125  }
126 
127  // Add variables to parser because we have deal with expression with variables.
128  InitVariables(vars, tokens, formula);
129  return Eval();
130 }
131 
132 //---------------------------------------------------------------------------------------------------------------------
133 /**
134  * @brief Calculator::InitVariables add variables to parser.
135  *
136  * For optimization purpose we try don't add variables that we don't need.
137  *
138  * @param vars list of variables.
139  * @param tokens all tokens (measurements names, variables with lengths) that parser have found in expression.
140  * @param formula expression, need for throwing better error message.
141  */
143  const QMap<int, QString> &tokens, const QString &formula)
144 {
145  QMap<int, QString>::const_iterator i = tokens.constBegin();
146  while (i != tokens.constEnd())
147  {
148  bool found = false;
149  if (vars->contains(i.value()))
150  {
151  DefineVar(i.value(), vars->value(i.value())->GetValue());
152  found = true;
153  }
154 
155  if (found == false && builInFunctions.contains(i.value()))
156  {// We have found built-in function
157  found = true;
158  }
159 
160  if (found == false)
161  {
162  throw qmu::QmuParserError (qmu::ecUNASSIGNABLE_TOKEN, i.value(), formula, i.key());
163  }
164  ++i;
165  }
166 }
qreal EvalFormula(const QHash< QString, QSharedPointer< VInternalVariable > > *vars, const QString &formula)
eval calculate formula.
Definition: calculator.cpp:96
void InitVariables(const QHash< QString, QSharedPointer< VInternalVariable > > *vars, const QMap< int, QString > &tokens, const QString &formula)
Calculator::InitVariables add variables to parser.
Definition: calculator.cpp:142
Calculator()
Calculator class wraper for QMuParser. Make easy initialization math parser.
Definition: calculator.cpp:77
virtual void InitCharSets() Q_DECL_OVERRIDE
InitCharSets init character set for parser.
void SetSepForEval()
SetSepForEval set separators for eval. Each expression eval in internal (C) locale.
static void RemoveAll(QMap< int, QString > &map, const QString &val)
RemoveAll remove token from token list.
static qreal * AddVariable(const QString &a_szName, void *a_pUserData)
void setAllowSubexpressions(bool value)
QMap< int, QString > GetTokens() const
void DefineVar(const QString &a_sName, qreal *a_pVar)
Add a user defined variable.
void SetExpr(const QString &a_sExpr)
Set the formula.
void SetVarFactory(facfun_type a_pFactory, void *pUserData=nullptr)
Set a function that can create variable pointer for unknown expression variables.
qreal Eval() const
Calculate the result.
Error class of the parser.
const QStringList builInFunctions
Definition: def.cpp:120
@ ecUNASSIGNABLE_TOKEN
Token cant be identified.