-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigInterpreter.go
98 lines (77 loc) · 2.83 KB
/
ConfigInterpreter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) 2019 Australian Rivers Institute.
package interpreter
import (
appData "github.com/LindsayBradford/crem/cmd/cremexplorer/config/data"
"github.com/LindsayBradford/crem/internal/pkg/annealing"
"github.com/LindsayBradford/crem/internal/pkg/annealing/annealers"
"github.com/LindsayBradford/crem/internal/pkg/config/data"
"github.com/LindsayBradford/crem/internal/pkg/config/interpreter"
"github.com/LindsayBradford/crem/internal/pkg/model"
"github.com/LindsayBradford/crem/internal/pkg/scenario"
assert "github.com/LindsayBradford/crem/pkg/assert/debug"
compositeErrors "github.com/LindsayBradford/crem/pkg/errors"
"github.com/pkg/errors"
)
func NewInterpreter() *ConfigInterpreter {
newInterpreter := new(ConfigInterpreter).initialise()
return newInterpreter
}
type ConfigInterpreter struct {
errors *compositeErrors.CompositeError
modelInterpreter *interpreter.ModelConfigInterpreter
model model.Model
annealerInterpreter *interpreter.AnnealerConfigInterpreter
annealer annealing.Annealer
scenarioInterpreter *ScenarioConfigInterpreter
scenario scenario.Scenario
}
func (i *ConfigInterpreter) initialise() *ConfigInterpreter {
i.errors = compositeErrors.New("Configuration")
i.model = model.NewNullModel()
i.modelInterpreter = interpreter.NewModelConfigInterpreter()
i.annealer = &annealers.NullAnnealer{}
i.annealerInterpreter = interpreter.NewAnnealerConfigInterpreter()
i.scenario = scenario.NullScenario
i.scenarioInterpreter = NewScenarioConfigInterpreter()
return i
}
func (i *ConfigInterpreter) Interpret(config *appData.Config) *ConfigInterpreter {
if config == nil {
i.errors.Add(errors.New("no config supplied for interpretation"))
return i
}
i.interpretModelConfig(&config.Model)
i.interpretAnnealerConfig(&config.Annealer)
i.interpretScenarioConfig(&config.Scenario)
i.annealer.SetModel(i.model)
i.scenario.SetAnnealer(i.annealer)
return i
}
func (i *ConfigInterpreter) interpretModelConfig(config *data.ModelConfig) {
i.model = i.modelInterpreter.Interpret(config).Model()
if i.modelInterpreter.Errors() != nil {
i.errors.Add(i.modelInterpreter.Errors())
}
}
func (i *ConfigInterpreter) interpretAnnealerConfig(config *data.AnnealerConfig) {
i.annealer = i.annealerInterpreter.Interpret(config).Annealer()
if i.annealerInterpreter.Errors() != nil {
i.errors.Add(i.annealerInterpreter.Errors())
}
}
func (i *ConfigInterpreter) interpretScenarioConfig(config *appData.ScenarioConfig) {
i.scenario = i.scenarioInterpreter.Interpret(config).Scenario()
if i.scenarioInterpreter.Errors() != nil {
i.errors.Add(i.scenarioInterpreter.Errors())
}
}
func (i *ConfigInterpreter) Scenario() scenario.Scenario {
assert.That(i.scenario != nil)
return i.scenario
}
func (i *ConfigInterpreter) Errors() error {
if i.errors.Size() > 0 {
return i.errors
}
return nil
}