Exemplo n.º 1
0
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)
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
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
Exemplo n.º 6
0
    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))
Exemplo n.º 7
0
def dpgraph_making_sure_no_reps(context):
    from mocdp.comp.connection import dpgraph
    # functionname -> list of names that use it
    functions = defaultdict(lambda: list())
    resources = defaultdict(lambda: list())

    for fname, name, ndp in context.iterate_new_functions():
        assert fname == ndp.get_fnames()[0]
        functions[fname].append(name)

    for rname, name, ndp in context.iterate_new_resources():
        assert rname == ndp.get_rnames()[0]
        resources[rname].append(name)

    for name, ndp in context.names.items():
        if context.is_new_function(name):
            continue
        for fn in ndp.get_fnames():

            if len(functions[fn]) != 0:
                # print('need to translate F (%s, %s) because already in %s' %
                #       (name, fn, functions[fn]))

                fn2 = '_%s_%s' % (name, fn)

                return dpgraph_translate_fn(context, name, fn, fn2)

            functions[fn].append(name)

    for name, ndp in context.names.items():
        if context.is_new_resource(name):
            continue
        for rn in ndp.get_rnames():

            if len(resources[rn]) != 0:
                # print('need to translate R (%s, %s) because already in %s' %
                #        (name, rn, resources[rn]))

                rn2 = '_%s_%s' % (name, rn)

                return dpgraph_translate_rn(context, name, rn, rn2)

            resources[rn].append(name)

    name2npd = context.names
    connections = context.connections
    fnames = context.fnames
    rnames = context.rnames

    # check if there are disconnected components
    cndp = CompositeNamedDP.from_context(context)

    n = cndp_num_connected_components(cndp)
    if n == 1:
        # This is the base case
        res0 = dpgraph(name2npd, connections, split=[])

        res = make_sure_order_functions_and_resources(res0, fnames, rnames)
        return res
    else:
        # This is the more complicated case
        cndp_list = cndp_split_in_components(cndp)
        assert len(cndp_list) == n

        # Now call each one in turn
        simple_wraps = []
        for cndp in cndp_list:
            dpi = cndp_dpgraph(cndp)
            assert isinstance(dpi, SimpleWrap)
            simple_wraps.append(dpi)

        final = ndps_parallel(simple_wraps)
        res = make_sure_order_functions_and_resources(final, fnames, rnames)

        assert isinstance(res, SimpleWrap), type(res)
        return res
Exemplo n.º 8
0
 def get_current_ndp(self):
     return CompositeNamedDP.from_context(self.context)
Exemplo n.º 9
0
 def get_current_ndp(self):
     return CompositeNamedDP.from_context(self.context)