neurolang.datalog#

class neurolang.datalog.Conjunction(formulas)#
classmethod apply(*args)#

Builds a new expression using a tuple of its parameters

property type#

Get the expression’s type.

For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.

This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.

unapply()#

Returns a tuple of parameters used to build the expression.

class neurolang.datalog.DatalogProgram(symbol_table=None)#
constant_contains = C{contains: typing.Callable[[typing.Any, typing.Any], bool]}#
constant_equals = C{eq: typing.Callable[[typing.Any, typing.Any], bool]}#
static infer_iterable_type(iterable)#

Infer the type of iterable elements without modifying the iterable.

Parameters:
iterableIterable

iterable from which to infer element’s type

Returns:
type, iterable

the inferred type and the iterable.

match(expression)#

Find the action for a given expression by going through the patterns.

Goes through the triplets in in patterns and calls the action specified by the first satisfied triplet.

pattern_match(pattern, expression)#

Return True if pattern matches expression.

Patterns are of the following form:

  • ... matches everything.

  • Expression class matches if expression is instance of the given class for any type.

  • Expression [T] matches if expression is instance of the given class with expression.type a subtype of T.

  • Expression [T](Pattern_0, Pattern_1, ....., Pattern_N) matches if expression is instance of the given class with expression.type a subtype of T and recursively matches the construction parameters of the instance on the types given as Pattern_0, Pattern_1, etc.

  • (Pattern_0, ....., Pattern_N) matches when expression is a tuple of length N + 1 where expression[0] matches Pattern_0, expression[1] matches Pattern_1, etc.

  • instance an instance of a python class not subclassing Expression matches when instance == expression

property patterns#

Property holding an iterator of triplets (pattern, guard, action).

  • pattern: is an Expression class, or instance where construction parameters and the type can be replaced by an ellipsis … to signal a wildcard. See pattern_match() for more details.

  • guard: is a function mapping an Expression instance to a boolean or None.

  • action: is the method receiving the matching expression instance to be executed upon pattern and match being True.

type#

alias of Any

union_of_cq(expression)#

This pattern is here to avoid processing the full program when one rule is rewritten.

class neurolang.datalog.Fact(consequent)#
classmethod apply(*args)#

Builds a new expression using a tuple of its parameters

property type#

Get the expression’s type.

For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.

This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.

unapply()#

Returns a tuple of parameters used to build the expression.

class neurolang.datalog.Implication(consequent, antecedent)#

Expression of the form P(x) ← Q(x)

classmethod apply(*args)#

Builds a new expression using a tuple of its parameters

property type#

Get the expression’s type.

For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.

This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.

unapply()#

Returns a tuple of parameters used to build the expression.

class neurolang.datalog.Negation(formula)#
classmethod apply(*args)#

Builds a new expression using a tuple of its parameters

property type#

Get the expression’s type.

For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.

This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.

unapply()#

Returns a tuple of parameters used to build the expression.

class neurolang.datalog.Union(formulas)#
classmethod apply(*args)#

Builds a new expression using a tuple of its parameters

property type#

Get the expression’s type.

For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.

This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.

unapply()#

Returns a tuple of parameters used to build the expression.

class neurolang.datalog.WrappedRelationalAlgebraSet(iterable=None, row_type=<class 'nl_expr.type_system.Unknown'>, verify_row_type=True, **kwargs)#
add(value)#

Add an element.

clear()#

This is slow (creates N new iterators!) but effective.

discard(value)#

Remove an element. Do not raise an exception if absent.

isdisjoint(other)#

Return True if two sets have a null intersection.

property might_have_duplicates#

Sets the flag manually on whether the set might have duplicates. Careful, if you set it to False and it’s not, then the semantics of the queries will break.

Returns:
bool

returns whether the set might have duplicates.

pop()#

Return the popped value. Raise KeyError if empty.

projection(*columns)#

Projects the set on the given list of columns.

Examples

>>> ras = RelationalAlgebraFrozenSet(
        [(i % 2, i, i * 2) for i in range(5)])
>>> ras
   0  1  2
0  0  0  0
1  1  1  2
2  0  2  4
3  1  3  6
4  0  4  8
>>> ras.projection(1)
   0
0  0
1  1
2  2
3  3
4  4
remove(value)#

Remove an element. If not a member, raise a KeyError.

row_count_estimate()#

Return the number of rows of the container.

Duplicates are not dropped, so this is an upper bound of len(self) obtained without any computation.

Returns:
int

Number of rows of the underlying pandas.DataFrame.

selection(select_criteria)#

Select the elements based on the given selet_criteria. The select_criteria may be a callable function, a string expression or a Dict of columns -> value

Parameters:
select_criteriaUnion[Callable, RelationalAlgebraStringExpression,
Dict[int, Union[int, Callable]]]

selection criteria

Returns:
RelationalAlgebraFrozenSet

A RelationalAlgebraFrozenSet with elements matching the criteria.

Parameters:

select_criteria (Callable | RelationalAlgebraStringExpression | Dict[int, int | Callable])

selection_columns(select_criteria)#

Select the elements where column pairs indicated as key, value items of the select_criteria are equal. The select_criteria must be a Dict of Int -> Int.

Parameters:
select_criteriaDict [Int, Int]

selection criteria

Returns:
RelationalAlgebraFrozenSet

A RelationalAlgebraFrozenSet with elements matching the criteria.

Parameters:

select_criteria (Dict[int, int])

Examples

Select the elements where col0 == col1 and col1 == col2

>>> ras = RelationalAlgebraFrozenSet(
...     [(i % 2, i, i * 2) for i in range(5)])
>>> ras.selection_columns({0:1, 1: 2})
   0  1  2
0  0  0  0
neurolang.datalog.extract_logic_free_variables(expression)#

Extract variables from expression assuming it’s in datalog format.

Parameters:
expressionExpression
Returns:
OrderedSet

set of all free variables in the expression.

neurolang.datalog.extract_logic_predicates(expression)#

Extract predicates from expression knowing that it’s in Datalog format

Parameters:
expressionExpression

expression to extract predicates from

Returns:
OrderedSet

set of all predicates in the expression in lexicographical order.