Skip to content
Snippets Groups Projects
Commit 22d20eca authored by Julien Michel's avatar Julien Michel
Browse files

- Ajouts de la gestion d'erreur à la lecture et à l'écriture des graphes RCC8 par des exceptions.

- Modifications du Writer pour éviter de briser le pipeline d'éxécution.
- Modification du Reader et du Writer pour la gestion des cas d'erreurs.
parent 4450cfbe
No related branches found
No related tags found
No related merge requests found
...@@ -19,9 +19,33 @@ ...@@ -19,9 +19,33 @@
#define __otbRCC8GraphFileReader_h #define __otbRCC8GraphFileReader_h
#include "otbRCC8GraphSource.h" #include "otbRCC8GraphSource.h"
#include "itkExceptionObject.h"
namespace otb namespace otb
{ {
/** \class RCC8GraphFileReaderException
* \brief Base exception class for IO problems during reading.
*/
class RCC8GraphFileReaderException
: public itk::ExceptionObject
{
public:
/** Run-time information. */
itkTypeMacro( RCC8GraphFileReaderException, ExceptionObject );
/** Constructor. */
RCC8GraphFileReaderException(const char *file, unsigned int line,
const char* message = "Error in IO",
const char* loc = "Unknown" ) :
ExceptionObject(file, line, message, loc)
{}
/** Constructor. */
RCC8GraphFileReaderException(const std::string &file, unsigned int line,
const char* message = "Error in IO",
const char* loc = "Unknown" ) :
ExceptionObject(file, line, message, loc)
{}
};
/** /**
* \class RCC8GraphFileReader * \class RCC8GraphFileReader
* \brief This class reads a RCC8 graph from a .dot file (graphviz format). * \brief This class reads a RCC8 graph from a .dot file (graphviz format).
......
...@@ -103,9 +103,26 @@ void ...@@ -103,9 +103,26 @@ void
RCC8GraphFileReader<TOutputGraph> RCC8GraphFileReader<TOutputGraph>
::GenerateData() ::GenerateData()
{ {
otbMsgDevMacro(<<"RCC8GraphFileWriter: Call to the GenerateData method");
std::ifstream fin; std::ifstream fin;
std::string line; std::string line;
// open file input stream
fin.open(m_FileName.c_str()); fin.open(m_FileName.c_str());
// Test if the file has been opened correctly
if(!fin)
{
RCC8GraphFileReaderException e(__FILE__, __LINE__);
itk::OStringStream msg;
msg << " Could not create IO object for file ";
msg<<m_FileName<<"."<<std::endl;
e.SetDescription(msg.str().c_str());
throw e;
return;
}
// if so, parse it
while(!fin.eof()) while(!fin.eof())
{ {
std::getline(fin,line); std::getline(fin,line);
...@@ -114,7 +131,7 @@ RCC8GraphFileReader<TOutputGraph> ...@@ -114,7 +131,7 @@ RCC8GraphFileReader<TOutputGraph>
// edge line // edge line
this->ParseEdge(line); this->ParseEdge(line);
} }
else else if(line.find("[")!=std::string::npos)
{ {
// vertex line // vertex line
this->ParseVertex(line); this->ParseVertex(line);
......
...@@ -19,12 +19,36 @@ ...@@ -19,12 +19,36 @@
#define __otbRCC8GraphFileWriter_h #define __otbRCC8GraphFileWriter_h
#include "itkProcessObject.h" #include "itkProcessObject.h"
#include "itkExceptionObject.h"
#include "otbRCC8Graph.h" #include "otbRCC8Graph.h"
namespace otb namespace otb
{ {
/** \class RCC8GraphFileWriterException
* \brief Base exception class for IO problems during writing.
*/
class RCC8GraphFileWriterException
: public itk::ExceptionObject
{
public:
/** Run-time information. */
itkTypeMacro( RCC8GraphFileWriterException, ExceptionObject );
/** Constructor. */
RCC8GraphFileWriterException(const char *file, unsigned int line,
const char* message = "Error in IO",
const char* loc = "Unknown" ) :
ExceptionObject(file, line, message, loc)
{}
/** Constructor. */
RCC8GraphFileWriterException(const std::string &file, unsigned int line,
const char* message = "Error in IO",
const char* loc = "Unknown" ) :
ExceptionObject(file, line, message, loc)
{}
};
/** /**
* \class RCC8GraphFileWriter * \Class RCC8GraphFileWriter
* \brief This class writes a RCC8 Graph to a dot file (graphviz file format). * \brief This class writes a RCC8 Graph to a dot file (graphviz file format).
* *
* The writer first loops on the vertices of the graph, getting the property map * The writer first loops on the vertices of the graph, getting the property map
...@@ -66,13 +90,17 @@ public: ...@@ -66,13 +90,17 @@ public:
* Set the input graph. * Set the input graph.
* \param inputGraph The graph to write. * \param inputGraph The graph to write.
*/ */
void SetInput(const InputGraphType* inputGraph); virtual void SetInput(const InputGraphType* inputGraph);
/** /**
* Get the input graph. * Get the input graph.
* \return The input graph pointer. * \return The input graph pointer.
*/ */
InputGraphPointerType GetInput(); virtual InputGraphPointerType GetInput();
/**
* Update method.
*/
virtual void Update(void);
protected: protected:
/** Constructor */ /** Constructor */
RCC8GraphFileWriter(); RCC8GraphFileWriter();
...@@ -81,7 +109,12 @@ protected: ...@@ -81,7 +109,12 @@ protected:
/** /**
* Main computation method. * Main computation method.
*/ */
void GenerateData(void); virtual void GenerateData(void);
/**
* Write Method.
* Performs checkings and invoke GenerateData().
*/
virtual void Write(void);
/** /**
* Write an edge to file. * Write an edge to file.
* \param of The output file stream. * \param of The output file stream.
......
...@@ -35,7 +35,6 @@ RCC8GraphFileWriter<TInputGraph> ...@@ -35,7 +35,6 @@ RCC8GraphFileWriter<TInputGraph>
{ {
this->SetNumberOfRequiredInputs(1); this->SetNumberOfRequiredInputs(1);
m_FileName = ""; m_FileName = "";
otbMsgDebugMacro(<<"RCC8GraphFileWriter: Call to constructor");
} }
/** /**
* Destructor * Destructor
...@@ -68,6 +67,49 @@ RCC8GraphFileWriter<TInputGraph> ...@@ -68,6 +67,49 @@ RCC8GraphFileWriter<TInputGraph>
{ {
return static_cast<TInputGraph*>(this->itk::ProcessObject::GetInput(0)); return static_cast<TInputGraph*>(this->itk::ProcessObject::GetInput(0));
} }
/**
* Update method.
* (Call the Write() method).
*/
template <class TInputGraph>
void
RCC8GraphFileWriter<TInputGraph>
::Update(void)
{
this->Write();
}
/**
* Write Method.
* Performs checkings and invoke GenerateData().
*/
template <class TInputGraph>
void
RCC8GraphFileWriter<TInputGraph>
::Write(void)
{
InputGraphType * input = this->GetInput();
itkDebugMacro( <<"Writing a RCC8Graph file" );
// Make sure input is available
if ( input == 0 )
{
itkExceptionMacro(<< "No input to writer!");
}
// Make sure that we can write the file given the name
//
if ( m_FileName == "" )
{
itkExceptionMacro(<<"No filename was specified");
}
if(input->GetSource())
{
input->GetSource()->UpdateOutputData(input);
}
this->GenerateData();
}
/** /**
* Main computation method. * Main computation method.
*/ */
...@@ -86,8 +128,21 @@ RCC8GraphFileWriter<TInputGraph> ...@@ -86,8 +128,21 @@ RCC8GraphFileWriter<TInputGraph>
// Output file stream // Output file stream
std::ofstream out; std::ofstream out;
// open the outputfile // open the outputfile
out.open(m_FileName.c_str(), std::ios::out); out.open(m_FileName.c_str(), std::ios::out);
// Test if the file has been opened correctly
if(!out)
{
RCC8GraphFileWriterException e(__FILE__, __LINE__);
itk::OStringStream msg;
msg << " Could not create IO object for file ";
msg<<m_FileName<<"."<<std::endl;
e.SetDescription(msg.str().c_str());
throw e;
return;
}
// Start writing the graph to file // Start writing the graph to file
out<<"digraph G {"<<std::endl; out<<"digraph G {"<<std::endl;
......
...@@ -31,8 +31,6 @@ RCC8GraphSource<TOutputGraph> ...@@ -31,8 +31,6 @@ RCC8GraphSource<TOutputGraph>
{ {
this->Superclass::SetNumberOfRequiredOutputs(1); this->Superclass::SetNumberOfRequiredOutputs(1);
this->Superclass::SetNthOutput(0,TOutputGraph::New().GetPointer()); this->Superclass::SetNthOutput(0,TOutputGraph::New().GetPointer());
TOutputGraph * output = this->GetOutput();
output = OutputGraphType::New();
} }
/** /**
* Get the output Graph * Get the output Graph
......
...@@ -113,6 +113,12 @@ ADD_TEST(srTvRCC8GraphIOEndToEnd ${SPATIALREASONING_TESTS} ...@@ -113,6 +113,12 @@ ADD_TEST(srTvRCC8GraphIOEndToEnd ${SPATIALREASONING_TESTS}
${TEMP}/srRCC8GraphIOEndToEndOut.dot ${TEMP}/srRCC8GraphIOEndToEndOut.dot
) )
# ------- otb::ImageListToRCC8GraphFilter --------------------------
#ADD_TEST(srTuImageListToRCC8GraphFilterNew ${SPATIALREASONING_TESTS}
# otbImageListToRCC8GraphFilterNew)
# ------- Fichiers sources CXX ----------------------------------- # ------- Fichiers sources CXX -----------------------------------
SET(BasicSpatialReasoning_SRCS SET(BasicSpatialReasoning_SRCS
otbImageToImageRCC8CalculatorNew.cxx otbImageToImageRCC8CalculatorNew.cxx
...@@ -131,6 +137,7 @@ otbRCC8GraphFileWriter.cxx ...@@ -131,6 +137,7 @@ otbRCC8GraphFileWriter.cxx
otbRCC8GraphFileReaderNew.cxx otbRCC8GraphFileReaderNew.cxx
otbRCC8GraphFileReader.cxx otbRCC8GraphFileReader.cxx
otbRCC8GraphIOEndToEnd.cxx otbRCC8GraphIOEndToEnd.cxx
#otbImageListTORCC8GraphFilterNew.cxx
) )
INCLUDE_DIRECTORIES("${OTBTesting_BINARY_DIR}") INCLUDE_DIRECTORIES("${OTBTesting_BINARY_DIR}")
......
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "itkExceptionObject.h"
#include "otbRCC8Graph.h"
#include "otbRCC8VertexBase.h"
int otbImageListToRCC8GraphFilterNew(int argc, char* argv[])
{
try
{
const unsigned int Dimension = 2;
typedef unsigned short LabelPixelType;
typedef otb::Image<LabelPixelType,Dimension> LabelImageType;
typedef otb::RCC8VertexBase<LabelPixelType> VertexType;
typedef otb::RCC8Graph<VertexType> RCC8GraphType;
typedef otb::ImageListToRCC8GraphFilter<LabelImageType,RCC8GraphType>
ImageListToRCC8GraphFilterType;
// Instanatiation
ImageListToRCC8GraphFilterType::Pointer filter = ImageListToRCC8GraphFilter::New();
}
catch( itk::ExceptionObject & err )
{
std::cout << "Exception itk::ExceptionObject thrown !" << std::endl;
std::cout << err << std::endl;
return EXIT_FAILURE;
}
catch( ... )
{
std::cout << "Unknown exception thrown !" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
}
...@@ -42,4 +42,5 @@ REGISTER_TEST(otbRCC8GraphFileWriter); ...@@ -42,4 +42,5 @@ REGISTER_TEST(otbRCC8GraphFileWriter);
REGISTER_TEST(otbRCC8GraphFileReaderNew); REGISTER_TEST(otbRCC8GraphFileReaderNew);
REGISTER_TEST(otbRCC8GraphFileReader); REGISTER_TEST(otbRCC8GraphFileReader);
REGISTER_TEST(otbRCC8GraphIOEndToEnd); REGISTER_TEST(otbRCC8GraphIOEndToEnd);
//REGISTER_TEST(otbImageListToRCC8GraphFilterNew);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment