File indexing completed on 2024-04-28 03:40:46

0001 /*************************************************************************************
0002  *  Copyright (C) 2010 by Aleix Pol <aleixpol@kde.org>                               *
0003  *                                                                                   *
0004  *  This program is free software; you can redistribute it and/or                    *
0005  *  modify it under the terms of the GNU General Public License                      *
0006  *  as published by the Free Software Foundation; either version 2                   *
0007  *  of the License, or (at your option) any later version.                           *
0008  *                                                                                   *
0009  *  This program is distributed in the hope that it will be useful,                  *
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
0012  *  GNU General Public License for more details.                                     *
0013  *                                                                                   *
0014  *  You should have received a copy of the GNU General Public License                *
0015  *  along with this program; if not, write to the Free Software                      *
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0017  *************************************************************************************/
0018 
0019 
0020 #include "transformation.h"
0021 #include <analitza/object.h>
0022 #include "substituteexpression.h"
0023 #include <analitza/expression.h>
0024 #include "container.h"
0025 
0026 using namespace Analitza;
0027 
0028 Transformation::Transformation(const Object* first, const Object* second, const QMap< QString, Transformation::treeCheck >& conditions)
0029     : first(first), second(second)
0030     , conditions(conditions)
0031 {}
0032 
0033 Transformation::Transformation(const Object* first, const Object* second)
0034     : first(first), second(second)
0035 {}
0036 
0037 Analitza::Object* Transformation::applyTransformation(const Analitza::Object* input) const {
0038     QMap<QString, const Object*> matchedValues;
0039     bool match = first->matches(input, &matchedValues);
0040     
0041 //     qDebug() << "beeeeee" << input->toString() << first->toString() << match;
0042     if(match) {
0043         bool satisfied=true;
0044         for(QMap<QString, treeCheck>::const_iterator it=conditions.constBegin(), itEnd=conditions.constEnd(); satisfied && it!=itEnd; ++it) {
0045             Q_ASSERT(matchedValues.contains(it.key()));
0046             const Object* value = matchedValues.value(it.key());
0047             
0048             satisfied = it.value()(value);
0049         }
0050         
0051         if(satisfied) {
0052 //             qDebug() << "match!" << first->toString() << input->toString();
0053             SubstituteExpression exp;
0054             Object* obj=exp.run(second.data(), matchedValues);
0055             return obj;
0056         }
0057     }
0058     return nullptr;
0059 }
0060 
0061 const Object* Transformation::parse(const QString& exp)
0062 {
0063     Expression e(exp);
0064 //     if(!e.isCorrect()) qDebug() << "lelele" << exp << e.error();
0065     Q_ASSERT(e.isCorrect());
0066     Object* tree = e.tree();
0067     e.setTree(nullptr);
0068     
0069     //We remove the math node
0070     Container* root = static_cast<Container*>(tree);
0071     
0072     Q_ASSERT(root->m_params.size()==1);
0073     tree=root->m_params.takeFirst();
0074     delete root;
0075     
0076     return tree;
0077 }