refactor(pyargus): implement new for PySignal
This commit is contained in:
parent
e2cfe3da56
commit
8027f86213
2 changed files with 18 additions and 16 deletions
|
|
@ -7,7 +7,7 @@ use pyo3::prelude::*;
|
||||||
use pyo3::types::{PyDict, PyString};
|
use pyo3::types::{PyDict, PyString};
|
||||||
|
|
||||||
use crate::expr::PyBoolExpr;
|
use crate::expr::PyBoolExpr;
|
||||||
use crate::signals::{BoolSignal, FloatSignal, PySignal, SignalKind};
|
use crate::signals::{BoolSignal, FloatSignal, PyInterp, PySignal, SignalKind};
|
||||||
use crate::PyArgusError;
|
use crate::PyArgusError;
|
||||||
|
|
||||||
#[pyclass(name = "Trace", module = "argus")]
|
#[pyclass(name = "Trace", module = "argus")]
|
||||||
|
|
@ -63,12 +63,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, BoolSignal::super_type(sig.into()))))
|
Python::with_gil(|py| Py::new(py, (BoolSignal, PySignal::new(sig, PyInterp::Linear))))
|
||||||
}
|
}
|
||||||
#[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, 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<()> {
|
pub fn init(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,18 @@ pub struct PySignal {
|
||||||
pub(crate) signal: SignalKind,
|
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]
|
#[pymethods]
|
||||||
impl PySignal {
|
impl PySignal {
|
||||||
#[getter]
|
#[getter]
|
||||||
|
|
@ -106,23 +118,13 @@ macro_rules! impl_signals {
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
pub struct [<$ty_name Signal>];
|
pub struct [<$ty_name Signal>];
|
||||||
|
|
||||||
impl [<$ty_name Signal>] {
|
|
||||||
#[inline]
|
|
||||||
pub fn super_type(signal: SignalKind) -> PySignal {
|
|
||||||
PySignal {
|
|
||||||
interpolation: PyInterp::Linear,
|
|
||||||
signal,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[pymethods]
|
#[pymethods]
|
||||||
impl [<$ty_name Signal>] {
|
impl [<$ty_name Signal>] {
|
||||||
/// Create a new empty signal
|
/// Create a new empty signal
|
||||||
#[new]
|
#[new]
|
||||||
#[pyo3(signature = ())]
|
#[pyo3(signature = ())]
|
||||||
fn new() -> (Self, PySignal) {
|
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
|
/// 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>> {
|
fn constant(_: &PyType, py: Python<'_>, value: $ty) -> PyResult<Py<Self>> {
|
||||||
Py::new(
|
Py::new(
|
||||||
py,
|
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| {
|
Python::with_gil(|py| {
|
||||||
Py::new(
|
Py::new(
|
||||||
py,
|
py,
|
||||||
(Self, Self::super_type(ret.into()))
|
(Self, PySignal::new(ret, PyInterp::Linear))
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue