def get_lower_bound_ndp(context): """ We create an NDP where each open resource becomes a new resource. and all the functions are given a lower bound of 0. The new resources are given a name like: dummy<i> """ context = clone_context(context) unconnected_fun, unconnected_res = get_missing_connections(context) # let's remove the new resources that are unconnected for rname, name, _ndp in context.iterate_new_resources(): if (name, rname) in unconnected_fun: unconnected_fun.remove((name, rname)) del context.names[name] context.rnames.remove(rname) # create a new resource for each unconnected resource resource2var = {} # CResource -> str for dp, s in unconnected_res: r = CResource(dp, s) R = context.get_rtype(r) rname = 'dummy%d' % len(resource2var) context.add_ndp_res_node(rname, R) c = Connection(dp1=dp, s1=s, dp2=get_name_for_res_node(rname), s2=rname) context.add_connection(c) resource2var[r] = rname # add a minimal bound for all unconnected functions for dp, s in unconnected_fun: f = CFunction(dp, s) F = context.get_ftype(f) minimals = F.get_minimal_elements() res = get_constant_minimals_as_resources(F, minimals, context) c = Connection(dp1=res.dp, s1=res.s, dp2=dp, s2=s) context.add_connection(c) ndp = CompositeNamedDP.from_context(context) ndp.check_fully_connected() return ndp, resource2var
def ifun_finish(self): """ Searches for all the automatically created DPs and closes them by adding 0 constants. """ from mcdp_lang.helpers import get_valuewithunits_as_resource from mcdp_lang.helpers import get_constant_minimals_as_resources def connectedfun(ndp_name, s): assert ndp_name in self.names assert s in self.names[ndp_name].get_fnames() for c in self.connections: if c.dp2 == ndp_name and c.s2 == s: return True return False for which, created in self.indexed_fun.items(): ndp = self.names[created] for fname in ndp.get_fnames(): connected = connectedfun(created, fname) if not connected: F = ndp.get_ftype(fname) if not self._can_ignore_unconnected(F): msg = 'Missing value %r for %r.' % (fname, which) raise_desc(DPSemanticError, msg) else: msg = 'Using default value for unconnected resource %s %s' % ( created, fname) # logger.warn(msg) try: zero = F.get_bottom() vu = ValueWithUnits(value=zero, unit=F) res = get_valuewithunits_as_resource(vu, self) except NotBounded: minimals = F.get_minimal_elements() res = get_constant_minimals_as_resources( F, minimals, self) c = Connection(dp1=res.dp, s1=res.s, dp2=created, s2=fname) self.add_connection(c)
def ifun_finish(self): """ Searches for all the automatically created DPs and closes them by adding 0 constants. """ from mcdp_lang.helpers import get_valuewithunits_as_resource from mcdp_lang.helpers import get_constant_minimals_as_resources def connectedfun(ndp_name, s): assert ndp_name in self.names assert s in self.names[ndp_name].get_fnames() for c in self.connections: if c.dp2 == ndp_name and c.s2 == s: return True return False for which, created in self.indexed_fun.items(): ndp = self.names[created] for fname in ndp.get_fnames(): connected = connectedfun(created, fname) if not connected: F = ndp.get_ftype(fname) if not self._can_ignore_unconnected(F): msg = 'Missing value %r for %r.' % (fname, which) raise_desc(DPSemanticError, msg) else: msg = 'Using default value for unconnected resource %s %s' % (created, fname) # logger.warn(msg) try: zero = F.get_bottom() vu = ValueWithUnits(value=zero, unit=F) res = get_valuewithunits_as_resource(vu, self) except NotBounded: minimals = F.get_minimal_elements() res = get_constant_minimals_as_resources(F, minimals, self) c = Connection(dp1=res.dp, s1=res.s, dp2=created, s2=fname) self.add_connection(c)