Skip to content

Commit

Permalink
refactor(ontology): clean up async/await function
Browse files Browse the repository at this point in the history
  • Loading branch information
kilchenmann committed Feb 26, 2021
1 parent 9c910c4 commit d174ba2
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 92 deletions.
4 changes: 2 additions & 2 deletions src/app/project/ontology/ontology.component.html
Expand Up @@ -30,7 +30,7 @@ <h2 class="mat-title">
Open from list...
</mat-label>
<mat-select [formControl]="ontologyForm.controls['ontology']" [(value)]="ontologyIri">
<mat-option *ngFor="let onto of ontologies; let first = first" [value]="onto.id">
<mat-option *ngFor="let onto of ontologies" [value]="onto.id">
{{onto.label}}
</mat-option>
</mat-select>
Expand Down Expand Up @@ -205,7 +205,7 @@ <h4 mat-card-title [matTooltip]="resClass.comment" matTooltipPosition="above">
<app-ontology-visualizer [ontology]="ontology" [ontoClasses]="ontoClasses"></app-ontology-visualizer>
</div>

<dsp-progress-indicator *ngIf="loadOntology"></dsp-progress-indicator>
<dsp-progress-indicator *ngIf="loadOntology && view !== 'graph'"></dsp-progress-indicator>
</div>

</div>
Expand Down
165 changes: 75 additions & 90 deletions src/app/project/ontology/ontology.component.ts
Expand Up @@ -176,90 +176,6 @@ export class OntologyComponent implements OnInit {
);
}

waitFor = (ms: number) => new Promise(r => setTimeout(r, ms));

/**
* Asyncs for each: Get all ontologies of project as ReadOntology
* @param ontologies
* @param callback
*/
async asyncForEach(ontologies: OntologyMetadata[], callback: any) {
for (let i = 0; i < ontologies.length; i++) {
// set list of already existing ontology names
// it will be used in ontology form
// because ontology name has to be unique
const name = this._resourceClassFormService.getOntologyName(ontologies[i].id);
this.existingOntologyNames.push(name);

// get each ontology
// this.getOntology(ontologies[i].id, true);
this._dspApiConnection.v2.onto.getOntology(ontologies[i].id, true).subscribe(
(response: ReadOntology) => {
this.ontologies.push(response);
if (ontologies[i].id === this.ontologyIri) {
// one ontology is selected:
// get all information to display this ontology
// with all classes, properties and connected lists
this.loadOntology = true;
this.ontology = this.ontologies.find(onto => onto.id === this.ontologyIri);
this._cache.set('currentOntology', this.ontology);
this.ontology = response;

// grab the onto class information to display
const allOntoClasses = response.getAllClassDefinitions();

// reset the ontology classes
this.ontoClasses = [];

// display only the classes which are not a subClass of Standoff
allOntoClasses.forEach(resClass => {
const splittedSubClass = resClass.subClassOf[0].split('#');
if (!splittedSubClass[0].includes(Constants.StandoffOntology) && !splittedSubClass[1].includes('Standoff')) {
this.ontoClasses.push(resClass);
}
});
// sort classes by label
// --> TODO: add sort functionallity to the gui
this.ontoClasses = this._sortingService.keySortByAlphabetical(this.ontoClasses, 'label');

// grab the onto properties information to display
const allOntoProperties = response.getAllPropertyDefinitions();

// reset the ontology properties
this.ontoProperties = [];

// display only the properties which are not a subjectType of Standoff
allOntoProperties.forEach(resProp => {
const standoff = (resProp.subjectType ? resProp.subjectType.includes('Standoff') : false);
if (resProp.objectType !== Constants.LinkValue && !standoff) {
this.ontoProperties.push(resProp);
}
});
// sort properties by label
// --> TODO: add sort functionallity to the gui
this.ontoProperties = this._sortingService.keySortByAlphabetical(this.ontoProperties, 'label');
}
},
(error: ApiResponseError) => {
this._errorHandler.showMessage(error);
}
);
await callback(ontologies[i]);
}
}

async loadAndCache(ontologies: OntologyMetadata[]) {
await this.asyncForEach(ontologies, async (onto: OntologyMetadata) => {
await this.waitFor(200);
if (this.ontologies.length === ontologies.length) {
// all ontologies were fetched from the API; we can set the cache
this._cache.set('currentProjectOntologies', this.ontologies);
this.loading = false;
this.setCache();
}
});
}

/**
* build the list of project ontologies
* and cache them as ReadOntology array
Expand All @@ -279,14 +195,84 @@ export class OntologyComponent implements OnInit {
this.setCache();
} else {
// in case project has only one ontology: open this ontology
// because there will be no form to select ontlogy
// because there will be no form to select an ontlogy
if (response.ontologies.length === 1) {
// open this ontology
this.openOntologyRoute(response.ontologies[0].id, this.view);
this.ontologyIri = response.ontologies[0].id;
}

this.loadAndCache(response.ontologies);
response.ontologies.forEach((ontoMeta, index, array) => {
// set list of already existing ontology names
// it will be used in ontology form
// because ontology name has to be unique
const name = this._resourceClassFormService.getOntologyName(ontoMeta.id);
this.existingOntologyNames.push(name);

// get each ontology
this._dspApiConnection.v2.onto.getOntology(ontoMeta.id, true).subscribe(
(response: ReadOntology) => {
this.ontologies.push(response);

if (ontoMeta.id === this.ontologyIri) {
// one ontology is selected:
// get all information to display this ontology
// with all classes, properties and connected lists
this.loadOntology = true;
this.ontology = this.ontologies.find(onto => onto.id === this.ontologyIri);
this._cache.set('currentOntology', this.ontology);
this.ontology = response;

// grab the onto class information to display
const allOntoClasses = response.getAllClassDefinitions();

// reset the ontology classes
this.ontoClasses = [];

// display only the classes which are not a subClass of Standoff
allOntoClasses.forEach(resClass => {
const splittedSubClass = resClass.subClassOf[0].split('#');
if (!splittedSubClass[0].includes(Constants.StandoffOntology) && !splittedSubClass[1].includes('Standoff')) {
this.ontoClasses.push(resClass);
}
});
// sort classes by label
// --> TODO: add sort functionallity to the gui
this.ontoClasses = this._sortingService.keySortByAlphabetical(this.ontoClasses, 'label');

// grab the onto properties information to display
const allOntoProperties = response.getAllPropertyDefinitions();

// reset the ontology properties
this.ontoProperties = [];

// display only the properties which are not a subjectType of Standoff
allOntoProperties.forEach(resProp => {
const standoff = (resProp.subjectType ? resProp.subjectType.includes('Standoff') : false);
if (resProp.objectType !== Constants.LinkValue && !standoff) {
this.ontoProperties.push(resProp);
}
});
// sort properties by label
// --> TODO: add sort functionallity to the gui
this.ontoProperties = this._sortingService.keySortByAlphabetical(this.ontoProperties, 'label');
if (this.loadOntology) {
this.setCache();
}
}
},
(error: ApiResponseError) => {
this._errorHandler.showMessage(error);
}
);

if (index === array.length - 1) {
this._cache.set('currentProjectOntologies', this.ontologies);
this.setCache();
}

});

}

},
Expand Down Expand Up @@ -375,7 +361,7 @@ export class OntologyComponent implements OnInit {
openResourceClassForm(mode: 'createResourceClass' | 'editResourceClass', resClassInfo: DefaultClass): void {

// set cache for ontology and lists
this.setCache();
// this.setCache();

const dialogConfig: MatDialogConfig = {
disableClose: true,
Expand Down Expand Up @@ -406,7 +392,7 @@ export class OntologyComponent implements OnInit {
updateCard(subClassOf: ResourceClassDefinition) {

// set cache for ontology and lists
this.setCache();
// this.setCache();

const dialogConfig: MatDialogConfig = {
disableClose: true,
Expand Down Expand Up @@ -509,9 +495,8 @@ export class OntologyComponent implements OnInit {
this._dspApiConnection.admin.listsEndpoint.getListsInProject(this.project.id).subscribe(
(response: ApiResponseData<ListsResponse>) => {
this._cache.set('currentOntologyLists', response.body.lists);

this.loading = false;
this.loadOntology = false;
this.loading = false;
},
(error: ApiResponseError) => {
this._errorHandler.showMessage(error);
Expand Down

0 comments on commit d174ba2

Please sign in to comment.