3
votes

I am using TitanGraphDB + Cassandra.I am starting Titan as follows

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

I have a Rexster shell that I can use to communicate to Titan+Cassandra above.

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

I am attempting to model a network topology using Titan Graph DB.I want to program the Titan Graph DB from my python program.I am using bulbs package for that. I create three types of vertices

 - switch
 - port 
 - device

I create labelled edges between ports that are connected physically.The label that I use is "link".

Let us say I have two port vertices portA and portB.

I want to write a function as below

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

How do I find if two vertices are "connected by a labelled edge"?

I have two graph vertices

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

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

I have tried

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

It is giving me the following error.

  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
2
gremlindocs.com is a sweet site that has lots of gremlin instructions and recipes. For example: gremlindocs.com/#recipes/finding-edges-between-verticesBob B

2 Answers

4
votes

espeed's answer is good. Here's another alternative:

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

A little better than using count as if you just want "existence" of an edge, you don't have to iterate the entire pipe to do so.

2
votes
def is_connected(portA, portB):
    return portA.both("link").retain([portB]).hasNext()

See http://gremlindocs.com/#recipes/finding-edges-between-vertices

NB: I used your function definition in the code above; however, your function definition uses Python syntax, not Groovy, so the above example will only work if you are calling the Gremlin code from Jython.

The Gremlin-Groovy definition would be:

def isConnected (portA, portB) {
     return portA.both("link").retain([portB]).hasNext()
}