Bulbs: How to check if two vertices are connected by an edge in Titan - groovy

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

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.

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()
}

Related

ORB SLAM2 + ROS transformation

I want to add an INS to tf which will transform pointcloud from ORB SLAM2 to real world coordinate. My idea is to do a transformation so that the INS is at the rotational center of the robot.
My tf_tree looks like this: INS -> map -> base_link -> camera_link
My problem - despite the set transformation, the point cloud is saved in the orb slam system, not in the real coordinate system with INS. For my future work I have to get point cloud with world coordinates in ECEF.
(Ubuntu 18.04 with ROS Melodic and orb_slam2__ros)
What I made:
Between INS and map I made a broadcaster that reads the data from IMU (x,y,z,w) and transforms it to ROS msg.
transform_msg.header.frame_id = "INS"
transform_msg.child_frame_id = "map"
If I understand correctly, ORB SLAM2 transforms the point cloud from base_link to the map. (These parameters can be changed na file Node.cc na file .launch of camera in OS2)
Between base_link and camera_link I have a static_transform_publisher.
node pkg="tf2_ros" type="static_transform_publisher" name="gopro_offset" args="0 0 0 0 0 0 base_link camera_link" / include file="$(find orb_slam2_ros)/ros/launch/GOPRO.launch" /

Confusion About Implementing LeafSystem With Vector Output Port Correctly

I'm a student teaching myself Drake, specifically pydrake with Dr. Russ Tedrake's excellent Underactuated Robotics course. I am trying to write a combined energy shaping and lqr controller for keeping a cartpole system balanced upright. I based the diagram on the cartpole example found in Chapter 3 of Underactuated Robotics [http://underactuated.mit.edu/acrobot.html], and the SwingUpAndBalanceController on Chapter 2: [http://underactuated.mit.edu/pend.html].
I have found that due to my use of the cart_pole.sdf model I have to create an abstract input port due receive FramePoseVector from the cart_pole.get_output_port(0). From there I know that I have to create a control signal output of type BasicVector to feed into a Saturation block before feeding into the cartpole's actuation port.
The problem I'm encountering right now is that I'm not sure how to get the system's current state data in the DeclareVectorOutputPort's callback function. I was under the assumption I would use the LeafContext parameter in the callback function, OutputControlSignal, obtaining the BasicVector continuous state vector. However, this resulting vector, x_bar is always NaN. Out of desperation (and testing to make sure the rest of my program worked) I set x_bar to the controller's initialization cart_pole_context and have found that the simulation runs with a control signal of 0.0 (as expected). I can also set output to 100 and the cartpole simulation just flies off into endless space (as expected).
TL;DR: What is the proper way to obtain the continuous state vector in a custom controller extending LeafSystem with a DeclareVectorOutputPort?
Thank you for any help! I really appreciate it :) I've been teaching myself so it's been a little arduous haha.
# Combined Energy Shaping (SwingUp) and LQR (Balance) Controller
# with a simple state machine
class SwingUpAndBalanceController(LeafSystem):
def __init__(self, cart_pole, cart_pole_context, input_i, ouput_i, Q, R, x_star):
LeafSystem.__init__(self)
self.DeclareAbstractInputPort("state_input", AbstractValue.Make(FramePoseVector()))
self.DeclareVectorOutputPort("control_signal", BasicVector(1),
self.OutputControlSignal)
(self.K, self.S) = BalancingLQRCtrlr(cart_pole, cart_pole_context,
input_i, ouput_i, Q, R, x_star).get_LQR_matrices()
(self.A, self.B, self.C, self.D) = BalancingLQRCtrlr(cart_pole, cart_pole_context,
input_i, ouput_i,
Q, R, x_star).get_lin_matrices()
self.energy_shaping = EnergyShapingCtrlr(cart_pole, x_star)
self.energy_shaping_context = self.energy_shaping.CreateDefaultContext()
self.cart_pole_context = cart_pole_context
def OutputControlSignal(self, context, output):
#xbar = copy(self.cart_pole_context.get_continuous_state_vector())
xbar = copy(context.get_continuous_state_vector())
xbar_ = np.array([xbar[0], xbar[1], xbar[2], xbar[3]])
xbar_[1] = wrap_to(xbar_[1], 0, 2.0*np.pi) - np.pi
# If x'Sx <= 2, then use LQR ctrlr. Cost-to-go J_star = x^T * S * x
threshold = np.array([2.0])
if (xbar_.dot(self.S.dot(xbar_)) < 2.0):
#output[:] = -self.K.dot(xbar_) # u = -Kx
output.set_value(-self.K.dot(xbar_))
else:
self.energy_shaping.get_input_port(0).FixValue(self.energy_shaping_context,
self.cart_pole_context.get_continuous_state_vector())
output_val = self.energy_shaping.get_output_port(0).Eval(self.energy_shaping_context)
output.set_value(output_val)
print(output)
Here are two things that might help:
If you want to get the state of the cart-pole from MultibodyPlant, you probably want to be connecting to the continuous_state output port, which gives you a normal vector instead of the abstract-type FramePoseVector. In that case, your call to get_input_port().Eval(context) should work just fine.
If you do really want to read the FramePoseVector, then you have to evaluate the input port slightly differently. You can find an example of that here.

Deriving boolean expressions from hand drawn logic gate diagrams with python OpenCv

Using tensorflow I identified all the gates, letters and nodes.
When identifying all above components, it draws a rectangle box around each component.
Therefore, in the following array it contains list of all the components detected. Each list is in the following order.
Name of component
X, Y coordinates of top left corner of rectangle
X, Y coordinates of right bottom of rectangle
Nodes (Black Points) are used to indicate a bridge over a line without crossing.
Array of all above components.
labels =[['NODE',(1002.9702758789062, 896.4686675071716), (1220.212585389614, 1067.1142654418945)], ['NODE',(1032.444071739912, 635.7160077095032),(1211.6839590370655, 763.4382424354553)],['M', (57.093908578157425,607.6229677200317),(311.9765570014715,833.807623386383)],['NODE', (344.5295810997486, 806.3690414428711), (501.8982524871826, 930.6454839706421)], ['Z', (21.986433800309896, 1327.9791088104248), (266.36098374426365, 1565.158670425415)], ['OR', (476.0066536962986, 574.401759147644), (918.3125713765621, 1177.1423168182373)], ['NODE', (333.50814148783684, 1058.0092916488647), (497.6142471432686, 1202.9034795761108)], ['K', (37.06201596558094, 870.0414619445801), (311.77860628068447, 1105.8665227890015)], ['AND', (665.9987451732159, 1227.940999031067), (1062.7052736580372, 1594.6843948364258)],['AND', (1373.9987451732159, 204.940999031067), (1703.7052736580372, 612.6843948364258)], ['NOT', (694.2882044911385, 260.5083291530609), (1027.812717139721, 450.35294365882874)], ['XOR', (2027.6711627840996, 593.0362477302551), (2457.9011510014534, 1093.9836854934692)], ['J', (85.69029207900167, 253.8458535671234), (334.48535946011543, 456.5887498855591)], ['OUTPUT', (2657.3825285434723, 670.8418045043945), (2929.8974316120148, 975.4852895736694)]]
Then, using line detection algorithm I identified all the 17 lines connecting each component.
All above 17 lines take as a list of arrays, and those lines are not in a correct order and each line has 2 end points.
lines = [[(60, 1502), (787, 1467)], [(125, 1031), (691, 988)], [(128, 772), (685, 758)], [(131, 336),(709,347)], [(927,350),(1455, 348)], [(400, 1361), (792, 1369)], [(834, 843), (2343, 939)], [(915, 1430), (1119, 1424)], [(1125, 468), (1453, 470)], [(1587, 399), (1911, 405)], [(1884, 755), (2245, 814)],[(2372, 831), (2918, 859)], [(1891, 397), (1901, 767)], [(1138, 457), (1128, 738)], [(441, 738), (421, 903)], [(1125, 946), (1101, 1437)], [(420, 1098), (408, 1373)]]
When connecting those lines, it is must to consider following scenario.
It means nodes are used to indicate a bridge without crossing lines.
M is an input to the AND gate and (M.Z) is an input to the other AND gate.
So how can I generate the following Boolean Expression using above 2 arrays and above scenario? This should be a function that works for all logic gates.
It can be assumed that image is always going to read from left to right.

actions=[[type=ACTION_OUTPUT action=[port=1 maxLen=0]];], as keywords to get_or_create in Bulbs

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 want to program the Titan Graph DB from my python program.I am using bulbs package for that.
I create a vertex from python using bulbs as given below.
fe1 = self.g.vertices.get_or_create('switch_dpid',switch_dpid,
{'actionOutputPort':actionOutputPort,
'switch_state':'FE_SWITCH_UPDATED',
'matchInPort': MatchInPort,
'type': 'flow_entry',
'user_state':'FE_USER_ADD',
'actions': ['type':'ACTION_OUTPUT', 'action':[port=actionOutputPort maxLen=0];]})
This is giving me an error
'actions': ['type':'ACTION_OUTPUT', 'action':[port=actionOutputPort maxLen=0];]})
SyntaxError: invalid syntax
The output that I would expect from the Rexster console is as follows.
switch_dpid=00:00:00:00:00:00:02:05,
actionOutputPort=1,
switch_state=FE_SWITCH_UPDATED,
matchInPort=2,
flow_entry_id=0x4ee30a9400000012,
type=flow_entry,
actions=[[type=ACTION_OUTPUT action=[port=1 maxLen=0]];],
user_state=FE_USER_ADD
How do I program actions so that it is as above.?
You're mixing up Groovy syntax with Python.
actions is a dictionary and action is a dictionary so in Python it should be:
'actions': {'type': 'ACTION_OUTPUT',
'action': {port: actionOutputPort,
maxLen: 0}}
Note it is usually more convenient (less quotes) to create Python dictionaries using the dict function:
'actions' = dict(type = 'ACTION_OUTPUT',
action = dict(port = actionOutputPort,
maxLen = 0))

Rexster query returns "No such property: v for class: com.thinkaurelius.titan.graphdb.database.StandardTitanGraph"

I'm using TitanGraphDB + Cassandra. I'm 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'm attempting to model a network topology using Titan Graph DB. I want to program the Titan Graph DB from my python program. I'm using bulbs package for that.
I create five types of vertices
- switch
- port
- device
- flow
- flow_entry
I create edges between vertices that are connected logically. The edges are not labelled.
Let us say I want to test the connectivity between Vertex A and Vertex B
I have a groovy script is_connected.groovy
def isConnected (portA, portB) {
return portA.both().retain([portB]).hasNext()
}
Now from my rexster console
g = rexster.getGraph("graph")
==>titangraph[embeddedcassandra:null]
rexster[groovy]> g.V('type', 'flow')
==>v[116]
==>v[100]
rexster[groovy]> g.V('type', 'flow_entry')
==>v[120]
==>v[104]
As you can see above I have two vertices of type flow v[116] and v[100]
I have two vertices of type flow_entry v[120] and v[104]
I want to check for the connectivity between v[120] and v[116] for e.g
rexster[groovy]> ?e is_connected.groovy
==>null
rexster[groovy]> is_connected(g.v[116],g.v[120])
==>An error occurred while processing the script for language [groovy]. All transactions across all graphs in the session have been concluded with failure: java.util.concurrent.ExecutionException: javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: v for class: com.thinkaurelius.titan.graphdb.database.StandardTitanGraph
Either I am doing something very wrong,or I am missing something obvious.It would be great if you could point me in the right direction.
This syntax is not valid groovy:
is_connected(g.v[116],g.v[120])
should be:
is_connected(g.v(116),g.v(120))
You're mixing up Python syntax with Gremlin-Groovy syntax:
You defined the Groovy script as:
def isConnected (portA, portB) {
return portA.both().retain([portB]).hasNext()
}
...so...
rexster[groovy]> is_connected(g.v[116], g.v[120])
...should be...
rexster[groovy]> isConnected(g.v(116), g.v(120))

Resources