Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

omgalgorithm.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Tim Sutton   *
00003  *   tim@linfiniti.com   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include "omgalgorithm.h"
00021 #include "omgui.h"
00022 #include <QVectorIterator>
00023 #include <QDomDocument>
00024 #include <QDomElement>
00025 OmgAlgorithm::OmgAlgorithm() : OmgSerialisable(), OmgGuid(),
00026                                mId(QString ("[not set]")),
00027                                mName(QString ("[not set]")),
00028                                mAuthor(QString( "[not set]")),
00029                                mCodeAuthor(QString ("[not set]")),
00030                                mContact(QString ("[not set]")),
00031                                mVersion(QString ("[not set]")),
00032                                mCategorical(0),
00033                                mAbsence(0),
00034                                mOverview(QString ("[not set]")),
00035                                mDescription(QString ("[not set]")),
00036                                mBibliography(QString ("[not set]")),
00037                                mOrigin(UNDEFINED)
00038 
00039 {
00040   mParameters.clear();
00041 }
00042 OmgAlgorithm::~OmgAlgorithm()
00043 {}
00044 
00045 //
00046 // Mutators
00047 //
00048 
00049 void OmgAlgorithm::setId(QString theId)
00050 {
00051   mId=theId;
00052 }
00053 void OmgAlgorithm::setName(QString theName)
00054 {
00055   mName=theName;
00056 }
00057 void OmgAlgorithm::setAuthor(QString theAuthor)
00058 {
00059   mAuthor=theAuthor;
00060 }
00061 void OmgAlgorithm::setCodeAuthor(QString theCodeAuthor)
00062 {
00063   mCodeAuthor=theCodeAuthor;
00064 }
00065 void OmgAlgorithm::setContact(QString theContact)
00066 {
00067   mContact=theContact;
00068 }
00069 void OmgAlgorithm::setVersion(QString theVersion)
00070 {
00071   mVersion=theVersion;
00072 }
00073 void OmgAlgorithm::setCategorical(int theCategorical)
00074 {
00075   mCategorical=theCategorical;
00076 }
00077 void OmgAlgorithm::setAbsence(int theAbsence)
00078 {
00079   mAbsence=theAbsence;
00080 }
00081 void OmgAlgorithm::setOverview(QString theOverview)
00082 {
00083   mOverview=theOverview;
00084 
00085 }
00086 void OmgAlgorithm::setDescription(QString theDescription)
00087 {
00088   mDescription=theDescription;
00089 }
00090 void OmgAlgorithm::setBibliography(QString theBibliography)
00091 {
00092   mBibliography=theBibliography;
00093 }
00094 void OmgAlgorithm::setParameters(QVector<OmgAlgorithmParameter> theParameters)
00095 {
00096   mParameters=theParameters;
00097 }
00098 void OmgAlgorithm::setOrigin(Origin theOrigin)
00099 {
00100   mOrigin=theOrigin;
00101 }
00102 //
00103 // Accessors
00104 //
00105 QString OmgAlgorithm::id()
00106 {
00107   return mId;
00108 }
00109 QString OmgAlgorithm::name() const
00110 {
00111   return mName;
00112 }
00113 QString OmgAlgorithm::author()
00114 {
00115   return mAuthor;
00116 }
00117 QString OmgAlgorithm::codeAuthor()
00118 {
00119   return mCodeAuthor;
00120 }
00121 QString OmgAlgorithm::contact()
00122 {
00123   return mContact;
00124 }
00125 QString OmgAlgorithm::version()
00126 {
00127   return mVersion;
00128 }
00129 int OmgAlgorithm::categorical()
00130 {
00131   return mCategorical;
00132 }
00133 int OmgAlgorithm::absence()
00134 {
00135   return mAbsence;
00136 }
00137 QString OmgAlgorithm::overview()
00138 {
00139   return mOverview;
00140 }
00141 QString OmgAlgorithm::description()
00142 {
00143   return mDescription;
00144 }
00145 QString OmgAlgorithm::bibliography()
00146 {
00147   return mBibliography;
00148 }
00149 QVector<OmgAlgorithmParameter> OmgAlgorithm::parameters()
00150 {
00151   return  mParameters;
00152 }
00153 OmgAlgorithmParameter OmgAlgorithm::parameter(QString theId)
00154 {
00155     //here we use the new Qt4 java style iterator!
00156     QVectorIterator<OmgAlgorithmParameter> myIterator(mParameters);
00157     OmgAlgorithmParameter myParameter;
00158     myParameter.setId("invalid");
00159     while (myIterator.hasNext())
00160     {
00161       myParameter=myIterator.next();
00162       if (myParameter.id()==theId)
00163       {
00164         return myParameter;
00165       }
00166     }
00168     return myParameter;
00169 }
00170 
00171 OmgAlgorithm::Origin OmgAlgorithm::origin()
00172 {
00173   return mOrigin;
00174 }
00175 //
00176 // end of accessors
00177 //
00178 
00179 void OmgAlgorithm::addParameter(OmgAlgorithmParameter theParameter)
00180 {
00181   mParameters.push_back(theParameter);
00182 }
00183 
00184 int OmgAlgorithm::parameterCount()
00185 {
00186   return mParameters.count();
00187 
00188 }
00189 QString OmgAlgorithm::toXml() const
00190 {
00191   QString myString("  <Algorithm  Id=\"" + mId +  "\" Version=\"" + mVersion + "\">\n");
00192   //alg metadata is not required when submitting models to om
00193   //but is used in other parts of the gui
00194   myString+=QString("  <Name>" + Omgui::xmlEncode(mName) + "</Name>\n " );
00195   myString+=QString("  <Overview>"+Omgui::xmlEncode(mOverview)+"</Overview>\n");
00196   myString+=QString("  <Description>"+Omgui::xmlEncode(mDescription)+"</Description>\n");
00197   //@todo make designers a collection in this class instead of just a string member
00198   myString+=QString("  <Designers>\n" );
00199   //@TODO Adda  designer contact field to this class and then update contact below
00200   myString+=QString("    <Designer Name=\"" + Omgui::xmlEncode(mAuthor) + "\" Contact=\"\"/>\n" );
00201   myString+=QString("  </Designers>\n" );
00202   myString+=QString("  <Bibliography>"+Omgui::xmlEncode(mBibliography)+"</Bibliography>\n");
00203   //@todo make developers a collection in this class instead of just a string member
00204   myString+=QString("  <Developers>\n");
00205   myString+=QString("    <Developer Name=\"" + Omgui::xmlEncode(mCodeAuthor) + "\" Contact=\"" + mContact + "\"/>\n" );
00206   myString+=QString("  </Developers>\n" );
00207   myString+=QString("  <Guid>" + guid() + "</Guid>\n" );
00208   myString+=QString("  <AcceptsCategoricalMaps>" + QString::number(mCategorical) + "</AcceptsCategoricalMaps>\n" );
00209   myString+=QString("  <RequiresAbsencePoints>" + QString::number(mAbsence) + "</RequiresAbsencePoints>\n" );
00210   myString+=QString("  <Parameters>\n");
00211   //here we use the new Qt4 java style iterator!
00212   QVectorIterator<OmgAlgorithmParameter> myIterator(mParameters);
00213   while (myIterator.hasNext())
00214   {
00215     OmgAlgorithmParameter myParameter=myIterator.next();
00216     myString += myParameter.toXml();
00217   }
00218   myString +=QString( "    </Parameters>\n");
00219   //TODO work out how to serialise and deserialise the model itself e.g.
00220   //+"      <BioclimModel Mean="271.1538461538461319833004 271.1538461538461319833004 271.1538461538461319833004" StdDev="12.19477741305047935327366 12.19477741305047935327366 12.19477741305047935327366" Minimum="6 6 6" Maximum="1141 1141 1141"/>\n"
00221   myString+=QString("  </Algorithm>\n");
00222   return myString;
00223 }
00224 QString OmgAlgorithm::toModelCreationXml() const
00225 {
00226   QString myString("  <Algorithm  Id=\"" + mId +  "\" Version=\"" + mVersion + "\">\n");
00227   myString +=QString( "    <Parameters>\n");
00228   QVectorIterator<OmgAlgorithmParameter> myIterator(mParameters);
00229   while (myIterator.hasNext())
00230   {
00231     OmgAlgorithmParameter myParameter=myIterator.next();
00232     myString += myParameter.toModelCreationXml();
00233   }
00234   myString +=QString( "    </Parameters>\n");
00235   myString+=QString("  </Algorithm>");
00236   return myString;
00237 }
00238 
00239 QString OmgAlgorithm::toModelProjectionXml(QString theModel, QString theNormalization) const
00240 {
00241   QString myString("  <Algorithm  Id=\"" + mId +  "\" Version=\"" + mVersion + "\">\n");
00242   myString +=QString( "    <Parameters>\n");
00243   QVectorIterator<OmgAlgorithmParameter> myIterator(mParameters);
00244   while (myIterator.hasNext())
00245   {
00246     OmgAlgorithmParameter myParameter=myIterator.next();
00247     myString += myParameter.toModelCreationXml();
00248   }
00249   myString +=QString( "    </Parameters>\n");
00250   //include teh model tag as returned by libopenModelller
00251   //algorithms when they serialise
00252   myString += theModel;
00253   if (!theNormalization.isEmpty())
00254   {
00255     myString += theNormalization;
00256   }
00257   myString+=QString("  </Algorithm>");
00258   return myString;
00259 }
00260 
00261 bool OmgAlgorithm::fromXml(const QString theXml) 
00262 {
00263   QDomDocument myDocument("mydocument");
00264   myDocument.setContent(theXml);
00265   QDomElement myTopElement = myDocument.firstChildElement("Algorithm");
00266   if (myTopElement.isNull())
00267   {
00268     //TODO - just make this a warning
00269     qDebug("top element could not be found!");
00270   }
00271   mId=myTopElement.attribute("Id");
00272   mVersion=myTopElement.attribute("Version");
00273   mName=Omgui::xmlDecode(myTopElement.firstChildElement("Name").text());
00274   mOverview=myTopElement.firstChildElement("Overview").text();
00275   mDescription=myTopElement.firstChildElement("Description").text();
00276   mBibliography=myTopElement.firstChildElement("Bibliography").text();
00277   mCategorical=myTopElement.firstChildElement("AcceptsCategoricalMaps").text().toInt();
00278   mAbsence=myTopElement.firstChildElement("RequiresAbsencePoints").text().toInt();
00279   //iterate through the nested designer details
00280   //@TODO update the class member to be a collection rather than a single string
00281   QDomElement myDesignersElement = myTopElement.firstChildElement("Designers");
00282   QDomElement myDesignerElement= myDesignersElement.firstChildElement("Designer");
00283   mAuthor="";
00284   //@TODO add author contact class member
00285   while(!myDesignerElement.isNull()) 
00286   {
00287     if (myDesignerElement.tagName()!="Designer")
00288     {
00289       myDesignerElement = myDesignerElement.nextSiblingElement();
00290       continue;
00291     }
00292     mAuthor+=myDesignerElement.attribute("Name");
00293     mAuthor+=" (" + myDesignerElement.attribute("Contact") + ")";
00294     myDesignerElement = myDesignerElement.nextSiblingElement();
00295   }
00296   //iterate through the nested developer details
00297   //@TODO update the class member to be a collection rather than a single string
00298   QDomElement myDevelopersElement = myTopElement.firstChildElement("Developers");
00299   QDomElement myDeveloperElement= myDevelopersElement.firstChildElement("Developer");
00300   mCodeAuthor="";
00301   mContact="";
00302   while(!myDeveloperElement.isNull()) 
00303   {
00304     if (myDeveloperElement.tagName()!="Developer")
00305     {
00306       myDeveloperElement = myDeveloperElement.nextSiblingElement();
00307       continue;
00308     }
00309     mCodeAuthor+=myDeveloperElement.attribute("Name");
00310     mContact+=myDeveloperElement.attribute("Contact");
00311     myDeveloperElement = myDeveloperElement.nextSiblingElement();
00312   }
00313   QString myGuid = myTopElement.firstChildElement("Guid").text();
00314   //assign a Guid if none was provided
00315   if (myGuid.isEmpty())
00316   {
00317     setGuid();
00318     //also assume origin to be from plugin if none was provided
00319     setOrigin(ADAPTERPROFILE);
00320   }
00321   else
00322   {
00323     setGuid(myGuid);
00324   }
00325   //now get the algs'parameters from the nested tags
00326   QDomElement myParametersElement = myTopElement.firstChildElement("Parameters");
00327   QDomElement myElement = myParametersElement.firstChildElement();
00328   while(!myElement.isNull()) 
00329   {
00330     if (myElement.tagName()!="Parameter")
00331     {
00332       myElement = myElement.nextSiblingElement();
00333       continue;
00334     }
00335     OmgAlgorithmParameter myParameter;
00336     //get a textual xml representation of the param tag
00337     QDomDocument myParamDoc("parameter");
00338     //note we need to do a deep copy here because the
00339     //element is shared otherwise and when we
00340     //reparent it the loop stops after the first node
00341     //as no more siblings are found!
00342     QDomElement myCopy = myElement.cloneNode().toElement();
00343     myParamDoc.appendChild(myCopy);
00344     QString myXml = myParamDoc.toString();
00345     //now hand over the xml snippet to the alg parameter class to be deserialised
00346     myParameter.fromXml(myXml);
00347     addParameter(myParameter);
00348     myElement = myElement.nextSiblingElement();
00349   }
00350   return true;
00351 }
00352 
00353 QString OmgAlgorithm::toString() const
00354 {
00355   QString myString("Id : " + mId+"\n"); 
00356   myString+=QString("Name : "+mName+"\n"); 
00357   myString+=QString("Version : "+mVersion+"\n");
00358   myString+=QString("Author :"+mAuthor+"\n"); 
00359   myString+=QString("CodeAuthor : " + mCodeAuthor+"\n"); 
00360   myString+=QString("Contact :"+mContact+"\n");
00361   myString+=QString("Globally Unique Identifier:"+guid()+"\n");
00362   myString+=QString("Categorical data supported: "+ QString::number(mCategorical)+"\n --- \n");
00363   myString+=QString("Absence data supported : "+ QString::number(mAbsence)+"\n --- \n");
00364   myString+=QString("Overview : "+ mOverview +"\n --- \n");
00365   myString+=QString("Description : "+ mDescription +"\n --- \n");
00366 
00367   //here we use the new Qt4 java style iterator!
00368   QVectorIterator<OmgAlgorithmParameter> myIterator(mParameters);
00369   while (myIterator.hasNext())
00370   {
00371     OmgAlgorithmParameter myParameter=myIterator.next();
00372     myString +=myParameter.id() + " : " + myParameter.value() + "\n";
00373   }
00374   return myString;
00375 }
00376 QString OmgAlgorithm::toHtml() const
00377 {
00378   QString myString("<table width=\"100%\" border=\"0\">\n"); 
00379   myString+=QString("<tr><th colspan=\"2\" class=\"glossy\"><h3>" + QObject::tr("Algorithm Name: ") + mName + "</h3></th></tr>\n");
00380   myString+=QString("<tr><th width=\"25%\"> " + QObject::tr("Id :") + "</th><td width=\"75%\"> "+mId+"</td></tr>\n");
00381   myString+=QString("<tr><th width=\"25%\"> " + QObject::tr("Version :") + "</th><td> "+mVersion+"</td></tr>\n");
00382   myString+=QString("<tr><th width=\"25%\"> " + QObject::tr("Author :") + "</th><td>"+mAuthor+"</td></tr>\n");
00383   myString+=QString("<tr><th width=\"25%\"> " + QObject::tr("CodeAuthor :") + "</th><td> " + mCodeAuthor+"</td></tr>\n");
00384   myString+=QString("<tr><th width=\"25%\"> " + QObject::tr("Contact :") + "</th><td>"+mContact+"</td></tr>\n");
00385   myString+=QString("<tr><th width=\"25%\"> " + QObject::tr("Guid :") + "</th><td>"+guid()+"</td></tr>\n");
00386   myString+=QString("<tr><th width=\"25%\"> " + QObject::tr("Categorical data supported:") + "</th><td> "+ QString::number(mCategorical)+"</td></tr>\n");
00387   myString+=QString("<tr><th width=\"25%\"> " + QObject::tr("Absence data supported :") + "</th><td> "+ QString::number(mAbsence)+"</td></tr>\n");
00388   myString+=QString("<tr><th width=\"25%\"> " + QObject::tr("Overview :") + "</th><td> "+ mOverview +"</td></tr>\n");
00389   myString+=QString("<tr><th width=\"25%\"> " + QObject::tr("Description :") + "</th><td> "+ mDescription +"</td></tr>\n");
00390   //make a mini table for the parameters
00391   myString+=QString("<tr><td colspan=\"2\">\n");
00392   myString+=QString("<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\">\n"); 
00393   myString+=QString("<tr><th class=\"glossy\"> " + QObject::tr("Id") + "</th>\n");
00394   myString+=QString("<th class=\"glossy\"> " + QObject::tr("Name") + "</th>\n");
00395   myString+=QString("<th class=\"glossy\"> " + QObject::tr("Value") + "</th>\n");
00396   myString+=QString("<th class=\"glossy\"> " + QObject::tr("Default") + "</th>\n");
00397   myString+=QString("<th class=\"glossy\"> " + QObject::tr("Min") + "</th>\n");
00398   myString+=QString("<th class=\"glossy\"> " + QObject::tr("Max") + "</th>\n");
00399   myString+=QString("<th class=\"glossy\"> " + QObject::tr("Type") + "</th></tr>\n");
00400 
00401   //here we use the new Qt4 java style iterator!
00402   QVectorIterator<OmgAlgorithmParameter> myIterator(mParameters);
00403   while (myIterator.hasNext())
00404   {
00405     OmgAlgorithmParameter myParameter=myIterator.next();
00406     myString+=QString("<tr><td class=\"parameterHeader\"><b>" + myParameter.id() + "</b></td>\n");
00407     myString+=QString("<td> "+ myParameter.name() +"</td>\n");
00408     myString+=QString("<td class=\"parameterHeader\"> "+ myParameter.value() +"</td>\n");
00409     myString+=QString("<td> "+ myParameter.defaultValue() +"</td>\n");
00410     myString+=QString("<td class=\"parameterHeader\"> "+ myParameter.minimum() +"</td>\n");
00411     myString+=QString("<td> "+ myParameter.maximum() +"</td>\n");
00412     myString+=QString("<td class=\"parameterHeader\"> "+ myParameter.type() +"</td></tr>\n");
00413     myString+=QString("<tr>\n");
00414     myString+=QString("<td class=\"parameterHeader\">" + QObject::tr("Description: ") + "</td><td colspan=\"6\"><b> " + "</b> "+ myParameter.description() +"</td>\n");
00415     myString+=QString("</tr>\n");
00416   }
00417   //end of alg parameters mini table
00418   myString +=QString("</td></tr></table>\n"); 
00419   //end of alg table
00420   myString +=QString("</table>\n"); 
00421   return myString;
00422 }

Generated on Mon Apr 28 15:07:58 2008 for openModellerDesktop by  doxygen 1.4.1-20050210