def find_dependencies(config_dirs, maindir, seeds): """ returns res, with res['fd'] ~ FindDependencies """ librarian = Librarian() for e in config_dirs: librarian.find_libraries(e) default_library = librarian.get_library_by_dir(maindir) fd = FindDependencies(default_library) if seeds is None: # add all models for all libraries libnames = list(librarian.get_libraries()) seeds = [] for libname in libnames: library = librarian.load_library(libname) ndps = library.list_spec(SPEC_MODELS) for name in ndps: seeds.append('%s.%s' % (libname, name)) else: pass fd.search(seeds) res = {} res['fd'] = fd return res
def find_dependencies(config_dirs, maindir, seeds): """ returns res, with res['fd'] ~ FindDependencies """ librarian = Librarian() for e in config_dirs: librarian.find_libraries(e) default_library = librarian.get_library_by_dir(maindir) fd = FindDependencies(default_library) if seeds is None: # add all models for all libraries libnames = list(librarian.get_libraries()) seeds = [] for libname in libnames: library = librarian.load_library(libname) ndps = library.list_ndps() for name in ndps: seeds.append('%s.%s' % (libname, name)) else: pass fd.search(seeds) res = {} res['fd'] = fd return res
def go(self): logger.setLevel(logging.DEBUG) options = self.get_options() if not options.contracts: disable_all() params = options.get_extra() if len(params) < 1: raise ValueError("Please specify name.") config_dirs = options.config_dirs.split(":") maindir = options.maindir out_dir = options.out if out_dir is None: out_dir = os.path.join("out", "mcdp_render") if options.cache: cache_dir = os.path.join(out_dir, "_cached", "solve") else: cache_dir = None librarian = Librarian() for e in config_dirs: librarian.find_libraries(e) library = librarian.get_library_by_dir(maindir) if cache_dir is not None: library.use_cache_dir(cache_dir) docs = params if not docs: msg = "At least one argument required." raise_desc(UserError, msg) for docname in docs: basename = docname + "." + MCDPLibrary.ext_doc_md f = library._get_file_data(basename) data = f["data"] realpath = f["realpath"] generate_pdf = options.pdf render(library, docname, data, realpath, out_dir, generate_pdf)
def do_plots(logger, model_name, plots, outdir, extra_params, maindir, extra_dirs, use_cache): if '.mcdp' in model_name: model_name2 = model_name.replace('.mcdp', '') msg = 'Arguments should be model names, not file names.' msg += ' Interpreting %r as %r.' % (model_name, model_name2) logger.warn(msg) model_name = model_name2 if use_cache: cache_dir = os.path.join(outdir, '_cached/mcdp_plot_cache') logger.info('using cache %s' % cache_dir) else: cache_dir = None librarian = Librarian() for e in extra_dirs: librarian.find_libraries(e) library = librarian.get_library_by_dir(maindir) if cache_dir is not None: library.use_cache_dir(cache_dir) assert library.library_name is not None is_ndp = library.file_exists(model_name + '.mcdp') is_poset = library.file_exists(model_name + '.mcdp_poset') if is_ndp: results = do_plots_ndp(model_name, library, plots, extra_params) elif is_poset: results = do_plots_poset(model_name, library, plots) else: msg = 'Cannot find anything corresponding to %r.' % model_name raise_desc(ValueError, msg) return write_results(results, model_name, outdir)
def other_reports(outdir, maindir, config_dirs, entry): print('config dirs: {}'.format(config_dirs)) librarian = Librarian() for e in config_dirs: librarian.find_libraries(e) default_library = librarian.get_library_by_dir(maindir) tex = '' library = default_library.load_library(entry.libname) if isinstance(entry, EntryTemplate): context = library._generate_context_with_hooks() template = library.load_template(entry.name, context) pdf = ndp_template_graph_enclosed(library=library, template=template, style=STYLE_GREENREDSYM, yourname=None, data_format='pdf', direction='TB', enclosed=True) base = entry.libname + '-' + entry.name + '-ndp_template_graph_enclosed.pdf' out = os.path.join(outdir, base) write_to_file(out, pdf) tex += '\n\\includegraphics{%s}' % base source_code = library._get_file_data(entry.name +'.mcdp_template')['data'] code_pdf = get_ast_as_pdf(s=source_code, parse_expr=Syntax.template) base = entry.libname + '-' + entry.name + '-syntax_pdf.pdf' out = os.path.join(outdir, base) write_to_file(out, code_pdf) tex += '\n\\includegraphics{%s}' % base if isinstance(entry, EntryNDP): filename = entry.name + '.mcdp' x = library._get_file_data(filename) data = {} data['model_name'] = entry.name data['s'] = x['data'].strip() data['filename'] = x['realpath'] data['params'] = {} data['library'] = library d = dict(allplots) plots = list(d.keys()) plots.remove('ndp_clean') plots.remove('ndp_default') plots.remove('ndp_greenred') plots.remove('dp_graph_tree_compact_labeled') # plots.remove('dp_graph_compact_labeled') for p in plots: # print('plotting %r ' % p) try: res = d[p](data) except DPSemanticError as e: print e continue except CmdException as e: print e continue assert isinstance(res, list), res for r in res: assert isinstance(r, tuple), r mime, name, x = r assert isinstance(x, str), x ext = mime base = entry.libname + '-' + entry.name + '-%s.%s' % (name, ext) out = os.path.join(outdir, base) write_to_file(out, x) if ext == 'pdf': tex += '\n\\includegraphics{%s}' % base print('outdir: %s' % outdir) print('entry: {}'.format(entry)) return tex
def do_plots(logger, model_name, plots, outdir, extra_params, maindir, extra_dirs, use_cache): data = {} if '.mcdp' in model_name: model_name2 = model_name.replace('.mcdp', '') msg = 'Arguments should be model names, not file names.' msg += ' Interpreting %r as %r.' % (model_name, model_name2) logger.warn(msg) model_name = model_name2 data['model_name'] = model_name if use_cache: cache_dir = os.path.join(outdir, '_cached/mcdp_plot_cache') logger.info('using cache %s' % cache_dir) else: cache_dir = None librarian = Librarian() for e in extra_dirs: librarian.find_libraries(e) library = librarian.get_library_by_dir(maindir) if cache_dir is not None: library.use_cache_dir(cache_dir) assert library.library_name is not None filename = model_name + '.mcdp' x = library._get_file_data(filename) data['s'] = x['data'] data['filename'] = x['realpath'] data['params'] = parse_params(extra_params) data['library'] = library d = dict(allplots) results = [] for p in plots: # print('plotting %r ' % p) try: if p in d: res = d[p](data) else: msg = 'Unknown plot.' raise_desc(ValueError, msg, plot=p, available=sorted(d.keys())) except CmdException as e: mcdp_dev_warning('Add better checks of error.') logger.error(e) continue except Exception as e: logger.error('While creating %r' % p) raise assert isinstance(res, list), res for r in res: assert isinstance(r, tuple), r mime, name, x = r assert isinstance(x, str), x ext = mime base = model_name + '-%s.%s' % (name, ext) out = os.path.join(outdir, base) logger.info('Writing to %s' % out) with open(out, 'w') as f: f.write(x) results.append(r) return results
def solve_main( logger, config_dirs, maindir, cache_dir, model_name, lower, upper, out_dir, max_steps, query_strings, intervals, _exp_advanced, expect_nres, imp, expect_nimp, plot, do_movie, # expect_res=None, expect_res, # @UnusedVariable make, ): if out_dir is None: out = solve_get_output_dir(prefix="out/out") else: out = out_dir logger.info("Using output dir %r" % out) librarian = Librarian() logger.info("Looking for libraries in %s..." % config_dirs) for e in config_dirs: librarian.find_libraries(e) logger.info("Found %d libraries." % len(librarian.get_libraries())) library = librarian.get_library_by_dir(maindir) if cache_dir is not None: library.use_cache_dir(cache_dir) ndp = library.load_ndp(model_name) basename = model_name if make or (plot and imp): ndp_labelled = get_labelled_version(ndp) else: ndp_labelled = ndp basename, dp = solve_get_dp_from_ndp(basename=basename, ndp=ndp_labelled, lower=lower, upper=upper) F = dp.get_fun_space() R = dp.get_res_space() UR = UpperSets(R) query = " ".join(query_strings) c = library.parse_constant(query) tu = get_types_universe() try: tu.check_leq(c.unit, F) except NotLeq as e: msg = "The value given cannot be converted to functionality space." raise_wrapped(UserError, e, msg, unit=c.unit, F=F, compact=True) fg = express_value_in_isomorphic_space(c.unit, c.value, F) logger.info("query: %s" % F.format(fg)) tracer = Tracer(logger=logger) res, trace = solve_meat_solve(tracer, ndp, dp, fg, intervals, max_steps, _exp_advanced) nres = len(res.minimals) if expect_nres is not None: if nres != expect_nres: msg = "Found wrong number of resources" raise_desc(ExpectationsNotMet, msg, expect_nres=expect_nres, nres=nres) if imp: M = dp.get_imp_space() nimplementations = 0 for r in res.minimals: ms = dp.get_implementations_f_r(fg, r) nimplementations += len(ms) s = "r = %s " % R.format(r) for j, m in enumerate(ms): # print('m = %s' % str(m)) s += "\n implementation %d of %d: m = %s " % (j + 1, len(ms), M.format(m)) if make: imp_dict = get_imp_as_recursive_dict(M, m) # , ignore_hidden=False) print("imp dict: %r" % imp_dict) context = {} artifact = ndp_make(ndp, imp_dict, context) print("artifact: %s" % artifact) tracer.log(s) if expect_nimp is not None: if expect_nimp != nimplementations: msg = "Found wrong number of implementations" raise_desc(ExpectationsNotMet, msg, expect_nimp=expect_nimp, nimplementations=nimplementations) # if expect_res is not None: # value = interpret_string(expect_res) # tracer.log('value: %s' % value) # res_expected = value.value # tu = get_types_universe() # # If it's a tuple of two elements, then we assume it's upper/lower bounds # if isinstance(value.unit, PosetProduct): # subs = value.unit.subs # assert len(subs) == 2, subs # # lower_UR_expected, upper_UR_expected = subs # lower_res_expected, upper_res_expected = value.value # # lower_bound = tu.get_embedding(lower_UR_expected, UR)[0](lower_res_expected) # upper_bound = tu.get_embedding(upper_UR_expected, UR)[0](upper_res_expected) # # tracer.log('lower: %s <= %s' % (UR.format(lower_bound), UR.format(res))) # tracer.log('upper: %s <= %s' % (UR.format(upper_bound), UR.format(res))) # # UR.check_leq(lower_bound, res) # UR.check_leq(res, upper_bound) # else: # # only one element: equality # UR_expected = value.unit # tu.check_leq(UR_expected, UR) # A_to_B, _B_to_A = tu.get_embedding(UR_expected, UR) # # res_expected_f = A_to_B(res_expected) # try: # UR.check_equal(res, res_expected_f) # except NotEqual as e: # raise_wrapped(ExpectationsNotMet, e, 'res is different', # res=res, res_expected=res_expected, compact=True) if plot: r = Report() if _exp_advanced: from mcdp_report.generic_report_utils import generic_report generic_report(r, dp, trace, annotation=None, axis0=(0, 0, 0, 0)) else: f = r.figure() from mcdp_report.generic_report_utils import generic_plot generic_plot(f, space=UR, value=res) from mcdp_report.generic_report_utils import generic_report_trace generic_report_trace(r, ndp, dp, trace, out, do_movie=do_movie) out_html = os.path.join(out, "report.html") logger.info("writing to %r" % out_html) r.to_html(out_html) if plot and imp: from mcdp_report_ndp_tests.test1 import GetValues from mcdp_report.gg_ndp import gvgen_from_ndp from mcdp_report.gdc import STYLE_GREENREDSYM from mcdp_report.gg_utils import gg_figure M = dp.get_imp_space() report_solutions = Report() for i, r in enumerate(res.minimals): ms = dp.get_implementations_f_r(fg, r) for j, m in enumerate(ms): imp_dict = get_imp_as_recursive_dict(M, m) images_paths = library.get_images_paths() gv = GetValues(ndp=ndp, imp_dict=imp_dict, nu=upper, nl=1) setattr(ndp, "_hack_force_enclose", True) with report_solutions.subsection("sol-%s-%s" % (i, j)) as rr: # Left right gg = gvgen_from_ndp( ndp=ndp, style=STYLE_GREENREDSYM, images_paths=images_paths, plotting_info=gv, direction="LR" ) gg_figure(rr, "figure", gg, do_png=True, do_pdf=True, do_svg=False, do_dot=False) # Top-bottom gg = gvgen_from_ndp( ndp=ndp, style=STYLE_GREENREDSYM, images_paths=images_paths, plotting_info=gv, direction="TB" ) gg_figure(rr, "figure2", gg, do_png=True, do_pdf=True, do_svg=False, do_dot=False) out_html = os.path.join(out, "report_solutions.html") logger.info("writing to %r" % out_html) report_solutions.to_html(out_html)
def solve_main( logger, config_dirs, maindir, cache_dir, model_name, lower, upper, out_dir, max_steps, query_strings, intervals, _exp_advanced, expect_nres, imp, expect_nimp, plot, do_movie, # expect_res=None, expect_res, # @UnusedVariable make): if out_dir is None: out = solve_get_output_dir(prefix='out/out') else: out = out_dir logger.info('Using output dir %r' % out) librarian = Librarian() logger.info('Looking for libraries in %s...' % config_dirs) for e in config_dirs: librarian.find_libraries(e) logger.info('Found %d libraries.' % len(librarian.get_libraries())) library = librarian.get_library_by_dir(maindir) if cache_dir is not None: library.use_cache_dir(cache_dir) ndp = library.load_ndp(model_name) basename = model_name if make or (plot and imp): ndp_labelled = get_labelled_version(ndp) else: ndp_labelled = ndp basename, dp = solve_get_dp_from_ndp(basename=basename, ndp=ndp_labelled, lower=lower, upper=upper) F = dp.get_fun_space() R = dp.get_res_space() UR = UpperSets(R) query = " ".join(query_strings) c = library.parse_constant(query) tu = get_types_universe() try: tu.check_leq(c.unit, F) except NotLeq as e: msg = 'The value given cannot be converted to functionality space.' raise_wrapped(UserError, e, msg, unit=c.unit, F=F, compact=True) fg = express_value_in_isomorphic_space(c.unit, c.value, F) logger.info('query: %s' % F.format(fg)) tracer = Tracer(logger=logger) res, trace = solve_meat_solve_ftor(tracer, ndp, dp, fg, intervals, max_steps, _exp_advanced) nres = len(res.minimals) if expect_nres is not None: if nres != expect_nres: msg = 'Found wrong number of resources' raise_desc(ExpectationsNotMet, msg, expect_nres=expect_nres, nres=nres) if imp: M = dp.get_imp_space() nimplementations = 0 for r in res.minimals: ms = dp.get_implementations_f_r(fg, r) nimplementations += len(ms) s = 'r = %s ' % R.format(r) for j, m in enumerate(ms): # print('m = %s' % str(m)) s += "\n implementation %d of %d: m = %s " % (j + 1, len(ms), M.format(m)) if make: imp_dict = get_imp_as_recursive_dict( M, m) # , ignore_hidden=False) print('imp dict: %r' % imp_dict) context = {} artifact = ndp_make(ndp, imp_dict, context) print('artifact: %s' % artifact) tracer.log(s) if expect_nimp is not None: if expect_nimp != nimplementations: msg = 'Found wrong number of implementations' raise_desc(ExpectationsNotMet, msg, expect_nimp=expect_nimp, nimplementations=nimplementations) # if expect_res is not None: # value = interpret_string(expect_res) # tracer.log('value: %s' % value) # res_expected = value.value # tu = get_types_universe() # # If it's a tuple of two elements, then we assume it's upper/lower bounds # if isinstance(value.unit, PosetProduct): # subs = value.unit.subs # assert len(subs) == 2, subs # # lower_UR_expected, upper_UR_expected = subs # lower_res_expected, upper_res_expected = value.value # # lower_bound = tu.get_embedding(lower_UR_expected, UR)[0](lower_res_expected) # upper_bound = tu.get_embedding(upper_UR_expected, UR)[0](upper_res_expected) # # tracer.log('lower: %s <= %s' % (UR.format(lower_bound), UR.format(res))) # tracer.log('upper: %s <= %s' % (UR.format(upper_bound), UR.format(res))) # # UR.check_leq(lower_bound, res) # UR.check_leq(res, upper_bound) # else: # # only one element: equality # UR_expected = value.unit # tu.check_leq(UR_expected, UR) # A_to_B, _B_to_A = tu.get_embedding(UR_expected, UR) # # res_expected_f = A_to_B(res_expected) # try: # UR.check_equal(res, res_expected_f) # except NotEqual as e: # raise_wrapped(ExpectationsNotMet, e, 'res is different', # res=res, res_expected=res_expected, compact=True) if plot: r = Report() if _exp_advanced: from mcdp_report.generic_report_utils import generic_report generic_report(r, dp, trace, annotation=None, axis0=(0, 0, 0, 0)) else: f = r.figure() from mcdp_report.generic_report_utils import generic_plot generic_plot(f, space=UR, value=res) from mcdp_report.generic_report_utils import generic_report_trace generic_report_trace(r, ndp, dp, trace, out, do_movie=do_movie) out_html = os.path.join(out, 'report.html') logger.info('writing to %r' % out_html) r.to_html(out_html) if plot and imp: from mcdp_report_ndp_tests.test1 import GetValues from mcdp_report.gg_ndp import gvgen_from_ndp from mcdp_report.gdc import STYLE_GREENREDSYM from mcdp_report.gg_utils import gg_figure M = dp.get_imp_space() report_solutions = Report() for i, r in enumerate(res.minimals): ms = dp.get_implementations_f_r(fg, r) for j, m in enumerate(ms): imp_dict = get_imp_as_recursive_dict(M, m) images_paths = library.get_images_paths() image_source = ImagesFromPaths(images_paths) gv = GetValues(ndp=ndp, imp_dict=imp_dict, nu=upper, nl=1) setattr(ndp, '_hack_force_enclose', True) with report_solutions.subsection('sol-%s-%s' % (i, j)) as rr: # Left right gg = gvgen_from_ndp(ndp=ndp, style=STYLE_GREENREDSYM, image_source=image_source, plotting_info=gv, direction='LR') gg_figure(rr, 'figure', gg, do_png=True, do_pdf=True, do_svg=False, do_dot=False) # Top-bottom gg = gvgen_from_ndp(ndp=ndp, style=STYLE_GREENREDSYM, image_source=image_source, plotting_info=gv, direction='TB') gg_figure(rr, 'figure2', gg, do_png=True, do_pdf=True, do_svg=False, do_dot=False) out_html = os.path.join(out, 'report_solutions.html') logger.info('writing to %r' % out_html) report_solutions.to_html(out_html)
def go(self): logger.setLevel(logging.DEBUG) options = self.get_options() symbols = self.options.symbols if symbols is not None: symbols = open(symbols).read() else: symbols = '' if not options.contracts: disable_all() stylesheet = options.stylesheet # make sure it exists get_css_filename('compiled/%s' % stylesheet) params = options.get_extra() if len(params) < 1: raise ValueError('Please specify name.') config_dirs = options.config_dirs.split(":") maindir = options.maindir out_dir = options.out if options.cache: cache_dir = os.path.join(out_dir, '_cached', 'solve') else: cache_dir = None librarian = Librarian() for e in config_dirs: librarian.find_libraries(e) library = librarian.get_library_by_dir(maindir) if cache_dir is not None: library.use_cache_dir(cache_dir) docs = params if not docs: msg = 'At least one argument required.' raise_desc(UserError, msg) for docname in docs: if '/' in docname: docname0 = os.path.split(docname)[-1] logger.info("Using %r rather than %r" % (docname0, docname)) docname = docname0 suffix = '.' + MCDPConstants.ext_doc_md if docname.endswith(suffix): docname = docname.replace(suffix, '') basename = docname + suffix f = library._get_file_data(basename) data = f['data'] realpath = f['realpath'] generate_pdf = options.pdf_figures if out_dir is None: use_out_dir = os.path.dirname(realpath) else: use_out_dir = os.path.join('out', 'mcdp_render') raise_errors = not options.forgiving use_mathjax = bool(options.mathjax) html_filename = render(library, docname, data, realpath, use_out_dir, generate_pdf, stylesheet=stylesheet, symbols=symbols, raise_errors=raise_errors, use_mathjax=use_mathjax) if options.pdf: run_prince(html_filename)
def other_reports(outdir, maindir, config_dirs, entry): print('config dirs: {}'.format(config_dirs)) librarian = Librarian() for e in config_dirs: librarian.find_libraries(e) default_library = librarian.get_library_by_dir(maindir) tex = '' library = default_library.load_library(entry.libname) if isinstance(entry, EntryTemplate): context = library._generate_context_with_hooks() template = library.load_spec(SPEC_TEMPLATES, entry.name, context) pdf = ndp_template_graph_enclosed(library=library, template=template, style=STYLE_GREENREDSYM, yourname=None, data_format='pdf', direction='TB', enclosed=True) base = entry.libname + '-' + entry.name + '-ndp_template_graph_enclosed.pdf' out = os.path.join(outdir, base) write_to_file(out, pdf) tex += '\n\\includegraphics{%s}' % base source_code = library._get_file_data(entry.name + '.mcdp_template')['data'] code_pdf = get_ast_as_pdf(s=source_code, parse_expr=Syntax.template) base = entry.libname + '-' + entry.name + '-syntax_pdf.pdf' out = os.path.join(outdir, base) write_to_file(out, code_pdf) tex += '\n\\includegraphics{%s}' % base if isinstance(entry, EntryNDP): filename = entry.name + '.mcdp' x = library._get_file_data(filename) data = {} data['model_name'] = entry.name data['s'] = x['data'].strip() data['filename'] = x['realpath'] data['params'] = {} data['library'] = library d = dict(allplots) plots = list(d.keys()) plots.remove('ndp_clean') plots.remove('ndp_default') plots.remove('ndp_greenred') plots.remove('dp_graph_tree_compact_labeled') # plots.remove('dp_graph_compact_labeled') for p in plots: # print('plotting %r ' % p) try: res = d[p](data) except DPSemanticError as e: logger.error(str(e)) continue except CmdException as e: logger.error(str(e)) continue assert isinstance(res, list), res for r in res: assert isinstance(r, tuple), r mime, name, x = r assert isinstance(x, str), x ext = mime base = entry.libname + '-' + entry.name + '-%s.%s' % (name, ext) out = os.path.join(outdir, base) write_to_file(out, x) if ext == 'pdf': tex += '\n\\includegraphics{%s}' % base print('outdir: %s' % outdir) print('entry: {}'.format(entry)) return tex