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 all 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
8 changes: 6 additions & 2 deletions src/PowerSystemsInvestmentsPortfolios.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import InfrastructureSystems:
CompressionTypes,
MultiLogger,
LogEventTracker,
StructField
StructField,
InfrastructureSystemsComponent

import PowerSystems
import JSONSchema
Expand All @@ -35,11 +36,14 @@ export add_technology!
export add_technologies!
export read_json_data
export generate_invest_structs
export generate_structs

const PSY = PowerSystems
const IS = InfrastructureSystems
const MU = IS.Mustache

include("models/includes.jl")
include("models/technologies.jl")
include("models/generated/includes.jl")
include("portfolio.jl")
include("generate_structs.jl")

Expand Down
47 changes: 33 additions & 14 deletions src/generate_structs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,42 @@ 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::JSONSchema.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"]
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

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 +64,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"] * "!"

push!(
accessors,
Dict(
Expand All @@ -63,6 +77,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 +91,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 +102,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,13 +125,16 @@ 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"]

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

open(filename, "w") do io
write(io, strip(Mustache.render(IS.STRUCT_TEMPLATE, item)))
write(io, strip(MU.render(IS.STRUCT_TEMPLATE, item)))
write(io, "\n")
push!(struct_names, item["struct_name"])
end
Expand Down Expand Up @@ -157,6 +176,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
9 changes: 0 additions & 9 deletions src/models/demand_requirement.jl

This file was deleted.

9 changes: 0 additions & 9 deletions src/models/demand_side.jl

This file was deleted.

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::Float64
name::String
power_systems_type::String
region::String
available::Bool
peak_load::Float64
end



# Arguments
- `load_growth::Float64`: 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::Bool`: identifies whether the technology is available
- `peak_load::Float64`: Demand value (MW) for single timepoint (for now)
"""
mutable struct DemandRequirement{T <: PSY.StaticInjection} <: InfrastructureSystemsComponent
"Annual load growth (%)"
load_growth::Float64
"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::Bool
"Demand value (MW) for single timepoint (for now)"
peak_load::Float64
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::Bool
end



# Arguments
- `name::String`: The technology name
- `power_systems_type::String`: maps to a valid PowerSystems.jl for PCM modeling
- `available::Bool`: 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::Bool
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::Bool
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::Bool`: 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::Bool
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