-
Notifications
You must be signed in to change notification settings - Fork 0
/
anchoring-evaluator.go
65 lines (58 loc) · 2.09 KB
/
anchoring-evaluator.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
package anchoring
import (
"fmt"
"github.com/Azbesciak/RealDecisionMaker/lib/model"
)
//go:generate easytags $GOFILE json:camel
type AnchoringEvaluator interface {
FunctionBase
Evaluate(params FunctionParams, difference float64) float64
}
func (a *Anchoring) getAnchoringEvaluatorFunction(params *FunctionDefinition, anchoringType string) AnchoringWithParams {
for _, fun := range a.anchoringEvaluators {
if fun.Identifier() == params.Function {
return AnchoringWithParams{
fun: fun,
params: parseFuncParams(fun, params),
}
}
}
existing := a.knownAnchoringEvaluatorsNames()
panic(fmt.Errorf("%s anchoring function '%s' not found in %v", anchoringType, params.Function, existing))
}
func (a *Anchoring) knownAnchoringEvaluatorsNames() []string {
existing := make([]string, len(a.anchoringEvaluators))
for i, id := range a.anchoringEvaluators {
existing[i] = id.Identifier()
}
return existing
}
func (a *Anchoring) evaluateAnchoringAlternatives(
allAlternatives []model.AlternativeWithCriteria,
parsedProps *AnchoringParams,
criteria *model.Criteria,
) []model.AlternativeWithCriteria {
anchoringAlternatives := fetchAnchoringAlternativesWithCriteria(&allAlternatives, &parsedProps.AnchoringAlternatives)
referencePointsEvaluator := a.getReferencePointsFunction(&parsedProps.ReferencePoints)
referencePoints := referencePointsEvaluator.Evaluate(parsedProps.ReferencePoints, anchoringAlternatives, criteria)
return referencePoints
}
func (a *Anchoring) getReferencePointsFunction(params *FunctionDefinition) ReferencePointsEvaluator {
for _, fun := range a.referencePointsEvaluators {
if fun.Identifier() == params.Function {
return fun
}
}
knownReferencePointsEvaluators := a.knownReferencePointsEvaluatorsNames()
panic(fmt.Errorf(
"reference points function type '%s' not found in %v",
params.Function, knownReferencePointsEvaluators,
))
}
func (a *Anchoring) knownReferencePointsEvaluatorsNames() []string {
existing := make([]string, len(a.referencePointsEvaluators))
for i, id := range a.referencePointsEvaluators {
existing[i] = id.Identifier()
}
return existing
}