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.1.tar.gz (5.6 kB 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.1-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for reforma-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3e3378dffa491a9793b8c5e60765d945ce0daf4e9a3013911663bfccc5052b89
MD5 d54d61db50c166a2f197700ec105933c
BLAKE2b-256 a4d8b18e8201418f5c395d828151f53d220a62457368d199b60ea9e476f81df3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: reforma-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.1 kB
  • 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 42b55d494772b297abe98e32b14e55a006fc54d45b88d5dfc058de2b4f3ca1e3
MD5 e35b9a05e465f84c86f1e987e4729af8
BLAKE2b-256 4a0dbf513a37e1bb10d80ba2091b7e5bf17da2c13ddc47ddcfd39ea44a6a7405

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