Skip to main content

Python bindings for the RePA/ReForma probabilistic automaton tool

Project description

ReForma — Reconfigurable Formal Automata in Python

A clean Python library that wraps the RePATool.jar CLI via subprocess, giving you a Pythonic interface for simulation, training, PDL/PCTL verification, and interactive graph visualizations.


Installation

You can install it directly from PyPI (once published):

pip install reforma

For visualization features (offline PNGs and interactive Jupyter graphs), install the required extras:

pip install networkx matplotlib

(Local Development: Run pip install -e . from the project root). Requires Python 3.10+ and a working java on your PATH.


Quick Start

from reforma import ReForma

modelo = ReForma()

# Load a model
state = modelo.load_file("recommender.r")

# Get a beautiful printout of the current state and probabilities
print(state.summary())

# Simulate
state = modelo.step("go_work")
state = modelo.step("easy_task")

# Undo last step
state = modelo.undo()            

# Reset to initial state
modelo.reset()

Visualization (Jupyter & Offline)

ReForma provides powerful graphing tools directly integrated into your workflow.

from reforma import ReForma

modelo = ReForma()
modelo.load_file("model.r")

# 1. Interactive Jupyter View 
# Renders a drag-and-drop Cytoscape.js graph right inside your Notebook!
modelo.show_interactive()

# 2. Offline Static Images
# Renders a high-res graph using Matplotlib/NetworkX (no browser needed)
modelo.save_image_plt("output/my_graph.png")

Simulation & State Inspection

state = modelo.load_file("model.r")

# Inspect variables
print(state.variables)   # {'counter': 0, 'flag': 1}

# Check enabled transitions manually
for t in state.enabled:
    print(f"{t.label}: {t.from_state}{t.to_state}  (p={t.probability:.3f})")

# History of labels taken
print(modelo.history)    # ['go_work', 'easy_task']

Training

Train the model on a batch of sessions (lists of event labels) to automatically update the weights:

modelo.train([
    ["go_work", "easy_task", "easy_task", "go_home"],
    ["battery_low", "go_charge", "finish_charge", "socialize"],
    ["no_money", "go_work", "go_home"],
])

# Or train directly from a log file (one session per line, comma-separated)
modelo.train_from_file("logs/sessions.txt")

# Save the updated model with the new calculated weights
modelo.save_source("model_trained.r")

PDL / PCTL Verification

# Quantitative: probability of eventually reaching Office
prob = modelo.check_pdl_value("Home", "{P=?[F Office]}")
print(f"P(reach Office from Home) = {prob:.4f}")

# Qualitative: is it probable?
holds = modelo.check_pdl_value("Home", "{P>=0.4[F Office]}")
print(f"P>=0.4? {holds}")   # True / False

# PDL: is there a path via go_work to Office?
holds = modelo.check_pdl_value("Home", "<go_work>Office")
print(holds)   # True

# Get the raw string result
raw = modelo.check_pdl("Home", "{P=?[F Office]}")
print(raw)  # "Result: 0.50000"

Formula syntax reference

Formula Meaning
{P=?[F target]} Probability of eventually reaching target
{P=?[G safe]} Probability of staying in safe forever
{P=?[X next]} Probability of reaching next in one step
{P=?[a U b]} Probability of a until b
{P>=0.5[F target]} Is probability of reaching target ≥ 0.5?
<action>state There exists a path via action to state
[action]state All paths via action lead to state

Export

# PRISM DTMC
prism_code = modelo.export_prism()
modelo.save_prism("output/model.pm")

# GLTS (imperative translation)
glts_code = modelo.export_glts()

# Mermaid diagram (initial state)
diagram = modelo.export_mermaid()

# Mermaid diagram (full LTS — all reachable states)
full_diagram = modelo.export_mermaid(full_lts=True)

Loading from a string

source = """
name MyModel
init s0
s0 ---> s1: a (0.6)
s0 ---> s2: b (0.4)
s1 ---> s0: back (1.0)
"""

state = modelo.load(source, name="MyModel")

Error handling

from reforma.jar_bridge import JarError

try:
    result = modelo.check_pdl("Home", "{P=?[F Office]}")
except JarError as e:
    print(f"JAR error: {e}")
except RuntimeError as e:
    print(f"Usage error: {e}")   # e.g. no model loaded, invalid transition

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reforma-0.1.2.tar.gz (11.5 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

reforma-0.1.2-py3-none-any.whl (11.5 MB view details)

Uploaded Python 3

File details

Details for the file reforma-0.1.2.tar.gz.

File metadata

  • Download URL: reforma-0.1.2.tar.gz
  • Upload date:
  • Size: 11.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for reforma-0.1.2.tar.gz
Algorithm Hash digest
SHA256 e7740739e4391c00908e1d578242fc2a37e386060b676991f8ec416f52ed9bf9
MD5 1998d086a43e72b9c2e4fc18e919ca0c
BLAKE2b-256 52b8c9514fa55e92ec5e54417c1dcb6fda6bf09a501cacf00f91edc861ecab23

See more details on using hashes here.

File details

Details for the file reforma-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: reforma-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 11.5 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for reforma-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 89b301954c55a4e91ffb6fa1dbb246a678e47add087fc811be50e2aaa17f4a7c
MD5 c660ba84358e48c9a0ec0a3cb06e3033
BLAKE2b-256 259d67564dc64df0f96c26478bf43e3e4da6b4bea9eef7586291388d4d14520e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page