AddressSpace.ts 1.8 KB

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