3

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
liv2hak
  • 14,472
  • 53
  • 157
  • 270
  • 3
    gremlindocs.com is a sweet site that has lots of gremlin instructions and recipes. For example: http://gremlindocs.com/#recipes/finding-edges-between-vertices – Bob B Jul 08 '14 at 13:11

2 Answers2

4

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.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • From my python code I do the following from bulbs.titan import Graph g = Graph() Will the above work with this or should I use createTinkerGraph()? – liv2hak Jul 09 '14 at 08:17
  • I only used `createTinkerGraph()` to create some sample data to demonstrate the use of `retain`. Obviously you should use your own instate of `g` defined in Rexster. – stephen mallette Jul 09 '14 at 09:44
  • I have tried what you suggested but I am getting some error.I have updated the question with details. thanks. – liv2hak Jul 09 '14 at 10:07
  • 1
    I realize that you are using Bulbs, but to properly use that tool you need to understand Gremlin and its roots in Groovy. Right now it seems that you are trying to mix my working Groovy with Python/Bulbs stuff...that won't work. espeed can correct me if I'm wrong, but I think you need to write your function as a groovy script which you can then execute as a function in bulbs. http://bulbflow.com/api/bulbs/gremlin/ – stephen mallette Jul 09 '14 at 11:31
  • I have posted another so question on how to call a gremlin script.http://stackoverflow.com/questions/24769857/calling-a-gremlin-script-from-python-program-that-uses-bulbs – liv2hak Jul 16 '14 at 05:00
2
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()
}
espeed
  • 4,754
  • 2
  • 39
  • 51
  • can count() return any positive number or zero or just '0' or '1'? – liv2hak Jul 08 '14 at 09:59
  • `count()` can return 0 or any positive number (any positive number is `true`); however, I updated the answer to use `retain()` and `hasNext()` so `count()` is no longer relevant to this answer. – espeed Jul 08 '14 at 10:04
  • I am using Gremlin code from Python? Did you mean Jython or was that a typo? – liv2hak Jul 09 '14 at 07:17
  • It seems to be able to use gremlin as mentioned in the link above I have to create a TinkerGraph() from my python code.I am currently creating Graph() ? Will that work if I want to use gremlin queries? – liv2hak Jul 09 '14 at 07:21
  • I got this error when I tried the above code File "/home/karthik/Projects/ryu/ryu/app/simple_switch.py", line 160, in is_connected return portA.both("link").retain([portB]).hasNext() File "/usr/local/lib/python2.7/dist-packages/bulbs/element.py", line 209, in __getattr__ raise AttributeError(name) AttributeError: both – liv2hak Jul 09 '14 at 07:32