Fixed PSTL construction

This commit is contained in:
Marcell Vazquez-Chanlatte 2017-10-26 16:14:45 -07:00
parent d5985406ad
commit eda63fd6f0
3 changed files with 38 additions and 30 deletions

View file

@ -12,8 +12,6 @@ from parsimonious import Grammar, NodeVisitor
from funcy import flatten
from lenses import lens
from sympy import Symbol, Number
from stl import ast
from stl.utils import implies, xor, iff, env, alw
@ -45,8 +43,7 @@ interval = "[" __ const_or_unbound __ "," __ const_or_unbound __ "]"
const_or_unbound = unbound / "inf" / const
lineq = terms _ op _ const_or_unbound
term = coeff? var
coeff = ((dt __ "*" __)? const __ "*" __) / (dt __ "*")
term = const? var
terms = (term __ pm __ terms) / term
var = id
@ -56,7 +53,7 @@ pm = "+" / "-"
dt = "dt"
unbound = id "?"
id = ~r"[a-zA-z\d]+"
const = ~r"[\+\-]?\d*(\.\d+)?"
const = ~r"[-+]?\d*\.\d+|\d+"
op = ">=" / "<=" / "<" / ">" / "="
_ = ~r"\s"+
__ = ~r"\s"*
@ -64,8 +61,10 @@ EOL = "\\n"
''')
oo = float('inf')
class STLVisitor(NodeVisitor):
def __init__(self, H=float('inf')):
def __init__(self, H=oo):
super().__init__()
self.default_interval = ast.Interval(0.0, H)
@ -92,7 +91,7 @@ class STLVisitor(NodeVisitor):
return node.text
def visit_unbound(self, node, _):
return Symbol(node.text)
return ast.Param(node.text)
visit_op = get_text
@ -128,23 +127,16 @@ class STLVisitor(NodeVisitor):
return env(psi, lo=lo, hi=hi) & alw(ast.Until(phi, psi), lo=0, hi=lo)
def visit_id(self, name, _):
return Symbol(name.text)
return name.text
def visit_const(self, const, children):
return float(const.text)
def visit_term(self, _, children):
coeffs, (iden, time) = children
c = coeffs[0] if coeffs else Number(1)
return ast.Var(coeff=c, id=iden, time=time)
coeffs, iden = children
c = coeffs[0] if coeffs else 1
return ast.Var(coeff=c, id=iden)
def visit_coeff(self, _, children):
dt, coeff, *_ = children[0]
if not isinstance(dt, Symbol):
dt = dt[0][0] if dt else Number(1)
return dt * coeff
else:
return dt
def visit_terms(self, _, children):
if isinstance(children[0], list):
@ -159,7 +151,7 @@ class STLVisitor(NodeVisitor):
return ast.LinEq(tuple(terms), op, const[0])
def visit_pm(self, node, _):
return Number(1) if node.text == "+" else Number(-1)
return 1 if node.text == "+" else -1
def visit_AP(self, *args):
return ast.AtomicPred(self.visit_id(*args))
@ -171,5 +163,5 @@ class STLVisitor(NodeVisitor):
return ast.Next(children[1])
def parse(stl_str: str, rule: str = "phi", H=float('inf')) -> "STL":
def parse(stl_str: str, rule: str = "phi", H=oo) -> "STL":
return STLVisitor(H).visit(STL_GRAMMAR[rule].parse(stl_str))