-
Notifications
You must be signed in to change notification settings - Fork 36
/
fzn2py
executable file
·49 lines (39 loc) · 1.63 KB
/
fzn2py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash -e
# translate flatzinc format to python/Numberjack
# usage: ./fzn2py problem.fzn | python
# remove end of line (;), annotations (except variable output), predicate and comments.
# replace a..b by range(a,1+b)
# replace false by 0 and true by 1
# replace array index a[i] by a[i-1] # BUT a[var] STILL NOT TRANSFORMED!?!
# after awk parsing, replace {} by []
SED='sed -r'
INPUT="$1"
uname=$(uname)
if [[ "$INPUT" == "" ]]; then
echo "Error: please specify an input fzn file."
exit 1
fi
pushd . > /dev/null
SCRIPT_PATH="${BASH_SOURCE[0]}";
if ([ -h "${SCRIPT_PATH}" ]) then
while([ -h "${SCRIPT_PATH}" ]) do cd `dirname "$SCRIPT_PATH"`; SCRIPT_PATH=`readlink "${SCRIPT_PATH}"`; done
fi
cd `dirname ${SCRIPT_PATH}` > /dev/null
SCRIPT_PATH=`pwd`;
popd > /dev/null
MZNNJ_DIR=$SCRIPT_PATH
if [[ "$uname" == "Darwin" ]]; then
# The version of sed on Mac has a different option to enable extended regular expressions
SED='sed -E'
if [[ "$INPUT" == "-" ]]; then
# sed on Mac will read from stdin by default, if '-' is specified it will try to open a file named '-'.
INPUT=""
fi
fi
$SED 's/;$//' $INPUT | $SED 's/:: output_/::_output_/g' | $SED 's/:: [a-zA-Z0-9_]+([(].*[)])*//g' | $SED 's/^predicate .*$//' | $SED 's/%.*$//' | $SED 's/false/0/g' | $SED 's/true/1/g' | $SED 's/([-]*[0-9]+)[.][.]([-]*[0-9]+)/range(\1,1+\2)/g' | $SED 's/([a-zA-Z0-9_]+)[[]([0-9+]+)[]]/\1[\2-1]/g' | gawk -f $MZNNJ_DIR/fzn2py.awk --assign MZNNJ_DIR=$MZNNJ_DIR | $SED 's/\{/\[/g' | $SED 's/\}/\]/g'
# Check if any of the piped statements failed.
for i in "${PIPESTATUS[@]}"; do
if [ "$i" != "0" ]; then
exit $i
fi
done