Beispiel #1
0
def sub_values(tree, var_values):
    """Substitute given values for variables.

    @param tree: AST

    @type var_values: C{dict}

    @return: AST with L{Var} nodes replaces by
        L{Num}, L{Const}, or L{Bool}
    """
    old2new = dict()
    for u in tree.nodes_iter():
        if u.type != 'var':
            continue
        val = var_values[u.value]
        # instantiate appropriate value type
        if isinstance(val, bool):
            v = nodes.Bool(val)
        elif isinstance(val, int):
            v = nodes.Num(val)
        elif isinstance(val, str):
            v = nodes.Str(val)
        old2new[u] = v
    # replace variable by value
    nx.relabel_nodes(tree, old2new, copy=False)
Beispiel #2
0
def tree_from_recursive_ast_test():
    # (1 - x) + y
    x = nodes.Var('x')
    one = nodes.Num('1')
    minus = nodes.Operator('-', one, x)
    y = nodes.Var('y')
    plus = nodes.Operator('+', minus, y)
    # convert to Tree
    g = tx.Tree.from_recursive_ast(plus)
    assert len(g) == 5
    assert set(g.nodes()) == set([x, one, minus, y, plus])
    assert g.has_edge(plus, y)
    assert g.has_edge(plus, minus)
    assert g.has_edge(minus, one)
    assert g.has_edge(minus, x)
Beispiel #3
0
def tree_to_recursive_ast_test():
    g = tx.Tree()
    x = nodes.Var('x')
    one = nodes.Num('1')
    minus = nodes.Operator('-', one, x)
    y = nodes.Var('y')
    # (1 - x) + y
    plus = nodes.Operator('+', minus, y)
    g.add_nodes_from([plus, x, y, one, minus])
    # g is different than forula:
    # x - (z + 1)
    z = nodes.Var('z')
    g.root = minus
    g.add_edge(minus, x, key=0)
    g.add_edge(minus, plus, key=1)
    g.add_edge(plus, z, key=0)
    g.add_edge(plus, one, key=1)
    t = g.to_recursive_ast()
    # -
    assert isinstance(t, nodes.Operator)
    assert t.operator == '-'
    # must be a new object
    assert t is not minus
    assert len(t.operands) == 2
    # x
    u = t.operands[0]
    assert isinstance(u, nodes.Var)
    assert u.value == 'x'
    assert u is not x
    # +
    u = t.operands[1]
    assert isinstance(u, nodes.Operator)
    assert u.operator == '+'
    assert u is not plus
    assert len(u.operands) == 2
    # z
    v = u.operands[0]
    assert isinstance(v, nodes.Var)
    assert v.value == 'z'
    assert v is not z
    # 1
    u = u.operands[1]
    assert isinstance(u, nodes.Num)
    assert u.value == '1'
    assert u is not one
Beispiel #4
0
def sub_constants(tree, var_str2int):
    """Replace string constants by integers.

    To be used for converting arbitrary finite domains
    to integer domains prior to calling gr1c.

    @param const2int: {'varname':['const_val0', ...], ...}
    @type const2int: C{dict} of C{list}
    """
    # logger.info('substitute ints for constants in:\n\t' + str(self))
    old2new = dict()
    for u in tree.nodes_iter():
        if u.type != 'str':
            continue
        var, op = pair_node_to_var(tree, u)
        # now: c, is the operator and: v, the variable
        str2int = var_str2int[str(var)]
        x = str2int.index(u.value)
        num = nodes.Num(str(x))
        # replace Const with Num
        old2new[u] = num
    nx.relabel_nodes(tree, old2new, copy=False)