Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pmcb/add type getter tests #1014

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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