Skip to content

Commit 7bf9885

Browse files
committed
Fix circular reference related bug.
1 parent 87e3f8f commit 7bf9885

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

src/graphDBModel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class GraphDBModel {
2828
*/
2929
internalKey2Option;
3030
/**
31-
* @type {Map<string, GraphDBModel>}
31+
* @type {Uri2ModelMap}
3232
*/
3333
nestedType2Model;
3434
/**

src/graphDBSchema.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export function createGraphDBModel(schema: GraphDBSchema, schemaOptions: SchemaO
6868
const internalKey2Option = new Map();
6969

7070
// nested model information
71-
const nestedType2Model = new Map();
71+
const nestedType2Model = new Uri2ModelMap();
7272

7373
for (let [key, options] of Object.entries(schema)) {
7474
// Map to our data structure with some predefined options
@@ -103,9 +103,6 @@ export function createGraphDBModel(schema: GraphDBSchema, schemaOptions: SchemaO
103103
else
104104
nestedType2Model.set(SPARQL.getFullURI(nestedRdfType), nestedModel);
105105
}
106-
for (const [innerKey, innerVal] of nestedModel.nestedType2Model?.entries() || []) {
107-
nestedType2Model.set(innerKey, innerVal);
108-
}
109106

110107
}
111108
}
@@ -131,4 +128,39 @@ function getGraphDBModel(name: string) {
131128
return store[`:${name}`];
132129
}
133130

131+
class Uri2ModelMap {
132+
private map = new Map<string, GraphDBModel>();
133+
134+
/**
135+
* Recursively find the model by URI.
136+
*/
137+
public get(uri: string, iterated = new Set()) {
138+
if (iterated.has(this)) {
139+
return;
140+
}
141+
iterated.add(this);
142+
143+
if (this.map.has(uri)) {
144+
return this.map.get(uri);
145+
} else {
146+
return this._getFromNestedModels(uri, iterated);
147+
}
148+
}
149+
150+
public set(uri: string, model: GraphDBModel) {
151+
this.map.set(uri, model);
152+
}
153+
154+
private _getFromNestedModels(uri: string, iterated = new Set()) {
155+
for (const nestedModel of this.map.values()) {
156+
const nestedResult = nestedModel.nestedType2Model.get(uri, iterated);
157+
if (nestedResult) {
158+
return nestedResult;
159+
}
160+
}
161+
return undefined;
162+
}
163+
}
164+
165+
134166
module.exports = {Types, DeleteType, regexBuilder, createGraphDBModel, getGraphDBModel}

tests/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe("GraphDB", function () {
2929
'foaf': 'http://xmlns.com/foaf/0.1/',
3030
'cwrc': 'http://sparql.cwrc.ca/ontologies/cwrc#',
3131
'tove_org': 'http://ontology.eil.utoronto.ca/tove/organization#',
32+
'iso21972': 'http://ontology.eil.utoronto.ca/ISO21972/iso21972#',
3233
};
3334

3435
const result = await initGraphDB({

tests/populateIssue.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export function PopulateIssue(repository: any) {
6666
await indicator1.save();
6767
outcome.indicators = [indicator1];
6868
await outcome.save();
69+
await GDBOrganizationModel.find({});
6970
const outcomes = await GDBOutcomeModel.find({_uri: outcome._uri}, {populates: ['indicators.unitOfMeasure']})
7071
expect(await GDBOutcomeModel.find({}, {populates: ['indicators']}));
7172
const result = await GDBOutcomeModel.find({});

0 commit comments

Comments
 (0)