3

我正在使用 TitanGraphDB + Cassandra。我按如下方式启动 Titan

cd titan-cassandra-0.3.1
bin/titan.sh config/titan-server-rexster.xml config/titan-server-cassandra.properties

我有一个 Rexster shell,可以用来与上面的 Titan+Cassandra 通信。

cd rexster-console-2.3.0
bin/rexster-console.sh

我正在尝试使用 Titan Graph DB 对网络拓扑进行建模。我想从我的 python 程序中对 Titan Graph DB 进行编程。我正在为此使用灯泡包。我创建了三种类型的顶点

 - switch
 - port 
 - device

我在物理连接的端口之间创建标记边缘。我使用的标签是“链接”。

假设我有两个端口顶点portAportB.

我想写一个函数如下

def is_connected(portA, portB):
     ...
     ... 
     ...

如何查找两个顶点是否“由标记的边缘连接”?

我有两个图顶点

src_sw_other_ports
<Vertex: http://localhost:8182/graphs/graph/vertices/48>

dst_sw_other_ports
<Vertex: http://localhost:8182/graphs/graph/vertices/72>

我努力了

link_exists = src_sw_other_ports.out.retain([dst_sw_other_ports]).hasNext()

它给了我以下错误。

  File "/home/karthik/Projects/ryu/ryu/app/simple_switch.py", line 238, in compute_path
    link_exists = src_sw_other_ports.out.retain([dst_sw_other_ports]).hasNext()
  File "/usr/local/lib/python2.7/dist-packages/bulbs/element.py", line 209, in __getattr__
    raise AttributeError(name)
AttributeError: out
4

2 回答 2

4

espeed 的回答很好。这是另一种选择:

gremlin> g = TinkerGraphFactory.createTinkerGraph()
==>tinkergraph[vertices:6 edges:6]
gremlin> v1 = g.v(1)
==>v[1]
gremlin> v3 = g.v(3)
==>v[3]
gremlin> v6 = g.v(6)
==>v[6]
gremlin> v1.out.retain([v3]).hasNext()
==>true
gremlin> v1.out.retain([v6]).hasNext()
==>false

比使用count好像你只想要边缘的“存在”更好一点,你不必迭代整个管道来这样做。

于 2014-07-08T10:01:46.420 回答
2
def is_connected(portA, portB):
    return portA.both("link").retain([portB]).hasNext()

请参阅http://gremlindocs.com/#recipes/finding-edges-between-vertices

注意:我在上面的代码中使用了你的函数定义;但是,您的函数定义使用 Python 语法,而不是 Groovy,因此上述示例仅在您从 Jython 调用 Gremlin 代码时才有效。

Gremlin-Groovy 的定义是:

def isConnected (portA, portB) {
     return portA.both("link").retain([portB]).hasNext()
}
于 2014-07-08T09:56:28.253 回答