本文整理汇总了Python中ion.util.enhanced_resource_registry_client.EnhancedResourceRegistryClient.has_cached_predicate方法的典型用法代码示例。如果您正苦于以下问题:Python EnhancedResourceRegistryClient.has_cached_predicate方法的具体用法?Python EnhancedResourceRegistryClient.has_cached_predicate怎么用?Python EnhancedResourceRegistryClient.has_cached_predicate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ion.util.enhanced_resource_registry_client.EnhancedResourceRegistryClient
的用法示例。
在下文中一共展示了EnhancedResourceRegistryClient.has_cached_predicate方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: AgentConfigurationBuilder
# 需要导入模块: from ion.util.enhanced_resource_registry_client import EnhancedResourceRegistryClient [as 别名]
# 或者: from ion.util.enhanced_resource_registry_client.EnhancedResourceRegistryClient import has_cached_predicate [as 别名]
#.........这里部分代码省略.........
for k in somekeys:
assert k in lookup_means
assert lookup_means[k] in self.associated_objects
#check_keys([PRED.hasAgentInstance, PRED.hasModel, PRED.hasAgentDefinition])
check_keys([PRED.hasAgentInstance, PRED.hasAgentDefinition])
assert RT.ProcessDefinition in self.associated_objects
def set_agent_instance_object(self, agent_instance_obj):
"""
Set the agent instance object that we'll be interacting with
it may be necessary to set this several times, such as if external operations update the object
"""
assert agent_instance_obj._id
if self.last_id != agent_instance_obj._id:
self.associated_objects = None
self.agent_instance_obj = agent_instance_obj
self.last_id = agent_instance_obj._id
self.generated_config = False
def prepare(self, will_launch=True):
"""
Prepare (validate) an agent for launch, fetching all associated resources
@param will_launch - whether the running status should be checked -- set false if just generating config
"""
assert self.agent_instance_obj
# fetch caches just in time
if any([not self.RR2.has_cached_predicate(x) for x in self._predicates_to_cache()]):
self._update_cached_predicates()
if any([not self.RR2.has_cached_resource(x) for x in self._resources_to_cache()]):
self._update_cached_resources()
# validate the associations, then pick things up
self._collect_agent_instance_associations()
if will_launch:
# if there is an agent pid then assume that a drive is already started
agent_process_id = ResourceAgentClient._get_agent_process_id(self._get_device()._id)
if agent_process_id:
raise BadRequest("Agent Instance already running for this device pid: %s" %
str(agent_process_id))
self.will_launch = will_launch
config = self.generate_config()
return config
def _generate_org_governance_name(self):
log.debug("_generate_org_governance_name for %s", self.agent_instance_obj.name)
log.debug("retrieve the Org governance name to which this agent instance belongs")
try:
org_obj = self.RR2.find_subject(RT.Org, PRED.hasResource, self.agent_instance_obj._id, id_only=False)
return org_obj.org_governance_name
except NotFound:
return ''
except:
raise
def _generate_device_type(self):
示例2: AgentConfigurationBuilder
# 需要导入模块: from ion.util.enhanced_resource_registry_client import EnhancedResourceRegistryClient [as 别名]
# 或者: from ion.util.enhanced_resource_registry_client.EnhancedResourceRegistryClient import has_cached_predicate [as 别名]
#.........这里部分代码省略.........
assert RT.ProcessDefinition in self.associated_objects
def set_agent_instance_object(self, agent_instance_obj):
"""
Set the agent instance object that we'll be interacting with
it may be necessary to set this several times, such as if external operations update the object
"""
assert agent_instance_obj._id
if self.last_id != agent_instance_obj._id:
self.associated_objects = None
self.agent_instance_obj = agent_instance_obj
self.last_id = agent_instance_obj._id
self.generated_config = False
def prepare(self, will_launch=True):
"""
Prepare (validate) an agent for launch, fetching all associated resources
@param will_launch - whether the running status should be checked -- set false if just generating config
"""
assert self.agent_instance_obj
if will_launch:
#if there is an agent pid then assume that a drive is already started
if self.agent_instance_obj.agent_process_id:
raise BadRequest("Agent Instance already running for this device pid: %s" %
str(self.agent_instance_obj.agent_process_id))
# fetch caches just in time
if any([not self.RR2.has_cached_predicate(x) for x in self._predicates_to_cache()]):
self._update_cached_predicates()
if any([not self.RR2.has_cached_resource(x) for x in self._resources_to_cache()]):
self._update_cached_resources()
# validate the associations, then pick things up
self._collect_agent_instance_associations()
self.will_launch = will_launch
return self.generate_config()
def _generate_org_governance_name(self):
log.debug("_generate_org_governance_name for %s", self.agent_instance_obj.name)
log.debug("retrieve the Org governance name to which this agent instance belongs")
try:
org_obj = self.RR2.find_subject(RT.Org, PRED.hasResource, self.agent_instance_obj._id, id_only=False)
return org_obj.org_governance_name
except NotFound:
return ''
except:
raise
def _generate_device_type(self):
log.debug("_generate_device_type for %s", self.agent_instance_obj.name)
return type(self._get_device()).__name__
def _generate_driver_config(self):
log.debug("_generate_driver_config for %s", self.agent_instance_obj.name)
# get default config
driver_config = self.agent_instance_obj.driver_config
agent_obj = self._get_agent()
示例3: TestEnhancedResourceRegistryClient
# 需要导入模块: from ion.util.enhanced_resource_registry_client import EnhancedResourceRegistryClient [as 别名]
# 或者: from ion.util.enhanced_resource_registry_client.EnhancedResourceRegistryClient import has_cached_predicate [as 别名]
#.........这里部分代码省略.........
self.rr.find_objects.reset_mock()
self.rr.get_association.reset_mock()
rst()
self.rr.find_objects.return_value = ([], [])
self.RR2.assign_one_instrument_model_to_instrument_device_with_has_model(y, x)
self.rr.create_association.assert_called_once_with(x, PRED.hasModel, y)
rst()
self.rr.find_objects.return_value = (["a", "b"], ["c", "d"])
self.assertRaises(Inconsistent, self.RR2.assign_one_instrument_model_to_instrument_device_with_has_model, y, x)
rst()
self.rr.find_objects.return_value = (["a"], ["b"])
self.rr.get_association.return_value = "yay"
self.RR2.assign_one_instrument_model_to_instrument_device_with_has_model(y, x)
rst()
self.rr.find_objects.return_value = (["a"], ["b"])
self.rr.get_association.side_effect = NotFound("")
self.assertRaises(BadRequest, self.RR2.assign_one_instrument_model_to_instrument_device_with_has_model, y, x)
def test_assign_single_subject(self):
x = "x_id"
y = "y_id"
def rst():
self.rr.find_subjects.reset_mock()
self.rr.get_association.reset_mock()
rst()
self.rr.find_subjects.return_value = ([], [])
self.RR2.assign_instrument_device_to_one_instrument_site_with_has_device(y, x)
self.rr.create_association.assert_called_once_with(x, PRED.hasDevice, y)
rst()
self.rr.find_subjects.return_value = (["a", "b"], ["c", "d"])
self.assertRaises(Inconsistent, self.RR2.assign_instrument_device_to_one_instrument_site_with_has_device, y, x)
rst()
self.rr.find_subjects.return_value = (["a"], ["b"])
self.rr.get_association.return_value = "yay"
self.RR2.assign_instrument_device_to_one_instrument_site_with_has_device(y, x)
rst()
self.rr.find_subjects.return_value = (["a"], ["b"])
self.rr.get_association.side_effect = NotFound("")
self.assertRaises(BadRequest, self.RR2.assign_instrument_device_to_one_instrument_site_with_has_device, y, x)
def test_bad_dynamics(self):
x = "x_id"
self.RR2.assign_foo_to_bar(x)
self.rr.assign_foo_to_bar.assert_called_once_with(x)
self.assertRaises(BadRequest, getattr, self.RR2, "find_instrument_model_of_instrument_device_using_has_site")
self.assertRaises(BadRequest, getattr, self.RR2, "find_instrument_model_of_instrument_device_using_has_banana")
#self.assertRaises(BadRequest, getattr, self.RR2, "find_data_product_of_data_process")
self.RR2.find_sensor_model_by_data_product(x)
self.rr.find_sensor_model_by_data_product.assert_called_once_with(x)
def test_cached_predicate_search(self):
d = "d_id"
m = "m_id"
x = "x_id"
good_assn = DotDict(s=d, st=RT.InstrumentDevice, p=PRED.hasModel, o=m, ot=RT.InstrumentModel)
bad_assn = DotDict(s=d, st=RT.PlatformDevice, p=PRED.hasModel, o=m, ot=RT.PlatformModel)
self.rr.find_associations.return_value = [good_assn, bad_assn]
self.RR2.cache_predicate(PRED.hasModel)
self.assertTrue(self.RR2.has_cached_predicate(PRED.hasModel))
self.rr.find_associations.assert_called_once_with(predicate=PRED.hasModel, id_only=False)
# object searches that should return 0, 0, 1 results
results = self.RR2.find_objects(x, PRED.hasModel, RT.InstrumentModel, True)
self.assertEqual([], results)
results = self.RR2.find_instrument_model_ids_of_instrument_device_using_has_model(x)
self.assertEqual([], results)
results = self.RR2.find_instrument_model_ids_of_instrument_device_using_has_model(d)
self.assertEqual([m], results)
self.assertEqual(0, self.rr.find_objects.call_count)
# subject searches that should return 0, 0, 1 results
results = self.RR2.find_subjects(RT.InstrumentDevice, PRED.hasModel, x, True)
self.assertEqual([], results)
results = self.RR2.find_instrument_device_ids_by_instrument_model_using_has_model(x)
self.assertEqual([], results)
results = self.RR2.find_instrument_device_ids_by_instrument_model_using_has_model(m)
self.assertEqual([d], results)
self.assertEqual(0, self.rr.find_subjects.call_count)
示例4: freeze
# 需要导入模块: from ion.util.enhanced_resource_registry_client import EnhancedResourceRegistryClient [as 别名]
# 或者: from ion.util.enhanced_resource_registry_client.EnhancedResourceRegistryClient import has_cached_predicate [as 别名]
def freeze():
if isinstance(resource_registry_client, EnhancedResourceRegistryClient):
RR2 = resource_registry_client
else:
RR2 = EnhancedResourceRegistryClient(resource_registry_client)
for p in predicate_list:
if not RR2.has_cached_predicate(p):
RR2.cache_predicate(p)
def get_related_resources_partial_fn(predicate_dictionary, resource_whitelist):
"""
This function generates a resource crawler from 2 data structures representing desired crawl behavior
The predicate dictionary is keyed on a predicate type, whose value is a 2-tuple of booleans
the first boolean is whether to crawl subject-object, the second boolean for object-subject
For example: dict([(PRED.hasModel, (False, True)]) would generate a crawler that could find
platforms or instruments with a given model
The resource whitelist is a list of resource types that will be crawled.
The return value of this function is a function that accepts a resource id and returns a list
of associations related (based on crawl behavior)
"""
log.trace("get_related_resources_partial_fn predicate_dict=%s rsrc_whitelist=%s",
predicate_dictionary,
resource_whitelist)
# assertions on data types
assert type({}) == type(predicate_dictionary)
for v in predicate_dictionary.values():
assert type((True, True)) == type(v)
assert type([]) == type(resource_whitelist)
for rt in resource_whitelist:
RR2.cache_resources(rt)
def lookup_fn(resource_id):
"""
return a dict of related resources as dictated by the pred dict and whitelist
- the key is the next resource id to crawl
- the value is the entire association
"""
retval = {}
sto_match = lambda assn: assn.s == resource_id and assn.ot in resource_whitelist
ots_match = lambda assn: assn.o == resource_id and assn.st in resource_whitelist
for p, (search_sto, search_ots) in predicate_dictionary.iteritems():
if search_sto:
for a in RR2.filter_cached_associations(p, sto_match):
log.trace("lookup_fn matched %s object", a.ot)
retval[a.o] = a
if search_ots:
for a in RR2.filter_cached_associations(p, ots_match):
log.trace("lookup_fn matched %s subject", a.st)
retval[a.s] = a
return retval
def get_related_resources_h(accum, input_resource_id, recursion_limit):
"""
This is a recursive helper function that does the work of crawling for related resources
The accum is a tuple: (set of associations that are deemed "Related", set of "seen" resources)
The input resource id is the current resource being crawled
The recursion limit decrements with each recursive call, ending at 0. So -1 for infinity.
The return value is a list of associations
"""
if 0 == recursion_limit:
return accum
if -1000 > recursion_limit:
log.warn("Terminating related resource recursion, hit -1000")
return accum
acc, seen = accum
matches = lookup_fn(input_resource_id)
log.trace("get_related_resources_h got matches %s",
[dict((k, "%s %s %s" % (a.st, a.p, a.ot)) for k, a in matches.iteritems())])
unseen = set(matches.keys()) - seen
seen.add(input_resource_id)
acc = acc | set(matches.values())
#if log.isEnabledFor(logging.TRACE):
# summary = {}
# for a in acc:
# label = "%s %s %s" % (a.st, a.p, a.ot)
# if not label in summary: summary[label] = 0
# summary[label] += 1
# log.trace("acc2 is now %s", ["%s x%d" % (k, v) for k, v in summary.iteritems()])
#.........这里部分代码省略.........
示例5: RollXBuilder
# 需要导入模块: from ion.util.enhanced_resource_registry_client import EnhancedResourceRegistryClient [as 别名]
# 或者: from ion.util.enhanced_resource_registry_client.EnhancedResourceRegistryClient import has_cached_predicate [as 别名]
class RollXBuilder(object):
"""
for rollups and rolldowns
"""
def __init__(self, process=None):
"""
the process should be the "self" of a service instance
"""
assert process
self.process = process
self.RR2 = EnhancedResourceRegistryClient(self.process.clients.resource_registry)
def get_toplevel_platformsite(self, site_id):
if not self.RR2.has_cached_predicate(PRED.hasSite):
self.RR2.cache_predicate(PRED.hasSite)
parent_ids = self.RR2.find_platform_site_ids_by_platform_site_using_has_site(site_id)
if 0 == len(parent_ids):
return site_id # assume this to be the top level
return self.get_toplevel_platformsite(parent_ids[0])
def get_parent_network_nodes(self, site_id):
"""
return the parent nodes of this network node, including the given node
"""
if not self.RR2.has_cached_predicate(PRED.hasNetworkParent):
self.RR2.cache_predicate(PRED.hasNetworkParent)
def get_h(acc, some_id):
acc.append(some_id)
parent_ids = self.RR2.find_platform_device_ids_of_platform_device_using_has_network_parent(site_id)
if 0 == len(parent_ids):
return acc
return get_h(acc, parent_ids[0])
return get_h([], site_id)
def get_toplevel_network_node(self, device_id):
if not self.RR2.has_cached_predicate(PRED.hasNetworkParent):
self.RR2.cache_predicate(PRED.hasNetworkParent)
parent_ids = self.RR2.find_platform_device_ids_of_platform_device_using_has_network_parent(device_id)
if 0 == len(parent_ids):
# it can only be the network parent if it has this association
if 0 < len(self.RR2.find_platform_device_ids_by_platform_device_using_has_network_parent(device_id)):
return device_id # assume this to be top level
else:
return None
return self.get_toplevel_network_node(parent_ids[0])
def get_site_hierarchy(self, site_id, site_val_fn):
"""
return (child_sites, site_ancestors)
where child_sites is a dict mapping all child site ids to the value of site_val_fn(child_site_id)
and site_ancestors is a dict mapping all site ids to a list of their hasSite children.
"""
if not self.RR2.has_cached_predicate(PRED.hasSite):
self.RR2.cache_predicate(PRED.hasSite)
full_list = [site_id]
acc = {}
def _get_ancestors_h(s_id):
s_child_ids = self.RR2.find_objects(s_id, PRED.hasSite, id_only=True)
if s_child_ids:
acc[s_id] = s_child_ids
for scid in s_child_ids:
_get_ancestors_h(scid)
full_list.append(scid)
_get_ancestors_h(site_id)
return dict([(s, site_val_fn(s)) for s in full_list]), acc
def get_network_hierarchy(self, device_id, device_val_fn):
"""
return (child_devices, device_ancestors)
where child_devices is a dict mapping all child device ids to the value of device_val_fn(child_device_id)
and device_ancestors is a dict mapping all device ids to a list of their children as per
"""
if not self.RR2.has_cached_predicate(PRED.hasNetworkParent):
self.RR2.cache_predicate(PRED.hasNetworkParent)
full_list = [device_id]
acc = {}
def _get_ancestors_h(d_id):
d_child_ids = self.RR2.find_subjects(d_id, PRED.hasNetworkParent, id_only=True)
if d_child_ids:
acc[d_id] = d_child_ids
for dcid in d_child_ids:
_get_ancestors_h(dcid)
full_list.append(dcid)
#.........这里部分代码省略.........