Skip to content

Commit

Permalink
rewrite how flag processors called
Browse files Browse the repository at this point in the history
  • Loading branch information
Artsiom Kaltovich committed Oct 15, 2023
1 parent fa9dfc9 commit b06efe4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ programs.

### Prerequisites

- You should have minizinc 2.5.4+ install and have it executable in ``$PATH``.
- You should have minizinc 2.6.0+ install and have it executable in ``$PATH``.
You can download it from [official site](https://www.minizinc.org/).
- Python 3.8+

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
setup(
name="zython",
version=read_version(),
python_requires=">=3.7",
python_requires=">=3.8",
author="Artsiom Kaltovich",
author_email="[email protected]",
description="Express constraint programming problem with python and solve it with minizinc",
Expand All @@ -30,6 +30,7 @@
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: CPython",
"License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)",
"Intended Audience :: Developers",
Expand Down
40 changes: 16 additions & 24 deletions zython/_compile/zinc/flags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import enum
from collections import UserDict
from functools import partial
from typing import Callable, Dict

from zython._compile.zinc.types import SourceCode

Expand All @@ -22,30 +22,22 @@ class Flags(enum.Flag):
float_used = enum.auto()


FLAG_TO_SRC_PREFIX = {
Flags.alldifferent: 'include "alldifferent.mzn";',
Flags.alldifferent_except_0: 'include "alldifferent_except_0.mzn";',
Flags.alldifferent_except: 'include "alldifferent_except.mzn";',
Flags.all_equal: 'include "all_equal.mzn";',
Flags.nvalue: 'include "nvalue_fn.mzn";',
Flags.circuit: 'include "circuit.mzn";',
Flags.increasing: 'include "increasing.mzn";',
Flags.strictly_increasing: 'include "strictly_increasing.mzn";',
Flags.decreasing: 'include "decreasing.mzn";',
Flags.strictly_decreasing: 'include "strictly_decreasing.mzn";',
Flags.cumulative: 'include "cumulative.mzn";',
Flags.table: 'include "table.mzn";',
}


def append(src: SourceCode, line: str):
src.appendleft(line)


class FlagProcessors(UserDict):
def __init__(self):
super().__init__()
self[Flags.float_used] = None

def __missing__(self, key):
return partial(append, line=FLAG_TO_SRC_PREFIX[key])
FLAG_PROCESSORS: Dict[Flags, Callable[[SourceCode], None]] = {
Flags.alldifferent: partial(append, line='include "alldifferent.mzn";'),
Flags.alldifferent_except_0: partial(append, line='include "alldifferent_except_0.mzn";'),
Flags.alldifferent_except: partial(append, line='include "alldifferent_except.mzn";'),
Flags.all_equal: partial(append, line='include "all_equal.mzn";'),
Flags.nvalue: partial(append, line='include "nvalue_fn.mzn";'),
Flags.circuit: partial(append, line='include "circuit.mzn";'),
Flags.increasing: partial(append, line='include "increasing.mzn";'),
Flags.strictly_increasing: partial(append, line='include "strictly_increasing.mzn";'),
Flags.decreasing: partial(append, line='include "decreasing.mzn";'),
Flags.strictly_decreasing: partial(append, line='include "strictly_decreasing.mzn";'),
Flags.cumulative: partial(append, line='include "cumulative.mzn";'),
Flags.table: partial(append, line='include "table.mzn";'),
Flags.float_used: lambda x: x,
}
11 changes: 4 additions & 7 deletions zython/_compile/zinc/zinc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Set

from zython._compile.ir import IR
from zython._compile.zinc.flags import Flags, FlagProcessors
from zython._compile.zinc.flags import Flags, FLAG_PROCESSORS
from zython._compile.zinc.to_str import to_str, _binary_op, _get_array_shape_decl
from zython._compile.zinc.types import SourceCode
from zython.operations.constraint import Constraint
Expand All @@ -15,21 +15,18 @@
def to_zinc(ir: IR):
result: SourceCode = deque()
flags: Set[Flags] = set()
flag_processors = FlagProcessors()
_process_enums(ir, result, flags)
_process_pars(ir, result, flags)
_process_vars(ir, result, flags)
_process_constraints(ir, result, flags)
_process_how_to_solve(ir, result)
_process_flags(flag_processors, flags, result)
_process_flags(flags, result)
return "\n".join(result)


def _process_flags(flag_processors: FlagProcessors, flags, result: SourceCode):
def _process_flags(flags, result: SourceCode):
for flag in flags:
pr = flag_processors.get(flag)
if pr:
pr(result)
FLAG_PROCESSORS[flag](result)


def _process_enums(ir: IR, result: SourceCode, flags: Set[Flags]) -> None:
Expand Down

0 comments on commit b06efe4

Please sign in to comment.