Пример #1
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)
Пример #2
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)
Пример #3
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))