Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
nature.jl 8.92 KiB
### Persephone - a socio-economic-ecological model of European agricultural landscapes.
###
### This file is responsible for managing the animal modules.
###


### FUNCTIONS AND TYPES INTEGRATING THE NATURE MODEL WITH THE REST OF PERSEPHONE

## An enum used to assign a sex to each animal
@enum Sex hermaphrodite male female

"""
    Animal

This is the generic agent type for all animals. Species are differentiated
by trait dictionaries passed by them during initialisation.
"""
@agent Animal GridAgent{2} begin
    #XXX is it (performance-)wise to use a dict for the traits?
    # Doesn't this rather obviate the point of having an agent struct?
    traits::Dict{String,Any}
    sex::Sex
    age::Int32
    #XXX keep track of parents and/or offspring?
end

"""
    animalid(animal)

A small utility function to return a string with the species name and ID of an animal.
"""
function animalid(a::Animal)
    return "$(a.traits["name"]) $(a.id)"
end

"""
    stepagent!(animal, model)

Update an animal by one day, executing it's currently active phase function.
"""
function stepagent!(animal::Animal, model::AgentBasedModel)
    animal.age += 1
    animal.traits[animal.traits["phase"]](animal,model)
end

"""
    initnature!(model)

Initialise the model with all simulated animal populations.
"""
function initnature!(model::AgentBasedModel)
    # The config file determines which species are simulated in this run
    for speciesname in param("nature.targetspecies")
        species = @eval $(Symbol(speciesname))($model)
        species["initialise!"](species, model)
    end
    # Initialise the data output
    initecologicaldata()
end


### MACROS IMPLEMENTING THE DOMAIN-SPECIFIC LANGUAGE FOR DEFINING SPECIES

"""
    @species(name, body)

A macro used to create new species definitions for the nature model.
This is effectively a simple domain-specific language, establishing a
custom syntax to describe species' biology: