Exemplo n.º 1
0
def check_tuples2():
    s = "<1g, 5J>"
    parsed = parse_wrap(Syntax.rvalue, s)[0]
    context = Context()
    _ret = eval_rvalue(parsed, context)
    #print(ret)

    same("take(<1g, 5J>, 1)", "5 J")
Exemplo n.º 2
0
def check_tuples2():
    
    #s =     "take(<1g, 5J>, 1)"

    s = "<1g, 5J>"
    parsed = parse_wrap(Syntax.rvalue, s)[0]
    context = Context()
    ret = eval_rvalue(parsed, context)
    print(ret)

    same("take(<1g, 5J>, 1)", "5 J")
Exemplo n.º 3
0
def eval_statement_ResShortcut2(r, context):
    # requires rname >= (rvalue)    
    from mcdp_lang.eval_resources_imp import eval_rvalue

    if isinstance(r.prep, CDP.geq):
        msg = 'This is deprecated, and should be "=".'
        warn_language(r.prep, MCDPWarnings.LANGUAGE_CONSTRUCT_DEPRECATED, msg, context)

    A = eval_rvalue(r.rvalue, context)
    check_isinstance(A, CResource)
    R = context.get_rtype(A)
    B = add_resource(r.rname.value, R, context, r)
    # B >= A
    add_constraint(context, resource=A, function=B)
Exemplo n.º 4
0
def eval_statement_ResShortcut2(r, context):
    # requires rname >= (rvalue)
    from mcdp_lang.eval_resources_imp import eval_rvalue

    if isinstance(r.prep, CDP.geq):
        msg = 'This is deprecated, and should be "=".'
        warn_language(
            r.prep, MCDPWarnings.LANGUAGE_CONSTRUCT_DEPRECATED, msg, context)

    A = eval_rvalue(r.rvalue, context)
    check_isinstance(A, CResource)
    R = context.get_rtype(A)
    B = add_resource(r.rname.value, R, context, r)
    # B >= A
    add_constraint(context, resource=A, function=B)
def eval_rvalue_MakeTuple(mt, context):
    from mcdp_lang.eval_resources_imp import eval_rvalue

    ops = get_odd_ops(unwrap_list(mt.ops))
    resources = [eval_rvalue(_, context) for _ in ops]
    Fs = [context.get_rtype(_) for _ in resources]
    F = PosetProduct(tuple(Fs))

    # Now it's easy - this corresponds to a simple Mux operation
    n = len(Fs)
    coords = list(range(n))
    dp = TakeFun(F, coords)

    return create_operation(context, dp=dp, resources=resources,
                            name_prefix='_make_tuple', op_prefix='_factors',
                            res_prefix='_result')
Exemplo n.º 6
0
def eval_rvalue_MakeTuple(mt, context):
    from mcdp_lang.eval_resources_imp import eval_rvalue

    ops = get_odd_ops(unwrap_list(mt.ops))
    resources = [eval_rvalue(_, context) for _ in ops]
    Fs = [context.get_rtype(_) for _ in resources]
    F = PosetProduct(tuple(Fs))

    # Now it's easy - this corresponds to a simple Mux operation
    n = len(Fs)
    coords = list(range(n))
    dp = TakeFun(F, coords)

    return create_operation(context,
                            dp=dp,
                            resources=resources,
                            name_prefix='_make_tuple',
                            op_prefix='_factors',
                            res_prefix='_result')
Exemplo n.º 7
0
def eval_statement(r, context):
    check_isinstance(context, ModelBuildingContext)
    from .eval_resources_imp import eval_rvalue
    from .eval_lfunction_imp import eval_lfunction

    invalid = (CDP.ConstraintInvalidRR, 
               CDP.ConstraintInvalidFF, 
               CDP.ConstraintInvalidSwapped)
    
    if isinstance(r, invalid):
        msg = 'This constraint is invalid. '
        
        if isinstance(r, CDP.ConstraintInvalidRR):
            msg += 'Both sides are resources.'
        if isinstance(r, CDP.ConstraintInvalidFF):
            msg += 'Both sides are functionalities.'
        if isinstance(r, CDP.ConstraintInvalidSwapped):
            msg += ('Functionality and resources are on the wrong side '
                    'of the inequality.')

        raise DPSemanticError(msg)

    if isinstance(r, Connection):
        context.add_connection(r)

    elif isinstance(r, CDP.Constraint):
        resource = eval_rvalue(r.rvalue, context)
        function = eval_lfunction(r.fvalue, context)
        try:
            add_constraint(context, resource, function)
        except MCDPExceptionWithWhere as e:
            _, _, tb = sys.exc_info()
            where = r.prep.where # indicate preposition "<="
            raise_with_info(e, where, tb)
            
    elif isinstance(r, CDP.VarStatement):
        P = eval_space(r.unit, context)
 
        vnames = get_odd_ops(unwrap_list(r.vnames))
        for v in vnames:
            vname = v.value
            where = v.where
            add_variable(vname, P, where, context)
       
    elif isinstance(r, CDP.SetNameNDPInstance):
        name = r.name.value
        ndp = eval_ndp(r.dp_rvalue, context)
        context.add_ndp(name, ndp)

    elif isinstance(r, CDP.SetNameMCDPType):
        name = r.name.value
        right_side = r.right_side
        x = eval_ndp(right_side, context)
        context.set_var2model(name, x)

    elif isinstance(r, CDP.SetNameRValue):
        return eval_statement_SetNameRValue(r, context)
    elif isinstance(r, CDP.SetNameConstant):
        return eval_statement_SetNameConstant(r, context)
    elif isinstance(r, CDP.SetNameFValue):
        return eval_statement_SetNameFvalue(r,context)

    elif isinstance(r, CDP.IgnoreFun):
        # equivalent to f >= any-of(Minimals S)
        lf = eval_lfunction(r.fvalue, context)
        F = context.get_ftype(lf)
        values = F.get_minimal_elements()
        
        dp = ConstantMinimals(F, values)
        ndp = SimpleWrap(dp, fnames=[], rnames='_out')
        name = context.new_name('_constant')
        context.add_ndp(name, ndp)
        r = context.make_resource(name, '_out')
        add_constraint(context, resource=r, function=lf)

    elif isinstance(r, CDP.IgnoreRes):
        # equivalent to r <= any-of(Maximals S)
        rv = eval_rvalue(r.rvalue, context)
        R = context.get_rtype(rv)
        try:
            values = R.get_maximal_elements()
        except NotImplementedError as e: # pragma: no cover
            msg = 'Could not call get_maximal_elements().'
            raise_wrapped(DPInternalError, e, msg, R=R)

        dp = LimitMaximals(R, values)
        ndp = SimpleWrap(dp, fnames='_limit', rnames=[])
        name = context.new_name('_limit')
        context.add_ndp(name, ndp)
        f = context.make_function(name, '_limit')
        add_constraint(context, resource=rv, function=f)
    else: 

        cases = {
            CDP.ResStatement: eval_statement_ResStatement,
            CDP.FunStatement: eval_statement_FunStatement,
            
            CDP.FunShortcut5: eval_statement_FunShortcut5,
            CDP.ResShortcut5: eval_statement_ResShortcut5,
            CDP.ResShortcut4: eval_statement_ResShortcut4,
            CDP.FunShortcut4: eval_statement_FunShortcut4,
        
            CDP.FunShortcut1m: eval_statement_FunShortcut1m,
            CDP.ResShortcut1m: eval_statement_ResShortcut1m,
            
            CDP.FunShortcut2: eval_statement_FunShortcut2,
            CDP.ResShortcut2: eval_statement_ResShortcut2,
                        
            CDP.FunShortcut1: eval_statement_FunShortcut1,
            CDP.ResShortcut1: eval_statement_ResShortcut1,
                  
        }
        
        for klass, hook in cases.items():
            if isinstance(r, klass):
                return hook(r, context)

        if True: # pragma: no cover
            msg = 'eval_statement(): cannot interpret.'
            r2 = recursive_print(r)
            raise_desc(DPInternalError, msg, r=r2) # where=r.where.__repr__())
Exemplo n.º 8
0
def eval_statement_SetNameRValue(r, context):
    """ 
        This is a special case, because it is the place
        where the syntax is ambiguous.
        
            x = Nat: 1 + r
        
        could be interpreted with r being a functionality
        or a resource.
        
        By default it is parsed as SetNameRValue, and so we get here.
        
        We check whether it could be parsed as setname_fvalue,
        and warn about that.
         
    """
    from .eval_resources_imp import eval_rvalue
    from .eval_constant_imp import NotConstant
    from .eval_lfunction_imp import eval_lfunction
    from .syntax import Syntax
        
    check_isinstance(r, CDP.SetNameRValue)

    # Check to see if this could have been interpreted using
    #    setname_fvalue    
    # Try to have an alternative parsing of the string as 
    #     Syntax.setname_fvalue
    try:
        w = r.where
        s = w.string[w.character:w.character_end]
        alt = parse_wrap(Syntax.setname_fvalue, s)[0]
        # print('alternative: %s' % recursive_print(alt))
    except Exception as _: # XXX: which one?
        #print "No, it does not parse: %s" % traceback.format_exc(e)
        alt = None
        
        
    name = r.name.value
    right_side = r.right_side

    if name in context.constants:
        msg = 'Constant %r already set.' % name
        raise DPSemanticError(msg, where=r.where)

    if name in context.var2resource:
        msg = 'Resource %r already set.' % name
        raise DPSemanticError(msg, where=r.where)

    if name in context.var2function:
        msg = 'Name %r already used.' % name
        raise DPSemanticError(msg, where=r.where)

    try:
        x = eval_constant(right_side, context)
        context.set_constant(name, x)
        used_constant = True
    except NotConstant:
        used_constant = False
        try:
            x = eval_rvalue(right_side, context)
            ndp1 = context.names[x.dp]
            current = x.s
            updated = name
            try: 
                ndp2 = ndp_rename_resource(ndp1, current=current, updated=updated)
                context.names[x.dp] = ndp2
                x = CResource(x.dp, updated)
            except CouldNotRename:
                pass
            
            context.set_var2resource(name, x)
            used_rvalue = True
            
        except DPSemanticError as e:
            if 'not declared' in str(e) and alt is not None:
                # XXX: this seems not to be used anymore
                # after we implemented the interpretation at the syntax level
                raise NotImplementedError
                x = eval_lfunction(alt.right_side, context)
                context.set_var2function(name, x)
                used_rvalue = False
            else:
                raise
            
    if alt is not None:
        msg = ('This expression could be parsed both as a functionality '
               'and as a resource.')
        if used_constant:
            pass
        #             msg += ' I parsed it as a constant.'
        else:
            if used_rvalue:
                msg += ' I parsed it as a resource.'
            else:
                msg += ' I parsed it as a function.'
          
                warn_language(r, MCDPWarnings.LANGUAGE_AMBIGUOS_EXPRESSION, 
                              msg, context)
Exemplo n.º 9
0
def eval_statement_SetNameRValue(r, context):
    """ 
        This is a special case, because it is the place
        where the syntax is ambiguous.

            x = Nat: 1 + r

        could be interpreted with r being a functionality
        or a resource.

        By default it is parsed as SetNameRValue, and so we get here.

        We check whether it could be parsed as setname_fvalue,
        and warn about that.

    """
    from .eval_resources_imp import eval_rvalue
    from .eval_constant_imp import NotConstant
    from .syntax import Syntax

    check_isinstance(r, CDP.SetNameRValue)

    # Check to see if this could have been interpreted using
    #    setname_fvalue
    # Try to have an alternative parsing of the string as
    #     Syntax.setname_fvalue
    try:
        w = r.where
        s = w.string[w.character:w.character_end]
        alt = parse_wrap(Syntax.setname_fvalue, s)[0]
        # print('alternative: %s' % recursive_print(alt))
    except Exception as _:  # XXX: which one?
        # print "No, it does not parse: %s" % traceback.format_exc(e)
        alt = None

    name = r.name.value
    right_side = r.right_side

    if name in context.constants:
        msg = 'Constant %r already set.' % name
        raise DPSemanticError(msg, where=r.where)

    if name in context.var2resource:
        msg = 'Resource %r already set.' % name
        raise DPSemanticError(msg, where=r.where)

    if name in context.var2function:
        msg = 'Name %r already used.' % name
        raise DPSemanticError(msg, where=r.where)

    try:
        x = eval_constant(right_side, context)
        context.set_constant(name, x)
        used_constant = True
    except NotConstant:
        used_constant = False
        try:
            x = eval_rvalue(right_side, context)
            ndp1 = context.names[x.dp]
            current = x.s
            updated = name
            try:
                ndp2 = ndp_rename_resource(
                    ndp1, current=current, updated=updated)
                context.names[x.dp] = ndp2
                x = CResource(x.dp, updated)
            except CouldNotRename:
                pass

            context.set_var2resource(name, x)
            used_rvalue = True

        except DPSemanticError:  # as e:
            #             if 'not declared' in str(e) and alt is not None:
            #                 # XXX: this seems not to be used anymore
            #                 # after we implemented the interpretation at the syntax level
            #                 msg = 'This should not happen...'
            #                 raise_wrapped(DPNotImplementedError, e, msg)
            #                 x = eval_lfunction(alt.right_side, context)
            #                 context.set_var2function(name, x)
            #                 used_rvalue = False
            #             else:
            raise

    if alt is not None:
        msg = ('This expression could be parsed both as a functionality '
               'and as a resource.')
        if used_constant:
            pass
        #             msg += ' I parsed it as a constant.'
        else:
            if used_rvalue:
                msg += ' I parsed it as a resource.'
            else:
                msg += ' I parsed it as a function.'

                warn_language(r, MCDPWarnings.LANGUAGE_AMBIGUOS_EXPRESSION,
                              msg, context)
Exemplo n.º 10
0
def eval_statement(r, context):
    check_isinstance(context, ModelBuildingContext)
    from .eval_resources_imp import eval_rvalue
    from .eval_lfunction_imp import eval_lfunction

    invalid = (CDP.ConstraintInvalidRR,
               CDP.ConstraintInvalidFF,
               CDP.ConstraintInvalidSwapped)

    if isinstance(r, invalid):
        msg = 'This constraint is invalid. '

        if isinstance(r, CDP.ConstraintInvalidRR):
            msg += 'Both sides are resources.'
        if isinstance(r, CDP.ConstraintInvalidFF):
            msg += 'Both sides are functionalities.'
        if isinstance(r, CDP.ConstraintInvalidSwapped):
            msg += ('Functionality and resources are on the wrong side '
                    'of the inequality.')

        raise DPSemanticError(msg)

    if isinstance(r, Connection):
        context.add_connection(r)

    elif isinstance(r, CDP.Constraint):
        resource = eval_rvalue(r.rvalue, context)
        function = eval_lfunction(r.fvalue, context)
        try:
            add_constraint(context, resource, function)
        except MCDPExceptionWithWhere as e:
            _, _, tb = sys.exc_info()
            where = r.prep.where  # indicate preposition "<="
            raise_with_info(e, where, tb)

    elif isinstance(r, CDP.VarStatement):
        P = eval_space(r.unit, context)

        vnames = get_odd_ops(unwrap_list(r.vnames))
        for v in vnames:
            vname = v.value
            where = v.where
            add_variable(vname, P, where, context)

    elif isinstance(r, CDP.SetNameNDPInstance):
        name = r.name.value
        ndp = eval_ndp(r.dp_rvalue, context)
        if name in context.names:
            msg = 'Repeated identifier "%s".' % name
            raise DPSemanticError(msg, where=r.name.where)
        context.add_ndp(name, ndp)

    elif isinstance(r, CDP.SetNameMCDPType):
        name = r.name.value
        right_side = r.right_side
        x = eval_ndp(right_side, context)
        context.set_var2model(name, x)

    elif isinstance(r, CDP.SetNameRValue):
        return eval_statement_SetNameRValue(r, context)
    elif isinstance(r, CDP.SetNameConstant):
        return eval_statement_SetNameConstant(r, context)
    elif isinstance(r, CDP.SetNameFValue):
        return eval_statement_SetNameFvalue(r, context)
    elif isinstance(r, CDP.Implements):
        return eval_statement_implements(r, context)

    elif isinstance(r, CDP.IgnoreFun):
        # equivalent to f >= any-of(Minimals S)
        lf = eval_lfunction(r.fvalue, context)
        F = context.get_ftype(lf)
        values = F.get_minimal_elements()

        dp = ConstantMinimals(F, values)
        ndp = SimpleWrap(dp, fnames=[], rnames='_out')
        name = context.new_name('_constant')
        context.add_ndp(name, ndp)
        r = context.make_resource(name, '_out')
        add_constraint(context, resource=r, function=lf)

    elif isinstance(r, CDP.IgnoreRes):
        # equivalent to r <= any-of(Maximals S)
        rv = eval_rvalue(r.rvalue, context)
        R = context.get_rtype(rv)
        try:
            values = R.get_maximal_elements()
        except NotImplementedError as e:  # pragma: no cover
            msg = 'Could not call get_maximal_elements().'
            raise_wrapped(DPInternalError, e, msg, R=R)

        dp = LimitMaximals(R, values)
        ndp = SimpleWrap(dp, fnames='_limit', rnames=[])
        name = context.new_name('_limit')
        context.add_ndp(name, ndp)
        f = context.make_function(name, '_limit')
        add_constraint(context, resource=rv, function=f)
    else:

        cases = {
            CDP.ResStatement: eval_statement_ResStatement,
            CDP.FunStatement: eval_statement_FunStatement,

            CDP.FunShortcut5: eval_statement_FunShortcut5,
            CDP.ResShortcut5: eval_statement_ResShortcut5,
            CDP.ResShortcut4: eval_statement_ResShortcut4,
            CDP.FunShortcut4: eval_statement_FunShortcut4,

            CDP.FunShortcut1m: eval_statement_FunShortcut1m,
            CDP.ResShortcut1m: eval_statement_ResShortcut1m,

            CDP.FunShortcut2: eval_statement_FunShortcut2,
            CDP.ResShortcut2: eval_statement_ResShortcut2,

            CDP.FunShortcut1: eval_statement_FunShortcut1,
            CDP.ResShortcut1: eval_statement_ResShortcut1,
            CDP.SetNameUncertainConstant: eval_statement_SetNameUncertainConstant,

        }

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

        if True:  # pragma: no cover
            msg = 'eval_statement(): cannot interpret.'
            r2 = recursive_print(r)
            raise_desc(DPInternalError, msg, r=r2)  # where=r.where.__repr__())
Exemplo n.º 11
0
def parse_as_rvalue(s):
    """ returns rvalue, context """
    parsed = parse_wrap(Syntax.rvalue, s)[0]
    context = Context()
    r = eval_rvalue(parsed, context)
    return r, context
Exemplo n.º 12
0
def parse_as_rvalue(s):
    """ returns rvalue, context """
    parsed = parse_wrap(Syntax.rvalue, s)[0]
    context = Context()
    r = eval_rvalue(parsed, context)
    return r, context