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 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)