Skip to content

Commit

Permalink
Merge pull request #29 from Accenture/develop
Browse files Browse the repository at this point in the history
feat: support var length relationship with label
  • Loading branch information
Roei-Levi authored Jul 2, 2023
2 parents f9ce25f + dbd2de2 commit 63a7c44
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 7 deletions.
22 changes: 18 additions & 4 deletions src/cymple/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,13 @@ def related_from(self, label: str = None, ref_name: str = None, properties: dict
"""
return RelationAvailable(self.query + self._directed_relation('backward', label, ref_name, properties, **kwargs))

def related_variable_len(self, min_hops: int = -1, max_hops: int = -1):
def related_variable_len(self, label: str = None, ref_name: str = None, min_hops: int = -1, max_hops: int = -1):
"""Concatenate a uni-directional graph Relationship, with a variable path length.
:param label: The relationship label (type) in the DB, defaults to None
:type label: str
:param ref_name: A reference name to be used later in the rest of the query, defaults to None
:type ref_name: str
:param min_hops: The minimal desired number of hops (set -1 for maximum boundary only), defaults to -1
:type min_hops: int
:param max_hops: The maximal desired number of hops (set -1 for minimal boundary only), defaults to -1
Expand All @@ -426,11 +430,14 @@ def related_variable_len(self, min_hops: int = -1, max_hops: int = -1):
min_hops_str = '' if min_hops == -1 else str(min_hops)
max_hops_str = '' if max_hops == -1 else str(max_hops)

relation_type = '' if label is None else f': {label}'
relation_ref_name = '' if ref_name is None else f'{ref_name}'

relation_length = '*' if min_hops == -1 and max_hops == - \
1 else (f'*{min_hops_str}'if min_hops == max_hops else f'*{min_hops_str}..{max_hops_str}')

if relation_length:
realtion_str = f'[{relation_length}]'
realtion_str = f'[{relation_ref_name}{relation_type}{relation_length}]'
else:
realtion_str = ''

Expand Down Expand Up @@ -528,9 +535,13 @@ def related_from(self, label: str = None, ref_name: str = None, properties: dict
"""
return RelationAvailable(self.query + self._directed_relation('backward', label, ref_name, properties, **kwargs))

def related_variable_len(self, min_hops: int = -1, max_hops: int = -1):
def related_variable_len(self, label: str = None, ref_name: str = None, min_hops: int = -1, max_hops: int = -1):
"""Concatenate a uni-directional graph Relationship, with a variable path length.
:param label: The relationship label (type) in the DB, defaults to None
:type label: str
:param ref_name: A reference name to be used later in the rest of the query, defaults to None
:type ref_name: str
:param min_hops: The minimal desired number of hops (set -1 for maximum boundary only), defaults to -1
:type min_hops: int
:param max_hops: The maximal desired number of hops (set -1 for minimal boundary only), defaults to -1
Expand All @@ -542,11 +553,14 @@ def related_variable_len(self, min_hops: int = -1, max_hops: int = -1):
min_hops_str = '' if min_hops == -1 else str(min_hops)
max_hops_str = '' if max_hops == -1 else str(max_hops)

relation_type = '' if label is None else f': {label}'
relation_ref_name = '' if ref_name is None else f'{ref_name}'

relation_length = '*' if min_hops == -1 and max_hops == - \
1 else (f'*{min_hops_str}'if min_hops == max_hops else f'*{min_hops_str}..{max_hops_str}')

if relation_length:
realtion_str = f'[{relation_length}]'
realtion_str = f'[{relation_ref_name}{relation_type}{relation_length}]'
else:
realtion_str = ''

Expand Down
10 changes: 10 additions & 0 deletions src/cymple/internal/declarations/relation.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@
"name": "related_variable_len",
"docstring_summary": "Concatenate a uni-directional graph Relationship, with a variable path length.",
"args": {
"label": {
"type": "str",
"default": "None",
"description": "The relationship label (type) in the DB"
},
"ref_name": {
"type": "str",
"default": "None",
"description": "A reference name to be used later in the rest of the query"
},
"min_hops": {
"type": "int",
"description": "The minimal desired number of hops (set -1 for maximum boundary only)",
Expand Down
10 changes: 10 additions & 0 deletions src/cymple/internal/declarations/relation_after_merge.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@
"name": "related_variable_len",
"docstring_summary": "Concatenate a uni-directional graph Relationship, with a variable path length.",
"args": {
"label": {
"type": "str",
"default": "None",
"description": "The relationship label (type) in the DB"
},
"ref_name": {
"type": "str",
"default": "None",
"description": "A reference name to be used later in the rest of the query"
},
"min_hops": {
"type": "int",
"description": "The minimal desired number of hops (set -1 for maximum boundary only)",
Expand Down
7 changes: 5 additions & 2 deletions src/cymple/internal/overloads/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ def related_from(self, label: str, ref_name: str = None, properties: str = {}, *
return RelationAvailable(self.query + self._directed_relation('backward', label, ref_name, properties, **kwargs))


def related_variable_len(self, min_hops: int = -1, max_hops: int = -1):
def related_variable_len(self, label: str, ref_name: str = None, min_hops: int = -1, max_hops: int = -1):
min_hops_str = '' if min_hops == -1 else str(min_hops)
max_hops_str = '' if max_hops == -1 else str(max_hops)

relation_type = '' if label is None else f': {label}'
relation_ref_name = '' if ref_name is None else f'{ref_name}'

relation_length = '*' if min_hops == -1 and max_hops == -1 else (f'*{min_hops_str}'if min_hops == max_hops else f'*{min_hops_str}..{max_hops_str}')

if relation_length:
realtion_str = f'[{relation_length}]'
realtion_str = f'[{relation_ref_name}{relation_type}{relation_length}]'
else:
realtion_str = ''

Expand Down
2 changes: 1 addition & 1 deletion src/cymple/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Version information for Cymple"""

__version__: str = "0.8.2"
__version__: str = "0.9.0"
2 changes: 2 additions & 0 deletions tests/unit/test_clauses.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
'RELATION (unidirectional)': qb.reset().match().node().related().node(),
'RELATION (variable length)': qb.reset().match().node().related_variable_len(min_hops=1, max_hops=2).node(),
'RELATION (variable length, empty)': qb.reset().match().node().related_variable_len().node(),
'RELATION (variable length, with label)': qb.reset().match().node().related_variable_len(label='Relation', ref_name='rel', min_hops=1, max_hops=2).node(),
'RETURN (literal)': qb.reset().match().node(ref_name='n').return_literal('n'),
'RETURN (mapping)': qb.reset().match().node(ref_name='n').return_mapping(('n.name', 'name')),
'RETURN (mapping, list)': qb.reset().match().node(ref_name='n').return_mapping([('n.name', 'name'), ('n.age', 'age')]),
Expand Down Expand Up @@ -79,6 +80,7 @@
'RELATION (unidirectional)': 'MATCH ()--()',
'RELATION (variable length)': 'MATCH ()-[*1..2]-()',
'RELATION (variable length, empty)': 'MATCH ()-[*]-()',
'RELATION (variable length, with label)': 'MATCH ()-[rel: Relation*1..2]-()',
'RETURN (literal)': 'MATCH (n) RETURN n',
'RETURN (mapping)': 'MATCH (n) RETURN n.name as name',
'RETURN (mapping, list)': 'MATCH (n) RETURN n.name as name, n.age as age',
Expand Down

0 comments on commit 63a7c44

Please sign in to comment.