in src/graph_notebook/network/gremlin/GremlinNetwork.py [0:0]
def add_vertex(self, v, path_index: int = -1):
"""
Adds a vertex to the network. If v is of :type Vertex, we will gather its id and label.
If v comes from a valueMap, it will be a dict, with the keys T.label and T.ID being present in
the dict for gathering the label and id, respectively.
:param v: The vertex taken from a path traversal object.
:param path_index: Position of the element in the path traversal order
"""
depth_group = "__DEPTH-" + str(path_index) + "__"
node_id = ''
if type(v) is Vertex:
if isinstance(v.id, uuid.UUID):
node_id = str(v.id)
else:
node_id = v.id
title = v.label
label_full = ''
tooltip_display_is_set = True
using_custom_tooltip = False
if self.tooltip_property and self.tooltip_property != self.display_property:
tooltip_display_is_set = False
using_custom_tooltip = True
vertex_dict = v.__dict__
if not isinstance(self.group_by_property, dict): # Handle string format group_by
if str(self.group_by_property) == DEFAULT_RAW_GRP_KEY:
group = str(v)
elif str(self.group_by_property) in [T_LABEL, 'label']: # this handles if it's just a string
# This sets the group key to the label if either "label" is passed in or
# T.label is set in order to handle the default case of grouping by label
# when no explicit key is specified
group = v.label
elif str(self.group_by_property) in [T_ID, 'id', '~id']:
group = v.id
elif self.group_by_property == DEPTH_GRP_KEY:
group = depth_group
else:
group = DEFAULT_GRP
else: # handle dict format group_by
try:
if str(v.label) in self.group_by_property:
if self.group_by_property[str(v.label)] == DEFAULT_RAW_GRP_KEY:
group = str(v)
elif self.group_by_property[str(v.label)] in [T_LABEL, 'label']:
group = v.label
elif self.group_by_property[str(v.label)] in [T_ID, 'id', '~id']:
group = v.id
elif self.group_by_property[str(v.label)] == DEPTH_GRP_KEY:
group = depth_group
else:
group = vertex_dict[self.group_by_property[str(v.label)]]
else:
group = DEFAULT_GRP
except KeyError:
group = DEFAULT_GRP
label = title if len(title) <= self.label_max_length else title[:self.label_max_length - 3] + '...'
if self.display_property:
label_property_raw_value = self.get_explicit_vertex_property_value(node_id, title,
self.display_property)
if label_property_raw_value:
label_full, label = self.strip_and_truncate_label_and_title(label_property_raw_value,
self.label_max_length)
if not using_custom_tooltip:
title = label_full
if using_custom_tooltip:
tooltip_property_raw_value = self.get_explicit_vertex_property_value(node_id, title,
self.tooltip_property)
if tooltip_property_raw_value:
title, label_plc = self.strip_and_truncate_label_and_title(tooltip_property_raw_value,
self.label_max_length)
tooltip_display_is_set = True
if not tooltip_display_is_set and label_full:
title = label_full
data = {'label': str(label).strip("[]'"), 'title': title, 'group': group,
'properties': {'id': node_id, 'label': label_full}}
elif type(v) is dict:
properties = {}
title = ''
label = ''
label_full = ''
group = ''
display_is_set = False
using_custom_tooltip = False
tooltip_display_is_set = True
group_is_set = False
if self.tooltip_property and self.tooltip_property != self.display_property:
using_custom_tooltip = True
tooltip_display_is_set = False
# Before looping though properties, we first search for T.label in vertex dict, then set title = T.label
# Otherwise, we will hit KeyError if we don't iterate through T.label first to set the title
# Since it is needed for checking for the vertex label's desired grouping behavior in group_by_property
if T.label in v.keys() or 'label' in v.keys():
label_key = T.label if T.label in v.keys() else 'label'
label_raw = v[label_key]
title_plc = str(label_raw)
title, label = self.strip_and_truncate_label_and_title(title_plc, self.label_max_length)
else:
label_raw = ''
title_plc = ''
group = DEFAULT_GRP
if str(self.group_by_property) == DEFAULT_RAW_GRP_KEY:
group = str(v)
group_is_set = True
for k in v:
if str(k) in [T_ID, 'id', '~id']:
node_id = str(v[k])
if isinstance(v[k], dict):
properties[k] = str(v[k])
elif isinstance(v[k], list):
copy_val = v[k]
for i, subvalue in enumerate(copy_val):
if isinstance(subvalue, Decimal):
copy_val[i] = float(subvalue)
properties[k] = copy_val
elif isinstance(v[k], Decimal):
properties[k] = float(v[k])
elif isinstance(v[k], uuid.UUID):
properties[k] = str(v[k])
else:
properties[k] = v[k]
if not group_is_set:
if isinstance(self.group_by_property, dict):
try:
if DEPTH_GRP_KEY == self.group_by_property[title_plc]:
group = depth_group
group_is_set = True
elif DEFAULT_RAW_GRP_KEY == self.group_by_property[title_plc]:
group = str(v)
group_is_set = True
elif str(k) == self.group_by_property[title_plc]:
group = str(v[k])
group_is_set = True
except KeyError:
pass
else:
if DEPTH_GRP_KEY == self.group_by_property:
group = depth_group
group_is_set = True
elif str(k) == self.group_by_property:
group = str(v[k])
group_is_set = True
if not display_is_set:
label_property_raw_value = self.get_dict_element_property_value(v, k, label_raw,
self.display_property)
if label_property_raw_value:
label_full, label = self.strip_and_truncate_label_and_title(label_property_raw_value,
self.label_max_length)
if not using_custom_tooltip:
title = label_full
display_is_set = True
if not tooltip_display_is_set:
tooltip_property_raw_value = self.get_dict_element_property_value(v, k, label_raw,
self.tooltip_property)
if tooltip_property_raw_value:
title, label_plc = self.strip_and_truncate_label_and_title(tooltip_property_raw_value,
self.label_max_length)
tooltip_display_is_set = True
if not tooltip_display_is_set and label_full:
title = label_full
if not group and not group_is_set:
group = DEFAULT_GRP
# handle when there is no id in a node. In this case, we will generate one which
# is consistently regenerated so that duplicate dicts will be reduced to the same vertex.
if node_id == '':
node_id = f'{generate_id_from_dict(v)}'
# similarly, if there is no label, we must generate one. This will be a concatenation
# of all values in the dict
if title == '':
for key in v:
title += str(v[key])
if label == '':
label = title if len(title) <= self.label_max_length else title[:self.label_max_length - 3] + '...'
data = {'properties': properties, 'label': label, 'title': title, 'group': group}
else:
node_id = str(v)
title = str(v)
if self.group_by_property == DEPTH_GRP_KEY:
group = depth_group
elif self.group_by_property == DEFAULT_RAW_GRP_KEY:
group = str(v)
else:
group = DEFAULT_GRP
label = title if len(title) <= self.label_max_length else title[:self.label_max_length - 3] + '...'
data = {'title': title, 'label': label, 'group': group}
if self.ignore_groups:
data['group'] = DEFAULT_GRP
self.add_node(node_id, data)