File indexing completed on 2024-04-14 03:39:20

0001 /*************************************************************************************
0002  *  Copyright (C) 2007-2009 by Aleix Pol <aleixpol@kde.org>                          *
0003  *  Copyright (C) 2010-2012 by Percy Camilo T. Aucahuasi <percy.camilo.ta@gmail.com> *
0004  *                                                                                   *
0005  *  This program is free software; you can redistribute it and/or                    *
0006  *  modify it under the terms of the GNU General Public License                      *
0007  *  as published by the Free Software Foundation; either version 2                   *
0008  *  of the License, or (at your option) any later version.                           *
0009  *                                                                                   *
0010  *  This program is distributed in the hope that it will be useful,                  *
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
0013  *  GNU General Public License for more details.                                     *
0014  *                                                                                   *
0015  *  You should have received a copy of the GNU General Public License                *
0016  *  along with this program; if not, write to the Free Software                      *
0017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0018  *************************************************************************************/
0019 
0020 #include "functiongraphfactory.h"
0021 #include "abstractfunctiongraph.h"
0022 
0023 #include <QStringList>
0024 #include <QCoreApplication>
0025 #include <analitza/analyzer.h>
0026 
0027 using namespace Analitza;
0028 
0029 FunctionGraphFactory* FunctionGraphFactory::m_self=nullptr;
0030 
0031 QString FunctionGraphFactory::typeName(const QString& id) const
0032 {
0033     return QCoreApplication::tr(typeNameFunctions[id]);
0034 }
0035 
0036 Analitza::ExpressionType FunctionGraphFactory::expressionType(const QString& id) const
0037 {
0038     return expressionTypeFunctions[id]();
0039 }
0040 
0041 Dimension FunctionGraphFactory::spaceDimension(const QString& id) const
0042 {
0043     return spaceDimensions[id];
0044 }
0045 
0046 CoordinateSystem FunctionGraphFactory::coordinateSystem(const QString& id) const
0047 {
0048     return coordinateSystemFunctions[id];
0049 }
0050 
0051 QString FunctionGraphFactory::iconName(const QString& id) const
0052 {
0053     return iconNameFunctions[id];
0054 }
0055 
0056 QStringList FunctionGraphFactory::examples(const QString& id) const
0057 {
0058     return examplesFunctions[id]();
0059 }
0060 
0061 QStringList FunctionGraphFactory::examples(Dimension dim) const
0062 {
0063     QStringList ret;
0064     QStringList ids = spaceDimensions.keys(dim);
0065     foreach(const QString& id, ids) {
0066         ret += examplesFunctions[id]();
0067     }
0068     return ret;
0069 }
0070 
0071 FunctionGraphFactory* FunctionGraphFactory::self()
0072 {
0073     if(!m_self)
0074         m_self=new FunctionGraphFactory;
0075     return m_self;
0076 }
0077 
0078 bool FunctionGraphFactory::registerFunctionGraph(Dimension dim, PlotItemConstuctor constructor,
0079                                                  BuilderFunctionWithVars builderFunctionWithVars, const char* typeNameFunction,
0080         ExpressionTypeFunction expressionTypeFunction, 
0081         CoordinateSystem coordinateSystemFunction, const QStringList& _arguments,
0082         const QString& iconNameFunction, ExamplesFunction examplesFunction)
0083 {
0084     QStringList arguments(_arguments);
0085     std::sort(arguments.begin(), arguments.end());
0086 
0087     //TODO: turn this id into an internal struct
0088     QString id = QString::number((int)dim)+'|'+
0089                  QString::number((int)coordinateSystemFunction)+'|'+
0090                  arguments.join(QStringLiteral(","));
0091     Q_ASSERT(!contains(id)); // verificar que no se registren los mismos tipos
0092 
0093     typeNameFunctions[id] = typeNameFunction;
0094     expressionTypeFunctions[id] = expressionTypeFunction;
0095     spaceDimensions[id] = dim;
0096     coordinateSystemFunctions[id] = coordinateSystemFunction;
0097     argumentsFunctions[id] = arguments;
0098     iconNameFunctions[id] = iconNameFunction;
0099     examplesFunctions[id] = examplesFunction;
0100     builderFunctionsWithVars[id] = builderFunctionWithVars;
0101     plotConstructor[id] = constructor;
0102 
0103     return true;
0104 }
0105 
0106 QString FunctionGraphFactory::trait(const Analitza::Expression& expr, const Analitza::ExpressionType& t, Dimension dim) const
0107 {
0108     Q_ASSERT(!expr.isEquation());
0109     QStringList args = expr.bvarList();
0110     std::sort(args.begin(), args.end());
0111 
0112     QString key;
0113     for (int i = 0; i < argumentsFunctions.size() && key.isEmpty(); ++i) {
0114 //         qDebug() << "---" << args << dim << t.toString() << " || " << argumentsFunctions.values()[i] << spaceDimensions.values()[i] << expressionTypeFunctions.values()[i]().toString();
0115         if (args == argumentsFunctions.values()[i]
0116             && dim == spaceDimensions.values()[i]
0117             && t.canReduceTo(expressionTypeFunctions.values()[i]()))
0118         {
0119             key = typeNameFunctions.key(typeNameFunctions.values()[i]);
0120         }
0121     }
0122     
0123     return key;
0124 }
0125 
0126 bool FunctionGraphFactory::contains(const QString& id) const
0127 {
0128     return builderFunctionsWithVars.contains(id);
0129 }
0130 
0131 AbstractFunctionGraph* FunctionGraphFactory::build(const QString& id, const Analitza::Expression& exp, const QSharedPointer<Analitza::Variables>& v) const
0132 {
0133     Q_ASSERT(builderFunctionsWithVars.contains(id));
0134     AbstractFunctionGraph* ret = builderFunctionsWithVars.value(id)(exp, v);
0135     Q_ASSERT(ret);
0136     ret->setInternalId(id);
0137     return ret;
0138 }
0139 
0140 FunctionGraph* FunctionGraphFactory::buildItem(const QString& id, const Analitza::Expression& exp, const QSharedPointer<Analitza::Variables>& v) const
0141 {
0142     return plotConstructor[id](build(id, exp, v));
0143 }
0144 
0145 QMap< QString, QPair< QStringList, Analitza::ExpressionType > > FunctionGraphFactory::registeredFunctionGraphs() const
0146 {
0147     QMap< QString, QPair< QStringList, Analitza::ExpressionType > > ret;
0148     
0149     for (int i = 0; i < typeNameFunctions.size(); ++i)
0150         ret[typeNameFunctions.values()[i]] = qMakePair( argumentsFunctions.values()[i],
0151             expressionTypeFunctions.values()[i]()); 
0152 
0153     return ret;
0154 }