From 5efed7213f8c2746924e21430dc6113d06bbc907 Mon Sep 17 00:00:00 2001 From: mdelez <60604010+mdelez@users.noreply.github.com> Date: Fri, 10 Jun 2022 13:58:34 +0200 Subject: [PATCH] refactor(link-value): recursively loop through all of a superclass's subclasses (#763) --- .../values/link-value/link-value.component.ts | 38 ++++++++++++++++--- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/app/workspace/resource/values/link-value/link-value.component.ts b/src/app/workspace/resource/values/link-value/link-value.component.ts index 8996ffdc85..bb1edd47ad 100644 --- a/src/app/workspace/resource/values/link-value/link-value.component.ts +++ b/src/app/workspace/resource/values/link-value/link-value.component.ts @@ -68,6 +68,7 @@ export class LinkValueComponent extends BaseValueDirective implements OnInit, On customValidators = [resourceValidator]; resourceClasses: ResourceClassDefinition[]; + subClasses: ResourceClassDefinition[] = []; properties: ResourcePropertyDefinition[]; loadingResults = false; @@ -143,15 +144,15 @@ export class LinkValueComponent extends BaseValueDirective implements OnInit, On this._dspApiConnection.v2.ontologyCache.getOntology(this.currentOntoIri).subscribe( (onto: Map) => { const resClasses = onto.get(this.currentOntoIri).getClassDefinitionsByType(ResourceClassDefinition); + + // add the superclass to the list of resource classes this.resourceClasses = resClasses.filter( (resClassDef: ResourceClassDefinition) => resClassDef.id === this.restrictToResourceClass ); - const subclasses = resClasses.filter( - (resClassDef: ResourceClassDefinition) => - resClassDef.subClassOf.indexOf(this.restrictToResourceClass) > -1 - ); - this.resourceClasses = this.resourceClasses.concat(subclasses); + // recursively loop through all of the superclass's subclasses, including nested subclasses + // and add them to the list of resource classes + this.resourceClasses = this.resourceClasses.concat(this._getSubclasses(resClasses, this.restrictToResourceClass)); this.properties = onto.get(this.currentOntoIri).getPropertyDefinitionsByType(ResourcePropertyDefinition); }, @@ -289,4 +290,31 @@ export class LinkValueComponent extends BaseValueDirective implements OnInit, On } }); } + + /** + * given a resource class Iri, return all subclasses + * @param resClasses List of all resource class definitions in the ontology + * @param resClassRestrictionIri The IRI of the resource class to filter the list of resource class defintions by + * @returns An array of all subclasses of type ResourceClassDefinition + */ + private _getSubclasses(resClasses: ResourceClassDefinition[], resClassRestrictionIri: string): ResourceClassDefinition[] { + + // filter list by the provided IRI to find all subclasses of the provided IRI + const subclasses = resClasses.filter( + (resClassDef: ResourceClassDefinition) => + resClassDef.subClassOf.indexOf(resClassRestrictionIri) > -1 + ); + + // add the filtered list to the list of all subclasses + this.subClasses = this.subClasses.concat(subclasses); + + // if the provided IRI has subclasses, recursively call this function to find any subclasses of those subclasses + if (subclasses.length){ + subclasses.forEach((sub) => { + this._getSubclasses(resClasses, sub.id); + }); + } + + return this.subClasses; + } }