Skip to content

Session

momapy_kb.clingo.core

Clingo session for momapy_kb.

Provides a Session class for converting momapy objects to clingo predicates and facts for answer set programming.

Classes:

Name Description
Session

Session for converting momapy objects to clingo predicates and facts.

Session

Session()

Session for converting momapy objects to clingo predicates and facts.

Example

import momapy_kb.clingo.core with momapy_kb.clingo.core.Session() as session: ... facts = session.make_facts_from_object(my_model)

Methods:

Name Description
get_or_make_predicate_classes_from_type

Get or create predicate classes for a given Python type.

make_facts_from_object

Convert a Python object to clingo facts.

make_ontology_rules_from_type

Generate ontology rules expressing type inheritance as ASP rules.

Source code in src/momapy_kb/clingo/core.py
def __init__(self) -> None:
    self._session = fieldz_kb.clingo.session.Session()

get_or_make_predicate_classes_from_type

get_or_make_predicate_classes_from_type(type_: type, module: str | None = None, make_predicate_classes_recursively: bool = True) -> list

Get or create predicate classes for a given Python type.

Parameters:

Name Type Description Default
type_ type

The Python type.

required
module str | None

Module name for resolving forward references.

None
make_predicate_classes_recursively bool

Whether to create predicates for nested types.

True

Returns:

Type Description
list

A list of predicate classes.

Source code in src/momapy_kb/clingo/core.py
def get_or_make_predicate_classes_from_type(
    self,
    type_: type,
    module: str | None = None,
    make_predicate_classes_recursively: bool = True,
) -> list:
    """Get or create predicate classes for a given Python type.

    Args:
        type_: The Python type.
        module: Module name for resolving forward references.
        make_predicate_classes_recursively: Whether to create predicates for nested types.

    Returns:
        A list of predicate classes.
    """
    return self._session.get_or_make_predicate_classes_from_type(
        type_,
        module=module,
        make_predicate_classes_recursively=make_predicate_classes_recursively,
    )

make_facts_from_object

make_facts_from_object(obj: object, id_to_object: dict | None = None, integration_mode: Literal['hash', 'id'] = 'id', exclude_from_integration: tuple[type, ...] | None = None) -> list

Convert a Python object to clingo facts.

Parameters:

Name Type Description Default
obj object

The object to convert.

required
id_to_object dict | None

Optional cache mapping fact IDs to objects.

None
integration_mode Literal['hash', 'id']

How to handle duplicate objects ("hash" or "id").

'id'
exclude_from_integration tuple[type, ...] | None

Types to exclude from integration logic.

None

Returns:

Type Description
list

A list of clorm facts.

Source code in src/momapy_kb/clingo/core.py
def make_facts_from_object(
    self,
    obj: object,
    id_to_object: dict | None = None,
    integration_mode: typing.Literal["hash", "id"] = "id",
    exclude_from_integration: tuple[type, ...] | None = None,
) -> list:
    """Convert a Python object to clingo facts.

    Args:
        obj: The object to convert.
        id_to_object: Optional cache mapping fact IDs to objects.
        integration_mode: How to handle duplicate objects ("hash" or "id").
        exclude_from_integration: Types to exclude from integration logic.

    Returns:
        A list of clorm facts.
    """
    return self._session.make_facts_from_object(
        obj,
        id_to_object=id_to_object,
        integration_mode=integration_mode,
        exclude_from_integration=exclude_from_integration,
    )

make_ontology_rules_from_type

make_ontology_rules_from_type(type_: type) -> list[str]

Generate ontology rules expressing type inheritance as ASP rules.

Parameters:

Name Type Description Default
type_ type

The Python type to generate rules for.

required

Returns:

Type Description
list[str]

A sorted list of ASP rule strings.

Source code in src/momapy_kb/clingo/core.py
def make_ontology_rules_from_type(self, type_: type) -> list[str]:
    """Generate ontology rules expressing type inheritance as ASP rules.

    Args:
        type_: The Python type to generate rules for.

    Returns:
        A sorted list of ASP rule strings.
    """
    return self._session.make_ontology_rules_from_type(type_)