def fix_resources_with_multiple_connections(context): # tuples (ndp, res) from mocdp.comp.connection import find_resources_with_multiple_connections res = find_resources_with_multiple_connections(context.connections) for id_ndp, rname in res: matches = lambda c: c.dp1 == id_ndp and c.s1 == rname its = [c for c in context.connections if matches(c)] assert len(its) >= 2 for c in its: context.connections.remove(c) P = context.get_rtype(CResource(id_ndp, rname)) new_name = context.new_name('_join_fname') dp = MeetNDualDP(n=len(its), P=P) new_connections = [] rnames = [] for i, c in enumerate(its): rn = '_%s_%d' % (rname, i) rnames.append(rn) c2 = Connection(dp1=new_name, s1=rn, dp2=c.dp2, s2=c.s2) new_connections.append(c2) fname = '_a' ndp = dpwrap(dp, fname, rnames) context.add_ndp(new_name, ndp) for c2 in new_connections: context.add_connection(c2) # [ id_ndp : rname ] -> [ new_name ] cc = Connection(dp2=new_name, s2=fname, dp1=id_ndp, s1=rname) context.add_connection(cc)
def fix_functions_with_multiple_connections(context): # tuples (ndp, res) from mocdp.comp.connection import find_functions_with_multiple_connections res = find_functions_with_multiple_connections(context.connections) for id_ndp, fname in res: matches = lambda c: c.dp2 == id_ndp and c.s2 == fname its = [c for c in context.connections if matches(c)] assert len(its) >= 2 for c in its: context.connections.remove(c) P = context.get_ftype(CFunction(id_ndp, fname)) new_name = context.new_name('_join_fname') dp = JoinNDP(n=len(its), P=P) new_connections = [] fnames = [] rname = '_a' for i, c in enumerate(its): fn = '_%s_%d' % (fname, i) fnames.append(fn) c2 = Connection(dp1=c.dp1, s1=c.s1, dp2=new_name, s2=fn) new_connections.append(c2) ndp = dpwrap(dp, fnames, rname) context.add_ndp(new_name, ndp) for c2 in new_connections: context.add_connection(c2) cc = Connection(dp1=new_name, s1=rname, dp2=id_ndp, s2=fname) context.add_connection(cc)
def add_constraint(context, resource, function): check_isinstance(resource, CResource) check_isinstance(function, CFunction) R1 = context.get_rtype(resource) F2 = context.get_ftype(function) tu = get_types_universe() try: if tu.equal(R1, F2): c = Connection(dp1=resource.dp, s1=resource.s, dp2=function.dp, s2=function.s) context.add_connection(c) else: # F2 ---- (<=) ---- becomes ----(<=)--- [R1_to_F2] ---- # | R1 F2 R1 R1 F2 # R1 try: R1_to_F2, F2_to_R1 = tu.get_super_conversion(R1, F2) conversion = Conversion(R1_to_F2, F2_to_R1) assert tu.equal(R1, conversion.get_fun_space()) assert tu.equal(F2, conversion.get_res_space()) resource2 = create_operation(context=context, dp=conversion, resources=[resource], name_prefix='_conversion') c = Connection(dp1=resource2.dp, s1=resource2.s, dp2=function.dp, s2=function.s) context.add_connection(c) except NotLeq as e: msg = 'Constraint between incompatible spaces.' msg += '\n %s can be embedded in %s: %s ' % ( R1, F2, tu.leq(R1, F2)) msg += '\n %s can be embedded in %s: %s ' % ( F2, R1, tu.leq(F2, R1)) raise_wrapped( DPSemanticError, e, msg, R1=R1, F2=F2, compact=True) except NotImplementedError as e: # pragma: no cover msg = 'Problem while creating embedding.' raise_wrapped(DPInternalError, e, msg, resource=resource, function=function, R1=R1, F2=F2)
def create_operation_lf(context, dp, functions, name_prefix=None, op_prefix='_op', res_prefix='_res', allow_conversion=True): if name_prefix is None: name_prefix = '_%s' % type(dp).__name__ name = context.new_name(name_prefix) name_result = context.new_res_name(res_prefix) rnames = [] for i, f in enumerate(functions): ni = context.new_fun_name('%s%s' % (op_prefix, i)) rnames.append(ni) _rnames = rnames[0] if len(rnames) == 1 else rnames ndp = dpwrap(dp, name_result, _rnames) connections = [] tu = get_types_universe() for i, f in enumerate(functions): # source resource Fi = context.get_ftype(f) # function Fhave = ndp.get_rtype(rnames[i]) # print('------- argu %d' % i) # # print('I need to connect function %s of type %s to resource %s of new NDP with type %s'% # (f, Fi, rnames[i], Fhave)) # # print('Fi: %s' % Fi) # print('Fhave: %s' % Fhave) if not tu.equal(Fi, Fhave): if not allow_conversion: msg = ('The types are %s and %s are not equal, and ' 'allow_conversion is False' % (Fi, Fhave)) raise DPInternalError(msg) # print('creating conversion') conversion = get_conversion(Fhave, Fi) if conversion is None: msg = 'I need a conversion from %s to %s' % (Fi, Fhave) raise DPInternalError(msg) else: # print('Conversion: %s' % conversion.repr_long()) # print('Creating recursive...') f = create_operation_lf(context, conversion, [f], name_prefix='_conversion_for_%s' % name_result, allow_conversion=False) c = Connection(dp2=f.dp, s2=f.s, dp1=name, s1=rnames[i]) connections.append(c) context.add_ndp(name, ndp) for c in connections: context.add_connection(c) res = context.make_function(name, name_result) return res
def create_operation(context, dp, resources, name_prefix=None, op_prefix=None, res_prefix=None): """ This is useful to create operations that take possibly many inputs and produce one output. Example use: R = mult_table_seq(resources_types) dp = ProductN(tuple(resources_types), R) from mcdp_lang.helpers import create_operation r = create_operation(context, dp, resources, name_prefix='_prod', op_prefix='_factor', res_prefix='_result') """ if name_prefix is None: name_prefix = '_%s' % type(dp).__name__ # new name for the ndp name = context.new_name(name_prefix) if op_prefix is None: op_prefix = '_op' if res_prefix is None: res_prefix = '_res' name_result = context.new_res_name(res_prefix) connections = [] fnames = [] for i, r in enumerate(resources): ni = context.new_fun_name('%s%s' % (op_prefix, i)) fnames.append(ni) fnames_ = fnames[0] if len(fnames) == 1 else fnames ndp = dpwrap(dp, fnames_, name_result) context.add_ndp(name, ndp) tu = get_types_universe() for i, r in enumerate(resources): # this is where we check for types # source resource R = context.get_rtype(r) # function F = ndp.get_ftype(fnames[i]) if not tu.equal(F, R): conversion = get_conversion(R, F) if conversion is None: msg = 'I need a conversion from %s to %s' % (R, F) raise DPInternalError(msg) else: r = create_operation(context, conversion, [r], name_prefix='_conversion_for_%s' % name_result) R = context.get_rtype(r) assert tu.equal(F, R) c = Connection(dp1=r.dp, s1=r.s, dp2=name, s2=fnames[i]) connections.append(c) for c in connections: context.add_connection(c) res = context.make_resource(name, name_result) return res