Skip to content

Commit

Permalink
Merge pull request #63 from jorgefandinno/jorge/remove-internal-control
Browse files Browse the repository at this point in the history
Jorge/remove internal control
  • Loading branch information
lute47lillo authored Sep 22, 2023
2 parents 52be946 + 6846223 commit afe752b
Show file tree
Hide file tree
Showing 23 changed files with 101 additions and 296 deletions.
6 changes: 2 additions & 4 deletions src/eclingo/solver/preprocessor.py → _preprocessor.pu
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from typing import Set

import clingo
from clingo import Symbol

import eclingo.internal_states.internal_control as internal_control
from eclingo.config import AppConfig


class Preprocessor:
def __init__(
self, config: AppConfig, control: internal_control.InternalStateControl
) -> None:
def __init__(self, config: AppConfig, control: clingo.Control) -> None:
self._control = control
self._config = config
self._epistemic_to_test_mapping = self._control.epistemic_to_test_mapping
Expand Down
1 change: 1 addition & 0 deletions helper_test/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ def setUp(self):
def assert_equal_ordered(self, obj1, obj2):
obj1 = sorted(obj1)
obj2 = sorted(obj2)
self.assertEqual(str(obj1), str(obj2))
self.assertEqual(obj1, obj2)
20 changes: 15 additions & 5 deletions helper_test/helper_parsing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import clingo.ast as _ast

from eclingo.config import AppConfig
from eclingo.internal_states.internal_control import ShowStatement
from eclingo.parsing.parser import parse_program as _parse_program

from . import helper
Expand Down Expand Up @@ -39,6 +38,16 @@ def assert_equal_parsing_program(self, program, expected_program):

def __assert_equal_parsing_program(self, parsed_program, expected_program):
expected_program = self.clingo_parse_program(expected_program)
parsed_program.sort()
expected_program.sort()
self.assertEqual(len(parsed_program), len(expected_program))
for r1, r2 in zip(parsed_program, expected_program):
self.assertEqual(str(r1), str(r2))
self.assertEqual(r1, r2)
self.assertCountEqual(
[str(s) for s in parsed_program], [str(s) for s in expected_program]
)
self.assertCountEqual(parsed_program, expected_program)
self.assert_equal_ordered(parsed_program, expected_program)

def assert_equal_parsing_program_with_show(
Expand All @@ -48,9 +57,10 @@ def assert_equal_parsing_program_with_show(
program_without_show = []
show_statements = []
for statement in parsed_program:
if isinstance(statement, ShowStatement):
show_statements.append(statement)
else:
program_without_show.append(statement)
# if isinstance(statement, ShowStatement):
# show_statements.append(statement)
# else:
# program_without_show.append(statement)
program_without_show.append(statement)
self.__assert_equal_parsing_program(program_without_show, expected_program)
self.assert_equal_ordered(show_statements, expected_show)
6 changes: 3 additions & 3 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ def tests(session: nox.Session):
# "tests/test_app.py",
"tests/test_eclingo.py",
# "tests/test_eclingo_examples.py",
"tests/test_grounder.py",
# "tests/test_grounder.py",
"tests/test_generator_reification.py",
"tests/test_literals.py",
"tests/test_internal_control_ground_program.py",
# "tests/test_internal_control_ground_program.py",
"tests/test_parsing.py",
"tests/test_show.py",
# "tests/test_show.py",
"tests/test_solver_reification.py",
"tests/test_worldview_builder_reification.py",
"tests/test_tester_reification.py",
Expand Down
6 changes: 1 addition & 5 deletions src/eclingo/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ def __init__(self, control, config=None):
control.configuration.solve.project = "auto,3"
control.configuration.solve.models = 0
self.control = control
# else:
# self.project = None
# self.max_models = 1
# self.control = internal_states.InternalStateControl(['0', '--project'])
if config is None:
config = AppConfig()
config = AppConfig(semantics="c19-1", use_reification=True)
self.config = config

if self.max_models == 0:
Expand Down
8 changes: 5 additions & 3 deletions src/eclingo/grounder.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
from typing import Dict, List, Sequence, Tuple

from clingo import Symbol
from clingo.ast import ProgramBuilder
from clingo.control import Control
from clingox import program as clingox_program
from clingox.reify import Reifier

from eclingo.config import AppConfig
from eclingo.internal_states.internal_control import InternalStateControl

from .parsing.parser import parse_program
from .parsing.transformers.ast_reify import reification_program_to_str


class Grounder:
def __init__(self, control: InternalStateControl, config: AppConfig = AppConfig()):
def __init__(self, control: Control, config: AppConfig = AppConfig()):
self.control = control
self.config = config
self.reification = self.config.eclingo_reification
Expand All @@ -27,7 +28,8 @@ def __init__(self, control: InternalStateControl, config: AppConfig = AppConfig(
def add_program(
self, program: str, parameters: Sequence[str] = (), name: str = "base"
) -> None:
with self.control.builder() as builder:
builder = ProgramBuilder(self.control)
with builder:
parse_program(program, builder.add, parameters, name, self.config)

def ground(
Expand Down
Empty file.
101 changes: 0 additions & 101 deletions src/eclingo/internal_states/internal_control.py

This file was deleted.

10 changes: 4 additions & 6 deletions src/eclingo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
import sys
from typing import Sequence

from clingo.application import Flag
from clingo.application import Flag, clingo_main

from eclingo.config import AppConfig
from eclingo.control import Control
from eclingo.internal_states import internal_control
from eclingo.internal_states.internal_control import InternalStateControl

from . import __version__

Expand All @@ -20,7 +18,7 @@
reification_flag = Flag(True)


class Application(internal_control.Application):
class Application:
"""
Application class that can be used with `clingo.clingo_main` to solve CSP
problems.
Expand Down Expand Up @@ -67,7 +65,7 @@ def _read(self, path):
with open(path) as file_:
return file_.read()

def main(self, control: InternalStateControl, files: Sequence[str]) -> None:
def main(self, control: Control, files: Sequence[str]) -> None:
"""
Entry point of the application registering the propagator and
implementing the standard ground and solve functionality.
Expand Down Expand Up @@ -102,5 +100,5 @@ def main(self, control: InternalStateControl, files: Sequence[str]) -> None:
def main():
sys.argv.append("--outf=3")
application = Application()
result = internal_control.clingo_main(application, sys.argv[1:])
result = clingo_main(application, sys.argv[1:])
return int(result)
3 changes: 1 addition & 2 deletions src/eclingo/parsing/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from clingo.ast import Location, Position

from eclingo.config import AppConfig
from eclingo.internal_states.internal_control import ASTObject

from .transformers.parser_negations import StrongNegationReplacement
from .transformers.theory_parser_epistemic import (
Expand All @@ -14,7 +13,7 @@
replace_negations_by_auxiliary_atoms_in_epistemic_literals,
)

_CallbackType = Callable[[ASTObject], None]
_CallbackType = Callable[[ast.AST], None]

from clingo.ast import ASTType, Location, Position, parse_string
from clingox.ast import (
Expand Down
12 changes: 6 additions & 6 deletions src/eclingo/solver/generator.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
from typing import Iterator
from typing import Iterator, cast

import clingo
from clingox.solving import approximate

from eclingo import util
from eclingo.config import AppConfig
from eclingo.internal_states import internal_control

from .candidate import Assumptions, Candidate

# from clingox.solving import approximate


class GeneratorReification:
def __init__(self, config: AppConfig, reified_program: str) -> None:
self._config = config
self.control = internal_control.InternalStateControl(["0"], message_limit=0)
self.control.configuration.solve.project = "show,3"
self.control = clingo.Control(["0"], message_limit=0)
cast(clingo.Configuration, self.control.configuration.solve).project = "show,3"
self.reified_program = reified_program
self.__initialeze_control(reified_program)

Expand Down Expand Up @@ -74,7 +74,7 @@ def __initialeze_control(self, reified_program) -> None:
self.control.ground([("base", [])])

def __call__(self) -> Iterator[Candidate]:
with self.control.solve(yield_=True) as handle:
with cast(clingo.SolveHandle, self.control.solve(yield_=True)) as handle:
for model in handle:
candidate = self._model_to_candidate(model)
yield candidate
Expand Down
19 changes: 12 additions & 7 deletions src/eclingo/solver/tester.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import eclingo.internal_states.internal_control as internal_control
from typing import cast

import clingo
from clingo.control import Configuration

from eclingo.config import AppConfig

from .candidate import Candidate
Expand All @@ -7,9 +11,9 @@
class CandidateTesterReification:
def __init__(self, config: AppConfig, reified_program: str):
self._config = config
self.control = internal_control.InternalStateControl(["0"], message_limit=0)
self.control = clingo.Control(["0"], message_limit=0)
self.reified_program = reified_program
self.control.control.configuration.solve.enum_mode = "cautious" # type: ignore
cast(Configuration, self.control.configuration.solve).enum_mode = "cautious" # type: ignore

program_meta_encoding = """conjunction(B) :- literal_tuple(B),
hold(L) : literal_tuple(B, L), L > 0;
Expand Down Expand Up @@ -77,8 +81,8 @@ def __call__(self, candidate: Candidate) -> bool:
assumption = (literal, False)
candidate_assumptions.append(assumption)

self.control.configuration.solve.models = 0
self.control.configuration.solve.project = "no"
cast(Configuration, self.control.configuration.solve).models = 0
cast(Configuration, self.control.configuration.solve).project = "no"

# print("\nTESTER")
# print("Candidate assumptions:\n", candidate_assumptions)
Expand All @@ -87,8 +91,9 @@ def __call__(self, candidate: Candidate) -> bool:
# "\n".join(str((str(a), v)) for a, v in candidate_assumptions),
# )

with self.control.solve(
yield_=True, assumptions=candidate_assumptions
with cast(
clingo.SolveHandle,
self.control.solve(yield_=True, assumptions=candidate_assumptions),
) as handle:
model = None
for model in handle:
Expand Down
Loading

0 comments on commit afe752b

Please sign in to comment.