AddressSpace.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { UANodeSet } from "./UANodeSet";
  2. import { UABaseNode } from "./UABaseNode";
  3. import { NamespaceTable } from "./NameSpaceTable";
  4. import { type IAddressSpace } from "./IAddressSpace";
  5. import type { IMappingEntry } from "@/ServerConfig";
  6. export class AddressSpace implements IAddressSpace{
  7. nodeMap: Map<string, UABaseNode>;
  8. nst: NamespaceTable;
  9. nodesets: UANodeSet[];
  10. mapping: Map<string, IMappingEntry>;
  11. constructor(nodesets: UANodeSet[]) {
  12. this.nst=new NamespaceTable();
  13. this.nodeMap=new Map();
  14. this.nodesets=[];
  15. this.mapping=new Map();
  16. for(const nodeset of nodesets) {
  17. this.addNodeset(nodeset);
  18. }
  19. }
  20. getNodeSets(): UANodeSet[]{
  21. return this.nodesets;
  22. }
  23. public findNode(nodeId: string):UABaseNode|undefined {
  24. return this.nodeMap.get(nodeId)
  25. }
  26. public getSubTreeAsList(nodeId: string): UABaseNode[] {
  27. const node=this.findNode(nodeId);
  28. if(!node)
  29. return [];
  30. const nlist:UABaseNode[]=[node];
  31. for(const n of node.getChildren()) {
  32. const list=this.getSubTreeAsList(n.nodeId.toString());
  33. nlist.push(...list);
  34. }
  35. return nlist;
  36. }
  37. public addNodeset(nodeset: UANodeSet) {
  38. nodeset.reIndex(this.nst);
  39. for(const node of nodeset.nodes) {
  40. this.nodeMap.set(node.nodeId.toString(), node);
  41. }
  42. nodeset.resolveReferences(this.nodeMap);
  43. this.nodesets.push(nodeset);
  44. }
  45. static async load(files: string[]) {
  46. const as=new AddressSpace([]);
  47. const promises:Promise<UANodeSet>[] = []
  48. for(const file of files) {
  49. promises.push(UANodeSet.load(file, as));
  50. }
  51. for(const p of promises) {
  52. as.addNodeset(await p);
  53. }
  54. return as;
  55. }
  56. }