Commit 3b2ddc64 authored by Floris Berendsen's avatar Floris Berendsen
Browse files

ENH: WIP: Changed index to name in blueprint API

parent 037de1bf
......@@ -3,6 +3,7 @@
#include "boost/graph/graph_traits.hpp"
#include "boost/graph/directed_graph.hpp"
#include "boost/graph/labeled_graph.hpp"
#include "itkObjectFactory.h"
#include "itkDataObject.h"
......@@ -33,12 +34,15 @@ public:
ParameterMapType parameterMap;
};
typedef boost::adjacency_list< boost::vecS,
boost::vecS,
boost::directedS,
ComponentPropertyType,
ConnectionPropertyType > GraphType;
typedef boost::labeled_graph < boost::adjacency_list <
boost::vecS,
boost::vecS,
boost::directedS,
ComponentPropertyType,
ConnectionPropertyType >,
std::string > GraphType;
typedef GraphType::label_type ComponentNameType;
typedef boost::graph_traits< GraphType >::vertex_descriptor ComponentIndexType;
typedef boost::graph_traits< GraphType >::vertex_iterator ComponentIteratorType;
typedef std::pair< ComponentIteratorType, ComponentIteratorType > ComponentIteratorPairType;
......@@ -54,10 +58,10 @@ public:
typedef std::pair< OutputIteratorType, OutputIteratorType > OutputIteratorPairType;
// Interface for managing components
ComponentIndexType AddComponent( void );
ComponentIndexType AddComponent( ParameterMapType parameterMap );
ParameterMapType GetComponent( ComponentIndexType index );
void SetComponent( ComponentIndexType, ParameterMapType parameterMap );
ComponentIndexType AddComponent( ComponentNameType name);
ComponentIndexType AddComponent(ComponentNameType name, ParameterMapType parameterMap);
ParameterMapType GetComponent(ComponentNameType name);
void SetComponent(ComponentNameType, ParameterMapType parameterMap);
// TODO: Let user delete component. Before we do this, we need a proper way of
// checking that a vertex exist. Otherwise a call to GetComponent() on
......@@ -73,12 +77,12 @@ public:
// Interface for managing connections between components in which we
// deliberately avoid using connection indexes, but instead force
// the user to think in terms of components (which is conceptually simpler)
bool AddConnection( ComponentIndexType upstream, ComponentIndexType downstream );
bool AddConnection( ComponentIndexType upstream, ComponentIndexType downstream, ParameterMapType parameterMap );
ParameterMapType GetConnection( ComponentIndexType upstream, ComponentIndexType downstream );
bool SetConnection( ComponentIndexType upstream, ComponentIndexType downstream, ParameterMapType parameterMap );
bool DeleteConnection( ComponentIndexType upstream, ComponentIndexType downstream );
bool ConnectionExists( ComponentIndexType upstream, ComponentIndexType downstream );
bool AddConnection(ComponentNameType upstream, ComponentNameType downstream);
bool AddConnection(ComponentNameType upstream, ComponentNameType downstream, ParameterMapType parameterMap);
ParameterMapType GetConnection(ComponentNameType upstream, ComponentNameType downstream);
bool SetConnection(ComponentNameType upstream, ComponentNameType downstream, ParameterMapType parameterMap);
bool DeleteConnection(ComponentNameType upstream, ComponentNameType downstream);
bool ConnectionExists(ComponentNameType upstream, ComponentNameType downstream);
// Returns iterator for all connections in the graph
ConnectionIteratorPairType GetConnectionIterator( void ) {
......@@ -87,15 +91,15 @@ public:
// Returns the outgoing connections from a component in the graph,
// i.e. all components that reads data from given component
OutputIteratorPairType GetOutputIterator( const ComponentIndexType index ) {
return boost::out_edges( index, this->m_Graph );
OutputIteratorPairType GetOutputIterator( const ComponentNameType name ) {
return boost::out_edges(this->m_Graph.vertex(name), this->m_Graph.graph());
}
void WriteBlueprint( const std::string filename );
private:
ConnectionIndexType GetConnectionIndex( ComponentIndexType upsteam, ComponentIndexType downstream );
ConnectionIndexType GetConnectionIndex( ComponentNameType upsteam, ComponentNameType downstream );
GraphType m_Graph;
......
......@@ -42,11 +42,10 @@ namespace selx {
Blueprint::ComponentIndexType
Blueprint
::AddComponent( void )
::AddComponent(ComponentNameType name)
{
this->Modified();
ComponentIndexType index = boost::add_vertex( this->m_Graph );
ComponentIndexType index = this->m_Graph.add_vertex(name);
// Return component index so component can retrieved at a later time
return index;
......@@ -54,12 +53,10 @@ Blueprint
Blueprint::ComponentIndexType
Blueprint
::AddComponent( ParameterMapType parameterMap )
::AddComponent(ComponentNameType name, ParameterMapType parameterMap)
{
this->Modified();
ComponentIndexType index = boost::add_vertex( this->m_Graph );
this->m_Graph[index].parameterMap = parameterMap;
ComponentIndexType index = this->m_Graph.add_vertex(name, { parameterMap });
// Return component index so component can retrieved at a later time
return index;
......@@ -67,19 +64,18 @@ Blueprint
Blueprint::ParameterMapType
Blueprint
::GetComponent( ComponentIndexType index )
::GetComponent(ComponentNameType name)
{
this->Modified();
return this->m_Graph[ index ].parameterMap;
return this->m_Graph[name].parameterMap;
}
void
Blueprint
::SetComponent( ComponentIndexType index, ParameterMapType parameterMap )
::SetComponent(ComponentNameType name, ParameterMapType parameterMap)
{
this->Modified();
this->m_Graph[ index ].parameterMap = parameterMap;
this->m_Graph[name].parameterMap = parameterMap;
}
// TODO: See explanation in selxBlueprint.h
......@@ -95,7 +91,7 @@ Blueprint
bool
Blueprint
::AddConnection( ComponentIndexType upstream, ComponentIndexType downstream )
::AddConnection(ComponentNameType upstream, ComponentNameType downstream)
{
this->Modified();
......@@ -104,18 +100,17 @@ Blueprint
}
// Adds directed connection from upstream component to downstream component
return boost::add_edge( upstream, downstream, this->m_Graph ).second;
return boost::add_edge_by_label(upstream, downstream, this->m_Graph).second;
}
bool
Blueprint
::AddConnection( ComponentIndexType upstream, ComponentIndexType downstream, ParameterMapType parameterMap )
::AddConnection(ComponentNameType upstream, ComponentNameType downstream, ParameterMapType parameterMap)
{
this->Modified();
if( !this->ConnectionExists( upstream, downstream ) ) {
ConnectionIndexType index = boost::add_edge( upstream, downstream, this->m_Graph ).first;
this->m_Graph[ index ].parameterMap = parameterMap;
boost::add_edge_by_label(upstream, downstream, { parameterMap }, this->m_Graph);
return true;
}
......@@ -127,7 +122,7 @@ Blueprint
Blueprint::ParameterMapType
Blueprint
::GetConnection( ComponentIndexType upstream, ComponentIndexType downstream )
::GetConnection( ComponentNameType upstream, ComponentNameType downstream )
{
this->Modified();
......@@ -136,7 +131,7 @@ Blueprint
bool
Blueprint
::SetConnection( ComponentIndexType upstream, ComponentIndexType downstream, ParameterMapType parameterMap )
::SetConnection(ComponentNameType upstream, ComponentNameType downstream, ParameterMapType parameterMap)
{
this->Modified();
......@@ -150,12 +145,12 @@ Blueprint
bool
Blueprint
::DeleteConnection( ComponentIndexType upstream, ComponentIndexType downstream )
::DeleteConnection(ComponentNameType upstream, ComponentNameType downstream)
{
this->Modified();
if( this->ConnectionExists( upstream, downstream ) ) {
this->m_Graph.remove_edge( this->GetConnectionIndex( upstream, downstream ) );
boost::remove_edge_by_label(upstream, downstream, this->m_Graph);
}
return !this->ConnectionExists( upstream, downstream );
......@@ -163,21 +158,21 @@ Blueprint
bool
Blueprint
::ConnectionExists( ComponentIndexType upstream, ComponentIndexType downstream )
::ConnectionExists( ComponentNameType upstream, ComponentNameType downstream )
{
return boost::edge( upstream, downstream, this->m_Graph).second;
return boost::edge_by_label( upstream, downstream, this->m_Graph).second;
}
Blueprint::ConnectionIndexType
Blueprint
::GetConnectionIndex( ComponentIndexType upstream, ComponentIndexType downstream )
::GetConnectionIndex( ComponentNameType upstream, ComponentNameType downstream )
{
// This function is part of the internal API and should fail hard if we use it incorrectly
if( !this->ConnectionExists( upstream, downstream ) ) {
itkExceptionMacro( "Blueprint does not contain connection from component " << upstream << " to " << downstream );
}
return boost::edge( upstream, downstream, this->m_Graph).first;
return boost::edge_by_label(upstream, downstream, this->m_Graph).first;
}
void
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment