ros_utils.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from bayesian_belief_networks.srv import Query, QueryResponse
4 from bayesian_belief_networks.msg import Result, Observation
5 from bayesian.bbn import *
6 
7 import rospy
8 _bnn_object = None
9 
10 def service_query(req):
11  global _bnn_object
12 
13  kwds = dict()
14  rospy.loginfo('query = %s'%req.query)
15  for observation in req.query:
16  kwds[observation.node] = observation.evidence
17  result = _bnn_object.query(**kwds)
18  rospy.loginfo('results = %s'%result)
19  #
20  res = QueryResponse()
21  for (node, value), prob in result.items():
22  r = Result(node,value,prob)
23  res.results.append(r)
24  res.results = sorted(res.results, key=lambda x: x.node)
25  return res
26 
27 
28 def ros_build_bbn(*args, **kwds):
29  global _bnn_object
30  "call rospy.init_node() before build_ros_bbn() and call rospy.spin() after that"
31  _bnn_object = build_bbn(*args, **kwds)
32  rospy.Service('~query', Query, service_query)
33  return _bnn_object
34 


bayesian_belief_networks
Author(s):
autogenerated on Wed Jul 10 2019 03:47:00