Skip to content

Commit

Permalink
Pmcb/add type getter tests (#1014)
Browse files Browse the repository at this point in the history
* better implementation of RDF type checking

* update CHANGELOG
  • Loading branch information
pmcb55 authored Oct 2, 2023
1 parent 9f0d0fc commit d5f9627
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 67 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
9 changes: 9 additions & 0 deletions src/VocabTerm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
97 changes: 31 additions & 66 deletions src/VocabTerm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
);
}

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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'. */
Expand Down

0 comments on commit d5f9627

Please sign in to comment.