Ejemplo n.º 1
0
 def test_basic(self):
     g = Graph()
     g.add_vertex("A")
     g.add_vertex("B")
     g.add_edge("A", "B", Link())
     g.add_edge("A", "B", Street("foo", 20.0))
     gdb_file = os.path.dirname(__file__) + "unit_test.db"
     if os.path.exists(gdb_file):
         os.remove(gdb_file)        
     gdb = GraphDatabase(gdb_file)
     gdb.populate(g)
     
     list(gdb.execute("select * from resources"))
     assert "A" in list(gdb.all_vertex_labels())
     assert "B" in list(gdb.all_vertex_labels())
     assert glen(gdb.all_edges()) == 2
     assert glen(gdb.all_outgoing("A")) == 2
     assert glen(gdb.all_outgoing("B")) == 0
     assert glen(gdb.all_incoming("A")) == 0
     assert glen(gdb.all_incoming("B")) == 2
     assert glen(gdb.resources()) == 0
     assert gdb.num_vertices() == 2
     assert gdb.num_edges() == 2
     
     g.destroy()
     g = gdb.incarnate()
     
     list(gdb.execute("select * from resources"))
     assert "A" in list(gdb.all_vertex_labels())
     assert "B" in list(gdb.all_vertex_labels())
     assert glen(gdb.all_edges()) == 2
     assert glen(gdb.all_outgoing("A")) == 2
     assert glen(gdb.all_outgoing("B")) == 0
     assert glen(gdb.all_incoming("A")) == 0
     assert glen(gdb.all_incoming("B")) == 2
     assert glen(gdb.resources()) == 0
     assert gdb.num_vertices() == 2
     assert gdb.num_edges() == 2
     
     os.remove( gdb_file )
Ejemplo n.º 2
0
    def test_basic(self):
        g = Graph()
        g.add_vertex("A")
        g.add_vertex("B")
        g.add_edge("A", "B", Link())
        g.add_edge("A", "B", Street("foo", 20.0))
        gdb_file = os.path.dirname(__file__) + "unit_test.db"
        if os.path.exists(gdb_file):
            os.remove(gdb_file)
        gdb = GraphDatabase(gdb_file)
        gdb.populate(g)

        list(gdb.execute("select * from resources"))
        assert "A" in list(gdb.all_vertex_labels())
        assert "B" in list(gdb.all_vertex_labels())
        assert glen(gdb.all_edges()) == 2
        assert glen(gdb.all_outgoing("A")) == 2
        assert glen(gdb.all_outgoing("B")) == 0
        assert glen(gdb.all_incoming("A")) == 0
        assert glen(gdb.all_incoming("B")) == 2
        assert glen(gdb.resources()) == 0
        assert gdb.num_vertices() == 2
        assert gdb.num_edges() == 2

        g.destroy()
        g = gdb.incarnate()

        list(gdb.execute("select * from resources"))
        assert "A" in list(gdb.all_vertex_labels())
        assert "B" in list(gdb.all_vertex_labels())
        assert glen(gdb.all_edges()) == 2
        assert glen(gdb.all_outgoing("A")) == 2
        assert glen(gdb.all_outgoing("B")) == 0
        assert glen(gdb.all_incoming("A")) == 0
        assert glen(gdb.all_incoming("B")) == 2
        assert glen(gdb.resources()) == 0
        assert gdb.num_vertices() == 2
        assert gdb.num_edges() == 2

        os.remove(gdb_file)
Ejemplo n.º 3
0
class GraphCrawler(Servable):
    def __init__(self, graphdb_filename):
        self.graphdb = GraphDatabase( graphdb_filename )
    
    def vertices(self, like=None):
        if like:
            return "\n".join( ["<a href=\"/vertex?label=&quot;%s&quot;\">%s</a><br>"%(vl[0], vl[0]) 
                               for vl in self.graphdb.execute("SELECT label from vertices where label like ? order by label", (like,)) ])
        else:
            return "\n".join( ["<a href=\"/vertex?label=&quot;%s&quot;\">%s</a><br>"%(vl[0], vl[0]) 
                               for vl in self.graphdb.execute("SELECT label from vertices order by label") ])
    vertices.mime = "text/html"
    
    def vertex(self, label, currtime=None, hill_reluctance=1.5, walking_speed=0.85):
        currtime = currtime or int(time.time())
        
        ret = []
        ret.append( "<h1>%s</h1>"%label )
        
        wo = WalkOptions()
        ret.append( "<h3>walk options</h3>" )
        ret.append( "<li>transfer_penalty: %s</li>"%wo.transfer_penalty )
        ret.append( "<li>turn_penalty: %s</li>"%wo.turn_penalty )
        ret.append( "<li>walking_speed: %s</li>"%wo.walking_speed )
        ret.append( "<li>walking_reluctance: %s</li>"%wo.walking_reluctance )
        ret.append( "<li>uphill_slowness: %s</li>"%wo.uphill_slowness )
        ret.append( "<li>downhill_fastness: %s</li>"%wo.downhill_fastness )
        ret.append( "<li>hill_reluctance: %s</li>"%wo.hill_reluctance )
        ret.append( "<li>max_walk: %s</li>"%wo.max_walk )
        ret.append( "<li>walking_overage: %s</li>"%wo.walking_overage )
        
        
        ret.append( "<h3>incoming from:</h3>" )
        for i, (vertex1, vertex2, edgetype) in enumerate( self.graphdb.all_incoming( label ) ):
            s1 = State(1,int(currtime))
            wo = WalkOptions()
            wo.hill_reluctance=hill_reluctance
            wo.walking_speed=walking_speed
            s0 = edgetype.walk_back( s1, wo )
            
            if s0:
                toterm = "<a href=\"/vertex?label=&quot;%s&quot;&currtime=%d\">%s@%d</a>"%(vertex1, s0.time, vertex1, s1.time)
            else:
                toterm = "<a href=\"/vertex?label=&quot;%s&quot;\">%s</a>"%(vertex1, vertex1)
            
            ret.append( "%s<br><pre>&nbsp;&nbsp;&nbsp;via %s (<a href=\"/incoming?label=&quot;%s&quot;&edgenum=%d\">details</a>)</pre>"%(toterm, cgi.escape(repr(edgetype)), vertex2, i) )
            
            if s0:
                ret.append( "<pre>&nbsp;&nbsp;&nbsp;%s</pre>"%cgi.escape(str(s0)) )
            
            
        ret.append( "<h3>outgoing to:</h3>" )
        for i, (vertex1, vertex2, edgetype) in enumerate( self.graphdb.all_outgoing( label ) ):
            s0 = State(1,int(currtime))
            wo = WalkOptions()
            wo.hill_reluctance=hill_reluctance
            wo.walking_speed=walking_speed
            s1 = edgetype.walk( s0, wo )
            
            if s1:
                toterm = "<a href=\"/vertex?label=&quot;%s&quot;&currtime=%d\">%s@%d</a>"%(vertex2, s1.time, vertex2, s1.time)
            else:
                toterm = "<a href=\"/vertex?label=&quot;%s&quot;\">%s</a>"%(vertex2, vertex2)
            
            ret.append( "%s<br><pre>&nbsp;&nbsp;&nbsp;via %s (<a href=\"/outgoing?label=&quot;%s&quot;&edgenum=%d\">details</a>)</pre>"%(toterm, cgi.escape(repr(edgetype)), vertex1, i) )
            
            if s1:
                ret.append( "<pre>&nbsp;&nbsp;&nbsp;%s</pre>"%cgi.escape(str(s1)) )
        
        wo.destroy()
        
        return "".join(ret)
    vertex.mime = "text/html"
    
    def outgoing(self, label, edgenum):
        all_outgoing = list( self.graphdb.all_outgoing( label ) )
        
        fromv, tov, edge = all_outgoing[edgenum]
        
        return edge.expound()
        
    def incoming(self, label, edgenum):
        all_incoming = list( self.graphdb.all_incoming( label ) )
        
        fromv, tov, edge = all_incoming[edgenum]
        
        return edge.expound()
    
    def str(self):
        return str(self.graphdb)
Ejemplo n.º 4
0
class GraphCrawler(Servable):
    def __init__(self, graphdb_filename):
        self.graphdb = GraphDatabase(graphdb_filename)

    def vertices(self, like=None):
        if like:
            return "\n".join([
                "<a href=\"/vertex?label=&quot;%s&quot;\">%s</a><br>" %
                (vl[0], vl[0]) for vl in self.graphdb.execute(
                    "SELECT label from vertices where label like ? order by label",
                    (like, ))
            ])
        else:
            return "\n".join([
                "<a href=\"/vertex?label=&quot;%s&quot;\">%s</a><br>" %
                (vl[0], vl[0]) for vl in self.graphdb.execute(
                    "SELECT label from vertices order by label")
            ])

    vertices.mime = "text/html"

    def vertex(self,
               label,
               currtime=None,
               hill_reluctance=1.5,
               walking_speed=0.85):
        currtime = currtime or int(time.time())

        ret = []
        ret.append("<h1>%s</h1>" % label)

        wo = WalkOptions()
        ret.append("<h3>walk options</h3>")
        ret.append("<li>transfer_penalty: %s</li>" % wo.transfer_penalty)
        ret.append("<li>turn_penalty: %s</li>" % wo.turn_penalty)
        ret.append("<li>walking_speed: %s</li>" % wo.walking_speed)
        ret.append("<li>walking_reluctance: %s</li>" % wo.walking_reluctance)
        ret.append("<li>uphill_slowness: %s</li>" % wo.uphill_slowness)
        ret.append("<li>downhill_fastness: %s</li>" % wo.downhill_fastness)
        ret.append("<li>hill_reluctance: %s</li>" % wo.hill_reluctance)
        ret.append("<li>max_walk: %s</li>" % wo.max_walk)
        ret.append("<li>walking_overage: %s</li>" % wo.walking_overage)

        ret.append("<h3>incoming from:</h3>")
        for i, (vertex1, vertex2,
                edgetype) in enumerate(self.graphdb.all_incoming(label)):
            s1 = State(1, int(currtime))
            wo = WalkOptions()
            wo.hill_reluctance = hill_reluctance
            wo.walking_speed = walking_speed
            s0 = edgetype.walk_back(s1, wo)

            if s0:
                toterm = "<a href=\"/vertex?label=&quot;%s&quot;&currtime=%d\">%s@%d</a>" % (
                    vertex1, s0.time, vertex1, s1.time)
            else:
                toterm = "<a href=\"/vertex?label=&quot;%s&quot;\">%s</a>" % (
                    vertex1, vertex1)

            ret.append(
                "%s<br><pre>&nbsp;&nbsp;&nbsp;via %s (<a href=\"/incoming?label=&quot;%s&quot;&edgenum=%d\">details</a>)</pre>"
                % (toterm, cgi.escape(repr(edgetype)), vertex2, i))

            if s0:
                ret.append("<pre>&nbsp;&nbsp;&nbsp;%s</pre>" %
                           cgi.escape(str(s0)))

        ret.append("<h3>outgoing to:</h3>")
        for i, (vertex1, vertex2,
                edgetype) in enumerate(self.graphdb.all_outgoing(label)):
            s0 = State(1, int(currtime))
            wo = WalkOptions()
            wo.hill_reluctance = hill_reluctance
            wo.walking_speed = walking_speed
            s1 = edgetype.walk(s0, wo)

            if s1:
                toterm = "<a href=\"/vertex?label=&quot;%s&quot;&currtime=%d\">%s@%d</a>" % (
                    vertex2, s1.time, vertex2, s1.time)
            else:
                toterm = "<a href=\"/vertex?label=&quot;%s&quot;\">%s</a>" % (
                    vertex2, vertex2)

            ret.append(
                "%s<br><pre>&nbsp;&nbsp;&nbsp;via %s (<a href=\"/outgoing?label=&quot;%s&quot;&edgenum=%d\">details</a>)</pre>"
                % (toterm, cgi.escape(repr(edgetype)), vertex1, i))

            if s1:
                ret.append("<pre>&nbsp;&nbsp;&nbsp;%s</pre>" %
                           cgi.escape(str(s1)))

        wo.destroy()

        return "".join(ret)

    vertex.mime = "text/html"

    def outgoing(self, label, edgenum):
        all_outgoing = list(self.graphdb.all_outgoing(label))

        fromv, tov, edge = all_outgoing[edgenum]

        return edge.expound()

    def incoming(self, label, edgenum):
        all_incoming = list(self.graphdb.all_incoming(label))

        fromv, tov, edge = all_incoming[edgenum]

        return edge.expound()

    def str(self):
        return str(self.graphdb)