Commit 12743023 authored by Rashad Kanavath's avatar Rashad Kanavath

inxml and outxml checked for more apps and param types

parent f82c2ec9
......@@ -8,4 +8,3 @@ syntax: glob
tags
*vim*
*.template
......@@ -174,29 +174,29 @@ int Application::Execute()
//read application from xml
std::string inXMLKey = "inxml";
if (GetParameterType(inXMLKey) == ParameterType_InputProcessXML
&& IsParameterEnabled(inXMLKey) )
{
&& IsParameterEnabled(inXMLKey) )
{
Parameter* param = GetParameterByKey(inXMLKey);
InputProcessXMLParameter* inXMLParam = dynamic_cast<InputProcessXMLParameter*>(param);
int ret = inXMLParam->Read(this);
if( ret != 0 )
{
return -1;
return -1;
}
this->UpdateParameters();
}
}
// before execute we set the seed of mersenne twister
std::vector<std::string> paramList = GetParametersKeys(true);
bool UseSpecificSeed = false;
for (std::vector<std::string>::const_iterator it = paramList.begin(); it != paramList.end(); ++it)
{
std::string key = *it;
if ((key.compare(0, 4, "rand") == 0) && HasValue("rand"))
{
UseSpecificSeed = true;
std::string key = *it;
if ((key.compare(0, 4, "rand") == 0) && HasValue("rand"))
{
UseSpecificSeed = true;
Parameter* param = GetParameterByKey(key);
IntParameter* randParam = dynamic_cast<IntParameter*> (param);
int seed = randParam->GetValue();
......@@ -205,99 +205,98 @@ int Application::Execute()
}
if (!UseSpecificSeed)
{
itk::Statistics::MersenneTwisterRandomVariateGenerator::GetInstance()->Initialize();
itk::Statistics::MersenneTwisterRandomVariateGenerator::GetInstance()->Initialize();
}
this->DoExecute();
return ret;
}
int Application::ExecuteAndWriteOutput()
{
int status = this->Execute();
if (status == 0)
{
std::vector<std::string> paramList = GetParametersKeys(true);
// First Get the value of the available memory to use with the
// writer if a RAMParameter is set
bool useRAM = false;
unsigned int ram = 0;
for (std::vector<std::string>::const_iterator it = paramList.begin();
it != paramList.end();
++it)
std::vector<std::string> paramList = GetParametersKeys(true);
// First Get the value of the available memory to use with the
// writer if a RAMParameter is set
bool useRAM = false;
unsigned int ram = 0;
for (std::vector<std::string>::const_iterator it = paramList.begin();
it != paramList.end();
++it)
{
std::string key = *it;
if (GetParameterType(key) == ParameterType_RAM
&& IsParameterEnabled(key))
{
Parameter* param = GetParameterByKey(key);
RAMParameter* ramParam = dynamic_cast<RAMParameter*>(param);
ram = ramParam->GetValue();
useRAM = true;
}
std::string key = *it;
if (GetParameterType(key) == ParameterType_RAM
&& IsParameterEnabled(key))
{
Parameter* param = GetParameterByKey(key);
RAMParameter* ramParam = dynamic_cast<RAMParameter*>(param);
ram = ramParam->GetValue();
useRAM = true;
}
}
for (std::vector<std::string>::const_iterator it = paramList.begin();
it != paramList.end();
++it)
{
std::string key = *it;
if (GetParameterType(key) == ParameterType_OutputImage
&& IsParameterEnabled(key) && HasValue(key) )
{
Parameter* param = GetParameterByKey(key);
OutputImageParameter* outputParam = dynamic_cast<OutputImageParameter*>(param);
outputParam->InitializeWriters();
if (useRAM)
{
outputParam->SetRAMValue(ram);
}
std::ostringstream progressId;
progressId << "Writing " << outputParam->GetFileName() << "...";
AddProcess(outputParam->GetWriter(), progressId.str());
outputParam->Write();
}
else if (GetParameterType(key) == ParameterType_OutputVectorData
&& IsParameterEnabled(key) && HasValue(key) )
{
Parameter* param = GetParameterByKey(key);
OutputVectorDataParameter* outputParam = dynamic_cast<OutputVectorDataParameter*>(param);
outputParam->InitializeWriters();
std::ostringstream progressId;
progressId << "Writing " << outputParam->GetFileName() << "...";
AddProcess(outputParam->GetWriter(), progressId.str());
outputParam->Write();
}
else if (GetParameterType(key) == ParameterType_ComplexOutputImage
&& IsParameterEnabled(key) && HasValue(key) )
{
Parameter* param = GetParameterByKey(key);
ComplexOutputImageParameter* outputParam = dynamic_cast<ComplexOutputImageParameter*>(param);
outputParam->InitializeWriters();
if (useRAM)
{
outputParam->SetRAMValue(ram);
}
std::ostringstream progressId;
progressId << "Writing " << outputParam->GetFileName() << "...";
AddProcess(outputParam->GetWriter(), progressId.str());
outputParam->Write();
}
//xml writer parameter
else if (GetParameterType(key) == ParameterType_OutputProcessXML
&& IsParameterEnabled(key) && HasValue(key) )
{
Parameter* param = GetParameterByKey(key);
OutputProcessXMLParameter* outXMLParam = dynamic_cast<OutputProcessXMLParameter*>(param);
outXMLParam->Write(this);
}
}
for (std::vector<std::string>::const_iterator it = paramList.begin();
it != paramList.end();
++it)
{
std::string key = *it;
if (GetParameterType(key) == ParameterType_OutputImage
&& IsParameterEnabled(key) && HasValue(key) )
{
Parameter* param = GetParameterByKey(key);
OutputImageParameter* outputParam = dynamic_cast<OutputImageParameter*>(param);
outputParam->InitializeWriters();
if (useRAM)
{
outputParam->SetRAMValue(ram);
}
std::ostringstream progressId;
progressId << "Writing " << outputParam->GetFileName() << "...";
AddProcess(outputParam->GetWriter(), progressId.str());
outputParam->Write();
}
else if (GetParameterType(key) == ParameterType_OutputVectorData
&& IsParameterEnabled(key) && HasValue(key) )
{
Parameter* param = GetParameterByKey(key);
OutputVectorDataParameter* outputParam = dynamic_cast<OutputVectorDataParameter*>(param);
outputParam->InitializeWriters();
std::ostringstream progressId;
progressId << "Writing " << outputParam->GetFileName() << "...";
AddProcess(outputParam->GetWriter(), progressId.str());
outputParam->Write();
}
else if (GetParameterType(key) == ParameterType_ComplexOutputImage
&& IsParameterEnabled(key) && HasValue(key) )
{
Parameter* param = GetParameterByKey(key);
ComplexOutputImageParameter* outputParam = dynamic_cast<ComplexOutputImageParameter*>(param);
outputParam->InitializeWriters();
if (useRAM)
{
outputParam->SetRAMValue(ram);
}
std::ostringstream progressId;
progressId << "Writing " << outputParam->GetFileName() << "...";
AddProcess(outputParam->GetWriter(), progressId.str());
outputParam->Write();
}
//xml writer parameter
else if (GetParameterType(key) == ParameterType_OutputProcessXML
&& IsParameterEnabled(key) && HasValue(key) )
{
Parameter* param = GetParameterByKey(key);
OutputProcessXMLParameter* outXMLParam = dynamic_cast<OutputProcessXMLParameter*>(param);
outXMLParam->Write(this);
}
}
}
return status;
}
......@@ -388,6 +387,16 @@ Role Application::GetParameterRole(std::string paramKey) const
return GetParameterByKey(paramKey)->GetRole();
}
void Application::SetParameterEmpty(std::string paramKey, bool active)
{
GetParameterByKey(paramKey)->SetActive(active);
}
bool Application::GetParameterEmpty(std::string paramKey)
{
return GetParameterByKey(paramKey)->GetActive();
}
/* Return the role (input/output) of a parameter */
void Application::SetParameterRole(std::string paramKey, Role role)
{
......@@ -859,6 +868,11 @@ void Application::SetParameterOutputVectorData(std::string parameter, VectorData
}
}
bool Application::IsUseXMLValue(std::string parameter)
{
Parameter* param = GetParameterByKey(parameter);
return param->IsUseXMLValue();
}
std::string Application::GetParameterName(std::string parameter)
{
Parameter* param = GetParameterByKey(parameter);
......
......@@ -176,12 +176,20 @@ public:
* or a value set externally by user */
bool HasValue(std::string paramKey) const;
/* Activate or deactivate the bool parameter
*/
void SetParameterEmpty(std::string paramKey, bool active);
/* Get active flag of parameter with key paramKey
*/
bool GetParameterEmpty(std::string paramKey);
/* Return the user level of access to a parameter */
UserLevel GetParameterUserLevel(std::string paramKey) const;
/** Get the role of the parameter */
Role GetParameterRole(std::string paramKey) const;
/* Get the parameter type from its name */
ParameterType GetParameterType(std::string paramKey) const;
......@@ -499,6 +507,13 @@ public:
*/
ImagePixelType GetParameterOutputImagePixelType(std::string parameter);
/* GetXMLOverride
*
* wrapper function for Paramter::IsUseXMLValue() for application instance.
* Needs parameter key as argument.
*/
bool IsUseXMLValue(std::string parameter);
/* Get the pixel type in which the complex image will be saved
*
* Can be called for types :
......
......@@ -28,7 +28,6 @@ namespace otb
{
namespace Wrapper
{
InputProcessXMLParameter::InputProcessXMLParameter()
{
this->SetKey("inxml");
......@@ -48,44 +47,44 @@ std::string
InputProcessXMLParameter::pixelTypeToString(ImagePixelType pixType)
{
std::string type;
switch( pixType )
{
case ImagePixelType_uint8:
{
type = "uint8";
break;
}
{
type = "uint8";
break;
}
case ImagePixelType_int16:
{
type = "int16";
break;
}
{
type = "int16";
break;
}
case ImagePixelType_uint16:
{
type = "uint16";
break;
}
{
type = "uint16";
break;
}
case ImagePixelType_int32:
{
type = "int32";
break;
}
{
type = "int32";
break;
}
case ImagePixelType_uint32:
{
type = "uint32";
break;
}
{
type = "uint32";
break;
}
case ImagePixelType_float:
{
type = "float";
break;
}
{
type = "float";
break;
}
case ImagePixelType_double:
{
type = "double";
break;
}
{
type = "double";
break;
}
}
return type;
}
......@@ -98,12 +97,21 @@ const std::string InputProcessXMLParameter::GetChildNodeTextOf(TiXmlElement *par
{
TiXmlElement* childElement = 0;
childElement = parentElement->FirstChildElement(key.c_str());
//same as childElement->GetText() does but that call is failing if there is no such node.
//but the below code works and is a replacement for GetText()
if(childElement)
{
value = childElement->GetText();
const TiXmlNode* child = childElement->FirstChild();
if ( child )
{
const TiXmlText* childText = child->ToText();
if ( childText )
{
value = childText->Value();
}
}
}
// value = parent->FirstChildElement(key)->GetText();
}
return value;
}
......@@ -187,10 +195,10 @@ InputProcessXMLParameter::Read(Application::Pointer app)
{
//hopefully shouldn't reach here ...
itkExceptionMacro( << "Input XML was generated for a different application( " <<
app_Name << ") while application loaded is:" << app->GetName());
app_Name << ") while application loaded is:" << app->GetName());
return -1;
}
// Iterate through the tree to get all the stats
for( TiXmlElement* n_Parameter = n_App->FirstChildElement("parameter"); n_Parameter != NULL;
n_Parameter = n_Parameter->NextSiblingElement() )
......@@ -204,63 +212,114 @@ InputProcessXMLParameter::Read(Application::Pointer app)
TiXmlElement* n_Values = NULL;
n_Values = n_Parameter->FirstChildElement("values");
if(n_Values)
{
for(TiXmlElement* n_Value = n_Values->FirstChildElement("value"); n_Value != NULL;
{
for(TiXmlElement* n_Value = n_Values->FirstChildElement("value"); n_Value != NULL;
n_Value = n_Value->NextSiblingElement())
{
values.push_back(n_Value->GetText());
}
}
{
values.push_back(n_Value->GetText());
}
}
if ( type == "InputFilename" || type == "OutputFilename" ||
type == "Directory" || type == "InputImage" ||
type == "ComplexInputImage" || type == "InputVectorData" ||
type == "OutputImage" || type == "ComplexOutputImage" ||
type == "OutputVectorData" || type == "String" ||
type == "Choice")
{
std::string userValue = app->GetParameterString(key);
if(userValue.empty())
app->SetParameterString(key, value);
}
else if (type == "Radius" || type == "Int" || type == "RAM" )
{
type == "Directory" || type == "InputImage" ||
type == "ComplexInputImage" || type == "InputVectorData" ||
type == "OutputImage" || type == "ComplexOutputImage" ||
type == "OutputVectorData" || type == "String" ||
type == "Choice")
{
if(app->IsUseXMLValue(key))
{
app->SetParameterString(key, value);
}
else
{
std::string userValue = app->GetParameterString(key);
if(!userValue.empty())
{
app->SetParameterString(key, userValue);
}
}
}
else if (type == "Radius" || type == "Int" || type == "RAM" || type == "rand" )
{
int intValue;
std::stringstream(value) >> intValue;
int userValue = 0;
userValue = app->GetParameterInt(key);
if(userValue == 0)
app->SetParameterInt(key,intValue);
}
if(app->IsUseXMLValue(key))
{
app->SetParameterInt(key, intValue);
}
else
{
int userValue = 0;
userValue = app->GetParameterInt(key);
if(userValue != 0)
{
app->SetParameterInt(key,userValue);
}
}
}
else if (type == "Float")
{
int floatValue;
float floatValue;
std::stringstream(value) >> floatValue;
float userValue = 0;
userValue = app->GetParameterFloat(key);
if( userValue == 0 )
app->SetParameterFloat(key, floatValue);
if(app->IsUseXMLValue(key))
{
app->SetParameterFloat(key, floatValue);
}
else
{
float userValue = 0;
userValue = app->GetParameterFloat(key);
if(userValue != 0)
{
app->SetParameterFloat(key,userValue);
}
}
}
else if (type == "Empty")
{
bool emptyValue = false;
// std::stringstream(value) >> floatValue;
if( value == "true")
{
emptyValue = true;
}
if(app->IsUseXMLValue(key))
{
app->SetParameterEmpty(key, emptyValue);
}
else
{
bool userValue = app->GetParameterEmpty(key);
app->SetParameterEmpty(key,userValue);
}
}
else if (type == "InputFilenameList" || type == "InputImageList" ||
type =="InputVectorDataList" || type == "StringList" ||
type == "ListView")
{
if(values.empty())
itkExceptionMacro(<< key << " has null values");
std::vector<std::string> userValues;
userValues = app->GetParameterStringList(key);
//skip = (skip == true) ? true: ( (userValues.size() == 0) ? true : false )
if( userValues.size() == 0 )
{
app->SetParameterStringList(key,values);
}
}
itkWarningMacro(<< key << " has null values");
if(app->IsUseXMLValue(key))
{
app->SetParameterStringList(key, values);
}
else
{
std::vector<std::string> userValues;
userValues = app->GetParameterStringList(key);
if(userValues.size() != 0)
{
app->SetParameterStringList(key,userValues);
}
}
//skip = (skip == true) ? true: ( (userValues.size() == 0) ? true : false )
}
//choice also comes as setint and setstring why??
}
return 0;
}
return 0;
}
} //end namespace wrapper
} //end namespace otb
......@@ -58,9 +58,9 @@ public:
const std::string GetChildNodeTextOf(TiXmlElement *parentElement, std::string key);
std::string pixelTypeToString(ImagePixelType pixType);
ParameterType GetParameterType(const Parameter* param) const;
int Read(Application::Pointer application);
void otbAppLogInfo(Application::Pointer app, std::string info);
......
......@@ -104,7 +104,6 @@ void
OutputProcessXMLParameter::Write(Application::Pointer app)
{
// Check if the filename is not empty
if(m_FileName.empty())
itkExceptionMacro("The XML output FileName is empty, please set the filename via the method SetFileName");
......@@ -113,10 +112,9 @@ OutputProcessXMLParameter::Write(Application::Pointer app)
{
itkExceptionMacro(<<itksys::SystemTools::GetFilenameLastExtension(m_FileName)
<<" is a wrong Extension FileName : Expected .xml");
}
}
// Write the XML file
// start creating XML file
TiXmlDocument doc;
TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );
......@@ -169,16 +167,22 @@ OutputProcessXMLParameter::Write(Application::Pointer app)
Parameter *param = paramGroup->GetParameterByKey(key);
ParameterType type = app->GetParameterType(key);
std::string typeAsString = paramGroup->GetParameterTypeAsString(type);
// if param is a Group, dont do anything, ParamGroup dont have values
if (type != ParameterType_Group)
{
bool paramExists = app->HasValue(key);
bool paramExists = app->HasValue(key) && app->IsParameterEnabled(key);
if ( key == "outxml" )
paramExists = false;
{
paramExists = false;
}
if (type == ParameterType_Empty)
{
paramExists = true;
}
// if parameter doesn't have any value then skip it
if (paramExists)
if (paramExists)
{
std::vector<std::string> values;
std::string value;
......@@ -197,8 +201,6 @@ OutputProcessXMLParameter::Write(Application::Pointer app)
{
values = app->GetParameterStringList(key);
hasValueList = true;
//std::cerr << "its list type!!:" << values[0] << "\n\n\n ";
}
else
if (type == ParameterType_Float || type == ParameterType_Int ||
......@@ -215,40 +217,57 @@ OutputProcessXMLParameter::Write(Application::Pointer app)
{
value = app->GetParameterString(key);
}
else
if(key == "rand")
{
std::ostringstream strm;
strm << app->GetParameterInt("rand");
value = strm.str();
}
else
if (typeAsString == "Empty")
{
EmptyParameter* eParam = dynamic_cast<EmptyParameter *> (param);
value = "false";
if( eParam->GetActive() )
{
value = "true";
}
}
//get only file name
/*
if(type == ParameterType_InputFilename || type == ParameterType_InputImage ||
type == ParameterType_ComplexInputImage || type == ParameterType_InputVectorData ||
type == ParameterType_OutputVectorData || type == ParameterType_OutputFilename)
if(type == ParameterType_InputFilename || type == ParameterType_InputImage ||
type == ParameterType_ComplexInputImage || type == ParameterType_InputVectorData ||
type == ParameterType_OutputVectorData || type == ParameterType_OutputFilename)
{
unsigned found = value.find_last_of("/\\");
//std::cerr << " path: " << value.substr(0,found) << '\n';
value = value.substr(found+1);
unsigned found = value.find_last_of("/\\");
//std::cerr << " path: " << value.substr(0,found) << '\n';
value = value.substr(found+1);
}
else
else
if(type == ParameterType_InputImageList || type == ParameterType_InputFilenameList ||
type == ParameterType_InputVectorDataList)
{
std::vector<std::string>::iterator strIt;
for(strIt = values.begin(); strIt != values.end(); ++strIt)
type == ParameterType_InputVectorDataList)
{
std::vector<std::string>::iterator strIt;
for(strIt = values.begin(); strIt != values.end(); ++strIt)
{
std::string val = *strIt;
unsigned found = val.find_last_of("/\\");
*strIt = val.substr(found+1);
std::string val = *strIt;
unsigned found = val.find_last_of("/\\");
*strIt = val.substr(found+1);
}
}
}
*/
//parameter node in xml
TiXmlElement * n_Parameter = new TiXmlElement("parameter");
const char * mandatory = "false";
if( param->GetMandatory() )