diff --git a/.pylintrc b/.pylintrc index e31a63a..79248ec 100644 --- a/.pylintrc +++ b/.pylintrc @@ -28,6 +28,7 @@ argument-rgx=^[a-z][a-z0-9]*((_[a-z0-9]+)*_?)?$ variable-rgx=^[a-z][a-z0-9]*((_[a-z0-9]+)*_?)?$ class-naming-style=any class-const-naming-style=PascalCase +disable: E1101 # Good variable names which should always be accepted, separated by a comma. good-names=_,M,N,B,A,Nn,Bn,An diff --git a/pyproject.toml b/pyproject.toml index af3af5f..be946fc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,3 +8,6 @@ build-backend = "setuptools.build_meta" [tool.black] skip_magic_trailing_comma = true line-length = 88 + +[tool.mypy] +disable_error_code = ["attr-defined"] \ No newline at end of file diff --git a/src/renopro/asp/ast.lp b/src/renopro/asp/ast.lp index 266516a..8e5d2a1 100644 --- a/src/renopro/asp/ast.lp +++ b/src/renopro/asp/ast.lp @@ -7,6 +7,7 @@ ast(binary_operation(Id,Operator,Left,Right)) :- binary_operation(Id,Operator,Le ast(interval(Id,Left,Right)) :- interval(Id,Left,Right). ast(terms(Id,Pos,Element)) :- terms(Id,Pos,Element). ast(function(Id,Name,Args)) :- function(Id,Name,Args). +ast(external_function(Id,Name,Args)) :- external_function(Id,Name,Args). ast(pool(Id,Args)) :- pool(Id,Args). ast(theory_terms(Id,Pos,Element)) :- theory_terms(Id,Pos,Element). ast(theory_sequence(Id,SeqType,Terms)) :- theory_sequence(Id,SeqType,Terms). @@ -22,22 +23,36 @@ ast(symbolic_atom(Id,Symbol)) :- symbolic_atom(Id,Symbol). ast(literal(Id,Sign,Atom)) :- literal(Id,Sign,Atom). ast(literals(Id,Pos,Element)) :- literals(Id,Pos,Element). ast(conditional_literal(Id,Literal,Condition)) :- conditional_literal(Id,Literal,Condition). -ast(agg_elements(Id,Pos,Element)) :- agg_elements(Id,Pos,Element). +ast(aggregate_elements(Id,Pos,Element)) :- aggregate_elements(Id,Pos,Element). ast(aggregate(Id,LGuard,Elements,RGuard)) :- aggregate(Id,LGuard,Elements,RGuard). ast(theory_atom_elements(Id,Pos,TheoryTerms,Condition)) :- theory_atom_elements(Id,Pos,TheoryTerms,Condition). ast(theory_guard(Id,OpName,Term)) :- theory_guard(Id,OpName,Term). ast(theory_atom(Id,Atom,Elements,TheoryGuard)) :- theory_atom(Id,Atom,Elements,TheoryGuard). -ast(body_agg_elements(Id,Pos,Terms,Condition)) :- body_agg_elements(Id,Pos,Terms,Condition). +ast(body_aggregate_elements(Id,Pos,Terms,Condition)) :- body_aggregate_elements(Id,Pos,Terms,Condition). ast(body_aggregate(Id,LGuard,Elements,RGuard)) :- body_aggregate(Id,LGuard,Elements,RGuard). ast(body_literal(Id,Sign,Atom)) :- body_literal(Id,Sign,Atom). ast(body_literals(Id,Pos,Element)) :- body_literals(Id,Pos,Element). -ast(head_agg_elements(Id,Pos,Terms,Condition)) :- head_agg_elements(Id,Pos,Terms,Condition). +ast(head_aggregate_elements(Id,Pos,Terms,Condition)) :- head_aggregate_elements(Id,Pos,Terms,Condition). ast(head_aggregate(Id,LGuard,Elements,RGuard)) :- head_aggregate(Id,LGuard,Elements,RGuard). ast(conditional_literals(Id,Pos,CondLit)) :- conditional_literals(Id,Pos,CondLit). ast(disjunction(Id,Pos,Element)) :- disjunction(Id,Pos,Element). ast(rule(Id,Head,Body)) :- rule(Id,Head,Body). +ast(definition(Id,Name,Value,Default)) :- definition(Id,Name,Value,Default). +ast(show_signature(Id,Name,Arity,Positive)) :- show_signature(Id,Name,Arity,Positive). +ast(defined(Id,Name,Arity,Positive)) :- defined(Id,Name,Arity,Positive). +ast(minimize(Id,Weight,Priority,Terms,Body)) :- minimize(Id,Weight,Priority,Terms,Body). +ast(script(Id,Name,Code)) :- script(Id,Name,Code). ast(statements(Id,Pos,Element)) :- statements(Id,Pos,Element). ast(constants(Id,Pos,Element)) :- constants(Id,Pos,Element). -ast(program(Name,Params,Statements)) :- program(Name,Params,Statements). +ast(program(Id,Name,Params,Statements)) :- program(Id,Name,Params,Statements). ast(external(Atom,Body,Type)) :- external(Atom,Body,Type). +ast(edge(Id,U,V,Body)) :-edge(Id,U,V,Body). +ast(heuristic(Id,Atom,Body,Bias,Priority,Modifier)) :- heuristic(Id,Atom,Body,Bias,Priority,Modifier). +ast(project_atom(Id,Atom,Body)) :- project_atom(Id,Atom,Body). +ast(project_signature(Id,Name,Arity,Positive)) :- project_signature(Id,Name,Arity,Positive). +ast(theory_operator_definitions(Id,Pos,Name,Priority,OpType)) :- theory_operator_definitions(Id,Pos,Name,Priority,OpType). +ast(theory_term_definitions(Id,Pos,Name,Operators)) :- theory_term_definitions(Id,Pos,Name,Operators). +ast(theory_guard_definition(Id,Operators,Term)) :- theory_guard_definition(Id,Operators,Term). +ast(theory_atom_definitions(Id,Pos,AtomType,Name,Arity,Term,Guard)) :- theory_atom_definitions(Id,Pos,AtomType,Name,Arity,Term,Guard). +ast(theory_definition(Id,Name,Terms,Atoms)) :- theory_definition(Id,Name,Terms,Atoms). diff --git a/src/renopro/asp/defined.lp b/src/renopro/asp/defined.lp index a804a00..39d5845 100644 --- a/src/renopro/asp/defined.lp +++ b/src/renopro/asp/defined.lp @@ -21,21 +21,35 @@ #defined literal/3. #defined literals/3. #defined conditional_literal/3. -#defined agg_elements/3. +#defined aggregate_elements/3. #defined aggregate/4. #defined theory_atom_elements/4. #defined theory_guard/3. #defined theory_atom/4. -#defined body_agg_elements/4. +#defined body_aggregate_elements/4. #defined body_aggregate/4. #defined body_literal/3. #defined body_literals/3. -#defined head_agg_elements/4. +#defined head_aggregate_elements/4. #defined head_aggregate/4. #defined conditional_literals/3. #defined disjunction/3. #defined rule/3. +#defined definition/4. +#defined show_signature/4. +#defined defined/4. +#defined minimize/5. +#defined script/3. #defined statements/3. #defined constants/3. -#defined program/3. +#defined program/4. #defined external/3. +#defined edge/4. +#defined heuristic/6. +#defined project_atom/3. +#defined project_signature/4. +#defined theory_operator_definitions/5. +#defined theory_term_definitions/5. +#defined theory_guard_definition/3. +#defined theory_atom_definitions/7. +#defined theory_definition/4. diff --git a/src/renopro/asp/viz.lp b/src/renopro/asp/viz.lp index 6c8b9d6..8f44b6c 100644 --- a/src/renopro/asp/viz.lp +++ b/src/renopro/asp/viz.lp @@ -76,9 +76,9 @@ edge((literals(Id),Element)) :- literals(Id,Pos,Element). attr(edge,(literals(Id),Element),label,pos) :- literals(Id,Pos,Element). % node(conditional_literal(Id)) :- conditional_literal(Id,Literal,Condition). -% node(agg_elements(Id)) :- agg_elements(Id,Pos,Element). +% node(aggregate_elements(Id)) :- aggregate_elements(Id,Pos,Element). % node(aggregate(Id)) :- aggregate(Id,LGuard,Elements,RGuard). -% node(body_agg_elements(Id)) :- body_agg_elements(Id,Pos,Terms,Condition). +% node(body_aggregate_elements(Id)) :- body_aggregate_elements(Id,Pos,Terms,Condition). % node(body_aggregate(Id)) :- body_aggregate(Id,LGuard,Elements,RGuard). node(body_literal(Id)) :- body_literal(Id,Sign,Atom). @@ -93,7 +93,7 @@ edge((body_literals(Id),Element)) :- body_literals(Id,Pos,Element). attr(edge,(body_literals(Id),Element),label,Pos) :- body_literals(Id,Pos,Element). -% node(head_agg_elements(Id)) :- head_agg_elements(Id,Pos,Terms,Condition). +% node(head_aggregate_elements(Id)) :- head_aggregate_elements(Id,Pos,Terms,Condition). % node(head_aggregate(Id)) :- head_aggregate(Id,LGuard,Elements,RGuard). % node(disjunction(Id)) :- disjunction(Id,Pos,Element). diff --git a/src/renopro/predicates.py b/src/renopro/predicates.py index 73e5b57..653cf3d 100644 --- a/src/renopro/predicates.py +++ b/src/renopro/predicates.py @@ -1,8 +1,11 @@ +# pylint: disable=too-many-lines """Definitions of AST elements as clorm predicates.""" import enum import inspect +import re from itertools import count -from typing import List, Sequence, Type, TypeVar, Union +from types import new_class +from typing import Sequence, Type, TypeVar, Union from clorm import ( BaseField, @@ -12,42 +15,23 @@ Predicate, RawField, StringField, - combine_fields, - define_enum_field, refine_field, ) +from clorm.orm.core import _PredicateMeta id_count = count() -# by default we use integer identifiers, but allow arbitrary symbols as well -# for flexibility when these are user generated -Identifier_Field = combine_fields([IntegerField, RawField], name="IdentifierField") - - -class LazilyCombinedField(BaseField): # nocoverage - # pylint: disable=no-self-argument - "A field defined by lazily combining multiple existing field definitions." - fields: List[BaseField] = [] - # cannot use a staticmethod decorator as clorm raises error if cltopy/pytocl - # is not callable, and staticmethods are not callable for python < 3.10 - def cltopy(v): - pass - - def pytocl(v): - pass - - -def combine_fields_lazily( +def combine_fields( fields: Sequence[Type[BaseField]], *, name: str = "" -) -> Type[LazilyCombinedField]: +) -> Type[BaseField]: """Factory function that returns a field sub-class that combines other fields lazily. Essentially the same as the combine_fields defined in the clorm - package, but allows us to add additional fields after the initial - combination of fields by appending to the fields attribute of the - combined field. + package, but exposes a 'fields' attrible, allowing us to add + additional fields after the initial combination of fields by + appending to the 'fields' attribute of the combined field. """ subclass_name = name if name else "AnonymousCombinedBaseField" @@ -80,11 +64,72 @@ def _cltopy(symbol): ) ) - return type( - subclass_name, - (BaseField,), - {"fields": fields, "pytocl": _pytocl, "cltopy": _cltopy}, - ) + def body(ns): + ns.update({"fields": fields, "pytocl": _pytocl, "cltopy": _cltopy}) + + return new_class(subclass_name, (BaseField,), {}, body) + + +def define_enum_field( + parent_field: Type[BaseField], enum_class: Type[enum.Enum], *, name: str = "" +) -> Type[BaseField]: # nocoverage + """Factory function that returns a BaseField sub-class for an + Enum. Essentially the same as the one defined in clorm, but stores + the enum that defines the field under attribute 'enum' for later + use. + + Enums are part of the standard library since Python 3.4. This method + provides an alternative to using refine_field() to provide a restricted set + of allowable values. + + Example: + .. code-block:: python + + class IO(str,Enum): + IN="in" + OUT="out" + + # A field that unifies against ASP constants "in" and "out" + IOField = define_enum_field(ConstantField,IO) + + Positional argument: + + field_class: the field that is being sub-classed + + enum_class: the Enum class + + Optional keyword-only arguments: + + name: name for new class (default: anonymously generated). + + """ + subclass_name = name if name else parent_field.__name__ + "_Restriction" + if not inspect.isclass(parent_field) or not issubclass(parent_field, BaseField): + raise TypeError(f"{parent_field} is not a subclass of BaseField") + + if not inspect.isclass(enum_class) or not issubclass(enum_class, enum.Enum): + raise TypeError(f"{enum_class} is not a subclass of enum.Enum") + + values = set(i.value for i in enum_class) + + def _pytocl(py): + val = py.value + if val not in values: + raise ValueError( + f"'{val}' is not a valid value of enum class '{enum_class.__name__}'" + ) + return val + + def body(ns): + ns.update({"pytocl": _pytocl, "cltopy": enum_class, "enum": enum_class}) + + return new_class(subclass_name, (parent_field,), {}, body) + + +# by default we use integer identifiers, but allow arbitrary symbols as well +# for flexibility when these are user generated +IdentifierField = combine_fields([IntegerField, RawField], name="IdentifierField") +IdentifierField = IdentifierField(default=lambda: next(id_count)) # type: ignore # Enum field definitions @@ -202,6 +247,33 @@ class AggregateFunction(str, enum.Enum): ) +class TheoryOperatorType(str, enum.Enum): + "String enum of clingo's theory definition types" + BinaryLeft = "binary_left" + BinaryRight = "binary_right" + Unary = "unary" + + +TheoryOperatorTypeField = define_enum_field( + parent_field=ConstantField, + enum_class=TheoryOperatorType, + name="TheoryOperatorTypeField", +) + + +class TheoryAtomType(str, enum.Enum): + "String enum of clingo's theory atom types." + Any = "any" + Body = "body" + Directive = "directive" + Head = "head" + + +TheoryAtomTypeField = define_enum_field( + parent_field=ConstantField, enum_class=TheoryAtomType, name="TheoryAtomTypeField" +) + + A = TypeVar("A", bound=enum.Enum) B = TypeVar("B", bound=enum.Enum) @@ -224,63 +296,78 @@ def convert_enum(enum_member: A, other_enum: Type[B]) -> B: raise ValueError(msg) from exc -# Terms +# Metaclass shenanigans to dynamically create unary versions of AST predicates, +# which are used to identify child AST facts + + +class _AstPredicateMeta(_PredicateMeta): + def __new__(mcs, cls_name, bases, namespace, **kwargs): + pattern = re.compile(r"(?", 1, 1), Position("", 1, 1)) +NodeConstructor = Callable[..., Union[AST, Symbol]] +NodeAttr = Union[AST, Symbol, Sequence[Symbol], ASTSequence, str, int, StrSequence] + class ChildQueryError(Exception): """Exception raised when a required child fact of an AST fact @@ -71,7 +85,7 @@ def handle_unify_error(error): """ unmatched = error.symbol name2arity2pred = { - pred.meta.name: {pred.meta.arity: pred} for pred in preds.AstPredicates + pred.meta.name: {pred.meta.arity: pred} for pred in preds.AstPreds } candidate = name2arity2pred.get(unmatched.name, {}).get( len(unmatched.arguments) @@ -112,56 +126,22 @@ def handle_unify_error(error): raise RuntimeError("Code should be unreachable") # nocoverage -def dispatch_on_node_type(meth): - """ "Dispatch method on node.ast_type if node is of type AST - or dispatch on node.type if node is of type Symbol. - - """ - registry = {} - - def dispatch(value): - try: - return registry[value] - except KeyError: - return meth - - def register(value, func=None): - if func is None: - return lambda f: register(value, f) - - registry[value] = func - return func - - def wrapper(self, node, *args, **kw): - if isinstance(node, AST): - return dispatch(node.ast_type)(self, node, *args, **kw) - if isinstance(node, Symbol): - return dispatch(node.type)(self, node, *args, **kw) - return dispatch(type(node))(self, node, *args, **kw) - - wrapper.register = register - wrapper.dispatch = dispatch - wrapper.registry = registry - - return wrapper - - class ReifiedAST: """Class for converting between reified and non-reified representation of ASP programs. """ - def __init__(self): + def __init__(self, parse_theory_atoms: bool = False): self._reified = FactBase() - self._program_ast = [] - self._id_counter = -1 - self._statement_tup_id = None - self._statement_pos = 0 - self._program_string = "" + self._program_ast: Sequence[AST] = [] + self._current_statement: Tuple[int, int] = (0, 0) + self._tuple_pos: count = count() + self._init_overrides() + self.parse_theory_atoms = parse_theory_atoms - def add_reified_facts(self, reified_facts: Iterator[preds.AstPredicate]) -> None: + def add_reified_facts(self, reified_facts: Iterator[preds.AstPred]) -> None: """Add iterator of reified AST facts to internal factbase.""" - unifier = Unifier(preds.AstPredicates) + unifier = Unifier(preds.AstPreds) # couldn't find a way in clorm to directly add a set of facts # while checking unification, so we have to unify against the # underlying symbols @@ -173,7 +153,7 @@ def add_reified_facts(self, reified_facts: Iterator[preds.AstPredicate]) -> None def add_reified_string(self, reified_string: str) -> None: """Add string of reified facts into internal factbase.""" - unifier = preds.AstPredicates + unifier = preds.AstPreds with TryUnify(): facts = parse_fact_string( reified_string, unifier=unifier, raise_nomatch=True, raise_nonfact=True @@ -186,7 +166,7 @@ def add_reified_files(self, reified_files: Sequence[Path]) -> None: with TryUnify(): facts = parse_fact_files( reified_files_str, - unifier=preds.AstPredicates, + unifier=preds.AstPreds, raise_nomatch=True, raise_nonfact=True, ) @@ -195,24 +175,35 @@ def add_reified_files(self, reified_files: Sequence[Path]) -> None: def reify_string(self, prog_str: str) -> None: """Reify input program string, adding reified facts to the internal factbase.""" - parse_string(prog_str, self.reify_node) + self._program_ast = [] + parse_string(prog_str, self._program_ast.append) + self.reify_ast(self._program_ast) def reify_files(self, files: Sequence[Path]) -> None: """Reify input program files, adding reified facts to the internal factbase.""" + self._program_ast = [] for f in files: if not f.is_file(): # nocoverage raise IOError(f"File {f} does not exist.") files_str = [str(f) for f in files] - parse_files(files_str, self.reify_node) + parse_files(files_str, self.program_ast.append) + self.reify_ast(self._program_ast) + + def reify_ast(self, asts: Sequence[AST]) -> None: + """Reify input sequence of AST nodes, adding reified facts to + the internal factbase.""" + self._program_ast = asts + for statement in self._program_ast: + self.reify_node(statement) @property def program_string(self) -> str: """String representation of reflected AST facts.""" - return self._program_string + return "\n".join([str(statement) for statement in self._program_ast]) @property - def program_ast(self) -> List[AST]: + def program_ast(self) -> Sequence[AST]: """AST nodes attained via reflection of AST facts.""" return self._program_ast @@ -236,497 +227,353 @@ def reified_string_doc(self) -> str: # nocoverage """ return self._reified.asp_str(commented=True) - @dispatch_on_node_type - def reify_node(self, node): - """Reify the input ast node by adding it's clorm fact - representation to the internal fact base, and recursively - reify child nodes. + def _init_overrides(self): + """Initialize override functions that change the default + behavior when reifying or reflecting""" + self._reify_overrides = { + "node": { + # we don't include this node in our reified representation + # as it doesn't add much from a knowledge representation standpoint. + ASTType.SymbolicTerm: lambda node: self.reify_node(node.symbol), + ASTType.Id: self._reify_id, + ASTType.Function: self._reify_function, + }, + "attr": { + # body literals need special treatment, as we distinguish + # between them and regular literals while the clingo AST + # does not. + "body": lambda node: self._reify_body_literals(node.body), + "position": lambda none: next(self._tuple_pos), + }, + "node_attr": { + ASTType.BooleanConstant: { + "value": lambda node: self._reify_bool(node.value) + }, + ASTType.Program: {"statements": lambda none: preds.Statements.unary()}, + ASTType.Definition: { + "is_default": lambda node: self._reify_bool(node.is_default) + }, + ASTType.ShowSignature: { + "positive": lambda node: self._reify_bool(node.positive) + }, + ASTType.Defined: { + "positive": lambda node: self._reify_bool(node.positive) + }, + ASTType.External: { + "external_type": lambda node: node.external_type.symbol.name + }, + ASTType.ProjectSignature: { + "positive": lambda node: self._reify_bool(node.positive) + }, + ASTType.TheoryUnparsedTermElement: { + "operators": lambda node: self._reify_theory_operators( + node.operators + ) + }, + ASTType.TheoryGuardDefinition: { + "operators": lambda node: self._reify_theory_operators( + node.operators + ) + }, + }, + } + self._reflect_overrides = { + "pred": { + preds.String: lambda fact: ast.SymbolicTerm( + DUMMY_LOC, symbol.String(fact.string) + ), + preds.Number: lambda fact: ast.SymbolicTerm( + DUMMY_LOC, symbol.Number(fact.number) + ), + preds.Terms: lambda fact: self._reflect_child(fact, fact.term), + preds.TheoryTerms: lambda fact: self._reflect_child( + fact, fact.theory_term + ), + preds.TheoryOperators: lambda fact: fact.operator, + preds.Guards: lambda fact: self._reflect_child(fact, fact.guard), + preds.Literals: lambda fact: self._reflect_child(fact, fact.literal), + preds.AggregateElements: lambda fact: self._reflect_child( + fact, fact.element + ), + preds.BodyLiterals: lambda fact: self._reflect_child( + fact, fact.body_literal + ), + preds.ConditionalLiterals: lambda fact: self._reflect_child( + fact, fact.conditional_literal + ), + preds.Statements: lambda fact: self._reflect_child( + fact, fact.statement + ), + preds.Constants: lambda fact: self._reflect_child(fact, fact.constant), + preds.Program: self._reflect_program, + }, + "attr": {"location": lambda none: DUMMY_LOC}, + "pred_attr": { + preds.Comparison: { + "guards": lambda fact: self._reflect_child(fact, fact.guards, "+") + }, + preds.TheoryUnparsedTerm: { + "elements": lambda fact: self._reflect_child( + fact, fact.elements, "+" + ) + }, + # we represent regular function and external (that is script functions) + # with different predicates. + preds.Function: {"external": lambda none: 0}, + preds.ExternalFunction: {"external": lambda none: 1}, + preds.BooleanConstant: { + "value": lambda fact: self._reflect_bool(fact.value) + }, + preds.Definition: { + "is_default": lambda fact: self._reflect_bool(fact.is_default) + }, + preds.ShowSignature: { + "positive": lambda fact: self._reflect_bool(fact.positive) + }, + preds.Defined: { + "positive": lambda fact: self._reflect_bool(fact.positive) + }, + preds.ProjectSignature: { + "positive": lambda fact: self._reflect_bool(fact.positive) + }, + preds.External: { + "external_type": lambda fact: ast.SymbolicTerm( + DUMMY_LOC, symbol.Function(fact.external_type, []) + ) + }, + }, + } - """ - if hasattr(node, "ast_type"): # nocoverage - raise NotImplementedError( - ( - "Reification not implemented for AST nodes of type: " - f"{node.ast_type.name}." + def _reify_ast_sequence( + self, + ast_seq: Union[ASTSequence, Sequence[Symbol]], + tuple_predicate: Type[preds.AstPredicate], + ): + "Reify an ast sequence into a list of facts of type tuple_predicate." + tuple_unary_fact = tuple_predicate.unary() + reified_facts = [] + tmp = self._tuple_pos + self._tuple_pos = count() + if tuple_predicate in preds.FlattenedTuples: + for node in ast_seq: + self.reify_node( + node, predicate=tuple_predicate, id_term=tuple_unary_fact ) - ) - if hasattr(node, "type"): # nocoverage - raise NotImplementedError( - ( - "Reification not implemented for symbol of type: " - f"{node.typle.name}." + else: + for position, node in enumerate(ast_seq): + child_fact_unary = self.reify_node(node) + tuple_fact = tuple_predicate( + tuple_unary_fact.id, position, child_fact_unary ) - ) - raise TypeError(f"Nodes should be of type AST or Symbol, got: {type(node)}") - - def _reify_ast_seqence( - self, seq: ASTSequence, tup_id: BaseField, tup_pred: Type[preds.AstPredicate] - ): - """Reify ast sequence into a tuple of predicates of type - tup_pred with identifier tup_id.""" - reified_seq = [ - tup_pred(tup_id, pos, self.reify_node(item)) - for pos, item in enumerate(seq, start=0) - ] - self._reified.add(reified_seq) - - @reify_node.register(ASTType.SymbolicTerm) - def _reify_symbolic_term(self, node): - """Reify symbolic term. - - Note that the only possible child of a symbolic term is a - clingo symbol denoting a number, variable, or constant, so we - don't represent this ast node in our reification. - - """ - return self.reify_node(node.symbol) - - @reify_node.register(SymbolType.String) - def _reify_symbol_string(self, symb): - string1 = preds.String1() - self._reified.add(preds.String(id=string1.id, value=symb.string)) - return string1 - - @reify_node.register(SymbolType.Number) - def _reify_symbol_number(self, symb): - number1 = preds.Number1() - self._reified.add(preds.Number(id=number1.id, value=symb.number)) - return number1 - - @reify_node.register(ASTType.Variable) - def _reify_variable(self, node): - variable1 = preds.Variable1() - self._reified.add(preds.Variable(id=variable1.id, name=node.name)) - return variable1 - - @reify_node.register(ASTType.UnaryOperation) - def _reify_unary_operation(self, node): - clorm_operator = preds.convert_enum( - ast.UnaryOperator(node.operator_type), preds.UnaryOperator - ) - unop1 = preds.Unary_Operation1() - unop = preds.Unary_Operation( - id=unop1.id, - operator=clorm_operator, - argument=self.reify_node(node.argument), - ) - self._reified.add(unop) - return unop1 - - @reify_node.register(ASTType.BinaryOperation) - def _reify_binary_operation(self, node): - clorm_operator = preds.convert_enum( - ast.BinaryOperator(node.operator_type), preds.BinaryOperator - ) - binop1 = preds.Binary_Operation1() - binop = preds.Binary_Operation( - id=binop1.id, - operator=clorm_operator, - left=self.reify_node(node.left), - right=self.reify_node(node.right), - ) - self._reified.add(binop) - return binop1 + reified_facts.append(tuple_fact) + self._reified.add(reified_facts) + self._tuple_pos = tmp + return tuple_unary_fact - @reify_node.register(ASTType.Interval) - def _reify_interval(self, node): - interval1 = preds.Interval1() - left = self.reify_node(node.left) - right = self.reify_node(node.right) - interval = preds.Interval(id=interval1.id, left=left, right=right) - self._reified.add(interval) - return interval1 - - @reify_node.register(SymbolType.Function) - def _reify_symbol_function(self, symb): - """Reify constant term. - - Note that clingo represents constant terms as a - clingo.Symbol.Function with empty argument list. - - """ - func1 = preds.Function1() - self._reified.add( - preds.Function(id=func1.id, name=symb.name, arguments=preds.Terms1()) - ) - return func1 - - @reify_node.register(ASTType.Function) - def _reify_function(self, node): - """Reify an ast node with node.ast_type of ASTType.Function. - - Note that clingo's ast also represents propositional constants - as nodes with node.type of ASTType.Function and an empty - node.arguments list; thus some additional care must be taken - to create the correct clorm predicate. + @staticmethod + def _get_type_constructor_from_node( + node: Union[AST, Symbol] + ) -> Tuple[Union[ASTType, SymbolType], NodeConstructor]: + "Return the type and constructor of input ast node." + if isinstance(node, Symbol): + ast_type = node.type + ast_constructor: NodeConstructor = getattr(symbol, ast_type.name) + return ast_type, ast_constructor + if isinstance(node, AST): + symbol_type = node.ast_type + symbol_constructor: NodeConstructor = getattr(ast, symbol_type.name) + return symbol_type, symbol_constructor + raise TypeError(f"Node must be of type AST or Symbol, got: {type(node)}") + + def _reify_attr(self, annotation: Type, attr: NodeAttr, field: BaseField): + """Reify an AST node's attribute attr based on the type hint + for the respective argument in the AST node's constructor. + This default behavior is overridden in certain cases; see reify_node.""" + if annotation in [AST, Symbol, Optional[AST], Optional[Symbol]]: + if annotation in [Optional[AST], Optional[Symbol]] and attr is None: + return field.complex() + return self.reify_node(attr) # type: ignore + if annotation in [Sequence[Symbol], Sequence[AST]]: + return self._reify_ast_sequence( + attr, field.complex.non_unary # type: ignore + ) + if hasattr(field, "enum"): + ast_enum = getattr(ast, field.enum.__name__) + return preds.convert_enum(ast_enum(attr), field.enum) + if annotation in [str, int]: + return attr + raise RuntimeError("Code should be unreachable.") # nocoverage + + def reify_node(self, node: Union[AST, Symbol], predicate=None, id_term=None): + """Reify the input ast node by adding it's clorm fact + representation to the internal fact base, and recursively + reify child nodes. """ - function1 = preds.Function1() - function = preds.Function( - id=function1.id, name=node.name, arguments=preds.Terms1() - ) - self._reified.add(function) - self._reify_ast_seqence(node.arguments, function.arguments.id, preds.Terms) - return function1 - - @reify_node.register(ASTType.Pool) - def _reify_pool(self, node): - pool1 = preds.Pool1() - pool = preds.Pool(id=pool1.id, arguments=preds.Terms1()) - self._reified.add(pool) - self._reify_ast_seqence(node.arguments, pool.arguments.id, preds.Terms) - return pool1 - - @reify_node.register(ASTType.TheorySequence) - def _reify_theory_sequence(self, node): - theory_seq1 = preds.Theory_Sequence1() - clorm_theory_seq_type = preds.convert_enum( - ast.TheorySequenceType(node.sequence_type), preds.TheorySequenceType - ) - theory_seq = preds.Theory_Sequence( - id=theory_seq1.id, - sequence_type=clorm_theory_seq_type, - terms=preds.Theory_Terms1(), - ) - self._reified.add(theory_seq) - self._reify_ast_seqence(node.terms, theory_seq.terms.id, preds.Theory_Terms) - return theory_seq1 - - @reify_node.register(ASTType.TheoryFunction) - def _reify_theory_function(self, node): - theory_func1 = preds.Theory_Function1() - theory_func = preds.Theory_Function( - id=theory_func1.id, name=node.name, arguments=preds.Theory_Terms1() - ) - self._reified.add(theory_func) - self._reify_ast_seqence( - seq=node.arguments, - tup_id=theory_func.arguments.id, - tup_pred=preds.Theory_Terms, - ) - return theory_func1 - - @reify_node.register(ASTType.TheoryUnparsedTerm) - def _reify_theory_unparsed_term(self, node): - reified_unparsed_theory_term1 = preds.Theory_Unparsed_Term1() - reified_unparsed_elements1 = preds.Theory_Unparsed_Term_Elements1() - # reified_unparsed_theory_term = preds.Theory_Unparsed_Term( - # id=reified_unparsed_theory_term1.id, - # elements=preds.Theory_Unparsed_Term_Elements1(), - # ) - for pos, element in enumerate(node.elements): - operators = preds.Theory_Operators1() - reified_operators = [ - preds.Theory_Operators(id=operators.id, position=p, operator=op) - for p, op in enumerate(element.operators) - ] - self._reified.add(reified_operators) - reified_theory_term1 = self.reify_node(element.term) - reified_unparsed_elements = preds.Theory_Unparsed_Term_Elements( - id=reified_unparsed_elements1.id, - position=pos, - operators=operators, - term=reified_theory_term1, - ) - self._reified.add(reified_unparsed_elements) - reified_unparsed = preds.Theory_Unparsed_Term( - id=reified_unparsed_theory_term1.id, elements=reified_unparsed_elements1 - ) - self._reified.add(reified_unparsed) - return reified_unparsed_theory_term1 - - @reify_node.register(ASTType.Guard) - def _reify_guard(self, node): - guard1 = preds.Guard1() - clorm_operator = preds.convert_enum( - ast.ComparisonOperator(node.comparison), preds.ComparisonOperator - ) - guard = preds.Guard( - id=guard1.id, comparison=clorm_operator, term=self.reify_node(node.term) - ) - self._reified.add(guard) - return guard1 - - @reify_node.register(ASTType.Comparison) - def _reify_comparison(self, node): - comparison1 = preds.Comparison1() - comparison = preds.Comparison( - id=comparison1.id, term=self.reify_node(node.term), guards=preds.Guards1() - ) - self._reified.add(comparison) - self._reify_ast_seqence(node.guards, comparison.guards.id, preds.Guards) - return comparison1 - - @reify_node.register(ASTType.BooleanConstant) - def _reify_boolean_constant(self, node): - bool_const1 = preds.Boolean_Constant1() - bool_str = "" - if node.value == 1: - bool_str = "true" - elif node.value == 0: - bool_str = "false" - else: # nocoverage - raise RuntimeError("Code should be unreachable") - bool_const = preds.Boolean_Constant(id=bool_const1.id, value=bool_str) - self._reified.add(bool_const) - return bool_const1 - - @reify_node.register(ASTType.SymbolicAtom) - def _reify_symbolic_atom(self, node): - atom1 = preds.Symbolic_Atom1() - atom = preds.Symbolic_Atom(id=atom1.id, symbol=self.reify_node(node.symbol)) - self._reified.add(atom) - return atom1 - - @reify_node.register(ASTType.Literal) - def _reify_literal(self, node): - clorm_sign = preds.convert_enum(ast.Sign(node.sign), preds.Sign) - lit1 = preds.Literal1() - lit = preds.Literal(id=lit1.id, sig=clorm_sign, atom=self.reify_node(node.atom)) - self._reified.add(lit) - return lit1 - - @reify_node.register(ASTType.ConditionalLiteral) - def _reify_conditional_literal(self, node) -> preds.Conditional_Literal1: - cond_lit1 = preds.Conditional_Literal1() - cond_lit = preds.Conditional_Literal( - id=cond_lit1.id, - literal=self.reify_node(node.literal), - condition=preds.Literals1(), - ) - self._reified.add(cond_lit) - self._reify_ast_seqence(node.condition, cond_lit.condition.id, preds.Literals) - return cond_lit1 - - @reify_node.register(ASTType.Aggregate) - def _reify_aggregate(self, node) -> preds.Aggregate1: - count_agg1 = preds.Aggregate1() - left_guard = ( - preds.Guard1() - if node.left_guard is None - else self.reify_node(node.left_guard) - ) - elements1 = preds.Agg_Elements1() - self._reify_ast_seqence(node.elements, elements1.id, preds.Agg_Elements) - right_guard = ( - preds.Guard1() - if node.right_guard is None - else self.reify_node(node.right_guard) - ) - count_agg = preds.Aggregate( - id=count_agg1.id, - left_guard=left_guard, - elements=elements1, - right_guard=right_guard, - ) - self._reified.add(count_agg) - return count_agg1 - - @reify_node.register(ASTType.TheoryAtom) - def _reify_theory_atom(self, node) -> preds.Theory_Atom1: - theory_atom1 = preds.Theory_Atom1() - # we make a slight modification in the reified representation - # vs the AST, wrapping the function into a symbolic atom, as - # that's what it really is IMO. - theory_symbolic_atom1 = self.reify_node(ast.SymbolicAtom(node.term)) - theory_atom_elements1 = preds.Theory_Atom_Elements1() - reified_elements = [] - for pos, element in enumerate(node.elements): - theory_terms1 = preds.Theory_Terms1() - self._reify_ast_seqence(element.terms, theory_terms1.id, preds.Theory_Terms) - literals1 = preds.Literals1() - self._reify_ast_seqence(element.condition, literals1.id, preds.Literals) - reified_element = preds.Theory_Atom_Elements( - id=theory_atom_elements1.id, - position=pos, - terms=theory_terms1, - condition=literals1, - ) - reified_elements.append(reified_element) - self._reified.add(reified_elements) - theory_guard1 = preds.Theory_Guard1() - if node.guard is not None: - guard_theory_term = self.reify_node(node.guard.term) - theory_guard = preds.Theory_Guard( - id=theory_guard1.id, - operator_name=node.guard.operator_name, - term=guard_theory_term, - ) - self._reified.add(theory_guard) - theory_atom = preds.Theory_Atom( - id=theory_atom1.id, - atom=theory_symbolic_atom1, - elements=theory_atom_elements1, - guard=theory_guard1, - ) - self._reified.add(theory_atom) - return theory_atom1 - - @reify_node.register(ASTType.BodyAggregate) - def _reify_body_aggregate(self, node) -> preds.Body_Aggregate1: - agg1 = preds.Body_Aggregate1() - left_guard = ( - preds.Guard1() - if node.left_guard is None - else self.reify_node(node.left_guard) - ) - clorm_agg_func = preds.convert_enum( - ast.AggregateFunction(node.function), preds.AggregateFunction - ) - elements1 = preds.Body_Agg_Elements1() - reified_elements = [] - for pos, element in enumerate(node.elements): - terms1 = preds.Terms1() - self._reify_ast_seqence(element.terms, terms1.id, preds.Terms) - literals1 = preds.Literals1() - self._reify_ast_seqence(element.condition, literals1.id, preds.Literals) - reified_element = preds.Body_Agg_Elements( - id=elements1.id, position=pos, terms=terms1, condition=literals1 + node_type, node_constructor = self._get_type_constructor_from_node(node) + + if node_override_func := self._reify_overrides["node"].get(node_type): + return node_override_func(node) + + annotations = node_constructor.__annotations__ + predicate = getattr(preds, node_type.name) if predicate is None else predicate + id_term = predicate.unary() if id_term is None else id_term + kwargs_dict = {"id": id_term.id} + + for key in predicate.meta.keys(): + # the id field has no corresponding attribute in nodes to be reified + if key == "id": + continue + if ( + node_attr_overrides := self._reify_overrides["node_attr"].get(node_type) + ) and (attr_override_func := node_attr_overrides.get(key)): + kwargs_dict.update({key: attr_override_func(node)}) + continue + if attr_override_func := self._reify_overrides["attr"].get(key): + kwargs_dict.update({key: attr_override_func(node)}) + continue + # sign is a reserved field name in clorm, so we had use + # sign_ instead + if key == "sign_": + annotation = annotations["sign"] + attr = getattr(node, "sign") + else: + annotation = annotations.get(key) + attr = getattr(node, key) + field = getattr(predicate, key).meta.field + reified_attr = self._reify_attr(annotation, attr, field) + kwargs_dict.update({key: reified_attr}) + reified_fact = predicate(**kwargs_dict) + self._reified.add(reified_fact) + if predicate is preds.Program: + self._current_statement = (reified_fact.statements.id, 0) + if predicate in preds.SubprogramStatements: + id_, position = self._current_statement + statement_element = preds.Statements( + id=id_, position=position, statement=id_term ) - reified_elements.append(reified_element) - self._reified.add(reified_elements) - right_guard = ( - preds.Guard1() - if node.right_guard is None - else self.reify_node(node.right_guard) - ) - agg = preds.Body_Aggregate( - id=agg1.id, - left_guard=left_guard, - function=clorm_agg_func, - elements=elements1, - right_guard=right_guard, - ) - self._reified.add(agg) - return agg1 + self._reified.add(statement_element) + self._current_statement = (id_, position + 1) + return id_term + + def _reify_theory_operators(self, operators: Sequence[str]): + operators1 = preds.TheoryOperators.unary() + reified_operators = [ + preds.TheoryOperators(id=operators1.id, position=p, operator=op) + for p, op in enumerate(operators) + ] + self._reified.add(reified_operators) + return operators1 - def _reify_body_literals(self, body_lits: Sequence[ast.AST], body_id): + def _reify_body_literals(self, nodes: Sequence[ast.AST]): + body_lits1 = preds.BodyLiterals.unary() reified_body_lits = [] - for pos, lit in enumerate(body_lits, start=0): + for pos, lit in enumerate(nodes, start=0): if lit.ast_type is ast.ASTType.ConditionalLiteral: cond_lit1 = self.reify_node(lit) reified_body_lits.append( - preds.Body_Literals( - id=body_id, position=pos, body_literal=cond_lit1 + preds.BodyLiterals( + id=body_lits1.id, position=pos, body_literal=cond_lit1 ) ) else: - body_lit1 = preds.Body_Literal1() + body_lit1 = preds.BodyLiteral.unary() reified_body_lits.append( - preds.Body_Literals( - id=body_id, position=pos, body_literal=body_lit1 + preds.BodyLiterals( + id=body_lits1.id, position=pos, body_literal=body_lit1 ) ) clorm_sign = preds.convert_enum(ast.Sign(lit.sign), preds.Sign) - body_lit = preds.Body_Literal( - id=body_lit1.id, sig=clorm_sign, atom=self.reify_node(lit.atom) + body_lit = preds.BodyLiteral( + id=body_lit1.id, sign_=clorm_sign, atom=self.reify_node(lit.atom) ) self._reified.add(body_lit) self._reified.add(reified_body_lits) + return body_lits1 - @reify_node.register(ASTType.HeadAggregate) - def _reify_head_aggregate(self, node) -> preds.Head_Aggregate1: - agg1 = preds.Head_Aggregate1() - left_guard = ( - preds.Guard1() - if node.left_guard is None - else self.reify_node(node.left_guard) - ) - clorm_agg_func = preds.convert_enum( - ast.AggregateFunction(node.function), preds.AggregateFunction - ) - elements1 = preds.Head_Agg_Elements1() - reified_elements = [] - for pos, element in enumerate(node.elements): - terms1 = preds.Terms1() - self._reify_ast_seqence(element.terms, terms1.id, preds.Terms) - cond_lit1 = self.reify_node(element.condition) - reified_element = preds.Head_Agg_Elements( - id=elements1.id, position=pos, terms=terms1, condition=cond_lit1 + def _reify_function(self, node): + if node.external == 0: + pred = preds.Function + func1 = pred.unary() + else: + pred = preds.ExternalFunction + func1 = pred.unary() + self._reified.add( + pred( + func1.id, + node.name, + arguments=self._reify_ast_sequence(node.arguments, preds.Terms), ) - reified_elements.append(reified_element) - self._reified.add(reified_elements) - right_guard = ( - preds.Guard1() - if node.right_guard is None - else self.reify_node(node.right_guard) ) - agg = preds.Head_Aggregate( - id=agg1.id, - left_guard=left_guard, - function=clorm_agg_func, - elements=elements1, - right_guard=right_guard, - ) - self._reified.add(agg) - return agg1 - - @reify_node.register(ASTType.Disjunction) - def _reify_disjunction(self, node) -> preds.Disjunction1: - disj1 = preds.Disjunction1() - cond_lits1 = preds.Conditional_Literals1() - self._reify_ast_seqence( - node.elements, cond_lits1.id, preds.Conditional_Literals + return func1 + + def _reify_id(self, node): + const1 = preds.Function.unary() + self._reified.add( + preds.Function(id=const1.id, name=node.name, arguments=preds.Terms.unary()) ) - disj = preds.Disjunction(id=disj1.id, elements=cond_lits1) - self._reified.add(disj) - return disj1 - - @reify_node.register(ASTType.Rule) - def _reify_rule(self, node): - rule1 = preds.Rule1() - head = self.reify_node(node.head) - rule = preds.Rule(id=rule1.id, head=head, body=preds.Body_Literals1()) - self._reified.add(rule) - statement_tup = preds.Statements( - id=self._statement_tup_id, position=self._statement_pos, statement=rule1 + return const1 + + def _reify_bool(self, boolean: int): + return "true" if boolean == 1 else "false" + + def _get_children( + self, + parent_fact: preds.AstPred, + child_id_fact, + expected_children_num: Literal["1", "?", "+", "*"] = "1", + ) -> Sequence[AST]: + identifier = child_id_fact.id + child_ast_pred = getattr(preds, type(child_id_fact).__name__.rstrip("1")) + query = self._reified.query(child_ast_pred).where( + child_ast_pred.id == identifier ) - self._reified.add(statement_tup) - self._statement_pos += 1 - self._reify_body_literals(node.body, rule.body.id) - - @reify_node.register(ASTType.Program) - def _reify_program(self, node): - const_tup_id = preds.Constants1() - for pos, param in enumerate(node.parameters, start=0): - const_tup = preds.Constants( - id=const_tup_id.id, position=pos, constant=preds.Function1() + base_msg = f"Error finding child fact of fact '{parent_fact}':\n" + if expected_children_num == "1": + child_facts = list(query.all()) + num_child_facts = len(child_facts) + if num_child_facts == 1: + return child_facts + msg = ( + f"Expected 1 child fact for identifier '{child_id_fact}'" + f", found {num_child_facts}." ) - const = preds.Function( - id=const_tup.constant.id, name=param.name, arguments=preds.Terms1() + raise ChildQueryError(base_msg + msg) + if expected_children_num == "?": + child_facts = list(query.all()) + num_child_facts = len(child_facts) + if num_child_facts in [0, 1]: + return child_facts + msg = ( + f"Expected 0 or 1 child fact for identifier " + f"'{child_id_fact}', found {num_child_facts}." ) - self._reified.add([const_tup, const]) - program = preds.Program( - name=node.name, parameters=const_tup_id, statements=preds.Statements1() - ) - self._reified.add(program) - self._statement_tup_id = program.statements.id - self._statement_pos = 0 - - @reify_node.register(ASTType.External) - def _reify_external(self, node): - ext_type = node.external_type.symbol.name - external1 = preds.External1() - external = preds.External( - id=external1.id, - atom=self.reify_node(node.atom), - body=preds.Body_Literals1(), - external_type=ext_type, - ) - self._reified.add(external) - statement_tup = preds.Statements( - id=self._statement_tup_id, position=self._statement_pos, statement=external1 - ) - self._reified.add(statement_tup) - self._statement_pos += 1 - self._reify_body_literals(node.body, external.body.id) - - ExpectedNum = Literal["1", "?", "+", "*"] + raise ChildQueryError(base_msg + msg) + # pylint: disable=consider-using-in + if expected_children_num == "*" or expected_children_num == "+": + query = query.order_by(child_ast_pred.position) + child_facts = list(query.all()) + num_child_facts = len(child_facts) + # check that there are no tuple elements in the same position + if num_child_facts != len(set(tup.position for tup in child_facts)): + msg = ( + "Found multiple child facts in the same position for " + f"identifier '{child_id_fact}'." + ) + raise ChildrenQueryError(base_msg + msg) + if expected_children_num == "+" and num_child_facts == 0: + msg = ( + f"Expected 1 or more child facts for identifier " + f"'{child_id_fact}', found 0." + ) + raise ChildrenQueryError(base_msg + msg) + return child_facts + assert_never(expected_children_num) @overload def _reflect_child( self, - parent_fact: preds.AstPredicate, + parent_fact: preds.AstPred, child_id_fact, expected_children_num: Literal["1"], ) -> AST: # nocoverage @@ -735,13 +582,15 @@ def _reflect_child( # for handling the default argument "1" @overload - def _reflect_child(self, parent_fact: preds.AstPredicate, child_id_fact) -> AST: #nocoverage + def _reflect_child( + self, parent_fact: preds.AstPred, child_id_fact + ) -> AST: # nocoverage ... @overload def _reflect_child( self, - parent_fact: preds.AstPredicate, + parent_fact: preds.AstPred, child_id_fact, expected_children_num: Literal["?"], ) -> Optional[AST]: # nocoverage @@ -750,7 +599,7 @@ def _reflect_child( @overload def _reflect_child( self, - parent_fact: preds.AstPredicate, + parent_fact: preds.AstPred, child_id_fact, expected_children_num: Literal["*", "+"], ) -> Sequence[AST]: # nocoverage @@ -758,9 +607,9 @@ def _reflect_child( def _reflect_child( self, - parent_fact: preds.AstPredicate, + parent_fact: preds.AstPred, child_id_fact, - expected_children_num: ExpectedNum = "1", + expected_children_num: Literal["1", "?", "+", "*"] = "1", ) -> Union[None, AST, Sequence[AST]]: """Utility function that takes a unary ast predicate identifying a child predicate, queries reified factbase for @@ -768,384 +617,97 @@ def _reflect_child( reflecting the child predicate. """ - identifier = child_id_fact.id - child_ast_pred = getattr(preds, type(child_id_fact).__name__.rstrip("1")) - query = self._reified.query(child_ast_pred).where( - child_ast_pred.id == identifier + child_facts = self._get_children( + parent_fact, child_id_fact, expected_children_num ) - base_msg = f"Error finding child fact of fact '{parent_fact}':\n" - if expected_children_num == "1": - child_facts = list(query.all()) - num_child_facts = len(child_facts) - if num_child_facts == 1: - return self.reflect_predicate(child_facts[0]) - else: - msg = ( - f"Expected 1 child fact for identifier '{child_id_fact}'" - f", found {num_child_facts}." - ) - raise ChildQueryError(base_msg + msg) - elif expected_children_num == "?": - child_facts = list(query.all()) - num_child_facts = len(child_facts) - if num_child_facts == 0: + num_child_facts = len(child_facts) + # pylint: disable=consider-using-in + if expected_children_num == "1" or expected_children_num == "?": + if expected_children_num == "?" and num_child_facts == 0: return None - elif num_child_facts == 1: - return self.reflect_predicate(child_facts[0]) - else: - msg = ( - f"Expected 0 or 1 child fact for identifier " - f"'{child_id_fact}', found {num_child_facts}." - ) - raise ChildQueryError(base_msg + msg) - elif expected_children_num == "*" or expected_children_num == "+": - query = query.order_by(child_ast_pred.position) - child_facts = list(query.all()) - num_child_facts = len(child_facts) - # check that there are no tuple elements in the same position - if num_child_facts != len(set(tup.position for tup in child_facts)): - msg = ( - "Found multiple child facts in the same position for " - f"identifier '{child_id_fact}'." - ) - raise ChildrenQueryError(base_msg + msg) - if expected_children_num == "+" and num_child_facts == 0: - msg = ( - f"Expected 1 or more child facts for identifier " - f"'{child_id_fact}', found 0." - ) - raise ChildrenQueryError(base_msg + msg) - child_nodes = [self.reflect_predicate(fact) for fact in child_facts] + return self.reflect_fact(child_facts[0]) + if expected_children_num == "+" or expected_children_num == "*": + child_nodes = [self.reflect_fact(fact) for fact in child_facts] return child_nodes assert_never(expected_children_num) + @staticmethod + def _node_constructor_from_pred(ast_pred: Type[preds.AstPred]) -> NodeConstructor: + "Return the constructor function from clingo.ast corresponding to ast_pred." + type_name = ast_pred.__name__.rstrip("s") + if ast_pred is preds.ExternalFunction: + return ast.Function + if ast_pred is preds.BodyLiteral: + return ast.Literal + ast_constructor = getattr(ast, type_name, None) + if ast_constructor is not None: + return ast_constructor + # currently this never gets executed, as symbol predicates get overridden + symbol_constructor = getattr(symbol, type_name, None) # nocoverage + if symbol_constructor is not None: # nocoverage + return symbol_constructor + raise ValueError( + f"AST Predicate '{ast_pred}' has no associated node constructor." + ) # nocoverage + @singledispatchmethod - def reflect_predicate(self, pred: preds.AstPredicate): # nocoverage + def reflect_fact(self, fact: preds.AstPred): # nocoverage """Convert the input AST element's reified fact representation back into a the corresponding member of clingo's abstract syntax tree, recursively reflecting all child facts. """ - raise NotImplementedError( - f"Reflection not implemented for predicate of type {type(pred)}." - ) - - @reflect_predicate.register - def _reflect_string(self, string: preds.String) -> AST: - """Reflect a String fact into a String symbol wrapped in SymbolicTerm.""" - return ast.SymbolicTerm( - location=DUMMY_LOC, symbol=symbol.String(string=str(string.value)) - ) - - @reflect_predicate.register - def _reflect_number(self, number: preds.Number) -> AST: - """Reflect a Number fact into a Number node.""" - return ast.SymbolicTerm( - location=DUMMY_LOC, - symbol=symbol.Number(number=number.value), # type: ignore - ) - - @reflect_predicate.register - def _reflect_variable(self, var: preds.Variable) -> AST: - """Reflect a Variable fact into a Variable node.""" - return ast.Variable(location=DUMMY_LOC, name=str(var.name)) - - @reflect_predicate.register - def _reflect_unary_operation(self, operation: preds.Unary_Operation) -> AST: - """Reflect a Unary_Operation fact into a UnaryOperation node.""" - clingo_operator = preds.convert_enum( - preds.UnaryOperator(operation.operator), ast.UnaryOperator - ) - return ast.UnaryOperation( - location=DUMMY_LOC, - operator_type=clingo_operator, - argument=self._reflect_child(operation, operation.argument), - ) - - @reflect_predicate.register - def _reflect_binary_operation(self, operation: preds.Binary_Operation) -> AST: - """Reflect a Binary_Operation fact into a BinaryOperation node.""" - clingo_operator = preds.convert_enum( - preds.BinaryOperator(operation.operator), ast.BinaryOperator - ) - reflected_left = self._reflect_child(operation, operation.left) - reflected_right = self._reflect_child(operation, operation.right) - return ast.BinaryOperation( - location=DUMMY_LOC, - operator_type=clingo_operator, - left=reflected_left, - right=reflected_right, - ) - - @reflect_predicate.register - def _reflect_interval(self, interval: preds.Interval) -> AST: - reflected_left = self._reflect_child(interval, interval.left) - reflected_right = self._reflect_child(interval, interval.right) - return ast.Interval( - location=DUMMY_LOC, left=reflected_left, right=reflected_right - ) - - @reflect_predicate.register - def _reflect_terms(self, terms: preds.Terms) -> AST: - return self._reflect_child(terms, terms.term) - - @reflect_predicate.register - def _reflect_function(self, func: preds.Function) -> AST: - """Reflect a Function fact into a Function node. - - Note that a Function fact is used to represent a propositional - constant, predicate, function symbol, or constant term. All of - these can be validly represented by a Function node in the - clingo AST and so we can return a Function node in each case. - Constant terms are parsed a Symbol by the parser, thus we need - to handle them differently when reifying. - - """ - arg_nodes = self._reflect_child(func, func.arguments, "*") - return ast.Function( - location=DUMMY_LOC, name=str(func.name), arguments=arg_nodes, external=0 - ) - - @reflect_predicate.register - def _reflect_pool(self, pool: preds.Pool) -> AST: - arg_nodes = self._reflect_child(pool, pool.arguments, "*") - return ast.Pool(location=DUMMY_LOC, arguments=arg_nodes) - - @reflect_predicate.register - def _reflect_theory_terms(self, theory_terms: preds.Theory_Terms) -> AST: - return self._reflect_child(theory_terms, theory_terms.theory_term) - - @reflect_predicate.register - def _reflect_theory_sequence(self, theory_seq: preds.Theory_Sequence) -> AST: - clingo_theory_sequence_type = preds.convert_enum( - preds.TheorySequenceType(theory_seq.sequence_type), ast.TheorySequenceType - ) - theory_term_nodes = self._reflect_child(theory_seq, theory_seq.terms, "*") - return ast.TheorySequence( - location=DUMMY_LOC, - sequence_type=clingo_theory_sequence_type, - terms=theory_term_nodes, - ) - - @reflect_predicate.register - def _reflect_theory_function(self, theory_func: preds.Theory_Function) -> AST: - arguments = self._reflect_child(theory_func, theory_func.arguments, "*") - return ast.TheoryFunction( - location=DUMMY_LOC, name=str(theory_func.name), arguments=arguments - ) - - @reflect_predicate.register - def _reflect_theory_operators( - self, theory_operators: preds.Theory_Operators - ) -> AST: - return theory_operators.operator - - @reflect_predicate.register - def _reflect_theory_unparsed_term_elements( - self, elements: preds.Theory_Unparsed_Term_Elements - ) -> AST: - reflected_operators = self._reflect_child(elements, elements.operators, "*") - reflected_term = self._reflect_child(elements, elements.term) - return ast.TheoryUnparsedTermElement( - operators=reflected_operators, term=reflected_term - ) - - @reflect_predicate.register - def _reflect_theory_unparsed_term( - self, theory_unparsed_term: preds.Theory_Unparsed_Term - ) -> AST: - reflected_elements = self._reflect_child( - theory_unparsed_term, theory_unparsed_term.elements, "*" - ) - return ast.TheoryUnparsedTerm(location=DUMMY_LOC, elements=reflected_elements) - - @reflect_predicate.register - def _reflect_guard(self, guard: preds.Guard) -> AST: - clingo_operator = preds.convert_enum( - preds.ComparisonOperator(guard.comparison), ast.ComparisonOperator - ) - reflected_guard = self._reflect_child(guard, guard.term) - return ast.Guard(comparison=clingo_operator, term=reflected_guard) - - @reflect_predicate.register - def _reflect_guards(self, guards: preds.Guards) -> AST: - return self._reflect_child(guards, guards.guard) - - @reflect_predicate.register - def _reflect_comparison(self, comparison: preds.Comparison) -> AST: - term_node = self._reflect_child(comparison, comparison.term) - guard_nodes = self._reflect_child(comparison, comparison.guards, "+") - return ast.Comparison(term=term_node, guards=guard_nodes) - - @reflect_predicate.register - def _reflect_boolean_constant(self, bool_const: preds.Boolean_Constant) -> AST: - bool_const_term = bool_const.value - if bool_const_term == "true": - b = 1 - elif bool_const_term == "false": - b = 0 - else: # nocoverage - raise RuntimeError("Code should be unreachable") - return ast.BooleanConstant(value=b) - - @reflect_predicate.register - def _reflect_symbolic_atom(self, atom: preds.Symbolic_Atom) -> AST: - reflected_symbol = self._reflect_child(atom, atom.symbol) - return ast.SymbolicAtom(symbol=reflected_symbol) - - @reflect_predicate.register - def _reflect_literal(self, lit: preds.Literal) -> AST: - clingo_sign = preds.convert_enum(preds.Sign(lit.sig), ast.Sign) - reflected_atom = self._reflect_child(lit, lit.atom) - return ast.Literal(location=DUMMY_LOC, sign=clingo_sign, atom=reflected_atom) - - @reflect_predicate.register - def _reflect_literals(self, literals: preds.Literals) -> AST: - return self._reflect_child(literals, literals.literal) - - @reflect_predicate.register - def _reflect_conditional_literal(self, cond_lit: preds.Conditional_Literal) -> AST: - reflected_literal = self._reflect_child(cond_lit, cond_lit.literal) - reflected_condition = self._reflect_child(cond_lit, cond_lit.condition, "*") - return ast.ConditionalLiteral( - location=DUMMY_LOC, literal=reflected_literal, condition=reflected_condition - ) - - @reflect_predicate.register - def _reflect_agg_elements(self, agg_elements: preds.Agg_Elements) -> AST: - return self._reflect_child(agg_elements, agg_elements.element) - - @reflect_predicate.register - def _reflect_aggregate(self, aggregate: preds.Aggregate) -> AST: - reflected_left_guard = self._reflect_child(aggregate, aggregate.left_guard, "?") - reflected_right_guard = self._reflect_child( - aggregate, aggregate.right_guard, "?" - ) - reflected_elements = self._reflect_child(aggregate, aggregate.elements, "*") - return ast.Aggregate( - location=DUMMY_LOC, - left_guard=reflected_left_guard, - elements=reflected_elements, - right_guard=reflected_right_guard, - ) - - @reflect_predicate.register - def _reflect_theory_guard(self, theory_guard: preds.Theory_Guard) -> AST: - reflected_operator_name = theory_guard.operator_name - reflected_theory_term = self._reflect_child(theory_guard, theory_guard.term) - return ast.TheoryGuard( - operator_name=str(reflected_operator_name), term=reflected_theory_term - ) - - @reflect_predicate.register - def _reflect_theory_atom_elements( - self, elements: preds.Theory_Atom_Elements - ) -> AST: - reflected_terms = self._reflect_child(elements, elements.terms, "*") - reflected_condition = self._reflect_child(elements, elements.condition, "*") - return ast.TheoryAtomElement( - terms=reflected_terms, condition=reflected_condition - ) - - @reflect_predicate.register - def _reflect_theory_atom(self, theory_atom: preds.Theory_Atom) -> AST: - reflected_syb_atom = self._reflect_child(theory_atom, theory_atom.atom) - reflected_elements = self._reflect_child(theory_atom, theory_atom.elements, "*") - reflected_guard = self._reflect_child(theory_atom, theory_atom.guard, "?") - return ast.TheoryAtom( - location=DUMMY_LOC, - term=reflected_syb_atom.symbol, - elements=reflected_elements, - guard=reflected_guard, - ) - - @reflect_predicate.register - def _reflect_body_agg_elements(self, elements: preds.Body_Agg_Elements) -> AST: - reflected_terms = self._reflect_child(elements, elements.terms, "*") - reflected_condition = self._reflect_child(elements, elements.condition, "*") - return ast.BodyAggregateElement( - terms=reflected_terms, condition=reflected_condition - ) - - @reflect_predicate.register - def _reflect_body_aggregate(self, aggregate: preds.Body_Aggregate) -> AST: - reflected_left_guard = self._reflect_child(aggregate, aggregate.left_guard, "?") - reflected_agg_function = preds.convert_enum( - preds.AggregateFunction(aggregate.function), ast.AggregateFunction - ) - reflected_elements = self._reflect_child(aggregate, aggregate.elements, "*") - reflected_right_guard = self._reflect_child( - aggregate, aggregate.right_guard, "?" - ) - return ast.BodyAggregate( - location=DUMMY_LOC, - left_guard=reflected_left_guard, - function=reflected_agg_function, - elements=reflected_elements, - right_guard=reflected_right_guard, - ) - - @reflect_predicate.register - def _reflect_body_literals(self, body_literals: preds.Body_Literals) -> AST: - return self._reflect_child(body_literals, body_literals.body_literal) - - @reflect_predicate.register - def _reflect_body_literal(self, body_lit: preds.Body_Literal) -> AST: - return self._reflect_literal(body_lit) - - @reflect_predicate.register - def _reflect_head_agg_elements(self, elements: preds.Head_Agg_Elements) -> AST: - reflected_terms = self._reflect_child(elements, elements.terms, "*") - reflected_condition = self._reflect_child(elements, elements.condition) - return ast.HeadAggregateElement( - terms=reflected_terms, condition=reflected_condition - ) - - @reflect_predicate.register - def _reflect_head_aggregate(self, aggregate: preds.Head_Aggregate) -> AST: - reflected_left_guard = self._reflect_child(aggregate, aggregate.left_guard, "?") - reflected_agg_function = preds.convert_enum( - preds.AggregateFunction(aggregate.function), ast.AggregateFunction - ) - reflected_elements = self._reflect_child(aggregate, aggregate.elements, "*") - reflected_right_guard = self._reflect_child( - aggregate, aggregate.right_guard, "?" - ) - return ast.HeadAggregate( - location=DUMMY_LOC, - left_guard=reflected_left_guard, - function=reflected_agg_function, - elements=reflected_elements, - right_guard=reflected_right_guard, - ) + predicate = type(fact) + if pred_override_func := self._reflect_overrides["pred"].get(predicate): + return pred_override_func(fact) + node_constructor = self._node_constructor_from_pred(predicate) + + annotations = node_constructor.__annotations__ + kwargs_dict = {} + for key, child_type in annotations.items(): + # sign is a reserved field name in clorm, so we had use + # sign_ instead + if key == "sign": + field_val = getattr(fact, "sign_") + field = getattr(predicate, "sign_").meta.field + else: + field_val = getattr(fact, key, None) + field = ( + getattr(predicate, key).meta.field + if field_val is not None + else None + ) + if key == "return": + continue + if ( + pred_attr_overrides := self._reflect_overrides["pred_attr"].get( + predicate + ) + ) and (attr_override_func := pred_attr_overrides.get(key)): + kwargs_dict.update({key: attr_override_func(fact)}) + elif attr_override_func := self._reflect_overrides["attr"].get(key): + kwargs_dict.update({key: attr_override_func(fact)}) + elif clorm_enum := getattr(field, "enum", None): + ast_enum = getattr(ast, clorm_enum.__name__) + ast_enum_member = preds.convert_enum(field_val, ast_enum) + kwargs_dict.update({key: ast_enum_member}) + elif child_type in [str, int]: + kwargs_dict.update({key: field_val}) + elif child_type in [AST, Symbol]: + child_node = self._reflect_child(fact, field_val, "1") + kwargs_dict.update({key: child_node}) + elif child_type in [Optional[AST], Optional[Symbol]]: + optional_child_node = self._reflect_child(fact, field_val, "?") + kwargs_dict.update({key: optional_child_node}) + elif child_type in [Sequence[AST], Sequence[Symbol], Sequence[str]]: + child_nodes = self._reflect_child(fact, field_val, "*") + kwargs_dict.update({key: child_nodes}) + reflected_node = node_constructor(**kwargs_dict) + return reflected_node + + def _reflect_bool(self, boolean: Literal["true", "false"]): + return 1 if boolean == "true" else 0 - @reflect_predicate.register - def _reflect_conditional_literals( - self, cond_lits: preds.Conditional_Literals - ) -> AST: - return self._reflect_child(cond_lits, cond_lits.conditional_literal) - - @reflect_predicate.register - def _reflect_disjunction(self, disjunction: preds.Disjunction) -> AST: - reflected_elements = self._reflect_child(disjunction, disjunction.elements, "*") - return ast.Disjunction(location=DUMMY_LOC, elements=reflected_elements) - - @reflect_predicate.register - def _reflect_rule(self, rule: preds.Rule) -> AST: - """Reflect a Rule fact into a Rule node.""" - reflected_head = self._reflect_child(rule, rule.head) - reflected_body = self._reflect_child(rule, rule.body, "*") - return ast.Rule(location=DUMMY_LOC, head=reflected_head, body=reflected_body) - - @reflect_predicate.register - def _reflect_statements(self, statements: preds.Statements) -> AST: - return self._reflect_child(statements, statements.statement) - - @reflect_predicate.register - def _reflect_constants(self, constants: preds.Constants) -> AST: - return self._reflect_child(constants, constants.constant) - - @reflect_predicate.register def _reflect_program(self, program: preds.Program) -> Sequence[AST]: """Reflect a (sub)program fact into sequence of AST nodes, one node per each statement in the (sub)program. @@ -1162,22 +724,6 @@ def _reflect_program(self, program: preds.Program) -> Sequence[AST]: subprogram.extend(statement_nodes) return subprogram - @reflect_predicate.register - def _reflect_external(self, external: preds.External) -> AST: - """Reflect an External fact into an External node.""" - symb_atom_node = self._reflect_child(external, external.atom) - body_nodes = self._reflect_child(external, external.body, "*") - ext_type = ast.SymbolicTerm( - location=DUMMY_LOC, - symbol=symbol.Function(name=str(external.external_type), arguments=[]), - ) - return ast.External( - location=DUMMY_LOC, - atom=symb_atom_node, - body=body_nodes, - external_type=ext_type, - ) - def reflect(self): """Convert stored reified ast facts into a (sequence of) AST node(s), and it's string representation. @@ -1187,11 +733,8 @@ def reflect(self): self._program_ast = [] # should probably define an order in which programs are queried for prog in self._reified.query(preds.Program).all(): - subprogram = self.reflect_predicate(prog) + subprogram = self.reflect_fact(prog) self._program_ast.extend(subprogram) - self._program_string = "\n".join( - [str(statement) for statement in self._program_ast] - ) logger.debug("Reflected program string:\n%s", self.program_string) def transform( @@ -1234,7 +777,7 @@ def transform( model_iterator = iter(handle) model = next(model_iterator) ast_symbols = [final.arguments[0] for final in model.symbols(shown=True)] - unifier = Unifier(preds.AstPredicates) + unifier = Unifier(preds.AstPreds) with TryUnify(): ast_facts = unifier.iter_unify(ast_symbols, raise_nomatch=True) self._reified = FactBase(ast_facts) diff --git a/tests/asp/reify_reflect/malformed/multiple_in_same_pos.lp b/tests/asp/reify_reflect/malformed/multiple_in_same_pos.lp new file mode 100644 index 0000000..00024c4 --- /dev/null +++ b/tests/asp/reify_reflect/malformed/multiple_in_same_pos.lp @@ -0,0 +1,12 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,literal(4),body_literals(11)). +literal(4,"pos",comparison(5)). +comparison(5,number(6),guards(7)). +number(6,1). +guards(7,0,guard(8)). +guard(8,"<",number(9)). +number(9,2). +guards(7,0,guard(11)). +guard(11,"<",number(12)). +number(12,3). diff --git a/tests/asp/reify_reflect/malformed_ast/not_an_ast_fact.lp b/tests/asp/reify_reflect/malformed/not_an_ast_fact.lp similarity index 100% rename from tests/asp/reify_reflect/malformed_ast/not_an_ast_fact.lp rename to tests/asp/reify_reflect/malformed/not_an_ast_fact.lp diff --git a/tests/asp/reify_reflect/malformed/one_expected_multiple_found.lp b/tests/asp/reify_reflect/malformed/one_expected_multiple_found.lp new file mode 100644 index 0000000..a005569 --- /dev/null +++ b/tests/asp/reify_reflect/malformed/one_expected_multiple_found.lp @@ -0,0 +1,11 @@ +% Malformed set of ast facts with a missing child fact + +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,literal(4),body_literals(8)). +% facts representing head literal a. +literal(4,"pos",symbolic_atom(5)). +symbolic_atom(5,function(6)). +% should only have one child like function(6,_,_) +function(6,a,terms(7)). +function(6,b,terms(8)). diff --git a/tests/asp/reify_reflect/malformed/one_expected_zero_found.lp b/tests/asp/reify_reflect/malformed/one_expected_zero_found.lp new file mode 100644 index 0000000..4f5da1e --- /dev/null +++ b/tests/asp/reify_reflect/malformed/one_expected_zero_found.lp @@ -0,0 +1,8 @@ +% Malformed set of ast facts with a missing child fact + +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,literal(4),body_literals(8)). +% facts representing head literal a. +literal(4,"pos",symbolic_atom(5)). +symbolic_atom(5,function(6)). % child fact function(6,_,_) is missing but required diff --git a/tests/asp/reify_reflect/malformed/one_or_more_expected_found_zero.lp b/tests/asp/reify_reflect/malformed/one_or_more_expected_found_zero.lp new file mode 100644 index 0000000..47d587d --- /dev/null +++ b/tests/asp/reify_reflect/malformed/one_or_more_expected_found_zero.lp @@ -0,0 +1,6 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,literal(4),body_literals(11)). +literal(4,"pos",comparison(5)). +comparison(5,number(6),guards(7)). +number(6,1). diff --git a/tests/asp/reify_reflect/malformed/zero_or_more_expected_multiple_found.lp b/tests/asp/reify_reflect/malformed/zero_or_more_expected_multiple_found.lp new file mode 100644 index 0000000..b822923 --- /dev/null +++ b/tests/asp/reify_reflect/malformed/zero_or_more_expected_multiple_found.lp @@ -0,0 +1,10 @@ +% malformed ast with multiple aggregate guards. + +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,aggregate(4),body_literals(10)). +aggregate(4,guard(5),aggregate_elements(8),guard(9)). +guard(5,"<=",number(6)). +number(6,1). +guard(5,"<",number(7)). +number(7,3). diff --git a/tests/asp/reify_reflect/malformed_ast/multiple_in_same_pos.lp b/tests/asp/reify_reflect/malformed_ast/multiple_in_same_pos.lp deleted file mode 100644 index a49f1f4..0000000 --- a/tests/asp/reify_reflect/malformed_ast/multiple_in_same_pos.lp +++ /dev/null @@ -1,12 +0,0 @@ -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,literal(3),body_literals(10)). -literal(3,"pos",comparison(4)). -comparison(4,number(5),guards(6)). -number(5,1). -guards(6,0,guard(7)). -guard(7,"<",number(8)). -number(8,2). -guards(6,0,guard(10)). -guard(10,"<",number(11)). -number(11,3). diff --git a/tests/asp/reify_reflect/malformed_ast/one_expected_multiple_found.lp b/tests/asp/reify_reflect/malformed_ast/one_expected_multiple_found.lp deleted file mode 100644 index 43fd49e..0000000 --- a/tests/asp/reify_reflect/malformed_ast/one_expected_multiple_found.lp +++ /dev/null @@ -1,11 +0,0 @@ -% Malformed set of ast facts with a missing child fact - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,literal(3),body_literals(7)). -% facts representing head literal a. -literal(3,"pos",symbolic_atom(4)). -symbolic_atom(4,function(5)). -% should only have one child like function(5,_,_) -function(5,a,terms(6)). -function(5,b,terms(7)). diff --git a/tests/asp/reify_reflect/malformed_ast/one_expected_zero_found.lp b/tests/asp/reify_reflect/malformed_ast/one_expected_zero_found.lp deleted file mode 100644 index bc6e798..0000000 --- a/tests/asp/reify_reflect/malformed_ast/one_expected_zero_found.lp +++ /dev/null @@ -1,8 +0,0 @@ -% Malformed set of ast facts with a missing child fact - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,literal(3),body_literals(7)). -% facts representing head literal a. -literal(3,"pos",symbolic_atom(4)). -symbolic_atom(4,function(5)). % child fact function(5,_,_) is missing but required diff --git a/tests/asp/reify_reflect/malformed_ast/one_or_more_expected_found_zero.lp b/tests/asp/reify_reflect/malformed_ast/one_or_more_expected_found_zero.lp deleted file mode 100644 index ab009cf..0000000 --- a/tests/asp/reify_reflect/malformed_ast/one_or_more_expected_found_zero.lp +++ /dev/null @@ -1,6 +0,0 @@ -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,literal(3),body_literals(10)). -literal(3,"pos",comparison(4)). -comparison(4,number(5),guards(6)). -number(5,1). diff --git a/tests/asp/reify_reflect/malformed_ast/zero_or_more_expected_multiple_found.lp b/tests/asp/reify_reflect/malformed_ast/zero_or_more_expected_multiple_found.lp deleted file mode 100644 index d62203e..0000000 --- a/tests/asp/reify_reflect/malformed_ast/zero_or_more_expected_multiple_found.lp +++ /dev/null @@ -1,10 +0,0 @@ -% malformed ast with multiple aggregate guards. - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,aggregate(3),body_literals(9)). -aggregate(3,guard(4),agg_elements(7),guard(8)). -guard(4,"<=",number(5)). -number(5,1). -guard(4,"<",number(6)). -number(6,3). diff --git a/tests/asp/reify_reflect/reflected/aggregate.lp b/tests/asp/reify_reflect/reflected/aggregate.lp new file mode 100644 index 0000000..a8f56a7 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/aggregate.lp @@ -0,0 +1 @@ +1 <= { a: b; c }. diff --git a/tests/asp/reify_reflect/reflected/aggregate2.lp b/tests/asp/reify_reflect/reflected/aggregate2.lp new file mode 100644 index 0000000..8e74396 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/aggregate2.lp @@ -0,0 +1 @@ +1 <= { a } < 3. diff --git a/tests/asp/reify_reflect/reflected/atom.lp b/tests/asp/reify_reflect/reflected/atom.lp new file mode 100644 index 0000000..bbf1f8c --- /dev/null +++ b/tests/asp/reify_reflect/reflected/atom.lp @@ -0,0 +1 @@ +rel(2,1) :- rel(1,2). diff --git a/tests/asp/reify_reflect/reflected/binary_operation.lp b/tests/asp/reify_reflect/reflected/binary_operation.lp new file mode 100644 index 0000000..3f891d4 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/binary_operation.lp @@ -0,0 +1 @@ +equal((1+1),2). diff --git a/tests/asp/reify_reflect/reflected/body_aggregate.lp b/tests/asp/reify_reflect/reflected/body_aggregate.lp new file mode 100644 index 0000000..c2bdf1d --- /dev/null +++ b/tests/asp/reify_reflect/reflected/body_aggregate.lp @@ -0,0 +1 @@ +#false :- 4 != #sum+ { 1,2: not a; 3: b }. diff --git a/tests/asp/reify_reflect/reflected/bool_const.lp b/tests/asp/reify_reflect/reflected/bool_const.lp new file mode 100644 index 0000000..6d8fb1a --- /dev/null +++ b/tests/asp/reify_reflect/reflected/bool_const.lp @@ -0,0 +1,2 @@ +#false. +#true. diff --git a/tests/asp/reify_reflect/reflected/comparison.lp b/tests/asp/reify_reflect/reflected/comparison.lp new file mode 100644 index 0000000..5a705df --- /dev/null +++ b/tests/asp/reify_reflect/reflected/comparison.lp @@ -0,0 +1 @@ +1 < 2 != 3 > 4. diff --git a/tests/asp/reify_reflect/reflected/conditional_literal.lp b/tests/asp/reify_reflect/reflected/conditional_literal.lp new file mode 100644 index 0000000..2053dd6 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/conditional_literal.lp @@ -0,0 +1 @@ +a :- b: c, d. diff --git a/tests/asp/reify_reflect/reflected/constant_term.lp b/tests/asp/reify_reflect/reflected/constant_term.lp new file mode 100644 index 0000000..aee46fb --- /dev/null +++ b/tests/asp/reify_reflect/reflected/constant_term.lp @@ -0,0 +1 @@ +good(human). diff --git a/tests/asp/reify_reflect/reflected/defined.lp b/tests/asp/reify_reflect/reflected/defined.lp new file mode 100644 index 0000000..3545941 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/defined.lp @@ -0,0 +1 @@ +#defined a/3. diff --git a/tests/asp/reify_reflect/reflected/definition.lp b/tests/asp/reify_reflect/reflected/definition.lp new file mode 100644 index 0000000..27211fd --- /dev/null +++ b/tests/asp/reify_reflect/reflected/definition.lp @@ -0,0 +1,2 @@ +#const x = 42. +#const y = a. [override] diff --git a/tests/asp/reify_reflect/reflected/disjunction.lp b/tests/asp/reify_reflect/reflected/disjunction.lp new file mode 100644 index 0000000..3ced798 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/disjunction.lp @@ -0,0 +1 @@ +a; b: c, not d. diff --git a/tests/asp/reify_reflect/reflected/edge.lp b/tests/asp/reify_reflect/reflected/edge.lp new file mode 100644 index 0000000..5758efb --- /dev/null +++ b/tests/asp/reify_reflect/reflected/edge.lp @@ -0,0 +1 @@ +#edge (a,b) : c. diff --git a/tests/asp/reify_reflect/reflected/external.lp b/tests/asp/reify_reflect/reflected/external.lp new file mode 100644 index 0000000..1a4d1c0 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/external.lp @@ -0,0 +1 @@ +#external a(X) : c(X); d(e(X)). [false] diff --git a/tests/asp/reify_reflect/reflected/external_function.lp b/tests/asp/reify_reflect/reflected/external_function.lp new file mode 100644 index 0000000..5729452 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/external_function.lp @@ -0,0 +1 @@ +a(@f(b)). diff --git a/tests/asp/reify_reflect/reflected/function.lp b/tests/asp/reify_reflect/reflected/function.lp new file mode 100644 index 0000000..f795ed2 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/function.lp @@ -0,0 +1 @@ +next(move(a)). diff --git a/tests/asp/reify_reflect/reflected/head_aggregate.lp b/tests/asp/reify_reflect/reflected/head_aggregate.lp new file mode 100644 index 0000000..d576ab4 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/head_aggregate.lp @@ -0,0 +1 @@ +4 = #sum { 1,2: a: not b; 3: c }. diff --git a/tests/asp/reify_reflect/reflected/heuristic.lp b/tests/asp/reify_reflect/reflected/heuristic.lp new file mode 100644 index 0000000..92be2c7 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/heuristic.lp @@ -0,0 +1 @@ +#heuristic a : b; c. [1@2,sign] diff --git a/tests/asp/reify_reflect/reflected/integrity_constraint.lp b/tests/asp/reify_reflect/reflected/integrity_constraint.lp new file mode 100644 index 0000000..ae72429 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/integrity_constraint.lp @@ -0,0 +1 @@ +:- a. diff --git a/tests/asp/reify_reflect/reflected/interval.lp b/tests/asp/reify_reflect/reflected/interval.lp new file mode 100644 index 0000000..3c70cbe --- /dev/null +++ b/tests/asp/reify_reflect/reflected/interval.lp @@ -0,0 +1 @@ +a((1..3)). diff --git a/tests/asp/reify_reflect/reflected/minimize.lp b/tests/asp/reify_reflect/reflected/minimize.lp new file mode 100644 index 0000000..bce34af --- /dev/null +++ b/tests/asp/reify_reflect/reflected/minimize.lp @@ -0,0 +1 @@ +:~ b. [1@0,"a",3] diff --git a/tests/asp/reify_reflect/reflected/pool.lp b/tests/asp/reify_reflect/reflected/pool.lp new file mode 100644 index 0000000..8e54586 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/pool.lp @@ -0,0 +1 @@ +pool((1;a)). diff --git a/tests/asp/reify_reflect/reflected/program_acid.lp b/tests/asp/reify_reflect/reflected/program_acid.lp new file mode 100644 index 0000000..d200928 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/program_acid.lp @@ -0,0 +1 @@ +#program acid(k). diff --git a/tests/asp/reify_reflect/reflected/project_atom.lp b/tests/asp/reify_reflect/reflected/project_atom.lp new file mode 100644 index 0000000..323f942 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/project_atom.lp @@ -0,0 +1 @@ +#project a : b. diff --git a/tests/asp/reify_reflect/reflected/project_signature.lp b/tests/asp/reify_reflect/reflected/project_signature.lp new file mode 100644 index 0000000..9ef6c52 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/project_signature.lp @@ -0,0 +1 @@ +#project a/1. diff --git a/tests/asp/reify_reflect/reflected/prop_fact.lp b/tests/asp/reify_reflect/reflected/prop_fact.lp new file mode 100644 index 0000000..2c88a88 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/prop_fact.lp @@ -0,0 +1 @@ +a. diff --git a/tests/asp/reify_reflect/reflected/prop_normal_rule.lp b/tests/asp/reify_reflect/reflected/prop_normal_rule.lp new file mode 100644 index 0000000..1449f62 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/prop_normal_rule.lp @@ -0,0 +1 @@ +a :- b; not c. diff --git a/tests/asp/reify_reflect/reflected/script.lp b/tests/asp/reify_reflect/reflected/script.lp new file mode 100644 index 0000000..1cc957e --- /dev/null +++ b/tests/asp/reify_reflect/reflected/script.lp @@ -0,0 +1,9 @@ +#script (python) +from clingo . symbol import Number + +def divisors (a): + a = a.number + for i in range (1 , a +1): + if a % i == 0: + yield Number(i) +#end. diff --git a/tests/asp/reify_reflect/reflected/show_signature.lp b/tests/asp/reify_reflect/reflected/show_signature.lp new file mode 100644 index 0000000..62035ac --- /dev/null +++ b/tests/asp/reify_reflect/reflected/show_signature.lp @@ -0,0 +1,2 @@ +#show a/1. +#show -b/0. diff --git a/tests/asp/reify_reflect/reflected/show_term.lp b/tests/asp/reify_reflect/reflected/show_term.lp new file mode 100644 index 0000000..f367388 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/show_term.lp @@ -0,0 +1 @@ +#show 1 : b. diff --git a/tests/asp/reify_reflect/reflected/string.lp b/tests/asp/reify_reflect/reflected/string.lp new file mode 100644 index 0000000..4d7f8ec --- /dev/null +++ b/tests/asp/reify_reflect/reflected/string.lp @@ -0,0 +1 @@ +yummy("carrot"). diff --git a/tests/asp/reify_reflect/reflected/theory_atom_simple.lp b/tests/asp/reify_reflect/reflected/theory_atom_simple.lp new file mode 100644 index 0000000..1b4b699 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/theory_atom_simple.lp @@ -0,0 +1 @@ +&a { }. diff --git a/tests/asp/reify_reflect/reflected/theory_atom_simple_arg.lp b/tests/asp/reify_reflect/reflected/theory_atom_simple_arg.lp new file mode 100644 index 0000000..009136a --- /dev/null +++ b/tests/asp/reify_reflect/reflected/theory_atom_simple_arg.lp @@ -0,0 +1 @@ +&a("b") { }. diff --git a/tests/asp/reify_reflect/reflected/theory_atom_simple_guard.lp b/tests/asp/reify_reflect/reflected/theory_atom_simple_guard.lp new file mode 100644 index 0000000..f014c41 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/theory_atom_simple_guard.lp @@ -0,0 +1 @@ +&a { } | b. diff --git a/tests/asp/reify_reflect/reflected/theory_definition.lp b/tests/asp/reify_reflect/reflected/theory_definition.lp new file mode 100644 index 0000000..52b6b96 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/theory_definition.lp @@ -0,0 +1,17 @@ +#theory dc { + constant { + - : 0, unary + }; + diff_term { + - : 0, binary, left + }; + domain_term { + .. : 1, binary, left + }; + show_term { + / : 1, binary, left + }; + &dom/0: domain_term, { = }, diff_term, any; + &diff/0: diff_term, { <= }, constant, any; + &show/0: show_term, directive +}. diff --git a/tests/asp/reify_reflect/reflected/theory_function.lp b/tests/asp/reify_reflect/reflected/theory_function.lp new file mode 100644 index 0000000..60abdce --- /dev/null +++ b/tests/asp/reify_reflect/reflected/theory_function.lp @@ -0,0 +1 @@ +&a { f(1) }. diff --git a/tests/asp/reify_reflect/reflected/theory_sequence.lp b/tests/asp/reify_reflect/reflected/theory_sequence.lp new file mode 100644 index 0000000..ce59ff6 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/theory_sequence.lp @@ -0,0 +1 @@ +&a { [1,b] }. diff --git a/tests/asp/reify_reflect/reflected/theory_unparsed_term.lp b/tests/asp/reify_reflect/reflected/theory_unparsed_term.lp new file mode 100644 index 0000000..e756d2e --- /dev/null +++ b/tests/asp/reify_reflect/reflected/theory_unparsed_term.lp @@ -0,0 +1 @@ +&a { (+ 1 !- > "b") }. diff --git a/tests/asp/reify_reflect/reflected/unary_operation.lp b/tests/asp/reify_reflect/reflected/unary_operation.lp new file mode 100644 index 0000000..09db51b --- /dev/null +++ b/tests/asp/reify_reflect/reflected/unary_operation.lp @@ -0,0 +1 @@ +neg(-1). diff --git a/tests/asp/reify_reflect/reflected/variable.lp b/tests/asp/reify_reflect/reflected/variable.lp new file mode 100644 index 0000000..bb499f3 --- /dev/null +++ b/tests/asp/reify_reflect/reflected/variable.lp @@ -0,0 +1 @@ +rel(Y,X) :- rel(X,Y). diff --git a/tests/asp/reify_reflect/reified/aggregate.lp b/tests/asp/reify_reflect/reified/aggregate.lp new file mode 100644 index 0000000..2daf0f0 --- /dev/null +++ b/tests/asp/reify_reflect/reified/aggregate.lp @@ -0,0 +1,20 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,aggregate(4),body_literals(25)). +aggregate(4,guard(5),aggregate_elements(7),guard(24)). +guard(5,"<=",number(6)). +number(6,1). +aggregate_elements(7,0,conditional_literal(8)). +conditional_literal(8,literal(9),literals(13)). +literal(9,"pos",symbolic_atom(10)). +symbolic_atom(10,function(11)). +function(11,a,terms(12)). +literals(13,0,literal(14)). +literal(14,"pos",symbolic_atom(15)). +symbolic_atom(15,function(16)). +function(16,b,terms(17)). +aggregate_elements(7,1,conditional_literal(18)). +conditional_literal(18,literal(19),literals(23)). +literal(19,"pos",symbolic_atom(20)). +symbolic_atom(20,function(21)). +function(21,c,terms(22)). diff --git a/tests/asp/reify_reflect/reified/aggregate2.lp b/tests/asp/reify_reflect/reified/aggregate2.lp new file mode 100644 index 0000000..8393ba4 --- /dev/null +++ b/tests/asp/reify_reflect/reified/aggregate2.lp @@ -0,0 +1,13 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,aggregate(4),body_literals(16)). +aggregate(4,guard(5),aggregate_elements(7),guard(14)). +guard(5,"<=",number(6)). +number(6,1). +aggregate_elements(7,0,conditional_literal(8)). +conditional_literal(8,literal(9),literals(13)). +literal(9,"pos",symbolic_atom(10)). +symbolic_atom(10,function(11)). +function(11,a,terms(12)). +guard(14,"<",number(15)). +number(15,3). diff --git a/tests/asp/reify_reflect/malformed_ast/ast_fact.lp b/tests/asp/reify_reflect/reified/ast_fact.lp similarity index 100% rename from tests/asp/reify_reflect/malformed_ast/ast_fact.lp rename to tests/asp/reify_reflect/reified/ast_fact.lp diff --git a/tests/asp/reify_reflect/reified/atom.lp b/tests/asp/reify_reflect/reified/atom.lp new file mode 100644 index 0000000..6c9be2f --- /dev/null +++ b/tests/asp/reify_reflect/reified/atom.lp @@ -0,0 +1,20 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,literal(4),body_literals(10)). +% facts representing head literal rel(3,1). +literal(4,"pos",symbolic_atom(5)). +symbolic_atom(5,function(6)). +function(6,rel,terms(7)). +terms(7,0,number(8)). +number(8,2). +terms(7,1,number(9)). +number(9,1). +% facts representing body literal rel(2,2). +body_literals(10,0,body_literal(11)). +body_literal(11,"pos",symbolic_atom(12)). +symbolic_atom(12,function(13)). +function(13,rel,terms(14)). +terms(14,0,number(15)). +number(15,1). +terms(14,1,number(16)). +number(16,2). diff --git a/tests/asp/reify_reflect/reified/binary_operation.lp b/tests/asp/reify_reflect/reified/binary_operation.lp new file mode 100644 index 0000000..2a331b8 --- /dev/null +++ b/tests/asp/reify_reflect/reified/binary_operation.lp @@ -0,0 +1,14 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,literal(4),body_literals(12)). +% facts representing head literal equal((2+1),2). +literal(4,"pos",symbolic_atom(5)). +symbolic_atom(5,function(6)). +function(6,equal,terms(7)). +% facts representing the binary operation (2+1). +terms(7,0,binary_operation(8)). +binary_operation(8,"+",number(9),number(10)). +number(9,1). +number(10,1). +terms(7,1,number(11)). +number(11,2). diff --git a/tests/asp/reify_reflect/reified/body_aggregate.lp b/tests/asp/reify_reflect/reified/body_aggregate.lp new file mode 100644 index 0000000..83282b0 --- /dev/null +++ b/tests/asp/reify_reflect/reified/body_aggregate.lp @@ -0,0 +1,26 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,literal(4),body_literals(6)). +literal(4,"pos",boolean_constant(5)). +boolean_constant(5,false). +body_literals(6,0,body_literal(7)). +body_literal(7,"pos",body_aggregate(8)). +body_aggregate(8,guard(9),"#sum+",body_aggregate_elements(11),guard(27)). +guard(9,"!=",number(10)). +number(10,4). +body_aggregate_elements(11,0,terms(12),literals(15)). +terms(12,0,number(13)). +number(13,1). +terms(12,1,number(14)). +number(14,2). +literals(15,0,literal(16)). +literal(16,"not",symbolic_atom(17)). +symbolic_atom(17,function(18)). +function(18,a,terms(19)). +body_aggregate_elements(11,1,terms(20),literals(22)). +terms(20,0,number(21)). +number(21,3). +literals(22,0,literal(23)). +literal(23,"pos",symbolic_atom(24)). +symbolic_atom(24,function(25)). +function(25,b,terms(26)). diff --git a/tests/asp/reify_reflect/reified/bool_const.lp b/tests/asp/reify_reflect/reified/bool_const.lp new file mode 100644 index 0000000..19fa317 --- /dev/null +++ b/tests/asp/reify_reflect/reified/bool_const.lp @@ -0,0 +1,10 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,literal(4),body_literals(6)). +% facts representing head literal a. +literal(4,"pos",boolean_constant(5)). +boolean_constant(5,false). +statements(2,1,rule(7)). +rule(7,literal(8),body_literals(10)). +literal(8,"pos",boolean_constant(9)). +boolean_constant(9,true). diff --git a/tests/asp/reify_reflect/reified/comparison.lp b/tests/asp/reify_reflect/reified/comparison.lp new file mode 100644 index 0000000..8abe63c --- /dev/null +++ b/tests/asp/reify_reflect/reified/comparison.lp @@ -0,0 +1,15 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,literal(4),body_literals(14)). +literal(4,"pos",comparison(5)). +comparison(5,number(6),guards(7)). +number(6,1). +guards(7,0,guard(8)). +guard(8,"<",number(9)). +number(9,2). +guards(7,1,guard(10)). +guard(10,"!=",number(11)). +number(11,3). +guards(7,2,guard(12)). +guard(12,">", number(13)). +number(13,4). diff --git a/tests/asp/reify_reflect/reified/conditional_literal.lp b/tests/asp/reify_reflect/reified/conditional_literal.lp new file mode 100644 index 0000000..8f61cd1 --- /dev/null +++ b/tests/asp/reify_reflect/reified/conditional_literal.lp @@ -0,0 +1,21 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,literal(4),body_literals(8)). +% facts representing head literal a. +literal(4,"pos",symbolic_atom(5)). +symbolic_atom(5,function(6)). +function(6,a,terms(7)). +% facts representing conditional literal b : c, d. +body_literals(8,0,conditional_literal(9)). +conditional_literal(9,literal(10),literals(14)). +literal(10,"pos",symbolic_atom(11)). +symbolic_atom(11,function(12)). +function(12,b,terms(13)). +literals(14,0,literal(15)). +literal(15,"pos",symbolic_atom(16)). +symbolic_atom(16,function(17)). +function(17,c,terms(18)). +literals(14,1,literal(19)). +literal(19,"pos",symbolic_atom(20)). +symbolic_atom(20,function(21)). +function(21,d,terms(22)). diff --git a/tests/asp/reify_reflect/reified/constant_term.lp b/tests/asp/reify_reflect/reified/constant_term.lp new file mode 100644 index 0000000..6ac235c --- /dev/null +++ b/tests/asp/reify_reflect/reified/constant_term.lp @@ -0,0 +1,9 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,literal(4),body_literals(10)). +% facts representing head literal yummy("carrot"). +literal(4,"pos",symbolic_atom(5)). +symbolic_atom(5,function(6)). +function(6,good,terms(7)). +terms(7,0,function(8)). +function(8,human,terms(9)). diff --git a/tests/asp/reify_reflect/reified/defined.lp b/tests/asp/reify_reflect/reified/defined.lp new file mode 100644 index 0000000..ca0ad15 --- /dev/null +++ b/tests/asp/reify_reflect/reified/defined.lp @@ -0,0 +1,3 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,defined(3)). +defined(3,a,3,true). diff --git a/tests/asp/reify_reflect/reified/definition.lp b/tests/asp/reify_reflect/reified/definition.lp new file mode 100644 index 0000000..ef42ef1 --- /dev/null +++ b/tests/asp/reify_reflect/reified/definition.lp @@ -0,0 +1,7 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,definition(3)). +definition(3,x,number(4),true). +number(4,42). +statements(2,1,definition(5)). +definition(5,y,function(6),false). +function(6,a,terms(7)). diff --git a/tests/asp/reify_reflect/reified/disjunction.lp b/tests/asp/reify_reflect/reified/disjunction.lp new file mode 100644 index 0000000..d47f97f --- /dev/null +++ b/tests/asp/reify_reflect/reified/disjunction.lp @@ -0,0 +1,22 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,disjunction(4),body_literals(26)). +disjunction(4,conditional_literals(5)). +conditional_literals(5,0,conditional_literal(6)). +conditional_literal(6,literal(7),literals(11)). +literal(7,"pos",symbolic_atom(8)). +symbolic_atom(8,function(9)). +function(9,a,terms(10)). +conditional_literals(5,1,conditional_literal(12)). +conditional_literal(12,literal(13),literals(17)). +literal(13,"pos",symbolic_atom(14)). +symbolic_atom(14,function(15)). +function(15,b,terms(16)). +literals(17,0,literal(18)). +literal(18,"pos",symbolic_atom(19)). +symbolic_atom(19,function(20)). +function(20,c,terms(21)). +literals(17,1,literal(22)). +literal(22,"not",symbolic_atom(23)). +symbolic_atom(23,function(24)). +function(24,d,terms(25)). diff --git a/tests/asp/reify_reflect/reified/edge.lp b/tests/asp/reify_reflect/reified/edge.lp new file mode 100644 index 0000000..0769b0d --- /dev/null +++ b/tests/asp/reify_reflect/reified/edge.lp @@ -0,0 +1,9 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,edge(3)). +edge(3,function(4),function(6),body_literals(8)). +function(4,a,terms(5)). +function(6,b,terms(7)). +body_literals(8,0,body_literal(9)). +body_literal(9,"pos",symbolic_atom(10)). +symbolic_atom(10,function(11)). +function(11,c,terms(12)). diff --git a/tests/asp/reify_reflect/reified/external.lp b/tests/asp/reify_reflect/reified/external.lp new file mode 100644 index 0000000..b595116 --- /dev/null +++ b/tests/asp/reify_reflect/reified/external.lp @@ -0,0 +1,21 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,external(3)). +external(3,symbolic_atom(4),body_literals(8),false). +symbolic_atom(4,function(5)). +function(5,a,terms(6)). +terms(6,0,variable(7)). +variable(7,"X"). +body_literals(8,0,body_literal(9)). +body_literal(9,"pos",symbolic_atom(10)). +symbolic_atom(10,function(11)). +function(11,c,terms(12)). +terms(12,0,variable(13)). +variable(13,"X"). +body_literals(8,1,body_literal(14)). +body_literal(14,"pos",symbolic_atom(15)). +symbolic_atom(15,function(16)). +function(16,d,terms(17)). +terms(17,0,function(18)). +function(18,e,terms(19)). +terms(19,0,variable(20)). +variable(20,"X"). diff --git a/tests/asp/reify_reflect/reified/external_function.lp b/tests/asp/reify_reflect/reified/external_function.lp new file mode 100644 index 0000000..3198156 --- /dev/null +++ b/tests/asp/reify_reflect/reified/external_function.lp @@ -0,0 +1,10 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,literal(4),body_literals(12)). +literal(4,"pos",symbolic_atom(5)). +symbolic_atom(5,function(6)). +function(6,a,terms(7)). +terms(7,0,external_function(8)). +external_function(8,f,terms(9)). +terms(9,0,function(10)). +function(10,b,terms(11)). diff --git a/tests/asp/reify_reflect/reified/function.lp b/tests/asp/reify_reflect/reified/function.lp new file mode 100644 index 0000000..91a208a --- /dev/null +++ b/tests/asp/reify_reflect/reified/function.lp @@ -0,0 +1,10 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,literal(4),body_literals(12)). +literal(4,"pos",symbolic_atom(5)). +symbolic_atom(5,function(6)). +function(6,next,terms(7)). +terms(7,0,function(8)). +function(8,move,terms(9)). +terms(9,0,function(10)). +function(10,a,terms(11)). diff --git a/tests/asp/reify_reflect/reified/head_aggregate.lp b/tests/asp/reify_reflect/reified/head_aggregate.lp new file mode 100644 index 0000000..478e6a0 --- /dev/null +++ b/tests/asp/reify_reflect/reified/head_aggregate.lp @@ -0,0 +1,26 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,head_aggregate(4),body_literals(30)). +head_aggregate(4,guard(5),"#sum",head_aggregate_elements(7),guard(29)). +guard(5,"=",number(6)). +number(6,4). +head_aggregate_elements(7,0,terms(8),conditional_literal(11)). +terms(8,0,number(9)). +number(9,1). +terms(8,1,number(10)). +number(10,2). +conditional_literal(11,literal(12),literals(16)). +literal(12,"pos",symbolic_atom(13)). +symbolic_atom(13,function(14)). +function(14,a,terms(15)). +literals(16,0,literal(17)). +literal(17,"not",symbolic_atom(18)). +symbolic_atom(18,function(19)). +function(19,b,terms(20)). +head_aggregate_elements(7,1,terms(21),conditional_literal(23)). +terms(21,0,number(22)). +number(22,3). +conditional_literal(23,literal(24),literals(28)). +literal(24,"pos",symbolic_atom(25)). +symbolic_atom(25,function(26)). +function(26,c,terms(27)). diff --git a/tests/asp/reify_reflect/reified/heuristic.lp b/tests/asp/reify_reflect/reified/heuristic.lp new file mode 100644 index 0000000..4afacd5 --- /dev/null +++ b/tests/asp/reify_reflect/reified/heuristic.lp @@ -0,0 +1,16 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,heuristic(3)). +heuristic(3,symbolic_atom(4),body_literals(7),number(16),number(17),function(18)). +symbolic_atom(4,function(5)). +function(5,a,terms(6)). +body_literals(7,0,body_literal(8)). +body_literal(8,"pos",symbolic_atom(9)). +symbolic_atom(9,function(10)). +function(10,b,terms(11)). +body_literals(7,1,body_literal(12)). +body_literal(12,"pos",symbolic_atom(13)). +symbolic_atom(13,function(14)). +function(14,c,terms(15)). +number(16,1). +number(17,2). +function(18,sign,terms(19)). diff --git a/tests/asp/reify_reflect/reified/integrity_constraint.lp b/tests/asp/reify_reflect/reified/integrity_constraint.lp new file mode 100644 index 0000000..8121a57 --- /dev/null +++ b/tests/asp/reify_reflect/reified/integrity_constraint.lp @@ -0,0 +1,9 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,literal(4),body_literals(6)). +literal(4,"pos",boolean_constant(5)). +boolean_constant(5,false). +body_literals(6,0,body_literal(7)). +body_literal(7,"pos",symbolic_atom(8)). +symbolic_atom(8,function(9)). +function(9,a,terms(10)). diff --git a/tests/asp/reify_reflect/reified/interval.lp b/tests/asp/reify_reflect/reified/interval.lp new file mode 100644 index 0000000..751d722 --- /dev/null +++ b/tests/asp/reify_reflect/reified/interval.lp @@ -0,0 +1,10 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,literal(4),body_literals(11)). +literal(4,"pos",symbolic_atom(5)). +symbolic_atom(5,function(6)). +function(6,a,terms(7)). +terms(7,0,interval(8)). +interval(8,number(9),number(10)). +number(9,1). +number(10,3). diff --git a/tests/asp/reify_reflect/reified/minimize.lp b/tests/asp/reify_reflect/reified/minimize.lp new file mode 100644 index 0000000..aa64e3c --- /dev/null +++ b/tests/asp/reify_reflect/reified/minimize.lp @@ -0,0 +1,13 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,minimize(3)). +minimize(3,number(4),number(5),terms(6),body_literals(9)). +number(4,1). +number(5,0). +terms(6,0,string(7)). +string(7,"a"). +terms(6,1,number(8)). +number(8,3). +body_literals(9,0,body_literal(10)). +body_literal(10,"pos",symbolic_atom(11)). +symbolic_atom(11,function(12)). +function(12,b,terms(13)). diff --git a/tests/asp/reify_reflect/reified/pool.lp b/tests/asp/reify_reflect/reified/pool.lp new file mode 100644 index 0000000..28ec816 --- /dev/null +++ b/tests/asp/reify_reflect/reified/pool.lp @@ -0,0 +1,13 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,literal(4),body_literals(13)). +% facts representing head literal pool(2;a). +literal(4,"pos",symbolic_atom(5)). +symbolic_atom(5,function(6)). +function(6,pool,terms(7)). +terms(7,0,pool(8)). +pool(8,terms(9)). +terms(9,0,number(10)). +number(10,1). +terms(9,1,function(11)). +function(11,a,terms(12)). diff --git a/tests/asp/reify_reflect/reified/program_acid.lp b/tests/asp/reify_reflect/reified/program_acid.lp new file mode 100644 index 0000000..d15947e --- /dev/null +++ b/tests/asp/reify_reflect/reified/program_acid.lp @@ -0,0 +1,4 @@ +program(0,"base",constants(1),statements(2)). +program(3,"acid",constants(4),statements(7)). +constants(4,0,function(5)). +function(5,k,terms(6)). diff --git a/tests/asp/reify_reflect/reified/project_atom.lp b/tests/asp/reify_reflect/reified/project_atom.lp new file mode 100644 index 0000000..2704e9d --- /dev/null +++ b/tests/asp/reify_reflect/reified/project_atom.lp @@ -0,0 +1,9 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,project_atom(3)). +project_atom(3,symbolic_atom(4),body_literals(7)). +symbolic_atom(4,function(5)). +function(5,a,terms(6)). +body_literals(7,0,body_literal(8)). +body_literal(8,"pos",symbolic_atom(9)). +symbolic_atom(9,function(10)). +function(10,b,terms(11)). diff --git a/tests/asp/reify_reflect/reified/project_signature.lp b/tests/asp/reify_reflect/reified/project_signature.lp new file mode 100644 index 0000000..dc67b15 --- /dev/null +++ b/tests/asp/reify_reflect/reified/project_signature.lp @@ -0,0 +1,3 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,project_signature(3)). +project_signature(3,a,1,true). diff --git a/tests/asp/reify_reflect/reified/prop_fact.lp b/tests/asp/reify_reflect/reified/prop_fact.lp new file mode 100644 index 0000000..43961a8 --- /dev/null +++ b/tests/asp/reify_reflect/reified/prop_fact.lp @@ -0,0 +1,7 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,literal(4),body_literals(8)). +% facts representing head literal a. +literal(4,"pos",symbolic_atom(5)). +symbolic_atom(5,function(6)). +function(6,a,terms(7)). diff --git a/tests/asp/reify_reflect/reified/prop_normal_rule.lp b/tests/asp/reify_reflect/reified/prop_normal_rule.lp new file mode 100644 index 0000000..cb5bdbc --- /dev/null +++ b/tests/asp/reify_reflect/reified/prop_normal_rule.lp @@ -0,0 +1,17 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,literal(4),body_literals(8)). +% facts representing head literal a. +literal(4,"pos",symbolic_atom(5)). +symbolic_atom(5,function(6)). +function(6,a,terms(7)). +% facts representing body literal b. +body_literals(8,0,body_literal(9)). +body_literal(9,"pos",symbolic_atom(10)). +symbolic_atom(10,function(11)). +function(11,b,terms(12)). +% facts representing body literal not c. +body_literals(8,1,body_literal(13)). +body_literal(13,"not",symbolic_atom(14)). +symbolic_atom(14,function(15)). +function(15,c,terms(16)). diff --git a/tests/asp/reify_reflect/reified/script.lp b/tests/asp/reify_reflect/reified/script.lp new file mode 100644 index 0000000..2aeb377 --- /dev/null +++ b/tests/asp/reify_reflect/reified/script.lp @@ -0,0 +1,3 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,script(3)). +script(3,python,"\nfrom clingo . symbol import Number\n\ndef divisors (a):\n a = a.number\n for i in range (1 , a +1):\n if a % i == 0:\n yield Number(i)\n"). diff --git a/tests/asp/reify_reflect/reified/show_signature.lp b/tests/asp/reify_reflect/reified/show_signature.lp new file mode 100644 index 0000000..feecaf6 --- /dev/null +++ b/tests/asp/reify_reflect/reified/show_signature.lp @@ -0,0 +1,5 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,show_signature(3)). +show_signature(3,a,1,true). +statements(2,1,show_signature(4)). +show_signature(4,b,0,false). diff --git a/tests/asp/reify_reflect/reified/show_term.lp b/tests/asp/reify_reflect/reified/show_term.lp new file mode 100644 index 0000000..3f24e30 --- /dev/null +++ b/tests/asp/reify_reflect/reified/show_term.lp @@ -0,0 +1,8 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,show_term(3)). +show_term(3,number(4),body_literals(5)). +number(4,1). +body_literals(5,0,body_literal(6)). +body_literal(6,"pos",symbolic_atom(7)). +symbolic_atom(7,function(8)). +function(8,b,terms(9)). diff --git a/tests/asp/reify_reflect/reified/string.lp b/tests/asp/reify_reflect/reified/string.lp new file mode 100644 index 0000000..77326ff --- /dev/null +++ b/tests/asp/reify_reflect/reified/string.lp @@ -0,0 +1,9 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,literal(4),body_literals(9)). +% facts representing head literal yummy("carrot"). +literal(4,"pos",symbolic_atom(5)). +symbolic_atom(5,function(6)). +function(6,yummy,terms(7)). +terms(7,0,string(8)). +string(8,"carrot"). diff --git a/tests/asp/reify_reflect/reified/theory_atom_simple.lp b/tests/asp/reify_reflect/reified/theory_atom_simple.lp new file mode 100644 index 0000000..e2c79bd --- /dev/null +++ b/tests/asp/reify_reflect/reified/theory_atom_simple.lp @@ -0,0 +1,5 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,theory_atom(4),body_literals(9)). +theory_atom(4,function(5),theory_atom_elements(7),theory_guard(8)). +function(5,a,terms(6)). diff --git a/tests/asp/reify_reflect/reified/theory_atom_simple_arg.lp b/tests/asp/reify_reflect/reified/theory_atom_simple_arg.lp new file mode 100644 index 0000000..86d4c00 --- /dev/null +++ b/tests/asp/reify_reflect/reified/theory_atom_simple_arg.lp @@ -0,0 +1,7 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,theory_atom(4),body_literals(10)). +theory_atom(4,function(5),theory_atom_elements(8),theory_guard(9)). +function(5,a,terms(6)). +terms(6,0,string(7)). +string(7,"b"). diff --git a/tests/asp/reify_reflect/reified/theory_atom_simple_guard.lp b/tests/asp/reify_reflect/reified/theory_atom_simple_guard.lp new file mode 100644 index 0000000..7ceabe2 --- /dev/null +++ b/tests/asp/reify_reflect/reified/theory_atom_simple_guard.lp @@ -0,0 +1,7 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,theory_atom(4),body_literals(11)). +theory_atom(4,function(5),theory_atom_elements(7),theory_guard(8)). +function(5,a,terms(6)). +theory_guard(8,"|",function(9)). +function(9,b,terms(10)). diff --git a/tests/asp/reify_reflect/reified/theory_definition.lp b/tests/asp/reify_reflect/reified/theory_definition.lp new file mode 100644 index 0000000..b990e77 --- /dev/null +++ b/tests/asp/reify_reflect/reified/theory_definition.lp @@ -0,0 +1,27 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,theory_definition(3)). +% #theory dc { +theory_definition(3,dc,theory_term_definitions(4),theory_atom_definitions(9)). +% constant { - : 0 , unary }; +theory_term_definitions(4,0,constant,theory_operator_definitions(5)). +theory_operator_definitions(5,0,"-",0,unary). +% diff_term { - : 0 , binary, left }; +theory_term_definitions(4,1,diff_term,theory_operator_definitions(6)). +theory_operator_definitions(6,0,"-",0,binary_left). +% domain_term { .. : 1 , binary, left }; +theory_term_definitions(4,2,domain_term,theory_operator_definitions(7)). +theory_operator_definitions(7,0,"..",1,binary_left). +% show_term { / : 1 , binary, left }; +theory_term_definitions(4,3,show_term,theory_operator_definitions(8)). +theory_operator_definitions(8,0,"/",1,binary_left). +% &dom/0 : domain_term, {=} , diff_term, any; +theory_atom_definitions(9,0,any,dom,0,domain_term,theory_guard_definition(10)). +theory_guard_definition(10,theory_operators(11),diff_term). +theory_operators(11,0,"="). +% &diff/0 : diff_term, { <=} , constant, any; +theory_atom_definitions(9,1,any,diff,0,diff_term,theory_guard_definition(12)). +theory_guard_definition(12,theory_operators(13),constant). +theory_operators(13,0,"<="). +% &show/0 : show_term, directive +theory_atom_definitions(9,2,directive,show,0,show_term,theory_guard_definition(14)). +% }. diff --git a/tests/asp/reify_reflect/reified/theory_function.lp b/tests/asp/reify_reflect/reified/theory_function.lp new file mode 100644 index 0000000..92732c0 --- /dev/null +++ b/tests/asp/reify_reflect/reified/theory_function.lp @@ -0,0 +1,10 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,theory_atom(4),body_literals(14)). +theory_atom(4,function(5),theory_atom_elements(7),theory_guard(13)). +function(5,a,terms(6)). +theory_atom_elements(7,0,theory_terms(8),literals(12)). +theory_terms(8,0,theory_function(9)). +theory_function(9,f,theory_terms(10)). +theory_terms(10,0,number(11)). +number(11,1). diff --git a/tests/asp/reify_reflect/reified/theory_sequence.lp b/tests/asp/reify_reflect/reified/theory_sequence.lp new file mode 100644 index 0000000..65cc202 --- /dev/null +++ b/tests/asp/reify_reflect/reified/theory_sequence.lp @@ -0,0 +1,12 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,theory_atom(4),body_literals(16)). +theory_atom(4,function(5),theory_atom_elements(7),theory_guard(15)). +function(5,a,terms(6)). +theory_atom_elements(7,0,theory_terms(8),literals(14)). +theory_terms(8,0,theory_sequence(9)). +theory_sequence(9,"[]",theory_terms(10)). +theory_terms(10,0,number(11)). +number(11,1). +theory_terms(10,1,function(12)). +function(12,b,terms(13)). diff --git a/tests/asp/reify_reflect/well_formed_ast/theory_unparsed_term.lp b/tests/asp/reify_reflect/reified/theory_unparsed_term.lp similarity index 59% rename from tests/asp/reify_reflect/well_formed_ast/theory_unparsed_term.lp rename to tests/asp/reify_reflect/reified/theory_unparsed_term.lp index 01d2c76..09b461d 100644 --- a/tests/asp/reify_reflect/well_formed_ast/theory_unparsed_term.lp +++ b/tests/asp/reify_reflect/reified/theory_unparsed_term.lp @@ -1,12 +1,7 @@ -% reified fact representation of program: -% #program base. -% &a { +1!-"b" }. - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,theory_atom(3),body_literals(17)). -theory_atom(3,symbolic_atom(4),theory_atom_elements(7),theory_guard(16)). -symbolic_atom(4,function(5)). +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,theory_atom(4),body_literals(17)). +theory_atom(4,function(5),theory_atom_elements(7),theory_guard(16)). function(5,a,terms(6)). theory_atom_elements(7,0,theory_terms(8),literals(15)). theory_terms(8,0,theory_unparsed_term(9)). diff --git a/tests/asp/reify_reflect/reified/unary_operation.lp b/tests/asp/reify_reflect/reified/unary_operation.lp new file mode 100644 index 0000000..a775743 --- /dev/null +++ b/tests/asp/reify_reflect/reified/unary_operation.lp @@ -0,0 +1,11 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,literal(4),body_literals(10)). +% facts representing head literal neg((-1)). +literal(4,"pos",symbolic_atom(5)). +symbolic_atom(5,function(6)). +function(6,neg,terms(7)). +% facts representing the unary operation (-1). +terms(7,0,unary_operation(8)). +unary_operation(8,"-",number(9)). +number(9,1). diff --git a/tests/asp/reify_reflect/reified/variable.lp b/tests/asp/reify_reflect/reified/variable.lp new file mode 100644 index 0000000..531f363 --- /dev/null +++ b/tests/asp/reify_reflect/reified/variable.lp @@ -0,0 +1,20 @@ +program(0,"base",constants(1),statements(2)). +statements(2,0,rule(3)). +rule(3,literal(4),body_literals(10)). +% facts representing head literal rel(Y,X). +literal(4,"pos",symbolic_atom(5)). +symbolic_atom(5,function(6)). +function(6,rel,terms(7)). +terms(7,0,variable(8)). +variable(8,"Y"). +terms(7,1,variable(9)). +variable(9,"X"). +% facts representing body literal rel(X,Y). +body_literals(10,0,body_literal(11)). +body_literal(11,"pos",symbolic_atom(12)). +symbolic_atom(12,function(13)). +function(13,rel,terms(14)). +terms(14,0,variable(15)). +variable(15,"X"). +terms(14,1,variable(16)). +variable(16,"Y"). diff --git a/tests/asp/reify_reflect/well_formed_ast/aggregate.lp b/tests/asp/reify_reflect/well_formed_ast/aggregate.lp deleted file mode 100644 index 41005c7..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/aggregate.lp +++ /dev/null @@ -1,24 +0,0 @@ -% reified fact representation of program: -% #program base. -% 1 <= { a: b; c }. - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,aggregate(3),body_literals(24)). -aggregate(3,guard(4),agg_elements(6),guard(23)). -guard(4,"<=",number(5)). -number(5,1). -agg_elements(6,0,conditional_literal(7)). -conditional_literal(7,literal(8),literals(12)). -literal(8,"pos",symbolic_atom(9)). -symbolic_atom(9,function(10)). -function(10,a,terms(11)). -literals(12,0,literal(13)). -literal(13,"pos",symbolic_atom(14)). -symbolic_atom(14,function(15)). -function(15,b,terms(16)). -agg_elements(6,1,conditional_literal(17)). -conditional_literal(17,literal(18),literals(22)). -literal(18,"pos",symbolic_atom(19)). -symbolic_atom(19,function(20)). -function(20,c,terms(21)). diff --git a/tests/asp/reify_reflect/well_formed_ast/aggregate2.lp b/tests/asp/reify_reflect/well_formed_ast/aggregate2.lp deleted file mode 100644 index 9938c82..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/aggregate2.lp +++ /dev/null @@ -1,17 +0,0 @@ -% reified fact representation of program: -% #program base. -% 1 <= { a } < 3. - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,aggregate(3),body_literals(15)). -aggregate(3,guard(4),agg_elements(6),guard(13)). -guard(4,"<=",number(5)). -number(5,1). -agg_elements(6,0,conditional_literal(7)). -conditional_literal(7,literal(8),literals(12)). -literal(8,"pos",symbolic_atom(9)). -symbolic_atom(9,function(10)). -function(10,a,terms(11)). -guard(13,"<",number(14)). -number(14,3). diff --git a/tests/asp/reify_reflect/well_formed_ast/atom.lp b/tests/asp/reify_reflect/well_formed_ast/atom.lp deleted file mode 100644 index 04e78ed..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/atom.lp +++ /dev/null @@ -1,24 +0,0 @@ -% reified fact representation of program: -% #program base. -% rel(2,1) :- rel(1,2). - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,literal(3),body_literals(9)). -% facts representing head literal rel(2,1). -literal(3,"pos",symbolic_atom(4)). -symbolic_atom(4,function(5)). -function(5,rel,terms(6)). -terms(6,0,number(7)). -number(7,2). -terms(6,1,number(8)). -number(8,1). -% facts representing body literal rel(1,2). -body_literals(9,0,body_literal(10)). -body_literal(10,"pos",symbolic_atom(11)). -symbolic_atom(11,function(12)). -function(12,rel,terms(13)). -terms(13,0,number(14)). -number(14,1). -terms(13,1,number(15)). -number(15,2). diff --git a/tests/asp/reify_reflect/well_formed_ast/binary_operation.lp b/tests/asp/reify_reflect/well_formed_ast/binary_operation.lp deleted file mode 100644 index 5f92330..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/binary_operation.lp +++ /dev/null @@ -1,18 +0,0 @@ -% reified fact representation of program: -% #program base. -% equal((1+1),2). - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,literal(3),body_literals(11)). -% facts representing head literal equal((1+1),2). -literal(3,"pos",symbolic_atom(4)). -symbolic_atom(4,function(5)). -function(5,equal,terms(6)). -% facts representing the binary operation (1+1). -terms(6,0,binary_operation(7)). -binary_operation(7,"+",number(8),number(9)). -number(8,1). -number(9,1). -terms(6,1,number(10)). -number(10,2). diff --git a/tests/asp/reify_reflect/well_formed_ast/body_aggregate.lp b/tests/asp/reify_reflect/well_formed_ast/body_aggregate.lp deleted file mode 100644 index 15a67c0..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/body_aggregate.lp +++ /dev/null @@ -1,30 +0,0 @@ -% reified fact representation of program: -% #program base. -% :- #sum+ { 1,2: not a; 3: b } != 4. - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,literal(3),body_literals(5)). -literal(3,"pos",boolean_constant(4)). -boolean_constant(4,false). -body_literals(5,0,body_literal(6)). -body_literal(6,"pos",body_aggregate(7)). -body_aggregate(7,guard(8),"#sum+",body_agg_elements(10),guard(26)). -guard(8,"!=",number(9)). -number(9,4). -body_agg_elements(10,0,terms(11),literals(14)). -terms(11,0,number(12)). -number(12,1). -terms(11,1,number(13)). -number(13,2). -literals(14,0,literal(15)). -literal(15,"not",symbolic_atom(16)). -symbolic_atom(16,function(17)). -function(17,a,terms(18)). -body_agg_elements(10,1,terms(19),literals(21)). -terms(19,0,number(20)). -number(20,3). -literals(21,0,literal(22)). -literal(22,"pos",symbolic_atom(23)). -symbolic_atom(23,function(24)). -function(24,b,terms(25)). diff --git a/tests/asp/reify_reflect/well_formed_ast/bool_const.lp b/tests/asp/reify_reflect/well_formed_ast/bool_const.lp deleted file mode 100644 index 01f2c57..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/bool_const.lp +++ /dev/null @@ -1,14 +0,0 @@ -% reified fact representation of program: -% #program base. -% #false. #true. - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,literal(3),body_literals(5)). -% facts representing head literal a. -literal(3,"pos",boolean_constant(4)). -boolean_constant(4,false). -statements(1,1,rule(6)). -rule(6,literal(7),body_literals(9)). -literal(7,"pos",boolean_constant(8)). -boolean_constant(8,true). diff --git a/tests/asp/reify_reflect/well_formed_ast/comparison.lp b/tests/asp/reify_reflect/well_formed_ast/comparison.lp deleted file mode 100644 index 572c793..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/comparison.lp +++ /dev/null @@ -1,19 +0,0 @@ -% reified fact representation of program: -% #program base. -% 1 < 2 != 3 > 4. - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,literal(3),body_literals(13)). -literal(3,"pos",comparison(4)). -comparison(4,number(5),guards(6)). -number(5,1). -guards(6,0,guard(7)). -guard(7,"<",number(8)). -number(8,2). -guards(6,1,guard(9)). -guard(9,"!=",number(10)). -number(10,3). -guards(6,2,guard(11)). -guard(11,">", number(12)). -number(12,4). diff --git a/tests/asp/reify_reflect/well_formed_ast/conditional_literal.lp b/tests/asp/reify_reflect/well_formed_ast/conditional_literal.lp deleted file mode 100644 index 692003e..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/conditional_literal.lp +++ /dev/null @@ -1,25 +0,0 @@ -% reified fact representation of program: -% #program base. -% a :- b : c, d. - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,literal(3),body_literals(7)). -% facts representing head literal a. -literal(3,"pos",symbolic_atom(4)). -symbolic_atom(4,function(5)). -function(5,a,terms(6)). -% facts representing conditional literal b : c, d. -body_literals(7,0,conditional_literal(8)). -conditional_literal(8,literal(9),literals(13)). -literal(9,"pos",symbolic_atom(10)). -symbolic_atom(10,function(11)). -function(11,b,terms(12)). -literals(13,0,literal(14)). -literal(14,"pos",symbolic_atom(15)). -symbolic_atom(15,function(16)). -function(16,c,terms(17)). -literals(13,1,literal(18)). -literal(18,"pos",symbolic_atom(19)). -symbolic_atom(19,function(20)). -function(20,d,terms(21)). diff --git a/tests/asp/reify_reflect/well_formed_ast/constant_term.lp b/tests/asp/reify_reflect/well_formed_ast/constant_term.lp deleted file mode 100644 index f907aca..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/constant_term.lp +++ /dev/null @@ -1,13 +0,0 @@ -% reified fact representation of program: -% #program base. -% good(human). - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,literal(3),body_literals(9)). -% facts representing head literal yummy("carrot"). -literal(3,"pos",symbolic_atom(4)). -symbolic_atom(4,function(5)). -function(5,good,terms(6)). -terms(6,0,function(7)). -function(7,human,terms(8)). diff --git a/tests/asp/reify_reflect/well_formed_ast/disjunction.lp b/tests/asp/reify_reflect/well_formed_ast/disjunction.lp deleted file mode 100644 index b8da52c..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/disjunction.lp +++ /dev/null @@ -1,26 +0,0 @@ -% reified fact representation of program: -% #program base. -% a; b : c, not d. - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,disjunction(3),body_literals(25)). -disjunction(3,conditional_literals(4)). -conditional_literals(4,0,conditional_literal(5)). -conditional_literal(5,literal(6),literals(10)). -literal(6,"pos",symbolic_atom(7)). -symbolic_atom(7,function(8)). -function(8,a,terms(9)). -conditional_literals(4,1,conditional_literal(11)). -conditional_literal(11,literal(12),literals(16)). -literal(12,"pos",symbolic_atom(13)). -symbolic_atom(13,function(14)). -function(14,b,terms(15)). -literals(16,0,literal(17)). -literal(17,"pos",symbolic_atom(18)). -symbolic_atom(18,function(19)). -function(19,c,terms(20)). -literals(16,1,literal(21)). -literal(21,"not",symbolic_atom(22)). -symbolic_atom(22,function(23)). -function(23,d,terms(24)). diff --git a/tests/asp/reify_reflect/well_formed_ast/external.lp b/tests/asp/reify_reflect/well_formed_ast/external.lp deleted file mode 100644 index f4b5af6..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/external.lp +++ /dev/null @@ -1,25 +0,0 @@ -% reified fact representation of program: -% #program base. -% #external a(X) : c(X); d(e(X)). - -program("base",constants(0),statements(1)). -statements(1,0,external(2)). -external(2,symbolic_atom(3),body_literals(7),false). -symbolic_atom(3,function(4)). -function(4,a,terms(5)). -terms(5,0,variable(6)). -variable(6,"X"). -body_literals(7,0,body_literal(8)). -body_literal(8,"pos",symbolic_atom(9)). -symbolic_atom(9,function(10)). -function(10,c,terms(11)). -terms(11,0,variable(12)). -variable(12,"X"). -body_literals(7,1,body_literal(13)). -body_literal(13,"pos",symbolic_atom(14)). -symbolic_atom(14,function(15)). -function(15,d,terms(16)). -terms(16,0,function(17)). -function(17,e,terms(18)). -terms(18,0,variable(19)). -variable(19,"X"). diff --git a/tests/asp/reify_reflect/well_formed_ast/function.lp b/tests/asp/reify_reflect/well_formed_ast/function.lp deleted file mode 100644 index f6fb076..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/function.lp +++ /dev/null @@ -1,15 +0,0 @@ -% reified fact representation of program: -% #program base. -% next(move(a)). - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,literal(3),body_literals(11)). -% facts representing head literal rel(2,1). -literal(3,"pos",symbolic_atom(4)). -symbolic_atom(4,function(5)). -function(5,next,terms(6)). -terms(6,0,function(7)). -function(7,move,terms(8)). -terms(8,0,function(9)). -function(9,a,terms(10)). diff --git a/tests/asp/reify_reflect/well_formed_ast/head_aggregate.lp b/tests/asp/reify_reflect/well_formed_ast/head_aggregate.lp deleted file mode 100644 index dab8a76..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/head_aggregate.lp +++ /dev/null @@ -1,30 +0,0 @@ -% reified fact representation of program: -% #program base. -% #sum { 1,2: a: not b; 3: c } = 4. - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,head_aggregate(3),body_literals(29)). -head_aggregate(3,guard(4),"#sum",head_agg_elements(6),guard(28)). -guard(4,"=",number(5)). -number(5,4). -head_agg_elements(6,0,terms(7),conditional_literal(10)). -terms(7,0,number(8)). -number(8,1). -terms(7,1,number(9)). -number(9,2). -conditional_literal(10,literal(11),literals(15)). -literal(11,"pos",symbolic_atom(12)). -symbolic_atom(12,function(13)). -function(13,a,terms(14)). -literals(15,0,literal(16)). -literal(16,"not",symbolic_atom(17)). -symbolic_atom(17,function(18)). -function(18,b,terms(19)). -head_agg_elements(6,1,terms(20),conditional_literal(22)). -terms(20,0,number(21)). -number(21,3). -conditional_literal(22,literal(23),literals(27)). -literal(23,"pos",symbolic_atom(24)). -symbolic_atom(24,function(25)). -function(25,c,terms(26)). diff --git a/tests/asp/reify_reflect/well_formed_ast/integrity_constraint.lp b/tests/asp/reify_reflect/well_formed_ast/integrity_constraint.lp deleted file mode 100644 index 5937b3a..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/integrity_constraint.lp +++ /dev/null @@ -1,13 +0,0 @@ -% reified fact representation of program: -% #program base. -% :- a. - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,literal(3),body_literals(5)). -literal(3,"pos",boolean_constant(4)). -boolean_constant(4,false). -body_literals(5,0,body_literal(6)). -body_literal(6,"pos",symbolic_atom(7)). -symbolic_atom(7,function(8)). -function(8,a,terms(9)). diff --git a/tests/asp/reify_reflect/well_formed_ast/interval.lp b/tests/asp/reify_reflect/well_formed_ast/interval.lp deleted file mode 100644 index 66a0e9a..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/interval.lp +++ /dev/null @@ -1,14 +0,0 @@ -% reified fact representation of program: -% #program base. -% a(1..3). - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,literal(3),body_literals(10)). -literal(3,"pos",symbolic_atom(4)). -symbolic_atom(4,function(5)). -function(5,a,terms(6)). -terms(6,0,interval(7)). -interval(7,number(8),number(9)). -number(8,1). -number(9,3). diff --git a/tests/asp/reify_reflect/well_formed_ast/pool.lp b/tests/asp/reify_reflect/well_formed_ast/pool.lp deleted file mode 100644 index 9126a9a..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/pool.lp +++ /dev/null @@ -1,17 +0,0 @@ -% reified fact representation of program: -% #program base. -% pool((1;a)). - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,literal(3),body_literals(12)). -% facts representing head literal pool(1;a). -literal(3,"pos",symbolic_atom(4)). -symbolic_atom(4,function(5)). -function(5,pool,terms(6)). -terms(6,0,pool(7)). -pool(7,terms(8)). -terms(8,0,number(9)). -number(9,1). -terms(8,1,function(10)). -function(10,a,terms(11)). diff --git a/tests/asp/reify_reflect/well_formed_ast/program_acid.lp b/tests/asp/reify_reflect/well_formed_ast/program_acid.lp deleted file mode 100644 index e6cf6cb..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/program_acid.lp +++ /dev/null @@ -1,7 +0,0 @@ -% reified fact representation of program: -% #program acid(k). - -program("base",constants(0),statements(1)). -program("acid",constants(2),statements(5)). -constants(2,0,function(3)). -function(3,k,terms(4)). diff --git a/tests/asp/reify_reflect/well_formed_ast/prop_fact.lp b/tests/asp/reify_reflect/well_formed_ast/prop_fact.lp deleted file mode 100644 index 89cee89..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/prop_fact.lp +++ /dev/null @@ -1,11 +0,0 @@ -% reified fact representation of program: -% #program base. -% a. - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,literal(3),body_literals(7)). -% facts representing head literal a. -literal(3,"pos",symbolic_atom(4)). -symbolic_atom(4,function(5)). -function(5,a,terms(6)). diff --git a/tests/asp/reify_reflect/well_formed_ast/prop_normal_rule.lp b/tests/asp/reify_reflect/well_formed_ast/prop_normal_rule.lp deleted file mode 100644 index d65b689..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/prop_normal_rule.lp +++ /dev/null @@ -1,21 +0,0 @@ -% reified fact representation of program: -% #program base. -% a :- b; not c. - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,literal(3),body_literals(7)). -% facts representing head literal a. -literal(3,"pos",symbolic_atom(4)). -symbolic_atom(4,function(5)). -function(5,a,terms(6)). -% facts representing body literal b. -body_literals(7,0,body_literal(8)). -body_literal(8,"pos",symbolic_atom(9)). -symbolic_atom(9,function(10)). -function(10,b,terms(11)). -% facts representing body literal not c. -body_literals(7,1,body_literal(12)). -body_literal(12,"not",symbolic_atom(13)). -symbolic_atom(13,function(14)). -function(14,c,terms(15)). diff --git a/tests/asp/reify_reflect/well_formed_ast/string.lp b/tests/asp/reify_reflect/well_formed_ast/string.lp deleted file mode 100644 index 468664c..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/string.lp +++ /dev/null @@ -1,13 +0,0 @@ -% reified fact representation of program: -% #program base. -% yummy("carrot"). - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,literal(3),body_literals(8)). -% facts representing head literal yummy("carrot"). -literal(3,"pos",symbolic_atom(4)). -symbolic_atom(4,function(5)). -function(5,yummy,terms(6)). -terms(6,0,string(7)). -string(7,"carrot"). diff --git a/tests/asp/reify_reflect/well_formed_ast/theory_atom_simple.lp b/tests/asp/reify_reflect/well_formed_ast/theory_atom_simple.lp deleted file mode 100644 index 43a1b32..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/theory_atom_simple.lp +++ /dev/null @@ -1,10 +0,0 @@ -% reified fact representation of program: -% #program base. -% &a. - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,theory_atom(3),body_literals(9)). -theory_atom(3,symbolic_atom(4),theory_atom_elements(7),theory_guard(8)). -symbolic_atom(4,function(5)). -function(5,a,terms(6)). diff --git a/tests/asp/reify_reflect/well_formed_ast/theory_atom_simple_arg.lp b/tests/asp/reify_reflect/well_formed_ast/theory_atom_simple_arg.lp deleted file mode 100644 index 5a8ed37..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/theory_atom_simple_arg.lp +++ /dev/null @@ -1,12 +0,0 @@ -% reified fact representation of program: -% #program base. -% &a("b"). - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,theory_atom(3),body_literals(10)). -theory_atom(3,symbolic_atom(4),theory_atom_elements(8),theory_guard(9)). -symbolic_atom(4,function(5)). -function(5,a,terms(6)). -terms(6,0,string(7)). -string(7,"b"). diff --git a/tests/asp/reify_reflect/well_formed_ast/theory_atom_simple_guard.lp b/tests/asp/reify_reflect/well_formed_ast/theory_atom_simple_guard.lp deleted file mode 100644 index 9c68bcc..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/theory_atom_simple_guard.lp +++ /dev/null @@ -1,12 +0,0 @@ -% reified fact representation of program: -% #program base. -% &a{} | b. - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,theory_atom(3),body_literals(11)). -theory_atom(3,symbolic_atom(4),theory_atom_elements(7),theory_guard(8)). -symbolic_atom(4,function(5)). -function(5,a,terms(6)). -theory_guard(8,"|",function(9)). -function(9,b,terms(10)). diff --git a/tests/asp/reify_reflect/well_formed_ast/theory_function.lp b/tests/asp/reify_reflect/well_formed_ast/theory_function.lp deleted file mode 100644 index 2748535..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/theory_function.lp +++ /dev/null @@ -1,15 +0,0 @@ -% reified fact representation of program: -% #program base. -% &a { f(1) }. - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,theory_atom(3),body_literals(14)). -theory_atom(3,symbolic_atom(4),theory_atom_elements(7),theory_guard(13)). -symbolic_atom(4,function(5)). -function(5,a,terms(6)). -theory_atom_elements(7,0,theory_terms(8),literals(12)). -theory_terms(8,0,theory_function(9)). -theory_function(9,f,theory_terms(10)). -theory_terms(10,0,number(11)). -number(11,1). diff --git a/tests/asp/reify_reflect/well_formed_ast/theory_sequence.lp b/tests/asp/reify_reflect/well_formed_ast/theory_sequence.lp deleted file mode 100644 index a3a031e..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/theory_sequence.lp +++ /dev/null @@ -1,17 +0,0 @@ -% reified fact representation of program: -% #program base. -% &a{[1,b]}. - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,theory_atom(3),body_literals(16)). -theory_atom(3,symbolic_atom(4),theory_atom_elements(7),theory_guard(15)). -symbolic_atom(4,function(5)). -function(5,a,terms(6)). -theory_atom_elements(7,0,theory_terms(8),literals(14)). -theory_terms(8,0,theory_sequence(9)). -theory_sequence(9,"[]",theory_terms(10)). -theory_terms(10,0,number(11)). -number(11,1). -theory_terms(10,1,function(12)). -function(12,b,terms(13)). diff --git a/tests/asp/reify_reflect/well_formed_ast/unary_operation.lp b/tests/asp/reify_reflect/well_formed_ast/unary_operation.lp deleted file mode 100644 index f5f566b..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/unary_operation.lp +++ /dev/null @@ -1,15 +0,0 @@ -% reified fact representation of program: -% #program base. -% neg(-1). - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,literal(3),body_literals(9)). -% facts representing head literal neg((-1)). -literal(3,"pos",symbolic_atom(4)). -symbolic_atom(4,function(5)). -function(5,neg,terms(6)). -% facts representing the unary operation (-1). -terms(6,0,unary_operation(7)). -unary_operation(7,"-",number(8)). -number(8,1). diff --git a/tests/asp/reify_reflect/well_formed_ast/variable.lp b/tests/asp/reify_reflect/well_formed_ast/variable.lp deleted file mode 100644 index 00efa58..0000000 --- a/tests/asp/reify_reflect/well_formed_ast/variable.lp +++ /dev/null @@ -1,24 +0,0 @@ -% reified fact representation of program: -% #program base. -% rel(Y,X) :- rel(X,Y). - -program("base",constants(0),statements(1)). -statements(1,0,rule(2)). -rule(2,literal(3),body_literals(9)). -% facts representing head literal rel(Y,X). -literal(3,"pos",symbolic_atom(4)). -symbolic_atom(4,function(5)). -function(5,rel,terms(6)). -terms(6,0,variable(7)). -variable(7,"Y"). -terms(6,1,variable(8)). -variable(8,"X"). -% facts representing body literal rel(X,Y). -body_literals(9,0,body_literal(10)). -body_literal(10,"pos",symbolic_atom(11)). -symbolic_atom(11,function(12)). -function(12,rel,terms(13)). -terms(13,0,variable(14)). -variable(14,"X"). -terms(13,1,variable(15)). -variable(15,"Y"). diff --git a/tests/test_preds.py b/tests/test_preds.py index 92251aa..4145ba4 100644 --- a/tests/test_preds.py +++ b/tests/test_preds.py @@ -14,7 +14,7 @@ def test_combine_fieleds_lazily_non_field_error(self): """Combining a non BaseField (sub)class should raise error""" message = "{preds.Function1} is not a BaseField or a sub-class." with self.assertRaises(TypeError, msg=message): - preds.combine_fields_lazily([preds.Variable1.Field, preds.Function1]) + preds.combine_fields([preds.Variable.unary.Field, preds.Function.unary]) def test_combine_fields_lazily_no_combined_pytocl_error(self): """If python data cannot be converted to clingo data by any of @@ -23,14 +23,14 @@ def test_combine_fields_lazily_no_combined_pytocl_error(self): """ asd_field = refine_field(StringField, ["asd"]) dsa_field = refine_field(StringField, ["dsa"]) - AsdOrDsaField = preds.combine_fields_lazily( - [asd_field, dsa_field], name="AsdOrDsaField" + asd_or_dsa_field = preds.combine_fields( + [asd_field, dsa_field], name="asd_or_dsa_field" ) py_str = "banana" message = f"No combined pytocl() match for value {py_str}." with self.assertRaises(TypeError, msg=message): - AsdOrDsaField.pytocl(py_str) + asd_or_dsa_field.pytocl(py_str) def test_combine_fields_lazily_failure_to_unify(self): """If clingo data cannot be converted to python data by any of @@ -39,7 +39,7 @@ def test_combine_fields_lazily_failure_to_unify(self): """ asd_field = refine_field(StringField, ["asd"]) dsa_field = refine_field(StringField, ["dsa"]) - asd_dsa_field = preds.combine_fields_lazily([asd_field, dsa_field]) + asd_dsa_field = preds.combine_fields([asd_field, dsa_field]) clingo_str = String("banana") message = ( f"Object '{clingo_str}' ({type(clingo_str)}) failed to " diff --git a/tests/test_reify_reflect.py b/tests/test_reify_reflect.py index cb2a7e1..4241f03 100644 --- a/tests/test_reify_reflect.py +++ b/tests/test_reify_reflect.py @@ -2,23 +2,25 @@ """Test cases for reification functionality.""" from itertools import count from pathlib import Path -from typing import List from unittest import TestCase -from clingo.ast import parse_string +from clingo import ast from clorm import FactBase, Predicate, UnifierNoMatchError import renopro.predicates as preds from renopro.rast import ChildQueryError, ChildrenQueryError, ReifiedAST test_files = Path("tests", "asp", "reify_reflect") -well_formed_ast_files = test_files / "well_formed_ast" -malformed_ast_files = test_files / "malformed_ast" +reified_files = test_files / "reified" +reflected_files = test_files / "reflected" +malformed_reified_files = test_files / "malformed" class TestReifiedAST(TestCase): """Common base class for tests involving the ReifiedAST class.""" + maxDiff = 930 + def setUp(self): # reset id generator between test cases so reification # auto-generates the expected integers @@ -78,10 +80,23 @@ def test_unification_error_message(self): def test_reified_files(self): """Test adding of reified facts from files to reified facts of a ReifiedAST.""" rast = ReifiedAST() - rast.add_reified_files([malformed_ast_files / "ast_fact.lp"]) - fb = FactBase([preds.Symbolic_Atom(id=12, symbol=preds.Function1(id=13))]) + rast.add_reified_files([reified_files / "ast_fact.lp"]) + fb = FactBase([preds.SymbolicAtom(id=12, symbol=preds.Function.unary(id=13))]) self.assertSetEqual(rast.reified_facts, fb) + def test_program_ast(self): + "Test accessibility of program AST nodes." + prog_str = "a." + stms = [] + ast.parse_string(prog_str, stms.append) + rast = ReifiedAST() + rast.reify_string(prog_str) + rast.reflect() + self.assertListEqual(stms, rast.program_ast) + reified = rast.reified_facts + rast.reify_ast(stms) + self.assertSetEqual(reified, rast.reified_facts) + class TestReifyReflect(TestReifiedAST): """Base class for tests for reification and reflection of @@ -89,120 +104,121 @@ class TestReifyReflect(TestReifiedAST): base_str = "#program base.\n" - def assertReifyEqual( - self, prog_str: str, ast_fact_files: List[str] - ): # pylint: disable=invalid-name - "Assert that reification of prog_str results in ast_facts." - ast_fact_files_str = [(well_formed_ast_files / f) for f in ast_fact_files] + def assertReifyEqual(self, file_name: str): # pylint: disable=invalid-name + """Assert that reification of file_name under reflected_files + results in file_name under reified_files.""" + reified_file = reified_files / file_name + reflected_file = reflected_files / file_name + reflected_str = reflected_file.read_text().strip() rast1 = ReifiedAST() - rast1.reify_string(prog_str) + rast1.reify_string(reflected_str) reified_facts = rast1.reified_facts rast2 = ReifiedAST() - rast2.add_reified_files(ast_fact_files_str) + rast2.add_reified_files([reified_file]) expected_facts = rast2.reified_facts self.assertSetEqual(reified_facts, expected_facts) # type: ignore - def assertReflectEqual( - self, prog_str: str, ast_fact_files: List[str] - ): # pylint: disable=invalid-name - "Assert that reflection of ast_facts results in prog_str." - ast_fact_files_str = [(well_formed_ast_files / f) for f in ast_fact_files] + def assertReflectEqual(self, file_name: str): # pylint: disable=invalid-name + """Assert that reflection of file_name under reified_files + results in file_name under reflected_files.""" + reified_file = reified_files / file_name + reflected_file = reflected_files / file_name + reflected_str = reflected_file.read_text().strip() rast = ReifiedAST() - rast.add_reified_files(ast_fact_files_str) + rast.add_reified_files([reified_file]) rast.reflect() - expected_string = self.base_str + prog_str + expected_string = self.base_str + reflected_str self.assertEqual(rast.program_string, expected_string) - def assertReifyReflectEqual( - self, prog_str: str, ast_fact_files: List[str] - ): # pylint: disable=invalid-name + def assertReifyReflectEqual(self, file_name: str): # pylint: disable=invalid-name """Assert that reification of prog_str results in ast_facts, and that reflection of ast_facts result in prog_str.""" for operation in ["reification", "reflection"]: with self.subTest(operation=operation): if operation == "reification": - self.assertReifyEqual(prog_str, ast_fact_files) + self.assertReifyEqual(file_name) elif operation == "reflection": - self.assertReflectEqual(prog_str, ast_fact_files) + self.assertReflectEqual(file_name) class TestReifyReflectNormalPrograms(TestReifyReflect): - """Test cases for reification and reflection of normal logic programs.""" + """Test cases for reification and reflection of disjunctive logic + program AST nodes.""" def test_rast_prop_fact(self): "Test reification and reflection of a propositional fact." - self.assertReifyReflectEqual("a.", ["prop_fact.lp"]) - rast = ReifiedAST() - rast.reify_string("a.") - statements = [] - parse_string( - "a.", lambda s: statements.append(s) # pylint: disable=unnecessary-lambda - ) - rast.reflect() - self.assertEqual(rast.program_ast, statements) + self.assertReifyReflectEqual("prop_fact.lp") def test_rast_prop_normal_rule(self): """Test reification and reflection of a normal rule containing only propositional atoms.""" - self.assertReifyReflectEqual("a :- b; not c.", ["prop_normal_rule.lp"]) + self.assertReifyReflectEqual("prop_normal_rule.lp") def test_rast_function(self): """Test reification and reflection of a variable-free normal rule with a symbolic atom.""" - self.assertReifyReflectEqual("rel(2,1) :- rel(1,2).", ["atom.lp"]) + self.assertReifyReflectEqual("atom.lp") + + def test_rast_external_function(self): + """Test reification and reflection of an external function term.""" + self.assertReifyReflectEqual("external_function.lp") def test_rast_nested_function(self): "Test reification and reflection of a rule with a function term." - self.assertReifyReflectEqual("next(move(a)).", ["function.lp"]) + self.assertReifyReflectEqual("function.lp") def test_rast_variable(self): "Test reification and reflection of normal rule with variable terms." - self.assertReifyReflectEqual("rel(Y,X) :- rel(X,Y).", ["variable.lp"]) + self.assertReifyReflectEqual("variable.lp") def test_rast_string(self): "Test reification and reflection of normal rule with a string term." - self.assertReifyReflectEqual('yummy("carrot").', ["string.lp"]) + self.assertReifyReflectEqual("string.lp") def test_rast_constant_term(self): "Test reification and reflection of a normal rule with constant term." - self.assertReifyReflectEqual("good(human).", ["constant_term.lp"]) + self.assertReifyReflectEqual("constant_term.lp") def test_rast_interval(self): "Test reification and reflection of a normal rule with an interval term." - self.assertReifyReflectEqual("a((1..3)).", ["interval.lp"]) + self.assertReifyReflectEqual("interval.lp") def test_rast_unary_operation(self): "Test reification and reflection of a normal rule with a unary operation." - self.assertReifyReflectEqual("neg(-1).", ["unary_operation.lp"]) + self.assertReifyReflectEqual("unary_operation.lp") def test_rast_binary_operation(self): "Test reification and reflection of a normal rule with a binary operation." - self.assertReifyReflectEqual("equal((1+1),2).", ["binary_operation.lp"]) + self.assertReifyReflectEqual("binary_operation.lp") def test_rast_pool(self): "Test reification and reflection of a normal rule with a pool." - self.assertReifyReflectEqual("pool((1;a)).", ["pool.lp"]) + self.assertReifyReflectEqual("pool.lp") def test_rast_comparison(self): "Test reification and reflection of a comparison operator" - self.assertReifyReflectEqual("1 < 2 != 3 > 4.", ["comparison.lp"]) + self.assertReifyReflectEqual("comparison.lp") def test_rast_boolean_constant(self): "Test reification and reflection of a boolean constant." - self.assertReifyReflectEqual("#false.\n#true.", ["bool_const.lp"]) + self.assertReifyReflectEqual("bool_const.lp") def test_rast_integrity_constraint(self): "Test reification and reflection of an integrity constraint." - self.assertReifyEqual(":- a.", ["integrity_constraint.lp"]) + self.assertReifyEqual("integrity_constraint.lp") def test_rast_conditional_literal(self): "Test reification and reflection of a conditional literal." - self.assertReifyReflectEqual("a :- b: c, d.", ["conditional_literal.lp"]) + self.assertReifyReflectEqual("conditional_literal.lp") def test_rast_disjunction(self): "Test reification and reflection of a disjunction." - self.assertReifyReflectEqual("a; b: c, not d.", ["disjunction.lp"]) + self.assertReifyReflectEqual("disjunction.lp") + + +class TestReifyReflectErrors(TestReifyReflect): + "Test cases where reification or reflection should fail." def test_rast_node_failure(self): """Reification for any object not of type clingo.ast.AST or @@ -219,8 +235,8 @@ def test_child_query_error_one_expected_zero_found(self): """ rast = ReifiedAST() - rast.add_reified_files([malformed_ast_files / "one_expected_zero_found.lp"]) - regex = r"(?s).*atom\(4,function\(5\)\).*function\(5\).*found 0.*" + rast.add_reified_files([malformed_reified_files / "one_expected_zero_found.lp"]) + regex = r"(?s).*atom\(5,function\(6\)\).*function\(6\).*found 0.*" with self.assertRaisesRegex(ChildQueryError, expected_regex=regex): rast.reflect() @@ -231,8 +247,10 @@ def test_child_query_error_one_expected_multiple_found(self): """ rast = ReifiedAST() - rast.add_reified_files([malformed_ast_files / "one_expected_multiple_found.lp"]) - regex = r"(?s).*atom\(4,function\(5\)\).*function\(5\).*found 2.*" + rast.add_reified_files( + [malformed_reified_files / "one_expected_multiple_found.lp"] + ) + regex = r"(?s).*atom\(5,function\(6\)\).*function\(6\).*found 2.*" with self.assertRaisesRegex(ChildQueryError, expected_regex=regex): rast.reflect() @@ -240,10 +258,10 @@ def test_children_query_error_multiple_in_same_position(self): """Refection of a child tuple with multiple elements in the same position should fail""" rast = ReifiedAST() - rast.add_reified_files([malformed_ast_files / "multiple_in_same_pos.lp"]) + rast.add_reified_files([malformed_reified_files / "multiple_in_same_pos.lp"]) regex = ( - r"(?s).*comparison\(4,number\(5\),guards\(6\)\).*" - r"multiple child facts in the same position.*guards\(6\)" + r"(?s).*comparison\(5,number\(6\),guards\(7\)\).*" + r"multiple child facts in the same position.*guards\(7\)" ) with self.assertRaisesRegex(ChildrenQueryError, expected_regex=regex): rast.reflect() @@ -252,10 +270,12 @@ def test_children_query_error_one_or_more_expected_zero_found(self): """Reflection of a child facts where one or more facts are expected should fail with an informative error message when zero are found.""" rast = ReifiedAST() - rast.add_reified_files([malformed_ast_files / "one_or_more_expected_found_zero.lp"]) + rast.add_reified_files( + [malformed_reified_files / "one_or_more_expected_found_zero.lp"] + ) regex = ( - r"(?s).*comparison\(4,number\(5\),guards\(6\)\).*" - r".*Expected 1 or more.*guards\(6\).*." + r"(?s).*comparison\(5,number\(6\),guards\(7\)\).*" + r".*Expected 1 or more.*guards\(7\).*." ) with self.assertRaisesRegex(ChildrenQueryError, expected_regex=regex): rast.reflect() @@ -264,10 +284,12 @@ def test_child_query_error_zero_or_one_expected_multiple_found(self): """Reflection of a child fact where zero or one fact is expected should fail with an informative error message when multiple are found.""" rast = ReifiedAST() - rast.add_reified_files([malformed_ast_files / "zero_or_more_expected_multiple_found.lp"]) + rast.add_reified_files( + [malformed_reified_files / "zero_or_more_expected_multiple_found.lp"] + ) regex = ( - r"(?s).*aggregate\(3,guard\(4\),agg_elements\(7\),guard\(8\)\).*" - r".*Expected 0 or 1.*guard\(4\).*found 2." + r"(?s).*aggregate\(4,guard\(5\),aggregate_elements\(8\),guard\(9\)\).*" + r".*Expected 0 or 1.*guard\(5\).*found 2." ) with self.assertRaisesRegex(ChildQueryError, expected_regex=regex): rast.reflect() @@ -279,77 +301,99 @@ class TestReifyReflectAggTheory(TestReifyReflect): def test_rast_aggregate(self): "Test reification and reflection of a simple count aggregate." - with self.subTest(operation="reify"): - self.assertReifyEqual("1 {a: b; c}.", ["aggregate.lp"]) - with self.subTest(operation="reflect"): - self.assertReflectEqual("1 <= { a: b; c }.", ["aggregate.lp"]) + self.assertReifyReflectEqual("aggregate.lp") self.setUp() - self.assertReifyReflectEqual("1 <= { a } < 3.", ["aggregate2.lp"]) + self.assertReifyReflectEqual("aggregate2.lp") def test_rast_simple_theory_atom(self): """Test reification and reflection of simple theory atoms consisting only of symbolic part.""" - with self.subTest(operation="reify"): - self.assertReifyEqual("&a.", ["theory_atom_simple.lp"]) - with self.subTest(operation="reflect"): - self.assertReflectEqual("&a { }.", ["theory_atom_simple.lp"]) + + self.assertReifyReflectEqual("theory_atom_simple.lp") self.setUp() - self.assertReifyReflectEqual('&a("b") { }.', ["theory_atom_simple_arg.lp"]) + self.assertReifyReflectEqual("theory_atom_simple_arg.lp") self.setUp() - self.assertReifyReflectEqual("&a { } | b.", ["theory_atom_simple_guard.lp"]) + self.assertReifyReflectEqual("theory_atom_simple_guard.lp") def test_rast_theory_sequence(self): "Test reification and reflection of a theory sequence." - self.assertReifyReflectEqual("&a { [1,b] }.", ["theory_sequence.lp"]) + self.assertReifyReflectEqual("theory_sequence.lp") def test_rast_theory_sequence_type(self): "Test that all theory sequence types are accepted in reified representation." def test_rast_theory_function(self): "Test reification and reflection of a theory function." - self.assertReifyReflectEqual("&a { f(1) }.", ["theory_function.lp"]) + self.assertReifyReflectEqual("theory_function.lp") def test_rast_theory_term(self): "Test that all theory terms are accepted in reified representation." def test_rast_theory_unparsed_term(self): "Test reification and reflection of an unparsed theory term." - self.assertReifyReflectEqual( - '&a { (+ 1 !- > "b") }.', ["theory_unparsed_term.lp"] - ) + self.assertReifyReflectEqual("theory_unparsed_term.lp") def test_rast_head_aggregate(self): "Test reification and reflection of a head aggregate." - with self.subTest(operation="reify"): - self.assertReifyEqual( - "#sum { 1,2: a: not b; 3: c } = 4.", ["head_aggregate.lp"] - ) - with self.subTest(operation="reflect"): - self.assertReflectEqual( - "4 = #sum { 1,2: a: not b; 3: c }.", ["head_aggregate.lp"] - ) + self.assertReifyReflectEqual("head_aggregate.lp") def test_rast_body_aggregate(self): "Test reification and reflection of a body aggregate." - with self.subTest(operation="reify"): - self.assertReifyEqual( - ":- #sum+ { 1,2: not a; 3: b } != 4.", ["body_aggregate.lp"] - ) - with self.subTest(operation="reflect"): - self.assertReflectEqual( - "#false :- 4 != #sum+ { 1,2: not a; 3: b }.", ["body_aggregate.lp"] - ) + self.assertReifyReflectEqual("body_aggregate.lp") class TestReifyReflectStatements(TestReifyReflect): """Test cases for reification and reflection of statements.""" + def test_rast_const_definition(self): + "Test reification and reflections of a constant definition." + self.assertReifyReflectEqual("definition.lp") + + def test_rast_show_signature(self): + "Test reification and reflection of a show signature statement." + self.assertReifyReflectEqual("show_signature.lp") + + def test_rast_defined(self): + "Test reification and reflection of a defined statement." + self.assertReifyReflectEqual("defined.lp") + + def test_rast_show_term(self): + "Test reification and reflection of a show term statement." + self.assertReifyReflectEqual("show_term.lp") + + def test_rast_minimize(self): + "Test reification and reflection of a minimize statement." + self.assertReifyReflectEqual("minimize.lp") + + def test_rast_script(self): + "Test reification and reflection of an embedded script statement." + self.assertReifyReflectEqual("script.lp") + def test_rast_external_false(self): - "Test reification of an external statement with default value false." - self.assertReifyReflectEqual( - "#external a(X) : c(X); d(e(X)). [false]", ["external.lp"] - ) + """Test reification and reflection of an external statement + with default value false.""" + self.assertReifyReflectEqual("external.lp") + + def test_rast_edge(self): + "Test reification and reflection of an edge statement." + self.assertReifyReflectEqual("edge.lp") + + def test_rast_heuristic(self): + "Test reification and reflection of a heuristic statement." + self.assertReifyReflectEqual("heuristic.lp") + + def test_rast_project_atom(self): + "Test reification and reflection of a project atom statement." + self.assertReifyReflectEqual("project_atom.lp") + + def test_rast_project_signature(self): + "Test reification and reflection of a project signature statement." + self.assertReifyReflectEqual("project_signature.lp") def test_rast_program_params(self): "Test reification and reflection of a program statement with parameters." - self.assertReifyReflectEqual("#program acid(k).", ["program_acid.lp"]) + self.assertReifyReflectEqual("program_acid.lp") + + def test_rast_theory_definition(self): + "Test reification and reflection of a theory definition." + self.assertReifyReflectEqual("theory_definition.lp")