Coverage for ion/services/coi/resource_registry/association_client : 73.61%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
|
#!/usr/bin/env python
@file ion/services/coi/resource_registry/association_client.py @author David Stuebe @brief Association Client and Association Instance are manager abstractions for associations
@ TODO """
""" A class for association client exceptions """
""" @brief This is the base class for a resource client. It is a factory for resource instances. The resource instance provides the interface for working with resources. The client helps create and manage resource instances. """
""" Initializes a association client @param proc a IProcess instance as originator of messages @param datastore the name of the datastore service with which you wish to interact with the OOICI. """ proc = process.Process()
# The resource client is backed by a process workbench.
def _check_init(self): """ Called in client methods to ensure that there exists a spawned process to send and receive messages """ yield self.proc.spawn()
'Process workbench is not initialized'
def create_association(self, subject, predicate_or_id, obj): """ @Brief Create an association between two resource instances @param subject is a resource instance which is to be the subject of the association @param predicate_id is the predicate id to use in creating the association @param obj is a resource instance which is to be the object of the association """
#if not isinstance(ResourceInstance, subject): # raise TypeError('The subject argument in the resource client, create_association method must be a resource instance.') # #if not isinstance(ResourceInstance, obj): # raise TypeError('The obj argument in the resource client, create_association method must be a resource instance.')
predicate_repo = predicate_or_id else: log.error('AssociationClient Error: type - %s, value - %s', type(predicate_or_id),str(predicate_or_id)) raise AssociationClientError('Invalid predicate_or_id passed to Create Association. Only a string ID or Predicate Repository can be passed.')
# Commit the current state of the subject and object log.error('AssociationClient Error: type - %s, value - %s', type(subject),str(subject)) raise AssociationClientError('Invalid subject passed to Create Association. Only Object Repositories and Instance types can be passed as subject or object')
log.error('AssociationClient Error: type - %s, value - %s', type(obj),str(obj)) raise AssociationClientError('Invalid object passed to Create Association. Only Object Repositories and Instance types can be passed as subject or object')
# The workbench method returns a fully formed association instance!
def get_instance(self, association_id): """ @brief Get the latest version of the identified association from the data store @param association_id can be either a string association identity or an IDRef object which specifies the association identity as well as optional parameters version and version state. @retval the specified AssociationInstance
"""
# Get the type of the argument and act accordingly # If it is a resource reference, unpack it. if association_id.branch: branch = association_id.branch
reference = association_id.key commit = association_id.commit
# if it is a string, us it as an identity # @TODO Some reasonable test to make sure it is valid?
else: raise AssociationClientError('''Illegal argument type in get_instance: \n type: %s \nvalue: %s''' % (type(association_id), str(association_id)))
# Pull the repository except workbench.WorkBenchError, ex: log.warn(ex) raise AssociationClientError('Could not pull the requested association from the datastore. Workbench exception: \n %s' % ex)
# Get the repository except repository.RepositoryError, ex: log.warn('Could not check out branch "%s":\n Current repo state:\n %s' % (branch, str(repo))) raise AssociationClientError('Could not checkout branch during get_instance.')
# Create a association instance to return # @TODO - Check and see if there is already one - what to do?
def association_exists(self, subject_or_id, predicate_or_id, object_or_id): """ @Brief Test for the existence of an association between these three resource or object identities @TODO change to take either string or IDref """
elif hasattr(object_or_id, 'Repository'): object_or_id.Repository.set_repository_reference(request.object, current_state=True)
elif hasattr(predicate_or_id, 'Repository'): predicate_or_id.Repository.set_repository_reference(request.predicate, current_state=True)
elif hasattr(subject_or_id, 'Repository'): subject_or_id.Repository.set_repository_reference(request.subject, current_state=True)
""" @Brief Get associations to a subject and/or object. Specify a predicate or predicates to limit the results @retval An association manager instance which can be used to iterate or and sort results """
elif hasattr(predicate_or_predicates, 'Repository'): predicates = [predicate_or_predicates] elif isinstance(predicate_or_predicates, list):
if None in predicates: raise AssociationClientError('None can not be in the list of predicates passed to find_associations')
# Do other type checking on the list later else: raise AssociationClientError('Invalid argument type for predicate passed to find_associations')
raise AssociationClientError('Either the subject and/or the obj must be specified in find_associations')
raise AssociationClientError('The subject argument in the resource client, fidn_associations method must be a resource instance.')
raise AssociationClientError('The "obj" argument in the resource client, find_associations method must be a resource instance.')
elif hasattr(predicate, 'Repository'): request.predicate.key = predicate.Repository.repository_key else: raise AssociationClientError('None can not be in the list of predicates passed to find_associations')
|