Commit cb0f6cfd authored by Raphaël Bleuse's avatar Raphaël Bleuse

Move computation of crossing levels to Template

parent 409b1738
......@@ -5,8 +5,13 @@ Core routines and optimization logic.
"""
import json
import logging
from . import check
from . import _legacy # XXX get rid of this!
logger = logging.getLogger(__name__) # pylint: disable=invalid-name
def final_position(matrix):
......@@ -36,6 +41,7 @@ class Template:
if not check.is_linking(matrix):
raise ValueError('Invalid linking matrix')
self.matrix = matrix
self.__crosslevels = None
@classmethod
def from_json(cls, fp):
......@@ -74,3 +80,13 @@ class Template:
raise TypeError('Invalid input structure')
return cls(matrix)
@property
def crosslevels(self):
"""Level-by-level list of concurrent crossings of the template."""
if self.__crosslevels is None:
logger.info('Starting optimization of template depth')
optimizer = _legacy.Tree.from_matrix(self.matrix)
self.__crosslevels = optimizer.getShortestPaths()
logger.info('Finished optimization of template depth')
return self.__crosslevels
......@@ -29,22 +29,16 @@ def run(infile, *, output, color=True, complete_flow=False, scale=1.0):
exit(code=2)
logger.info('Input matrix')
for m in template.matrix:
logger.info(f' {m}')
logger.info("Starting constructing the tree")
myTree = _legacy.Tree.from_matrix(template.matrix)
logger.info("Finished constructing the tree")
for row in template.matrix:
logger.info(f' {row}')
logger.info("Starting creation of the SVG template")
try:
if output == '-': # the special argument '-' means stdout
shortestPath = myTree.getShortestPaths()
_legacy.drawSVGTemplate(template.matrix, shortestPath, sys.stdout, entireTemplate=complete_flow, white=not color, scale=scale)
_legacy.drawSVGTemplate(template.matrix, template.crosslevels, sys.stdout, entireTemplate=complete_flow, white=not color, scale=scale)
else:
with open(output, mode='w') as fp:
shortestPath = myTree.getShortestPaths()
_legacy.drawSVGTemplate(template.matrix, shortestPath, fp, entireTemplate=complete_flow, white=not color, scale=scale)
_legacy.drawSVGTemplate(template.matrix, template.crosslevels, fp, entireTemplate=complete_flow, white=not color, scale=scale)
except OSError as err:
logger.critical(f'Unable to write output: {err}')
exit(code=2)
......
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