Go to the documentation of this file.00001
00002
00003 from bayesian_belief_networks.srv import Query, QueryResponse
00004 from bayesian_belief_networks.msg import Result, Observation
00005 from bayesian.bbn import *
00006
00007 import rospy
00008 _bnn_object = None
00009
00010 def service_query(req):
00011 global _bnn_object
00012
00013 kwds = dict()
00014 rospy.loginfo('query = %s'%req.query)
00015 for observation in req.query:
00016 kwds[observation.node] = observation.evidence
00017 result = _bnn_object.query(**kwds)
00018 rospy.loginfo('results = %s'%result)
00019
00020 res = QueryResponse()
00021 for (node, value), prob in result.items():
00022 r = Result(node,value,prob)
00023 res.results.append(r)
00024 res.results = sorted(res.results, key=lambda x: x.node)
00025 return res
00026
00027
00028 def ros_build_bbn(*args, **kwds):
00029 global _bnn_object
00030 "call rospy.init_node() before build_ros_bbn() and call rospy.spin() after that"
00031 _bnn_object = build_bbn(*args, **kwds)
00032 rospy.Service('~query', Query, service_query)
00033 return _bnn_object
00034