Skip to content

Commit 2818dc0

Browse files
committed
WIP
1 parent 28451d9 commit 2818dc0

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

packages/tlm-core-db/src/loader/tlmd.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IModeler, Modeler } from "@typelinkmodel/tlm-core-model";
1+
import { IModeler, Modeler, TlmNamespace, TlmType } from "@typelinkmodel/tlm-core-model";
22
import { createReadStream, ReadStream } from "fs";
33
import { createInterface } from "readline";
44
import { ILoader, IReader, ISearcher } from "../api";
@@ -59,11 +59,16 @@ export class TlmdStreamHandler {
5959
private readonly _modeler: IModeler;
6060
private readonly _continueOnError: boolean;
6161
private readonly _debug: boolean;
62+
private _currentObjectType: TlmType | undefined;
63+
private _currentId: string | undefined;
64+
private _currentNamespace: TlmNamespace | undefined;
6265

6366
constructor(modeler: IModeler, continueOnError = false, debug = false) {
6467
this._modeler = modeler;
6568
this._continueOnError = continueOnError;
6669
this._debug = debug;
70+
this._currentObjectType = undefined;
71+
this._currentId = undefined;
6772
}
6873

6974
public debug(message: string): void {
@@ -136,10 +141,41 @@ export class TlmdStreamHandler {
136141

137142
public async handleObject(type: string, id: string): Promise<void> {
138143
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+
}
139163
}
140164

141165
public async handleFact(link: string, value: string): Promise<void> {
142166
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+
143179
}
144180

145181
public async handleToggle(link: string): Promise<void> {

packages/tlm-core-model/src/api/modeler.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,6 @@ export interface IModeler {
3333
getValueTypeForLink(link: TlmLink): TlmType;
3434

3535
getTypeByName(type: string): TlmType;
36+
37+
findNamespaceByOid(oid: number): TlmNamespace;
3638
}

packages/tlm-core-model/src/modeler/modeler.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ export class Modeler implements IModeler {
127127
return this._typeModel.typeMap[ns][name];
128128
}
129129

130+
public findNamespaceByOid(oid: number): TlmNamespace {
131+
return this._namespaceModel.findNamespaceByOid(oid);
132+
}
133+
130134
private async processLinkDefinitionStatement(
131135
match: RegExpMatchArray
132136
): Promise<void> {

packages/tlm-core-model/test/modeler/modeler.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,13 @@ test("getValueTypeForLink: basic usage", async () => {
401401
).toBe("Namespace");
402402
});
403403

404+
test("findNamespaceByOid: basic usage", async () => {
405+
const modeler: Modeler = new Modeler();
406+
modeler.initialize();
407+
const oid = modeler.namespaces.tlm.oid;
408+
expect(modeler.findNamespaceByOid(oid).prefix).toBe(modeler.namespaces.tlm.prefix);
409+
});
410+
404411
test("processLinkDefinitionStatement: cover unreachable default case", async () => {
405412
const modeler: Modeler = new Modeler();
406413

0 commit comments

Comments
 (0)