From 33513f74a8fa8ee8d2aed2fc8fed48a94ae3d0cf Mon Sep 17 00:00:00 2001 From: Thibaut Cuvelier Date: Thu, 9 Sep 2021 21:36:56 +0200 Subject: [PATCH] FZN: support several annotations when parsing. --- src/ConstraintProgrammingExtensions.jl | 3 --- src/FlatZinc/import.jl | 9 ++++++-- test/FlatZinc/import.jl | 31 +++++++++++++++++--------- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/ConstraintProgrammingExtensions.jl b/src/ConstraintProgrammingExtensions.jl index e3e8ef5f..042309db 100644 --- a/src/ConstraintProgrammingExtensions.jl +++ b/src/ConstraintProgrammingExtensions.jl @@ -28,7 +28,4 @@ include("Test/Test.jl") include("FlatZinc/FlatZinc.jl") include("XCSP/XCSP.jl") -# Compatibility layer. -@deprecate Reified Reification false - end diff --git a/src/FlatZinc/import.jl b/src/FlatZinc/import.jl index d2355fd9..223cfdcf 100644 --- a/src/FlatZinc/import.jl +++ b/src/FlatZinc/import.jl @@ -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 @@ -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) @@ -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) diff --git a/test/FlatZinc/import.jl b/test/FlatZinc/import.jl index 824292f6..7c02e818 100644 --- a/test/FlatZinc/import.jl +++ b/test/FlatZinc/import.jl @@ -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. @@ -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). @@ -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). @@ -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). @@ -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). @@ -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. @@ -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. @@ -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. @@ -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. @@ -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