-
Notifications
You must be signed in to change notification settings - Fork 0
/
conceptrepo.go
185 lines (168 loc) · 5.45 KB
/
conceptrepo.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright 2020 InfAI (CC SES)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package conceptrepo
import (
"context"
"errors"
"github.com/SENERGY-Platform/marshaller/lib/config"
"github.com/SENERGY-Platform/marshaller/lib/marshaller/model"
"github.com/SENERGY-Platform/service-commons/pkg/signal"
"log"
"runtime/debug"
"sync"
"time"
)
type ConceptRepo struct {
config config.Config
access Access
defaults []ConceptRepoDefault
concepts map[string]model.Concept
characteristics map[string]model.Characteristic
conceptByCharacteristic map[string][]model.Concept
rootCharacteristicByCharacteristic map[string]model.Characteristic
characteristicsOfFunction map[string][]string
functionToConcept map[string]string
mux sync.Mutex
}
type ConceptRepoDefault struct {
Concept model.Concept
Characteristics []model.Characteristic
}
type Access interface {
Ensure() (config.Impersonate, error)
}
func New(ctx context.Context, conf config.Config, access Access, defaults ...ConceptRepoDefault) (result *ConceptRepo, err error) {
result = &ConceptRepo{
config: conf,
access: access,
defaults: defaults,
concepts: map[string]model.Concept{},
characteristics: map[string]model.Characteristic{},
conceptByCharacteristic: map[string][]model.Concept{},
rootCharacteristicByCharacteristic: map[string]model.Characteristic{},
characteristicsOfFunction: map[string][]string{},
functionToConcept: map[string]string{},
}
err = result.Load()
if err != nil {
return result, err
}
ticker := time.NewTicker(time.Duration(conf.ConceptRepoRefreshInterval) * time.Second)
go func() {
defer ticker.Stop()
<-ctx.Done()
}()
refresh := func() {
log.Println("refresh concept-repo")
err = result.Load()
if err != nil {
log.Println("WARNING: unable to update concept repository", err)
}
}
go func() {
for range ticker.C {
refresh()
}
}()
f := func(_ string, _ *sync.WaitGroup) {
refresh()
}
signal.Known.CacheInvalidationAll.Sub("concept-repo-all", f)
signal.Known.CharacteristicCacheInvalidation.Sub("concept-repo-characteristics", f)
signal.Known.ConceptCacheInvalidation.Sub("concept-repo-concept", f)
signal.Known.FunctionCacheInvalidation.Sub("concept-repo-function", f)
return result, nil
}
func (this *ConceptRepo) GetCharacteristicsOfFunction(functionId string) (characteristicIds []string, err error) {
this.mux.Lock()
defer this.mux.Unlock()
var ok bool
characteristicIds, ok = this.characteristicsOfFunction[functionId]
if !ok {
err = errors.New("unknown function-id")
}
return
}
func (this *ConceptRepo) GetConcept(id string) (concept model.Concept, err error) {
this.mux.Lock()
defer this.mux.Unlock()
concept, ok := this.concepts[id]
if !ok {
debug.PrintStack()
return concept, errors.New("no concept found for id " + id)
}
return concept, nil
}
func (this *ConceptRepo) GetConceptIdOfFunction(id string) string {
this.mux.Lock()
defer this.mux.Unlock()
return this.functionToConcept[id]
}
func getCharacteristicDescendents(characteristic model.Characteristic) (result []model.Characteristic) {
result = []model.Characteristic{characteristic}
for _, child := range characteristic.SubCharacteristics {
result = append(result, getCharacteristicDescendents(child)...)
}
return result
}
func (this *ConceptRepo) GetConceptsOfCharacteristic(characteristicId string) (conceptIds []string, err error) {
this.mux.Lock()
defer this.mux.Unlock()
concepts, ok := this.conceptByCharacteristic[this.rootCharacteristicByCharacteristic[characteristicId].Id]
if !ok {
debug.PrintStack()
return conceptIds, errors.New("no concept found for characteristic id " + characteristicId)
}
for _, concept := range concepts {
conceptIds = append(conceptIds, concept.Id)
}
return conceptIds, nil
}
func (this *ConceptRepo) GetCharacteristic(id string) (characteristic model.Characteristic, err error) {
if id == "" {
return model.NullCharacteristic, nil
}
this.mux.Lock()
defer this.mux.Unlock()
characteristic, ok := this.characteristics[id]
if !ok {
debug.PrintStack()
return characteristic, errors.New("no characteristic found for id " + id)
}
return characteristic, nil
}
func (this *ConceptRepo) GetRootCharacteristics(ids []string) (result []string) {
this.mux.Lock()
defer this.mux.Unlock()
for _, id := range ids {
root, ok := this.rootCharacteristicByCharacteristic[id]
if ok {
result = append(result, root.Id)
}
}
return
}
func (this *ConceptRepo) registerFunction(f FunctionInfo) {
if f.ConceptId != "" {
concept, ok := this.concepts[f.ConceptId]
if !ok {
log.Println("WARNING: unable to register function with unknown concept", f)
return
}
this.characteristicsOfFunction[f.Id] = concept.CharacteristicIds
this.functionToConcept[f.Id] = f.ConceptId
}
}