neuralogic.core.constructs package

Subpackages

Submodules

neuralogic.core.constructs.factories module

class AtomFactory[source]

Bases: object

Factory for creating atoms (relations) in the logic program. It supports dot notation for predicate names and provides access to special and hidden factories.

get(name: str) BaseRelation[source]

Creates a relation with the given name and 0 arity.

Parameters:

name (str) – The name of the predicate.

Returns:

The created relation.

Return type:

BaseRelation

static get_predicate(name: str, arity: int, hidden: bool, special: bool) Predicate[source]
class ConstantFactory[source]

Bases: object

Factory for creating constants. Constants are automatically converted to lowercase unless specified otherwise.

get(item: str, const_type: str | None = None) Constant[source]

Creates a constant with the given name and optional type.

Parameters:
  • item (str) – The name of the constant.

  • const_type (str, optional) – The type of the constant. Default: None.

Returns:

The created constant.

Return type:

Constant

class HiddenPredicateFactory[source]

Bases: object

Factory for creating hidden predicates. Hidden predicates are not part of the output unless explicitly requested.

get(name: str) BaseRelation[source]
property special: SpecialPredicateFactory
class SpecialPredicateFactory(hidden: bool = False)[source]

Bases: object

Factory for creating special predicates, such as β€˜alldiff’, β€˜neq’, β€˜eq’, etc. Special predicates are handled differently by the backend engine.

add(a: Any, b: Any, c: Any) BaseRelation[source]
add_eval(a: Any, b: Any) BaseRelation[source]
alldiff(*args: Any) BaseRelation[source]
anypred() BaseRelation[source]
div_eval(a: Any, b: Any) BaseRelation[source]
eq(a: Any, b: Any) BaseRelation[source]
geq(a: Any, b: Any) BaseRelation[source]
get(name: str) BaseRelation[source]

Creates a special relation with the given name and 0 arity.

Parameters:

name (str) – The name of the special predicate.

Returns:

The created special relation.

Return type:

BaseRelation

gt(a: Any, b: Any) BaseRelation[source]
property hidden: SpecialPredicateFactory
leq(a: Any, b: Any) BaseRelation[source]
lt(a: Any, b: Any) BaseRelation[source]
max_eval(a: Any, b: Any) BaseRelation[source]
maxcard(*args: Any) BaseRelation[source]
min_eval(a: Any, b: Any) BaseRelation[source]
mod(a: Any, b: Any, c: Any) BaseRelation[source]
mod_eval(a: Any, b: Any) BaseRelation[source]
mul_eval(a: Any, b: Any) BaseRelation[source]
neq(a: Any, b: Any) BaseRelation[source]
next(a: Any, b: Any) BaseRelation[source]
sub(a: Any, b: Any, c: Any) BaseRelation[source]
sub_eval(a: Any, b: Any) BaseRelation[source]
truepred() BaseRelation[source]
class VariableFactory[source]

Bases: object

Factory for creating variables. Variables are automatically capitalized unless specified otherwise.

get(item: str, var_type: str | None = None) Variable[source]

Creates a variable with the given name and optional type.

Parameters:
  • item (str) – The name of the variable.

  • var_type (str, optional) – The type of the variable. Default: None.

Returns:

The created variable.

Return type:

Variable

neuralogic.core.constructs.java_objects module

class JavaFactory(settings: SettingsProxy | None = None)[source]

Bases: object

Factory for converting high-level Python logic constructs (Atoms, Rules, etc.) into their corresponding Java objects. It maintains internal factories for predicates, constants, and weights.

add_metadata_function(metadata: Metadata, map: Any, function: str) None[source]
add_parametrized_function(metadata: Metadata, metadata_obj: Any, function: str) None[source]
atom_to_clause(atom: Any) Any[source]

Converts a single atom to a Java Clause.

Parameters:

atom (Any) – The atom to convert.

Returns:

The Java Clause object.

Return type:

Any

get_conjunction(relations: Iterable[Any], variable_factory: Any, default_weight: Any = None, is_example: bool = False) Any[source]
get_generic_relation(relation_class: Any, relation: Any, variable_factory: Any, default_weight: Any = None, is_example: bool = False) Any[source]

Generic method to convert a Python relation to a Java relation object.

Parameters:
  • relation_class (Any) – The Java class of the relation (e.g., BodyAtom, ValuedFact).

  • relation (Any) – The Python relation object.

  • variable_factory (Any) – The Java variable factory.

  • default_weight (Any, optional) – A default weight if the relation is unweighted. Default: None.

  • is_example (bool) – Whether the relation is part of an example. Default: False.

Returns:

The Java relation object.

Return type:

Any

get_lifted_example(example: Any, learnable_facts: bool = False) tuple[Any, Any][source]

Converts a Python example to a Java LiftedExample.

Parameters:
  • example (Any) – The example to convert.

  • learnable_facts (bool) – Whether facts in the example should be learnable. Default: False.

Returns:

A tuple containing the Java label conjunction and the Java LiftedExample.

Return type:

tuple[Any, Any]

get_metadata(metadata: Metadata | None, metadata_class: Any) Any[source]
get_new_weight_factory() Any[source]
get_predicate(predicate: Any) Any[source]
get_predicate_metadata_pair(predicate_metadata: Any) Any[source]
get_query(query: Any) tuple[Any, list[Any] | None][source]

Converts a Python query to a Java representation.

Parameters:

query (Any) – The query to convert. Can be a Rule, list of relations, or a single relation.

Returns:

A tuple containing the Java head relation and a list of Java body relations.

Return type:

tuple[Any, list[Any] | None]

get_relation(relation: Any, variable_factory: Any, is_example: bool = False) Any[source]
get_rule(rule: Any) Any[source]

Converts a Python Rule to a Java WeightedRule.

Parameters:

rule (Any) – The Python rule to convert.

Returns:

The Java WeightedRule object.

Return type:

Any

get_term(term: Any, variable_factory: Any) Any[source]

Converts a Python term (Variable, Constant, str, int, float) to a Java term.

Parameters:
  • term (Any) – The term to convert.

  • variable_factory (Any) – The Java variable factory to use for variables.

Returns:

The Java term object.

Return type:

Any

get_valued_fact(relation: Any, variable_factory: Any, default_weight: Any = None, is_example: bool = False) Any[source]
get_variable_factory() Any[source]
get_weight(weight: Any, name: str | None, fixed: bool) Any[source]
to_clause(atoms: Iterable[Any]) Any[source]
class ValueFactory[source]

Bases: object

Factory for converting between Python values (scalars, lists, numpy arrays) and Java Value objects. It handles ScalarValue, VectorValue, and MatrixValue.

static from_java(value: Any) float | list | ndarray[source]

Converts a Java Value object to a Python representation (float, list, or numpy array).

Parameters:

value (Any) – The Java Value object to convert.

Returns:

The Python representation of the value.

Return type:

float | list | np.ndarray

get_value(weight: Any) tuple[bool, Any][source]

Converts a Python weight to a Java Value object.

Parameters:

weight (Any) – The weight to convert. Can be a scalar, tuple (for dimensions), list, or numpy array.

Returns:

A tuple containing a boolean (True if initialized, False if just dimensions) and the Java Value object.

Return type:

tuple[bool, Any]

neuralogic.core.constructs.metadata module

class Metadata(learnable: bool | None = None, transformation: TransformationFunction | CombinationFunction | None = None, combination: CombinationFunction | None = None, aggregation: AggregationFunction | None = None, duplicate_grounding: bool | None = None)[source]

Bases: object

Represents metadata for a logic construct (e.g., rule, predicate).

Metadata can specify properties like learnability, transformation functions, aggregation functions, and combination functions.

aggregation
combination
combine(other: Metadata) Metadata[source]

Combines this metadata with another Metadata object. Values from the other object take precedence.

Parameters:

other (Metadata) – The other Metadata object to combine with.

Returns:

A new combined Metadata object.

Return type:

Metadata

copy() Metadata[source]

Returns a shallow copy of the metadata.

Returns:

The copy of the metadata.

Return type:

Metadata

duplicate_grounding
static from_iterable(iterable: Iterable[Any]) Metadata[source]

Creates a Metadata object from an iterable of functions or values.

Parameters:

iterable (Iterable) – The iterable containing metadata entries.

Returns:

The created Metadata object.

Return type:

Metadata

learnable
transformation

neuralogic.core.constructs.predicate module

class Predicate(name: str, arity: int, hidden: bool = False, special: bool = False)[source]

Bases: object

Represents a predicate in the logic program, defined by its name and arity.

arity
hidden
name
set_arity(arity: int) Predicate | None[source]
special
to_str() str[source]

Returns a string representation of the predicate (without arity).

Returns:

The string representation.

Return type:

str

class PredicateMetadata(predicate: Predicate, metadata: Metadata)[source]

Bases: object

Associates metadata with a predicate.

metadata
predicate

neuralogic.core.constructs.relation module

class BaseRelation(predicate: Predicate, terms: Any = None, function: TransformationFunction | CombinationFunction | None = None, negated: bool = False)[source]

Bases: object

Represents a relation with a predicate, terms, and an optional activation function.

property T: BaseRelation
attach_activation_function(function: TransformationFunction | CombinationFunction) BaseRelation[source]

Attaches an activation or combination function to the relation.

Parameters:

function (Union[TransformationFunction, CombinationFunction]) – The function to attach.

Returns:

A new relation with the attached function.

Return type:

BaseRelation

function
negated
predicate
terms
to_str(end=False) str[source]

Returns a string representation of the relation.

Parameters:

end (bool) – Whether to append a dot at the end. Default: False.

Returns:

The string representation.

Return type:

str

class WeightedRelation(weight: Any, predicate: Predicate, fixed: bool = False, terms: Any = None, function: TransformationFunction | CombinationFunction | None = None)[source]

Bases: BaseRelation

Represents a relation with an associated weight (learnable or fixed).

property T: WeightedRelation
attach_activation_function(function: Transformation | Combination) WeightedRelation[source]

Attaches an activation or combination function to the relation.

Parameters:

function (Union[TransformationFunction, CombinationFunction]) – The function to attach.

Returns:

A new relation with the attached function.

Return type:

BaseRelation

fixed() WeightedRelation[source]

Returns a copy of the relation with the weight fixed.

Returns:

The weighted relation with a fixed weight.

Return type:

WeightedRelation

is_fixed
to_str(end=False)[source]

Returns a string representation of the relation.

Parameters:

end (bool) – Whether to append a dot at the end. Default: False.

Returns:

The string representation.

Return type:

str

weight
weight_name

neuralogic.core.constructs.rule module

class Rule(head: BaseRelation, body: RuleBody | Iterable[BodyItem] | BodyItem)[source]

Bases: object

Represents a rule in the model, consisting of a head and a body.

body: list[BodyItem] | FContainer
head
metadata: Metadata | None
to_str(_: bool = False) str[source]
class RuleBody(lit1: BaseRelation, lit2: BaseRelation)[source]

Bases: object

Represents the body of a rule, which is a collection of literals.

literals: list['BaseRelation' | FContainer]
metadata: Metadata | None

neuralogic.core.constructs.term module

class Constant(name: str, type: str | None = None)[source]

Bases: object

Represents a constant in a logic expression.

name
type
class Variable(name: str, type: str | None = None)[source]

Bases: object

Represents a variable in a logic expression.

name
type

Module contents