Skip to main content

adjudicator

Adjudicator (nount): An adjudicator is a person or body that makes formal judgments on a disputed matter. They are the ones who settle disputes or decide who is right in a disagreement. This could be a judge in a courtroom, an arbitrator in a negotiation, or any person or system given the power to make decisions of this type.

Adjudicator is a framework for implementing type-based rule engines largely inspired by the Pants build system. The rule graph consists of nodes which are concrete Python types and edges which are functions that take a set of input types and produce an output type.

Rules are matched on a set of facts, which populate the possible input types of a rule. Deriving a type may require chained execution of rules, which is supported. Global facts may be used to populate a potential input type for all rule executions.

The Adjudicator rule engine is designed to be used in a request-response fashion. A request is a set of facts that should be used to derive a type. A response is the derived type. The rule engine will execute the rules necessary to derive the type and return the response. If a rule was already executed with the same inputs, it will not be executed again. For optimal use of the caching mechanism, all types participating in the rule evaluation should be immutable and implement a stable hash (e.g. using @dataclass(frozen=True) and tuple instead of list, etc.).

When a mutable type should intentionally participate in the rule evaluation, usually this works automatically because the hash of a Python object that does not provide a custom __hash__() implementation or disables its hashing is based on the object's identity. This means that the hash is stable for the memory allocation of the object, and will not change if the object is mutated. For types that do not support hashing, support can be enabled explicitly using the RuleEngine.hashsupport object.

Table of Contents

Quickstart

The following example shows how to use Adjudicator to implement a simple "Hello World" application. The rule engine invokes the say_hello() production rule because a HelloResponse is requested and a HelloRequest is provided, which matches the rule's signature.

# fmt: off
# ruff: noqa
from dataclasses import dataclass
from adjudicator import Params, RuleEngine, rule

@dataclass(frozen=True)
class HelloRequest:
    name: str

@dataclass(frozen=True)
class HelloResponse:
    greeting: str

@rule()
def say_hello(request: HelloRequest) -> HelloResponse:
    return HelloResponse(greeting=f"Hello {request.name}!")

engine = RuleEngine()
engine.load_module(__name__)
response = engine.get(HelloResponse, Params(HelloRequest(name="World")))
print(response.greeting)

A more complex example can be found in the mksync project.

Installation

Adjudicator is available on PyPI. You need at least Python 3.10.

pip install python-adjudicator

Future Extensions

  • Currently the rule graph stores rules as connections between types and rules on edges. A more efficient representation would be the one illustrated above, where types are connected to rules which are connected to types.
  • The ability to mark facts as required to be consumed. If such a fact is not consumed during the execution of a request, an error will be raised.

Metadata

Release files for python-adjudicator 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-adjudicator 0.5.1
File Size Uploaded
python_adjudicator-0.5.1.tar.gz 15.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-adjudicator 0.5.1
File Interpreter ABI Platform
python_adjudicator-0.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 36.7 kB

Release files / python_adjudicator-0.5.1.tar.gz

Download URL python_adjudicator-0.5.1.tar.gz
Size 15.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c890fe0f129bcf2c30797b42bb8cd05d7d0f71fbe8646ebe2a551502af52d8d7
BLAKE2b-256 checksum
How to use checksums
780a6607ee577677aaf4b5f808b61c7a1e99da824873f43643a261e267d7d5dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.13

Release files / python_adjudicator-0.5.1-py3-none-any.whl

Download URL python_adjudicator-0.5.1-py3-none-any.whl
Size 21.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
20761fea1a9010167113a1ba0eabe1a71bd0c34d29304dc102926f409cc00d73
BLAKE2b-256 checksum
How to use checksums
1d84535db006b93a7a60e2924176639125a090ba04fa954c6d1400c1dad3fb32
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.13

Release history Release notifications | RSS feed

This release

0.5.1 This release

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page