Commit 4688fc5a authored by VIGNET Pierre's avatar VIGNET Pierre
Browse files

Move convert_solution_file_to_graphs, solutions_2_graph to separated module

parent f0ac297c
......@@ -66,9 +66,9 @@ def solutions_2_graph(args):
"""
# Module import
import solution_repr
import solution_sort
params = args_to_param(args)
solution_repr.solutions_2_graph(
solution_sort.solutions_2_graph(
params['output'],
params['chart_file'],
params['path']
......
......@@ -86,7 +86,6 @@ from tools.models import get_transitions, \
from tools.models import get_places_data
from tools.graphs import build_graph, get_json_graph
import cadbiom.commons as cm
LOGGER = cm.logger()
......@@ -185,49 +184,6 @@ def draw_graph(output_dir, solution, solution_index, G,
plt.show()
def convert_solution_file_to_graphs(output_dir, sol_steps, transitions):
"""Build a graph based on the given solution
Each solution is composed of a set of frontier places and steps,
themselves composed of events.
We construct a graph based on the transitions that occur in the composition
of the events of the given solution.
:param output_dir: Output path.
:param sol_steps: A generator of tuples of "frontier places" and a list of
events in each step.
:Example:
.. code-block:: python
("Bx Ax", [['h2', 'h00'], ['h3'], ['h0', 'h1'], ['hlast']])
:param transitions: A dictionnary of events as keys, and transitions as values.
Since many transitions can define an event, values are lists.
Each transition is a tuple with: origin node, final node, attributes
like label and condition.
:Example:
.. code-block:: python
{'h00': [('Ax', 'n1', {'label': 'h00[]'}),]
:type output_dir: <str>
:type sol_steps: <tuple <str>, <list>>
:type transitions: <dict <list <tuple <str>, <str>, <dict <str>: <str>>>>
"""
for sol_index, (sol, steps) in enumerate(sol_steps):
# build_graph() returns :
# G, transition_nodes, all_nodes, edges_in_cond, edges
# sol_index is used to order files according to the order of appearance
# in the file
export_graph(output_dir, sol, sol_index,
*build_graph(sol, steps, transitions)
)
def test_main():
......@@ -304,55 +260,7 @@ def test_main():
def solutions_2_graph(output_dir, model_file, solution_path):
"""Entry point for solutions_2_graph
Create GraphML formated files containing a representation of the
trajectories for every solution in complete MAC files (*cam_complete files).
This is a function to visualize paths taken by the solver from the boundaries
to the entities of interest.
This functions tests if the given path is a directory or a file.
:param output_dir: Output path.
:param model_file: Filepath of the model.
:param solution_path:
:type output_dir: <str>
:type model_file: <str>
:type solution_path: <str>
"""
# Check valid input file/directory
assert os.path.isfile(solution_path) or os.path.isdir(solution_path)
# Get transitions from the model
model_transitions = get_transitions_from_model_file(model_file)
if os.path.isfile(solution_path):
# The given path is a solution file
convert_solution_file_to_graphs(
output_dir,
load_solutions(solution_path),
model_transitions
)
elif os.path.isdir(solution_path):
# The given path is a directory
solution_path = solution_path if solution_path[-1] == '/' \
else solution_path + '/'
# Decompilation of all files in the directory
for file_number, solution_path in \
enumerate(glob.glob(solution_path + '*cam_complete.txt'), 1):
convert_solution_file_to_graphs(
output_dir,
load_solutions(solution_path),
model_transitions
)
LOGGER.info("Files processed: " + str(file_number))
def graph_isomorph_test(model_file_1, model_file_2, output_dir='graphs/',
......
......@@ -37,6 +37,7 @@ import glob
from tools.solutions import get_solutions
from tools.models import get_transitions_from_model_file
from tools.solutions import load_solutions, get_json_solutions
from tools.graphs import export_graph, build_graph
import cadbiom.commons as cm
......@@ -176,3 +177,99 @@ def solutions_2_json(output_dir, model_file, solution_path, conditions=True):
write_json_file(decompiled_filename, decomp_solutions)
LOGGER.info("Files processed: " + str(file_number))
def solutions_2_graph(output_dir, model_file, solution_path):
"""Entry point for solutions_2_graph
Create GraphML formated files containing a representation of the
trajectories for every solution in complete MAC files (*cam_complete files).
This is a function to visualize paths taken by the solver from the boundaries
to the entities of interest.
This functions tests if the given path is a directory or a file.
:param output_dir: Output path.
:param model_file: Filepath of the model.
:param solution_path:
:type output_dir: <str>
:type model_file: <str>
:type solution_path: <str>
"""
# Check valid input file/directory
assert os.path.isfile(solution_path) or os.path.isdir(solution_path)
# Get transitions from the model
model_transitions = get_transitions_from_model_file(model_file)
if os.path.isfile(solution_path):
# The given path is a solution file
convert_solution_file_to_graphs(
output_dir,
load_solutions(solution_path),
model_transitions
)
elif os.path.isdir(solution_path):
# The given path is a directory
solution_path = solution_path if solution_path[-1] == '/' \
else solution_path + '/'
# Decompilation of all files in the directory
for file_number, solution_path in \
enumerate(glob.glob(solution_path + '*cam_complete.txt'), 1):
convert_solution_file_to_graphs(
output_dir,
load_solutions(solution_path),
model_transitions
)
LOGGER.info("Files processed: " + str(file_number))
def convert_solution_file_to_graphs(output_dir, sol_steps, transitions):
"""Build a graph based on the given solution
Each solution is composed of a set of frontier places and steps,
themselves composed of events.
We construct a graph based on the transitions that occur in the composition
of the events of the given solution.
:param output_dir: Output path.
:param sol_steps: A generator of tuples of "frontier places" and a list of
events in each step.
:Example:
.. code-block:: python
("Bx Ax", [['h2', 'h00'], ['h3'], ['h0', 'h1'], ['hlast']])
:param transitions: A dictionnary of events as keys, and transitions as values.
Since many transitions can define an event, values are lists.
Each transition is a tuple with: origin node, final node, attributes
like label and condition.
:Example:
.. code-block:: python
{'h00': [('Ax', 'n1', {'label': 'h00[]'}),]
:type output_dir: <str>
:type sol_steps: <tuple <str>, <list>>
:type transitions: <dict <list <tuple <str>, <str>, <dict <str>: <str>>>>
"""
for sol_index, (sol, steps) in enumerate(sol_steps):
# build_graph() returns :
# G, transition_nodes, all_nodes, edges_in_cond, edges
# sol_index is used to order files according to the order of appearance
# in the file
export_graph(output_dir, sol, sol_index,
*build_graph(sol, steps, transitions)
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment