Skip to main content

A lightweight DSL for business rules with dependency analysis

Project description

rulang (Python)

Python implementation of the Rulang business-rules DSL. Semantic parity with the TypeScript runtime is enforced by a shared spec suite.

Install

uv add rulang

Quick start

from rulang import RuleEngine

engine = RuleEngine(mode="all_match")
engine.add_rules([
    "entity.age >= 18 => entity.is_adult = true",
    "entity.is_adult == true => entity.discount += 0.1",
])

entity = {"age": 25, "is_adult": False, "discount": 0.0}
engine.evaluate(entity)
# entity is now {'age': 25, 'is_adult': True, 'discount': 0.1}

The full DSL (operators, paths, built-in functions, null-safe access) is documented in the top-level README and in the runtime-shipped reference (rulang.grammar_reference()).

Tooling API at a glance

  • rulang.parse(text) — returns a public AST with source spans on every node.
  • rulang.walk(node, visitor) — pre-order AST traversal.
  • rulang.format(rule) — canonical string form. Idempotent; round-trips through parse. Companion helpers: format_condition, format_action, format_path, format_expr.
  • rulang.detect_conflicts(rules, mode=…) — duplicate/contradiction/shadowing findings across a rule set.
  • engine.dry_run(entity) — structured trace with per-action before/after, aggregated diff, final entity, and return value.
  • rulang.validate(rule, resolver, severity_overrides=…) — list of Diagnostic. Schema-agnostic; consumers subclass BaseResolver and override hooks. Parse errors are returned as diagnostics — validate never raises.
  • rulang.builders — ergonomic AST constructors: rule, comparisons (eq, neq, lt, gt, lte, gte, in_, not_in, contains, startswith, endswith, matches, contains_any, contains_all), logical (and_, or_, not_), existence (exists, is_empty), actions (set_, compound, workflow_call, ret), and pathref/lit/float_lit/int_lit/fn/binary/unary/null_coalesce/workflow_expr.
  • rulang.grammar_reference() — canonical LLM-ready grammar cheatsheet (markdown), versioned with the package.

For worked examples of each feature, see the top-level README's "Tooling API" section.

Diagnostic codes

rulang.validation.DIAGNOSTIC_CODES lists every rulang-owned code. Consumer diagnostics should use their own namespace (e.g. myapp.*). Defaults and overrides are documented in the root README.

Repository layout

  • src/rulang/ — source
  • tests/ — Python-specific tests
  • tests/spec/ — cross-runtime spec runners; fixtures live in ../spec/cases/ and ../spec/feature-cases/
  • ../grammar/BusinessRules.g4 — shared ANTLR grammar (source of truth)
  • ../docs/proposals/ — design proposals for each capability

Development

# Dev install
uv sync --all-extras

# Run tests with coverage
uv run pytest tests/

# Regenerate parsers after grammar edits (run from repo root)
cd .. && npm run generate:parsers

License

MIT

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

rulang-1.1.2.tar.gz (171.6 kB view details)

Uploaded Source

Built Distribution

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

rulang-1.1.2-py3-none-any.whl (84.0 kB view details)

Uploaded Python 3

File details

Details for the file rulang-1.1.2.tar.gz.

File metadata

  • Download URL: rulang-1.1.2.tar.gz
  • Upload date:
  • Size: 171.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for rulang-1.1.2.tar.gz
Algorithm Hash digest
SHA256 f571f5baf5de74829ac4677168472c49e961346848e59aa993de109f5f8b8b97
MD5 925c0c2af1107a6700978a8a162c3f32
BLAKE2b-256 9328ff6ec7465ab39ac8beb2ef74ee7beaf4898df01e15992331c943678b659f

See more details on using hashes here.

File details

Details for the file rulang-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: rulang-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 84.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for rulang-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 04103801fce8a9d2abd37503423c274a24384b6f16bb651fe234c32909bde52f
MD5 8f39f65c7afae63fdcf54bba85c523e3
BLAKE2b-256 665f886a8722c6402883c0513844c85a73415bfa4dc41000d4b11bbdac64e0a8

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