Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jp/add code autogen #12

Merged
merged 10 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/PowerSystemsInvestmentsPortfolios.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export add_technology!
export add_technologies!
export read_json_data
export generate_invest_structs
export generate_structs

const PSY = PowerSystems
const IS = InfrastructureSystems
Expand Down
60 changes: 45 additions & 15 deletions src/generate_structs.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
using JSON3
using JSONSchema
using Mustache
using InfrastructureSystems
using PowerSystems

const IS = InfrastructureSystems
const PSY = PowerSystems

function read_json_data(filename::String)
try
return JSONSchema.Schema(JSON3.read(filename))
Expand All @@ -6,30 +15,44 @@ function read_json_data(filename::String)
end
end

function generate_invest_structs(directory, data::Dict{String, Any}; print_results=true)
function generate_invest_structs(directory, data::Schema; print_results=true)
struct_names = Vector{String}()
unique_accessor_functions = Set{String}()
unique_setter_functions = Set{String}()

for (struc_name, input) in data
for (struct_name, input) in data.data["\$defs"]

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

properties = input["properties"]
item = Dict{String, Any}()
item["has_internal"] = true
item["has_internal"] = false
item["has_null_values"] = true
item["supertype"] = input["supertype"]

accessors = Vector{Dict}()
setters = Vector{Dict}()
item["has_null_values"] = true
has_non_default_values = false

item["has_non_default_values"] = false

item["constructor_func"] = struct_name
item["struct_name"] = struct_name
item["closing_constructor_text"] = ""
item["parametric"] = properties["power_systems_type"]
item["constructor_func"] *= "{T}"
item["closing_constructor_text"] = " where T <: $(item["parametric"])"

item["parametric"] = input["parametric"]
if haskey(item, "parametric")
item["constructor_func"] *= "{T}"
item["closing_constructor_text"] = " where T <: $(item["parametric"])"
end


Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

parameters = Vector{Dict}()
for (field, values) in properties
param = Dict{String, Any}()

param["struct_name"] = item["struct_name"]
param["name"] = field
param["data_type"] = values["type"]
param["comment"] = values["description"]

if haskey(param, "valid_range")
if typeof(param["valid_range"]) == Dict{String, Any}
min = param["valid_range"]["min"]
Expand All @@ -52,8 +75,10 @@ function generate_invest_structs(directory, data::Dict{String, Any}; print_resul
accessor_module = ""
create_docstring = true
end

accessor_name = accessor_module * "get_" * param["name"]
setter_name = accessor_module * "set_" * param["name"] * "!"

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

push!(
accessors,
Dict(
Expand All @@ -63,6 +88,7 @@ function generate_invest_structs(directory, data::Dict{String, Any}; print_resul
"needs_conversion" => get(param, "needs_conversion", false),
),
)

include_setter = !get(param, "exclude_setter", false)
if include_setter
push!(
Expand All @@ -76,7 +102,8 @@ function generate_invest_structs(directory, data::Dict{String, Any}; print_resul
),
)
end
if field["name"] != "internal" && accessor_module == ""

if field != "internal" && accessor_module == ""
push!(unique_accessor_functions, accessor_name)
push!(unique_setter_functions, setter_name)
end
Expand All @@ -86,10 +113,10 @@ function generate_invest_structs(directory, data::Dict{String, Any}; print_resul
param["kwarg_value"] = "=" * param["default"]
elseif !isnothing(get(param, "internal_default", nothing))
param["kwarg_value"] = "=" * string(param["internal_default"])
has_internal = true
item["has_internal"] = true
continue
else
has_non_default_values = true
item["has_non_default_values"] = true
end

# This controls whether a demo constructor will be generated.
Expand All @@ -109,22 +136,24 @@ function generate_invest_structs(directory, data::Dict{String, Any}; print_resul
item["parameters"] = parameters
item["accessors"] = accessors
item["setters"] = setters

# If all parameters have defaults then the positional constructor will
# collide with the kwarg constructor.
item["needs_positional_constructor"] = has_internal && has_non_default_values
item["needs_positional_constructor"] = item["has_internal"] && item["has_non_default_values"]

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
item["needs_positional_constructor"] = item["has_internal"] && item["has_non_default_values"]
item["needs_positional_constructor"] =
item["has_internal"] && item["has_non_default_values"]


filename = joinpath(directory, item["struct_name"] * ".jl")

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

open(filename, "w") do io
write(io, strip(Mustache.render(IS.STRUCT_TEMPLATE, item)))
write(io, "\n")
push!(struct_names, item["struct_name"])
end

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

if print_results
println("Wrote $filename")
end
end

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

accessors = sort!(collect(unique_accessor_functions))
setters = sort!(collect(unique_setter_functions))
filename = joinpath(directory, "includes.jl")
Expand All @@ -144,6 +173,7 @@ function generate_invest_structs(directory, data::Dict{String, Any}; print_resul
println("Wrote $filename")
end
end

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

end

function generate_structs(
Expand All @@ -157,6 +187,6 @@ function generate_structs(
end

data = read_json_data(input_file)
generate_structs(output_directory, data; print_results=print_results)
generate_invest_structs(output_directory, data; print_results=print_results)
return
end
71 changes: 71 additions & 0 deletions src/models/generated/DemandRequirement.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#=
This file is auto-generated. Do not edit.
=#

#! format: off

"""
mutable struct DemandRequirement{T <: PSY.StaticInjection} <: InfrastructureSystemsComponent
load_growth::Float
name::String
power_systems_type::String
region::String
available::Boolean
peak_load::Float
end



# Arguments
- `load_growth::Float`: Annual load growth (%)
- `name::String`: The technology name
- `power_systems_type::String`: maps to a valid PowerSystems.jl for PCM modeling
- `region::String`: Corresponding region for peak demand
- `available::Boolean`: identifies whether the technology is available
- `peak_load::Float`: Demand value (MW) for single timepoint (for now)
"""
mutable struct DemandRequirement{T <: PSY.StaticInjection} <: InfrastructureSystemsComponent
"Annual load growth (%)"
load_growth::Float
"The technology name"
name::String
"maps to a valid PowerSystems.jl for PCM modeling"
power_systems_type::String
"Corresponding region for peak demand"
region::String
"identifies whether the technology is available"
available::Boolean
"Demand value (MW) for single timepoint (for now)"
peak_load::Float
end


function DemandRequirement{T}(; load_growth, name, power_systems_type, region, available, peak_load, ) where T <: PSY.StaticInjection
DemandRequirement{T}(load_growth, name, power_systems_type, region, available, peak_load, )
end

"""Get [`DemandRequirement`](@ref) `load_growth`."""
get_load_growth(value::DemandRequirement) = value.load_growth
"""Get [`DemandRequirement`](@ref) `name`."""
get_name(value::DemandRequirement) = value.name
"""Get [`DemandRequirement`](@ref) `power_systems_type`."""
get_power_systems_type(value::DemandRequirement) = value.power_systems_type
"""Get [`DemandRequirement`](@ref) `region`."""
get_region(value::DemandRequirement) = value.region
"""Get [`DemandRequirement`](@ref) `available`."""
get_available(value::DemandRequirement) = value.available
"""Get [`DemandRequirement`](@ref) `peak_load`."""
get_peak_load(value::DemandRequirement) = value.peak_load

"""Set [`DemandRequirement`](@ref) `load_growth`."""
set_load_growth!(value::DemandRequirement, val) = value.load_growth = val
"""Set [`DemandRequirement`](@ref) `name`."""
set_name!(value::DemandRequirement, val) = value.name = val
"""Set [`DemandRequirement`](@ref) `power_systems_type`."""
set_power_systems_type!(value::DemandRequirement, val) = value.power_systems_type = val
"""Set [`DemandRequirement`](@ref) `region`."""
set_region!(value::DemandRequirement, val) = value.region = val
"""Set [`DemandRequirement`](@ref) `available`."""
set_available!(value::DemandRequirement, val) = value.available = val
"""Set [`DemandRequirement`](@ref) `peak_load`."""
set_peak_load!(value::DemandRequirement, val) = value.peak_load = val
47 changes: 47 additions & 0 deletions src/models/generated/DemandsideTechnology.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#=
This file is auto-generated. Do not edit.
=#

#! format: off

"""
mutable struct DemandsideTechnology{T <: PSY.StaticInjection} <: InfrastructureSystemsComponent
name::String
power_systems_type::String
available::Boolean
end



# Arguments
- `name::String`: The technology name
- `power_systems_type::String`: maps to a valid PowerSystems.jl for PCM modeling
- `available::Boolean`: identifies whether the technology is available
"""
mutable struct DemandsideTechnology{T <: PSY.StaticInjection} <: InfrastructureSystemsComponent
"The technology name"
name::String
"maps to a valid PowerSystems.jl for PCM modeling"
power_systems_type::String
"identifies whether the technology is available"
available::Boolean
end


function DemandsideTechnology{T}(; name, power_systems_type, available, ) where T <: PSY.StaticInjection
DemandsideTechnology{T}(name, power_systems_type, available, )
end

"""Get [`DemandsideTechnology`](@ref) `name`."""
get_name(value::DemandsideTechnology) = value.name
"""Get [`DemandsideTechnology`](@ref) `power_systems_type`."""
get_power_systems_type(value::DemandsideTechnology) = value.power_systems_type
"""Get [`DemandsideTechnology`](@ref) `available`."""
get_available(value::DemandsideTechnology) = value.available

"""Set [`DemandsideTechnology`](@ref) `name`."""
set_name!(value::DemandsideTechnology, val) = value.name = val
"""Set [`DemandsideTechnology`](@ref) `power_systems_type`."""
set_power_systems_type!(value::DemandsideTechnology, val) = value.power_systems_type = val
"""Set [`DemandsideTechnology`](@ref) `available`."""
set_available!(value::DemandsideTechnology, val) = value.available = val
55 changes: 55 additions & 0 deletions src/models/generated/StorageTechnology.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#=
This file is auto-generated. Do not edit.
=#

#! format: off

"""
mutable struct StorageTechnology{T <: PSY.Storage} <: InfrastructureSystemsComponent
name::String
storage_tech::String
power_systems_type::String
available::Boolean
end



# Arguments
- `name::String`: The technology name
- `storage_tech::String`: Storage Technology Type
- `power_systems_type::String`: maps to a valid PowerSystems.jl for PCM modeling
- `available::Boolean`: identifies whether the technology is available
"""
mutable struct StorageTechnology{T <: PSY.Storage} <: InfrastructureSystemsComponent
"The technology name"
name::String
"Storage Technology Type"
storage_tech::String
"maps to a valid PowerSystems.jl for PCM modeling"
power_systems_type::String
"identifies whether the technology is available"
available::Boolean
end


function StorageTechnology{T}(; name, storage_tech, power_systems_type, available, ) where T <: PSY.Storage
StorageTechnology{T}(name, storage_tech, power_systems_type, available, )
end

"""Get [`StorageTechnology`](@ref) `name`."""
get_name(value::StorageTechnology) = value.name
"""Get [`StorageTechnology`](@ref) `storage_tech`."""
get_storage_tech(value::StorageTechnology) = value.storage_tech
"""Get [`StorageTechnology`](@ref) `power_systems_type`."""
get_power_systems_type(value::StorageTechnology) = value.power_systems_type
"""Get [`StorageTechnology`](@ref) `available`."""
get_available(value::StorageTechnology) = value.available

"""Set [`StorageTechnology`](@ref) `name`."""
set_name!(value::StorageTechnology, val) = value.name = val
"""Set [`StorageTechnology`](@ref) `storage_tech`."""
set_storage_tech!(value::StorageTechnology, val) = value.storage_tech = val
"""Set [`StorageTechnology`](@ref) `power_systems_type`."""
set_power_systems_type!(value::StorageTechnology, val) = value.power_systems_type = val
"""Set [`StorageTechnology`](@ref) `available`."""
set_available!(value::StorageTechnology, val) = value.available = val
Loading
Loading