Beispiel #1
0
    def generate_STS(self, var_str, init_str, invar_str, trans_str):
        ts = TS("Additional system")
        init = []
        trans = []
        invar = []

        sparser = StringParser()

        for var in var_str:
            ts.add_state_var(self._define_var(var))

        for init_s in init_str:
            init.append(sparser.parse_formula(init_s))

        for invar_s in invar_str:
            invar.append(sparser.parse_formula(invar_s))

        for trans_s in trans_str:
            trans.append(sparser.parse_formula(trans_s))

        ts.init = And(init)
        ts.invar = And(invar)
        ts.trans = And(trans)

        return ts
Beispiel #2
0
    def compile_sts(self, name, params):
        ts = TS()
        parsize = params[0]
        size = None

        if type(parsize) == str:
            sparser = StringParser()
            parsize = sparser.parse_formula(parsize)

        if parsize.is_constant():
            size = parsize.constant_value()

        if get_type(parsize).is_bv_type():
            size = get_type(parsize).width

        if size is None:
            Logger.error("Undefined size for symbol \"%s\"" % (params[0]))

        value = Symbol("%s.value" % name, BVType(size))
        ts.add_var(value)
        ts.trans = EqualsOrIff(value, TS.get_prime(value))

        return ts
Beispiel #3
0
    def parse_string(self, lines):

        [none, var, state, input, output, init, invar, trans,
         ftrans] = range(9)
        section = none

        inits = TRUE()
        invars = TRUE()
        transs = TRUE()
        ftranss = {}

        sparser = StringParser()

        count = 0
        vars = set([])
        states = set([])
        inputs = set([])
        outputs = set([])
        invar_props = []
        ltl_props = []

        for line in lines:
            count += 1

            if (line.strip() in ["", "\n"]) or line[0] == T_COM:
                continue

            if T_VAR == line[:len(T_VAR)]:
                section = var
                continue

            if T_STATE == line[:len(T_STATE)]:
                section = state
                continue

            if T_INPUT == line[:len(T_INPUT)]:
                section = input
                continue

            if T_OUTPUT == line[:len(T_OUTPUT)]:
                section = output
                continue

            if T_INIT == line[:len(T_INIT)]:
                section = init
                continue

            if T_INVAR == line[:len(T_INVAR)]:
                section = invar
                continue

            if T_TRANS == line[:len(T_TRANS)]:
                section = trans
                continue

            if T_FTRANS == line[:len(T_FTRANS)]:
                section = ftrans
                continue

            if section in [var, state, input, output]:
                varname, vartype = line[:-2].replace(" ", "").split(":")
                if varname[0] == "'":
                    varname = varname[1:-1]
                vartype = parse_typestr(vartype)
                vardef = self._define_var(varname, vartype)

                vars.add(vardef)
                if section == state:
                    states.add(vardef)
                if section == input:
                    inputs.add(vardef)
                if section == output:
                    outputs.add(vardef)

            if section in [init, invar, trans]:
                line = line.replace(T_SC, "").strip()
                qline = quote_names(line, replace_ops=False)

            if section == init:
                inits = And(inits, sparser.parse_formula(qline))

            if section == invar:
                invars = And(invars, sparser.parse_formula(qline))

            if section == trans:
                transs = And(transs, sparser.parse_formula(qline))

            if section == ftrans:
                strvar = line[:line.find(":=")]
                var = sparser.parse_formula(
                    quote_names(strvar, replace_ops=False))
                cond_ass = line[line.find(":=") + 2:].strip()
                ftranss[var] = []

                for cond_as in cond_ass.split("{"):
                    if cond_as == "":
                        continue
                    cond = cond_as[:cond_as.find(",")]
                    ass = cond_as[cond_as.find(",") + 1:cond_as.find("}")]
                    ftranss[var].append((sparser.parse_formula(
                        quote_names(cond, replace_ops=False)),
                                         sparser.parse_formula(
                                             quote_names(ass,
                                                         replace_ops=False))))

        hts = HTS("STS")
        ts = TS()

        ts.vars = vars
        ts.state_vars = states
        ts.input_vars = inputs
        ts.output_vars = outputs
        ts.init = inits
        ts.invar = invars
        ts.trans = transs
        ts.ftrans = ftranss

        hts.add_ts(ts)

        return (hts, invar_props, ltl_props)
Beispiel #4
0
    def generate_HTS(self, module, modulesdic):
        hts = HTS(module.name)
        ts = TS("TS %s" % module.name)

        init = []
        trans = []
        invar = []
        params = []

        sparser = StringParser()

        (vars, states, inputs,
         outputs) = self._collect_sub_variables(module,
                                                modulesdic,
                                                path=[],
                                                varlist=[],
                                                statelist=[],
                                                inputlist=[],
                                                outputlist=[])

        for var in vars:
            ts.add_var(self._define_var(var, module.name))

        for var in states:
            ts.add_state_var(self._define_var(var, module.name))

        for var in inputs:
            ts.add_input_var(self._define_var(var, module.name))

        for var in outputs:
            ts.add_output_var(self._define_var(var, module.name))

        self._check_parameters(module, modulesdic, ts.vars)

        for par in module.pars:
            assert len(par) == 2, "Expecting a variable"
            hts.add_param(self._define_var((par[0], par[1]), module.name))

        for init_s in module.init:
            formula = sparser.parse_formula(quote_names(init_s, module.name),
                                            False)
            init.append(formula)

        for invar_s in module.invar:
            formula = sparser.parse_formula(quote_names(invar_s, module.name),
                                            False)
            invar.append(formula)

        for trans_s in module.trans:
            formula = sparser.parse_formula(quote_names(trans_s, module.name),
                                            False)
            trans.append(formula)

        for sub in module.subs:
            hts.add_sub(sub[0],
                        self.generate_HTS(modulesdic[sub[1]], modulesdic),
                        tuple([v[0] for v in sub[2]]))

        ts.init = And(init)
        ts.invar = And(invar)
        ts.trans = And(trans)

        hts.add_ts(ts)

        return hts
Beispiel #5
0
    def compile_sts(self, name, params):
        sparser = StringParser()
        in_port, max_val, c_push, c_pop = list(params)
        max_val = int(max_val)

        if type(c_push) == str:
            c_push = sparser.parse_formula(c_push)
        if type(c_pop) == str:
            c_pop = sparser.parse_formula(c_pop)

        tracking = Symbol("%s.tracking" % name, BOOL)
        end = Symbol("%s.end" % name, BOOL)
        done = Symbol("%s.done" % name, BOOL)
        packet = Symbol("%s.packet" % name,
                        BVType(in_port.symbol_type().width))
        max_width = math.ceil(math.log(max_val) / math.log(2))

        max_bvval = BV(max_val, max_width)
        zero = BV(0, max_width)
        one = BV(1, max_width)
        count = Symbol("%s.count" % name, BVType(max_width))
        size = Symbol("%s.size" % name, BVType(max_width))

        pos_c_push = BV2B(c_push)
        neg_c_push = Not(BV2B(c_push))

        pos_c_pop = BV2B(c_pop)
        neg_c_pop = Not(BV2B(c_pop))

        init = []
        trans = []
        invar = []

        # INIT DEFINITION #

        # count = 0
        init.append(EqualsOrIff(count, BV(0, max_width)))
        # tracking = False
        init.append(EqualsOrIff(tracking, FALSE()))
        # size = 0
        init.append(EqualsOrIff(size, BV(0, max_width)))
        # end = false
        init.append(EqualsOrIff(end, FALSE()))

        # INVAR DEFINITION #

        # !done -> (end = (tracking & (size = count)))
        invar.append(
            Implies(Not(done),
                    EqualsOrIff(end, And(tracking, EqualsOrIff(size, count)))))

        # count <= size
        invar.append(BVULE(count, size))
        # count <= maxval
        invar.append(BVULE(count, max_bvval))
        # size <= maxval
        invar.append(BVULE(size, max_bvval))

        # done -> (end <-> False);
        invar.append(Implies(done, EqualsOrIff(end, FALSE())))
        # done -> (count = 0_8);
        invar.append(Implies(done, EqualsOrIff(count, BV(0, max_width))))
        # done -> (size = 0_8);
        invar.append(Implies(done, EqualsOrIff(size, BV(0, max_width))))
        # done -> (packet = 0_8);
        invar.append(
            Implies(done,
                    EqualsOrIff(packet, BV(0,
                                           in_port.symbol_type().width))))

        # TRANS DEFINITION #

        # (!end & !done) -> next(!done);
        trans.append(Implies(And(Not(end), Not(done)), TS.to_next(Not(done))))
        # end -> next(done);
        trans.append(Implies(end, TS.to_next(done)))
        # done -> next(done);
        trans.append(Implies(done, TS.to_next(done)))

        # tracking -> next(tracking);
        trans.append(
            Implies(Not(done), Implies(tracking, TS.to_next(tracking))))
        # tracking -> (next(packet) = packet);
        trans.append(
            Implies(Not(done),
                    Implies(tracking, EqualsOrIff(TS.to_next(packet),
                                                  packet))))
        # !tracking & next(tracking) -> c_push;
        trans.append(
            Implies(
                Not(done),
                Implies(And(Not(tracking), TS.to_next(tracking)), pos_c_push)))
        # (c_push & next(tracking)) -> ((packet = in) & (next(packet) = in);
        trans.append(
            Implies(
                Not(done),
                Implies(
                    And(pos_c_push, TS.to_next(tracking)),
                    And(EqualsOrIff(packet, in_port),
                        EqualsOrIff(TS.to_next(packet), in_port)))))
        # (c_push & !c_pop & tracking) -> (next(count) = (count + 1_8));
        trans.append(
            Implies(
                Not(done),
                Implies(
                    And(pos_c_push, neg_c_pop, tracking),
                    EqualsOrIff(TS.to_next(count),
                                BVAdd(count, BV(1, max_width))))))
        # (c_push & size < maxval) -> (next(size) = (size + 1_8));
        trans.append(
            Implies(
                Not(done),
                Implies(
                    And(pos_c_push, BVULT(size, max_bvval)),
                    EqualsOrIff(TS.to_next(size),
                                BVAdd(size, BV(1, max_width))))))
        # (c_pop & size > 0) -> (next(size) = (size - 1_8));
        trans.append(
            Implies(
                Not(done),
                Implies(
                    And(pos_c_pop, BVUGT(size, zero)),
                    EqualsOrIff(TS.to_next(size),
                                BVSub(size, BV(1, max_width))))))
        # (!(c_push | c_pop)) -> (next(count) = count);
        trans.append(
            Implies(
                Not(done),
                Implies(Not(Or(pos_c_push, pos_c_pop)),
                        EqualsOrIff(count, TS.to_next(count)))))
        # ((c_push | c_pop) & !tracking) -> (next(count) = count);
        trans.append(
            Implies(
                Not(done),
                Implies(And(Or(pos_c_push, pos_c_pop), Not(tracking)),
                        EqualsOrIff(count, TS.to_next(count)))))

        # (c_push & size = maxval) -> (next(size) = size);
        trans.append(
            Implies(
                Not(done),
                Implies(And(pos_c_push, EqualsOrIff(size, max_bvval)),
                        EqualsOrIff(TS.to_next(size), size))))
        # (!(c_push | c_pop)) -> (next(size) = size);
        trans.append(
            Implies(
                Not(done),
                Implies(Not(Or(pos_c_push, pos_c_pop)),
                        EqualsOrIff(size, TS.to_next(size)))))
        # (!(c_push | c_pop)) -> (next(count) = count);
        trans.append(
            Implies(
                Not(done),
                Implies(Not(Or(pos_c_push, pos_c_pop)),
                        EqualsOrIff(count, TS.to_next(count)))))
        # (c_pop & size = 0) -> (next(size) = 0);
        trans.append(
            Implies(
                Not(done),
                Implies(And(pos_c_pop, EqualsOrIff(size, zero)),
                        EqualsOrIff(TS.to_next(size), zero))))

        # (!c_push) -> (next(count) = count);
        trans.append(
            Implies(Not(done),
                    Implies(neg_c_push, EqualsOrIff(TS.to_next(count),
                                                    count))))

        init = And(init)
        invar = And(invar)
        trans = And(trans)

        ts = TS()
        ts.vars, ts.init, ts.invar, ts.trans = set(
            [tracking, end, packet, count, size]), init, invar, trans

        return ts
    def parse_string(self, lines):

        [none, var, state, input, output, init, invar, trans] = range(8)
        section = none

        inits = TRUE()
        invars = TRUE()
        transs = TRUE()

        sparser = StringParser()

        count = 0
        vars = set([])
        states = set([])
        inputs = set([])
        outputs = set([])
        invar_props = []
        ltl_props = []

        for line in lines:
            count += 1

            if line.strip() in ["", "\n"]:
                continue

            if T_VAR == line[:len(T_VAR)]:
                section = var
                continue

            if T_STATE == line[:len(T_STATE)]:
                section = state
                continue

            if T_INPUT == line[:len(T_INPUT)]:
                section = input
                continue

            if T_OUTPUT == line[:len(T_OUTPUT)]:
                section = output
                continue

            if T_INIT == line[:len(T_INIT)]:
                section = init
                continue

            if T_INVAR == line[:len(T_INVAR)]:
                section = invar
                continue

            if T_TRANS == line[:len(T_TRANS)]:
                section = trans
                continue

            if section in [var, state, input, output]:
                line = line[:-2].replace(" ", "").split(":")
                varname, vartype = line[0], (line[1][:-1].split("("))
                if varname[0] == "'":
                    varname = varname[1:-1]
                vardef = self._define_var(varname, vartype)

                vars.add(vardef)
                if section == state:
                    states.add(vardef)
                if section == input:
                    inputs.add(vardef)
                if section == output:
                    outputs.add(vardef)

            if section in [init, invar, trans]:
                qline = quote_names(line[:-2], replace_ops=False)

            if section == init:
                inits = And(inits, sparser.parse_formula(qline))

            if section == invar:
                invars = And(invars, sparser.parse_formula(qline))

            if section == trans:
                transs = And(transs, sparser.parse_formula(qline))

        hts = HTS("STS")
        ts = TS()

        ts.vars = vars
        ts.state_vars = states
        ts.input_vars = inputs
        ts.output_vars = outputs
        ts.init = inits
        ts.invar = invars
        ts.trans = transs

        hts.add_ts(ts)

        return (hts, invar_props, ltl_props)
Beispiel #7
0
class InitParser(ModelParser):
    extensions = ['init']
    name = "INIT"

    def __init__(self):
        self.parser = StringParser()
        self._pysmt_formula_manager = get_env().formula_manager

    def parse_line(self, string:str)->Union[FNode, None]:
        if '=' not in string:
            raise RuntimeError("Expecting a single equality but got: {}".format(string))

        split = string.split("=")
        if len(split) > 2:
            raise RuntimeError("Expecting exactly one equality but got: {}".format(string))

        lhs, rhs = split

        try:
            lhs = self.parser.parse_formula(lhs)
            rhs = self.parser.parse_formula(rhs)
        except UndefinedSymbolError:
            return None

        for fv in get_free_variables(lhs):
            if not self._pysmt_formula_manager.is_state_symbol(fv):
                return None

        if not rhs.is_constant():
            raise RuntimeError("Expecting a constant on the right side but got: {}".format(rhs))

        return EqualsOrIff(lhs, rhs)

    # implementing ModelParser interface
    @staticmethod
    def get_extensions():
        return InitParser.extensions

    def is_available(self):
        return True

    def get_model_info(self):
        return None

    def parse_string(self, contents:str)->HTS:
        '''
        Parses a string representation of an initial state file
        '''
        hts = HTS("INIT")
        ts = TS("TS INIT")

        init = []

        for line in contents.split('\n'):
            line = line.strip()
            if not line:
                continue
            else:
                res = self.parse_line(line)
                if res is not None:
                    init.append(res)

        Logger.msg("Initial state file set concrete values for {} state variables".format(len(init)), 1)

        ts.init = And(init)
        ts.invar = TRUE()
        ts.trans = TRUE()
        hts.add_ts(ts)

        return hts

    def parse_file(self,
                   filepath:Path,
                   config:NamedTuple,
                   flags:str=None)->Tuple[HTS, List[FNode], List[FNode]]:
        '''
        Reads an initial state file and produces (HTS, invariants, ltl_invariants)
        '''
        hts = HTS(filepath.name)
        ts = TS("TS %s"%filepath.name)

        init = []

        with filepath.open("r") as f:
            hts = self.parse_string(f.read())

        return hts, None, None