Core
fieldz_kb.clingo.core
Core clingo integration for fieldz_kb.
This module provides: - ClingoTypePlugin: abstract base class for clingo type conversion plugins - ClingoContext: plugin registry and cache - Module-level dispatch functions for predicate class generation and fact conversion - make_context(): factory for creating fresh contexts with built-in plugins
Classes:
| Name | Description |
|---|---|
ClingoContext |
Plugin registry and cache for clingo type conversion. |
ClingoTypePlugin |
Abstract base class for clingo type conversion plugins. |
Functions:
| Name | Description |
|---|---|
get_default_context |
Return the shared default context, creating it on first access. |
get_or_make_predicate_classes_from_field |
Get or create predicate classes for a dataclass field. |
get_or_make_predicate_classes_from_type |
Get or create predicate classes for a given Python type. |
make_context |
Create a fresh ClingoContext with all built-in plugins registered. |
make_fact_id |
Get or create a fact ID for an object. |
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. |
ClingoContext
Plugin registry and cache for clingo type conversion.
Stores registered plugin classes and caches for type-to-predicate-class mappings.
Initialize an empty context with no plugins registered.
Methods:
| Name | Description |
|---|---|
get_plugin_for_type |
Look up the plugin class for a Python type. |
register |
Register a type plugin class with this context. |
reset |
Reset all internal caches and ID counter. |
Source code in src/fieldz_kb/clingo/core.py
get_plugin_for_type
get_plugin_for_type(type_: type) -> type[ClingoTypePlugin]
Look up the plugin class for a Python type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_
|
type
|
The Python type to look up. |
required |
Returns:
| Type | Description |
|---|---|
type[ClingoTypePlugin]
|
The matching ClingoTypePlugin subclass. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no plugin can handle the type. |
Source code in src/fieldz_kb/clingo/core.py
register
register(plugin: type[ClingoTypePlugin]) -> None
Register a type plugin class with this context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plugin
|
type[ClingoTypePlugin]
|
A ClingoTypePlugin subclass (not an instance). |
required |
reset
Reset all internal caches and ID counter.
Primarily intended for use in tests to ensure isolation between test cases.
Source code in src/fieldz_kb/clingo/core.py
ClingoTypePlugin
Bases: ABC
Abstract base class for clingo type conversion plugins.
Plugins are used as classes, not instances — all methods are classmethods. Each plugin handles one or more Python types, providing: - Predicate class generation - Object-to-fact conversion
Methods:
| Name | Description |
|---|---|
can_handle_type |
Return True if this plugin can handle the given Python type. |
make_facts |
Convert a Python object to clingo facts. |
make_predicate_classes |
Create clorm predicate classes for the given Python type. |
can_handle_type
abstractmethod
classmethod
make_facts
abstractmethod
classmethod
make_facts(obj: object, ctx: ClingoContext, 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 |
ctx
|
ClingoContext
|
The plugin registry and cache. |
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/fieldz_kb/clingo/core.py
make_predicate_classes
abstractmethod
classmethod
make_predicate_classes(type_: type, ctx: ClingoContext, module: str | None = None, make_predicate_classes_recursively: bool = True, guard: set | None = None) -> list
Create clorm predicate classes for the given Python type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_
|
type
|
The Python type. |
required |
ctx
|
ClingoContext
|
The plugin registry and cache. |
required |
module
|
str | None
|
Module name for resolving forward references. |
None
|
make_predicate_classes_recursively
|
bool
|
Whether to create predicates for nested types. |
True
|
guard
|
set | None
|
Set of types currently being processed (prevents infinite recursion). |
None
|
Returns:
| Type | Description |
|---|---|
list
|
A list of predicate classes. |
Source code in src/fieldz_kb/clingo/core.py
get_default_context
get_default_context() -> ClingoContext
Return the shared default context, creating it on first access.
get_or_make_predicate_classes_from_field
get_or_make_predicate_classes_from_field(ctx: ClingoContext, fieldz_class: type, field: object, type_: type | None = None, module: str | None = None, make_predicate_classes_recursively: bool = True, guard: set | None = None) -> list
Get or create predicate classes for a dataclass field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
ClingoContext
|
The plugin registry and cache. |
required |
fieldz_class
|
type
|
The owning dataclass type. |
required |
field
|
object
|
The fieldz field descriptor. |
required |
type_
|
type | None
|
Optional specific type for cache lookup. |
None
|
module
|
str | None
|
Module name for resolving forward references. |
None
|
make_predicate_classes_recursively
|
bool
|
Whether to create predicates for nested types. |
True
|
guard
|
set | None
|
Guard set for recursion prevention. |
None
|
Returns:
| Type | Description |
|---|---|
list
|
A list of predicate classes. |
Source code in src/fieldz_kb/clingo/core.py
get_or_make_predicate_classes_from_type
get_or_make_predicate_classes_from_type(ctx: ClingoContext, type_: type, module: str | None = None, make_predicate_classes_recursively: bool = True, guard: set | None = None) -> list
Get or create predicate classes for a given Python type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
ClingoContext
|
The plugin registry and cache. |
required |
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
|
guard
|
set | None
|
Set of types currently being processed (prevents infinite recursion). |
None
|
Returns:
| Type | Description |
|---|---|
list
|
A list of predicate classes. |
Source code in src/fieldz_kb/clingo/core.py
make_context
make_context() -> ClingoContext
Create a fresh ClingoContext with all built-in plugins registered.
Source code in src/fieldz_kb/clingo/core.py
make_fact_id
make_fact_id(ctx: ClingoContext, obj: object, integration_mode: Literal['hash', 'id'] = 'id', exclude_from_integration: tuple[type, ...] | None = None) -> str
Get or create a fact ID for an object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
ClingoContext
|
The plugin registry and cache. |
required |
obj
|
object
|
The object to get/create an ID for. |
required |
integration_mode
|
Literal['hash', 'id']
|
How to key the dedup cache ("hash" or "id"). |
'id'
|
exclude_from_integration
|
tuple[type, ...] | None
|
Types to exclude from deduplication. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A string ID like "id_0", "id_1", etc. |
Source code in src/fieldz_kb/clingo/core.py
make_facts_from_object
make_facts_from_object(ctx: ClingoContext, 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 |
|---|---|---|---|
ctx
|
ClingoContext
|
The plugin registry and cache. |
required |
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/fieldz_kb/clingo/core.py
make_ontology_rules_from_type
make_ontology_rules_from_type(ctx: ClingoContext, type_: type) -> list[str]
Generate ontology rules expressing type inheritance as ASP rules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
ClingoContext
|
The plugin registry and cache. |
required |
type_
|
type
|
The Python type to generate rules for. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
A sorted list of ASP rule strings. |