Skip to content

Commit

Permalink
Merge pull request #5 from villelaitila/You_are_not_dead_yet_But_watc…
Browse files Browse the repository at this point in the history
…h_for_further_reports

Model filtering to copy attribute data also
  • Loading branch information
villelaitila authored Aug 18, 2021
2 parents b73f363 + 5679ff0 commit 5fc1eae
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
7 changes: 5 additions & 2 deletions src/sgraph/converters/sgraph_to_cytoscape.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,13 @@ def convert_graph_assocs(elem):

def main():
import sys
g = SGraph.parse_xml(sys.argv[1])
g = SGraph.parse_xml_or_zipped_xml(sys.argv[1])
central_element_path = sys.argv[2]
# Print this way for debugging pursposes: print(g.to_deps(fname=None))
elem = g.createOrGetElementFromPath(central_element_path)
subg = ModelApi().filter_model(elem)
subg = ModelApi().filter_model(elem, g)

# Print this way for debugging purposes: print(subg.to_xml(fname=None))
graph_json = graph_to_cyto(subg)
print(graph_json)

Expand Down
2 changes: 1 addition & 1 deletion src/sgraph/graphdataservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def extract_subgraph_as_json(analysis_target_name, output_dir, element_path, rec
elem = graph.createOrGetElementFromPath(element_path)
if elem:
# TODO handle also recursion param
subgraph = ModelApi().filter_model(elem)
subgraph = ModelApi().filter_model(elem, graph)
return produce_output(flavour, subgraph)
else:
raise Exception(f'Element path {element_path} not found')
Expand Down
43 changes: 28 additions & 15 deletions src/sgraph/modelapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,35 @@ def add_if_matches(self, ea, found, dep_filter):
if not ModelApi.intra_file(ea):
found.append(ea)

def filter_model(self, elem):
subg = SGraph()
# noinspection PyUnusedLocal
selem = subg.createOrGetElement(elem)
s = [elem]
while s:
e = s.pop(0)
for ea in e.outgoing:
selem_from = subg.createOrGetElement(ea.fromElement)
selem_to = subg.createOrGetElement(ea.toElement)
def filter_model(self, source_elem, source_graph):
sub_graph = SGraph()
sub_graph.createOrGetElement(source_elem)
stack = [source_elem]
# Traverse related elements from the source_graph using stack
while stack:
elem = stack.pop(0)
for ea in elem.outgoing:
selem_from = sub_graph.createOrGetElement(ea.fromElement)
selem_to = sub_graph.createOrGetElement(ea.toElement)
sea = SElementAssociation(selem_from, selem_to, ea.deptype, ea.attrs)
sea.initElems()
for ea in e.incoming:
selem_from = subg.createOrGetElement(ea.fromElement)
selem_to = subg.createOrGetElement(ea.toElement)
for ea in elem.incoming:
selem_from = sub_graph.createOrGetElement(ea.fromElement)
selem_to = sub_graph.createOrGetElement(ea.toElement)
sea = SElementAssociation(selem_from, selem_to, ea.deptype, ea.attrs)
sea.initElems()
s.extend(e.children)
return subg
stack.extend(elem.children)

# Now that elements have been created, copy attribute data from the whole graph, via
# traversal using two stacks.
stack = [sub_graph.rootNode]
whole_graph_stack = [source_graph.rootNode]
while stack:
elem = stack.pop(0)
corresponding_source_elem = whole_graph_stack.pop(0)
elem.attrs = corresponding_source_elem.attrs.copy()
for elem in elem.children:
stack.append(elem)
whole_graph_stack.append(corresponding_source_elem.getChildByName(elem.name))

return sub_graph

0 comments on commit 5fc1eae

Please sign in to comment.