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 predicatesAbsurd: 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 characteristicEquality: Represents equality between two elementsDifferent: Represents inequality between two elementsGreaterThan: Represents the βgreater thanβ relationGreaterEqual: Represents the βgreater than or equal toβ relationLessThan: Represents the βless thanβ relationLessEqual: Represents the βless than or equal toβ relationMembership: Represents membership in a set
- class Predicate(**data)#
Bases:
BaseModel,ABCRepresents a predicate.
- Parameters:
space β The characteristics to be verified
- Raises:
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:
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.
- class BasicPredicate(**data)#
-
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
- class BooleanPredicate(**data)#
Bases:
BasicPredicateRepresents an boolean predicate.
- Parameters:
space β The characteristic to verify
- property template: str#
Get the template.
- Returns:
The template
- Return type:
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.
- class Absurd(**data)#
Bases:
PredicateRepresents 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
- class Degenerated(**data)#
-
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
- class Tautology(**data)#
Bases:
PredicateRepresents 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
- class Equality(**data)#
Bases:
BasicPredicateRepresents 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
- class Different(**data)#
Bases:
BasicPredicateRepresents 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.
- class GreaterThan(**data)#
Bases:
BasicPredicateRepresents 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.
- class GreaterEqual(**data)#
Bases:
BasicPredicateRepresents 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.
- class LessThan(**data)#
Bases:
BasicPredicateRepresents 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.
- class LessEqual(**data)#
Bases:
BasicPredicateRepresents 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.
- class Membership(**data)#
Bases:
BasicPredicateRepresents 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