File indexing completed on 2024-04-28 03:40:43
0001 /************************************************************************************* 0002 * Copyright (C) 2008 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 #include "mathmlexpressionwriter.h" 0020 #include "value.h" 0021 #include "vector.h" 0022 #include "operator.h" 0023 #include "container.h" 0024 #include <QStringList> 0025 #include "list.h" 0026 #include "variable.h" 0027 #include "apply.h" 0028 #include "explexer.h" 0029 #include "analitzautils.h" 0030 #include "matrix.h" 0031 0032 using namespace Analitza; 0033 0034 MathMLExpressionWriter::MathMLExpressionWriter(const Object* o) 0035 { 0036 m_result=o->accept(this); 0037 } 0038 0039 QVariant MathMLExpressionWriter::visit(const Ci* var) 0040 { 0041 QString attrib; 0042 if(var->isFunction()) 0043 attrib=QStringLiteral(" type='function'"); 0044 return QVariant::fromValue<QString>(QStringLiteral("<ci")+attrib+'>'+var->name()+QStringLiteral("</ci>")); 0045 } 0046 0047 QVariant MathMLExpressionWriter::visit(const Operator* op) 0048 { 0049 if(op->operatorType()==Operator::function) 0050 return QString(); 0051 else 0052 return QStringLiteral("<%1 />").arg(op->name()); 0053 } 0054 0055 QVariant MathMLExpressionWriter::visit(const Cn* val) 0056 { 0057 if(val->isBoolean()) { 0058 if(val->isTrue()) 0059 return "<cn type='constant'>true</cn>"; 0060 else 0061 return "<cn type='constant'>false</cn>"; 0062 } else { 0063 QString type; 0064 if(val->format()==Cn::Real) 0065 type = QStringLiteral(" type='real'"); 0066 0067 return QStringLiteral("<cn%1>%2</cn>").arg(type).arg(val->value(), 0, 'g', 12); 0068 } 0069 } 0070 0071 QVariant MathMLExpressionWriter::visit(const Vector* vec) 0072 { 0073 QStringList elements; 0074 for(Vector::const_iterator it=vec->constBegin(); it!=vec->constEnd(); ++it) 0075 { 0076 elements += (*it)->accept(this).toString(); 0077 } 0078 return QStringLiteral("<vector>%1</vector>").arg(elements.join(QString())); 0079 } 0080 0081 QVariant MathMLExpressionWriter::visit(const Matrix* m) 0082 { 0083 QStringList elements; 0084 for(Matrix::const_iterator it=m->constBegin(); it!=m->constEnd(); ++it) 0085 { 0086 elements += (*it)->accept(this).toString(); 0087 } 0088 return QStringLiteral("<matrix>%1</matrix>").arg(elements.join(QString())); 0089 } 0090 0091 QVariant MathMLExpressionWriter::visit(const MatrixRow* mr) 0092 { 0093 QStringList elements; 0094 for(MatrixRow::const_iterator it=mr->constBegin(); it!=mr->constEnd(); ++it) 0095 { 0096 elements += (*it)->accept(this).toString(); 0097 } 0098 return QStringLiteral("<matrixrow>%1</matrixrow>").arg(elements.join(QString())); 0099 } 0100 0101 QVariant MathMLExpressionWriter::visit(const List* vec) 0102 { 0103 QStringList elements; 0104 if(vec->size()==0) 0105 return "<list />"; 0106 else if(vec->at(0)->type()==Object::value && static_cast<const Cn*>(vec->at(0))->format()==Cn::Char) { 0107 QString ret=AnalitzaUtils::listToString(vec); 0108 ret = ExpLexer::escape(ret); 0109 ret = "<cs>"+ret+"</cs>"; 0110 return ret; 0111 } else { 0112 for(List::const_iterator it=vec->constBegin(); it!=vec->constEnd(); ++it) 0113 elements += (*it)->accept(this).toString(); 0114 0115 return QStringLiteral("<list>%1</list>").arg(elements.join(QString())); 0116 } 0117 } 0118 0119 QVariant MathMLExpressionWriter::visit(const Container* c) 0120 { 0121 QString ret; 0122 foreach(const Object* o, c->m_params) 0123 ret += o->accept(this).toString(); 0124 0125 return QStringLiteral("<%1>%2</%1>").arg(c->tagName(), ret); 0126 } 0127 0128 QVariant MathMLExpressionWriter::visit(const Apply* a) 0129 { 0130 QString ret; 0131 0132 ret += a->firstOperator().accept(this).toString(); 0133 foreach(const Ci* bvar, a->bvarCi()) 0134 ret += "<bvar>"+bvar->accept(this).toString()+"</bvar>"; 0135 if(a->ulimit()) ret += "<uplimit>"+a->ulimit()->accept(this).toString()+"</uplimit>"; 0136 if(a->dlimit()) ret += "<downlimit>"+a->dlimit()->accept(this).toString()+"</downlimit>"; 0137 if(a->domain()) ret += "<domainofapplication>"+a->domain()->accept(this).toString()+"</domainofapplication>"; 0138 0139 foreach(const Object* o, a->m_params) 0140 ret += o->accept(this).toString(); 0141 0142 return QStringLiteral("<apply>%1</apply>").arg(ret); 0143 } 0144 0145 QVariant MathMLExpressionWriter::visit(const Analitza::CustomObject*) 0146 { 0147 return "<!-- custom object -->"; 0148 } 0149 0150 QVariant MathMLExpressionWriter::visit(const None* ) 0151 { 0152 return QString(); 0153 } 0154