|
1 | | -import { IModeler, Modeler } from "@typelinkmodel/tlm-core-model"; |
| 1 | +import { IModeler, Modeler, TlmNamespace, TlmType } from "@typelinkmodel/tlm-core-model"; |
2 | 2 | import { createReadStream, ReadStream } from "fs"; |
3 | 3 | import { createInterface } from "readline"; |
4 | 4 | import { ILoader, IReader, ISearcher } from "../api"; |
@@ -59,11 +59,16 @@ export class TlmdStreamHandler { |
59 | 59 | private readonly _modeler: IModeler; |
60 | 60 | private readonly _continueOnError: boolean; |
61 | 61 | private readonly _debug: boolean; |
| 62 | + private _currentObjectType: TlmType | undefined; |
| 63 | + private _currentId: string | undefined; |
| 64 | + private _currentNamespace: TlmNamespace | undefined; |
62 | 65 |
|
63 | 66 | constructor(modeler: IModeler, continueOnError = false, debug = false) { |
64 | 67 | this._modeler = modeler; |
65 | 68 | this._continueOnError = continueOnError; |
66 | 69 | this._debug = debug; |
| 70 | + this._currentObjectType = undefined; |
| 71 | + this._currentId = undefined; |
67 | 72 | } |
68 | 73 |
|
69 | 74 | public debug(message: string): void { |
@@ -136,10 +141,41 @@ export class TlmdStreamHandler { |
136 | 141 |
|
137 | 142 | public async handleObject(type: string, id: string): Promise<void> { |
138 | 143 | this.debug(`Object: type = '${type}', id = '${id}'`); |
| 144 | + const objectType = this._modeler.getTypeByName(type); |
| 145 | + if (objectType === undefined) { |
| 146 | + throw new Error(`No type registered '${type}', cannot process object using it.`); |
| 147 | + } |
| 148 | + this._currentObjectType = objectType; |
| 149 | + this._currentId = id; |
| 150 | + this._currentNamespace = this._modeler.findNamespaceByOid(objectType.namespace); |
| 151 | + const links = this._modeler.links[this._currentNamespace.prefix][type]; |
| 152 | + let setPrimaryId = false; |
| 153 | + for (const [linkName, link] of Object.entries(links)) { |
| 154 | + if(link.isPrimaryId) { |
| 155 | + // todo this._db.addFact(objectType, link, id); |
| 156 | + setPrimaryId = true; |
| 157 | + break; |
| 158 | + } |
| 159 | + } |
| 160 | + if (!setPrimaryId) { |
| 161 | + throw new Error(`No primary ID registered for type '${type}, so cannot set its id.`); |
| 162 | + } |
139 | 163 | } |
140 | 164 |
|
141 | 165 | public async handleFact(link: string, value: string): Promise<void> { |
142 | 166 | this.debug(`- fact: link = '${link}', value = '${value}'`); |
| 167 | + if (this._currentObjectType === undefined) { |
| 168 | + throw new Error('Define object type before adding a fact for it.'); |
| 169 | + } |
| 170 | + const ns = this._currentNamespace; |
| 171 | + if (ns === undefined) { |
| 172 | + throw new Error('Define object with namespace before adding a fact for it.'); |
| 173 | + } |
| 174 | + const linkType = this._modeler.links[ns.prefix][link]; |
| 175 | + if (linkType === undefined) { |
| 176 | + throw new Error(`Define link type ${link} before adding a fact using it.`); |
| 177 | + } |
| 178 | + |
143 | 179 | } |
144 | 180 |
|
145 | 181 | public async handleToggle(link: string): Promise<void> { |
|
0 commit comments