From b6824dd09a58d2e400851fc310b416f4311bd030 Mon Sep 17 00:00:00 2001 From: LV-426 Date: Wed, 15 Oct 2014 18:36:02 +0200 Subject: model.graph package --- src-py/model/model.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src-py/model/model.py (limited to 'src-py/model/model.py') diff --git a/src-py/model/model.py b/src-py/model/model.py new file mode 100644 index 00000000..9ee61397 --- /dev/null +++ b/src-py/model/model.py @@ -0,0 +1,9 @@ +class link(): + """ + documentation anchor - this class currently carries no implementation + and only acts as a documentation anchor + + link['__src'] - meta attribute for link source + link['__dst'] - meta attribute for link destination + """ + pass -- cgit v1.3.1 From 8d35498c06d3e832bdbcad2916efe2df9b23492d Mon Sep 17 00:00:00 2001 From: LV-426 Date: Tue, 21 Oct 2014 19:56:58 +0200 Subject: introducing the Link_Ptr concept - able to fuzzy point at a link by src_id, dst_id or both --- src-py/db_controller.py | 40 +++++++++++++++++++++++++--------------- src-py/model/model.py | 25 +++++++++++++++++++++++-- src-py/test_db_controller.py | 29 +++++++++++++++++++---------- 3 files changed, 67 insertions(+), 27 deletions(-) (limited to 'src-py/model/model.py') diff --git a/src-py/db_controller.py b/src-py/db_controller.py index 7cd1af7b..ceb5267c 100644 --- a/src-py/db_controller.py +++ b/src-py/db_controller.py @@ -264,28 +264,38 @@ class DBO_match_node_set_by_id_attribute(DBO_match_node_id_set): super(DBO_match_node_set_by_id_attribute, self).__init__(filter_attr_map={'id': id_set}) -class DBO_match_link_set_by_src_or_dst_id_attributes(DB_op): - def __init__(self, src_id=None, dst_id=None): +class DBO_load_link_set(DB_op): + def __init__(self, link_ptr_set): """ - match a set of links by source/target node id attributes + match a set of sets of links by source/target node id attributes + This class should be instantiated through a static factory function + + @link_ptr_set link pointer set @return: a set of loaded links """ - assert None != src_id or None != dst_id + super(DBO_load_link_set, self).__init__() + + for l_ptr in link_ptr_set: + if not l_ptr.src_id: + q = "match ()-[r]->({id: {dst_id}}) return r" + q_params = {'dst_id': l_ptr.dst_id} + elif not l_ptr.dst_id: + q = "match ({id: {src_id}})-[r]->() return r" + q_params = {'src_id': l_ptr.src_id} + else: + q = "match ({id: {src_id}})-[r]->({id: {dst_id}}) return r" + q_params = {'src_id': l_ptr.src_id, 'dst_id': l_ptr.dst_id} - super(DBO_match_link_set_by_src_or_dst_id_attributes, self).__init__() + self.add_statement(q, q_params) - if not src_id: - q = "match ()-[r]->({id: {dst_id}}) return r" - q_params = {'dst_id': dst_id} - elif not dst_id: - q = "match ({id: {src_id}})-[r]->() return r" - q_params = {'src_id': src_id} - else: - q = "match ({id: {src_id}})-[r]->({id: {dst_id}}) return r" - q_params = {'src_id': src_id, 'dst_id': dst_id} + @staticmethod + def init_from_link_ptr(l_ptr): + return DBO_load_link_set([l_ptr]) - self.add_statement(q, q_params) + @staticmethod + def init_from_link_ptr_set(l_ptr_set): + return DBO_load_link_set(l_ptr_set) class DBO_match_link_id_set(DB_op): def __init__(self, filter_type=None, filter_attr_map={}): diff --git a/src-py/model/model.py b/src-py/model/model.py index 9ee61397..701074ab 100644 --- a/src-py/model/model.py +++ b/src-py/model/model.py @@ -1,4 +1,4 @@ -class link(): +class Link(): """ documentation anchor - this class currently carries no implementation and only acts as a documentation anchor @@ -6,4 +6,25 @@ class link(): link['__src'] - meta attribute for link source link['__dst'] - meta attribute for link destination """ - pass + + class Link_Ptr(dict): + def __init__(self, src_id=None, dst_id=None): + assert None != src_id or None != dst_id + + self['__src'] = src_id + self['__dst'] = dst_id + + @property + def src_id(self): + return self['__src'] + + @property + def dst_id(self): + return self['__dst'] + + @staticmethod + def link_ptr(src_id=None, dst_id=None): + """ + init from src_id or dst_id attributes - at least one must be provided + """ + return Link.Link_Ptr(src_id, dst_id) diff --git a/src-py/test_db_controller.py b/src-py/test_db_controller.py index 4af4cabc..cdc0c5e9 100644 --- a/src-py/test_db_controller.py +++ b/src-py/test_db_controller.py @@ -122,19 +122,28 @@ class TestDBController(unittest.TestCase): id_set = self.db_ctl.exec_op(op) self.assertEqual(len(id_set), 0) - def test_match_link_set_by_src_or_dst_id_attributes(self): - op = dbc.DBO_match_link_set_by_src_or_dst_id_attributes(src_id='person_00', dst_id='skill_00') - n_set = self.db_ctl.exec_op(op) - self.assertEqual(len(n_set), 1) + def test_load_link_set(self): + l_ptr = Link.link_ptr(src_id='person_00', dst_id='skill_00') + op = dbc.DBO_load_link_set.init_from_link_ptr(l_ptr) + l_set = self.db_ctl.exec_op(op) + self.assertEqual(len(l_set), 1) - op = dbc.DBO_match_link_set_by_src_or_dst_id_attributes(src_id='person_00') - n_set = self.db_ctl.exec_op(op) - self.assertEqual(len(n_set), 2) + l_ptr = Link.link_ptr(src_id='person_00') + op = dbc.DBO_load_link_set.init_from_link_ptr(l_ptr) + l_set = self.db_ctl.exec_op(op) + self.assertEqual(len(l_set), 2) - op = dbc.DBO_match_link_set_by_src_or_dst_id_attributes(dst_id='skill_00') - n_set = self.db_ctl.exec_op(op) - self.assertEqual(len(n_set), 1) + l_ptr = Link.link_ptr(dst_id='skill_00') + op = dbc.DBO_load_link_set.init_from_link_ptr(l_ptr) + l_set = self.db_ctl.exec_op(op) + self.assertEqual(len(l_set), 1) + # load sets + l_ptr_set = [Link.link_ptr(s,d) for (s,d) in [('person_00', 'skill_00'), ('person_00', 'skill_01')]] + op = dbc.DBO_load_link_set.init_from_link_ptr_set(l_ptr_set) + l_set = self.db_ctl.exec_op(op) + self.assertEqual(len(l_set), 2) + def test_load_node_set_by_DB_id(self): """ test node DB id life cycle -- cgit v1.3.1 From 0950293605eecff2de9cdadccf1def2d7d9b73bc Mon Sep 17 00:00:00 2001 From: LV-426 Date: Wed, 10 Dec 2014 15:16:06 +0200 Subject: distinguish __src (obj) vs. __src_id (str) --- src-py/db_controller.py | 4 ++-- src-py/model/model.py | 25 ++++++++++++++++--------- src-py/neo4j_util.py | 16 ++++++++-------- src-py/rhizi_api.py | 4 ++-- 4 files changed, 28 insertions(+), 21 deletions(-) (limited to 'src-py/model/model.py') diff --git a/src-py/db_controller.py b/src-py/db_controller.py index 460686a5..2d4fe8ce 100644 --- a/src-py/db_controller.py +++ b/src-py/db_controller.py @@ -426,8 +426,8 @@ class DBO_rz_clone(DB_op): continue l = l_tuple[1] - l['__src'] = n['id'] - l['__dst'] = l_tuple[0] + l['__src_id'] = n['id'] + l['__dst_id'] = l_tuple[0] l['__label_set'] = [l_tuple[2]] # box single value returned by type() ret_l_set.append(l) diff --git a/src-py/model/model.py b/src-py/model/model.py index 701074ab..33cf9e6a 100644 --- a/src-py/model/model.py +++ b/src-py/model/model.py @@ -3,24 +3,31 @@ class Link(): documentation anchor - this class currently carries no implementation and only acts as a documentation anchor - link['__src'] - meta attribute for link source - link['__dst'] - meta attribute for link destination + link['__src'] - meta attribute for link source object + link['__dst'] - meta attribute for link destination object """ - + + def __init__(self, src=None, dst=None): + assert False, 'currently unused' + class Link_Ptr(dict): + """ + link['__src_id'] - meta attribute for link source id + link['__dst_id'] - meta attribute for link destination id + """ def __init__(self, src_id=None, dst_id=None): assert None != src_id or None != dst_id - - self['__src'] = src_id - self['__dst'] = dst_id - + + self['__src_id'] = src_id + self['__dst_id'] = dst_id + @property def src_id(self): - return self['__src'] + return self['__src_id'] @property def dst_id(self): - return self['__dst'] + return self['__dst_id'] @staticmethod def link_ptr(src_id=None, dst_id=None): diff --git a/src-py/neo4j_util.py b/src-py/neo4j_util.py index cc3d53d8..a203207f 100644 --- a/src-py/neo4j_util.py +++ b/src-py/neo4j_util.py @@ -199,16 +199,16 @@ def gen_query_create_from_link_map(link_map, input_to_DB_property_map=lambda _: for link in l_set: __type_check_link(link) - n_src = link['__src'] - n_dst = link['__dst'] + src_id = link['__src_id'] + dst_id = link['__dst_id'] # TODO: use object based link representation prop_dict = link.copy() - del prop_dict['__dst'] - del prop_dict['__src'] + del prop_dict['__dst_id'] + del prop_dict['__src_id'] - q_params = {'src': { 'id': n_src} , - 'dst': { 'id': n_dst} , + q_params = {'src': { 'id': src_id} , + 'dst': { 'id': dst_id} , 'link_attr' : input_to_DB_property_map(prop_dict)} ret.append((q, q_params)) @@ -239,8 +239,8 @@ def meta_attr_list_to_meta_attr_map(e_set, meta_attr='__label_set'): return ret def __type_check_link(link): - assert link.has_key('__src') - assert link.has_key('__dst') + assert link.has_key('__src_id') + assert link.has_key('__dst_id') def __type_check_link_or_node_map(x_map): for k, v in x_map.iteritems(): # do some type sanity checking diff --git a/src-py/rhizi_api.py b/src-py/rhizi_api.py index 8d787a5c..ef904a37 100644 --- a/src-py/rhizi_api.py +++ b/src-py/rhizi_api.py @@ -127,8 +127,8 @@ def load_link_set_by_link_ptr_set(): l_ptr_set = [] for lptr_dict in l_ptr_set_raw: - src_id = lptr_dict.get('__src') - dst_id = lptr_dict.get('__dst') + src_id = lptr_dict.get('__src_id') + dst_id = lptr_dict.get('__dst_id') l_ptr_set += [Link.Link_Ptr(src_id=src_id, dst_id=dst_id) ] return l_ptr_set -- cgit v1.3.1