Skip to content

Commit

Permalink
FZN: support several annotations when parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
dourouc05 committed Sep 9, 2021
1 parent ea9b5dd commit 33513f7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
3 changes: 0 additions & 3 deletions src/ConstraintProgrammingExtensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,4 @@ include("Test/Test.jl")
include("FlatZinc/FlatZinc.jl")
include("XCSP/XCSP.jl")

# Compatibility layer.
@deprecate Reified Reification false

end
9 changes: 7 additions & 2 deletions src/FlatZinc/import.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,7 @@ function split_variable(item::AbstractString)
# Typical input: "var int: x1;" -> scalar
# Complex input: "array [1..5] of var int: x1;" -> array
# Complex input: "var int: x1 :: some_annotation = some_value;" -> scalar
# Complex input: "var int: x1 :: some_annotation :: some_other_annotation;" -> scalar

@assert length(item) > 5

Expand Down Expand Up @@ -1713,7 +1714,8 @@ function split_variable_scalar(item::AbstractString)
item = lstrip(item)

# Potentially split on the double colon (::) to detect annotations, then
# on the equal (=) to detect literal values.
# on the equal (=) to detect literal values. There may be several
# annotations.
if occursin("::", item)
var_name, item = split(item, "::", limit=2)
var_name = strip(var_name)
Expand All @@ -1727,8 +1729,11 @@ function split_variable_scalar(item::AbstractString)
var_annotations = strip(item)
var_value = ""
end

var_annotations = split(var_annotations, "::")
var_annotations = map(strip, var_annotations)
else
var_annotations = ""
var_annotations = [""]

if occursin('=', item)
var_name, var_value = split(item, '=', limit=2)
Expand Down
31 changes: 21 additions & 10 deletions test/FlatZinc/import.jl
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@
@test var_array == ""
@test var_type == "int"
@test var_name == "x1"
@test var_annotations == ""
@test var_annotations == [""]
@test var_value == ""

# With another type (still a string) and an *invalid* variable name.
Expand All @@ -293,7 +293,7 @@
@test var_array == ""
@test var_type == "bool"
@test var_name == "5454"
@test var_annotations == ""
@test var_annotations == [""]
@test var_value == ""

# With another type (range of integers).
Expand All @@ -302,7 +302,7 @@
@test var_array == ""
@test var_type == "4..8"
@test var_name == "x1"
@test var_annotations == ""
@test var_annotations == [""]
@test var_value == ""

# With another type (range of floats).
Expand All @@ -311,7 +311,7 @@
@test var_array == ""
@test var_type == "4.5..8.4"
@test var_name == "x1"
@test var_annotations == ""
@test var_annotations == [""]
@test var_value == ""

# With another type (set of floats, intension).
Expand All @@ -320,7 +320,7 @@
@test var_array == ""
@test var_type == "set of 4.5..8.4"
@test var_name == "x1"
@test var_annotations == ""
@test var_annotations == [""]
@test var_value == ""

# With another type (set of floats, extension).
Expand All @@ -329,7 +329,7 @@
@test var_array == ""
@test var_type == "set of {4.5, 8.4}"
@test var_name == "x1"
@test var_annotations == ""
@test var_annotations == [""]
@test var_value == ""

# With annotations.
Expand All @@ -340,7 +340,18 @@
@test var_array == ""
@test var_type == "int"
@test var_name == "x1"
@test var_annotations == "some_annotation"
@test var_annotations == ["some_annotation"]
@test var_value == ""

# With several annotations.
var_array, var_type, var_name, var_annotations, var_value =
CP.FlatZinc.split_variable(
"var int: x1 :: some_annotation :: some_other_annotation;",
)
@test var_array == ""
@test var_type == "int"
@test var_name == "x1"
@test var_annotations == ["some_annotation", "some_other_annotation"]
@test var_value == ""

# With value.
Expand All @@ -349,7 +360,7 @@
@test var_array == ""
@test var_type == "int"
@test var_name == "x1"
@test var_annotations == ""
@test var_annotations == [""]
@test var_value == "some_value"

# With annotations and value.
Expand All @@ -360,7 +371,7 @@
@test var_array == ""
@test var_type == "int"
@test var_name == "x1"
@test var_annotations == "some_annotation"
@test var_annotations == ["some_annotation"]
@test var_value == "some_value"

# Array declaration.
Expand All @@ -369,7 +380,7 @@
@test var_array == "[1..5]"
@test var_type == "int"
@test var_name == "x1"
@test var_annotations == ""
@test var_annotations == [""]
@test var_value == ""
end

Expand Down

2 comments on commit 33513f7

@dourouc05
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/44599

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.0 -m "<description of version>" 33513f74a8fa8ee8d2aed2fc8fed48a94ae3d0cf
git push origin v0.5.0

Please sign in to comment.