Skip to content

Add new --edge-display-property parameter to Gremlin magic for specifying edge labels #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/graph_notebook/magics/graph_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@ def gremlin(self, line, cell, local_ns: dict = None):
help='Property used to group nodes (e.g. code, T.region) default is T.label')
parser.add_argument('-d', '--display-property', type=str, default='T.label',
help='Property to display the value of on each node, default is T.label')
parser.add_argument('-de', '--edge-display-property', type=str, default='T.label',
help='Property to display the value of on each edge, default is T.label')
parser.add_argument('-l', '--label-max-length', type=int, default=10,
help='Specifies max length of vertex label, in characters. Default is 10')
parser.add_argument('--store-to', type=str, default='', help='store query result to this variable')
Expand Down Expand Up @@ -393,9 +395,11 @@ def gremlin(self, line, cell, local_ns: dict = None):
try:
logger.debug(f'groupby: {args.group_by}')
logger.debug(f'display_property: {args.display_property}')
logger.debug(f'edge_display_property: {args.edge_display_property}')
logger.debug(f'label_max_length: {args.label_max_length}')
logger.debug(f'ignore_groups: {args.ignore_groups}')
gn = GremlinNetwork(group_by_property=args.group_by, display_property=args.display_property,
edge_display_property=args.edge_display_property,
label_max_length=args.label_max_length, ignore_groups=args.ignore_groups)

if args.path_pattern == '':
Expand Down
37 changes: 32 additions & 5 deletions src/graph_notebook/network/gremlin/GremlinNetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ class GremlinNetwork(EventfulNetwork):
"""

def __init__(self, graph: MultiDiGraph = None, callbacks=None, label_max_length=DEFAULT_LABEL_MAX_LENGTH,
group_by_property=T_LABEL, display_property=T_LABEL, ignore_groups=False):
group_by_property=T_LABEL, display_property=T_LABEL, edge_display_property=T_LABEL,
ignore_groups=False):
if graph is None:
graph = MultiDiGraph()
if label_max_length < 3:
Expand All @@ -113,6 +114,10 @@ def __init__(self, graph: MultiDiGraph = None, callbacks=None, label_max_length=
self.display_property = json.loads(display_property)
except ValueError:
self.display_property = display_property
try:
self.edge_display_property = json.loads(edge_display_property)
except ValueError:
self.edge_display_property = edge_display_property
self.ignore_groups = ignore_groups
super().__init__(graph, callbacks)

Expand Down Expand Up @@ -390,18 +395,40 @@ def add_path_edge(self, edge, from_id='', to_id='', data=None):
from_id = from_id if from_id != '' else edge.outV.id
to_id = to_id if to_id != '' else edge.inV.id
data['properties'] = {'id': edge.id, 'label': edge.label, 'outV': str(edge.outV), 'inV': str(edge.inV)}
self.add_edge(from_id, to_id, edge.id, edge.label, data)
if isinstance(self.edge_display_property, dict):
try:
display_label = data['properties'][self.edge_display_property[edge.label]]
except KeyError:
display_label = edge.label
else:
if self.edge_display_property == T_LABEL:
display_label = edge.label
else:
try:
display_label = data['properties'][self.edge_display_property]
except KeyError:
display_label = edge.label
self.add_edge(from_id, to_id, edge.id, display_label, data)
elif type(edge) is dict:
properties = {}
edge_id = ''
edge_label = ''
if T.label in edge.keys():
edge_label = str(edge[T.label])
for k in edge:
if str(k) == T_LABEL:
edge_label = str(edge[k])
elif str(k) == T_ID:
if str(k) == T_ID:
edge_id = str(edge[k])

properties[k] = edge[k]
if self.edge_display_property is not T_LABEL:
if isinstance(self.edge_display_property, dict):
try:
if str(k) == self.edge_display_property[edge_label]:
edge_label = str(edge[k])
except KeyError:
continue
elif str(k) == self.edge_display_property:
edge_label = str(edge[k])
data['properties'] = properties
self.add_edge(from_id, to_id, edge_id, edge_label, data)
else:
Expand Down
Loading