Metadata-Version: 2.2
Name: stlrom
Version: 0.4.0
Summary: A library for robust online monitoring of STL specifications.
Keywords: cpp,cmake,extension,
Author-Email: Alexandre Donzé <alexandre.donze@univ-grenoble-alpes.fr>
License: -- STLRom -- 
         
         Copyright (c) 2024, Université Grenoble Alpes, Laboratoire Verimag
         All rights reserved.
         
         Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
         
            * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
            * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
            * Neither the name of Decyphir, Inc, nor the name of the University of California, Berkeley, nor the name of Verimag, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
         
         THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
         
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.12
Requires-Dist: matplotlib
Provides-Extra: dev
Requires-Dist: ipykernel; extra == "dev"
Requires-Dist: ipywidgets; extra == "dev"
Description-Content-Type: text/markdown

# STLRom

STLRom is a C++ library with python bindings for Robust Online Monitoring of Signal Temporal Logic. It computes interval robustness loosely as defined in 'Robust online monitoring of signal temporal logic' by Deshmuk et al, although the algorithm is not exactly the one described in this paper.

## Install

### Requirements

Building and installing STLRom has been tested on Linux (Ubuntu) and Mac OS. 
STLRom depends on "recent" versions of Flex and Bison. To install them on Linux, use `apt`. On Mac OS, Homebrew is required:
```
brew install bison flex 
``` 
If you don't like Homebrew, it is probably not *required*, provided you like messing with `CMakeList.txt` files. As long as the thing can find the required Bison and Flex libraries, that should work.

### Install from pypi

In an environment with pip, first make sure that `python-dev` or equivalent is installed, e.g., in Ubuntu 24.04:

```bash
sudo apt install python-dev-is-python3
```

To build the library, `bison` and `flex` are also required, as mentioned above.  Then build and install with pip:

```bash
pip install stlrom
```

### Install from source

Requirements: same as above (`bison`,`flex` and `python-dev`).

### Python

#### Using `pip`

From the `stlrom` folder, install with

```bash
pip install -e .
```

#### Using `uv` (Recommended)

A python interface for stlrom is implemented with pybind11. To build and install it in a local virtual environment we recommend using [uv](https://docs.astral.sh/uv/getting-started/installation/).
To create a virtual environment, build and install stlrom, run:

```bash
uv venv
source .venv/bin/activate
uv pip install -e .
```

Note this requires compiling the C++ library, so cmake and a C++ compiler must be available.

It is also possible to install the Python interface by simply running the following command in the project's root directory:

```bash
uv sync
```

then you can activate the virtual environment by running

```bash
source .venv/bin/activate
```

and then using Python normally inside the virtual enviornment.

#### Testing the python bindings

In the build folder, execute

```bash
make test-python
```

This will run the unit tests python programs from the `test` folder. This assumes having run `cmake`, having built the Python library, and having activated the `uv` virtual environment.

### Building the C++ library only

STLRom is primarily a C++ library. It comes with a standard cmake configuration file. Assuming cmake and a C++ development environment are installed, a library called stlromlib can be built with

```bash
mkdir build; cd build
cmake ..
make
```

#### Testing the C++ Library

After running `cmake` in the `build` folder and building the C++ library using `make`, you can run:
```bash
make test-cpp
```
to run C++ test files in the `test` folder.

**Note**: you can also execute `make test` to run both C++ and Python test units.

## Usage

### Tutorial Notebook

The following notebooks are hopefully good resources to get started. 

- [test/tutorial_notebook1.ipynb](test/tutorial_notebook1.ipynb) is formated as a quick tutorial for stlrom.
- [test/tutorial_notebook2.ipynb](test/tutorial_notebook2.ipynb) is longer and has plots. 

Of course, at least `ipykernel` is required for the notebooks execution. 

###  A Complete Example

```python
import stlrom


# define a "driver", which can parse and store a context of STL formulas, as well as data
stl_driver =stlrom.STLDriver()

s="""
signal x, y    # signal names
mux := x[t]>0  # simple predicate 

param a=1, b=2, p = -3   
muy := a*y[t] + b > p   # operation and parameter in predicate

phi1 := alw_[0, 5] mux               # always (G works too) 
phi2 := ev_[3, 4] (mux) or phi1      # eventually (or F)
phi_until := phi1 until_[0, 1] phi2  # until 
"""

# parse the formulas
stl_driver.parse_string(s)  # or write the above in spec.stl, and use parse_file('spec.stl')

# add data as timed samples
stl_driver.add_sample([0,  2 , 1 ])  # must be of the format [t, x_val, y_val]
stl_driver.add_sample([0.5, -3, 2])  # i.e., contain signal value with same order as declared
stl_driver.add_sample([2.1, 10, 20])

print(stl_driver)
print('Robustness of phi1 at time 0.: ', phi1.get_rob())
print('Robustness of phi1 from time 1.: ', phi1.get_rob(1.))
```

### Test folder

In you don't mind the mess, the `test` folder contains several examples and test programs and scripts which may be useful. 

