Skip to content

Commit

Permalink
Comments added
Browse files Browse the repository at this point in the history
  • Loading branch information
corot committed Jun 6, 2014
1 parent 003fd37 commit 5cec40f
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions world_canvas_server/src/annotations_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ class AnnotationsServer:
##########################################################################

def __init__(self):
# Set up collections
# Set up collections: for every annotation, we store a couple of Annotation and AnnotationData
# messages, the second containing the annotation id and a serialized representation of a message
# of type annotation.type (data field)
# TODO following issue #1, we must go for N-1 implementation instead of 1-1 as we have now
self.anns_collection = \
wr.MessageCollection("world_canvas", "annotations", Annotation)
self.anns_collection.ensure_index("id")
Expand All @@ -62,6 +65,7 @@ def __init__(self):
wr.MessageCollection("world_canvas", "annotations_data", AnnotationData)
self.data_collection.ensure_index("id")

# Set up services
self.get_anns_srv = \
rospy.Service('get_annotations', GetAnnotations, self.getAnnotations)
self.get_data_srv = \
Expand All @@ -79,7 +83,7 @@ def __init__(self):
self.set_related_srv = \
rospy.Service('set_relationship', SetRelationship, self.setRelationship)

# Configure import from/export to YAML file
# Configure services for import from/export to YAML file
self.yaml_db = YAMLDatabase(self.anns_collection, self.data_collection)
self.import_srv = \
rospy.Service('yaml_import', YAMLImport, self.yaml_db.importFromYAML)
Expand All @@ -97,6 +101,9 @@ def getAnnotations(self, request):

response = GetAnnotationsResponse()

# Compose query concatenating filter criteria in an '$and' operator
# Keywords and relationships are lists: operator '$in' makes a N to N matching
# Empty fields are ignored
query = {'$and':[]}
query['$and'].append({'world_id': {'$in': [unique_id.toHexString(request.world_id)]}})
if len(request.ids) > 0:
Expand All @@ -108,6 +115,7 @@ def getAnnotations(self, request):
if len(request.relationships) > 0:
query['$and'].append({'relationships': {'$in': [unique_id.toHexString(r) for r in request.relationships]}})

# Execute the query and retrieve results
matching_anns = self.anns_collection.query(query)

i = 0
Expand Down Expand Up @@ -173,6 +181,7 @@ def pubAnnotationsData(self, request):
object_list = list()
while True:
try:
# Get annotation data and deserialize data field to get the original message of type request.topic_type
ann_data = matching_data.next()[0]
object = pickle.loads(ann_data.data)
if request.pub_as_list:
Expand All @@ -195,7 +204,12 @@ def pubAnnotationsData(self, request):


def loadAnnotationsData(self, request):
'''
Legacy method kept for debug purposes: loads together annotations and its data
assuming a 1-1 relationship.
:param request: Service request.
'''
response = LoadAnnotationsDataResponse()

query = {'world_id': {'$in': [unique_id.toHexString(request.world_id)]}}
Expand Down Expand Up @@ -234,6 +248,12 @@ def loadAnnotationsData(self, request):
return response

def saveAnnotationsData(self, request):
'''
Legacy method kept for debug purposes: saves together annotations and its data
assuming a 1-1 relationship.
:param request: Service request.
'''
response = SaveAnnotationsDataResponse()

print request.annotations
Expand All @@ -257,6 +277,7 @@ def saveAnnotationsData(self, request):

# Insert both annotation and associated data to the appropriate collection
# TODO: using by now the same metadata for both, while data only need annotation id
# WARN WARN WARN: setKeyword and setRelationship only operate on annotations metadata!
self.anns_collection.remove({'id': {'$in': [unique_id.toHexString(annotation.id)]}})
self.anns_collection.insert(annotation, metadata)
self.data_collection.remove({'id': {'$in': [unique_id.toHexString(annotation.id)]}})
Expand Down

0 comments on commit 5cec40f

Please sign in to comment.