00001
00012
00013
00014 #include "config_handler.h"
00015 #include "../algorithms/algorithm_definition.h"
00016 #include "../algorithms/dataset_definition.h"
00017 #include <iostream>
00018 #include <boost/lexical_cast.hpp>
00019
00020 namespace imedgine
00021 {
00022
00023
00024 ConfigHandler::ConfigHandler(AlgorithmConfig::algorithm_config_pointer_type algorithm_config)
00025 :algorithm_config_(algorithm_config),
00026 is_algorithm_(false), is_combinations_(false), is_combination_(false), is_input_(false), is_output_(false), is_dataset_(false),
00027 is_name_(false), is_classification_(false), is_library_(false), is_type_(false)
00028 {
00029 }
00030
00031
00032
00033 ConfigHandler::~ConfigHandler()
00034 {
00035 }
00036
00037
00038
00039
00040 bool ConfigHandler::startElement(const QString &namespace_uri, const QString &local_name, const QString &qname, const QXmlAttributes &attrs)
00041 {
00042 if (local_name == "algorithms")
00043 {
00044
00045 }
00046 else if (local_name == "algorithm")
00047 {
00048 current_definition_ = AlgorithmConfig::algorithm_definition_pointer_type(new AlgorithmDefinition);
00049 is_algorithm_ = true;
00050 }
00051 else if (local_name == "combinations")
00052 {
00053 is_combinations_ = true;
00054 }
00055 else if (local_name == "combination")
00056 {
00057 is_combination_ = true;
00058 }
00059 else if (local_name == "input")
00060 {
00061 is_input_ = true;
00062 }
00063 else if (local_name == "output")
00064 {
00065 is_output_ = true;
00066 }
00067
00068 else if (local_name == "dataset" && is_input_)
00069 {
00070 current_dataset_ = AlgorithmDefinition::dataset_definition_pointer_type(new DatasetDefinition);
00071 if (attrs.count() == 0)
00072 {
00073 return false;
00074 }
00075 else
00076 {
00077 current_dataset_->setName(attrs.value("name").toStdString());
00078 bool in_place = true;
00079 if (attrs.value("inplace") == "false")
00080 in_place = false;
00081 current_dataset_->setInPlace(in_place);
00082 }
00083
00084 is_dataset_ = true;
00085 }
00086
00087 else if (local_name == "dataset" && is_output_)
00088 {
00089 current_dataset_ = AlgorithmDefinition::dataset_definition_pointer_type(new DatasetDefinition);
00090 if (attrs.count() == 0)
00091 {
00092 return false;
00093 }
00094 else
00095 {
00096 current_dataset_->setName(attrs.value("name").toStdString());
00097 std::string type = attrs.value("type").toStdString();
00098 std::string bytes = attrs.value("bytes").toStdString();
00099
00100 dataset_type ds_type = getDatasetType(type, bytes);
00101 current_dataset_->setType(ds_type);
00102 }
00103 }
00104
00105 else if (local_name == "dataset" && is_combination_)
00106 {
00107 if (attrs.count() == 0)
00108 {
00109 return false;
00110 }
00111 current_dataset_ = current_definition_->getInputDataset(attrs.value("name").toStdString());
00112 is_dataset_ = true;
00113 }
00114 else if (local_name == "name")
00115 {
00116 is_name_ = true;
00117 }
00118 else if (local_name == "classification")
00119 {
00120 is_classification_ = true;
00121 }
00122 else if (local_name == "library")
00123 {
00124 is_library_ = true;
00125 }
00126 else if (local_name == "type")
00127 {
00128 if (attrs.count() == 0)
00129 {
00130 return false;
00131 }
00132 else
00133 {
00134 std::string type = attrs.value("type").toStdString();
00135 std::string bytes = attrs.value("bytes").toStdString();
00136 current_type_ = getDatasetType(type, bytes);
00137 }
00138 is_type_ = true;
00139 }
00140 return true;
00141 }
00142
00143
00144
00145 bool ConfigHandler::endElement(const QString &namespace_uri, const QString &local_name, const QString &qname)
00146 {
00147 if (local_name == "algorithm")
00148 {
00149 algorithm_config_->addAlgorithmDefinition(current_definition_);
00150 is_algorithm_ = false;
00151 }
00152 else if (local_name == "dataset" && is_combination_)
00153 {
00154 current_combination_.insert(std::make_pair(current_dataset_->getName(), accepted_datasets_));
00155 accepted_datasets_.clear();
00156 is_dataset_ = false;
00157 }
00158 else if (local_name == "combination")
00159 {
00160 combinations_.push_back(current_combination_);
00161 current_combination_.clear();
00162 is_combination_ = false;
00163 }
00164 else if (local_name == "combinations")
00165 {
00166 current_definition_->setDatasetCombinations(combinations_);
00167 combinations_.clear();
00168 is_combinations_ = false;
00169 }
00170 else if (local_name == "input")
00171 {
00172 is_input_ = false;
00173 }
00174 else if (local_name == "output")
00175 {
00176 is_output_ = false;
00177 }
00178 else if (local_name == "dataset")
00179 {
00180 if (is_input_)
00181 {
00182 current_definition_->addInputDataset(current_dataset_);
00183 }
00184 else if (is_output_)
00185 {
00186 current_definition_->addOutputDataset(current_dataset_);
00187 }
00188 is_dataset_ = false;
00189 }
00190 else if (local_name == "name")
00191 {
00192 is_name_ = false;
00193 }
00194 else if (local_name == "classification")
00195 {
00196 is_classification_ = false;
00197 }
00198 else if (local_name == "library")
00199 {
00200 is_library_ = false;
00201 }
00202 else if (local_name == "type")
00203 {
00204 accepted_datasets_.insert(current_type_);
00205 is_type_ = false;
00206 }
00207 return true;
00208 }
00209
00210 bool ConfigHandler::characters (const QString &characters)
00211 {
00212 if (is_algorithm_ && is_name_)
00213 {
00214 current_definition_->setName(characters.toStdString());
00215 }
00216 else if (is_classification_)
00217 {
00218 current_definition_->setClassification(characters.toStdString());
00219 }
00220 else if (is_library_)
00221 {
00222 current_definition_->setLibraryName(characters.toStdString());
00223 }
00224
00225 return true;
00226 }
00227
00228
00229
00230 dataset_type ConfigHandler::getDatasetType(std::string type, std::string bytes)
00231 {
00232 dataset_type ds = ONE_BYTE_VOLUME_PIXEL_DATASET;
00233 if((type=="volume")&&(bytes=="1"))
00234 {
00235 ds = ONE_BYTE_VOLUME_PIXEL_DATASET;
00236 }
00237 else if((type=="volume")&&(bytes=="2"))
00238 {
00239 ds = TWO_BYTE_VOLUME_PIXEL_DATASET;
00240 }
00241 else if((type=="volume")&&(bytes=="4"))
00242 {
00243 ds = FOUR_BYTE_VOLUME_PIXEL_DATASET;
00244 }
00245 else if((type=="image")&&(bytes=="1"))
00246 {
00247 ds = ONE_BYTE_IMAGE_PIXEL_DATASET;
00248 }
00249 else if((type=="image")&&(bytes=="2"))
00250 {
00251 ds = TWO_BYTE_IMAGE_PIXEL_DATASET;
00252 }
00253 else if((type=="image")&&(bytes=="4"))
00254 {
00255 ds = TWO_BYTE_IMAGE_PIXEL_DATASET;
00256 }
00257 else if(type=="matrix")
00258 {
00259 ds = MATRIX_DATASET;
00260 }
00261 return ds;
00262 }
00263
00264 }
00265