chocoteam / choco-solver

An open-source Java library for Constraint Programming
http://choco-solver.org/
BSD 4-Clause "Original" or "Old" License
683 stars 137 forks source link

[BUG] fzn-choco.exe doesn't work #1097

Closed SYury closed 1 week ago

SYury commented 1 month ago

Describe the bug I tried adding Choco to MiniZinc IDE on Windows and encountered several problems with fzn-choco.exe script:

  1. While the documentation suggests adding it to config file as-is, in practice it doesn't work because despite the .exe extension it's actually a bat script, so the execution fails.
  2. The multi-line usage description is not parsed correctly as a multi-line variable.
  3. There is some problem with if-else syntax in argument parsing, the script dies with The syntax of the command is incorrect.. And even if it's fixed, there also is a logical error in the parsing, it considers path to flatzinc file as unrecognized argument.
  4. There are various small problems with setting variables and executing java, too many to describe every problem separately.

I'm not sure if the script is just wrong or some problems are caused by my Windows installation,

To Reproduce Try adding Choco to MiniZinc on Windows.

Expected behavior Everything should work.

Possible solution I managed to make it work with trial and error. It's quite ugly and I'm not sure I found every possible bug, but I can make a pull request if you want.

Environment (please complete the following information):

cprudhom commented 1 month ago

I have no doubt that the script can be improved in many ways. The fact is I have no computer with Windows running on it, so I tried to adapt it from the shell file I use (fzn-choco.sh file) using Copilot and a plugin from IntelliJ 😄. I could possibly look around for a PC and really test the script.

cprudhom commented 3 weeks ago

Hello I started to study an alternative in Python. The main problem was due to MiniZincIDE that only support executable MiniZinc doc.

But Nuitka makes possible to turn a Python app into an executable. It seems quite straightforward.

I suggest using the Python script instead of the bash script. This lets you run choco-solver with MiniZinc options outside MiniZincIDE. You can also integrate the executable you create with Nuitka directly into MiniZincIDE.

cprudhom commented 2 weeks ago

I pushed some changes. You can have a look (master branch) at MINIZINC.md that explains hopefully clearly enough how to generate the executable for MiniZinc. Would you accept to test the solution?

SYury commented 2 weeks ago

Thank you, I'll test it when I have time.

SYury commented 2 weeks ago

I tested the script and I have two comments:

  1. In line 109 the class path is passed as -cp .:{args.jar_file} and I had to remove .: to make it work on Windows
  2. Using nuitka turned out to be quite inconvenient because it downloads a C compiler to build the executable. Fortunately, it's possible to wrap the python script in a simple .bat:
    set "PY=%~dp0fzn-choco.py"
    python3 %PY% %*

    and passing this bat script to minizinc works. Maybe it should be added somewhere in the docs.

cprudhom commented 2 weeks ago

It seems a simpler alternative to maintain. I will simply remove the nuitka steps and add 2 files:

fzn-choco.sh

#!/bin/bash

# Call the python script with the arguments passed to this script
python3 fzn-choco.py "$@"

fzn-choco.bat

@echo off
:: Call the python script with the arguments passed to this script
set "PY=%~dp0fzn-choco.py"
python3 %PY% %*

and update the documentation and remove the .: from fzn-choco.py.