def __copy__(self): c = Context() c.names = dict(**self.context.names) c.connections = list(self.context.connections) c.fnames = list(self.context.fnames) c.rnames = list(self.context.rnames) return CompositeNamedDP(c)
def from_parts(name2ndp, connections, fnames, rnames): c = Context() c.names = name2ndp c.connections = connections c.fnames = fnames c.rnames = rnames return CompositeNamedDP(c)
def __init__(self, context): self.context = Context() self.context.names = context.names.copy() self.context.connections = list(context.connections) self.context.fnames = list(context.fnames) self.context.rnames = list(context.rnames) check_consistent_data(self.context.names, self.context.fnames, self.context.rnames, self.context.connections) self._rnames = list(self.context.rnames) self._fnames = list(self.context.fnames)
def __init__(self, host_cache, db_view, subscribed_shelves, current_library_name): self.host_cache = host_cache self.db_view = db_view self.subscribed_shelves = subscribed_shelves self.current_library_name = current_library_name Context.__init__(self) self.load_ndp_hooks = [self.load_ndp] self.load_posets_hooks = [self.load_poset] self.load_primitivedp_hooks = [self.load_primitivedp] self.load_template_hooks = [self.load_template] self.load_library_hooks = [self.load_library]
def cndp_create_one_without_some_connections(ndp, exclude_connections, names): """ Creates a new CompositeNDP without some of the connections. A new function / resource pair is created for each cut connection. """ from mocdp.comp.context import Context context = Context() # Create the fun/res node in the original order for fname in ndp.get_fnames(): # simply copy the functionnode - it might be a LabeledNDP name = get_name_for_fun_node(fname) fndp = ndp.get_name2ndp()[name] context.fnames.append(fname) context.add_ndp(name, fndp) for rname in ndp.get_rnames(): # simply copy the functionnode - it might be a LabeledNDP name = get_name_for_res_node(rname) rndp = ndp.get_name2ndp()[name] context.rnames.append(rname) context.add_ndp(name, rndp) for _name, _ndp in ndp.get_name2ndp().items(): isf, fname = is_fun_node_name(_name) isr, rname = is_res_node_name(_name) if isf and fname in ndp.get_fnames(): pass elif isr and rname in ndp.get_rnames(): pass else: # print('regular: %r' % _name) context.add_ndp(_name, _ndp) for c in ndp.get_connections(): if c in exclude_connections: continue # print('adding connection %s' % str(c)) context.connections.append(c) # print('done') # for each cut connection for e, name in zip(exclude_connections, names): S = context.get_rtype(CResource(e.dp1, e.s1)) fn = context.add_ndp_fun_node(name, S) rn = context.add_ndp_res_node(name, S) c1 = Connection(e.dp1, e.s1, rn, name) c2 = Connection(fn, name, e.dp2, e.s2) context.connections.append(c1) context.connections.append(c2) return CompositeNamedDP.from_context(context)
def check_mark_suggestions(): s = """#comment mcdp { provides a [Nat] }""" parse_expr = Syntax.ndpt_dp_rvalue x = parse_wrap(Syntax.ndpt_dp_rvalue, s)[0] context = Context() xr = parse_ndp_refine(x, context) suggestions = get_suggestions(xr) # make sure we can apply them _s2 = apply_suggestions(s, suggestions) def postprocess(block): x = parse_ndp_refine(block, context) return x html = ast_to_html(s, parse_expr=parse_expr, add_line_gutter=False, encapsulate_in_precode=False, postprocess=postprocess) for where, replacement in suggestions: # @UnusedVariable #print('suggestion: %r' % replacement) html = html_mark(html, where, "suggestion") assert 'suggestion' in html
def mark_errors(): s = """#comment mcdp { #@ syntax error }""" parse_expr = Syntax.ndpt_dp_rvalue x = parse_wrap(Syntax.ndpt_dp_rvalue, s)[0] context = Context() xr = parse_ndp_refine(x, context) suggestions = get_suggestions(xr) # make sure we can apply them _s2 = apply_suggestions(s, suggestions) def postprocess(block): x = parse_ndp_refine(block, context) return x html = ast_to_html(s, parse_expr=parse_expr, add_line_gutter=False, encapsulate_in_precode=False, postprocess=postprocess) text = project_html(html) s2 = """#comment mcdp { syntax error }""" assert_equal(text, s2) assert not '#@' in text
def solve_stats(ndp): res = {} query = { "endurance": "1.5 hour", "velocity": "1 m/s", "extra_power": " 1 W", "extra_payload": "100 g", "num_missions": "100 []", } context = Context() f = convert_string_query(ndp=ndp, query=query, context=context) dp0 = ndp.get_dp() dpL, dpU = get_dp_bounds(dp0, nl=1, nu=1) F = dp0.get_fun_space() F.belongs(f) from mcdp import logger traceL = Tracer(logger=logger) resL = dpL.solve_trace(f, traceL) traceU = Tracer(logger=logger) resU = dpU.solve_trace(f, traceU) R = dp0.get_res_space() UR = UpperSets(R) print('resultsL: %s' % UR.format(resL)) print('resultsU: %s' % UR.format(resU)) res['traceL'] = traceL res['traceU'] = traceU res['resL'] = resL res['resU'] = resU res['nsteps'] = 100 return res
def get_gg(self, mf): from mcdp_report.gg_ndp import gvgen_from_ndp template = mf.get_template() library = mf.get_library() yourname = mf.get_yourname() if library is not None: context = library._generate_context_with_hooks() else: from mocdp.comp.context import Context context = Context() ndp = template.get_template_with_holes(context) if self.templatize_children: from mcdp_figures.figures_ndp import templatize_children_for_figures ndp = templatize_children_for_figures(ndp, enclosed=self.enclosed) if self.enclosed: setattr(ndp, '_hack_force_enclose', True) images_paths = library.get_images_paths() image_source = ImagesFromPaths(images_paths) gg = gvgen_from_ndp(ndp, style=self.style, direction=self.direction, image_source=image_source, yourname=yourname, skip_initial=True) return gg
def feat_import2(): """ NDP load """ data = { 'lib1.mcdplib/model1.mcdp': "mcdp {}", 'lib2.mcdplib/model2.mcdp': "`lib1.model1", 'lib2.mcdplib/model3.mcdp': """\ mcdp { a = instance `lib1.model1 } """ } d = create_hierarchy(data) librarian = Librarian() librarian.find_libraries(d) lib1 = librarian.load_library('lib1') _model1 = lib1.load_ndp('model1', context=Context()) lib2 = librarian.load_library('lib2') context = lib1._generate_context_with_hooks() _model2 = lib2.load_ndp('model2', context) _model3 = lib2.load_ndp('model3', context)
def eval_rvalue_as_constant(s): """ Parses as an rvalue (resource) and evaluates as a constant value. Returns ValueWithUnit. """ parsed = parse_wrap(Syntax.rvalue, s)[0] context = Context() from mcdp_lang.eval_constant_imp import eval_constant value = eval_constant(parsed, context) return value
def check_tuples2(): s = "<1g, 5J>" parsed = parse_wrap(Syntax.rvalue, s)[0] context = Context() _ret = eval_rvalue(parsed, context) #print(ret) same("take(<1g, 5J>, 1)", "5 J")
def _generate_context_with_hooks(self): context = Context() context.load_ndp_hooks = [self.load_ndp] context.load_posets_hooks = [self.load_poset] context.load_primitivedp_hooks = [self.load_primitivedp] context.load_template_hooks = [self.load_template] context.load_library_hooks = [self.load_library] return context
def check_suggestions(filename, source): # @UnusedVariable # skip generated files (hack) if 'drone_unc2_' in filename: return # print filename source = open(filename).read() x = parse_wrap(Syntax.ndpt_dp_rvalue, source)[0] xr = parse_ndp_refine(x, Context()) suggestions = get_suggestions(xr) for w, r in suggestions: # @UnusedVariable #print('"%s" -> "%s"' % (w.string[w.character:w.character_end], r)) pass # print source.__repr__() print(source) s2 = apply_suggestions(source, suggestions) print(s2) # if suggestions: # print(s2) # do it a second time x = parse_wrap(Syntax.ndpt_dp_rvalue, s2)[0] xr = parse_ndp_refine(x, Context()) suggestions2 = get_suggestions(xr) s3 = apply_suggestions(s2, suggestions2) # the third time, there should not be any more suggestions x = parse_wrap(Syntax.ndpt_dp_rvalue, s3)[0] xr = parse_ndp_refine(x, Context()) suggestions3 = get_suggestions(xr) if suggestions3: msg = 'I expected that there are at most 2 rounds of suggestions.' raise_desc(ValueError, msg, s=source, s2=s2, s3=s3, suggestions=suggestions, suggestions2=suggestions2, suggestions3=suggestions3)
def actual_load(): # maybe we should clone l = self.clone() #logger.debug('Parsing %r' % (name)) context_mine = Context() res = parsing_function(l, data, realpath, context=context_mine) setattr(res, MCDPConstants.ATTR_LOAD_NAME, name) return dict(res=res, context_warnings=context_mine.warnings, generation=current_generation)
def parse_template(string, context=None): from mcdp_lang.syntax import Syntax from mocdp.comp.context import Context if context is None: context = Context() expr = Syntax.template x = parse_wrap(expr, string)[0] x = parse_template_refine(x, context) res = parse_template_eval(x, context) return res
def parse_primitivedp(string, context=None): from mocdp.comp.context import Context from mcdp_lang.syntax import Syntax v = parse_wrap(Syntax.primitivedp_expr, string)[0] if context is None: context = Context() v2 = parse_primitivedp_refine(v, context) res = parse_primitivedp_eval(v2, context) return res
def clone_context(c): c2 = Context() c2.names = dict(**c.names) c2.connections = list(c.connections) c2.fnames = list(c.fnames) c2.rnames = list(c.rnames) return c2
def parse_constant(string, context=None): from mcdp_lang.syntax import Syntax from mocdp.comp.context import Context expr = Syntax.rvalue x = parse_wrap(expr, string)[0] if context is None: context = Context() x = parse_constant_refine(x, context) res = parse_constant_eval(x, context) return res
def parse_poset(string, context=None): from mocdp.comp.context import Context from .syntax import Syntax v = parse_wrap(Syntax.space, string)[0] if context is None: context = Context() v2 = parse_poset_refine(v, context) res = parse_poset_eval(v2, context) return res
def callback(tag0): context = Context() load = lambda x: library.load_spec(SPEC_TEMPLATES, x, context=context) parse = lambda x: library.parse_template(x, realpath=realpath, context=context) template = load_or_parse_from_tag(tag0, load, parse) mf = MakeFiguresTemplate(template=template, library=library, yourname=None) # XXX formats = ['svg'] if generate_pdf: formats.append('pdf') data = mf.get_figure(which,formats) tag = make_tag(tag0, which, data, ndp=None, template=template) return tag
def callback(tag0): context = Context() load = lambda x: library.load_poset(x, context=context) parse = lambda x: library.parse_poset(x, realpath=realpath, context=context) poset = load_or_parse_from_tag(tag0, load, parse) mf = MakeFiguresPoset(poset=poset, image_source=image_source) formats = ['svg'] if generate_pdf: formats.append('pdf') data = mf.get_figure(which, formats) tag = make_tag(tag0, which, data, ndp=None, template=None, poset=poset) return tag
def callback(tag0): assert tag0.parent is not None context = Context() load = lambda x: library.load_ndp(x, context=context) parse = lambda x: library.parse_ndp(x, realpath=realpath, context=context) ndp = load_or_parse_from_tag(tag0, load, parse) mf = MakeFiguresNDP(ndp=ndp, image_source=image_source, yourname=None) # XXX formats = ['svg'] if generate_pdf: formats.append('pdf') data = mf.get_figure(which,formats) tag = make_tag(tag0, which, data, ndp=ndp, template=None) return tag
def interpret_params_1string(p, F, context=None): if context is None: context = Context() res = parse_wrap(Syntax.constant_value, p)[0] vu = eval_constant(res, context) Fd = vu.unit fd = vu.value tu = get_types_universe() tu.check_leq(Fd, F) A_to_B, _ = tu.get_embedding(Fd, F) fg = A_to_B(fd) return fg
def parse_ndp(string, context=None): from mocdp.comp.context import Context from .syntax import Syntax from mocdp.comp.interfaces import NamedDP if context is None: context = Context() expr = parse_wrap(Syntax.ndpt_dp_rvalue, string)[0] expr2 = parse_ndp_refine(expr, context) res = parse_ndp_eval(expr2, context) assert isinstance(res, NamedDP), res return res
def parse_ndp(string, context=None): from mocdp.comp.context import Context from .syntax import Syntax from mocdp.comp.interfaces import NamedDP if context is None: context = Context() expr = parse_wrap(Syntax.ndpt_dp_rvalue, string)[0] # logger.debug('TMP:\n'+ recursive_print(expr)) expr2 = parse_ndp_refine(expr, context) # expr2 = expr # logger.debug('TMP:\n'+ recursive_print(expr2)) res = parse_ndp_eval(expr2, context) assert isinstance(res, NamedDP), res return res
def try_corrections2(s): x = parse_wrap(Syntax.ndpt_dp_rvalue, s)[0] context = Context() xr = parse_ndp_refine(x, context) print indent(recursive_print(xr), 'xr|') suggestions = get_suggestions(xr) for orig_where, sub in suggestions: orig_1 = orig_where.string[orig_where.character:orig_where. character_end] print 'Change %r in %r' % (orig_1, sub) s2 = apply_suggestions(s, suggestions) #print s2 _x2 = parse_wrap(Syntax.ndpt_dp_rvalue, s2)[0] return s2
def visit(recname, ndp): if isinstance(ndp, CompositeNamedDP): # NOTE: Does not label CompositeNamedDP context = Context() for child_name, child_ndp in ndp.get_name2ndp().items(): child_ndp2 = visit(recname + (child_name, ), child_ndp) context.add_ndp(child_name, child_ndp2) for c in ndp.get_connections(): context.add_connection(c) context.fnames = ndp.get_fnames() context.rnames = ndp.get_rnames() res = CompositeNamedDP.from_context(context) return res # res2 = LabelerNDP(res, recname) # return res2 elif isinstance(ndp, SimpleWrap): ndp2 = LabelerNDP(ndp, recname) return ndp2 elif isinstance(ndp, NamedDPCoproduct): assert len(ndp.ndps) == len(ndp.labels), ndp children = [] labels = [] for child_ndp, child_label in zip(ndp.ndps, ndp.labels): child2 = visit(recname + (child_label, ), child_ndp) children.append(child2) labels.append(child_label) res = NamedDPCoproduct(tuple(children), tuple(labels)) res2 = LabelerNDP(res, recname) return res2 elif isinstance(ndp, LabelerNDP): msg = 'Trying to re-label this as {}'.format(recname) raise_desc(DPInternalError, msg, ndp=ndp) else: raise NotImplementedError(type(ndp))
def check_join_not_existence(): """ A test for finite posets where the join might not exist. """ from mcdp_library.library import MCDPLibrary l = MCDPLibrary() add_def_poset( l, 'P', """ finite_poset { a <= b <= c A <= B <= C } """) # parse_wrap(Syntax.LOAD, '`') # parse_wrap(Syntax.posetname, 'P') # print Syntax.load_poset # parse_wrap(Syntax.load_poset, '`P') # parse_wrap(Syntax.space_operand, '`P') # parse_wrap(Syntax.fun_statement, "provides x [`P]") ndp = l.parse_ndp(""" mcdp { provides x [`P] provides y [`P] requires z [`P] z >= x z >= y } """, context=Context()) dp = ndp.get_dp() res1 = dp.solve(('a', 'b')) P = l.load_poset('P') UR = UpperSets(P) UR.check_equal(res1, UpperSet(['b'], P)) res2 = dp.solve(('a', 'A')) UR.check_equal(res2, UpperSet([], P))
def dpgraph_translate_rn(context, name, rn, rn2): def translate_connections(c): if c.dp1 == name and c.s1 == rn: c = Connection(name, rn2, c.dp2, c.s2) return c connections2 = map(translate_connections, context.connections) names2 = context.names.copy() names2[name] = wrap_change_name_resource(context.names[name], rn, rn2) c2 = Context() c2.rnames = context.rnames c2.fnames = context.fnames c2.connections = connections2 c2.names = names2 return dpgraph_making_sure_no_reps(c2)
def dpgraph_translate_fn(context, name, fn, fn2): def translate_connections(c): if c.dp2 == name and c.s2 == fn: c = Connection(c.dp1, c.s1, name, fn2) return c connections2 = map(translate_connections, context.connections) names2 = context.names.copy() names2[name] = wrap_change_name_function(context.names[name], fn, fn2) c2 = Context() c2.rnames = context.rnames c2.fnames = context.fnames c2.connections = connections2 c2.names = names2 return dpgraph_making_sure_no_reps(c2)
def ignore_some(ndp, ignore_fnames, ignore_rnames): """ Ignores some functionalities or resources """ fnames0 = ndp.get_fnames() rnames0 = ndp.get_rnames() for fname in ignore_fnames: check_isinstance(fname, str) if not fname in fnames0: msg = 'Could not find functionality %r in %r.' % (fname, fnames0) raise_desc(ValueError, msg, fname=fname, fnames=fnames0) for rname in ignore_rnames: check_isinstance(rname, str) if not rname in rnames0: msg = 'Could not find resource %r in %r.' % (rname, rnames0) raise_desc(ValueError, msg, rname=rname, rnames=rnames0) c = Context() orig = '_orig' c.add_ndp(orig, ndp) for fname in ndp.get_fnames(): F = ndp.get_ftype(fname) if fname in ignore_fnames: dp = Constant(F, F.get_bottom()) n = '_const_f_%s' % fname c.add_ndp(n, dpwrap(dp, [], fname)) else: n = c.add_ndp_fun_node(fname, F) con = Connection(n, fname, orig, fname) c.add_connection(con) for rname in ndp.get_rnames(): R = ndp.get_rtype(rname) if rname in ignore_rnames: dp = LimitMaximals(R, R.get_maximal_elements()) n = '_const_r_%s' % rname c.add_ndp(n, dpwrap(dp, rname, [])) else: n = c.add_ndp_res_node(rname, R) con = Connection(orig, rname, n, rname) c.add_connection(con) return CompositeNamedDP.from_context(c)
class CompositeNamedDP(NamedDP): """ The only tricky thing is that if there is only one function, then F = F1 but if there are two, then F = PosetProduct((F1, F2)) Same thing with the resources. """ def __init__(self, context): self.context = Context() self.context.names = context.names.copy() self.context.connections = list(context.connections) self.context.fnames = list(context.fnames) self.context.rnames = list(context.rnames) check_consistent_data(self.context.names, self.context.fnames, self.context.rnames, self.context.connections) self._rnames = list(self.context.rnames) self._fnames = list(self.context.fnames) def __copy__(self): c = Context() c.names = dict(**self.context.names) c.connections = list(self.context.connections) c.fnames = list(self.context.fnames) c.rnames = list(self.context.rnames) return CompositeNamedDP(c) @staticmethod def from_context(context): return CompositeNamedDP(context) @staticmethod def from_parts(name2ndp, connections, fnames, rnames): c = Context() c.names = name2ndp c.connections = connections c.fnames = fnames c.rnames = rnames return CompositeNamedDP(c) @contract(returns='dict(str:$NamedDP)') def get_name2ndp(self): """ Returns the map from name to nodes """ return self.context.names @contract(returns='set($Connection)') def get_connections(self): return set(self.context.connections) @contract(returns='list(str)') def get_fnames(self): """ Returns list of string """ return list(self._fnames) @contract(returns='list(str)') def get_rnames(self): """ Returns list of strings """ return list(self._rnames) # accessories def get_rtype(self, rn): ndp = self.context.get_ndp_res(rn) return ndp.get_rtype(ndp.get_rnames()[0]) def get_ftype(self, fn): ndp = self.context.get_ndp_fun(fn) return ndp.get_ftype(ndp.get_fnames()[0]) def check_fully_connected(self): for name, ndp in self.context.names.items(): try: ndp.check_fully_connected() except NotConnected as e: msg = 'Sub-design problem %r is not connected.' % name raise_wrapped(NotConnected, e, msg, compact=True) from mcdp_lang.blocks import check_missing_connections check_missing_connections(self.context) def compact(self): """ Each set of edges that share both tail and head are replaced by their product. """ from mocdp.comp.composite_compact import compact_context context = compact_context(self.context) return CompositeNamedDP(context) def flatten(self): from mocdp.comp.flattening.flatten import cndp_flatten return cndp_flatten(self) def templatize_children(self): from .composite_templatize import cndp_templatize_children return cndp_templatize_children(self) def abstract(self): if not self.context.names: # this means that there are nor children, nor functions nor resources dp = Mux(PosetProduct(()), ()) ndp = SimpleWrap(dp, fnames=[], rnames=[]) return ndp try: self.check_fully_connected() except NotConnected as e: msg = 'Cannot abstract because not all subproblems are connected.' raise_wrapped(DPSemanticError, e, msg, exc=sys.exc_info(), compact=True) from mocdp.comp.composite_abstraction import cndp_abstract res = cndp_abstract(self) assert isinstance(res, SimpleWrap), type(res) assert res.get_fnames() == self.context.fnames assert res.get_rnames() == self.context.rnames return res def get_dp(self): ndp = self.abstract() dp = ndp.get_dp() return dp def __repr__(self): s = 'CompositeNDP' if hasattr(self, ATTR_LOAD_NAME): s += '\n (loaded as %r)' % getattr(self, ATTR_LOAD_NAME) # if hasattr(self, ATTRIBUTE_NDP_RECURSIVE_NAME): # s += '\n (labeled as %s)' % getattr(self, ATTRIBUTE_NDP_RECURSIVE_NAME).__str__() for f in self._fnames: s += '\n provides %s [%s]' % (f, self.get_ftype(f)) for r in self._rnames: s += '\n requires %s [%s]' % (r, self.get_rtype(r)) s += '\n %d nodes, %d edges' % (len(self.context.names), len(self.context.connections)) s += '\n connections: \n' + format_list_long(self.context.connections, informal=True) s += '\n names: \n' + format_dict_long(self.context.names, informal=True) return s