Skip to content

require

relationalai.semantics
require(*args: Statement) -> Fragment

Add requirements (constraints) to the active model.

This is a convenience wrapper around Model.require that operates on the single active model. It returns a composable Fragment representing one or more requirements. Requirements can be unconditional (calling require directly) or conditional by chaining Fragment.where on the returned fragment. You can also apply requirements to an existing fragment by calling Fragment.require.

If you have more than one model, call Model.require on the specific model instead.

A requirement is an existence check: it fails when there are no matches. Roughly speaking, require(expr) asserts EXISTS(expr) and fails when NOT EXISTS(expr). For example, require(Person.age > 0) fails if there are no people, if no one has an age value, or if no age values are greater than zero.

To enforce a per-entity requirement, scope it with where (for example, where(Person).require(Person.age > 0)) or use Concept.require (for example, Person.require(Person.age > 0)). Both of those forms are equivalent and the choice is mostly a matter of style.

  • *args

    (Statement, default: ()) - One or more semantics.frontend.base.Statement items to enforce as requirements.

    Common examples include:

    • Comparison operators: Person.age >= 0 (requires at least one matching fact)
    • Property existence: Person.name (requires at least one name fact)
    • Entity existence: Person (requires at least one Person entity)
  • Fragment - A composable fragment representing the requirement(s).
  • relationalai.util.error.RAIException - If there is no active model, or if multiple models exist and the active model would be ambiguous.

Require that at least one match exists:

>>> from relationalai.semantics import Integer, Model, require, where
>>> m = Model()
>>> Person = m.Concept("Person")
>>> m.define(Person.new(), Person.new(age=1))
>>> # Passes because there exists a Person with age >= 0.
>>> require(Person.age >= 0)

Enforce a requirement per entity by scoping it with where:

>>> # Requires every Person to have an age >= 0.
>>> where(Person).require(Person.age >= 0)

Add a requirement on an existing fragment:

>>> from relationalai.semantics import where
>>> where(Person).require(Person.age >= 0)

Requirements can have performance implications (they may add work, but can also make queries faster by narrowing what needs to be considered). For more detail and guidance, see Model.require.

RelationalAI Documentation
└──  Build With RelationalAI
    └──  Understand how PyRel works > Configure PyRel
        └──  Configure reasoners
            └──  Configure logic reasoner defaults