Example #1
0
def x_minus_constants(x, constants):
    R0 = x.unit
    
    if not isinstance(R0, RcompUnits):
        msg = 'Cannot evaluate "-" on this space.'
        raise_desc(DPSemanticError, msg, R0=R0)

    Rb = RbicompUnits.from_rcompunits(R0)
    
    # convert each factor to R0
    try:
        v0 = x.value
        for c in constants:
            vi = express_value_in_isomorphic_space(c.unit, c.value, Rb)
            v0 = RbicompUnits_subtract(Rb, v0, vi)
    except TypeError as e:
        msg = 'Failure to compute subtraction.'
        raise_wrapped(DPInternalError, e, msg, x=x, constants=constants)
    
    if Rb.leq(0.0, v0):
        R1 = R0
    else:
        R1 = Rb
        
    return ValueWithUnits(unit=R1, value=v0)
Example #2
0
    def _update_file(self, f):
        basename = os.path.basename(f)
        check_isinstance(basename, str)
        # This will fail because then in pyparsing everything is unicode
        # import codecs
        # data = codecs.open(f, encoding='utf-8').read()
        data = open(f).read()
        realpath = os.path.realpath(f)
        res = dict(data=data, realpath=realpath, path=f)

        strict = False
        if basename in self.file_to_contents:
            realpath1 = self.file_to_contents[basename]["realpath"]
            path1 = self.file_to_contents[basename]["path"]
            if res["realpath"] == realpath1:
                msg = "File %r reached twice." % basename
                if not strict:
                    logger.warning(msg + "\n" + format_obs(dict(path1=path1, path2=res["path"])))
                else:
                    raise_desc(DPSemanticError, msg, path1=path1, path2=res["path"])

            else:
                msg = "Found duplicated file %r." % basename
                if not strict:
                    if log_duplicates:
                        logger.warning(msg + "\n" + format_obs(dict(path1=realpath1, path2=res["realpath"])))
                else:
                    raise_desc(DPSemanticError, msg, path1=realpath1, path2=res["realpath"])

        assert isinstance(basename, str), basename

        self.file_to_contents[basename] = res
Example #3
0
def get_invplus_op(context, lf, c):
    """ (fvalue) + constant >= f """
    ftype = context.get_ftype(lf)

    T1 = ftype
    T2 = c.unit

    if isinstance(T1, Rcomp) and isinstance(T2, Rcomp):
        val = c.value
        dp = MinusValueRcompDP(val)
    elif isinstance(T1, Rcomp) and isinstance(T2, Nat):
        # cast Nat to Rcomp
        val = float(c.value)
        dp = MinusValueRcompDP(val)
    elif isinstance(T1, RcompUnits) and isinstance(T2, RcompUnits):
        dp = MinusValueDP(T1, c.value, T2)
    elif isinstance(T1, Nat) and isinstance(T2, Nat):
        dp = MinusValueNatDP(c.value)
    elif isinstance(T1, Nat) and isinstance(T2, Rcomp):
        # f2 <= required rb + Rcomp:2.3
        dp = MinusValueRcompDP(c.value)
    else:
        msg = (
            'Cannot create inverse addition operation between variable of type %s '
            'and constant of type %s.' % (T1, T2))
        raise_desc(DPInternalError, msg)

    r2 = create_operation_lf(context,
                             dp,
                             functions=[lf],
                             name_prefix='_invplusop')
    return r2
Example #4
0
 def check_leq(self, a, b):
     i, xi = self.unpack(a)
     j, xj = self.unpack(b)
     if i != j:
         msg = 'They belong to different sub-spaces.'
         raise_desc(NotLeq, msg)
     self.spaces[i].check_leq(xi, xj)
Example #5
0
def render_markdown(s):  # pragma: no cover
    """ Returns an HTML string encoded in UTF-8"""
    if isinstance(s, unicode):
        msg = 'I expect utf-8 encoded bytes.'
        raise_desc(TypeError, msg, s=s.__repr__())

    import markdown  # @UnresolvedImport
    import logging
    logging.getLogger("MARKDOWN").setLevel(logging.CRITICAL)

    extensions = [
        'markdown.extensions.smarty',
        #         'markdown.extensions.toc',
        'markdown.extensions.attr_list',
        'markdown.extensions.extra',  # need for markdown=1
        'markdown.extensions.fenced_code',
        'markdown.extensions.admonition',
        'markdown.extensions.tables',
    ]

    # markdown takes and returns unicode
    u = unicode(s, 'utf-8')
    html = markdown.markdown(u, extensions)
    html = html.encode('utf-8')
    return html
 def belongs(self, x):
     if not isinstance(x, FiniteCollection):
         msg = 'Not a finite collection.'
         raise_desc(NotBelongs, msg, x=x)
     if not x.S == self.S:
         msg = 'Different spaces: %s ≠ %s' % (self.S, x.S)
         raise_desc(NotBelongs, msg, x=x)
Example #7
0
def eval_rvalue_approx_step(r, context):
    assert isinstance(r, CDP.ApproxStepRes)

    resource = eval_rvalue(r.rvalue, context)
    step = eval_constant(r.step, context)
    
    R = context.get_rtype(resource)
    tu = get_types_universe()
    try:
        tu.check_leq(step.unit, R)
    except NotLeq:
        msg = ('The step is specified in a unit (%s), which is not compatible '
               'with the resource (%s).' % (step.unit, R))
        raise_desc(DPSemanticError, msg)

    stepu = express_value_in_isomorphic_space(S1=step.unit, s1=step.value, S2=R)

    if not isinstance(R, (RcompUnits)):
        msg = 'approx() not implemented for %s.'%R
        raise_desc(DPNotImplementedError, msg)
        
    dp = makeLinearCeilDP(R, stepu)

    return create_operation(context, dp=dp, resources=[resource],
                               name_prefix='_approx', op_prefix='_toapprox',
                                res_prefix='_result')
Example #8
0
def choose_connection_to_cut1(connections, name2dp):
    G = get_connection_multigraph(connections)

    from collections import defaultdict
    counts = defaultdict(lambda: 0)

    c_as_e = simple_cycles_as_edges(G)
    if not c_as_e:
        msg = 'There are no connections to cut.'
        raise_desc(ValueError, msg)
    
    for cycle in c_as_e:
        for edge in cycle:
            counts[edge] += 1

    ncycles = len(c_as_e)
    best_edge, ncycles_broken = max(list(counts.items()), key=lambda x: x[1])

    def find_one(a, b):
        for c in connections:
            if c.dp1 == a and c.dp2 == b:
                return c
        assert False

    its_connection = find_one(best_edge[0], best_edge[1])
    F = name2dp[its_connection.dp1].get_rtype(its_connection.s1)
    print('Min cut: breaking %d of %d cycles by removing %s, space = %s.' %
        (ncycles_broken, ncycles, str(its_connection), F))
    # print('its connection is %s' % str(its_connection))
    # print('querying F = %s ' % name2dp[its_connection.dp1].get_rtype(its_connection.s1))
    return its_connection
Example #9
0
def get_best_plotter(space):
    p = list(get_plotters(get_all_available_plotters(), space))
    if not p:
        msg = 'Could not find any plotter for space %s.' % space
        raise_desc(ValueError, msg, space=space)
        
    return p[0][1]
Example #10
0
    def _load_hooks(self, load_arg, hooks, expected):
        errors = []
        if not hooks:
            msg = 'Could not load %r because no loading hooks provided.' % load_arg
            raise_desc(DPSemanticError, msg)
        for hook in hooks:
            try:
                try:
                    res = hook(load_arg, context=self)
                    if not isinstance(res, expected):
                        msg = 'The hook did not return the expected type.'
                        raise_desc(DPSemanticError, msg, res=res, expected=expected)
                    return res
                except TypeError:
                    msg = 'Could not use hook %r' % hook
                    logger.error(msg)
                    raise
            except DPSemanticError as e:
                if len(hooks) == 1:
                    raise
                else:
                    errors.append(e)

        s = "\n\n".join(map(str, errors))
        msg = 'Could not load %r: \n%s' % (load_arg, s)
        raise DPSemanticError(msg)
Example #11
0
 def add_ndp(self, name, ndp):
     self.info('Adding name %r = %r' % (name, ndp))
     if name in self.names:
         # where?
         msg = 'Repeated identifier'
         raise_desc(DPInternalError, msg, name=name)
     self.names[name] = ndp
Example #12
0
    def solve_r(self, _r):
        maximals = self.F.get_maximal_elements()
        if not maximals:
            msg = 'No maximal elements for poset %s' % self.R
            raise_desc(DPInternalError, msg, ndp=self)

        return self.F.Ls(maximals)
Example #13
0
    def solve(self, _f):
        minimals = self.R.get_minimal_elements()
        if not minimals: 
            msg = 'No minimal elements for poset %s' % self.R
            raise_desc(DPInternalError, msg, ndp=self)

        return self.R.Us(minimals)
Example #14
0
def check_fails(f, *args, **kwargs):
    try:
        f(*args, **kwargs)
    except BaseException as e:
        logger.error('Known failure for %s ' % f)
        logger.warn('Fails with error %s' % e)
        # comptest_fails = kwargs.get('comptest_fails', f.__name__)
        d = 'out/comptests-failures'
        if not os.path.exists(d):
            os.makedirs(d)
        try:
            from compmake.jobs.job_execution import JobCompute
            job_id = JobCompute.current_job_id
            if job_id is None:
                job_id = 'nojob-%s' % f.__name__
            out = os.path.join(d, job_id + '.txt')
            #         for i in range(1000):
            #             outi = out % i
            #             if not os.path.exists(outi):
            s = traceback.format_exc()
            if isinstance(s, bytes):
                s = s.decode('utf-8', errors='ignore')

            with open(out, 'wb') as f:
                f.write(s.encode('utf-8'))
        except ImportError:
            pass

    else:
        msg = 'Function was supposed to fail.'
        raise_desc(Exception, msg, f=f, args=args, kwargs=kwargs)
Example #15
0
 def belongs(self, x):
     #         if isinstance(x, dict):  # unhashable
     #             msg = 'Value is not hashable.'
     #             raise_desc(NotBelongs, msg, x=x, elements=self.elements)
     if not x in self.elements:
         msg = 'Element does not belong to poset.'
         raise_desc(NotBelongs, msg=msg, x=x, elements=self.elements)
Example #16
0
def eval_template_spec(r, context):

    assert isinstance(r, CDP.TemplateSpec)
    from .eval_ndp_imp import eval_ndp

    params_ops = unwrap_list(r.params)
    if params_ops:
        keys = params_ops[::2]
        values =  params_ops[1::2]
        keys = [_.value for _ in keys]

        if len(set(keys)) != len(keys):
            msg = 'Repeated parameters.'
            raise_desc(DPSemanticError, msg, keys=keys)

        values = [eval_ndp(_, context) for _ in values]
        d = dict(zip(keys, values))
        params = d
    else:
        params = {}
    ndpt = r.ndpt

#     libname = context.get_default_library_name()
#     if libname is None:
#         raise ValueError()
#     print('libname: %s' % libname)
    return TemplateForNamedDP(parameters=params, template_code=ndpt)
Example #17
0
 def belongs(self, x):
     if not isinstance(x, FiniteCollection):
         msg = 'Not a finite collection.'
         raise_desc(NotBelongs, msg, x=x)
     if not x.S == self.S:
         msg = 'Different spaces: %s ≠ %s' % (self.S, x.S)
         raise_desc(NotBelongs, msg, x=x)
Example #18
0
def eval_space(r, context):
    cases = {
        CDP.RcompUnit: eval_space_rcompunit,
        CDP.SpaceProduct: eval_space_spaceproduct,
        CDP.SpaceCoproduct: eval_space_spacecoproduct,
        CDP.PowerSet: eval_space_powerset,
        CDP.LoadPoset: eval_poset_load,
        CDP.FinitePoset: eval_space_finite_poset,
        CDP.CodeSpecNoArgs: eval_space_code_spec,
        CDP.CodeSpec: eval_space_code_spec,
        CDP.MakeUpperSets: eval_space_makeuppersets,
        CDP.MakeLowerSets: eval_space_makelowersets,
        CDP.SpaceInterval: eval_space_interval,
        CDP.ProductWithLabels: eval_space_productwithlabels,
        CDP.SingleElementPoset: eval_space_single_element_poset,
        CDP.Nat: lambda r, context: Nat(),  # @UnusedVariable
        CDP.Int: lambda r, context: Int(),  # @UnusedVariable
        CDP.Rcomp: lambda r, context: Rcomp(),  # @UnusedVariable
        CDP.AddBottom: eval_space_addbottom,
    }

    for klass, hook in cases.items():
        if isinstance(r, klass):
            return hook(r, context)
                        
    # This should be removed...
    if isinstance(r, CDP.Unit):
        return r.value

    if True: # pragma: no cover
        msg = 'eval_space(): Cannot interpret as a space.'
        r = recursive_print(r)
        raise_desc(DPInternalError, msg, r=r)
Example #19
0
    def go(self):

        options = self.get_options()
        if not options.contracts:
            disable_all()
            
        filenames = options.get_extra()
        #print options
        if len(filenames) == 0:
            raise_desc(UserError, 'Need at least one filename.', filenames=filenames)

        if len(filenames) > 1:
            raise_desc(NotImplementedError, 'Want only 1 filename.', filenames=filenames)

        filename = filenames[0]

        if options.out is None:
            out = os.path.dirname(filename)
            if not out:
                out = 'out-mcdp_plot'
        else:
            out = options.out
        mkdirs_thread_safe(out)
        possible = [p for p, _ in allplots]
        plots = expand_string(options.plots, list(possible))
        logger = self.logger  # HasLogger
        maindir = options.maindir
        extra_dirs = options.extra_dirs.split(':')
        use_cache = options.cache
        do_plots(logger, filename, plots, out, options.extra_params,
                maindir=maindir,
                extra_dirs=extra_dirs,
                 use_cache=use_cache) 
Example #20
0
 def check_equal(self, a, b):
     m1 = a.get_elements()
     m2 = b.get_elements()
     mcdp_dev_warning('#XXX should use S.equal()')
     if not (m1 == m2):  # XXX: should use S.equal()...
         msg = "Not equal"
         raise_desc(NotEqual, msg, elements1=m1, elements2=m2)
Example #21
0
def check_uncertainty4():
    """ This will give an error somewhere """
    ndp = parse_ndp("""
mcdp {
    requires r1 [USD]
    r1 >= Uncertain(2 USD, 1 USD)
}

""")
    # > DPSemanticError: Run-time check failed; wrong use of "Uncertain" operator.
    # > l: Instance of <type 'float'>.
    # >    2.0
    # > u: Instance of <type 'float'>.
    # >    1.0
    dp = ndp.get_dp()
    dpl, dpu = get_dp_bounds(dp, 1, 1)

    f = ()
    try:
        dpl.solve(f)
    except WrongUseOfUncertain:
        pass
    else:  # pragma: no cover
        msg = 'Expected WrongUseOfUncertain.'
        raise_desc(Exception, msg)

    try:
        dpu.solve(f)
    except WrongUseOfUncertain:
        pass
    else:  # pragma: no cover
        msg = 'Expected WrongUseOfUncertain.'
        raise_desc(Exception, msg)
Example #22
0
def generic_mult_constantsN(seq):
    """ Multiplies a sequence of constants that could be either Nat, Rcomp, or RCompUnits """
    for c in seq:
        if isinstance(c.unit, RbicompUnits):
            assert c.value < 0
            msg = 'Cannot multiply by negative number %s.' % c
            raise_desc(DPSemanticError, msg)

    posets = [_.unit for _ in seq]
    for p in posets:
        check_isinstance(p, (Nat, Rcomp, RcompUnits))

    promoted, R = generic_mult_table(posets)

    if isinstance(R, Nat):
        values = [_.value for _ in seq]
        from functools import reduce
        res = reduce(Nat_mult_uppersets_continuous, values)
        return ValueWithUnits(res, R)
    else:
        res = 1.0
        for vu, F2 in zip(seq, promoted):
            value2 = express_value_in_isomorphic_space(vu.unit, vu.value, F2)
            if F2.equal(value2, F2.get_top()):
                res = R.get_top()
                break
            res *= value2
        return ValueWithUnits(res, R)
        # XXX needs to check overflow
    return res
Example #23
0
def eval_ndp_compact(r, context):
    ndp = eval_ndp(r.dp_rvalue, context)
    if isinstance(ndp, CompositeNamedDP):
        return ndp.compact()
    else:
        msg = 'Cannot compact primitive NDP.'
        raise_desc(DPSemanticError, msg, ndp=ndp.repr_long())
Example #24
0
def get_job_cache(job_id, db):
    cache_key = job2cachekey(job_id)
    if cache_key in db:
        try:
            cache = db[cache_key]
            assert isinstance(cache, Cache)
        except Exception as e:
            del db[cache_key]
            # also remove user object?
            msg = 'Could not read Cache object for job "%s": %s; deleted.' % (
                job_id, e)
            raise CompmakeException(msg)
        return cache
    else:
        # make sure this is a valid job_id
        # XXX expensive
        # known = all_jobs()
        # if not job_id in known:
        # raise CompmakeException("invalid job %s, I know %s"
        # % (job_id, known)) 
        if not job_exists(job_id, db):
            raise_desc(CompmakeDBError,
                       'Requesting cache for job that does not exist.',
                       job_id=job_id)

        cache = Cache(Cache.NOT_STARTED)
        # we only put it later: NOT_STARTEd == not existent
        # get_compmake_db().set(cache_key, cache)
        return cache
Example #25
0
def eval_ndp_load(r, context):
    check_isinstance(r, CDP.LoadNDP)
    arg = r.load_arg
    check_isinstance(arg, (CDP.NDPName, CDP.NDPNameWithLibrary))

    if isinstance(arg, CDP.NDPNameWithLibrary):
        check_isinstance(arg.library, CDP.LibraryName), arg
        check_isinstance(arg.name, CDP.NDPName), arg
        libname = arg.library.value
        name = arg.name.value
        library = context.load_library(libname)
        # XXX: add warning location?
         
        context2 = context.child()
        res = library.load_ndp(name, context2)

        msg = 'While loading %r from library %r:' % (name, libname)
        warnings_copy_from_child_make_nested2(context, context2, r.where, msg)
            
        return res

    if isinstance(arg, CDP.NDPName):
        name = arg.value
        # XXX: add warning location?
        context2 = context.child()
        ndp = context2.load_ndp(name)
        msg = 'While loading %r:' % (name)
        warnings_copy_from_child_make_nested2(context, context2, r.where, msg)
        return ndp

    if True: # pragma: no cover
        msg = 'Unknown construct.'
        raise_desc(DPInternalError, msg, r=r)
Example #26
0
def get_approx_dp(S, name, approx_perc, approx_abs, approx_abs_S, max_value,
                  max_value_S):
    from mcdp_posets.types_universe import express_value_in_isomorphic_space

    approx_abs_ = express_value_in_isomorphic_space(S1=approx_abs_S,
                                                    s1=approx_abs,
                                                    S2=S)
    max_value_ = express_value_in_isomorphic_space(S1=max_value_S,
                                                   s1=max_value,
                                                   S2=S)

    if approx_perc > 0:  # pragma: no cover
        raise_desc(DPNotImplementedError, 'Approx_perc not implemented')

    dps = []

    if max_value > 0:
        dp_max = FuncNotMoreThan(S, max_value_)
        dps.append(dp_max)

    if approx_abs_ > 0:
        dps.append(makeLinearCeilDP(S, approx_abs_))

    dp = wrap_series(S, dps)

    ndp = dpwrap(dp, name, name)
    return ndp
Example #27
0
def eval_lfunction_invplus_sort_ops(ops, context, wants_constant):
    """
            pos_constants, neg_constants, functions = sort_ops(ops, context)
    """
    from .eval_constant_imp import eval_constant

    pos_constants = []
    neg_constants = []
    functions = []

    for op in ops:
        try:
            x = eval_constant(op, context)
            check_isinstance(x, ValueWithUnits)
            if isinstance(x.unit, (RcompUnits, Rcomp, Nat)):
                pos_constants.append(x)
            elif isinstance(x.unit, RbicompUnits):
                neg_constants.append(x)
            else:
                msg = 'Invalid addition - needs error'
                raise_desc(DPInternalError, msg, x=x)
        except NotConstant as e:
            if wants_constant:
                msg = 'Sum not constant because one op is not constant.'
                raise_wrapped(NotConstant, e, msg, op=op, compact=True)
            x = eval_lfunction(op, context)
            assert isinstance(x, CFunction)
            functions.append(x)

    return pos_constants, neg_constants, functions
Example #28
0
def gg_get_format(gg, data_format):
    from reprep import Report
    r = Report()
    do_dot = data_format == 'dot'
    do_png = data_format == 'png'
    do_pdf = data_format == 'pdf'
    do_svg = data_format == 'svg'

    with timeit_wall('gg_figure %s' % data_format):
        gg_figure(r,
                  'graph',
                  gg,
                  do_dot=do_dot,
                  do_png=do_png,
                  do_pdf=do_pdf,
                  do_svg=do_svg)

    if data_format == 'pdf':
        pdf = r.resolve_url('graph_pdf').get_raw_data()
        return pdf
    elif data_format == 'png':
        png = r.resolve_url('graph/graph').get_raw_data()
        return png
    elif data_format == 'dot':
        dot = r.resolve_url('dot').get_raw_data()
        return dot
    elif data_format == 'svg':
        svg = r.resolve_url('graph_svg').get_raw_data()
        if '<html>' in svg:
            msg = 'I did not expect a tag <html> in the SVG output'
            svg = indent(svg, '> ')
            raise_desc(Exception, msg, svg=svg)
        return svg
    else:
        raise ValueError('No known format %r.' % data_format)
Example #29
0
 def solve_r(self, r):
     if len(self.Fs) > 2:
         msg = 'SumNLDP:solve_r: Cannot invert more than two terms.'
         raise_desc(NotImplementedError, msg)
         
     options = sample_sum_lowersets(self.R, self.F, r, self.n)
     return self.F.Ls(options)
Example #30
0
 def unpack(self, x):
     """ Returns index, and the active element. """
     if not (isinstance(x, tuple) and len(x) == 2 and isinstance(x[0], int)):
         msg = 'This is not a valid element.'
         raise_desc(ValueError, msg, x=x, self=self)
     i, active = x
     return i, active[i]
Example #31
0
def choose_connection_to_cut1(connections, name2dp):
    G = get_connection_multigraph(connections)

    from collections import defaultdict
    counts = defaultdict(lambda: 0)

    c_as_e = simple_cycles_as_edges(G)
    if not c_as_e:
        msg = 'There are no connections to cut.'
        raise_desc(ValueError, msg)

    for cycle in c_as_e:
        for edge in cycle:
            counts[edge] += 1

    ncycles = len(c_as_e)
    best_edge, ncycles_broken = max(list(counts.items()), key=lambda x: x[1])

    def find_one(a, b):
        for c in connections:
            if c.dp1 == a and c.dp2 == b:
                return c
        assert False

    its_connection = find_one(best_edge[0], best_edge[1])
    F = name2dp[its_connection.dp1].get_rtype(its_connection.s1)
    print('Min cut: breaking %d of %d cycles by removing %s, space = %s.' %
          (ncycles_broken, ncycles, str(its_connection), F))
    # print('its connection is %s' % str(its_connection))
    # print('querying F = %s ' % name2dp[its_connection.dp1].get_rtype(its_connection.s1))
    return its_connection
Example #32
0
    def belongs(self, x):
        try:
            if not isinstance(x, tuple) or not len(x) == 2:
                raise NotBelongs()

            n = len(self.spaces)
            label, s = x
            if not label in self.labels:
                msg = 'Unknown label.'
                raise_desc(NotBelongs, msg, label=label, labels=self.labels)

            if not isinstance(s, tuple) or not len(s) == n:
                raise NotBelongs()

            i = self.labels.index(label)

            for j, sj in enumerate(s):
                if j == i:
                    try:
                        self.spaces[j].belongs(sj)
                    except NotBelongs as e:
                        msg = 'Element %d' % j
                        raise_wrapped(NotBelongs, e, msg, j=j, sj=sj,
                                      spacej=self.spaces[j])
                else:
                    if not sj == Coproduct1.fill:
                        msg = 'sj is not fill'
                        raise_desc(NotBelongs, msg, sj=sj)
        except NotBelongs as e:
            msg = ''
            raise_wrapped(NotBelongs, e, msg, x=x)
Example #33
0
    def __init__(self, ndps, labels=None):
        from mcdp_posets.types_universe import get_types_universe
        if not isinstance(ndps, tuple) or not len(ndps) >= 1:
            raise_desc(ValueError, 'Expected a nonempty tuple.', ndps=ndps)

        if labels is not None:
            if not isinstance(labels, tuple) or not len(labels) == len(ndps):
                raise_desc(ValueError, 'Need labels to be consistent',
                           ndps=ndps, labels=labels)
        self.labels = labels

        tu = get_types_universe()
        first = ndps[0]
        ftypes = first.get_ftypes(first.get_fnames())
        rtypes = first.get_rtypes(first.get_rnames())

        for i, ndp in enumerate(ndps):
            ftypes_i = ndp.get_ftypes(ndp.get_fnames())
            rtypes_i = ndp.get_rtypes(ndp.get_rnames())
            name = 'model #%d' % i if not self.labels else self.labels[i].__repr__()
            try:
                tu.check_equal(ftypes, ftypes_i)
            except NotEqual as e:
                msg = 'Cannot create co-product: ftypes of %s do not match the first.' % name
                raise_wrapped(ValueError, e, msg,
                              ftypes=ftypes, ftypes_i=ftypes_i)

            try:
                tu.check_equal(rtypes, rtypes_i)
            except NotEqual as e:
                msg = 'Cannot create co-product: rtypes of %s not match the first.' % name
                raise_wrapped(ValueError, e, msg,
                              rtypes=rtypes, rtypes_i=rtypes_i)

        self.ndps = ndps
Example #34
0
    def belongs(self, x):
        try:
            if not isinstance(x, tuple) or not len(x) == 2:
                raise NotBelongs()

            n = len(self.spaces)
            i, s = x
            if not isinstance(i, int) or not 0 <= i <= n - 1:
                raise NotBelongs()

            if not isinstance(s, tuple) or not len(s) == n:
                raise NotBelongs()
            for j, sj in enumerate(s):
                if j == i:
                    try:
                        self.spaces[j].belongs(sj)
                    except NotBelongs as e:
                        msg = 'Element %d' % j
                        raise_wrapped(NotBelongs, e, msg, j=j, sj=sj,
                                      spacej=self.spaces[j])
                else:
                    if not sj == Coproduct1.fill:
                        msg = 'sj is not fill'
                        raise_desc(NotBelongs, msg, sj=sj)
        except NotBelongs as e:
            msg = ''
            raise_wrapped(NotBelongs, e, msg, x=x)
Example #35
0
 def check_leq(self, a, b):
     e1 = a.elements
     e2 = b.elements
     res = e1.issubset(e2)
     if not res:
         msg = 'Not included'
         raise_desc(NotLeq, msg, e1=e1, e2=e2)
Example #36
0
def eval_space(r, context):
    cases = {
        CDP.RcompUnit: eval_space_rcompunit,
        CDP.SpaceProduct: eval_space_spaceproduct,
        CDP.SpaceCoproduct: eval_space_spacecoproduct,
        CDP.PowerSet: eval_space_powerset,
        CDP.LoadPoset: eval_poset_load,
        CDP.FinitePoset: eval_space_finite_poset,
        CDP.CodeSpecNoArgs: eval_space_code_spec,
        CDP.CodeSpec: eval_space_code_spec,
        CDP.MakeUpperSets: eval_space_makeuppersets,
        CDP.MakeLowerSets: eval_space_makelowersets,
        CDP.SpaceInterval: eval_space_interval,
        CDP.ProductWithLabels: eval_space_productwithlabels,
        CDP.SingleElementPoset: eval_space_single_element_poset,
        CDP.Nat: lambda r, context: Nat(),  # @UnusedVariable
        CDP.Int: lambda r, context: Int(),  # @UnusedVariable
        CDP.Rcomp: lambda r, context: Rcomp(),  # @UnusedVariable
        CDP.AddBottom: eval_space_addbottom,
    }

    for klass, hook in cases.items():
        if isinstance(r, klass):
            return hook(r, context)

    if True: # pragma: no cover
        msg = 'eval_space(): Cannot interpret as a space.'
        r = recursive_print(r)
        raise_desc(DPInternalError, msg, r=r)
 def check_leq(self, a, b):
     e1 = a.elements
     e2 = b.elements
     res = e1.issubset(e2)
     if not res:
         msg = 'Not included'
         raise_desc(NotLeq, msg, e1=e1, e2=e2)
Example #38
0
def parse_pint(s0):
    """ thin wrapper taking care of dollars not recognized """
    check_isinstance(s0, str)
    replacements = {
        '$': ' dollars ',
        '¹': '^1',
        '²': '^2',
        '³': '^3',
        '⁴': '^4',
        '⁵': '^5',
        '⁶': '^6',
        '⁷': '^7',
        '⁸': '^8',
        '⁹': '^9',
    }
    s = s0
    for p, replacement in replacements.items():
        check_isinstance(p, str)
        check_isinstance(replacement, str)
        s = s.replace(p, replacement)
        
    ureg = get_ureg()
    try:
        return ureg.parse_expression(s)
    except UndefinedUnitError as e:
        msg = 'Cannot parse units %r: %s.' % (s0, str(e))
        raise_desc(DPSemanticError, msg)
    except SyntaxError as e:
        msg = 'Cannot parse units %r.' % s0
        raise_wrapped(DPSemanticError, e, msg, compact=True, exc=sys.exc_info())
        # ? for some reason compact does not have effect here
    except Exception as e:
        msg = 'Cannot parse units %r (%s).' % (s0, type(e))
        raise_wrapped(DPSemanticError, e, msg, compact=True, exc=sys.exc_info())
Example #39
0
def generic_mult_constantsN(seq):
    """ Multiplies a sequence of constants that could be either Nat, Rcomp, or RCompUnits """
    for c in seq:
        if isinstance(c.unit, RbicompUnits):
            assert c.value < 0
            msg = "Cannot multiply by negative number %s." % c
            raise_desc(DPSemanticError, msg)

    posets = [_.unit for _ in seq]
    for p in posets:
        check_isinstance(p, (Nat, Rcomp, RcompUnits))

    promoted, R = generic_mult_table(posets)

    if isinstance(R, Nat):
        values = [_.value for _ in seq]
        from functools import reduce

        res = reduce(Nat_mult_uppersets_continuous, values)
        return ValueWithUnits(res, R)
    else:
        res = 1.0
        for vu, F2 in zip(seq, promoted):
            value2 = express_value_in_isomorphic_space(vu.unit, vu.value, F2)
            if F2.equal(value2, F2.get_top()):
                res = R.get_top()
                break
            res *= value2
        return ValueWithUnits(res, R)
        # XXX needs to check overflow
    return res
Example #40
0
def add_mathjax_call(soup):
    head = soup.find('head')
    if not head:
        msg = 'Could not find <head>'
        raise_desc(ValueError, msg, s=str(soup))

    src = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-MML-AM_CHTML'

    config = r"""
 MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'], ],
      displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
      processEscapes: true
    },
    "HTML-CSS": { availableFonts: ["TeX"] }
  });
    """
    script = Tag(name='script')
    script['type'] = 'text/x-mathjax-config'
    script.append(config)
    head.append(script)

    script = Tag(name='script')
    script.attrs['src'] = src

    head.append(script)
Example #41
0
def eval_rvalue_anyofres(r, context):
    from mcdp_posets import FiniteCollectionsInclusion
    from mcdp_posets import FiniteCollection
    from mcdp_dp.dp_constant import ConstantMinimals

    assert isinstance(r, CDP.AnyOfRes)
    constant = eval_constant(r.value, context)
    if not isinstance(constant.unit, FiniteCollectionsInclusion):
        msg = ('I expect that the argument to any-of evaluates to '
              'a finite collection.')
        raise_desc(DPSemanticError, msg, constant=constant)
    assert isinstance(constant.unit, FiniteCollectionsInclusion)
    P = constant.unit.S
    assert isinstance(constant.value, FiniteCollection)

    elements = constant.value.elements
    minimals = poset_minima(elements, P.leq)
    if len(elements) != len(minimals):
        msg = 'The elements are not minimal.'
        raise_desc(DPSemanticError, msg, elements=elements, minimals=minimals)

    dp = ConstantMinimals(R=P, values=minimals)
    return create_operation(context, dp=dp, resources=[],
                               name_prefix='_anyof', op_prefix='_',
                                res_prefix='_result')
Example #42
0
 def validate(self, data):
     if self.can_be_none and (data is None):
         return
     
     if not isinstance(data, str):
         msg = 'Expected a string object.'
         raise_desc(NotValid, msg, data=describe_value(data))
Example #43
0
def check_editor_response(filename, source, libname):  # @UnusedVariable
    if libname in ['loading_python', 'making']:
        # mcdplib-loading_python-source_mcdp-load1.mcdp-check_editor_response
        # mcdplib-making-source_mcdp-test1.mcdp-check_editor_response
        return
    library = get_test_library(libname)
    string = source
    spec = filename2spec(filename)

    key = ()
    cache = {}
    make_relative = lambda x: x
    res = process_parse_request(library, string, spec, key, cache,
                                make_relative)

    if res['ok']:

        if 'highlight' in res:
            check_isinstance(res['highlight'], unicode)

    else:
        forgive = ['Could not find file', 'DPNotImplementedError']

        if any(_ in res['error'] for _ in forgive):
            pass
        else:
            msg = 'Failed'
            raise_desc(ValueError, msg, source=source, res=res)
Example #44
0
    def _load_hooks(self, load_arg, hooks, expected):
        errors = []
        if not hooks:
            msg = 'Could not load %r because no loading hooks provided.' % load_arg
            raise_desc(DPSemanticError, msg)
        for hook in hooks:
            try:
                try:
                    res = hook(load_arg, context=self)
                    if not isinstance(res, expected):
                        msg = 'The hook did not return the expected type.'
                        raise_desc(DPSemanticError,
                                   msg,
                                   res=res,
                                   expected=expected)
                    return res
                except TypeError:
                    msg = 'Could not use hook %r' % hook
                    logger.error(msg)
                    raise
            except DPSemanticError as e:
                if len(hooks) == 1:
                    raise
                else:
                    errors.append(e)

        s = "\n\n".join(map(str, errors))
        msg = 'Could not load %r: \n%s' % (load_arg, s)
        raise DPSemanticError(msg)
 def belongs(self, x):
     #         if isinstance(x, dict):  # unhashable
     #             msg = 'Value is not hashable.'
     #             raise_desc(NotBelongs, msg, x=x, elements=self.elements)
     if not x in self.elements:
         msg = "Element does not belong to poset."
         raise_desc(NotBelongs, msg=msg, x=x, elements=self.elements)
Example #46
0
 def add_ndp(self, name, ndp):
     self.info('Adding name %r = %r' % (name, ndp))
     if name in self.names:
         # where?
         msg = 'Repeated identifier'
         raise_desc(DPInternalError, msg, name=name)
     self.names[name] = ndp
Example #47
0
def eval_ndp_instancefromlibrary(r, context):
    msg = 'Construct "new X" is deprecated in favor of "instance `X".'
    warn_language(r, MCDPWarnings.LANGUAGE_CONSTRUCT_DEPRECATED, msg, context)

    check_isinstance(r, CDP.DPInstanceFromLibrary)
    check_isinstance(r.dpname, (CDP.NDPName, CDP.NDPNameWithLibrary))
    arg = r.dpname

    if isinstance(arg, CDP.NDPNameWithLibrary):
        check_isinstance(arg.library, CDP.LibraryName), arg
        check_isinstance(arg.name, CDP.NDPName), arg

        libname = arg.library.value
        name = arg.name.value

        library = context.load_library(libname)

        context2 = context.child()
        res = library.load_ndp(name, context2)
        msg = 'While loading %r from library %r:' % (name, libname)
        warnings_copy_from_child_make_nested2(context, context2, r.where, msg)
        return res

    if isinstance(arg, CDP.NDPName):
        name = arg.value
        ndp = context.load_ndp(name)
        return ndp

    if True:  # pragma: no cover
        msg = 'Unknown construct.'
        raise_desc(DPInternalError, msg, r=r)
Example #48
0
    def __init__(self, dps):
        check_isinstance(dps, tuple)
        tu = get_types_universe()

        F1 = dps[0].get_fun_space()
        R1 = dps[0].get_res_space()

        for dp in dps:
            Fj = dp.get_fun_space()
            Rj = dp.get_res_space()

            try:
                tu.check_equal(F1, Fj)
                tu.check_equal(R1, Rj)
            except NotEqual:
                msg = 'Cannot form the co-product.'
                raise_desc(ValueError, msg, dps=dps)

        F = F1
        R = R1
        Ms = [dp.get_imp_space() for dp in dps]

        self.dps = dps
        self.M = Coproduct1(tuple(Ms))
        PrimitiveDP.__init__(self, F=F, R=R, I=self.M)
Example #49
0
def eval_ndp_compact(r, context):
    ndp = eval_ndp(r.dp_rvalue, context)
    if isinstance(ndp, CompositeNamedDP):
        return ndp.compact()
    else:
        msg = 'Cannot compact primitive NDP.'
        raise_desc(DPSemanticError, msg, ndp=ndp.repr_long())
Example #50
0
    def get_implementations_f_r(self, f, r):
        """ Returns a nonempty set of elements of self.M.
            Might raise NotFeasible() """
        res = set()
        es = []
        ms = None
        for j, dp in enumerate(self.dps):
            try:
                ms = dp.get_implementations_f_r(f, r)
                # print('%s: dp.get_implementations_f_r(f, r) = %s ' % (j, ms))
                for m in ms:
                    if do_extra_checks():
                        Mj = dp.get_imp_space()
                        try:
                            Mj.belongs(m)
                        except NotBelongs:
                            raise ValueError(dp)
                    res.add(self.M.pack(j, m))
            except NotFeasible as e:
                es.append(e)
        if not ms:
            # no one was feasible
            msg = 'None was feasible'
            msg += '\n\n' + '\n\n'.join(str(e) for e in es)
            raise_desc(NotFeasible, msg, f=f, r=r, self=self)

        if do_extra_checks():
            for _ in res:
                self.M.belongs(_)

        return res
Example #51
0
def eval_ndp_instancefromlibrary(r, context):
    msg = 'Construct "new X" is deprecated in favor of "instance `X".'
    warn_language(r, MCDPWarnings.LANGUAGE_CONSTRUCT_DEPRECATED, msg, context)
    
    check_isinstance(r, CDP.DPInstanceFromLibrary)
    check_isinstance(r.dpname, (CDP.NDPName, CDP.NDPNameWithLibrary))
    arg = r.dpname

    if isinstance(arg, CDP.NDPNameWithLibrary):
        check_isinstance(arg.library, CDP.LibraryName), arg
        check_isinstance(arg.name, CDP.NDPName), arg

        libname = arg.library.value
        name = arg.name.value
        
        library = context.load_library(libname)
        
        context2 = context.child()
        res = library.load_ndp(name, context2)
        msg = 'While loading %r from library %r:' % (name, libname)
        warnings_copy_from_child_make_nested2(context, context2, r.where, msg)
        return res

    if isinstance(arg, CDP.NDPName):
        name = arg.value
        ndp = context.load_ndp(name)
        return ndp

    if True: # pragma: no cover
        msg = 'Unknown construct.'
        raise_desc(DPInternalError, msg, r=r)
Example #52
0
def eval_constant_SimpleValue(op, context):
    from .eval_space_imp import eval_space  # @Reimport
    F = eval_space(op.space, context)
    assert isinstance(F, Space), op
    assert isinstance(F, RcompUnits)

    v = op.value.value

    # promote integer to float
    if isinstance(v, int) and isinstance(F, (Rcomp, RcompUnits)):
        v = float(v)

    if v < 0:
        if isinstance(F, RcompUnits):
            F = RbicompUnits(F.units, F.string)
        else:
            msg = 'Negative %s not implemented yet.' % F
            raise_desc(NotImplementedError, msg, F=F)

    try:
        F.belongs(v)
    except NotBelongs as e:
        msg = 'Not in space'
        raise_wrapped(DPSemanticError, e, msg, F=F, v=v, op=op)
    return ValueWithUnits(unit=F, value=v)
Example #53
0
def check_get_id_indices(a, res):
    got = get_id_indices(a)
    if got != res:
        msg = 'Result is different'
        raise_desc(ValueError, msg, a=a, res=res, got=got)

    # now compute the composition
    from mcdp_posets.poset_product import PosetProduct
    if isinstance(a, PosetProduct):
        reducel = PosetProduct
    else:
        reducel = list
    try:
        a2 = get_it(a, got, reducel)
    except Exception as e:
        raise_wrapped(ValueError,
                      e,
                      "invalid index produced",
                      a=a,
                      res=res,
                      got=got)

    if str(a2) != str(a):
        msg = 'Not invertible'
        raise_desc(ValueError, msg, a=a, res=res, got=got, a2=a2)
Example #54
0
def eval_solve_r(op, context):
    check_isinstance(op, CDP.SolveRModel)
    from .eval_ndp_imp import eval_ndp

    ndp = eval_ndp(op.model, context)
    dp = ndp.get_dp()
    r0 = eval_constant(op.r, context)
    F = dp.get_fun_space()
    R = dp.get_res_space()

    tu = get_types_universe()
    try:
        tu.check_leq(r0.unit, R)
    except NotLeq as e:
        msg = 'Input not correct.'
        raise_wrapped(DPSemanticError, e, msg, compact=True)
    r = r0.cast_value(R)

    res = dp.solve_r(r)
    try:
        LF = LowerSets(F)
        return ValueWithUnits(res, LF)
    except NotBelongs as e:
        msg = 'Invalid result of solve_r().'
        raise_desc(DPInternalError, msg, res=res, dp=dp.repr_long())
Example #55
0
def eval_lfunction_invplus(lf, context):
    ops = get_odd_ops(unwrap_list(lf.ops))
    pos_constants, neg_constants, functions = \
        eval_lfunction_invplus_sort_ops(ops, context, wants_constant=False)

    if neg_constants:
        msg = 'Inverse plus of negative constants not implemented yet.'
        raise_desc(DPNotImplementedError, msg)

    constants = pos_constants

    try:
        if len(functions) == 0:
            c = plus_constantsN(constants)
            return get_valuewithunits_as_function(c, context)

        elif len(functions) == 1:
            if len(constants) > 0:
                c = plus_constantsN(constants)
                return get_invplus_op(context, functions[0], c)
            else:
                return functions[0]
        else:
            # there are some functions
            r = eval_lfunction_invplus_ops(functions, context)
            if not constants:
                return r
            else:
                c = plus_constantsN(constants)
                return get_invplus_op(context, r, c)
    except ConstantsNotCompatibleForAddition as e:
        msg = 'Incompatible units for addition.'
        raise_wrapped(DPSemanticError, e, msg, compact=True)
Example #56
0
File: plot.py Project: kannode/mcdp
def do_plots_ndp(model_name, library, plots, extra_params):
    possible = [p for p, _ in allplots]
    plots = expand_string(plots, list(possible))

    filename = model_name + '.mcdp'
    x = library._get_file_data(filename)
    data = {}
    data['model_name'] = model_name

    data['s'] = x['data']
    data['filename'] = x['realpath']
    data['params'] = parse_params(extra_params)
    data['library'] = library

    d = dict(allplots)
    results = []
    for p in plots:
        try:
            if p in d:
                res = d[p](data)
            else:
                msg = 'Unknown plot.'
                raise_desc(ValueError, msg, plot=p, available=sorted(d.keys()))
                return  # XXX
        except CmdException as e:
            mcdp_dev_warning('Add better checks of error.')
            logger.error(e)
            continue
        except Exception as e:
            logger.error('While creating %r' % p)
            raise
        assert isinstance(res, list), res
        results.extend(res)

    return results
Example #57
0
def eval_lfunction_invmult_ops(fs, context):
    if len(fs) == 1:
        return fs[0]
    elif len(fs) > 2:
        mcdp_dev_warning('Maybe this should be smarter?')
        rest = eval_lfunction_invmult_ops(fs[1:], context)
        return eval_lfunction_invmult_ops([fs[0], rest], context)
    else:
        assert len(fs) == 2
        Fs = tuple(map(context.get_ftype, fs))

        if isinstance(Fs[0], Nat) and isinstance(Fs[1], Nat):
            dp = InvMult2Nat(Nat(), Fs)
        else:
            if isinstance(Fs[0], RcompUnits) and \
               isinstance(Fs[1], RcompUnits):
                R = mult_table(Fs[0], Fs[1])
                dp = InvMult2(R, Fs)
            elif isinstance(Fs[0], Rcomp) and isinstance(Fs[1], Rcomp):
                R = Rcomp()
                dp = InvMult2(R, Fs)
            else:
                msg = 'Could not create invmult for types {}.'.format(Fs)
                raise_desc(DPNotImplementedError, msg, Fs0=Fs[0], Fs1=Fs[1])

        return create_operation_lf(context,
                                   dp=dp,
                                   functions=fs,
                                   name_prefix='_invmult',
                                   op_prefix='_ops',
                                   res_prefix='_result')
Example #58
0
    def __init__(self, string, character, character_end=None):
        from contracts.utils import raise_desc
        if not isinstance(string, six.string_types):
            msg = 'I expect the string to be a str, not %r' % string
            raise ValueError(msg)

        if not (0 <= character <= len(string)):
            msg = ('Invalid character loc %s for string of len %s.' %
                   (character, len(string)))
            raise_desc(ValueError, msg, string=string.__repr__())

        self.line, self.col = line_and_col(character, string)

        if character_end is not None:
            if not (0 <= character_end <= len(string)):
                msg = ('Invalid character_end loc %s for string of len %s.' %
                       (character_end, len(string)))

                raise_desc(ValueError, msg, string=string.__repr__())

            if not (character_end >= character):
                msg = 'Invalid interval [%d:%d]' % (character, character_end)
                raise ValueError(msg)

            self.line_end, self.col_end = line_and_col(character_end, string)
        else:
            self.line_end, self.col_end = None, None

        self.string = string
        self.character = character
        self.character_end = character_end
        self.filename = None
Example #59
0
def get_job_cache(job_id, db):
    cache_key = job2cachekey(job_id)
    if cache_key in db:
        try:
            cache = db[cache_key]
            assert isinstance(cache, Cache)
        except Exception as e:
            del db[cache_key]
            # also remove user object?
            msg = 'Could not read Cache object for job "%s": %s; deleted.' % (
                job_id, e)
            raise CompmakeException(msg)
        return cache
    else:
        # make sure this is a valid job_id
        # XXX expensive
        # known = all_jobs()
        # if not job_id in known:
        # raise CompmakeException("invalid job %s, I know %s"
        # % (job_id, known))
        if not job_exists(job_id, db):
            raise_desc(CompmakeDBError,
                       'Requesting cache for job that does not exist.',
                       job_id=job_id)

        cache = Cache(Cache.NOT_STARTED)
        # we only put it later: NOT_STARTEd == not existent
        # get_compmake_db().set(cache_key, cache)
        return cache
Example #60
0
def eval_PlusN_sort_ops(ops, context, wants_constant):
    """
            pos_constants, neg_constants, resources = sort_ops(ops, context)
    """
    from .eval_constant_imp import eval_constant

    pos_constants = []
    neg_constants = []
    resources = []

    for op in ops:
        try:
            x = eval_constant(op, context)
            check_isinstance(x, ValueWithUnits)

            if isinstance(x.unit, (RcompUnits, Rcomp, Nat)):
                pos_constants.append(x)
            elif isinstance(x.unit, RbicompUnits):
                neg_constants.append(x)
            else:
                msg = 'Cannot use the type %s in a sum.' % x.unit
                raise_desc(DPInternalError, msg, x=x)
                
        except NotConstant as e:
            if wants_constant:
                msg = 'Sum not constant because one op is not constant.'
                raise_wrapped(NotConstant, e, msg, op=op)
            x = eval_rvalue(op, context)
            assert isinstance(x, CResource)
            resources.append(x)
            
    return pos_constants, neg_constants, resources