|
| 1 | +# constraints/extension.jl |
| 2 | + |
| 3 | +""" |
| 4 | + ExtensionConstraint{T1, T2} |
| 5 | +
|
| 6 | +Represents an extension constraint in XCSP3, defined by a list of variables and tuples. |
| 7 | +
|
| 8 | +XML Structure for non-unary positive table constraints: |
| 9 | +```xml |
| 10 | +<extension> |
| 11 | +<list> (intVar wspace)2+ </list> |
| 12 | +<supports> ("(" intVal ("," intVal)+ ")")* </supports> |
| 13 | +</extension> |
| 14 | +``` |
| 15 | +
|
| 16 | +XML Structure for non-unary negative table constraints: |
| 17 | +```xml |
| 18 | +<extension> |
| 19 | +<list> (intVar wspace)2+ </list> |
| 20 | +<conflicts> ("(" intVal ("," intVal)+ ")")* </conflicts> |
| 21 | +</extension> |
| 22 | +``` |
| 23 | +
|
| 24 | +XML Structure for unary positive table constraints: |
| 25 | +```xml |
| 26 | +<extension> |
| 27 | +<list> intVar </list> |
| 28 | +<supports> ((intVal | intIntvl) wspace)* </supports> |
| 29 | +</extension> |
| 30 | +``` |
| 31 | +
|
| 32 | +XML Structure for unary negative table constraints: |
| 33 | +```xml |
| 34 | +<extension> |
| 35 | +<list> intVar </list> |
| 36 | +<conflicts> ((intVal | intIntvl) wspace)* </conflicts> |
| 37 | +</extension> |
| 38 | +``` |
| 39 | +
|
| 40 | +Examples: |
| 41 | +```julia |
| 42 | +# Ternary constraint with supports (Example 26 from XCSP3 spec) |
| 43 | +# <extension id="c1"> |
| 44 | +# <list> x1 x2 x3 </list> |
| 45 | +# <supports> (0,1,0)(1,0,0)(1,1,0)(1,1,1) </supports> |
| 46 | +# </extension> |
| 47 | +ExtensionConstraint( |
| 48 | + id = "c1", |
| 49 | + list = ["x1", "x2", "x3"], |
| 50 | + supports = [[0, 1, 0], [1, 0, 0], [1, 1, 0], [1, 1, 1]] |
| 51 | +) |
| 52 | +
|
| 53 | +# Quaternary constraint with conflicts (Example 26 from XCSP3 spec) |
| 54 | +# <extension id="c2"> |
| 55 | +# <list> y1 y2 y3 y4 </list> |
| 56 | +# <conflicts> (1,2,3,4)(3,1,3,4) </conflicts> |
| 57 | +# </extension> |
| 58 | +ExtensionConstraint( |
| 59 | + id = "c2", |
| 60 | + list = ["y1", "y2", "y3", "y4"], |
| 61 | + conflicts = [[1, 2, 3, 4], [3, 1, 3, 4]] |
| 62 | +) |
| 63 | +
|
| 64 | +# Unary constraint with supports (Example 27 from XCSP3 spec) |
| 65 | +# <extension id="c3"> |
| 66 | +# <list> x </list> |
| 67 | +# <supports> 1 2 4 8..10 </supports> |
| 68 | +# </extension> |
| 69 | +ExtensionConstraint( |
| 70 | + id = "c3", |
| 71 | + list = ["x"], |
| 72 | + supports = [1, 2, 4, 8, 9, 10] # Note: Intervals expanded to individual values |
| 73 | +) |
| 74 | +
|
| 75 | +# Short table constraint (Example 28 from XCSP3 spec) |
| 76 | +# <extension id="c4"> |
| 77 | +# <list> z1 z2 z3 z4 </list> |
| 78 | +# <supports> (1,*,1,2)(2,1,*,*) </supports> |
| 79 | +# </extension> |
| 80 | +ExtensionConstraint( |
| 81 | + id = "c4", |
| 82 | + list = ["z1", "z2", "z3", "z4"], |
| 83 | + supports = [[1, "*", 1, 2], [2, 1, "*", "*"]] # "*" represents any value |
| 84 | +) |
| 85 | +``` |
| 86 | +""" |
| 87 | +struct ExtensionConstraint{T1, T2, T3} <: AbstractConstraint |
| 88 | + id::String # Optional ID, empty string if not provided |
| 89 | + list::Vector{T1} # Variables involved |
| 90 | + supports::Vector{T2} # List of support tuples (can be empty) |
| 91 | + conflicts::Vector{T3} # List of conflict tuples (can be empty) |
| 92 | +end |
| 93 | + |
| 94 | +# Constructor for non-unary constraints with supports |
| 95 | +function ExtensionConstraint(; |
| 96 | + id::AbstractString = "", |
| 97 | + list::Vector, |
| 98 | + supports = Vector{Vector{Number}}(), |
| 99 | + conflicts = Vector{Vector{Number}}(), |
| 100 | +) |
| 101 | + sup = supports isa Vector ? supports : Vector(supports) |
| 102 | + conf = conflicts isa Vector ? conflicts : Vector(conflicts) |
| 103 | + T1, T2, T3 = eltype(list), eltype(sup), eltype(conf) |
| 104 | + return ExtensionConstraint{T1, T2, T3}(id, list, sup, conf) |
| 105 | +end |
| 106 | + |
| 107 | +## SECTION - Test items |
| 108 | + |
| 109 | +@testitem "ExtensionConstraint" tags=[:constraints, :extension] begin |
| 110 | + import XCSP3: ExtensionConstraint |
| 111 | + |
| 112 | + # Test examples from XCSP3 specification |
| 113 | + |
| 114 | + # Example 26: Ternary constraint with supports |
| 115 | + # <extension id="c1"> |
| 116 | + # <list> x1 x2 x3 </list> |
| 117 | + # <supports> (0,1,0)(1,0,0)(1,1,0)(1,1,1) </supports> |
| 118 | + # </extension> |
| 119 | + @test begin |
| 120 | + ctr = ExtensionConstraint(; |
| 121 | + id = "c1", |
| 122 | + list = ["x1", "x2", "x3"], |
| 123 | + supports = [[0, 1, 0], [1, 0, 0], [1, 1, 0], [1, 1, 1]], |
| 124 | + ) |
| 125 | + ctr.id == "c1" && |
| 126 | + ctr.list == ["x1", "x2", "x3"] && |
| 127 | + length(ctr.supports) == 4 && |
| 128 | + isempty(ctr.conflicts) |
| 129 | + end |
| 130 | + |
| 131 | + # Example 26: Quaternary constraint with conflicts |
| 132 | + # <extension id="c2"> |
| 133 | + # <list> y1 y2 y3 y4 </list> |
| 134 | + # <conflicts> (1,2,3,4)(3,1,3,4) </conflicts> |
| 135 | + # </extension> |
| 136 | + @test begin |
| 137 | + ctr = ExtensionConstraint(; |
| 138 | + id = "c2", |
| 139 | + list = ["y1", "y2", "y3", "y4"], |
| 140 | + conflicts = [[1, 2, 3, 4], [3, 1, 3, 4]], |
| 141 | + ) |
| 142 | + ctr.id == "c2" && |
| 143 | + ctr.list == ["y1", "y2", "y3", "y4"] && |
| 144 | + isempty(ctr.supports) && |
| 145 | + length(ctr.conflicts) == 2 |
| 146 | + end |
| 147 | + |
| 148 | + # Example 27: Unary constraint with supports |
| 149 | + # <extension id="c3"> |
| 150 | + # <list> x </list> |
| 151 | + # <supports> 1 2 4 8..10 </supports> |
| 152 | + # </extension> |
| 153 | + @test begin |
| 154 | + ctr = ExtensionConstraint(; |
| 155 | + id = "c3", |
| 156 | + list = ["x"], |
| 157 | + supports = [1, 2, 4, 8, 9, 10], # Note: Intervals expanded to individual values |
| 158 | + ) |
| 159 | + ctr.id == "c3" && |
| 160 | + ctr.list == ["x"] && |
| 161 | + length(ctr.supports) == 6 && |
| 162 | + isempty(ctr.conflicts) |
| 163 | + end |
| 164 | + |
| 165 | + # Example 28: Short table constraint |
| 166 | + # <extension id="c4"> |
| 167 | + # <list> z1 z2 z3 z4 </list> |
| 168 | + # <supports> (1,*,1,2)(2,1,*,*) </supports> |
| 169 | + # </extension> |
| 170 | + @test begin |
| 171 | + ctr = ExtensionConstraint(; |
| 172 | + id = "c4", |
| 173 | + list = ["z1", "z2", "z3", "z4"], |
| 174 | + supports = [[1, "*", 1, 2], [2, 1, "*", "*"]], # "*" represents any value |
| 175 | + ) |
| 176 | + ctr.id == "c4" && |
| 177 | + ctr.list == ["z1", "z2", "z3", "z4"] && |
| 178 | + length(ctr.supports) == 2 && |
| 179 | + isempty(ctr.conflicts) |
| 180 | + end |
| 181 | +end |
0 commit comments