Source

genet / class_edge.py

#!/usr/bin/env python
################################################################################
# $Id$
################################################################################
# Copyright (c) 2008 Santiago Paya Miralta <santiagopm::wanadoo.es>
#
# This file is part of mine.
#
# Mine is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
################################################################################
"""
Define base class `edge' with the minimal attributes as abstract 
attributes.

Every element in the network has:

* level: topological level, ISO level, layer, ...
* id: its own identifier.
* iddown: in which element is this element encapsulated into.
* idtype: what kind of element is.

And in linear (edges) elements that connects two puntual (nodes) elements
there are:

* idnA: one side connection point id.
* idnB: other side id.

"""

class edge:
    """
    Simple class with basic attributes.
    
    The the edge itself, 'idnA' and 'idnB' should be from the same level, 
    this is resolved at execution time from an intelligent interface.

    The 'iddown' object should be from the 'downlevel' level,
    this is resolved at execution time from an intelligent interface.
    """
    def __init__(self, level, idnA, idnB, iddown, downlevel, idtype):
        self.id = None
        self.level = level
        self.idnA = idnA
        self.idnB = idnB
        self.iddown = iddown
        self.downlevel = downlevel
        self.idtype = idtype
    
    def store(self, db):
        """
        Stores data in db. 

        Param: db is the database object.
        Return: the SQL statement and node id.
        """
        if self.id:
            st = "UPDATE gn_" + str(self.level) + "_edges SET " \
                "idnA = " + str(self.idnA) + ", " \
                "idnB" + str(self.idnB) + ", " \
                "iddown = " + str(self.iddown) + ", " \
                "downlevel = " + str(self.downlevel) + ", " \
                "idtype = " + str(self.idtype) + " " \
                "WHERE id = " + str(self.id) + ";"
            db.query(st)
        else:
            st = "INSERT INTO gn_" + str(self.level) + "_edges(idna, idnb, iddown, downlevel, idtype) " \
                "VALUES(" \
                + str(self.idnA) + ", " \
                + str(self.idnB) + ", " \
                + str(self.iddown) + ", " \
                + str(self.downlevel) + ", " \
                + str(self.idtype) + ") " \
                "RETURNING id;"
            self.id = db.query(st).getresult()[0][0]
        return st, self.id