Commits

Santiago Payà Miralta committed a022240

Connect nodes tab has three trees, with edge types.

  • Participants
  • Parent commits 29671ab

Comments (0)

Files changed (7)

File control/genet_browser.py

 from control import genet_global 
 from model.db_connection import db
 
-def listLevels():
+def listLevels(level=None):
     """List levels from db.
 
+    @param level from which gets information
     @returns list the levels
     """
     st = "SELECT id, description FROM gn____levels;"
+    if level is not None:
+        st = st[:-1] + " WHERE id = " + str(level) + ";"
     return db.query(st).getresult()
 
 def listNodeTypes(level=None):

File control/genet_creator.py

     TODO store any attributes len.
 
     @param level id
-    @param idparent id
+    @param parent id
     @param parentlevel id
-    @param idtype id
+    @param type id
     @param attributes tuple of node attributes
     """
     N = class_node.node(level_id, parent_id, parentlevel_id, type_id)
     tag_type =  genet_global.tagName(type_id)
 
     st = ("INSERT INTO gn_" + tag_level + "_nodes_type_" + tag_type +  
-          " VALUES(" + str(node_id) + 
-          ", '" + attributes[0] + "');")
+          " VALUES(" + str(node_id) + ", '" + attributes[0] + "');")
     db.query(st)
 
 
-def connectNodes(level, idnA, idnB):
+def connectNodes(level_id, idnA, idnB, parent_id, parentlevel_id, type_id, attributes):
+    """Create edge in db.
+
+    Uses Edge class for the edge creation and coding for the edge type
+    attributes store.
+
+    TODO store any attributes len.
+
+    @param level id
+    @param node A id
+    @param node B id
+    @param parent id
+    @param parentlevel id
+    @param type id
+    @param attributes tuple of edge attributes
     """
-    TODO
-    """
+    E = class_edge.edge(level_id, idnA, idnB, parent_id, parentlevel_id, type_id)
+    edge_id = E.store(db)
 
+    tag_level = genet_global.tagName(level_id)
+    tag_type =  genet_global.tagName(type_id)
+
+    st = ("INSERT INTO gn_" + tag_level + "_edges_type_" + tag_type +  
+          " VALUES(" + str(edge_id) + ", '" + attributes[0] + "');")
+    db.query(st)
+    
+    

File model/class_edge.py

 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 ################################################################################
-
-from control import genet_global
-
 """
 Define base class `edge' with the minimal attributes as abstract 
 attributes.
 * idnB: other side id.
 
 """
+
+from control import genet_global
+
 class edge:
     """
     Simple class with basic attributes.
                 "WHERE idchild = " + str(self.id) + ";")
             db.query(st2)
 
-            return st1, st2, self.id
+            return self.id
 
         # If the edge does not exist.
         else:
                 + str(self.level) + ");")
             db.query(st2)
 
-            return st1, st2, self.id
+            return self.id
         

File model/db_schema.py

     ALTER TABLE """ + tablename + """
         ADD CONSTRAINT """ + tablename + """_fkey_type 
         FOREIGN KEY ("idedge")
-        REFERENCES gn_""" + level + """_nodes("id") 
+        REFERENCES gn_""" + level + """_edges("id") 
         ON UPDATE RESTRICT ON DELETE RESTRICT;
     """
     return st, db.query(st)

File view/genet.glade

                 <property name="n_rows">3</property>
                 <property name="n_columns">3</property>
                 <child>
-                  <widget class="GtkButton" id="button_connect_nodes">
-                    <property name="label" translatable="yes">Connect</property>
+                  <widget class="GtkButton" id="button_browser_draw_level">
+                    <property name="label" translatable="yes">Level</property>
                     <property name="visible">True</property>
                     <property name="can_focus">True</property>
                     <property name="receives_default">True</property>
-                    <signal name="clicked" handler="on_button_connect_nodes_clicked"/>
+                    <signal name="clicked" handler="on_button_browser_draw_level_clicked"/>
                   </widget>
                   <packing>
-                    <property name="left_attach">1</property>
-                    <property name="right_attach">2</property>
-                    <property name="top_attach">1</property>
-                    <property name="bottom_attach">2</property>
+                    <property name="left_attach">2</property>
+                    <property name="right_attach">3</property>
+                    <property name="x_options">GTK_FILL</property>
+                    <property name="y_options"></property>
+                    <property name="x_padding">5</property>
+                    <property name="y_padding">5</property>
                   </packing>
                 </child>
                 <child>
-                  <widget class="GtkLabel" id="label_browser_level">
+                  <widget class="GtkButton" id="button_browser_draw_tree">
+                    <property name="label" translatable="yes">Tree</property>
                     <property name="visible">True</property>
-                    <property name="label" translatable="yes">Level</property>
-                  </widget>
-                </child>
-                <child>
-                  <widget class="GtkComboBoxEntry" id="comboboxentry_browser_level">
-                    <property name="visible">True</property>
-                  </widget>
-                  <packing>
-                    <property name="left_attach">1</property>
-                    <property name="right_attach">2</property>
-                  </packing>
-                </child>
-                <child>
-                  <widget class="GtkComboBoxEntry" id="comboboxentry_browser_nA">
-                    <property name="visible">True</property>
-                  </widget>
-                  <packing>
-                    <property name="top_attach">1</property>
-                    <property name="bottom_attach">2</property>
-                  </packing>
-                </child>
-                <child>
-                  <widget class="GtkComboBoxEntry" id="comboboxentry_browser_nB">
-                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="receives_default">True</property>
+                    <signal name="clicked" handler="on_button_browser_draw_tree_clicked"/>
                   </widget>
                   <packing>
                     <property name="left_attach">2</property>
                     <property name="right_attach">3</property>
                     <property name="top_attach">1</property>
                     <property name="bottom_attach">2</property>
+                    <property name="x_options">GTK_FILL</property>
+                    <property name="y_options"></property>
+                    <property name="x_padding">5</property>
+                    <property name="y_padding">5</property>
+                  </packing>
+                </child>
+                <child>
+                  <widget class="GtkScrolledWindow" id="treeview_browser_scrolledwindow">
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="hscrollbar_policy">automatic</property>
+                    <property name="vscrollbar_policy">automatic</property>
+                    <child>
+                      <widget class="GtkTreeView" id="treeview_browser">
+                        <property name="visible">True</property>
+                        <property name="can_focus">True</property>
+                      </widget>
+                    </child>
+                  </widget>
+                  <packing>
+                    <property name="bottom_attach">3</property>
                   </packing>
                 </child>
                 <child>
                     <property name="bottom_attach">2</property>
                     <property name="x_options">GTK_FILL</property>
                     <property name="y_options"></property>
+                    <property name="x_padding">5</property>
+                    <property name="y_padding">5</property>
                   </packing>
                 </child>
                 <child>
                     <property name="bottom_attach">3</property>
                     <property name="x_options">GTK_FILL</property>
                     <property name="y_options"></property>
+                    <property name="x_padding">5</property>
+                    <property name="y_padding">5</property>
                   </packing>
                 </child>
                 <child>
-                  <widget class="GtkComboBox" id="combobox_conn_nodes_type">
+                  <widget class="GtkScrolledWindow" id="treeview_conn_nodes_edge_types_scrolledwindow">
                     <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="hscrollbar_policy">automatic</property>
+                    <property name="vscrollbar_policy">automatic</property>
+                    <child>
+                      <widget class="GtkTreeView" id="treeview_conn_nodes_edge_types">
+                        <property name="visible">True</property>
+                        <property name="can_focus">True</property>
+                      </widget>
+                    </child>
                   </widget>
                   <packing>
                     <property name="left_attach">1</property>
                     <property name="right_attach">2</property>
-                    <property name="x_options">GTK_FILL</property>
-                    <property name="y_options"></property>
+                    <property name="x_padding">5</property>
+                    <property name="y_padding">5</property>
                   </packing>
                 </child>
               </widget>
                       <packing>
                         <property name="x_options">GTK_FILL</property>
                         <property name="y_options"></property>
+                        <property name="x_padding">5</property>
+                        <property name="y_padding">5</property>
                       </packing>
                     </child>
                     <child>
                         <property name="bottom_attach">2</property>
                         <property name="x_options">GTK_FILL</property>
                         <property name="y_options"></property>
+                        <property name="x_padding">5</property>
+                        <property name="y_padding">5</property>
                       </packing>
                     </child>
                   </widget>

File view/genet_treemodels.py

 from control import genet_browser
 
 ################################################################################
-# Connect nodes tree
+# Browse nodes tree
 #
-class ConnectNodesTreeModel(gtk.TreeStore):
-    """Tree model to select node for connect each others.
+class BrowseNodesTreeModel(gtk.TreeStore):
+    """Tree model to browse network nodes.
 
-    The tree presents the nodes to select the connect one.
+    The tree presents the nodes to see heritage. 
 
     |> level id, level name, ""
     |  > node type id, node type name, "Node"
-    |    > node id, node att, "Node"
+    |    > node id, node id, node att
     """
     def __init__(self):
         gtk.TreeStore.__init__(self,
         self.load()
 
 ################################################################################
+# Connect nodes tree
+#
+class ConnectNodesTreeModel(gtk.TreeStore):
+    """Tree model to select node for connect each others.
+
+    The tree presents the nodes to select the connect one.
+
+    |> level id, level name, ""
+    |  > node type id, node type name, "Node"
+    |    > node id, node id, node att
+    """
+    def __init__(self):
+        gtk.TreeStore.__init__(self,
+                               gobject.TYPE_STRING, 
+                               gobject.TYPE_STRING,
+                               gobject.TYPE_STRING)
+        self.load()
+    
+    def load(self):
+        list_levels = genet_browser.listLevels()
+    
+        for i in list_levels:
+            iter_level = self.append(None)
+            self.set(iter_level, 0, i[0], 1, i[1], 2, "")
+        
+            # Fill children with this level nodetypes
+            list_node_types = genet_browser.listNodeTypes(i[0])
+            for j in list_node_types:
+                iter_node_type = self.append(iter_level)
+                self.set(iter_node_type, 0, j[0], 1, j[1], 2, "Node")
+            
+                # Fill children with this level & type nodes
+                list_nodes = genet_browser.listNodesAtt(i[0], j[0])
+                for k in list_nodes:
+                    iter_node = self.append(iter_node_type)
+                    self.set(iter_node, 0, k[0], 1, k[0], 2, k[1])
+
+    def reload(self):
+        self.clear()
+        self.load()
+
+################################################################################
+# Connect nodes edge types tree
+#
+class ConnectNodesEdgeTypesTreeModel(gtk.TreeStore):
+    """Tree model to select edge type for connect nodes.
+
+    The tree presents the edge types that may be used to connect nodes.
+
+    |> level id, level name, ""
+    |  > edge type id, edge type name, ""
+    """
+    def __init__(self):
+        gtk.TreeStore.__init__(self,
+                               gobject.TYPE_STRING, 
+                               gobject.TYPE_STRING,
+                               gobject.TYPE_STRING)
+        self.load()
+    
+    def load(self, level=None):
+        list_levels = genet_browser.listLevels(level)
+    
+        for i in list_levels:
+            iter_level = self.append(None)
+            self.set(iter_level, 0, i[0], 1, i[1], 2, "")
+        
+            # Fill children with this level edgetypes
+            list_edge_types = genet_browser.listEdgeTypes(i[0])
+            for j in list_edge_types:
+                iter_edge_type = self.append(iter_level)
+                self.set(iter_edge_type, 0, j[0], 1, j[1], 2, "")
+            
+    def reload(self, level=None):
+        self.clear()
+        self.load(level)
+
+################################################################################
 # Create node types tree
 #
 class CreateNodeTypesTreeModel(gtk.TreeStore):
 
     |> level id, level name, ""
     |  > node type id, node type name, "Node"
-    |    > node id, node att, "Node"
+    |    > node id, node att, ""
     """
     def __init__(self):
         gtk.TreeStore.__init__(self,
 # Admin tree
 #
 class AdminTreeModel(gtk.TreeStore):
-    """
-    Has three columns: level, id, ...
+    """Tree model to select and admin levels, node types or edge types.
+
+    The tree presents levels and types. Can select any item to delete or add
+    stuff.
+
+    |> level id, level name, ""
+    |  > node type id, node type name, "Node"
+    |  > edge type id, edge type name, "Edge"
     """
     def __init__(self):
         gtk.TreeStore.__init__(self,

File view/genet_window.py

 
 Has one tab for every work:
 
-Admin tab: create levels and types.
-Create tab: create nodes into the network. Must declare type of node and 
+* Admin tab: create levels and types.
+* Create tab: create nodes into the network. Must declare type of node and 
 parent in which the node is embebed.
-Connect tab: connects nodes.
-Browse tab: shows network graphs.
+* Connect tab: connects nodes.
+* Browse tab: shows network graphs (uses networkx, graphviz, ...).
 """
 
 ## TODO My emacs needs this
 import sys
-sys.path.append('/home/santiago/workspace/genet')
+if '/home/santiago/workspace/genet' not in sys.path:
+    sys.path.append('/home/santiago/workspace/genet')
 ##
 
 import gtk
         
         # Fill tree views.
         ##
+        # Browse tree
+        tree = self.wTree.get_widget("treeview_browser")
+        model = genet_treemodels.BrowseNodesTreeModel()
+        tree.set_model(model)
+    
+        cell = gtk.CellRendererText()
+
+        # Ids column should disapear in production version
+        column00 = gtk.TreeViewColumn("Id", cell, text=0)
+        column00.set_clickable(True)
+        tree.append_column(column00)
+
+        column01 = gtk.TreeViewColumn("Level", cell, text=1)
+        column01.set_clickable(True)
+        tree.append_column(column01)
+
+        column02 = gtk.TreeViewColumn("Attribute", cell, text=2)
+        column02.set_clickable(True)
+        tree.append_column(column02)
+
+        tree.set_expander_column(tree.get_column(1))
+
+        ##
         # Connect nodes A tree
         tree = self.wTree.get_widget("treeview_conn_nodes_A")
         model = genet_treemodels.ConnectNodesTreeModel()
         tree.set_expander_column(tree.get_column(1))
 
         ##
+        # Connect nodes edge types tree
+        tree = self.wTree.get_widget("treeview_conn_nodes_edge_types")
+        model = genet_treemodels.ConnectNodesEdgeTypesTreeModel()
+        tree.set_model(model)
+    
+        cell = gtk.CellRendererText()
+
+        # Ids column should disapear in production version
+        column00 = gtk.TreeViewColumn("Id", cell, text=0)
+        column00.set_clickable(True)
+        tree.append_column(column00)
+
+        column01 = gtk.TreeViewColumn("Type", cell, text=1)
+        column01.set_clickable(True)
+        tree.append_column(column01)
+
+        column02 = gtk.TreeViewColumn("", cell, text=2)
+        column02.set_clickable(True)
+        tree.append_column(column02)
+
+        tree.set_expander_column(tree.get_column(1))
+
+        ##
         # Create node types tree
         tree = self.wTree.get_widget("treeview_create_node_types")
         model = genet_treemodels.CreateNodeTypesTreeModel()
         # Declare actions dictionary.
         dic = {
             # Browse tab.
-            "on_button_connect_nodes_clicked" : 
-            self.button_connect_nodes_clicked,
+            "on_button_browser_draw_level_clicked" : 
+            self.button_browser_draw_level_clicked,
+            "on_button_browser_draw_tree_clicked" : 
+            self.button_browser_draw_tree_clicked,
 
             # Connect nodes tab.
             "on_button_conn_nodes_clicked" :
     ############################################################################
     # Browse tab.
     #
-    def button_connect_nodes_clicked(self, widget):
-        print 'Connect Clicked!'
+    def button_browser_draw_level_clicked(self, widget):
+        print 'Level Clicked!'
+
+    def button_browser_draw_tree_clicked(self, widget):
+        print 'Tree Clicked!'
         
     ############################################################################
     # Connect nodes tab.
     #
     def button_conn_nodes_clicked(self, widget):
-        print 'Connect Clicked!!'
+        """Connects nodes selected left and right with an edge type.
+        
+        The edge type is taken from the edge types tree.
+        """
+        # TODO all interface flow must be loaded by steps, selecting node A,
+        # then edge type and then node B.
+        # Get node A level
+        root_values = self.get_tree_selected_root_values("treeview_conn_nodes_A")
+        level_id = root_values[0]
 
-        # Refresh tree
-        self.tree_refresh("treeview_conn_nodes_A")
-        self.tree_refresh("treeview_conn_nodes_B")
+        # Load edge types in this level
+        tree = self.wTree.get_widget("treeview_conn_nodes_edge_types")
+        tree.get_model().reload(int(level_id))
+        tree.expand_all()
         
+        # Get type
+#        type_values = self.get_tree_selected_values("treeview_conn_nodes_edge_types")
+#        type_id = type_values[0]
+
     def button_conn_nodes_refresh_clicked(self, widget):
         """ Refresh trees from data model.
         """
         self.tree_refresh("treeview_conn_nodes_A")
+        self.tree_refresh("treeview_conn_nodes_edge_types")
         self.tree_refresh("treeview_conn_nodes_B")
-        
 
     ############################################################################
     # Create tab.
     # Admin tab.
     #
     def button_create_level_clicked(self, widget):
+        """Create a level in the network.
+        """
         # Get description
         description = self.wTree.get_widget("entry_admin_level").get_text()
 
         self.tree_refresh("treeview_admin_levels")
 
     def button_create_node_type_clicked(self, widget):
+        """Create a node type.
+
+        Gets the level in which to create the node type from any selected
+        item in the tree.
+        """
         # Get level
-        level = self.get_admin_tree_level()
+        root_values = self.get_tree_selected_root_values("treeview_admin_levels")
+        level_id = root_values[0]
 
         # Get description
         description = self.wTree.get_widget("entry_admin_type").get_text()
 
         # Create type
-        genet_creator.createNodeType(level, description)
+        genet_creator.createNodeType(level_id, description)
 
         # Refresh tree
         self.tree_refresh("treeview_admin_levels")
 
     def button_create_edge_type_clicked(self, widget):
+        """Create an edge type.
+
+        Gets the level in which to create the edge type from any selected
+        item in the tree.
+        """
         # Get level
-        level = self.get_admin_tree_level()
+        root_values = self.get_tree_selected_root_values("treeview_admin_levels")
+        level_id = root_values[0]
 
         # Get description
         description = self.wTree.get_widget("entry_admin_type").get_text()
         self.tree_refresh("treeview_admin_levels")
 
     def button_drop_item_clicked(self, widget):
+        """Purge selected item.
+
+        Gets the item to delete from selection tree.
+        """
         values = self.get_tree_selected_values("treeview_admin_levels")
 
         values_id = values[0]
         # Refresh tree
         self.tree_refresh("treeview_admin_levels")
 
-    def get_admin_tree_level(self):
-        """Get selected level. From the level row or any selected child.
-        """
-        tree = self.wTree.get_widget("treeview_admin_levels")
-        tree_selection = tree.get_selection()
-        tree_selection_model = tree_selection.get_selected()[0]
-        tree_selection_iter = tree_selection.get_selected()[1]
-        
-        # Get if selection has a parent or if it is the parent
-        parent_iter = tree_selection_model.iter_parent(tree_selection_iter)
-        if parent_iter is None:
-            values = tree_selection_model.get(tree_selection_iter, 0, 1, 2)
-            level = values[0]
-        else:
-            parent_values = tree_selection_model.get(parent_iter, 0, 1, 2)
-            level = parent_values[0]
-
-        return level        
-
     ############################################################################
     # Auxiliary functions.
     #
         """Get all information from the selected row in the tree.
         """
         tree = self.wTree.get_widget(tree_widget)
-        tree_selection = tree.get_selection()
-        tree_selection_model = tree_selection.get_selected()[0]
-        tree_selection_iter = tree_selection.get_selected()[1]
-        tree_n_columns = tree_selection_model.get_n_columns()
+        tree_sel = tree.get_selection()
+        tree_sel_model, tree_sel_iter = tree_sel.get_selected()
+        tree_n_columns = tree_sel_model.get_n_columns()
         
         values = ()
         for c in range(tree_n_columns):
-            values += tree_selection_model.get(tree_selection_iter, c)
+            values += tree_sel_model.get(tree_sel_iter, c)
 
         return values
 
     def tree_refresh(self, tree_widget):
         """Refresh tree data without forget selection.
         """
-        tree_selected_path = (0,)
-
         tree = self.wTree.get_widget(tree_widget)
-        tree_selection = tree.get_selection()
-        tree_selection_model = tree_selection.get_selected()[0]
-        tree_selection_iter = tree_selection.get_selected()[1]
+        tree_sel = tree.get_selection()
+        tree_sel_model, tree_sel_iter = tree_sel.get_selected()
         
         # Save selected item or path
-        if tree_selection_iter is not None:
-            tree_selected_path = tree_selection_model.get_path(tree_selection_iter)
+        tree_sel_path = (0,)
+        if tree_sel_iter is not None:
+            tree_sel_path = tree_sel_model.get_path(tree_sel_iter)
 
         # Refresh data, reload data into model
         tree.get_model().reload()
 
         # Expand old selection
-        tree.expand_to_path(tree_selected_path)
-        #tree_selection.select_path(tree_selected_path)
+        tree.expand_to_path(tree_sel_path)
+        #tree_sel.select_path(tree_sel_path)
 
 if __name__ == '__main__':
     app = GenetWindow()