Skip to content

Utils

fieldz_kb.clingo.utils

Shared utility functions for the clingo backend.

Classes:

Name Description
FloatField

Custom clorm field for float values, stored as strings.

Functions:

Name Description
make_predicate_class

Dynamically create a clorm Predicate subclass.

make_predicate_class_name_from_type

Return a predicate class name for a given Python type.

make_predicate_name_from_field

Generate a predicate name from a field name.

FloatField

Bases: StringField

Custom clorm field for float values, stored as strings.

Methods:

Name Description
cltopy

Convert a clingo string to a Python float.

pytocl

Convert a Python float to a clingo string.

cltopy

cltopy(value: str) -> float

Convert a clingo string to a Python float.

Source code in src/fieldz_kb/clingo/utils.py
def cltopy(value: str) -> float:
    """Convert a clingo string to a Python float."""
    return float(value)

pytocl

pytocl(value: float) -> str

Convert a Python float to a clingo string.

Source code in src/fieldz_kb/clingo/utils.py
def pytocl(value: float) -> str:
    """Convert a Python float to a clingo string."""
    return str(value)

make_predicate_class

make_predicate_class(predicate_class_name: str, predicate_name: str, fields: dict) -> type

Dynamically create a clorm Predicate subclass.

Parameters:

Name Type Description Default
predicate_class_name str

The Python class name.

required
predicate_name str

The ASP predicate name.

required
fields dict

A dict mapping field names to their types.

required

Returns:

Type Description
type

A new Predicate subclass.

Source code in src/fieldz_kb/clingo/utils.py
def make_predicate_class(
    predicate_class_name: str, predicate_name: str, fields: dict
) -> type:
    """Dynamically create a clorm Predicate subclass.

    Args:
        predicate_class_name: The Python class name.
        predicate_name: The ASP predicate name.
        fields: A dict mapping field names to their types.

    Returns:
        A new Predicate subclass.
    """
    annotations = {}
    attributes = {}
    for name, type_ in fields.items():
        if isinstance(type_, type) and issubclass(type_, clorm.StringField):
            attributes[name] = clorm.field(type_)
            annotations[name] = str
        else:
            annotations[name] = type_
    attributes["__annotations__"] = annotations
    return type(clorm.Predicate)(
        predicate_class_name, (clorm.Predicate,), attributes, name=predicate_name
    )

make_predicate_class_name_from_type

make_predicate_class_name_from_type(type_: type) -> str

Return a predicate class name for a given Python type.

The first character is lowercased to follow ASP naming conventions.

Parameters:

Name Type Description Default
type_ type

The Python type.

required

Returns:

Type Description
str

The predicate class name string.

Source code in src/fieldz_kb/clingo/utils.py
def make_predicate_class_name_from_type(type_: type) -> str:
    """Return a predicate class name for a given Python type.

    The first character is lowercased to follow ASP naming conventions.

    Args:
        type_: The Python type.

    Returns:
        The predicate class name string.
    """
    predicate_class_name = type_.__name__
    predicate_class_name = predicate_class_name[0].lower() + predicate_class_name[1:]
    return predicate_class_name

make_predicate_name_from_field

make_predicate_name_from_field(field_name: str, many: bool) -> str

Generate a predicate name from a field name.

For plural field names (many=True), singularizes the name segments.

Parameters:

Name Type Description Default
field_name str

The field name to convert.

required
many bool

Whether the field represents a to-many relationship.

required

Returns:

Type Description
str

A predicate name string like 'hasFieldName'.

Source code in src/fieldz_kb/clingo/utils.py
def make_predicate_name_from_field(field_name: str, many: bool) -> str:
    """Generate a predicate name from a field name.

    For plural field names (many=True), singularizes the name segments.

    Args:
        field_name: The field name to convert.
        many: Whether the field represents a to-many relationship.

    Returns:
        A predicate name string like 'hasFieldName'.
    """
    words = field_name.split("_")
    if not words[-1]:
        del words[-1]
        words[-1] = f"{words[-1]}_"
    if many:
        inflect_engine = inflect.engine()
        singulars = []
        for i, word in enumerate(words):
            singular = inflect_engine.singular_noun(word)
            if singular and singular != word:
                singulars.append(singular)
                break
            else:
                singulars.append(word)
        singulars += words[i + 1 :]
        words = singulars
    words = [word[0].upper() + word[1:] for word in words]
    predicate_class_name = f"has{''.join(words)}"
    return predicate_class_name