Пример #1
0
def test_str_to_int():
    r = parse('a = "hehe"')
    g = tx.Tree.from_recursive_ast(r)
    var_str2int = {'a': ['hehe', 'haha']}
    tx.sub_constants(g, var_str2int)
    f = g.to_recursive_ast()
    s = f.flatten()
    assert s == '( a = 0 )'

    x = '(loc = "s2") -> X((((env_alice = "left") && (env_bob = "bright"))))'
    var_str2int = {
        'loc': ['s0', 's2'],
        'env_alice': ['left', 'right'],
        'env_bob': ['bleft', 'bright']
    }
    r = parse(x)
    print(repr(r))
    g = tx.Tree.from_recursive_ast(r)
    print(g)
    tx.sub_constants(g, var_str2int)
    print(str(g))
    f = g.to_recursive_ast()
    print(repr(f))
    s = f.flatten()
    print(s)
    assert s == ('( ( loc = 1 ) -> '
                 '( X ( ( env_alice = 0 ) & ( env_bob = 1 ) ) ) )')
Пример #2
0
def test_str_to_int():
    r = parse('a = "hehe"')
    g = tx.Tree.from_recursive_ast(r)
    var_str2int = {'a': ['hehe', 'haha']}
    tx.sub_constants(g, var_str2int)
    f = g.to_recursive_ast()
    s = f.flatten()
    assert s == '( a = 0 )'

    x = '(loc = "s2") -> X((((env_alice = "left") && (env_bob = "bright"))))'
    var_str2int = {
        'loc': ['s0', 's2'],
        'env_alice': ['left', 'right'],
        'env_bob': ['bleft', 'bright']}
    r = parse(x)
    print(repr(r))
    g = tx.Tree.from_recursive_ast(r)
    print(g)
    tx.sub_constants(g, var_str2int)
    print(str(g))
    f = g.to_recursive_ast()
    print(repr(f))
    s = f.flatten()
    print(s)
    assert s == ('( ( loc = 1 ) -> '
                 '( X ( ( env_alice = 0 ) & ( env_bob = 1 ) ) ) )')
Пример #3
0
    def str_to_int(self):
        """Replace arbitrary finite vars with int vars.

        Returns spec itself if it contains only int vars.
        Otherwise it returns a copy of spec with all arbitrary
        finite vars replaced by int-valued vars.
        """
        logger.info('convert string variables to integers...')
        vars_dict = dict(self.env_vars)
        vars_dict.update(self.sys_vars)
        fvars = {v: d for v, d in vars_dict.iteritems() if isinstance(d, list)}
        # replace symbols by ints
        for p in self._parts:
            for x in getattr(self, p):
                if self._bool_int.get(x) in self._ast:
                    logger.debug(str(x) + ' is in _bool_int cache')
                    continue
                else:
                    logger.debug(str(x) + ' is not in _bool_int cache')
                # get AST
                a = self.ast(x)
                # create AST copy with int and bool vars only
                g = tx.Tree.from_recursive_ast(a)
                tx.sub_constants(g, fvars)
                b = g.to_recursive_ast()
                # formula of int/bool AST
                f = b.flatten()
                self._ast[f] = b  # cache
                # remember map from clauses to int/bool clauses
                self._bool_int[x] = f
        logger.info('done converting to integer variables.\n')
Пример #4
0
    def str_to_int(self):
        """Replace arbitrary finite vars with int vars.

        Returns spec itself if it contains only int vars.
        Otherwise it returns a copy of spec with all arbitrary
        finite vars replaced by int-valued vars.
        """
        logger.info('convert string variables to integers...')
        vars_dict = dict(self.env_vars)
        vars_dict.update(self.sys_vars)
        fvars = {v: d for v, d in vars_dict.items() if isinstance(d, list)}
        # replace symbols by ints
        for p in self._parts:
            for x in getattr(self, p):
                if self._bool_int.get(x) in self._ast:
                    logger.debug(str(x) + ' is in _bool_int cache')
                    continue
                else:
                    logger.debug(str(x) + ' is not in _bool_int cache')
                # get AST
                a = self.ast(x)
                # create AST copy with int and bool vars only
                g = tx.Tree.from_recursive_ast(a)
                tx.sub_constants(g, fvars)
                b = g.to_recursive_ast()
                # formula of int/bool AST
                f = b.flatten()
                self._ast[f] = b  # cache
                # remember map from clauses to int/bool clauses
                self._bool_int[x] = f
        logger.info('done converting to integer variables.\n')