refactor(pyargus): implement new for PySignal

This commit is contained in:
Anand Balakrishnan 2023-09-07 15:44:46 -07:00
parent e2cfe3da56
commit 8027f86213
No known key found for this signature in database
2 changed files with 18 additions and 16 deletions

View file

@ -7,7 +7,7 @@ use pyo3::prelude::*;
use pyo3::types::{PyDict, PyString};
use crate::expr::PyBoolExpr;
use crate::signals::{BoolSignal, FloatSignal, PySignal, SignalKind};
use crate::signals::{BoolSignal, FloatSignal, PyInterp, PySignal, SignalKind};
use crate::PyArgusError;
#[pyclass(name = "Trace", module = "argus")]
@ -63,12 +63,12 @@ impl Trace for PyTrace {
#[pyfunction]
fn eval_bool_semantics(expr: &PyBoolExpr, trace: &PyTrace) -> PyResult<Py<BoolSignal>> {
let sig = BooleanSemantics::eval(&expr.0, trace).map_err(PyArgusError::from)?;
Python::with_gil(|py| Py::new(py, (BoolSignal, BoolSignal::super_type(sig.into()))))
Python::with_gil(|py| Py::new(py, (BoolSignal, PySignal::new(sig, PyInterp::Linear))))
}
#[pyfunction]
fn eval_robust_semantics(expr: &PyBoolExpr, trace: &PyTrace) -> PyResult<Py<FloatSignal>> {
let sig = QuantitativeSemantics::eval(&expr.0, trace).map_err(PyArgusError::from)?;
Python::with_gil(|py| Py::new(py, (FloatSignal, FloatSignal::super_type(sig.into()))))
Python::with_gil(|py| Py::new(py, (FloatSignal, PySignal::new(sig, PyInterp::Linear))))
}
pub fn init(_py: Python, m: &PyModule) -> PyResult<()> {

View file

@ -40,6 +40,18 @@ pub struct PySignal {
pub(crate) signal: SignalKind,
}
impl PySignal {
pub fn new<T>(signal: T, interpolation: PyInterp) -> Self
where
T: Into<SignalKind>,
{
Self {
interpolation,
signal: signal.into(),
}
}
}
#[pymethods]
impl PySignal {
#[getter]
@ -106,23 +118,13 @@ macro_rules! impl_signals {
#[derive(Debug, Copy, Clone)]
pub struct [<$ty_name Signal>];
impl [<$ty_name Signal>] {
#[inline]
pub fn super_type(signal: SignalKind) -> PySignal {
PySignal {
interpolation: PyInterp::Linear,
signal,
}
}
}
#[pymethods]
impl [<$ty_name Signal>] {
/// Create a new empty signal
#[new]
#[pyo3(signature = ())]
fn new() -> (Self, PySignal) {
(Self, Self::super_type(Signal::<$ty>::new().into()))
(Self, PySignal::new(Signal::<$ty>::new(), PyInterp::Linear))
}
/// Create a new signal with constant value
@ -130,7 +132,7 @@ macro_rules! impl_signals {
fn constant(_: &PyType, py: Python<'_>, value: $ty) -> PyResult<Py<Self>> {
Py::new(
py,
(Self, Self::super_type(Signal::constant(value).into()))
(Self, PySignal::new(Signal::constant(value), PyInterp::Linear))
)
}
@ -144,7 +146,7 @@ macro_rules! impl_signals {
Python::with_gil(|py| {
Py::new(
py,
(Self, Self::super_type(ret.into()))
(Self, PySignal::new(ret, PyInterp::Linear))
)
})
}