# HG changeset patch # User casties # Date 1443030422 -7200 # Node ID 97f2da68fb5f40590a28b13b80e81b105b18ed3a # Parent 45a823b5bf33e163af155104bd1f57e13e8b5c29 first version of model2model graph manipulation tool. doesn't work yet. diff -r 45a823b5bf33 -r 97f2da68fb5f importFromOpenMind/importer/model2model.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/importFromOpenMind/importer/model2model.py Wed Sep 23 19:47:02 2015 +0200 @@ -0,0 +1,75 @@ +import networkx as nx +import sys + +## configure behaviour + +# metworkx graph files +input_fn = 'ismi_graph.gpickle' +output_fn = 'ismi_graph_mod.gpickle' + +# operations +ops = ['contract', 'inv_rels'] + +# add relations to these objects as attributes with the relations name +contract_relations_into_attributes = {'PLACE': ['label'], + 'ALIAS': ['label']} + + + + +def fixName(name, is_src_rel=False, is_tar_rel=False, att_from_rel=False): + # these are too embarrassing... + if 'FLORUIT' in name: + name = name.replace('FLORUIT', 'FLOURISH') + + elif 'floruit' in name: + name = name.replace('floruit', 'flourish') + + if is_src_rel: + #name = name + '>' + pass + + if is_tar_rel: + name = '<' + name + + if att_from_rel: + # clean up relations as attribute names + name = name.replace('is_', '') + name = name.replace('has_', '') + name = name.replace('was_', '') + name = name.replace('_of', '') + + return name + + +def contractRelations(nx_graph): + """contract relations into attributes""" + + + + +## main + +print("Modify networkx graph") + +# read commandline parameters +if len(sys.argv) > 2: + input_fn = sys.argv[1] + output_fn = sys.argv[2] + #ops = sys.argv[3] + +# read networkx graph from pickle +print("Reading graph from %s"%input_fn) +nx_graph = nx.read_gpickle(input_fn) +print("Graph info: %s"%nx.info(nx_graph)) + +# operate +for op in ops: + if op == 'contract': + contractRelations(nx_graph) + + elif op == 'inv_rels': + invertRelations(nx_graph) + + +print("Done.")