from typing import Any
import jpype
[docs]
class Function:
"""
Base class for all logic functions (transformation, combination, aggregation).
Functions are used to transform, combine, or aggregate values in the logic program.
"""
__slots__ = "name", "operator", "can_flatten", "namespace"
def __init__(self, name: str, *, namespace: str = "", operator: str | None = None, can_flatten: bool = False):
"""
Parameters
----------
name : str
The name of the function.
namespace : str
The Java namespace of the function. Default: "".
operator : str, optional
The operator associated with the function (e.g., '+', '@'). Default: None.
can_flatten : bool
Whether the function can be flattened in the logic program. Default: False.
"""
self.name: str = name.lower()
self.operator: str | None = operator
self.can_flatten = can_flatten
self.namespace = namespace
def __str__(self) -> str:
return self.name
[docs]
def wrap(self, content: str) -> str:
return f"{self.name}({content})"
[docs]
def pretty_str(self) -> str:
return str(self).capitalize()
def __call__(self, *args: Any, **kwargs: Any) -> Any:
if len(args) == 0:
return self
raise NotImplementedError
[docs]
def is_parametrized(self) -> bool:
return False
[docs]
def rule_head_dependant(self) -> bool:
return False
[docs]
def process_head(self, head: Any) -> "Function":
raise NotImplementedError
[docs]
def get(self) -> Any:
"""
Returns the Java representation of the function.
Returns
-------
Any
The Java function object.
"""
name = "".join(s.capitalize() for s in self.name.split("_"))
formatted_namespace = self.namespace.format(name=name)
return jpype.JClass(f"cz.cvut.fel.ida.algebra.functions.{formatted_namespace}")()
[docs]
class CombinationFunction(Function):
"""
Represents a combination function used to combine multiple relations into a single output.
"""
def __init__(
self,
name: str,
*,
namespace: str = "combination.{name}",
operator: str | None = None,
can_flatten: bool = False,
):
super().__init__(name, namespace=namespace, operator=operator, can_flatten=can_flatten)
def __call__(self, *relations: Any) -> Any:
from neuralogic.core.constructs.function.function_container import FContainer
if len(relations) == 0:
return self
return FContainer(relations, self)
[docs]
class AggregationFunction(Function):
"""
Represents an aggregation function used to aggregate multiple groundings of the same rule.
"""
[docs]
def get(self) -> Any:
raise NotImplementedError