Skip to content

Commit 5369229

Browse files
authored
Pretty printing (#35)
* Add a pretty print function for parameters used by a collection of constraints * Add pretty printing for parameters and descriptions. * New version
1 parent 0235100 commit 5369229

File tree

7 files changed

+73
-8
lines changed

7 files changed

+73
-8
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Constraints"
22
uuid = "30f324ab-b02d-43f0-b619-e131c61659f7"
33
authors = ["Jean-François Baffier"]
4-
version = "0.5.1"
4+
version = "0.5.2"
55

66
[deps]
77
CompositionalNetworks = "4b67e4b5-442d-4ef5-b760-3f5df3a57537"

src/Constraints.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using PrettyTables
1010
using TestItemRunner
1111
using TestItems
1212

13-
import ConstraintCommons: extract_parameters
13+
import ConstraintCommons: extract_parameters, USUAL_CONSTRAINT_PARAMETERS
1414

1515
# Exports
1616
export Constraint
@@ -20,6 +20,8 @@ export USUAL_SYMMETRIES
2020

2121
export args
2222
export concept
23+
export constraints_parameters
24+
export constraints_descriptions
2325
export describe
2426
export error_f
2527
export extract_parameters

src/constraints/all_equal.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ concept_all_equal(x, val) = all(y -> y == val, x)
66

77
xcsp_all_equal(; list) = concept_all_equal(list; val=first(list))
88

9-
@usual concept_all_equal(x; val=nothing) = concept_all_equal(x, val)
10-
9+
@usual function concept_all_equal(x; val=nothing, pair_vars=zero(x))
10+
return concept_all_equal(x+pair_vars, val)
11+
end
1112
concept_all_equal(x, ::Nothing) = xcsp_all_equal(list=x)

src/constraints/extension.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
##!NOTE - constraints of type extension represent a list of supported or conflicted tuples
22

3+
# TODO - Better input for conflicts and supports (cf XCSP3-core)
4+
35
xcsp_extension(list, ::Nothing, conflicts) = list conflicts
46
xcsp_extension(list, supports, _) = list supports
57

@@ -8,16 +10,16 @@ function xcsp_extension(; list, supports=nothing, conflicts=nothing)
810
end
911

1012
function concept_extension(x, pair_vars::Vector{T}) where {T <: Number}
11-
return xcsp_extension(list = x, supports = pair_vars[1], conflicts = nothing)
13+
return xcsp_extension(list = x, supports = pair_vars)
1214
end
1315

1416
function concept_extension(x, pair_vars)
1517
supports = pair_vars[1]
1618
conflicts = pair_vars[2]
17-
return xcsp_extension(; list = x, supports, conflicts)
19+
return xcsp_extension(; list = x, supports) || xcsp_extension(; list = x, conflicts)
1820
end
1921

20-
const description_extension = """Global constraint ensuring that all ...`"""
22+
const description_extension = """Global constraint ensuring that `x` is in the set of configurations given by `pair_vars[1]` and not in the set given by `pair_vars[2]`: `x ∈ pair_vars[1] || x ∉ pair_vars[2]`"""
2123

2224
@usual concept_extension(x; pair_vars) = concept_extension(x, pair_vars)
2325

src/constraints/intention.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!NOTE - constraints of type intension represent general predicate over a set of variables
22

3+
# TODO - Add a DSL for intension (cf XCSP3-core)
4+
35
const description_dist_different = """Local constraint ensuring that `concept(dist_different, x) = |x[1] - x[2]| ≠ |x[3] - x[4]|`"""
46

57
"""

src/constraints/sum.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
xcsp_sum(; list, coeffs, condition) = condition[1](sum(coeffs .* list), condition[2])
22

3-
const description_sum = """Global constraint ensuring that ..."""
3+
const description_sum = """Global constraint ensuring that `op(sum(x *. pair_vars), val)` is true. Defaults to `op = ==` and `pair_vars = ones(eltype(x), length(x))`"""
44

55
@usual function concept_sum(x; op = ==, pair_vars = ones(eltype(x), length(x)), val)
66
return xcsp_sum(list = x, coeffs = pair_vars, condition = (op, val))

src/usual_constraints.jl

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,64 @@ macro usual(ex::Expr)
7979
return nothing
8080
end
8181

82+
function constraints_parameters(C=USUAL_CONSTRAINTS)
83+
df = DataFrame(Constraint=Symbol[], bool=String[], dim=String[], id=String[], language=String[], op=String[], pair_vars=String[], val=String[], vals=String[])
84+
85+
for (s,c) in C
86+
base = vcat([s], fill("", length(USUAL_CONSTRAINT_PARAMETERS)))
87+
for P in c.params
88+
push!(df, base)
89+
for (p, b) in P
90+
df[end, p] = b == 1 ? "o" : "×"
91+
end
92+
end
93+
end
94+
95+
sort!(df)
96+
97+
hl_odd = Highlighter(
98+
f = (data, i, j) -> i % 2 == 0,
99+
crayon = Crayon(background = :light_blue),
100+
)
101+
102+
return pretty_table(
103+
df;
104+
highlighters = hl_odd,
105+
header_crayon = crayon"yellow bold",
106+
title = "Available parameters per constraint (× -> required, o -> optional)",
107+
show_subheader=false,
108+
crop=:none,
109+
)
110+
end
111+
112+
function constraints_descriptions(C=USUAL_CONSTRAINTS)
113+
df = DataFrame(Constraint=Symbol[], Description=String[])
114+
115+
for (s,c) in C
116+
push!(df, [s, c.description])
117+
end
118+
119+
sort!(df)
120+
121+
hl_odd = Highlighter(
122+
f = (data, i, j) -> i % 2 == 0,
123+
crayon = Crayon(background = :light_blue),
124+
)
125+
126+
return pretty_table(
127+
df;
128+
# highlighters = hl_odd,
129+
header_crayon = crayon"yellow bold",
130+
autowrap = true,
131+
linebreaks = true,
132+
columns_width = [0,80],
133+
hlines = :all,
134+
alignment=:l,
135+
show_subheader=false,
136+
crop=:none,
137+
)
138+
end
139+
82140
## SECTION - Test Items
83141
@testitem "Usual constraints" tags = [:usual, :constraints] default_imports=false begin
84142
using ConstraintDomains

0 commit comments

Comments
 (0)