def read(cls,filename): ''' Reads in the reachability game from a given file. Parameters: filename (.rg file) -- Game description in rg-format. Returns: (Game) -- Game in PySMT description (alters formulas to enforce alternation). ''' with open(filename) as file: contents = file.read() tokens = re.split('\n|:',contents) mode = '' ints = [] bools = [] reals = [] init = '' safe = '' reach = '' goal = '' modes = re.compile('int|bool|real|init|safe|reach|goal') for tok in tokens: t = tok.rstrip() if modes.match(t): mode = t else: if mode == 'int': ints.extend(re.split(',|\[|\]',t.replace(' ',''))) if mode == 'bool': bools.extend(re.split(',',t.replace(' ',''))) if mode == 'real': reals.extend(re.split(',|\[|\]',t.replace(' ',''))) elif mode == 'init': init += t elif mode == 'safe': safe += t elif mode == 'reach': reach += t elif mode == 'goal': goal += t # Handle boolean variables (implicitly bounded) bools.append('r') bool_next = {Symbol(v,BOOL): Symbol(v.upper(),BOOL) for v in bools} # Parse integer variables (might be bounded) var_int, bnd_int = parse_bounds(ints,INT) int_next = {v: Next(v) for v in var_int} # Parse real variables (might be bounded) var_real, bnd_real = parse_bounds(reals,REAL) real_next = {v: Next(v) for v in var_real} # Merge int and real nexts = {**int_next,**bool_next,**real_next} bnds = And(bnd_int + bnd_real) alt_init = And(parse(init),Iff(Symbol('r',BOOL),Bool(False)),bnds) alt_safe = And(parse(safe),Iff(Symbol('r',BOOL),Bool(False)),Iff(Symbol('R',BOOL),Bool(True)),bnds,bnds.substitute(nexts)) alt_reach = And(parse(reach),Iff(Symbol('r',BOOL),Bool(True)),Iff(Symbol('R',BOOL),Bool(False)),bnds,bnds.substitute(nexts)) alt_goal = And(parse(goal),Iff(Symbol('r',BOOL),Bool(False)),bnds) return Game(nexts,alt_init,alt_safe,alt_reach,alt_goal)
def parse_typestr(string: str) -> PySMTType: ''' Takes a string and returns the PySMTType. We use parentheses instead of curly-brackets in CoSA, e.g. Array(BV(3), BV(8)) instead of Array{BV{3}, BV{8}} ''' return parse(string.replace("(", "{").replace(")", "}"))
def test_precedences(self): p = HRParser() a,b,c = (Symbol(v) for v in "abc") x,y = (Symbol(v, REAL) for v in "xy") tests = [] tests.append(("a | b & c", Or(a, And(b,c)))) tests.append(("a & b | c", Or(And(a, b) ,c))) f1 = LE(Plus(Plus(x, y), Real(5)), Real(7)) f2 = LE(Plus(x, Real(5)), Minus(Real(7), y)) tests.append(("x + y + 5.0 <= 7.0", f1)) tests.append(("x + 5.0 <= 7.0 - y", f2)) tests.append(("x + y + 5.0 <= 7.0 & x + 5.0 <= 7.0 - y", And(f1, f2))) tests.append(("x + 5.0 <= 7.0 - y | x + y + 5.0 <= 7.0 & x + 5.0 <= 7.0 - y", Or(f2, And(f1, f2)))) for string, formula in tests: self.assertEqual(p.parse(string), formula) self.assertEqual(parse(string), formula)
def test_precedences(self): p = HRParser() a, b, c = (Symbol(v) for v in "abc") x, y = (Symbol(v, REAL) for v in "xy") tests = [] tests.append(("a | b & c", Or(a, And(b, c)))) tests.append(("a & b | c", Or(And(a, b), c))) f1 = LE(Plus(Plus(x, y), Real(5)), Real(7)) f2 = LE(Plus(x, Real(5)), Minus(Real(7), y)) tests.append(("x + y + 5.0 <= 7.0", f1)) tests.append(("x + 5.0 <= 7.0 - y", f2)) tests.append(("x + y + 5.0 <= 7.0 & x + 5.0 <= 7.0 - y", And(f1, f2))) tests.append( ("x + 5.0 <= 7.0 - y | x + y + 5.0 <= 7.0 & x + 5.0 <= 7.0 - y", Or(f2, And(f1, f2)))) for string, formula in tests: self.assertEqual(p.parse(string), formula) self.assertEqual(parse(string), formula)
def parse_violation(self, violation): file = open(violation) for line in file: predicate = parse(line) self.formulae.append(predicate)