From d5f9627daded7bf2c7beecfb90964a5091ee36f2 Mon Sep 17 00:00:00 2001 From: Pat McBennett Date: Mon, 2 Oct 2023 03:52:50 +0100 Subject: [PATCH] Pmcb/add type getter tests (#1014) * better implementation of RDF type checking * update CHANGELOG --- CHANGELOG.md | 2 + src/VocabTerm.test.ts | 9 ++++ src/VocabTerm.ts | 97 ++++++++++++++----------------------------- tsconfig.json | 2 +- 4 files changed, 43 insertions(+), 67 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index facda68c..d012890b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html ## Unreleased +- Better implementation of RDF type checkers. + ## 1.3.0 2023/10/02 - Rename RDF type Class and Property getters (to make them RDF-specific). diff --git a/src/VocabTerm.test.ts b/src/VocabTerm.test.ts index da30d39f..4b688121 100755 --- a/src/VocabTerm.test.ts +++ b/src/VocabTerm.test.ts @@ -621,9 +621,18 @@ describe("VocabTerm tests", () => { .addType(TEST_TERM_NAME); expect(myTerm.type!.size).toBe(1); + expect(myTerm.isRdfClass).toBe(false); + expect(myTerm.isRdfProperty).toBe(false); myTerm.addType(rdfFactory.namedNode("https://example.com/myNewType")); expect(myTerm.type!.size).toBe(2); + expect(myTerm.isRdfClass).toBe(false); + + myTerm.addType( + rdfFactory.namedNode("http://www.w3.org/2000/01/rdf-schema#Class"), + ); + expect(myTerm.type!.size).toBe(3); + expect(myTerm.isRdfClass).toBe(true); }); it("should handle rdf:type of Class", () => { diff --git a/src/VocabTerm.ts b/src/VocabTerm.ts index 88f34dac..4d1595a5 100755 --- a/src/VocabTerm.ts +++ b/src/VocabTerm.ts @@ -39,21 +39,23 @@ import { IriString } from "./index"; const DEFAULT_LOCALE = "en"; -// We need an instance of an RDF Factory to instantiate a Named Node, but we -// only want to create instances of these RDF types if we are checking for the -// -let LAZY_TYPE_RDFS_CLASS: NamedNode | undefined = undefined; -let LAZY_TYPE_OWL_CLASS: NamedNode | undefined = undefined; - -let LAZY_TYPE_RDF_PROPERTY: NamedNode | undefined = undefined; -let LAZY_TYPE_OWL_DATATYPE_PROPERTY: NamedNode | undefined = undefined; -let LAZY_TYPE_OWL_OBJECT_PROPERTY: NamedNode | undefined = undefined; -let LAZY_TYPE_OWL_ANNOTATION_PROPERTY: NamedNode | undefined = undefined; -let LAZY_TYPE_OWL_TRANSITIVE_PROPERTY: NamedNode | undefined = undefined; -let LAZY_TYPE_OWL_FUNCTIONAL_PROPERTY: NamedNode | undefined = undefined; -let LAZY_TYPE_OWL_SYMMETRIC_PROPERTY: NamedNode | undefined = undefined; -let LAZY_TYPE_OWL_INVERSE_FUNCTIONAL_PROPERTY: NamedNode | undefined = - undefined; +// Array of RDF types that we consider 'Classes'. +const RDF_TYPE_CLASS = [ + "http://www.w3.org/2000/01/rdf-schema#Class", + "http://www.w3.org/2002/07/owl#Class", +]; + +// Array of RDF types that we consider 'Properties'. +const RDF_TYPE_PROPERTY = [ + "http://www.w3.org/1999/02/22-rdf-syntax-ns#Property", + "http://www.w3.org/2002/07/owl#Property", + "http://www.w3.org/2002/07/owl#ObjectProperty", + "http://www.w3.org/2002/07/owl#AnnotationProperty", + "http://www.w3.org/2002/07/owl#TransitiveProperty", + "http://www.w3.org/2002/07/owl#FunctionalProperty", + "http://www.w3.org/2002/07/owl#InverseFunctionalProperty", + "http://www.w3.org/2002/07/owl#SymmetricProperty", +]; /** * Class to represent vocabulary terms. We expect derived classes to extend @@ -265,68 +267,31 @@ class VocabTerm implements NamedNode { return message && message.value; } - createNamedNodeConstantsClass(): void { - LAZY_TYPE_RDFS_CLASS = this.rdfFactory.namedNode( - "http://www.w3.org/2000/01/rdf-schema#Class", - ); - - LAZY_TYPE_OWL_CLASS = this.rdfFactory.namedNode( - "http://www.w3.org/2002/07/owl#Property", - ); - } - - createNamedNodeConstantsProperty(): void { - LAZY_TYPE_RDF_PROPERTY = this.rdfFactory.namedNode( - "http://www.w3.org/1999/02/22-rdf-syntax-ns#Property", - ); - LAZY_TYPE_OWL_DATATYPE_PROPERTY = this.rdfFactory.namedNode( - "http://www.w3.org/2002/07/owl#Property", - ); - LAZY_TYPE_OWL_OBJECT_PROPERTY = this.rdfFactory.namedNode( - "http://www.w3.org/2002/07/owl#ObjectProperty", - ); - LAZY_TYPE_OWL_ANNOTATION_PROPERTY = this.rdfFactory.namedNode( - "http://www.w3.org/2002/07/owl#AnnotationProperty", - ); - LAZY_TYPE_OWL_TRANSITIVE_PROPERTY = this.rdfFactory.namedNode( - "http://www.w3.org/2002/07/owl#TransitiveProperty", - ); - LAZY_TYPE_OWL_FUNCTIONAL_PROPERTY = this.rdfFactory.namedNode( - "http://www.w3.org/2002/07/owl#FunctionalProperty", - ); - LAZY_TYPE_OWL_INVERSE_FUNCTIONAL_PROPERTY = this.rdfFactory.namedNode( - "http://www.w3.org/2002/07/owl#InverseFunctionalProperty", - ); - LAZY_TYPE_OWL_SYMMETRIC_PROPERTY = this.rdfFactory.namedNode( - "http://www.w3.org/2002/07/owl#SymmetricProperty", - ); - } - get isRdfClass(): boolean { - if (!LAZY_TYPE_RDFS_CLASS) { - this.createNamedNodeConstantsClass(); + if (!this._type) { + return false; } + // Spread our types Set into an array to find the first (if any) + // occurrence of array of matching types. return ( - (this._type?.has(LAZY_TYPE_RDFS_CLASS!) || - this._type?.has(LAZY_TYPE_OWL_CLASS!)) !== undefined + [...this._type].find((rdfType) => + RDF_TYPE_CLASS.includes(rdfType.value), + ) !== undefined ); } get isRdfProperty(): boolean { - if (!LAZY_TYPE_RDF_PROPERTY) { - this.createNamedNodeConstantsProperty(); + if (!this._type) { + return false; } + // Spread our types Set into an array to find the first (if any) + // occurrence of array of matching types. return ( - (this._type?.has(LAZY_TYPE_RDF_PROPERTY!) || - this._type?.has(LAZY_TYPE_OWL_OBJECT_PROPERTY!) || - this._type?.has(LAZY_TYPE_OWL_DATATYPE_PROPERTY!) || - this._type?.has(LAZY_TYPE_OWL_ANNOTATION_PROPERTY!) || - this._type?.has(LAZY_TYPE_OWL_FUNCTIONAL_PROPERTY!) || - this._type?.has(LAZY_TYPE_OWL_INVERSE_FUNCTIONAL_PROPERTY!) || - this._type?.has(LAZY_TYPE_OWL_SYMMETRIC_PROPERTY!) || - this._type?.has(LAZY_TYPE_OWL_TRANSITIVE_PROPERTY!)) !== undefined + [...this._type].find((rdfType) => + RDF_TYPE_PROPERTY.includes(rdfType.value), + ) !== undefined ); } diff --git a/tsconfig.json b/tsconfig.json index 94eb1a30..1b55bfb2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,7 @@ /* Basic Options */ "target": "es2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */ "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ - "lib": ["es2015", "dom"], /* Specify library files to be included in the compilation. */ + "lib": ["es2017", "dom"], /* Specify library files to be included in the compilation. */ "allowJs": false, /* Allow javascript files to be compiled. */ // "checkJs": true, /* Report errors in .js files. */ // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */