refactor(pyargus): update python bindings for breaking changes
This commit is contained in:
parent
0e38c2fecf
commit
b8c67bcea9
5 changed files with 94 additions and 41 deletions
|
|
@ -13,5 +13,5 @@ argus-semantics = { version = "0.1.0", path = "../argus-semantics" }
|
||||||
derive_more = "0.99.17"
|
derive_more = "0.99.17"
|
||||||
log = "0.4.17"
|
log = "0.4.17"
|
||||||
paste = "1.0.12"
|
paste = "1.0.12"
|
||||||
pyo3 = "0.18.3"
|
pyo3 = "0.19.2"
|
||||||
pyo3-log = "0.8.1"
|
pyo3-log = "0.8.1"
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
use argus_core::expr::Ordering;
|
use std::time::Duration;
|
||||||
|
|
||||||
use argus_core::prelude::*;
|
use argus_core::prelude::*;
|
||||||
use pyo3::prelude::*;
|
use pyo3::prelude::*;
|
||||||
use pyo3::pyclass::CompareOp;
|
use pyo3::pyclass::CompareOp;
|
||||||
|
|
@ -61,7 +62,7 @@ pub struct ConstInt;
|
||||||
impl ConstInt {
|
impl ConstInt {
|
||||||
#[new]
|
#[new]
|
||||||
fn new(val: i64) -> (Self, PyNumExpr) {
|
fn new(val: i64) -> (Self, PyNumExpr) {
|
||||||
(Self, Box::new(NumExpr::IntLit(val)).into())
|
(Self, Box::new(NumExpr::IntLit(argus_core::expr::IntLit(val))).into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -78,7 +79,7 @@ pub struct ConstUInt;
|
||||||
impl ConstUInt {
|
impl ConstUInt {
|
||||||
#[new]
|
#[new]
|
||||||
fn new(val: u64) -> (Self, PyNumExpr) {
|
fn new(val: u64) -> (Self, PyNumExpr) {
|
||||||
(Self, Box::new(NumExpr::UIntLit(val)).into())
|
(Self, Box::new(NumExpr::UIntLit(argus_core::expr::UIntLit(val))).into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -90,7 +91,10 @@ pub struct ConstFloat;
|
||||||
impl ConstFloat {
|
impl ConstFloat {
|
||||||
#[new]
|
#[new]
|
||||||
fn new(val: f64) -> (Self, PyNumExpr) {
|
fn new(val: f64) -> (Self, PyNumExpr) {
|
||||||
(Self, Box::new(NumExpr::FloatLit(val)).into())
|
(
|
||||||
|
Self,
|
||||||
|
Box::new(NumExpr::FloatLit(argus_core::expr::FloatLit(val))).into(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -102,7 +106,10 @@ pub struct VarInt;
|
||||||
impl VarInt {
|
impl VarInt {
|
||||||
#[new]
|
#[new]
|
||||||
fn new(name: String) -> (Self, PyNumExpr) {
|
fn new(name: String) -> (Self, PyNumExpr) {
|
||||||
(Self, Box::new(NumExpr::IntVar { name }).into())
|
(
|
||||||
|
Self,
|
||||||
|
Box::new(NumExpr::IntVar(argus_core::expr::IntVar { name })).into(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -114,7 +121,10 @@ pub struct VarUInt;
|
||||||
impl VarUInt {
|
impl VarUInt {
|
||||||
#[new]
|
#[new]
|
||||||
fn new(name: String) -> (Self, PyNumExpr) {
|
fn new(name: String) -> (Self, PyNumExpr) {
|
||||||
(Self, Box::new(NumExpr::UIntVar { name }).into())
|
(
|
||||||
|
Self,
|
||||||
|
Box::new(NumExpr::UIntVar(argus_core::expr::UIntVar { name })).into(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -126,7 +136,10 @@ pub struct VarFloat;
|
||||||
impl VarFloat {
|
impl VarFloat {
|
||||||
#[new]
|
#[new]
|
||||||
fn new(name: String) -> (Self, PyNumExpr) {
|
fn new(name: String) -> (Self, PyNumExpr) {
|
||||||
(Self, Box::new(NumExpr::FloatVar { name }).into())
|
(
|
||||||
|
Self,
|
||||||
|
Box::new(NumExpr::FloatVar(argus_core::expr::FloatVar { name })).into(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -139,7 +152,7 @@ impl Negate {
|
||||||
#[new]
|
#[new]
|
||||||
fn new(arg: PyNumExpr) -> (Self, PyNumExpr) {
|
fn new(arg: PyNumExpr) -> (Self, PyNumExpr) {
|
||||||
let arg = arg.0;
|
let arg = arg.0;
|
||||||
(Self, Box::new(NumExpr::Neg { arg }).into())
|
(Self, Box::new(NumExpr::Neg(argus_core::expr::Neg { arg })).into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -154,7 +167,7 @@ impl Add {
|
||||||
#[new]
|
#[new]
|
||||||
fn new(args: Vec<PyNumExpr>) -> (Self, PyNumExpr) {
|
fn new(args: Vec<PyNumExpr>) -> (Self, PyNumExpr) {
|
||||||
let args: Vec<NumExpr> = args.into_iter().map(|arg| *arg.0).collect();
|
let args: Vec<NumExpr> = args.into_iter().map(|arg| *arg.0).collect();
|
||||||
(Self, Box::new(NumExpr::Add { args }).into())
|
(Self, Box::new(NumExpr::Add(argus_core::expr::Add { args })).into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -167,7 +180,7 @@ impl Sub {
|
||||||
fn new(lhs: PyNumExpr, rhs: PyNumExpr) -> (Self, PyNumExpr) {
|
fn new(lhs: PyNumExpr, rhs: PyNumExpr) -> (Self, PyNumExpr) {
|
||||||
let lhs = lhs.0;
|
let lhs = lhs.0;
|
||||||
let rhs = rhs.0;
|
let rhs = rhs.0;
|
||||||
(Self, Box::new(NumExpr::Sub { lhs, rhs }).into())
|
(Self, Box::new(NumExpr::Sub(argus_core::expr::Sub { lhs, rhs })).into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -179,7 +192,7 @@ impl Mul {
|
||||||
#[new]
|
#[new]
|
||||||
fn new(args: Vec<PyNumExpr>) -> (Self, PyNumExpr) {
|
fn new(args: Vec<PyNumExpr>) -> (Self, PyNumExpr) {
|
||||||
let args: Vec<NumExpr> = args.into_iter().map(|arg| *arg.0).collect();
|
let args: Vec<NumExpr> = args.into_iter().map(|arg| *arg.0).collect();
|
||||||
(Self, Box::new(NumExpr::Mul { args }).into())
|
(Self, Box::new(NumExpr::Mul(argus_core::expr::Mul { args })).into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -192,7 +205,10 @@ impl Div {
|
||||||
fn new(dividend: PyNumExpr, divisor: PyNumExpr) -> (Self, PyNumExpr) {
|
fn new(dividend: PyNumExpr, divisor: PyNumExpr) -> (Self, PyNumExpr) {
|
||||||
let dividend = dividend.0;
|
let dividend = dividend.0;
|
||||||
let divisor = divisor.0;
|
let divisor = divisor.0;
|
||||||
(Self, Box::new(NumExpr::Div { dividend, divisor }).into())
|
(
|
||||||
|
Self,
|
||||||
|
Box::new(NumExpr::Div(argus_core::expr::Div { dividend, divisor })).into(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -204,7 +220,7 @@ impl Abs {
|
||||||
#[new]
|
#[new]
|
||||||
fn new(arg: PyNumExpr) -> (Self, PyNumExpr) {
|
fn new(arg: PyNumExpr) -> (Self, PyNumExpr) {
|
||||||
let arg = arg.0;
|
let arg = arg.0;
|
||||||
(Self, Box::new(NumExpr::Abs { arg }).into())
|
(Self, Box::new(NumExpr::Abs(argus_core::expr::Abs { arg })).into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -238,7 +254,7 @@ pub struct ConstBool;
|
||||||
impl ConstBool {
|
impl ConstBool {
|
||||||
#[new]
|
#[new]
|
||||||
fn new(val: bool) -> (Self, PyBoolExpr) {
|
fn new(val: bool) -> (Self, PyBoolExpr) {
|
||||||
(Self, Box::new(BoolExpr::BoolLit(val)).into())
|
(Self, Box::new(BoolExpr::BoolLit(argus_core::expr::BoolLit(val))).into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -249,7 +265,10 @@ pub struct VarBool;
|
||||||
impl VarBool {
|
impl VarBool {
|
||||||
#[new]
|
#[new]
|
||||||
fn new(name: String) -> (Self, PyBoolExpr) {
|
fn new(name: String) -> (Self, PyBoolExpr) {
|
||||||
(Self, Box::new(BoolExpr::BoolVar { name }).into())
|
(
|
||||||
|
Self,
|
||||||
|
Box::new(BoolExpr::BoolVar(argus_core::expr::BoolVar { name })).into(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -265,7 +284,10 @@ impl Cmp {
|
||||||
let op = op.0;
|
let op = op.0;
|
||||||
let lhs = lhs.0;
|
let lhs = lhs.0;
|
||||||
let rhs = rhs.0;
|
let rhs = rhs.0;
|
||||||
(Self, Box::new(BoolExpr::Cmp { op, lhs, rhs }).into())
|
(
|
||||||
|
Self,
|
||||||
|
Box::new(BoolExpr::Cmp(argus_core::expr::Cmp { op, lhs, rhs })).into(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -310,7 +332,7 @@ impl Not {
|
||||||
#[new]
|
#[new]
|
||||||
fn new(arg: PyBoolExpr) -> (Self, PyBoolExpr) {
|
fn new(arg: PyBoolExpr) -> (Self, PyBoolExpr) {
|
||||||
let arg = arg.0;
|
let arg = arg.0;
|
||||||
(Self, PyBoolExpr(Box::new(BoolExpr::Not { arg })))
|
(Self, PyBoolExpr(Box::new(BoolExpr::Not(argus_core::expr::Not { arg }))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -322,7 +344,10 @@ impl And {
|
||||||
#[new]
|
#[new]
|
||||||
fn new(args: Vec<PyBoolExpr>) -> (Self, PyBoolExpr) {
|
fn new(args: Vec<PyBoolExpr>) -> (Self, PyBoolExpr) {
|
||||||
let args: Vec<BoolExpr> = args.into_iter().map(|arg| *arg.0).collect();
|
let args: Vec<BoolExpr> = args.into_iter().map(|arg| *arg.0).collect();
|
||||||
(Self, PyBoolExpr(Box::new(BoolExpr::And { args })))
|
(
|
||||||
|
Self,
|
||||||
|
PyBoolExpr(Box::new(BoolExpr::And(argus_core::expr::And { args }))),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -334,7 +359,7 @@ impl Or {
|
||||||
#[new]
|
#[new]
|
||||||
fn new(args: Vec<PyBoolExpr>) -> (Self, PyBoolExpr) {
|
fn new(args: Vec<PyBoolExpr>) -> (Self, PyBoolExpr) {
|
||||||
let args: Vec<BoolExpr> = args.into_iter().map(|arg| *arg.0).collect();
|
let args: Vec<BoolExpr> = args.into_iter().map(|arg| *arg.0).collect();
|
||||||
(Self, PyBoolExpr(Box::new(BoolExpr::Or { args })))
|
(Self, PyBoolExpr(Box::new(BoolExpr::Or(argus_core::expr::Or { args }))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -346,7 +371,10 @@ impl Next {
|
||||||
#[new]
|
#[new]
|
||||||
fn new(arg: PyBoolExpr) -> (Self, PyBoolExpr) {
|
fn new(arg: PyBoolExpr) -> (Self, PyBoolExpr) {
|
||||||
let arg = arg.0;
|
let arg = arg.0;
|
||||||
(Self, PyBoolExpr(Box::new(BoolExpr::Next { arg })))
|
(
|
||||||
|
Self,
|
||||||
|
PyBoolExpr(Box::new(BoolExpr::Next(argus_core::expr::Next { arg }))),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -356,14 +384,18 @@ pub struct Always;
|
||||||
#[pymethods]
|
#[pymethods]
|
||||||
impl Always {
|
impl Always {
|
||||||
#[new]
|
#[new]
|
||||||
fn new(arg: PyBoolExpr) -> (Self, PyBoolExpr) {
|
#[pyo3(signature = (arg, *, interval=(None, None)))]
|
||||||
|
fn new(arg: PyBoolExpr, interval: (Option<f64>, Option<f64>)) -> (Self, PyBoolExpr) {
|
||||||
let arg = arg.0;
|
let arg = arg.0;
|
||||||
|
let interval: Interval = match interval {
|
||||||
|
(None, None) => (..).into(),
|
||||||
|
(None, Some(b)) => (..Duration::from_secs_f64(b)).into(),
|
||||||
|
(Some(a), None) => (Duration::from_secs_f64(a)..).into(),
|
||||||
|
(Some(a), Some(b)) => (Duration::from_secs_f64(a)..Duration::from_secs_f64(b)).into(),
|
||||||
|
};
|
||||||
(
|
(
|
||||||
Self,
|
Self,
|
||||||
PyBoolExpr(Box::new(BoolExpr::Always {
|
PyBoolExpr(Box::new(BoolExpr::Always(argus_core::expr::Always { arg, interval }))),
|
||||||
arg,
|
|
||||||
interval: (..).into(),
|
|
||||||
})),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -374,14 +406,21 @@ pub struct Eventually;
|
||||||
#[pymethods]
|
#[pymethods]
|
||||||
impl Eventually {
|
impl Eventually {
|
||||||
#[new]
|
#[new]
|
||||||
fn new(arg: PyBoolExpr) -> (Self, PyBoolExpr) {
|
#[pyo3(signature = (arg, *, interval=(None, None)))]
|
||||||
|
fn new(arg: PyBoolExpr, interval: (Option<f64>, Option<f64>)) -> (Self, PyBoolExpr) {
|
||||||
let arg = arg.0;
|
let arg = arg.0;
|
||||||
|
let interval: Interval = match interval {
|
||||||
|
(None, None) => (..).into(),
|
||||||
|
(None, Some(b)) => (..Duration::from_secs_f64(b)).into(),
|
||||||
|
(Some(a), None) => (Duration::from_secs_f64(a)..).into(),
|
||||||
|
(Some(a), Some(b)) => (Duration::from_secs_f64(a)..Duration::from_secs_f64(b)).into(),
|
||||||
|
};
|
||||||
(
|
(
|
||||||
Self,
|
Self,
|
||||||
PyBoolExpr(Box::new(BoolExpr::Eventually {
|
PyBoolExpr(Box::new(BoolExpr::Eventually(argus_core::expr::Eventually {
|
||||||
arg,
|
arg,
|
||||||
interval: (..).into(),
|
interval,
|
||||||
})),
|
}))),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -392,16 +431,23 @@ pub struct Until;
|
||||||
#[pymethods]
|
#[pymethods]
|
||||||
impl Until {
|
impl Until {
|
||||||
#[new]
|
#[new]
|
||||||
fn new(lhs: PyBoolExpr, rhs: PyBoolExpr) -> (Self, PyBoolExpr) {
|
#[pyo3(signature = (lhs, rhs, *, interval=(None, None)))]
|
||||||
|
fn new(lhs: PyBoolExpr, rhs: PyBoolExpr, interval: (Option<f64>, Option<f64>)) -> (Self, PyBoolExpr) {
|
||||||
let lhs = lhs.0;
|
let lhs = lhs.0;
|
||||||
let rhs = rhs.0;
|
let rhs = rhs.0;
|
||||||
|
let interval: Interval = match interval {
|
||||||
|
(None, None) => (..).into(),
|
||||||
|
(None, Some(b)) => (..Duration::from_secs_f64(b)).into(),
|
||||||
|
(Some(a), None) => (Duration::from_secs_f64(a)..).into(),
|
||||||
|
(Some(a), Some(b)) => (Duration::from_secs_f64(a)..Duration::from_secs_f64(b)).into(),
|
||||||
|
};
|
||||||
(
|
(
|
||||||
Self,
|
Self,
|
||||||
PyBoolExpr(Box::new(BoolExpr::Until {
|
PyBoolExpr(Box::new(BoolExpr::Until(argus_core::expr::Until {
|
||||||
lhs,
|
lhs,
|
||||||
rhs,
|
rhs,
|
||||||
interval: (..).into(),
|
interval,
|
||||||
})),
|
}))),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ impl From<PyArgusError> for PyErr {
|
||||||
fn from(value: PyArgusError) -> Self {
|
fn from(value: PyArgusError) -> Self {
|
||||||
use argus_core::Error::*;
|
use argus_core::Error::*;
|
||||||
match value.0 {
|
match value.0 {
|
||||||
err @ (IncompleteArgs | InvalidOperation | IdentifierRedeclaration) => {
|
err @ (IncompleteArgs | InvalidOperation | IdentifierRedeclaration | InvalidInterval { reason: _ }) => {
|
||||||
PyValueError::new_err(err.to_string())
|
PyValueError::new_err(err.to_string())
|
||||||
}
|
}
|
||||||
err @ (InvalidPushToSignal
|
err @ (InvalidPushToSignal
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use argus_core::signals::{AnySignal, Signal};
|
use argus_core::signals::{AnySignal, Signal};
|
||||||
use argus_semantics::{BooleanSemantics, QuantitativeSemantics, Semantics, Trace};
|
use argus_semantics::{BooleanSemantics, QuantitativeSemantics, Trace};
|
||||||
use pyo3::exceptions::PyTypeError;
|
use pyo3::exceptions::PyTypeError;
|
||||||
use pyo3::prelude::*;
|
use pyo3::prelude::*;
|
||||||
use pyo3::types::{PyDict, PyString};
|
use pyo3::types::{PyDict, PyString};
|
||||||
|
|
@ -84,12 +84,12 @@ impl Trace for PyTrace {
|
||||||
|
|
||||||
#[pyfunction]
|
#[pyfunction]
|
||||||
fn eval_bool_semantics(expr: &PyBoolExpr, trace: &PyTrace) -> PyResult<Py<BoolSignal>> {
|
fn eval_bool_semantics(expr: &PyBoolExpr, trace: &PyTrace) -> PyResult<Py<BoolSignal>> {
|
||||||
let sig = BooleanSemantics::eval(&expr.0, trace, ()).map_err(PyArgusError::from)?;
|
let sig = BooleanSemantics::eval(&expr.0, trace).map_err(PyArgusError::from)?;
|
||||||
Python::with_gil(|py| Py::new(py, (BoolSignal::from(sig), BoolSignal::super_type())))
|
Python::with_gil(|py| Py::new(py, (BoolSignal::from(sig), BoolSignal::super_type())))
|
||||||
}
|
}
|
||||||
#[pyfunction]
|
#[pyfunction]
|
||||||
fn eval_robust_semantics(expr: &PyBoolExpr, trace: &PyTrace) -> PyResult<Py<FloatSignal>> {
|
fn eval_robust_semantics(expr: &PyBoolExpr, trace: &PyTrace) -> PyResult<Py<FloatSignal>> {
|
||||||
let sig = QuantitativeSemantics::eval(&expr.0, trace, ()).map_err(PyArgusError::from)?;
|
let sig = QuantitativeSemantics::eval(&expr.0, trace).map_err(PyArgusError::from)?;
|
||||||
Python::with_gil(|py| Py::new(py, (FloatSignal::from(sig), FloatSignal::super_type())))
|
Python::with_gil(|py| Py::new(py, (FloatSignal::from(sig), FloatSignal::super_type())))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,22 @@
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use argus_core::signals::{InterpolationMethod, Signal};
|
use argus_core::signals::Signal;
|
||||||
use pyo3::prelude::*;
|
use pyo3::prelude::*;
|
||||||
|
|
||||||
use crate::{DType, PyArgusError};
|
use crate::{DType, PyArgusError};
|
||||||
|
|
||||||
|
#[pyclass(name = "InterpolationMethod", module = "argus")]
|
||||||
|
#[derive(Debug, Clone, Copy, Default)]
|
||||||
|
pub enum PyInterp {
|
||||||
|
#[default]
|
||||||
|
Linear,
|
||||||
|
}
|
||||||
|
|
||||||
#[pyclass(name = "Signal", subclass, module = "argus")]
|
#[pyclass(name = "Signal", subclass, module = "argus")]
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct PySignal {
|
pub struct PySignal {
|
||||||
pub kind: DType,
|
pub kind: DType,
|
||||||
pub interpolation: InterpolationMethod,
|
pub interpolation: PyInterp,
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! impl_signals {
|
macro_rules! impl_signals {
|
||||||
|
|
@ -23,7 +30,7 @@ macro_rules! impl_signals {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn super_type() -> PySignal {
|
pub fn super_type() -> PySignal {
|
||||||
PySignal {
|
PySignal {
|
||||||
interpolation: InterpolationMethod::Linear,
|
interpolation: PyInterp::Linear,
|
||||||
kind: DType::$ty_name,
|
kind: DType::$ty_name,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue