def test_x(self): n1 = ElementMaker.create_nodes_aspect_element(1, 'node 1', 'N1') n2 = ElementMaker.create_nodes_aspect_element(2, 'node 2', 'N2') e = ElementMaker.create_edges_aspect_element(3, 1, 2, '1->2') nea = ElementMaker.create_network_attributes_aspect_element( 1200, 'size', 12.3, CxConstants.DATA_TYPE_DOUBLE) noa = ElementMaker.create_node_attributes_aspect_element( 1200, 1, 'weight', '12.0', CxConstants.DATA_TYPE_DOUBLE) eda = ElementMaker.create_edge_attributes_aspect_element( 1200, 3, 'length', 303.409883, CxConstants.DATA_TYPE_DOUBLE) neal = ElementMaker.create_network_attributes_aspect_element( 1200, 'used', ['true', 'false'], CxConstants.DATA_TYPE_LIST_OF_BOOLEAN) noal = ElementMaker.create_node_attributes_aspect_element( 1200, 1, 'weights', ['1.1', '2.2'], CxConstants.DATA_TYPE_LIST_OF_DOUBLE) edal = ElementMaker.create_edge_attributes_aspect_element( 1200, 3, 'lengths', ['23.3', '13.34'], CxConstants.DATA_TYPE_LIST_OF_DOUBLE) ha = ElementMaker.create_hidden_attributes_aspect_element( 1200, 'size', 12.3, CxConstants.DATA_TYPE_DOUBLE) hal = ElementMaker.create_hidden_attributes_aspect_element( 1200, 'used', ['true', 'false'], CxConstants.DATA_TYPE_LIST_OF_BOOLEAN) c1 = ElementMaker.create_cartesian_layout_element( 1, 2000, 100.11, 200.22, 3.33) c2 = ElementMaker.create_cartesian_layout_element( 2, 2000, -100.11, -200.22) sn = ElementMaker.create_sub_networks_aspect_element(1200, [1, 2], [3]) v = ElementMaker.create_views_aspect_element(2000, 1200) g = ElementMaker.create_groups_aspect_element(555, 1200, 'group 1', [1, 2], [3], [3]) nr1 = ElementMaker.create_network_relations_aspect_element( 1200, 3000, 'subnetwork', 'child 1200') nr2 = ElementMaker.create_network_relations_aspect_element( 2000, 1200, 'view', 'view 2000') tc = ElementMaker.create_table_column_aspect_element( 1200, 'node table', 'weight', CxConstants.DATA_TYPE_INTEGER) properties = { "NODE_BORDER_PAINT": "#CCCCCC", "NODE_BORDER_STROKE": "SOLID", "NODE_BORDER_TRANSPARENCY": "255" } dependencies = { "nodeCustomGraphicsSizeSync": "true", "nodeSizeLocked": "false" } mappings = { "NODE_LABEL": { "type": "PASSTHROUGH", "definition": "COL=name,T=string" } } vp = ElementMaker.create_visual_properties_aspect_element( "nodes:default", 1, 1200, properties, dependencies, mappings) prmd = ElementMaker.create_pre_metadata_element( 'nodes', 1, 'v2.2', 1234567, [], 2) pomd1 = ElementMaker.create_post_metadata_element('nodes', 2) pomd2 = ElementMaker.create_post_metadata_element('edges', 1) fo = io.StringIO('') w = CxWriter(fo) w.add_pre_meta_data([prmd]) w.start() w.start_aspect_fragment(CxConstants.NODES) w.write_aspect_element(n1) w.write_aspect_element(n2) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.EDGES) w.write_aspect_element(e) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.CARTESIAN_LAYOUT) w.write_aspect_element(c1) w.write_aspect_element(c2) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.NETWORK_ATTRIBUTES) w.write_aspect_element(nea) w.write_aspect_element(neal) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.NODE_ATTRIBUTES) w.write_aspect_element(noa) w.write_aspect_element(noal) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.EDGE_ATTRIBUTES) w.write_aspect_element(eda) w.write_aspect_element(edal) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.HIDDEN_ATTRIBUTES) w.write_aspect_element(ha) w.write_aspect_element(hal) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.SUB_NETWORKS) w.write_aspect_element(sn) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.VIEWS) w.write_aspect_element(v) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.GROUPS) w.write_aspect_element(g) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.NETWORK_RELATIONS) w.write_aspect_element(nr1) w.write_aspect_element(nr2) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.TABLE_COLUMN) w.write_aspect_element(tc) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.VISUAL_PROPERTIES) w.write_aspect_element(vp) w.end_aspect_fragment() w.add_post_meta_data([pomd1, pomd2]) w.end(True, "no error") json_str = fo.getvalue() print(json_str) # READING fi = io.StringIO(json_str) cx_reader = CxReader(fi) self.assertEquals(len(cx_reader.get_pre_meta_data()), 1) cx = cx_reader.parse_as_dictionary() self.assertEquals(cx_reader.get_error_msg(), "no error") self.assertEquals(cx_reader.get_is_success(), True) self.assertEquals(len(cx_reader.get_post_meta_data()), 2) self.assertEquals(len(cx[CxConstants.NODES]), 2) self.assertEquals(len(cx[CxConstants.EDGES]), 1) self.assertEquals(len(cx[CxConstants.CARTESIAN_LAYOUT]), 2) self.assertEquals(len(cx[CxConstants.NODE_ATTRIBUTES]), 2) self.assertEquals(len(cx[CxConstants.EDGE_ATTRIBUTES]), 2) self.assertEquals(len(cx[CxConstants.NETWORK_ATTRIBUTES]), 2) self.assertEquals(len(cx[CxConstants.HIDDEN_ATTRIBUTES]), 2) self.assertEquals(len(cx[CxConstants.SUB_NETWORKS]), 1) self.assertEquals(len(cx[CxConstants.VIEWS]), 1) self.assertEquals(len(cx[CxConstants.GROUPS]), 1) self.assertEquals(len(cx[CxConstants.NETWORK_RELATIONS]), 2) self.assertEquals(len(cx[CxConstants.TABLE_COLUMN]), 1) self.assertEquals(len(cx[CxConstants.VISUAL_PROPERTIES]), 1) sn2 = cx[CxConstants.SUB_NETWORKS][0] self.assertEquals(sn2.get_name(), CxConstants.SUB_NETWORKS) d = sn2.get_data() self.assertEquals(d['@id'], 1200) self.assertEquals(d['nodes'][0], 1) self.assertEquals(d['nodes'][1], 2) self.assertEquals(d['edges'][0], 3) v2 = cx[CxConstants.VIEWS][0] self.assertEquals(v2.get_name(), CxConstants.VIEWS) d = v2.get_data() self.assertEquals(d['@id'], 2000) self.assertEquals(d['s'], 1200) g2 = cx[CxConstants.GROUPS][0] self.assertEquals(g2.get_name(), CxConstants.GROUPS) d = g2.get_data() self.assertEquals(d['@id'], 555) self.assertEquals(d['name'], 'group 1') self.assertEquals(d['view'], 1200) self.assertEquals(len(d['internal_edges']), 1) self.assertEquals(len(d['external_edges']), 1) self.assertEquals(len(d['nodes']), 2) self.assertEquals(d['internal_edges'][0], 3) self.assertEquals(d['nodes'][0], 1) self.assertEquals(d['nodes'][1], 2) nr2 = cx[CxConstants.NETWORK_RELATIONS][0] self.assertEquals(nr2.get_name(), CxConstants.NETWORK_RELATIONS) d = nr2.get_data() self.assertEquals(d['c'], 1200) self.assertEquals(d['p'], 3000) self.assertEquals(d['r'], 'subnetwork') self.assertEquals(d['name'], 'child 1200')
print(e) print() print() # WRITING # ------- # Writing to a string pretending to be a file/stream fo = io.StringIO("") # Creating a CX writer w = CxWriter(fo) # Adding pre meta data w.add_pre_meta_data(cx_reader.get_pre_meta_data()) # Starting the json list w.start() # Writing various aspects CxUtil.write_aspect_fragment(w, cx[CxConstants.NODES]) CxUtil.write_aspect_fragment(w, cx[CxConstants.EDGES]) CxUtil.write_aspect_fragment(w, cx[CxConstants.CARTESIAN_LAYOUT]) CxUtil.write_aspect_fragment(w, cx[CxConstants.EDGE_ATTRIBUTES]) # Adding post meta data
class NdexCXHelper: def __init__(self, output_stream): self.__contexts = {} self.__citation_id_counter = 0 self.__support_id_counter = 0 self.__edge_id_counter = 0 self.__node_id_counter = 0 self.__update_time = -1 self.__current_name = None self.__cx_writer = CxWriter(output_stream) self.__aspect_names = ["@context", CxConstants.NODES, CxConstants.EDGES, CxConstants.NETWORK_ATTRIBUTES, CxConstants.NODE_ATTRIBUTES, CxConstants.EDGE_ATTRIBUTES, CxConstants.CARTESIAN_LAYOUT, "citations", "nodeCitations", "edgeCitations", "nodeSupports", "edgeSupports", "provenanceHistory", "supports"] def get_cx_writer(self): return self.__cx_writer def start(self): self.__current_name = None self.__update_time = int(round(time.time() * 1000)) self.__add_pre_metadata() self.__cx_writer.start() def end(self, success=True, error_msg=''): if self.__current_name is not None: self.__cx_writer.end_aspect_fragment() self.__current_name = None self.__add_post_metadata() self.__cx_writer.end(success, error_msg) def add_cx_context(self, prefix, uri): self.__contexts[prefix] = uri def emit_cx_context(self): self.__cx_writer.write_single_aspect_fragment( ElementMaker.create_ndex_context_element(self.__contexts)) self.__current_name = None def emit_cx_node(self, node_name): self.__node_id_counter += 1 self.__process_element(ElementMaker.create_nodes_aspect_element( self.__node_id_counter, node_name)) return self.__node_id_counter def emit_cx_edge(self, source_id, target_id, interaction): self.__edge_id_counter += 1 self.__process_element( ElementMaker.create_edges_aspect_element(self.__edge_id_counter, source_id, target_id, interaction)) return self.__edge_id_counter def emit_cx_node_attribute(self, node_id, name, value, data_type=None): self.__process_element( ElementMaker.create_node_attributes_aspect_element(None, node_id, name, value, data_type)) def emit_cx_edge_attribute(self, edge_id, name, value, data_type=None): self.__process_element( ElementMaker.create_edge_attributes_aspect_element(None, edge_id, name, value, data_type)) def emit_cx_network_attribute(self, sub_network_id, name, value, data_type=None): self.__process_element( ElementMaker.create_network_attributes_aspect_element(sub_network_id, name, value, data_type)) def emit_cx_hidden_attribute(self, sub_network_id, name, value, data_type=None): self.__process_element( ElementMaker.create_hidden_attributes_aspect_element(sub_network_id, name, value, data_type)) def emit_cx_cartesian_layout_element(self, node_id, view_id, x, y, z=None): self.__process_element( ElementMaker.create_cartesian_layout_element(node_id, view_id, x, y, z)) def emit_cx_sub_networks(self, sub_network_id, node_ids, edge_ids): self.__process_element( ElementMaker.create_sub_networks_aspect_element(sub_network_id, node_ids, edge_ids)) def emit_cx_views(self, view_id, sub_network_id): self.__process_element( ElementMaker.create_views_aspect_element(view_id, sub_network_id)) def emit_cx_visual_properties(self, properties_of, applies_to, view, properties, dependencies=None, mappings=None): self.__process_element( ElementMaker.create_visual_properties_aspect_element(properties_of, applies_to, view, properties, dependencies, mappings)) def emit_cx_table_column(self, sub_network, applies_to, name, data_type): self.__process_element( ElementMaker.create_table_column_aspect_element(sub_network, applies_to, name, data_type)) def emit_cx_network_relations(self, child, parent, relationship=None, name=None): self.__process_element(ElementMaker.create_network_relations_aspect_element(child, parent, relationship, name)) def emit_cx_groups(self, group_id, view_id, name, nodes, external_edges, internal_edges): self.__process_element(ElementMaker.create_groups_aspect_element(group_id, view_id, name, nodes, external_edges, internal_edges)) def emit_cx_citation(self, citation_type, title, contributors, identifier, description): self.__citation_id_counter += 1 self.__process_element( ElementMaker.create_ndex_citation_aspect_element(self.__citation_id_counter, citation_type, title, contributors, identifier, description)) return self.__citation_id_counter def emit_cx_support(self, cx_citation_id, text): self.__support_id_counter += 1 self.__process_element( ElementMaker.create_ndex_support_aspect_element(self.__support_id_counter, cx_citation_id, text)) return self.__support_id_counter def emit_cx_function_term(self, function_term): self.__process_element(ElementMaker.create_ndex_function_term_aspect_element(function_term)) def emit_cx_node_citation(self, node_id, citation_id): self.__process_element(ElementMaker.create_ndex_node_citation_aspect_element(node_id, citation_id)) def emit_cx_edge_citation(self, edge_id, citation_id): self.__process_element(ElementMaker.create_ndex_edge_citation_aspect_element(edge_id, citation_id)) def emit_cx_node_support(self, node_id, support_id): self.__process_element(ElementMaker.create_ndex_node_support_aspect_element(node_id, support_id)) def emit_cx_edge_support(self, edge_id, support_id): self.__process_element(ElementMaker.create_ndex_edge_support_aspect_element(edge_id, support_id)) def __add_pre_metadata(self): pre_meta_data = [] for aspect_name in self.__aspect_names: pre_meta_data.append(ElementMaker.create_pre_metadata_element(aspect_name, 1, '1.0', self.__update_time, [], 1)) self.__cx_writer.add_pre_meta_data(pre_meta_data) def __add_post_metadata(self): post_meta_data = [ ElementMaker.create_post_metadata_element('nodes', self.__node_id_counter), ElementMaker.create_post_metadata_element('edges', self.__edge_id_counter), ElementMaker.create_post_metadata_element('supports', self.__support_id_counter), ElementMaker.create_post_metadata_element('citations', self.__citation_id_counter) ] self.__cx_writer.add_post_meta_data(post_meta_data) def __process_element(self, element): aspect_name = element.get_name() if self.__current_name is None: self.__cx_writer.start_aspect_fragment(aspect_name) elif self.__current_name != aspect_name: self.__cx_writer.end_aspect_fragment() self.__cx_writer.start_aspect_fragment(aspect_name) self.__current_name = aspect_name self.__cx_writer.write_aspect_element(element)
if n2 not in nn_id: nn_id[n2] = node_count id_nn[node_count] = n2 node_count += 1 edges.append(ElementMaker.create_edges_aspect_element(edge_count, nn_id[n1], nn_id[n2], interaction)) edge_count += 1 # Writing CX: w = CxWriter(fo) w.set_pretty_formatting(True) id_count = node_count + edge_count + 1 w.add_pre_meta_data(ElementMaker.create_pre_metadata_element(CxConstants.NODES, ASPECT_CONSISTENCY_GROUP, ASPECT_VERSION, UPDATE_TIME, [], id_count, len(id_nn))) w.add_pre_meta_data(ElementMaker.create_pre_metadata_element(CxConstants.EDGES, ASPECT_CONSISTENCY_GROUP, ASPECT_VERSION, UPDATE_TIME, [], id_count, len(edges))) w.start() w.start_aspect_fragment(CxConstants.NODES) for node_id, nn in id_nn.items(): w.write_aspect_element(ElementMaker.create_nodes_aspect_element(node_id, nn)) w.end_aspect_fragment() w.write_aspect_fragment(edges) w.end() print('Aspect elements written: ') for name, count in w.get_aspect_element_counts().items():
def test_x(self): n1 = ElementMaker.create_nodes_aspect_element(1, 'node 1', 'N1') n2 = ElementMaker.create_nodes_aspect_element(2, 'node 2', 'N2') e = ElementMaker.create_edges_aspect_element(3, 1, 2, '1->2') nea = ElementMaker.create_network_attributes_aspect_element(1200, 'size', 12.3, CxConstants.DATA_TYPE_DOUBLE) noa = ElementMaker.create_node_attributes_aspect_element(1200, 1, 'weight', '12.0', CxConstants.DATA_TYPE_DOUBLE) eda = ElementMaker.create_edge_attributes_aspect_element(1200, 3, 'length', 303.409883, CxConstants.DATA_TYPE_DOUBLE) neal = ElementMaker.create_network_attributes_aspect_element(1200, 'used', ['true', 'false'], CxConstants.DATA_TYPE_LIST_OF_BOOLEAN) noal = ElementMaker.create_node_attributes_aspect_element(1200, 1, 'weights', ['1.1', '2.2'], CxConstants.DATA_TYPE_LIST_OF_DOUBLE) edal = ElementMaker.create_edge_attributes_aspect_element(1200, 3, 'lengths', ['23.3', '13.34'], CxConstants.DATA_TYPE_LIST_OF_DOUBLE) ha = ElementMaker.create_hidden_attributes_aspect_element(1200, 'size', 12.3, CxConstants.DATA_TYPE_DOUBLE) hal = ElementMaker.create_hidden_attributes_aspect_element(1200, 'used', ['true', 'false'], CxConstants.DATA_TYPE_LIST_OF_BOOLEAN) c1 = ElementMaker.create_cartesian_layout_element(1, 2000, 100.11, 200.22, 3.33) c2 = ElementMaker.create_cartesian_layout_element(2, 2000, -100.11, -200.22) sn = ElementMaker.create_sub_networks_aspect_element(1200, [1, 2], [3]) v = ElementMaker.create_views_aspect_element(2000, 1200) g = ElementMaker.create_groups_aspect_element(555, 1200, 'group 1', [1, 2], [3], [3]) nr1 = ElementMaker.create_network_relations_aspect_element(1200, 3000, 'subnetwork', 'child 1200') nr2 = ElementMaker.create_network_relations_aspect_element(2000, 1200, 'view', 'view 2000') tc = ElementMaker.create_table_column_aspect_element(1200, 'node table', 'weight', CxConstants.DATA_TYPE_INTEGER) properties = {"NODE_BORDER_PAINT": "#CCCCCC", "NODE_BORDER_STROKE": "SOLID", "NODE_BORDER_TRANSPARENCY": "255"} dependencies = {"nodeCustomGraphicsSizeSync": "true", "nodeSizeLocked": "false"} mappings = {"NODE_LABEL": { "type": "PASSTHROUGH", "definition": "COL=name,T=string"}} vp = ElementMaker.create_visual_properties_aspect_element("nodes:default", 1, 1200, properties, dependencies, mappings) prmd = ElementMaker.create_pre_metadata_element('nodes', 1, 'v2.2', 1234567, [], 2) pomd1 = ElementMaker.create_post_metadata_element('nodes', 2) pomd2 = ElementMaker.create_post_metadata_element('edges', 1) fo = io.StringIO('') w = CxWriter(fo) w.add_pre_meta_data([prmd]) w.start() w.start_aspect_fragment(CxConstants.NODES) w.write_aspect_element(n1) w.write_aspect_element(n2) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.EDGES) w.write_aspect_element(e) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.CARTESIAN_LAYOUT) w.write_aspect_element(c1) w.write_aspect_element(c2) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.NETWORK_ATTRIBUTES) w.write_aspect_element(nea) w.write_aspect_element(neal) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.NODE_ATTRIBUTES) w.write_aspect_element(noa) w.write_aspect_element(noal) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.EDGE_ATTRIBUTES) w.write_aspect_element(eda) w.write_aspect_element(edal) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.HIDDEN_ATTRIBUTES) w.write_aspect_element(ha) w.write_aspect_element(hal) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.SUB_NETWORKS) w.write_aspect_element(sn) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.VIEWS) w.write_aspect_element(v) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.GROUPS) w.write_aspect_element(g) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.NETWORK_RELATIONS) w.write_aspect_element(nr1) w.write_aspect_element(nr2) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.TABLE_COLUMN) w.write_aspect_element(tc) w.end_aspect_fragment() w.start_aspect_fragment(CxConstants.VISUAL_PROPERTIES) w.write_aspect_element(vp) w.end_aspect_fragment() w.add_post_meta_data([pomd1, pomd2]) w.end(True, "no error") json_str = fo.getvalue() print(json_str) # READING fi = io.StringIO(json_str) cx_reader = CxReader(fi) self.assertEquals(len(cx_reader.get_pre_meta_data()), 1) cx = cx_reader.parse_as_dictionary() self.assertEquals(cx_reader.get_error_msg(), "no error") self.assertEquals(cx_reader.get_is_success(), True) self.assertEquals(len(cx_reader.get_post_meta_data()), 2) self.assertEquals(len(cx[CxConstants.NODES]), 2) self.assertEquals(len(cx[CxConstants.EDGES]), 1) self.assertEquals(len(cx[CxConstants.CARTESIAN_LAYOUT]), 2) self.assertEquals(len(cx[CxConstants.NODE_ATTRIBUTES]), 2) self.assertEquals(len(cx[CxConstants.EDGE_ATTRIBUTES]), 2) self.assertEquals(len(cx[CxConstants.NETWORK_ATTRIBUTES]), 2) self.assertEquals(len(cx[CxConstants.HIDDEN_ATTRIBUTES]), 2) self.assertEquals(len(cx[CxConstants.SUB_NETWORKS]), 1) self.assertEquals(len(cx[CxConstants.VIEWS]), 1) self.assertEquals(len(cx[CxConstants.GROUPS]), 1) self.assertEquals(len(cx[CxConstants.NETWORK_RELATIONS]), 2) self.assertEquals(len(cx[CxConstants.TABLE_COLUMN]), 1) self.assertEquals(len(cx[CxConstants.VISUAL_PROPERTIES]), 1) sn2 = cx[CxConstants.SUB_NETWORKS][0] self.assertEquals(sn2.get_name(), CxConstants.SUB_NETWORKS) d = sn2.get_data() self.assertEquals(d['@id'], 1200) self.assertEquals(d['nodes'][0], 1) self.assertEquals(d['nodes'][1], 2) self.assertEquals(d['edges'][0], 3) v2 = cx[CxConstants.VIEWS][0] self.assertEquals(v2.get_name(), CxConstants.VIEWS) d = v2.get_data() self.assertEquals(d['@id'], 2000) self.assertEquals(d['s'], 1200) g2 = cx[CxConstants.GROUPS][0] self.assertEquals(g2.get_name(), CxConstants.GROUPS) d = g2.get_data() self.assertEquals(d['@id'], 555) self.assertEquals(d['name'], 'group 1') self.assertEquals(d['view'], 1200) self.assertEquals(len(d['internal_edges']), 1) self.assertEquals(len(d['external_edges']), 1) self.assertEquals(len(d['nodes']), 2) self.assertEquals(d['internal_edges'][0], 3) self.assertEquals(d['nodes'][0], 1) self.assertEquals(d['nodes'][1], 2) nr2 = cx[CxConstants.NETWORK_RELATIONS][0] self.assertEquals(nr2.get_name(), CxConstants.NETWORK_RELATIONS) d = nr2.get_data() self.assertEquals(d['c'], 1200) self.assertEquals(d['p'], 3000) self.assertEquals(d['r'], 'subnetwork') self.assertEquals(d['name'], 'child 1200')
fi = open(fi_name, 'r') fo = open(fo_name, 'w') print('Infile : ' + str(fi.name)) print('Outfile: ' + str(fo.name)) print() r = CxReader(fi) cx = r.parse_as_dictionary() w = CxWriter(fo) w.set_pretty_formatting(True) w.add_pre_meta_data(r.get_pre_meta_data()) w.add_post_meta_data(r.get_post_meta_data()) w.start() for name, fragment in cx.items(): w.write_aspect_fragment(fragment) w.end() print('Aspect elements written: ') for name, count in w.get_aspect_element_counts().items(): print(name + ': ' + str(count)) print() print('OK')
node_count += 1 edges.append( ElementMaker.create_edges_aspect_element( edge_count, nn_id[n1], nn_id[n2], interaction)) edge_count += 1 # Writing CX: w = CxWriter(fo) w.set_pretty_formatting(True) id_count = node_count + edge_count + 1 w.add_pre_meta_data( ElementMaker.create_pre_metadata_element(CxConstants.NODES, ASPECT_CONSISTENCY_GROUP, ASPECT_VERSION, UPDATE_TIME, [], id_count, len(id_nn))) w.add_pre_meta_data( ElementMaker.create_pre_metadata_element(CxConstants.EDGES, ASPECT_CONSISTENCY_GROUP, ASPECT_VERSION, UPDATE_TIME, [], id_count, len(edges))) w.start() w.start_aspect_fragment(CxConstants.NODES) for node_id, nn in id_nn.items(): w.write_aspect_element( ElementMaker.create_nodes_aspect_element(node_id, nn)) w.end_aspect_fragment() w.write_aspect_fragment(edges)
class NdexCXHelper: def __init__(self, output_stream): self.__contexts = {} self.__citation_id_counter = 0 self.__support_id_counter = 0 self.__edge_id_counter = 0 self.__node_id_counter = 0 self.__update_time = -1 self.__current_name = None self.__cx_writer = CxWriter(output_stream) self.__aspect_names = [ "@context", CxConstants.NODES, CxConstants.EDGES, CxConstants.NETWORK_ATTRIBUTES, CxConstants.NODE_ATTRIBUTES, CxConstants.EDGE_ATTRIBUTES, CxConstants.CARTESIAN_LAYOUT, "citations", "nodeCitations", "edgeCitations", "nodeSupports", "edgeSupports", "provenanceHistory", "supports" ] def get_cx_writer(self): return self.__cx_writer def start(self): self.__current_name = None self.__update_time = int(round(time.time() * 1000)) self.__add_pre_metadata() self.__cx_writer.start() def end(self, success=True, error_msg=''): if self.__current_name is not None: self.__cx_writer.end_aspect_fragment() self.__current_name = None self.__add_post_metadata() self.__cx_writer.end(success, error_msg) def add_cx_context(self, prefix, uri): self.__contexts[prefix] = uri def emit_cx_context(self): self.__cx_writer.write_single_aspect_fragment( ElementMaker.create_ndex_context_element(self.__contexts)) self.__current_name = None def emit_cx_node(self, node_name): self.__node_id_counter += 1 self.__process_element( ElementMaker.create_nodes_aspect_element(self.__node_id_counter, node_name)) return self.__node_id_counter def emit_cx_edge(self, source_id, target_id, interaction): self.__edge_id_counter += 1 self.__process_element( ElementMaker.create_edges_aspect_element(self.__edge_id_counter, source_id, target_id, interaction)) return self.__edge_id_counter def emit_cx_node_attribute(self, node_id, name, value, data_type=None): self.__process_element( ElementMaker.create_node_attributes_aspect_element( None, node_id, name, value, data_type)) def emit_cx_edge_attribute(self, edge_id, name, value, data_type=None): self.__process_element( ElementMaker.create_edge_attributes_aspect_element( None, edge_id, name, value, data_type)) def emit_cx_network_attribute(self, sub_network_id, name, value, data_type=None): self.__process_element( ElementMaker.create_network_attributes_aspect_element( sub_network_id, name, value, data_type)) def emit_cx_hidden_attribute(self, sub_network_id, name, value, data_type=None): self.__process_element( ElementMaker.create_hidden_attributes_aspect_element( sub_network_id, name, value, data_type)) def emit_cx_cartesian_layout_element(self, node_id, view_id, x, y, z=None): self.__process_element( ElementMaker.create_cartesian_layout_element( node_id, view_id, x, y, z)) def emit_cx_sub_networks(self, sub_network_id, node_ids, edge_ids): self.__process_element( ElementMaker.create_sub_networks_aspect_element( sub_network_id, node_ids, edge_ids)) def emit_cx_views(self, view_id, sub_network_id): self.__process_element( ElementMaker.create_views_aspect_element(view_id, sub_network_id)) def emit_cx_visual_properties(self, properties_of, applies_to, view, properties, dependencies=None, mappings=None): self.__process_element( ElementMaker.create_visual_properties_aspect_element( properties_of, applies_to, view, properties, dependencies, mappings)) def emit_cx_table_column(self, sub_network, applies_to, name, data_type): self.__process_element( ElementMaker.create_table_column_aspect_element( sub_network, applies_to, name, data_type)) def emit_cx_network_relations(self, child, parent, relationship=None, name=None): self.__process_element( ElementMaker.create_network_relations_aspect_element( child, parent, relationship, name)) def emit_cx_groups(self, group_id, view_id, name, nodes, external_edges, internal_edges): self.__process_element( ElementMaker.create_groups_aspect_element(group_id, view_id, name, nodes, external_edges, internal_edges)) def emit_cx_citation(self, citation_type, title, contributors, identifier, description): self.__citation_id_counter += 1 self.__process_element( ElementMaker.create_ndex_citation_aspect_element( self.__citation_id_counter, citation_type, title, contributors, identifier, description)) return self.__citation_id_counter def emit_cx_support(self, cx_citation_id, text): self.__support_id_counter += 1 self.__process_element( ElementMaker.create_ndex_support_aspect_element( self.__support_id_counter, cx_citation_id, text)) return self.__support_id_counter def emit_cx_function_term(self, function_term): self.__process_element( ElementMaker.create_ndex_function_term_aspect_element( function_term)) def emit_cx_node_citation(self, node_id, citation_id): self.__process_element( ElementMaker.create_ndex_node_citation_aspect_element( node_id, citation_id)) def emit_cx_edge_citation(self, edge_id, citation_id): self.__process_element( ElementMaker.create_ndex_edge_citation_aspect_element( edge_id, citation_id)) def emit_cx_node_support(self, node_id, support_id): self.__process_element( ElementMaker.create_ndex_node_support_aspect_element( node_id, support_id)) def emit_cx_edge_support(self, edge_id, support_id): self.__process_element( ElementMaker.create_ndex_edge_support_aspect_element( edge_id, support_id)) def __add_pre_metadata(self): pre_meta_data = [] for aspect_name in self.__aspect_names: pre_meta_data.append( ElementMaker.create_pre_metadata_element( aspect_name, 1, '1.0', self.__update_time, [], 1)) self.__cx_writer.add_pre_meta_data(pre_meta_data) def __add_post_metadata(self): post_meta_data = [ ElementMaker.create_post_metadata_element('nodes', self.__node_id_counter), ElementMaker.create_post_metadata_element('edges', self.__edge_id_counter), ElementMaker.create_post_metadata_element( 'supports', self.__support_id_counter), ElementMaker.create_post_metadata_element( 'citations', self.__citation_id_counter) ] self.__cx_writer.add_post_meta_data(post_meta_data) def __process_element(self, element): aspect_name = element.get_name() if self.__current_name is None: self.__cx_writer.start_aspect_fragment(aspect_name) elif self.__current_name != aspect_name: self.__cx_writer.end_aspect_fragment() self.__cx_writer.start_aspect_fragment(aspect_name) self.__current_name = aspect_name self.__cx_writer.write_aspect_element(element)