galactic.algebras.convex.predicates.core#

galactic.algebras.convex.predicates.core module

Defines basic predicates used in convex algebras.

Predicates

This module defines basic predicates used in convex algebras, including:

  • Predicate: Base class for all predicates

  • Absurd: Represents an absurd predicate (always false)

  • Degenerated: Represents a degenerated predicate (whose truth value is determined by a list of other predicates)

  • Tautology: Represents a tautological predicate (always true)

  • BooleanPredicate: Represents a predicate defined by a boolean characteristic

  • Equality: Represents equality between two elements

  • Different: Represents inequality between two elements

  • GreaterThan: Represents the β€œgreater than” relation

  • GreaterEqual: Represents the β€œgreater than or equal to” relation

  • LessThan: Represents the β€œless than” relation

  • LessEqual: Represents the β€œless than or equal to” relation

  • Membership: Represents membership in a set

class Predicate(**data)#

Bases: BaseModel, ABC

Represents a predicate.

Parameters:

space – The characteristics to be verified

Raises:
  • TypeError – If the space is not a tuple

  • TypeError – If the space does not contain only Characteristic instances

Notes

  • The __eq__ and __hash__ methods ensure that instances of this class can be compared and used in sets or as dictionary keys based on their characteristic and parameters, allowing for efficient storage and retrieval in collections.

  • The __str__ method provides a string representation of the predicate, formatted according to the specified template, space, and parameters. This makes it easy to visualize the predicate and understand its purpose when printed or logged. The template should contain {0} for the characteristic and {parameter_name} for each parameter used in the predicate. For example, if the template is β€œ{0} = {parameter_name!r}”, it will be formatted with the characteristic and the parameter value.

  • The parameters are passed as keyword arguments and can be accessed using their names in the template.

abstractmethod __call__(item)#

Make this predicate callable.

This method should not be overridden in subclasses.

Parameters:

item (Item) – An item

Returns:

A ternary boolean value indicating the truth of the predicate for the given item.

Return type:

TernaryBool

abstract property template: str#

Get the template.

Returns:

The template

Return type:

str

Notes

The template should contain {0} for the characteristic and {parameter_name} for each parameter used in the predicate. For example, if the template is β€œ{0} = {parameter_name!r}”, it will be formatted with the characteristic and the parameter value.

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class BasicPredicate(**data)#

Bases: Predicate, ABC

Represents a predicate.

Notes

  • The exception handling in the __call__ method ensures that if one of the characteristics returns None or if the value cannot be processed, the predicate will return None, indicating that the truth value is unknown.

  • This allows for flexible and robust handling of various data types and structures. The __call__ method is designed to be used as a callable predicate, allowing instances of this class to be used directly in conditional statements or other contexts where a truth value is required.

  • The __call__ method iterates over the values returned by the characteristics, applying the _truth method to each tuple of values. If any value returns False, the predicate returns False. If all values return True, it returns True. If one of the characteristics returns None or if an exception occurs during the truth evaluation, the predicate returns None, indicating that the truth value is unknown.

__call__(item)#

Make this predicate callable.

This method should not be overridden in subclasses.

Parameters:

item (Item) – An item

Returns:

A ternary boolean value indicating the truth of the predicate for the given item.

Return type:

TernaryBool

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class BooleanPredicate(**data)#

Bases: BasicPredicate

Represents an boolean predicate.

Parameters:

space – The characteristic to verify

property template: str#

Get the template.

Returns:

The template

Return type:

str

Notes

The template should contain {0} for the characteristic and {parameter_name} for each parameter used in the predicate. For example, if the template is β€œ{0} = {parameter_name!r}”, it will be formatted with the characteristic and the parameter value.

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class Absurd(**data)#

Bases: Predicate

Represents the absurd predicate, which is always false.

Examples

>>> from galactic.algebras.concept.core import Item
>>> from galactic.algebras.convex.characteristics.core import Component, String
>>> from galactic.algebras.convex.predicates.core import Absurd
>>> absurd = Absurd(space=(String(components=(Component(),)),))
>>> absurd
<galactic.algebras.convex.predicates.core.Absurd object at 0x...>
>>> str(absurd)
'βŠ₯'
>>> print(absurd(Item(value="This is an example string.")))
False
>>> print(absurd(Item(value="This is a test string.")))
False
>>> print(absurd(Item()))
False
>>> print(absurd(Item(value="")))
False
>>> print(absurd(Item(value=5)))
False

Notes

  • The Absurd class is a concrete implementation of the Predicate class that represents a predicate that is always false.

  • It is used to represent a situation where the predicate cannot be satisfied, indicating that the condition is impossible or contradictory.

  • The __call__ method is overridden to always return False, regardless of the input item’s datum. This means that any evaluation of this predicate will always return False, indicating that the condition is not met.

Hint

The Absurd class can be used in situations where a predicate is required, but the condition is inherently impossible or contradictory.

__call__(item)#

Return False.

Parameters:

item (Item) – An item

Returns:

TernaryBool.FALSE

Return type:

TernaryBool

property template: str#

Get the template.

Returns:

The template

Return type:

str

Notes

The template for the Absurd predicate is simply β€˜βŠ₯’, representing falsehood.

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class Degenerated(**data)#

Bases: Predicate, ABC

Represents a degenerated predicate.

Parameters:

predicates – The predicates to use to compute the truth value

__call__(item)#

Compute the truth value of the degenerated predicate.

Parameters:

item (Item) – An item

Returns:

The ternary truth value of the degenerated predicate.

Return type:

TernaryBool

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class Tautology(**data)#

Bases: Predicate

Represents the tautology predicate, which is always true.

Examples

>>> from galactic.algebras.concept.core import Item
>>> from galactic.algebras.convex.characteristics.core import Component, String
>>> from galactic.algebras.convex.predicates.core import Tautology
>>> tautology = Tautology(space=(String(components=(Component(),)),))
>>> tautology
<galactic.algebras.convex.predicates.core.Tautology object at 0x...>
>>> str(tautology)
'⊀'
>>> print(tautology(Item(value="This is an example string.")))
True
>>> print(tautology(Item(value="This is a test string.")))
True
>>> print(tautology(Item()))
True
>>> print(tautology(Item(value="")))
True
>>> print(tautology(Item(value=5)))
True

Notes

  • The Tautology class is a concrete implementation of the Predicate class that represents a predicate that is always true.

  • It is used to represent a situation where the predicate is always satisfied, indicating that the condition is universally true.

  • The Tautology class can be used in situations where a predicate is required but the condition is always true, such as in logical proofs or when a condition is known to be universally valid.

  • It has been added to mirror the Absurd class and provide a counterpart that represents a condition that is always true, allowing for a complete set of logical predicates.

__call__(item)#

Return True.

Parameters:

item (Item) – An item

Returns:

TernaryBool.TRUE

Return type:

TernaryBool

property template: str#

Get the template.

Returns:

The template

Return type:

str

Notes

The template for the Tautology predicate is simply β€˜βŠ€β€™, representing truth.

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class Equality(**data)#

Bases: BasicPredicate

Represents an equality predicate.

Parameters:
  • space – The characteristic to verify

  • value – The value to check for equality

Raises:
  • ValueError – If the space does not contain exactly one Characteristic when value is not a tuple

  • ValueError – If the length of space does not match the length of value when value is a tuple

Examples

>>> from galactic.algebras.concept.core import Item
>>> from galactic.algebras.convex.characteristics.core import Component, String
>>> from galactic.algebras.convex.predicates.core import Equality
>>> characteristic = String(components=(Component(),))
>>> value = "example"
>>> predicate = Equality(space=(characteristic,), value=value)
>>> predicate
<galactic.algebras.convex.predicates.core.Equality object at 0x...>
>>> str(predicate)
"str(@) = 'example'"
>>> print(predicate(Item(value="This is an example string.")))
False
>>> print(predicate(Item(value="This is a test string.")))
False
>>> print(predicate(Item(value="example")))
True
>>> print(predicate(Item(value=5)))
False
>>> print(predicate(Item()))
Unknown
>>> print(predicate(Item(value="")))
False
property template: str#

Get the template.

Returns:

The template for the equality predicate

Return type:

str

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class Different(**data)#

Bases: BasicPredicate

Represents a difference predicate.

Parameters:
  • space – The characteristic to verify

  • value – The value to check for difference

Raises:
  • ValueError – If the space does not contain exactly one Characteristic when value is not a tuple

  • ValueError – If the length of space does not match the length of value when value is a tuple

Examples

>>> from galactic.algebras.concept.core import Item
>>> from galactic.algebras.convex.characteristics.core import Component, String
>>> from galactic.algebras.convex.predicates.core import Different
>>> characteristic = String(components=(Component(),))
>>> value = "example"
>>> predicate = Different(space=(characteristic,), value=value)
>>> predicate
<galactic.algebras.convex.predicates.core.Different object at 0x...>
>>> str(predicate)
"str(@) β‰  'example'"
>>> print(predicate(Item(value="This is an example string.")))
True
>>> print(predicate(Item(value="This is a test string.")))
True
>>> print(predicate(Item(value="example")))
False
>>> print(predicate(Item()))
Unknown
>>> print(predicate(Item(value="")))
True
>>> print(predicate(Item(value=5)))
True

Notes

The Different class is a concrete implementation of the Predicate class that represents a predicate that checks if a value is different from a specified value.

Hint

The Different class can be used in situations where a predicate is required to check for inequality, such as in filtering data or validating conditions where a specific value should not be present.

property template: str#

Get the template.

Returns:

The template for the difference predicate

Return type:

str

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class GreaterThan(**data)#

Bases: BasicPredicate

Represents a greater than predicate.

Parameters:
  • space – The characteristic to verify

  • value – The value to check for greater than

Raises:
  • ValueError – If the space does not contain exactly one Characteristic when value is not a tuple

  • ValueError – If the length of space does not match the length of value when value is a tuple

Examples

>>> from galactic.algebras.concept.core import Item
>>> from galactic.algebras.convex.characteristics.core import Component, Number
>>> from galactic.algebras.convex.predicates.core import GreaterThan
>>> characteristic = Number(components=(Component(),))
>>> value = 10
>>> predicate = GreaterThan(space=(characteristic,), value=value)
>>> predicate
<galactic.algebras.convex.predicates.core.GreaterThan object at 0x...>
>>> str(predicate)
'float(@) > 10'
>>> print(predicate(Item(value=15.0)))
True
>>> print(predicate(Item(value=10.0)))
False
>>> print(predicate(Item(value=5.0)))
False
>>> print(predicate(Item()))
Unknown
>>> print(predicate(Item(value="")))
Unknown

Notes

The GreaterThan class is a concrete implementation of the Predicate class that represents a predicate that checks if a value is greater than a specified value.

Hint

The GreaterThan class can be used in situations where a predicate is required to check for greater than conditions, such as in filtering data or validating conditions where a value should exceed a certain threshold.

property template: str#

Get the template.

Returns:

The template for the greater than predicate

Return type:

str

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class GreaterEqual(**data)#

Bases: BasicPredicate

Represents a greater than or equal predicate.

Parameters:
  • space – The characteristic to verify

  • value – The value to check for greater than or equal to

Raises:
  • ValueError – If the space does not contain exactly one Characteristic when value is not a tuple

  • ValueError – If the length of space does not match the length of value when value is a tuple

Examples

>>> from galactic.algebras.concept.core import Item
>>> from galactic.algebras.convex.characteristics.core import Component, Number
>>> from galactic.algebras.convex.predicates.core import GreaterEqual
>>> characteristic = Number(components=(Component(),))
>>> value = 10
>>> predicate = GreaterEqual(space=(characteristic,), value=value)
>>> predicate
<galactic.algebras.convex.predicates.core.GreaterEqual object at 0x...>
>>> str(predicate)
'float(@) β‰₯ 10'
>>> print(predicate(Item(value=15.0)))
True
>>> print(predicate(Item(value=10.0)))
True
>>> print(predicate(Item(value=5.0)))
False
>>> print(predicate(Item()))
Unknown
>>> print(predicate(Item(value="")))
Unknown

Notes

The GreaterEqual class is a concrete implementation of the Predicate class that represents a predicate that checks if a value is greater than or equal to a specified value.

Hint

The GreaterEqual class can be used in situations where a predicate is required to check for greater than or equal conditions, such as in filtering data or validating conditions where a value should not fall below a certain threshold.

property template: str#

Get the template.

Returns:

The template for the greater than or equal predicate

Return type:

str

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class LessThan(**data)#

Bases: BasicPredicate

Represents a less than predicate.

Parameters:
  • space – The characteristic to verify

  • value – The value to check for less than

Raises:
  • ValueError – If the space does not contain exactly one Characteristic when value is not a tuple

  • ValueError – If the length of space does not match the length of value when value is a tuple

Examples

>>> from galactic.algebras.concept.core import Item
>>> from galactic.algebras.convex.characteristics.core import Component, Number
>>> from galactic.algebras.convex.predicates.core import LessThan
>>> characteristic = Number(components=(Component(),))
>>> value = 10
>>> predicate = LessThan(space=(characteristic,), value=value)
>>> predicate
<galactic.algebras.convex.predicates.core.LessThan object at 0x...>
>>> str(predicate)
'float(@) < 10'
>>> print(predicate(Item(value=5.0)))
True
>>> print(predicate(Item(value=10.0)))
False
>>> print(predicate(Item(value=15.0)))
False
>>> print(predicate(Item()))
Unknown
>>> print(predicate(Item(value="")))
Unknown

Notes

The LessThan class is a concrete implementation of the Predicate class that represents a predicate that checks if a value is less than a specified value.

Hint

The LessThan class can be used in situations where a predicate is required to check for less than conditions, such as in filtering data or validating conditions where a value should not exceed a certain threshold.

property template: str#

Get the template.

Returns:

The template for the less than predicate

Return type:

str

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class LessEqual(**data)#

Bases: BasicPredicate

Represents a less than or equal predicate.

Parameters:
  • space – The characteristic to verify

  • value – The value to check for less than or equal to

Raises:
  • ValueError – If the space does not contain exactly one Characteristic when value is not a tuple

  • ValueError – If the length of space does not match the length of value when value is a tuple

Examples

>>> from galactic.algebras.concept.core import Item
>>> from galactic.algebras.convex.characteristics.core import Component, Number
>>> from galactic.algebras.convex.predicates.core import LessEqual
>>> characteristic = Number(components=(Component(),))
>>> value = 10
>>> predicate = LessEqual(space=(characteristic,), value=value)
>>> predicate
<galactic.algebras.convex.predicates.core.LessEqual object at 0x...>
>>> str(predicate)
'float(@) ≀ 10'
>>> print(predicate(Item(value=5.0)))
True
>>> print(predicate(Item(value=10.0)))
True
>>> print(predicate(Item(value=15.0)))
False
>>> print(predicate(Item()))
Unknown
>>> print(predicate(Item(value="")))
Unknown

Notes

The LessEqual class is a concrete implementation of the Predicate class that represents a predicate that checks if a value is less than or equal to a specified value.

Hint

The LessEqual class can be used in situations where a predicate is required to check for less than or equal conditions, such as in filtering data or validating conditions where a value should not exceed a certain threshold.

property template: str#

Get the template.

Returns:

The template for the less than or equal predicate

Return type:

str

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class Membership(**data)#

Bases: BasicPredicate

Represents a membership predicate.

Parameters:
  • space – The characteristic to verify

  • member – The member to check for membership

Raises:

ValueError – If the space does not contain exactly one Characteristic

Examples

>>> from galactic.algebras.concept.core import Item
>>> from galactic.algebras.convex.characteristics.core import Component, String
>>> from galactic.algebras.convex.predicates.core import Membership
>>> characteristic = String(components=(Component(),))
>>> member = "example"
>>> predicate = Membership(space=(characteristic,), member=member)
>>> predicate
<galactic.algebras.convex.predicates.core.Membership object at 0x...>
>>> str(predicate)
"'example' ∈ str(@)"
>>> print(predicate(Item(value="This is an example string.")))
True
>>> print(predicate(Item(value="This is a test string.")))
False
property template: str#

Get the template.

Returns:

The template for the membership predicate

Return type:

str

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].