Skip to content

Commit e9dae6f

Browse files
committed
fix(core): roots are the instances with no parent (conformance EXP-15)
GET /objects?root=true returned Objects with a non-null parentId: an instance counted as root when it had no Object ancestor, but an Object under a top-level Variable (the HA Configuration of a historized variable) has that Variable as parent. On opcuademo:26541 that made 52 of 84 roots carry a parentId. Derive roots from childrenById instead, so root <=> parentIdOf === null. The HA Configuration objects stay reachable as children of their Variable.
1 parent 4fb86e9 commit e9dae6f

2 files changed

Lines changed: 48 additions & 3 deletions

File tree

‎packages/core/src/services/model-service.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,15 +295,22 @@ export class ModelService {
295295
nodesById.set(nodeId, mapped);
296296
childrenById.set(nodeId, childIds);
297297

298-
if (mapped.kind === 'instance' && findParentAsset(srcNode) == null) {
299-
rootIds.push(nodeId);
300-
}
301298
if (mapped.kind === 'property') propertyToSource.set(nodeId, srcNode.sourceNodeId);
302299
if (mapped.kind === 'action' && srcNode.parentSourceNodeId != null) {
303300
actionToMethod.set(nodeId, [srcNode.parentSourceNodeId, srcNode.sourceNodeId]);
304301
}
305302
}
306303

304+
// A root is an instance nothing in the model points to as a child, so that its
305+
// parentId (parentIdOf) is null. Having no Object ancestor is not enough: an
306+
// Object under a top-level Variable (e.g. its HA Configuration) has that Variable
307+
// as parent.
308+
const allChildIds = new Set<string>();
309+
for (const ids of childrenById.values()) for (const id of ids) allChildIds.add(id);
310+
for (const [nodeId, node] of nodesById) {
311+
if (node.kind === 'instance' && !allChildIds.has(nodeId)) rootIds.push(nodeId);
312+
}
313+
307314
// ── Pre-fetch and map engineering units automatically ──
308315
const engUnitReqs: { propertyId: string; engUnitSourceId: string }[] = [];
309316
for (const node of nodesById.values()) {

‎packages/core/tests/model-service.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,44 @@ describe('ModelService – browse path construction', () => {
171171
expect(idA).not.toBe(idB);
172172
});
173173

174+
it('an Object under a top-level Variable is not a root (conformance EXP-15)', async () => {
175+
// Objects/Bool (historized Variable in a folder) -> HA Configuration (Object)
176+
const nodes = [
177+
sourceNode({
178+
sourceNodeId: 'i=85',
179+
nsuQualifiedName: 'nsu=http://opcfoundation.org/UA/:Objects',
180+
typeDefinition: 'ns=0;i=61',
181+
}),
182+
sourceNode({
183+
sourceNodeId: 'ns=2;i=1',
184+
nsuQualifiedName: 'nsu=http://test.org/:Line',
185+
}),
186+
sourceNode({
187+
sourceNodeId: 'ns=2;i=2',
188+
nsuQualifiedName: 'nsu=http://test.org/:Bool',
189+
parentSourceNodeId: 'i=85',
190+
nodeClass: 'Variable',
191+
}),
192+
sourceNode({
193+
sourceNodeId: 'ns=2;i=3',
194+
nsuQualifiedName: 'nsu=http://opcfoundation.org/UA/:HA Configuration',
195+
parentSourceNodeId: 'ns=2;i=2',
196+
}),
197+
];
198+
const svc = new ModelService(mockDataSource(nodes), nullLogger);
199+
const model = await svc.getOrBuildModel();
200+
201+
const lineId = stableI3xId('nsu=http://test.org/:Line', 'instance');
202+
expect(model.rootIds).toEqual([lineId]);
203+
for (const id of model.rootIds) expect(svc.parentIdOf(model, id)).toBeNull();
204+
205+
const haConfig = [...model.nodesById.values()].find(
206+
(n) => n.name === 'HA Configuration',
207+
);
208+
expect(haConfig).toBeDefined();
209+
expect(model.nodesById.get(svc.parentIdOf(model, haConfig!.id)!)?.name).toBe('Bool');
210+
});
211+
174212
it('multi-namespace tree encodes both URIs in child browse path', async () => {
175213
const rootNsu = 'nsu=http://di.org/:DeviceSet';
176214
const childNsu = 'nsu=http://coffee.com/:Machine';

0 commit comments

Comments
 (0)