def test_grouping(self): for n_str in self.numbers: assert vexpr.Number(float(n_str)) == parser.parse("(" + n_str + ")") for v in self.variables: assert vexpr.Variable(v) == parser.parse("(" + v + ")")
def _to_term(self, x: Union[float, int, np.float64]) -> expr.Number: if (isinstance(x, float) or isinstance(x, np.float) or isinstance(x, np.float64) or isinstance(x, np.float32) or isinstance(x, np.int) or isinstance(x, np.int64)): return expr.Number(x) else: raise NotImplementedError( f"Conversion to term not implemented for {x} of type {x.__class__}" )
def state_constants(self): return { vexpr.Variable("A"): vexpr.Number(self.A), vexpr.Variable("B"): vexpr.Number(self.B), vexpr.Variable("T"): vexpr.Number(self.T), vexpr.Variable("safe_sep"): vexpr.Number(self._safe_sep), vexpr.Variable("min_w"): vexpr.Number(self.min_w), vexpr.Variable("max_w"): vexpr.Number(self.max_w), vexpr.Variable("num_obstacles"): vexpr.Number(self.num_obstacles), }
def _simplify(node): if isinstance(node, expr.And): if isinstance(node.left, expr.Bool): if node.left: return node.right else: return expr.FalseF() if isinstance(node.right, expr.Bool): if node.right: return node.left else: return expr.FalseF() elif isinstance(node, expr.Or): if isinstance(node.left, expr.Bool): if node.left: return expr.TrueF() else: return node.right if isinstance(node.right, expr.Bool): if node.right: return expr.TrueF() else: return node.left else: try: if isinstance(node.left, expr.Number) and isinstance( node.right, expr.Number): func = NUMBER_OPS.get(type(node).__name__) if func: return expr.Number(func(node.left.val, node.right.val)) func = BOOL_OPS.get(type(node).__name__) if func: ret = func(node.left.val, node.right.val) assert isinstance(ret, (bool, np.bool_)) return expr.TrueF() if ret else expr.FalseF() elif isinstance(node.left, expr.Bool) and isinstance( node.right, expr.Bool): func = BOOL_OPS.get(type(node).__name__) if func: ret = func(bool(node.left), bool(node.right)) assert isinstance(ret, (bool, np.bool_)) return expr.TrueF() if ret else expr.FalseF() except AttributeError: pass return node
def test_numbers(self): for n_str in self.numbers: assert vexpr.Number(float(n_str)) == parser.parse(n_str)
def _convert(ast: parsimonious.nodes.Node) -> vexpr.Expression: if ast.expr_name == "term": assert len(ast.children) == 3 return _convert(ast.children[1]) elif ast.expr_name == "formula": assert len(ast.children) == 3 return _convert(ast.children[1]) elif ast.expr_name == "program": assert len(ast.children) == 3 return _convert(ast.children[1]) if ast.expr_name == "number": return vexpr.Number(float(ast.text)) elif ast.expr_name == "variable": return vexpr.Variable(ast.text) elif ast.expr_name == "neg": assert len(ast.children) == 2 return vexpr.Neg(_convert(ast.children[1])) elif (ast.expr_name == "term_group" or ast.expr_name == "formula_group" or ast.expr_name == "program_group"): assert len(ast.children) == 3 return _convert(ast.children[1]) elif ast.expr_name == "power": return functools.reduce(vexpr.Power, _collect_right_assoc(ast)) elif ast.expr_name == "mult": return functools.reduce(vexpr.Times, _collect_right_assoc(ast)) elif ast.expr_name == "divide": return functools.reduce(vexpr.Divide, _collect_right_assoc(ast)) elif ast.expr_name == "plus": return functools.reduce(vexpr.Plus, _collect_right_assoc(ast)) elif ast.expr_name == "minus": return functools.reduce(vexpr.Minus, _collect_right_assoc(ast)) elif ast.expr_name == "true": return vexpr.TrueF() elif ast.expr_name == "false": return vexpr.FalseF() elif ast.expr_name == "not": return vexpr.Not(_convert(ast.children[1])) elif ast.expr_name == "less": left = _convert(ast.children[0]) right = _convert(ast.children[2]) return vexpr.Less(left, right) elif ast.expr_name == "lesseq": left = _convert(ast.children[0]) right = _convert(ast.children[2]) return vexpr.LessEq(left, right) elif ast.expr_name == "greater": left = _convert(ast.children[0]) right = _convert(ast.children[2]) return vexpr.Greater(left, right) elif ast.expr_name == "greatereq": left = _convert(ast.children[0]) right = _convert(ast.children[2]) return vexpr.GreaterEq(left, right) elif ast.expr_name == "equal": left = _convert(ast.children[0]) right = _convert(ast.children[2]) return vexpr.Eq(left, right) elif ast.expr_name == "and": return functools.reduce(vexpr.And, _collect_right_assoc(ast)) elif ast.expr_name == "or": return functools.reduce(vexpr.Or, _collect_right_assoc(ast)) elif ast.expr_name == "imply": return functools.reduce(vexpr.Imply, _collect_right_assoc(ast)) elif ast.expr_name == "equiv": return functools.reduce(vexpr.Equiv, _collect_right_assoc(ast)) elif ast.expr_name == "box": return vexpr.Box(_convert(ast.children[1]), _convert(ast.children[3])) elif ast.expr_name == "variable_list": # variable_list = variable / (("{" ~"\s*")? variable+ (~"\s*" "}")?) if len(ast.children) == 1: return [_convert(ast.children[0])] else: vs = [] for c in ast.children[0].children[1]: vs.append(_convert(c)) return vs elif ast.expr_name == "forall": vs = _convert(ast.children[1]) f = _convert(ast.children[3]) return vexpr.Forall(set(vs), f) elif ast.expr_name == "exists": vs = _convert(ast.children[1]) f = _convert(ast.children[3]) return vexpr.Forall(set(vs), f) elif ast.expr_name == "assignment": return vexpr.Assign(_convert(ast.children[0]), _convert(ast.children[2])) elif ast.expr_name == "test": return vexpr.Test(_convert(ast.children[1])) elif ast.expr_name == "star": return vexpr.Loop(_convert(ast.children[1])) elif ast.expr_name == "choice": return functools.reduce(vexpr.Choice, _collect_right_assoc(ast)) elif ast.expr_name == "seqcomp": return functools.reduce(vexpr.SequentialCompose, _collect_right_assoc(ast)) elif ast.expr_name == "assignment": return vexpr.Box(_convert(ast.children[0]), _convert(ast.children[2])) elif ast.expr_name == "atomic_ode": return vexpr.AtomicODE(_convert(ast.children[1]), _convert(ast.children[3])) elif ast.expr_name == "diff_product": return functools.reduce(vexpr.DiffPair, _collect_right_assoc(ast)) elif ast.expr_name == "ode_system": f = _convert(ast.children[3]) ode = ast.children[1] assert len(ode.children) == 1 ode = ode.children[0] assert ode.expr_name == "diff_product" or ode.expr_name == "atomic_ode" return vexpr.ODESystem(_convert(ode), f) elif ast.expr_name == "ode_system_no_evdom": ode = ast.children[1] assert len(ode.children) == 1 ode = ode.children[0] assert ode.expr_name == "diff_product" or ode.expr_name == "atomic_ode" return vexpr.ODESystem(_convert(ode), vexpr.TrueF()) else: if len(ast.children) != 1: raise ParserException( f"Successfully parsed into a parsimonious library node, but " f"_convert does not know how to handle expr_name: {ast.expr_name}" ) else: return _convert(ast.children[0])
def is_configured(self): return self.check( expr.Eq(expr.Number(2), expr.Plus(expr.Number(1), expr.Number(1))), "QE")