Skip to content

Commit

Permalink
Add Convenience Methods to Ontology Information (#265)
Browse files Browse the repository at this point in the history
* refactor (ontology utils): make utility method to filter property defs

* feature (ontology): add helper methods for class defs

* refactor (ontology utils): make util methods generic

* refactor (ontology): make interface an abstract class

* tests (ontologies): add more assertions

* docs (ontologies): use @category tag
  • Loading branch information
tobiasschweizer authored Dec 7, 2020
1 parent fc1c086 commit 31a3044
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 14 deletions.
23 changes: 23 additions & 0 deletions src/api/v2/ontology/ontologies-endpoint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { SystemPropertyDefinition } from "../../../models/v2/ontologies/system-p
import { UpdateOntology } from "../../../models/v2/ontologies/update/update-ontology";
import { UpdateOntologyResourceClassCardinality } from "../../../models/v2/ontologies/update/update-ontology-resource-class-cardinality";
import { StringLiteralV2 } from "../../../models/v2/string-literal-v2";
import { StandoffClassDefinition } from "../../../models/v2/ontologies/standoff-class-definition";

describe("OntologiesEndpoint", () => {

Expand Down Expand Up @@ -143,6 +144,28 @@ describe("OntologiesEndpoint", () => {
expect((response.properties["http://0.0.0.0:3333/ontology/0001/anything/v2#hasListItem"] as ResourcePropertyDefinition).isLinkValueProperty).toBeFalsy();
expect((response.properties["http://0.0.0.0:3333/ontology/0001/anything/v2#hasListItem"] as ResourcePropertyDefinition).guiAttributes).toEqual(["hlist=<http://rdfh.ch/lists/0001/treeList>"]);

const classDefs = response.getAllClassDefinitions();
expect(classDefs.length).toEqual(9);
expect(classDefs[0] instanceof ResourceClassDefinition).toBeTruthy();
expect((classDefs[0] as ResourceClassDefinition).id).toEqual("http://0.0.0.0:3333/ontology/0001/anything/v2#BlueThing");

const resClassDefs = response.getClassDefinitionsByType(ResourceClassDefinition);
expect(resClassDefs.length).toEqual(8);
expect(resClassDefs[0].id).toEqual("http://0.0.0.0:3333/ontology/0001/anything/v2#BlueThing");

const standoffClassDefs = response.getClassDefinitionsByType(StandoffClassDefinition);
expect(standoffClassDefs.length).toEqual(1);
expect(standoffClassDefs[0].id).toEqual("http://0.0.0.0:3333/ontology/0001/anything/v2#StandoffEventTag");

const systemProps: SystemPropertyDefinition[] = response.getPropertyDefinitionsByType(SystemPropertyDefinition);
expect(systemProps.length).toEqual(1);
expect(systemProps[0] instanceof SystemPropertyDefinition).toBe(true);
expect(systemProps[0].id).toEqual("http://0.0.0.0:3333/ontology/0001/anything/v2#standoffEventTagHasDescription");

const resourceProps: ResourcePropertyDefinition[] = response.getPropertyDefinitionsByType(ResourcePropertyDefinition);
expect(resourceProps.length).toEqual(28);
expect(resourceProps[0] instanceof ResourcePropertyDefinition).toBe(true);

done();
});

Expand Down
1 change: 0 additions & 1 deletion src/cache/ontology-cache/OntologyCache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ describe("OntologyCache", () => {
knoraApiConnection.v2.ontologyCache.getResourceClassDefinition("http://0.0.0.0:3333/ontology/0001/anything/v2#Thing").subscribe(
resClassDef => {
const systemProps: SystemPropertyDefinition[] = resClassDef.getPropertyDefinitionsByType(SystemPropertyDefinition);

expect(systemProps.length).toEqual(13);
expect(systemProps.length).toEqual(MockOntologyAssertions.systemPropertyIndexesAnythingThing.length);
expect(systemProps.map(prop => prop.id).sort()).toEqual(MockOntologyAssertions.systemPropertyIndexesAnythingThing.sort());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ClassAndPropertyDefinitions } from "../../models/v2/ontologies/ClassAndPropertyDefinitions";
import { PropertyDefinition } from "../../models/v2/ontologies/property-definition";
import { TypeGuard } from "../../models/v2/resources/type-guard";
import { ResourceClassDefinitionWithPropertyDefinition } from "./resource-class-definition-with-property-definition";
Expand All @@ -8,7 +9,7 @@ import { ResourceClassDefinitionWithPropertyDefinition } from "./resource-class-
*
* @category Model V2
*/
export class ResourceClassAndPropertyDefinitions {
export class ResourceClassAndPropertyDefinitions extends ClassAndPropertyDefinitions {

/**
* Resource class definitions and their cardinalities.
Expand All @@ -21,7 +22,7 @@ export class ResourceClassAndPropertyDefinitions {
properties: { [index: string]: PropertyDefinition };

constructor(resClassDefs: { [index: string]: ResourceClassDefinitionWithPropertyDefinition }, propDefs: { [index: string]: PropertyDefinition }) {

super();
this.classes = resClassDefs;
this.properties = propDefs;
}
Expand All @@ -30,11 +31,7 @@ export class ResourceClassAndPropertyDefinitions {
* Gets all property definitions from the resource's entity info.
*/
getAllPropertyDefinitions(): PropertyDefinition[] {
const propIndexes = Object.keys(this.properties);

return propIndexes.map((propIndex: string) => {
return this.properties[propIndex];
});
return this.getAllEntityDefinitionsAsArray(this.properties);
}

/**
Expand All @@ -43,10 +40,6 @@ export class ResourceClassAndPropertyDefinitions {
* @param type restriction to a certain property definition type.
*/
getPropertyDefinitionsByType<T extends PropertyDefinition>(type: TypeGuard.Constructor<T>): T[] {

return this.getAllPropertyDefinitions().filter(
(prop: PropertyDefinition) => {
return TypeGuard.typeGuard(prop, type);
}) as T[];
return this.getEntityDefinitionsByTypeAsArray(this.getAllPropertyDefinitions(), type);
}
}
44 changes: 44 additions & 0 deletions src/models/v2/ontologies/ClassAndPropertyDefinitions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { TypeGuard } from "../resources/type-guard";
import { ClassDefinition } from "./class-definition";
import { EntityDefinition } from "./EntityDefinition";
import { PropertyDefinition } from "./property-definition";

/**
* @category Internal
*/
export abstract class ClassAndPropertyDefinitions {

abstract properties: { [index: string]: PropertyDefinition };
abstract classes: { [index: string]: ClassDefinition };

abstract getAllPropertyDefinitions(): PropertyDefinition[];
abstract getPropertyDefinitionsByType<T extends PropertyDefinition>(type: TypeGuard.Constructor<T>): T[];

/**
* Given a map of entity Iris to entity definitions,
* returns an array of entity definitions.
*
* @param entityDefs Entity definitions to be returned as an array.
*/
protected getAllEntityDefinitionsAsArray<T extends EntityDefinition>(entityDefs: { [ index: string ]: T } ): T[] {
const entityIndexes = Object.keys(entityDefs);

return entityIndexes.map((entityIndex: string) => {
return entityDefs[entityIndex];
});
}

/**
* Given an array of entity definitions,
* returns only the entity definitions matching the given type.
*
* @param entityDefs The entity definitions to be filtered.
* @param type The type of entity definitions to be returned.
*/
protected getEntityDefinitionsByTypeAsArray<T extends EntityDefinition>(entityDefs: EntityDefinition[], type: TypeGuard.Constructor<T>): T[] {
return entityDefs.filter(
(entityDef: EntityDefinition) => {
return TypeGuard.typeGuard(entityDef, type);
}) as T[];
}
}
1 change: 1 addition & 0 deletions src/models/v2/ontologies/OntologyConversionUtil.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { JsonConvert } from "json2typescript";
import { KnoraApiConfig } from "../../../knora-api-config";
import { Constants } from "../Constants";
import { TypeGuard } from "../resources/type-guard";
import { IHasProperty } from "./class-definition";
import { EntityDefinition } from "./EntityDefinition";
import { OntologiesMetadata, OntologyMetadata } from "./ontology-metadata";
Expand Down
36 changes: 35 additions & 1 deletion src/models/v2/ontologies/read/read-ontology.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { JsonObject, JsonProperty } from "json2typescript";
import { Constants } from "../../Constants";
import { DateTimeStampConverter } from "../../custom-converters/date-time-stamp-converter";
import { TypeGuard } from "../../resources/type-guard";
import { ClassDefinition } from "../class-definition";
import { ClassAndPropertyDefinitions } from "../ClassAndPropertyDefinitions";
import { PropertyDefinition } from "../property-definition";

/**
* @category Model V2
*/
@JsonObject("ReadOntology")
export class ReadOntology {
export class ReadOntology extends ClassAndPropertyDefinitions {

@JsonProperty("@id", String)
id: string = "";
Expand All @@ -24,4 +26,36 @@ export class ReadOntology {

dependsOnOntologies: Set<string>;

/**
* Gets all class definitions from the ontology's entity info.
*/
getAllClassDefinitions(): ClassDefinition[] {
return this.getAllEntityDefinitionsAsArray(this.classes);
}

/**
* Gets class definitions restricted by type from the ontology's entity info.
*
* @param type restriction to a certain class definition type.
*/
getClassDefinitionsByType<T extends ClassDefinition>(type: TypeGuard.Constructor<T>): T[] {
return this.getEntityDefinitionsByTypeAsArray(this.getAllClassDefinitions(), type);
}

/**
* Gets all property definitions from the ontology's entity info.
*/
getAllPropertyDefinitions(): PropertyDefinition[] {
return this.getAllEntityDefinitionsAsArray(this.properties);
}

/**
* Gets property definitions restricted by type from the ontology's entity info.
*
* @param type restriction to a certain property definition type.
*/
getPropertyDefinitionsByType<T extends PropertyDefinition>(type: TypeGuard.Constructor<T>): T[] {
return this.getEntityDefinitionsByTypeAsArray(this.getAllPropertyDefinitions(), type);
}

}

0 comments on commit 31a3044

Please sign in to comment.