def setUp(cls): cls.VALUE_KEY = "@display_value" cls.RESOURCE_KEY = "resource" cls.DISPLAY_DESCRIPTION_KEY = "displaydescription" cls.DISPLAY_NAME_KEY = "displayname" cls.GRAPH_ID_KEY = "graph_id" cls.LEGACY_ID_KEY = "legacyid" cls.MAP_POPUP_KEY = "map_popup" cls.RESOURCE_INSTANCE_ID_KEY = "resourceinstanceid" cls.nodegroup = models.NodeGroup() cls.grouping_node = models.Node(datatype="semantic", name="Test Node Grouping", nodegroup=cls.nodegroup) cls.string_node = models.Node(datatype="string", name="Test Node") cls.grouping_tile = models.TileModel(data={}, nodegroup_id=str( cls.grouping_node.pk)) cls.string_tile = models.TileModel( data={str(cls.string_node.pk): "value_1"}, nodegroup_id=str(cls.string_node.pk)) cls.grouping_node.nodegroupid = cls.grouping_node.nodeid cls.string_node.nodegroupid = cls.string_node.nodeid # let's mock Resource since it's minimally used # and complex to get `displayname` cls.test_resource = mock.Mock(displayname="Test Resource", tiles=[])
def add_node(self, node): """ Adds a node to this graph Arguments: node -- a dictionary representing a Node instance or an actual models.Node instance """ if not isinstance(node, models.Node): nodeobj = node.copy() node = models.Node() node.nodeid = nodeobj.get('nodeid', None) node.name = nodeobj.get('name', '') node.description = nodeobj.get('description', '') node.istopnode = nodeobj.get('istopnode', '') node.ontologyclass = nodeobj.get('ontologyclass', '') node.datatype = nodeobj.get('datatype', '') node.nodegroup_id = nodeobj.get('nodegroupid', '') node.branchmetadata_id = nodeobj.get('branchmetadataid', '') if node.nodegroup_id != None and node.nodegroup_id != '': node.nodegroup = models.NodeGroup(pk=node.nodegroup_id, cardinality=nodeobj.get( 'cardinality', '')) if node.pk == None: node.pk = uuid.uuid1() if node.nodegroup != None: self.nodegroups[node.nodegroup.pk] = node.nodegroup self.nodes[node.pk] = node return node
def traverse_tree(tree, current_nodegroup=None): if tree['node'].nodegroup == None: tree['node'].nodegroup = current_nodegroup else: current_nodegroup = models.NodeGroup( pk=tree['node'].nodegroup_id, parentnodegroup=current_nodegroup) for child in tree['children']: traverse_tree(child, current_nodegroup) return tree
def copy(self): """ returns an unsaved copy of self """ new_nodegroups = {} copy_of_self = deepcopy(self) # returns a list of node ids sorted by nodes that are collector nodes first and then others last node_ids = sorted( copy_of_self.nodes, key=lambda node_id: copy_of_self.nodes[node_id].is_collector, reverse=True) copy_of_self.pk = uuid.uuid1() for node_id in node_ids: node = copy_of_self.nodes[node_id] if node == self.root: copy_of_self.root = node node.graph = copy_of_self is_collector = node.is_collector node.pk = uuid.uuid1() if is_collector: old_nodegroup_id = node.nodegroup_id node.nodegroup = models.NodeGroup(pk=node.pk) for card in copy_of_self.cards.itervalues(): if str(card.nodegroup_id) == str(old_nodegroup_id): card.pk = uuid.uuid1() card.nodegroup = node.nodegroup else: node.nodegroup = None copy_of_self.populate_null_nodegroups() copy_of_self.nodes = { node.pk: node for node_id, node in copy_of_self.nodes.iteritems() } for edge_id, edge in copy_of_self.edges.iteritems(): edge.pk = uuid.uuid1() edge.graph = copy_of_self edge.domainnode_id = edge.domainnode.pk edge.rangenode_id = edge.rangenode.pk copy_of_self.edges = { edge.pk: edge for edge_id, edge in copy_of_self.edges.iteritems() } return copy_of_self
def setUp(cls): cls.nodegroup = models.NodeGroup() cls.grouping_node = models.Node(datatype="semantic", name="Test Node Grouping", nodegroup=cls.nodegroup) cls.string_node = models.Node(datatype="string", name="Test Node") cls.grouping_tile = models.TileModel(data={}, nodegroup_id=str(cls.grouping_node.pk)) cls.string_tile = models.TileModel(data={str(cls.string_node.pk): "value_1"}, nodegroup_id=str(cls.string_node.pk)) cls.grouping_node.nodegroupid = cls.grouping_node.nodeid cls.string_node.nodegroupid = cls.string_node.nodeid # let's mock Resource since it's minimally used # and complex to get `displayname` cls.test_resource = mock.Mock(displayname="Test Resource", tiles=[])
def get_or_create_nodegroup(self, nodegroupid): """ get a nodegroup from an id by first looking through the nodes and cards associated with this graph. if not found then get the nodegroup instance from the database, otherwise return a new instance of a nodegroup Keyword Arguments nodegroupid -- return a nodegroup with this id """ for nodegroup in self.get_nodegroups(): if str(nodegroup.nodegroupid) == str(nodegroupid): return nodegroup try: return models.NodeGroup.objects.get(pk=nodegroupid) except models.NodeGroup.DoesNotExist: return models.NodeGroup(pk=nodegroupid)
def copy(self): """ returns an unsaved copy of self """ nodegroup_map = {} copy_of_self = deepcopy(self) # returns a list of node ids sorted by nodes that are collector nodes first and then others last node_ids = sorted( copy_of_self.nodes, key=lambda node_id: copy_of_self.nodes[node_id].is_collector, reverse=True) copy_of_self.pk = uuid.uuid1() node_map = {} card_map = {} for node_id in node_ids: node = copy_of_self.nodes[node_id] if node == self.root: copy_of_self.root = node node.graph = copy_of_self is_collector = node.is_collector node.pk = uuid.uuid1() node_map[node_id] = node.pk if is_collector: old_nodegroup_id = node.nodegroup_id node.nodegroup = models.NodeGroup( pk=node.pk, cardinality=node.nodegroup.cardinality) if old_nodegroup_id not in nodegroup_map: nodegroup_map[old_nodegroup_id] = node.nodegroup_id for card in copy_of_self.cards.itervalues(): if str(card.nodegroup_id) == str(old_nodegroup_id): new_id = uuid.uuid1() card_map[card.pk] = new_id card.pk = new_id card.nodegroup = node.nodegroup card.graph = copy_of_self else: node.nodegroup = None for widget in copy_of_self.widgets.itervalues(): widget.pk = uuid.uuid1() widget.node_id = node_map[widget.node_id] widget.card_id = card_map[widget.card_id] copy_of_self.populate_null_nodegroups() copy_of_self.nodes = { node.pk: node for node_id, node in copy_of_self.nodes.iteritems() } for edge_id, edge in copy_of_self.edges.iteritems(): edge.pk = uuid.uuid1() edge.graph = copy_of_self edge.domainnode_id = edge.domainnode.pk edge.rangenode_id = edge.rangenode.pk copy_of_self.edges = { edge.pk: edge for edge_id, edge in copy_of_self.edges.iteritems() } return { 'copy': copy_of_self, 'cards': card_map, 'nodes': node_map, 'nodegroups': nodegroup_map }