Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r77348 - sandbox/icl/libs/xplore/br1/sqlbrowser
From: afojgo_at_[hidden]
Date: 2012-03-16 12:34:04


Author: jofaber
Date: 2012-03-16 12:34:03 EDT (Fri, 16 Mar 2012)
New Revision: 77348
URL: http://svn.boost.org/trac/boost/changeset/77348

Log:
Exploration of boost::graph.
Text files modified:
   sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.cpp | 77 ++++++++++++++++++---------------
   sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.h | 92 ++++++++++++++++++++++++++++-----------
   2 files changed, 107 insertions(+), 62 deletions(-)

Modified: sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.cpp
==============================================================================
--- sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.cpp (original)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.cpp 2012-03-16 12:34:03 EDT (Fri, 16 Mar 2012)
@@ -19,8 +19,8 @@
     foreach (QString header, headers)
         rootData << header;
 
- //rootItem = new DagItem(rootData);
- //setupModelData(data.split(QString("\n")), rootItem);
+ //m_rootItem = new DagItem(rootData);
+ //setupModelData(data.split(QString("\n")), m_rootItem);
 }
 
 /* JODO
@@ -32,20 +32,20 @@
     foreach (QString header, headers)
         rootData << header;
 
- rootItem = new DagItem(rootData);
- setupModelData(data.split(QString("\n")), rootItem);
+ m_rootItem = new DagItem(rootData);
+ setupModelData(data.split(QString("\n")), m_rootItem);
 }
 */
 
 DagModel::~DagModel()
 {
- delete rootItem;
+ delete m_rootItem;
 }
 
 
 int DagModel::columnCount(const QModelIndex & /* parent */) const
 {
- return rootItem->columnCount();
+ return m_rootItem->columnCount();
 }
 
 
@@ -78,7 +78,7 @@
         DagItem *item = static_cast<DagItem*>(index.internalPointer());
         if (item) return item;
     }
- return rootItem;
+ return m_rootItem;
 }
 
 
@@ -86,7 +86,7 @@
                                int role) const
 {
     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
- return rootItem->data(section);
+ return m_rootItem->data(section);
 
     return QVariant();
 }
@@ -112,7 +112,7 @@
     bool success;
 
     beginInsertColumns(parent, position, position + columns - 1);
- success = rootItem->insertColumns(position, columns);
+ success = m_rootItem->insertColumns(position, columns);
     endInsertColumns();
 
     return success;
@@ -124,7 +124,7 @@
     bool success;
 
     beginInsertRows(parent, position, position + rows - 1);
- success = parentItem->insertChildren(position, rows, rootItem->columnCount());
+ success = parentItem->insertChildren(position, rows, m_rootItem->columnCount());
     endInsertRows();
 
     return success;
@@ -139,7 +139,7 @@
     DagItem *childItem = getItem(index);
     DagItem *parentItem = childItem->parent();
 
- if (parentItem == rootItem)
+ if (parentItem == m_rootItem)
         return QModelIndex();
 
     return createIndex(parentItem->childNumber(), 0, parentItem);
@@ -151,10 +151,10 @@
     bool success;
 
     beginRemoveColumns(parent, position, position + columns - 1);
- success = rootItem->removeColumns(position, columns);
+ success = m_rootItem->removeColumns(position, columns);
     endRemoveColumns();
 
- if (rootItem->columnCount() == 0)
+ if (m_rootItem->columnCount() == 0)
         removeRows(0, rowCount());
 
     return success;
@@ -202,7 +202,7 @@
     if (role != Qt::EditRole || orientation != Qt::Horizontal)
         return false;
 
- bool result = rootItem->setData(section, value);
+ bool result = m_rootItem->setData(section, value);
 
     if (result)
         emit headerDataChanged(orientation, section, section);
@@ -253,7 +253,7 @@
 
             // Append a new item to the current parent's list of children.
             DagItem *parent = parents.last();
- parent->insertChildren(parent->childCount(), 1, rootItem->columnCount());
+ parent->insertChildren(parent->childCount(), 1, m_rootItem->columnCount());
             for (int column = 0; column < columnData.size(); ++column)
                 parent->child(parent->childCount() - 1)->setData(column, columnData[column]);
         }
@@ -262,6 +262,21 @@
     }
 }
 
+void DagModel::setupDag(DagItem *parent)
+{
+ /*JODO CONT
+ boost::depth_first_search(
+ m_dag
+ , boost::visitor(make_dfs_visitor(boost::make_list(
+ creater::node_start(m_rootItem, m_nodeAttributes)
+ , creater::edge_visit(m_rootItem, m_nodeAttributes)
+ , creater::node_stop (m_rootItem, m_nodeAttributes)
+ )
+ ))
+ );
+ */
+}
+
 void DagModel::getEdges(QSqlQuery& query)
 {
     while(query.next())
@@ -285,13 +300,13 @@
     else
     {
         //We skip the first record and its NIL information.
- fromSql(query, rootItem, 0);
+ fromSql(query, m_rootItem, 0);
     }
 }
 
 void DagModel::makeDag()
 {
- m_nameTags = get(name_tag(), m_dag);
+ m_nodeAttributes = get(attribute_tag(), m_dag);
 
     for(tEdgeList::iterator iter = m_edges.begin(); iter != m_edges.end(); iter++)
     {
@@ -300,27 +315,27 @@
         if(!(source==0 && target==0))
         {
             boost::add_edge(source, target, m_dag);
- m_nameTags[source] = (*iter)[m_parentName].toString();
- m_nameTags[target] = (*iter)[m_childName].toString();
+ m_nodeAttributes[source] = (*iter)[m_parentName].toString();
+ m_nodeAttributes[target] = (*iter)[m_childName].toString();
         }
     }
 }
 
 QString DagModel::dagToString()
 {
- QString dagst;
+ QString dagAsString;
 
     boost::depth_first_search(
         m_dag
       , boost::visitor(make_dfs_visitor(boost::make_list(
- node_arrival(&dagst, m_nameTags)
- , edge_visit(&dagst, m_nameTags)
- , node_final(&dagst, m_nameTags)
+ node_arrival(&dagAsString, m_nodeAttributes)
+ , edge_visit(&dagAsString, m_nodeAttributes)
+ , node_final(&dagAsString, m_nodeAttributes)
                                             )
                       ))
     );
 
- return dagst;
+ return dagAsString;
 }
 
 
@@ -343,10 +358,10 @@
         //fill node
         fillData(data, query);
 
- DagItem* curNode = (depth==0) ? new DagItem(data, 0) //curNode==rootItem
+ DagItem* curNode = (depth==0) ? new DagItem(data, 0) //curNode==m_rootItem
                                       : new DagItem(data, parent);
         if(depth == 0)
- rootItem = curNode;
+ m_rootItem = curNode;
 
         //if the new node is not a leaf, create children.
         if(!curNode->isLeaf(m_typeId))
@@ -367,7 +382,7 @@
 //JOFA Iteration example: The container as String
 QString DagModel::toString()const
 {
- return nodeToString(rootItem, 0);
+ return nodeToString(m_rootItem, 0);
 }
 
 QString DagModel::nodeToString(DagItem* node, int depth)const
@@ -389,14 +404,6 @@
     }
 }
 
-QString DagModel::indentation(int depth)const
-{
- QString indent;
- for(int idx=0; idx < depth; idx++)
- indent += " ";
- return indent;
-}
-
 
 void DagModel::fillData(QVector<QVariant>& data, QSqlQuery& query)
 {

Modified: sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.h
==============================================================================
--- sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.h (original)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.h 2012-03-16 12:34:03 EDT (Fri, 16 Mar 2012)
@@ -17,11 +17,42 @@
 
 #include <map>
 
-typedef std::map<int, int> daggy;
+
+inline QString indentation(int depth)
+{
+ QString indent;
+ for(int idx=0; idx < depth; idx++)
+ indent += " ";
+ return indent;
+}
+
+
+typedef std::map<int, int> daggy; //CL?
 
 class QSqlQuery;
 class DagItem;
 
+// An object to collect results on graph traversal
+class NodeAttributes
+{
+public:
+ NodeAttributes(): m_name(), m_depth() {}
+ NodeAttributes(const QString& name): m_name(name), m_depth() {}
+ NodeAttributes(const QString& name, int depth): m_name(name), m_depth(depth) {}
+
+ void setName(const QString& name) { m_name = name; }
+ QString name()const { return m_name; }
+
+ void setDepth(int depth){ m_depth = depth; }
+ int depth()const { return m_depth; }
+
+ int inc(){ return ++m_depth; }
+
+private:
+ QString m_name;
+ int m_depth;
+};
+
 class DagModel : public QAbstractItemModel
 {
     Q_OBJECT
@@ -78,13 +109,13 @@
 
 private:
     void setupModelData(const QStringList &lines, DagItem *parent);
+ void setupDag(DagItem *parent);
+
     DagItem *getItem(const QModelIndex &index) const;
 
     void fillData(QVector<QVariant>& data, QSqlQuery& query);
- QString indentation(int depth)const;
-
 
- DagItem *rootItem;
+ DagItem *m_rootItem;
 
     int m_parentId ;
     int m_childId ;
@@ -99,32 +130,32 @@
 
     //==========================================================================
     // Graph
- struct name_tag {
+ struct attribute_tag {
         typedef boost::vertex_property_tag kind;
     };
 
- typedef boost::property<name_tag, QString> tNameTag;
+ typedef boost::property<attribute_tag, NodeAttributes> tAttributeTag;
 
     typedef boost::adjacency_list
     < boost::vecS
     , boost::vecS
     , boost::directedS
- , tNameTag
+ , tAttributeTag
> tDag;
 
     tDag m_dag;
 
- typedef boost::property_map<tDag, name_tag>::type
- tNameTagMap;
+ typedef boost::property_map<tDag, attribute_tag>::type
+ tAttributesMap;
 
- tNameTagMap m_nameTags;
+ tAttributesMap m_nodeAttributes;
 
     //--------------------------------------------------------------------------
     // Visitors
     struct node_arrival : public boost::base_visitor<node_arrival>
     {
- node_arrival(QString* result, const tNameTagMap& names)
- : p_result(result), r_names(names){}
+ node_arrival(QString* result, tAttributesMap& names)
+ : p_result(result), r_attrs(names){}
 
         typedef boost::on_discover_vertex event_filter;
 
@@ -133,8 +164,8 @@
         {
             if(boost::out_degree(node, dag) > 0)
             {
- *p_result += "(";
- *p_result += r_names[node];
+ *p_result += indentation(r_attrs[node].depth()) + "(";
+ *p_result += r_attrs[node].name();
                 *p_result += "\n";
             }
         }
@@ -154,32 +185,39 @@
             return edge_cnt;
         }
 
- QString* p_result;
- const tNameTagMap& r_names;
+ QString* p_result;
+ tAttributesMap& r_attrs;
     };
 
     struct edge_visit : public boost::base_visitor<edge_visit>
     {
- edge_visit(QString* result, const tNameTagMap& names)
- : p_result(result), r_names(names){}
+ edge_visit(QString* result, tAttributesMap& names)
+ : p_result(result), r_attrs(names){}
 
         typedef boost::on_examine_edge event_filter;
 
         template<class Edge, class Graph>
         void operator()(Edge edge, Graph& dag)
         {
- *p_result += " ->" + r_names[target(edge, dag)];
- *p_result += "\n";
+ int source_depth = r_attrs[source(edge, dag)].depth();
+ int target_depth = source_depth + 1;
+ r_attrs[target(edge, dag)].setDepth(target_depth);
+
+ if(boost::out_degree(target(edge, dag), dag)==0)
+ {
+ *p_result += indentation(target_depth) + r_attrs[target(edge, dag)].name();
+ *p_result += "\n";
+ }
         }
 
- QString* p_result;
- const tNameTagMap& r_names;
+ QString* p_result;
+ tAttributesMap& r_attrs;
     };
 
     struct node_final : public boost::base_visitor<node_final>
     {
- node_final(QString* result, const tNameTagMap& names)
- : p_result(result), r_names(names){}
+ node_final(QString* result, tAttributesMap& names)
+ : p_result(result), r_attrs(names){}
 
         typedef boost::on_finish_vertex event_filter;
 
@@ -188,13 +226,13 @@
         {
             if(boost::out_degree(node, dag) > 0)
             {
- *p_result += ")";
+ *p_result += indentation(r_attrs[node].depth()) + ")";
                 *p_result += "\n";
             }
         }
 
- QString* p_result;
- const tNameTagMap& r_names;
+ QString* p_result;
+ tAttributesMap& r_attrs;
     };
 
 };


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk