diff --git a/src/instance.jl b/src/instance.jl index f22df96..1d7190c 100644 --- a/src/instance.jl +++ b/src/instance.jl @@ -54,12 +54,7 @@ mutable struct Instance plants::Array{Plant, 1} end - -function load(path::String)::Instance - basedir = dirname(@__FILE__) - json = JSON.parsefile(path) - schema = Schema(JSON.parsefile("$basedir/schemas/input.json")) - +function validate(json, schema) result = JSONSchema.validate(json, schema) if result !== nothing if result isa JSONSchema.SingleIssue @@ -73,8 +68,20 @@ function load(path::String)::Instance end throw(msg) end +end + + +function load(path::String)::Instance + basedir = dirname(@__FILE__) + json = JSON.parsefile(path) + json_schema = JSON.parsefile("$basedir/schemas/input.json") + validate(json, Schema(json_schema)) T = json["parameters"]["time horizon (years)"] + json_schema["definitions"]["TimeSeries"]["minItems"] = T + json_schema["definitions"]["TimeSeries"]["maxItems"] = T + validate(json, Schema(json_schema)) + plants = Plant[] products = Product[] collection_centers = CollectionCenter[] diff --git a/test/fixtures/s1-wrong-length.json b/test/fixtures/s1-wrong-length.json new file mode 100644 index 0000000..1cca6cc --- /dev/null +++ b/test/fixtures/s1-wrong-length.json @@ -0,0 +1,202 @@ +{ + "parameters": { + "time horizon (years)": 2 + }, + "products": { + "P1": { + "transportation cost ($/km/tonne)": [0.015, 0.015], + "transportation energy (J/km/tonne)": [0.12, 0.11], + "transportation emissions (tonne/km/tonne)": { + "CO2": [0.052], + "CH4": [0.003, 0.002] + }, + "initial amounts": { + "C1": { + "latitude (deg)": 7.0, + "longitude (deg)": 7.0, + "amount (tonne)": [934.56, 934.56] + }, + "C2": { + "latitude (deg)": 7.0, + "longitude (deg)": 19.0, + "amount (tonne)": [198.95, 198.95] + }, + "C3": { + "latitude (deg)": 84.0, + "longitude (deg)": 76.0, + "amount (tonne)": [212.97, 212.97] + }, + "C4": { + "latitude (deg)": 21.0, + "longitude (deg)": 16.0, + "amount (tonne)": [352.19, 352.19] + }, + "C5": { + "latitude (deg)": 32.0, + "longitude (deg)": 92.0, + "amount (tonne)": [510.33, 510.33] + }, + "C6": { + "latitude (deg)": 14.0, + "longitude (deg)": 62.0, + "amount (tonne)": [471.66, 471.66] + }, + "C7": { + "latitude (deg)": 30.0, + "longitude (deg)": 83.0, + "amount (tonne)": [785.21, 785.21] + }, + "C8": { + "latitude (deg)": 35.0, + "longitude (deg)": 40.0, + "amount (tonne)": [706.17, 706.17] + }, + "C9": { + "latitude (deg)": 74.0, + "longitude (deg)": 52.0, + "amount (tonne)": [30.08, 30.08] + }, + "C10": { + "latitude (deg)": 22.0, + "longitude (deg)": 54.0, + "amount (tonne)": [536.52, 536.52] + } + } + }, + "P2": { + "transportation cost ($/km/tonne)": [0.02, 0.02] + }, + "P3": { + "transportation cost ($/km/tonne)": [0.0125, 0.0125] + }, + "P4": { + "transportation cost ($/km/tonne)": [0.0175, 0.0175] + } + }, + "plants": { + "F1": { + "input": "P1", + "outputs (tonne/tonne)": { + "P2": 0.2, + "P3": 0.5 + }, + "energy (GJ/tonne)": [0.12, 0.11], + "emissions (tonne/tonne)": { + "CO2": [0.052, 0.050], + "CH4": [0.003, 0.002] + }, + "locations": { + "L1": { + "latitude (deg)": 0.0, + "longitude (deg)": 0.0, + "disposal": { + "P2": { + "cost ($/tonne)": [-10.0, -10.0], + "limit (tonne)": [1.0, 1.0] + }, + "P3": { + "cost ($/tonne)": [-10.0, -10.0], + "limit (tonne)": [1.0, 1.0] + } + }, + "capacities (tonne)": { + "250.0": { + "opening cost ($)": [500.0, 500.0], + "fixed operating cost ($)": [30.0, 30.0], + "variable operating cost ($/tonne)": [30.0, 30.0] + }, + "1000.0": { + "opening cost ($)": [1250.0, 1250.0], + "fixed operating cost ($)": [30.0, 30.0], + "variable operating cost ($/tonne)": [30.0, 30.0] + } + } + }, + "L2": { + "latitude (deg)": 0.5, + "longitude (deg)": 0.5, + "capacities (tonne)": { + "0.0": { + "opening cost ($)": [1000, 1000], + "fixed operating cost ($)": [50.0, 50.0], + "variable operating cost ($/tonne)": [50.0, 50.0] + }, + "10000.0": { + "opening cost ($)": [10000, 10000], + "fixed operating cost ($)": [50.0, 50.0], + "variable operating cost ($/tonne)": [50.0, 50.0] + } + } + } + } + }, + "F2": { + "input": "P2", + "outputs (tonne/tonne)": { + "P3": 0.05, + "P4": 0.80 + }, + "locations": { + "L3": { + "latitude (deg)": 25.0, + "longitude (deg)": 65.0, + "disposal": { + "P3": { + "cost ($/tonne)": [100.0, 100.0] + } + }, + "capacities (tonne)": { + "1000.0": { + "opening cost ($)": [3000, 3000], + "fixed operating cost ($)": [50.0, 50.0], + "variable operating cost ($/tonne)": [50.0, 50.0] + } + } + }, + "L4": { + "latitude (deg)": 0.75, + "longitude (deg)": 0.20, + "capacities (tonne)": { + "10000": { + "opening cost ($)": [3000, 3000], + "fixed operating cost ($)": [50.0, 50.0], + "variable operating cost ($/tonne)": [50.0, 50.0] + } + } + } + } + }, + "F3": { + "input": "P4", + "locations": { + "L5": { + "latitude (deg)": 100.0, + "longitude (deg)": 100.0, + "capacities (tonne)": { + "15000": { + "opening cost ($)": [0.0, 0.0], + "fixed operating cost ($)": [0.0, 0.0], + "variable operating cost ($/tonne)": [-15.0, -15.0] + } + } + } + } + }, + "F4": { + "input": "P3", + "locations": { + "L6": { + "latitude (deg)": 50.0, + "longitude (deg)": 50.0, + "capacities (tonne)": { + "10000": { + "opening cost ($)": [0.0, 0.0], + "fixed operating cost ($)": [0.0, 0.0], + "variable operating cost ($/tonne)": [-15.0, -15.0] + } + } + } + } + } + } +} \ No newline at end of file diff --git a/test/instance_test.jl b/test/instance_test.jl index 8e5bc5b..3f712a7 100644 --- a/test/instance_test.jl +++ b/test/instance_test.jl @@ -71,5 +71,9 @@ using RELOG @test plant.disposal_limit[p3] == [1e8, 1e8] @test plant.disposal_limit[p4] == [0, 0] end + + @testset "validate timeseries" begin + @test_throws String RELOG.load("fixtures/s1-wrong-length.json") + end end