def cndp_iterate_fun_nodes(cndp): res = [] for name2, ndp2 in cndp.get_name2ndp().items(): isitr, fname = is_fun_node_name(name2) if isitr: res.append((fname, ndp2)) return res
def transform(name2, ndp2): # Returns name, ndp isf, fname = is_fun_node_name(name2) isr, rname = is_res_node_name(name2) if isf or isr: if isinstance(ndp2, SimpleWrap): if isf: fnames = "%s%s%s" % (prefix, sep, fname) rnames = "%s%s%s" % (prefix, sep, fname) if isr: fnames = "%s%s%s" % (prefix, sep, rname) rnames = "%s%s%s" % (prefix, sep, rname) dp = ndp2.dp res = SimpleWrap(dp=dp, fnames=fnames, rnames=rnames) elif isinstance(ndp2, LabelerNDP): ndp_inside = transform(name2, ndp2.ndp) return LabelerNDP(ndp_inside, ndp2.recursive_name) else: raise NotImplementedError(type(ndp2)) else: res = flatten_add_prefix(ndp2, prefix) return res
def get_new_name(name2): isf, fname = is_fun_node_name(name2) isr, rname = is_res_node_name(name2) if isf: return get_name_for_fun_node('%s%s%s' % (prefix, sep, fname)) elif isr: return get_name_for_res_node('%s%s%s' % (prefix, sep, rname)) else: return "%s%s%s" % (prefix, sep, name2)
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 cndp_get_name_ndp_notfunres(cndp): """ Yields a sequence of (name, ndp) excluding the fake ones that represent function or resource. """ assert isinstance(cndp, CompositeNamedDP) from mocdp.comp.context import is_res_node_name res = [] for name2, ndp2 in cndp.get_name2ndp().items(): isitf, _ = is_fun_node_name(name2) isitr, _ = is_res_node_name(name2) if isitf or isitr: # do not add the identity nodes # that represent functions or resources continue else: res.append((name2, ndp2)) return res
def check_consistent_data(names, fnames, rnames, connections): from mocdp.comp.context import get_name_for_res_node, get_name_for_fun_node from mocdp.comp.context import is_res_node_name from mcdp_posets.types_universe import get_types_universe tu = get_types_universe() for n in names: try: check_good_name(n) except ValueError as e: msg = 'This name is not good.' raise_wrapped(ValueError, e, msg, names=names) isit, x = is_fun_node_name(n) if isit and not x in fnames: msg = 'The name for the node seems to be the one for a function.' raise_desc(ValueError, msg, n=n, fnames=fnames) isit, x = is_res_node_name(n) if isit and not x in rnames: if not n in rnames: msg = 'The name for the node seems to be the one for a resource.' raise_desc(ValueError, msg, n=n, rnames=rnames) for f in fnames: fnode = get_name_for_fun_node(f) if not fnode in names: msg = 'Expecting to see a node with the name of the function.' raise_desc(ValueError, msg, f=f, names=list(names.keys())) fn = names[fnode] if not f in fn.get_fnames(): msg = ('Expecting to see the special function node have function ' 'with function name.') raise_desc(ValueError, msg, f=f, fnode=fnode, fn=fn, fn_fnames=fn.get_fnames()) for r in rnames: rnode = get_name_for_res_node(r) if not rnode in names: msg = 'Expecting to see a node with the name of the resource.' raise_desc(ValueError, msg, r=r, names=list(names.keys())) rn = names[rnode] if not r in rn.get_rnames(): msg = ('Expecting to see the special resource node have resource ' 'with resource name.') raise_desc(ValueError, msg, r=r, rnode=rnode, rn=rn, rn_rnames=rn.get_rnames()) for c in connections: try: if not c.dp1 in names: raise_desc(ValueError, 'First DP %r not found.' % c.dp1, name=c.dp1, available=list(names)) if not c.s1 in names[c.dp1].get_rnames(): raise_desc(ValueError, 'Resource %r of first DP %r not found' % (c.s1, c.dp1), rname=c.s1, available=names[c.dp1].get_rnames()) if not c.dp2 in names: raise_desc(ValueError, 'Second DP %r not found.' % c.dp2, name=c.dp2, available=list(names)) if not c.s2 in names[c.dp2].get_fnames(): raise_desc(ValueError, 'Function %r of second DP %r not found.' % (c.s2, c.dp2), s2=c.s2, available=names[c.dp2].get_fnames()) R = names[c.dp1].get_rtype(c.s1) F = names[c.dp2].get_ftype(c.s2) try: tu.check_equal(R, F) except NotEqual as e: msg = 'Invalid connection %s' % c.__repr__() raise_wrapped(ValueError, e, msg, R=R, F=F) except ValueError as e: msg = 'Invalid connection %s.' % (c.__repr__()) raise_wrapped(ValueError, e, msg, compact=True)
def create_composite_(gdc0, ndp, plotting_info, SKIP_INITIAL): try: assert isinstance(ndp, CompositeNamedDP) # names2functions[name][fn] = item # names2resources[name][rn] = item names2resources = defaultdict(lambda: {}) names2functions = defaultdict(lambda: {}) if gdc0.should_I_enclose(ndp): if gdc0.yourname is None: container_label = '' else: if gdc0.yourname and gdc0.yourname[0] == '_': container_label = '' else: container_label = gdc0.yourname c = gdc0.newItem(container_label) gdc0.styleApply('container', c) gdc = gdc0.child_context(parent=c, yourname=gdc0.yourname) else: gdc = gdc0 for name, value in ndp.context.names.items(): # do not create these edges if SKIP_INITIAL: if is_function_with_one_connection_that_is_not_a_res_one( ndp, name): # print('Skipping extra node for is_function_with_one_connection %r' % name) # warnings.warn('hack') continue if SKIP_INITIAL: if is_resource_with_one_connection_that_is_not_a_fun_one( ndp, name): # print('skipping extra node for %r' % name) # warnings.warn('hack') continue if False: # this makes the nodes appear as red dots if is_function_with_no_connections(ndp, name): # only draw the balloon item = gdc.newItem("%s" % name) gdc.styleApply('unconnected', item) for fn in value.get_fnames(): names2functions[name][fn] = item for rn in value.get_rnames(): names2resources[name][rn] = item continue if is_resource_with_no_connections(ndp, name): # only draw the balloon instead of "Identity" node item = gdc.newItem("%s" % name) gdc.styleApply('unconnected', item) for fn in value.get_fnames(): names2functions[name][fn] = item for rn in value.get_rnames(): names2resources[name][rn] = item continue with gdc.child_context_yield(yourname=name, parent=gdc.parent) as child: plotting_info2 = RecursiveEdgeLabeling(plotting_info, name) f, r = create(child, value, plotting_info=plotting_info2) # print('name %s -> functions %s , resources = %s' % (name, list(f), list(r))) names2resources[name] = r names2functions[name] = f for rn in names2resources[name]: if resource_has_more_than_one_connected(ndp, name, rn): # create new splitter orig = names2resources[name][rn] split = gdc.newItem('') gdc.styleApply('splitter', split) l = gdc.newLink(orig, split) gdc.gg.propertyAppend(l, "constraint", "false") gdc.gg.propertyAppend(l, "weight", "0") gdc.styleApply('splitter_link', l) gdc.decorate_arrow_resource(l) names2resources[name][rn] = split ignore_connections = set() if SKIP_INITIAL: for name, value in ndp.context.names.items(): if is_function_with_one_connection_that_is_not_a_res_one( ndp, name): only_one = get_connections_to_function(ndp, name)[0] ignore_connections.add(only_one) if not only_one.dp2 in names2functions: msg = ( 'Cannot find function node ref for %r' % only_one.dp2 + ' while drawing one connection %s' % str(only_one)) # warnings.warn('giving up') # continue raise_desc(ValueError, msg, names=list(ndp.context.names), names2functions=list(names2functions)) node = names2functions[only_one.dp2][only_one.s2] names2functions[name][only_one.s1] = node # XXX: not really sure names2resources[name][only_one.s1] = node for name, value in ndp.context.names.items(): if is_resource_with_one_connection_that_is_not_a_fun_one( ndp, name): only_one = get_connections_to_resource(ndp, name)[0] ignore_connections.add(only_one) if not only_one.dp1 in names2resources: # warnings.warn('giving up') # continue raise ValueError( 'Cannot find function node ref for %r' % only_one.dp1 + ' while drawing one connection %s' % str(only_one)) node = names2resources[only_one.dp1][only_one.s1] names2resources[name][only_one.s2] = node # XXX: not really sure names2functions[name][only_one.s2] = node for c in ndp.context.connections: if c in ignore_connections: # print('ignoring connection %s' % str(c)) continue dpa = names2functions[c.dp2] n_a = dpa[c.s2] dpb = names2resources[c.dp1] n_b = dpb[c.s1] skip = gdc.should_I_skip_leq(ndp.context, c) ndp_first = ndp.context.names[c.dp1] ndp_second = ndp.context.names[c.dp2] second_simple = is_simple(ndp_second) first_simple = is_simple(ndp_first) any_simple = second_simple or first_simple both_simple = second_simple and first_simple ua = ndp.context.names[c.dp2].get_ftype(c.s2) ub = ndp.context.names[c.dp1].get_rtype(c.s1) if skip: label = get_signal_label(c.s1, ub) l1 = gdc.newLink(n_b, n_a, label=label) else: box = gdc.newItem('') # '≼') # LEQ rel_to_8 = MCDPConstants.diagrams_fontsize / 8 diagrams_leqimagesize = MCDPConstants.diagrams_leqimagesize_rel * \ rel_to_8 gdc.gg.propertyAppend(box, 'height', diagrams_leqimagesize) gdc.styleApply("leq", box) l1_label = get_signal_label(c.s2, ua) dec = plotting_info.get_fname_label(ndp_name=(c.dp2, ), fname=c.s2) if dec is not None: l1_label = get_signal_label_namepart(c.s2) + '\n' + dec if isinstance(ndp_second, SimpleWrap) and isinstance( ndp_second.dp, ResourceNode): l1_label = 'required ' + l1_label # print('Creating label with %r %s' % l1_label) l1 = gdc.newLink(box, n_a, label=l1_label) # if False: # gdc.gg.propertyAppend(l1, "headport", "w") l2_label = get_signal_label(c.s1, ub) if isinstance(ndp_first, SimpleWrap) and isinstance( ndp_first.dp, FunctionNode): l2_label = 'provided ' + l2_label dec = plotting_info.get_rname_label(ndp_name=(c.dp1, ), rname=c.s1) if dec is not None: l2_label = get_signal_label_namepart(c.s1) + '\n' + dec l2 = gdc.newLink(n_b, box, label=l2_label) # if False: # gdc.gg.propertyAppend(l2, "tailport", "e") # # if False: # gdc.gg.propertyAppend(l1, 'constraint', 'false') # gdc.gg.propertyAppend(l2, 'constraint', 'false') if both_simple: weight = 0 elif any_simple: weight = 0.5 else: weight = 1 if any_simple: gdc.gg.propertyAppend(l2, 'weight', '%s' % weight) gdc.gg.propertyAppend(l1, 'weight', '%s' % weight) # gdc.gg.propertyAppend(l2, 'color', 'blue') # gdc.gg.propertyAppend(l1, 'color', 'blue') gdc.decorate_arrow_function(l1) gdc.decorate_arrow_resource(l2) unconnected_fun, unconnected_res = get_missing_connections(ndp.context) for (dp, fn) in unconnected_fun: x = gdc.newItem('') gdc.styleApply("unconnected_node", x) n = names2functions[dp][fn] F = ndp.context.names[dp].get_ftype(fn) label = get_signal_label(fn, F) it_is, _ = is_res_node_name(dp) if it_is: label = 'required ' + label l = gdc.newLink(x, n, label=label) gdc.decorate_arrow_function(l) # XXX? gdc.styleApply('unconnected_link', l) for (dp, rn) in unconnected_res: x = gdc.newItem('') gdc.styleApply("unconnected_node", x) n = names2resources[dp][rn] R = ndp.context.names[dp].get_rtype(rn) label = get_signal_label(rn, R) it_is, _ = is_fun_node_name(dp) if it_is: label = 'provided ' + label l = gdc.newLink(n, x, label=label) gdc.decorate_arrow_resource(l) # XXX? gdc.styleApply('unconnected_link', l) functions = {} resources = {} for rname in ndp.get_rnames(): name = get_name_for_res_node(rname) resources[rname] = list(names2resources[name].values())[0] for fname in ndp.get_fnames(): name = get_name_for_fun_node(fname) functions[fname] = list(names2functions[name].values())[0] if not (gdc is gdc0): gdc0.all_nodes.extend(gdc.all_nodes) return functions, resources except BaseException as e: raise msg = 'Could not draw diagram.' raise_wrapped(Exception, e, msg, names2functions=names2functions, names2resources=names2resources, ndp=ndp)
def create_composite_(gdc0, ndp, plotting_info, SKIP_INITIAL): try: assert isinstance(ndp, CompositeNamedDP) # names2functions[name][fn] = item # names2resources[name][rn] = item names2resources = defaultdict(lambda: {}) names2functions = defaultdict(lambda: {}) if gdc0.should_I_enclose(ndp): if gdc0.yourname is None: container_label = '' else: if gdc0.yourname and gdc0.yourname[0] == '_': container_label = '' else: container_label = gdc0.yourname c = gdc0.newItem(container_label) gdc0.styleApply('container', c) gdc = gdc0.child_context(parent=c, yourname=gdc0.yourname) else: gdc = gdc0 for name, value in ndp.context.names.items(): # do not create these edges if SKIP_INITIAL: if is_function_with_one_connection_that_is_not_a_res_one(ndp, name): # print('Skipping extra node for is_function_with_one_connection %r' % name) # warnings.warn('hack') continue if SKIP_INITIAL: if is_resource_with_one_connection_that_is_not_a_fun_one(ndp, name): # print('skipping extra node for %r' % name) # warnings.warn('hack') continue if False: # this makes the nodes appear as red dots if is_function_with_no_connections(ndp, name): # only draw the balloon item = gdc.newItem("%s" % name) gdc.styleApply('unconnected', item) for fn in value.get_fnames(): names2functions[name][fn] = item for rn in value.get_rnames(): names2resources[name][rn] = item continue if is_resource_with_no_connections(ndp, name): # only draw the balloon instead of "Identity" node item = gdc.newItem("%s" % name) gdc.styleApply('unconnected', item) for fn in value.get_fnames(): names2functions[name][fn] = item for rn in value.get_rnames(): names2resources[name][rn] = item continue with gdc.child_context_yield(yourname=name, parent=gdc.parent) as child: plotting_info2 = RecursiveEdgeLabeling(plotting_info, name) f, r = create(child, value, plotting_info=plotting_info2) # print('name %s -> functions %s , resources = %s' % (name, list(f), list(r))) names2resources[name] = r names2functions[name] = f for rn in names2resources[name]: if resource_has_more_than_one_connected(ndp, name, rn): # create new splitter orig = names2resources[name][rn] split = gdc.newItem('') gdc.styleApply('splitter', split) l = gdc.newLink(orig, split) gdc.gg.propertyAppend(l, "constraint", "false") gdc.gg.propertyAppend(l, "weight", "0") gdc.styleApply('splitter_link', l) gdc.decorate_arrow_resource(l) names2resources[name][rn] = split ignore_connections = set() if SKIP_INITIAL: for name, value in ndp.context.names.items(): if is_function_with_one_connection_that_is_not_a_res_one(ndp, name): only_one = get_connections_to_function(ndp, name)[0] ignore_connections.add(only_one) if not only_one.dp2 in names2functions: msg = ('Cannot find function node ref for %r' % only_one.dp2 + ' while drawing one connection %s' % str(only_one)) # warnings.warn('giving up') # continue raise_desc(ValueError, msg, names=list(ndp.context.names), names2functions=list(names2functions)) node = names2functions[only_one.dp2][only_one.s2] names2functions[name][only_one.s1] = node # XXX: not really sure names2resources[name][only_one.s1] = node for name, value in ndp.context.names.items(): if is_resource_with_one_connection_that_is_not_a_fun_one(ndp, name): only_one = get_connections_to_resource(ndp, name)[0] ignore_connections.add(only_one) if not only_one.dp1 in names2resources: # warnings.warn('giving up') # continue raise ValueError('Cannot find function node ref for %r' % only_one.dp1 + ' while drawing one connection %s' % str(only_one)) node = names2resources[only_one.dp1][only_one.s1] names2resources[name][only_one.s2] = node # XXX: not really sure names2functions[name][only_one.s2] = node for c in ndp.context.connections: if c in ignore_connections: # print('ignoring connection %s' % str(c)) continue dpa = names2functions[c.dp2] n_a = dpa[c.s2] dpb = names2resources[c.dp1] n_b = dpb[c.s1] skip = gdc.should_I_skip_leq(ndp.context, c) ndp_first = ndp.context.names[c.dp1] ndp_second = ndp.context.names[c.dp2] second_simple = is_simple(ndp_second) first_simple = is_simple(ndp_first) any_simple = second_simple or first_simple both_simple = second_simple and first_simple ua = ndp.context.names[c.dp2].get_ftype(c.s2) ub = ndp.context.names[c.dp1].get_rtype(c.s1) if skip: l1 = gdc.newLink(n_b, n_a , label=get_signal_label(c.s1, ub)) else: box = gdc.newItem('') # '≼') gdc.styleApply("leq", box) l1_label = get_signal_label(c.s2, ua) dec = plotting_info.get_fname_label(ndp_name=(c.dp2,), fname=c.s2) if dec is not None: l1_label = get_signal_label_namepart(c.s2) + '\n' + dec if isinstance(ndp_second, SimpleWrap) and isinstance(ndp_second.dp, ResourceNode): l1_label = 'required ' + l1_label # print('Creating label with %r %s' % l1_label) l1 = gdc.newLink(box, n_a , label=l1_label) # if False: # gdc.gg.propertyAppend(l1, "headport", "w") l2_label = get_signal_label(c.s1, ub) if isinstance(ndp_first, SimpleWrap) and isinstance(ndp_first.dp, FunctionNode): l2_label = 'provided ' + l2_label dec = plotting_info.get_rname_label(ndp_name=(c.dp1,), rname=c.s1) if dec is not None: l2_label = get_signal_label_namepart(c.s1) + '\n' + dec l2 = gdc.newLink(n_b, box, label=l2_label) # if False: # gdc.gg.propertyAppend(l2, "tailport", "e") # # if False: # gdc.gg.propertyAppend(l1, 'constraint', 'false') # gdc.gg.propertyAppend(l2, 'constraint', 'false') if both_simple: weight = 0 elif any_simple: weight = 0.5 else: weight = 1 if any_simple: gdc.gg.propertyAppend(l2, 'weight', '%s' % weight) gdc.gg.propertyAppend(l1, 'weight', '%s' % weight) # gdc.gg.propertyAppend(l2, 'color', 'blue') # gdc.gg.propertyAppend(l1, 'color', 'blue') gdc.decorate_arrow_function(l1) gdc.decorate_arrow_resource(l2) unconnected_fun, unconnected_res = get_missing_connections(ndp.context) for (dp, fn) in unconnected_fun: x = gdc.newItem('') gdc.styleApply("unconnected_node", x) n = names2functions[dp][fn] F = ndp.context.names[dp].get_ftype(fn) label = get_signal_label(fn, F) it_is, _ = is_res_node_name(dp) if it_is: label = 'required ' + label l = gdc.newLink(x, n, label=label) gdc.decorate_arrow_function(l) # XXX? gdc.styleApply('unconnected_link', l) for (dp, rn) in unconnected_res: x = gdc.newItem('') gdc.styleApply("unconnected_node", x) n = names2resources[dp][rn] R = ndp.context.names[dp].get_rtype(rn) label = get_signal_label(rn, R) it_is, _ = is_fun_node_name(dp) if it_is: label = 'provided ' + label l = gdc.newLink(n, x, label=label) gdc.decorate_arrow_resource(l) # XXX? gdc.styleApply('unconnected_link', l) functions = {} resources = {} for rname in ndp.get_rnames(): name = get_name_for_res_node(rname) resources[rname] = list(names2resources[name].values())[0] for fname in ndp.get_fnames(): name = get_name_for_fun_node(fname) functions[fname] = list(names2functions[name].values())[0] if not (gdc is gdc0): gdc0.all_nodes.extend(gdc.all_nodes) return functions, resources except BaseException as e: raise msg = 'Could not draw diagram.' raise_wrapped(Exception, e, msg, names2functions=names2functions, names2resources=names2resources, ndp=ndp)
def cndp_flatten(ndp): check_isinstance(ndp, CompositeNamedDP) name2ndp = ndp.get_name2ndp() connections = ndp.get_connections() fnames = ndp.get_fnames() rnames = ndp.get_rnames() names2 = {} connections2 = [] proxy_functions = defaultdict(lambda: dict()) # proxy[name][fname] proxy_resources = defaultdict(lambda: dict()) for name, n0 in name2ndp.items(): # add empty (in case they have no functions/resources) proxy_functions[name] = {} proxy_resources[name] = {} n1 = n0.flatten() if isinstance(n1, CompositeNamedDP): nn = flatten_add_prefix(n1, prefix=name) else: nn = n1 if isinstance(nn, CompositeNamedDP): # nn_connections = nn.get_connections() for name2, ndp2 in nn.get_name2ndp().items(): assert not name2 in names2 isitf, is_fname = is_fun_node_name(name2) isitr, is_rname = is_res_node_name(name2) if (isitf or isitr): # connected_to_something = is_dp_connected(name2, nn_connections) # do not add the identity nodes # that represent functions or resources # except if they are unconnected # add_identities = not connected_to_something add_identities = True if not add_identities: # pragma: no cover continue else: # think of the case where there are a f and r with # same name if isitf: use_name = is_fname + '_f' if isitr: use_name = is_rname + '_r' assert not use_name in names2, use_name names2[use_name] = ndp2 if isitf: proxy_functions[name][is_fname] = ( use_name, ndp2.get_fnames()[0]) if isitr: proxy_resources[name][is_rname] = ( use_name, ndp2.get_rnames()[0]) else: assert not name2 in names2 names2[name2] = ndp2 for c in nn.get_connections(): # is it a connection to a function if False: isitf, fn = is_fun_node_name(c.dp1) isitr, rn = is_res_node_name(c.dp2) if isitf and isitr: # This is the case where there is a straight connection # from function to resource: # # f = instance mcdp { # provides a [dimensionless] # requires c [dimensionless] # # c >= a # } # In this case, we need to add an identity new_name = '_%s_pass_through_%s' % (name, c.s2) F = nn.get_name2ndp()[c.dp1].get_ftype(c.s1) ndp_pass = SimpleWrap(Identity(F), fnames=fn, rnames=rn) assert not new_name in names2 names2[new_name] = ndp_pass proxy_functions[name][fn] = (new_name, fn) proxy_resources[name][rn] = (new_name, rn) continue if isitf: proxy_functions[name][fn] = (c.dp2, c.s2) continue if isitr: proxy_resources[name][rn] = (c.dp1, c.s1) continue isitf, fn = is_fun_node_name(c.dp1) if isitf: dp1 = fn + '_f' assert dp1 in names2 else: dp1 = c.dp1 isitr, rn = is_res_node_name(c.dp2) if isitr: dp2 = rn + '_r' assert dp2 in names2 else: dp2 = c.dp2 assert dp1 in names2 assert dp2 in names2 assert c.s1 in names2[dp1].get_rnames() assert c.s2 in names2[dp2].get_fnames() c2 = Connection(dp1=dp1, s1=c.s1, dp2=dp2, s2=c.s2) connections2.append(c2) else: for fn in nn.get_fnames(): proxy_functions[name][fn] = (name, fn) for rn in nn.get_rnames(): proxy_resources[name][rn] = (name, rn) assert not name in names2 names2[name] = nn # check that these were correctly generated: # proxy_functions # proxy_resources def exploded(name): return isinstance(name2ndp[name], CompositeNamedDP) # print list(proxy_resources) errors = [] for name, n0 in name2ndp.items(): try: assert name in proxy_functions, name assert name in proxy_resources if exploded(name): for fname in n0.get_fnames(): newfname = "%s/%s" % (name, fname) assert newfname in proxy_functions[name], ( newfname, proxy_functions[name]) for rname in n0.get_rnames(): newrname = "%s/%s" % (name, rname) assert newrname in proxy_resources[name], ( newrname, proxy_resources[name]) else: for fname in n0.get_fnames(): assert fname in proxy_functions[name], ( fname, proxy_functions[name]) for rname in n0.get_rnames(): assert rname in proxy_resources[name] except Exception as e: # pragma: no cover s = '%s:\n %s %s \n\n%s' % (name, proxy_resources[name], proxy_functions[name], e) errors.append(s) if errors: # pragma: no cover s = "\n\n".join(errors) s += '%s %s' % (proxy_resources, proxy_functions) raise Exception(s) for c in connections: dp2 = c.dp2 s2 = c.s2 dp1 = c.dp1 s1 = c.s1 dp2_was_exploded = isinstance(name2ndp[dp2], CompositeNamedDP) if dp2_was_exploded: if not dp2 in proxy_functions: # pragma: no cover msg = 'Bug: cannot find dp2.' raise_desc(DPInternalError, msg, dp2=dp2, keys=list(proxy_functions), c=c) (dp2_, s2_) = proxy_functions[dp2]["%s/%s" % (dp2, s2)] if not dp2_ in names2: # pragma: no cover raise_desc(DPInternalError, "?", dp2_=dp2_, c=c, names2=sorted(names2)) else: dp2_ = dp2 s2_ = s2 dp1_was_exploded = isinstance(name2ndp[dp1], CompositeNamedDP) if dp1_was_exploded: (dp1_, s1_) = proxy_resources[dp1]["%s/%s" % (dp1, s1)] else: dp1_ = dp1 s1_ = s1 assert dp1_ in names2 assert s1_ in names2[dp1_].get_rnames(), (s1_, names2[dp1_].get_rnames()) assert dp2_ in names2 assert s2_ in names2[dp2_].get_fnames(), (s2_, names2[dp2_].get_fnames()) c2 = Connection(dp1=dp1_, s1=s1_, dp2=dp2_, s2=s2_) connections2.append(c2) ndp_res = CompositeNamedDP.from_parts(names2, connections2, fnames, rnames) ndp_simplified = simplify_identities(ndp_res) return ndp_simplified
def check_consistent_data(names, fnames, rnames, connections): from mocdp.comp.context import get_name_for_res_node, get_name_for_fun_node from mocdp.comp.context import is_res_node_name from mcdp_posets.types_universe import get_types_universe tu = get_types_universe() for n in names: try: check_good_name(n) except ValueError as e: msg = 'This name is not good.' raise_wrapped(ValueError, e, msg, names=names) isit, x = is_fun_node_name(n) if isit and not x in fnames: msg = 'The name for the node seems to be the one for a function.' raise_desc(ValueError, msg, n=n, fnames=fnames) isit, x = is_res_node_name(n) if isit and not x in rnames: if not n in rnames: msg = 'The name for the node seems to be the one for a resource.' raise_desc(ValueError, msg, n=n, rnames=rnames) for f in fnames: fnode = get_name_for_fun_node(f) if not fnode in names: msg = 'Expecting to see a node with the name of the function.' raise_desc(ValueError, msg, f=f, names=list(names.keys())) fn = names[fnode] if not f in fn.get_fnames(): msg = ('Expecting to see the special function node have function ' 'with function name.') raise_desc(ValueError, msg, f=f, fnode=fnode, fn=fn, fn_fnames=fn.get_fnames()) for r in rnames: rnode = get_name_for_res_node(r) if not rnode in names: msg = 'Expecting to see a node with the name of the resource.' raise_desc(ValueError, msg, r=r, names=list(names.keys())) rn = names[rnode] if not r in rn.get_rnames(): msg = ('Expecting to see the special resource node have resource ' 'with resource name.') raise_desc(ValueError, msg, r=r, rnode=rnode, rn=rn, rn_rnames=rn.get_rnames()) for c in connections: try: if not c.dp1 in names: raise_desc(ValueError, 'First DP %r not found.' % c.dp1, name=c.dp1, available=list(names)) if not c.s1 in names[c.dp1].get_rnames(): raise_desc(ValueError, 'Resource %r of first DP %r not found' %( c.s1, c.dp1), rname=c.s1, available=names[c.dp1].get_rnames()) if not c.dp2 in names: raise_desc(ValueError, 'Second DP %r not found.' % c.dp2, name=c.dp2, available=list(names)) if not c.s2 in names[c.dp2].get_fnames(): raise_desc(ValueError, 'Function %r of second DP %r not found.' % (c.s2, c.dp2), s2=c.s2, available=names[c.dp2].get_fnames()) R = names[c.dp1].get_rtype(c.s1) F = names[c.dp2].get_ftype(c.s2) try: tu.check_equal(R, F) except NotEqual as e: msg = 'Invalid connection %s' % c.__repr__() raise_wrapped(ValueError, e, msg, R=R, F=F) except ValueError as e: msg = 'Invalid connection %s.' % (c.__repr__()) raise_wrapped(ValueError, e, msg, compact=True)