/
SemanticsMetadataProvider.java
316 lines (282 loc) · 12.1 KB
/
SemanticsMetadataProvider.java
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.semantics.internal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.common.registry.AbstractProvider;
import org.openhab.core.common.registry.RegistryChangeListener;
import org.openhab.core.items.GroupItem;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.items.ItemRegistryChangeListener;
import org.openhab.core.items.Metadata;
import org.openhab.core.items.MetadataKey;
import org.openhab.core.items.MetadataProvider;
import org.openhab.core.semantics.Equipment;
import org.openhab.core.semantics.Location;
import org.openhab.core.semantics.Point;
import org.openhab.core.semantics.Property;
import org.openhab.core.semantics.SemanticTag;
import org.openhab.core.semantics.SemanticTagRegistry;
import org.openhab.core.semantics.SemanticTags;
import org.openhab.core.semantics.Tag;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This {@link MetadataProvider} collects semantic information about items and provides them as metadata under the
* "semantics" namespace.
*
* The main value of the metadata holds the semantic type of the item, i.e. a sub-class of Location, Equipment or Point.
* The metadata configuration contains the information about the relations with the key being the name of the relation
* (e.g. "hasLocation") and the value being the id of the referenced entity (e.g. its item name).
*
* @author Kai Kreuzer - Initial contribution
*/
@Component(immediate = true, service = MetadataProvider.class)
@NonNullByDefault
public class SemanticsMetadataProvider extends AbstractProvider<Metadata>
implements ItemRegistryChangeListener, MetadataProvider {
private final Logger logger = LoggerFactory.getLogger(SemanticsMetadataProvider.class);
// the namespace to use for the metadata
public static final String NAMESPACE = "semantics";
// holds the static definition of the relations between entities
private final Map<List<Class<? extends Tag>>, String> parentRelations = new HashMap<>();
private final Map<List<Class<? extends Tag>>, String> memberRelations = new HashMap<>();
private final Map<List<Class<? extends Tag>>, String> propertyRelations = new HashMap<>();
// local cache of the created metadata as a map from itemName->Metadata
private final Map<String, Metadata> semantics = new TreeMap<>(String::compareTo);
private final ItemRegistry itemRegistry;
private final SemanticTagRegistry semanticTagRegistry;
private SemanticTagRegistryChangeListener listener;
@Activate
public SemanticsMetadataProvider(final @Reference ItemRegistry itemRegistry,
final @Reference SemanticTagRegistry semanticTagRegistry) {
this.itemRegistry = itemRegistry;
this.semanticTagRegistry = semanticTagRegistry;
this.listener = new SemanticTagRegistryChangeListener(this);
}
@Activate
protected void activate() {
initRelations();
for (Item item : itemRegistry.getAll()) {
processItem(item);
}
itemRegistry.addRegistryChangeListener(this);
semanticTagRegistry.addRegistryChangeListener(listener);
}
@Deactivate
protected void deactivate() {
semanticTagRegistry.removeRegistryChangeListener(listener);
itemRegistry.removeRegistryChangeListener(this);
semantics.clear();
}
@Override
public Collection<Metadata> getAll() {
return semantics.values();
}
/**
* Updates the semantic metadata for an item and notifies all listeners about changes
*
* @param item the item to update the metadata for
*/
private void processItem(Item item) {
processItem(item, new ArrayList<>());
}
private void processItem(Item item, List<String> parentItems) {
MetadataKey key = new MetadataKey(NAMESPACE, item.getName());
Map<String, Object> configuration = new HashMap<>();
Class<? extends Tag> type = SemanticTags.getSemanticType(item);
if (type != null) {
processProperties(item, configuration);
processHierarchy(item, configuration);
Metadata md = new Metadata(key, SemanticTagRegistryImpl.buildId(type), configuration);
Metadata oldMd = semantics.put(item.getName(), md);
if (oldMd == null) {
notifyListenersAboutAddedElement(md);
} else {
notifyListenersAboutUpdatedElement(oldMd, md);
}
} else {
Metadata removedMd = semantics.remove(item.getName());
if (removedMd != null) {
notifyListenersAboutRemovedElement(removedMd);
}
}
if (item instanceof GroupItem groupItem) {
parentItems.add(item.getName());
for (Item memberItem : groupItem.getMembers()) {
if (parentItems.contains(memberItem.getName())) {
logger.error(
"Recursive group membership found: {} is a member of {}, but it is also one of its ancestors.",
memberItem.getName(), groupItem.getName());
} else {
processItem(memberItem, new ArrayList<>(parentItems));
}
}
}
}
/**
* Processes Property tags on items and if found, adds it to the metadata configuration.
*
* @param item the item to process
* @param configuration the metadata configuration that should be amended
*/
private void processProperties(Item item, Map<String, Object> configuration) {
Class<? extends Tag> type = SemanticTags.getSemanticType(item);
if (type == null) {
return;
}
for (Entry<List<Class<? extends Tag>>, String> relation : propertyRelations.entrySet()) {
Class<? extends Tag> entityClass = relation.getKey().get(0);
if (entityClass.isAssignableFrom(type)) {
Class<? extends Property> p = SemanticTags.getProperty(item);
if (p != null) {
configuration.put(relation.getValue(), SemanticTagRegistryImpl.buildId(p));
}
}
}
}
/**
* Retrieves semantic information from parent or member items.
*
* @param item the item to gather the semantic metadata for
* @param configuration the metadata configuration that should be amended
*/
private void processHierarchy(Item item, Map<String, Object> configuration) {
Class<? extends Tag> type = SemanticTags.getSemanticType(item);
if (type != null) {
for (String parent : item.getGroupNames()) {
Item parentItem = itemRegistry.get(parent);
if (parentItem != null) {
processParent(type, parentItem, configuration);
}
}
if (item instanceof GroupItem gItem) {
for (Item memberItem : gItem.getMembers()) {
processMember(type, memberItem, configuration);
}
}
}
}
/**
* Retrieves semantic information from a parent items.
*
* @param type the semantic type of the item for which the semantic information is gathered
* @param parentItem the parent item to process
* @param configuration the metadata configuration that should be amended
*/
private void processParent(Class<? extends Tag> type, Item parentItem, Map<String, Object> configuration) {
Class<? extends Tag> typeParent = SemanticTags.getSemanticType(parentItem);
if (typeParent == null) {
return;
}
for (Entry<List<Class<? extends Tag>>, String> relation : parentRelations.entrySet()) {
List<Class<? extends Tag>> relClasses = relation.getKey();
Class<? extends Tag> entityClass = relClasses.get(0);
Class<? extends Tag> parentClass = relClasses.get(1);
// process relations of locations
if (entityClass.isAssignableFrom(type)) {
if (parentClass.isAssignableFrom(typeParent)) {
configuration.put(relation.getValue(), parentItem.getName());
}
}
}
}
/**
* Retrieves semantic information from a member items.
*
* @param type the semantic type of the item for which the semantic information is gathered
* @param memberItem the member item to process
* @param configuration the metadata configuration that should be amended
*/
private void processMember(Class<? extends Tag> type, Item memberItem, Map<String, Object> configuration) {
Class<? extends Tag> typeMember = SemanticTags.getSemanticType(memberItem);
if (typeMember == null) {
return;
}
for (Entry<List<Class<? extends Tag>>, String> relation : memberRelations.entrySet()) {
List<Class<? extends Tag>> relClasses = relation.getKey();
Class<? extends Tag> entityClass = relClasses.get(0);
Class<? extends Tag> parentClass = relClasses.get(1);
// process relations of locations
if (entityClass.isAssignableFrom(type)) {
if (parentClass.isAssignableFrom(typeMember)) {
configuration.put(relation.getValue(), memberItem.getName());
}
}
}
}
private void initRelations() {
parentRelations.put(List.of(Equipment.class, Location.class), "hasLocation");
parentRelations.put(List.of(Point.class, Location.class), "hasLocation");
parentRelations.put(List.of(Location.class, Location.class), "isPartOf");
parentRelations.put(List.of(Equipment.class, Equipment.class), "isPartOf");
parentRelations.put(List.of(Point.class, Equipment.class), "isPointOf");
memberRelations.put(List.of(Equipment.class, Point.class), "hasPoint");
propertyRelations.put(List.of(Point.class), "relatesTo");
}
@Override
public void allItemsChanged(Collection<String> oldItemNames) {
for (Item item : itemRegistry.getItems()) {
added(item);
}
}
@Override
public void added(Item item) {
processItem(item);
}
@Override
public void removed(Item item) {
Metadata removedMd = semantics.remove(item.getName());
if (removedMd != null) {
notifyListenersAboutRemovedElement(removedMd);
if (item instanceof GroupItem groupItem) {
for (Item memberItem : groupItem.getMembers()) {
processItem(memberItem);
}
}
}
}
@Override
public void updated(Item oldItem, Item item) {
processItem(item);
}
private static class SemanticTagRegistryChangeListener implements RegistryChangeListener<SemanticTag> {
private SemanticsMetadataProvider provider;
public SemanticTagRegistryChangeListener(SemanticsMetadataProvider provider) {
this.provider = provider;
}
@Override
public void added(SemanticTag element) {
provider.allItemsChanged(List.of());
}
@Override
public void removed(SemanticTag element) {
provider.allItemsChanged(List.of());
}
@Override
public void updated(SemanticTag oldElement, SemanticTag element) {
provider.allItemsChanged(List.of());
}
}
}