From b96622d64fde2af06a74b807dc74f076d4dde69f Mon Sep 17 00:00:00 2001 From: na57 Date: Wed, 22 May 2024 16:10:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=94=99=E8=AF=AF=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=90=8DcreateRdfResource?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 12 ++++++------ package.json | 4 ++-- src/index.ts | 45 ++++++++++++++++++++++++++------------------- yarn.lock | 8 ++++---- 4 files changed, 38 insertions(+), 31 deletions(-) diff --git a/package-lock.json b/package-lock.json index d0d843d..944a1fa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "nagu-owl", - "version": "1.11.0", + "version": "1.11.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "nagu-owl", - "version": "1.11.0", + "version": "1.11.2", "license": "MIT", "dependencies": { - "nagu-owl-types": "^1.1.1", + "nagu-owl-types": "^1.2.0", "nagu-triples": "^2.0.5", "nagu-triples-types": "^1.0.2" }, @@ -2735,9 +2735,9 @@ } }, "node_modules/nagu-owl-types": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/nagu-owl-types/-/nagu-owl-types-1.1.1.tgz", - "integrity": "sha512-lBU015q3CQkFeJ+ED9B6T0hre7Sgcdld92L94SRHlaSom1cdVm+eD1hf8bMZAit0YkLjz9b6t9xGY2ecnoVbeQ==", + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/nagu-owl-types/-/nagu-owl-types-1.2.0.tgz", + "integrity": "sha512-xQI7EZPQJYqYpT/cJNM0CFz/YRgvQpFiCC0wm8cYlVuysIJDxcnizVot54aoQoLWqfqKRml0mpaTUAqvmQe2QA==", "dependencies": { "nagu-triples-types": "^1.0.2" } diff --git a/package.json b/package.json index 8873fb4..0d3511b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nagu-owl", - "version": "1.11.1", + "version": "1.11.3", "description": "[![Node.js CI](https://github.com/nagucc/owl/actions/workflows/node.js.yml/badge.svg)](https://github.com/nagucc/owl/actions/workflows/node.js.yml)", "type": "module", "main": "dist/index.js", @@ -40,7 +40,7 @@ "typedoc": "^0.25.11" }, "dependencies": { - "nagu-owl-types": "^1.1.1", + "nagu-owl-types": "^1.2.0", "nagu-triples": "^2.0.5", "nagu-triples-types": "^1.0.2" }, diff --git a/src/index.ts b/src/index.ts index 5086b98..017608c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,18 +9,13 @@ import triples from 'nagu-triples'; import { Notion } from 'nagu-triples/dist/notions'; import { rdf, rdfs } from './constants'; import { INotion, ITriple } from 'nagu-triples-types'; -import { IAnnotations, IRdfProperty, IRdfsClass, IRdfsResource } from 'nagu-owl-types'; +import { AnnotationProps, IRdfProperty, IRdfsClass, IRdfsResource } from 'nagu-owl-types'; export class Factory { options: any; constructor(options) { this.options = options; } - async createRdfResource (uri: INotion|string, forceInit: boolean = true) { - const res = new RdfsResource(uri, this.options); - if (forceInit) await res.init(); - return res; - } async createRdfsResource (uri: INotion|string, forceInit: boolean = true) { const res = new RdfsResource(uri, this.options); if (forceInit) await res.init(); @@ -133,42 +128,54 @@ export class RdfsResource extends Notion implements IRdfsResource { label: string|Notion; comment: string|Notion; - isDefinedBy: string|Notion; + isDefinedBy: string|Notion|IRdfsResource; seeAlso: string|Notion; /** * 删除原值后添加新值 */ - async setAnnotations({ label, comment, isDefinedBy, seeAlso }): Promise { + async setAnnotations(annotations: AnnotationProps): Promise { + const { label, comment, isDefinedBy, seeAlso } = annotations; const operations = []; // 删除原值 if (this.label) operations.push(this.removePropertyValue(rdfs.label, this.label)); if (this.comment) operations.push(this.removePropertyValue(rdfs.comment, this.comment)); - if (this.isDefinedBy) operations.push(this.removePropertyValue(rdfs.isDefinedBy, this.isDefinedBy)); + if (this.isDefinedBy) operations.push(this.removePropertyValue(rdfs.isDefinedBy, this.isDefinedBy as INotion)); if (this.seeAlso) operations.push(this.removePropertyValue(rdfs.seeAlso, this.seeAlso)); await Promise.all(operations); // 设置新值 - const setOps = [] - if (label) setOps.push(this.setPropertyValue(rdfs.label, label)); - if (comment) setOps.push(this.setPropertyValue(rdfs.comment, comment)); - if (isDefinedBy) setOps.push(this.setPropertyValue(rdfs.isDefinedBy, isDefinedBy)); - if (seeAlso) setOps.push(this.setPropertyValue(rdfs.seeAlso, seeAlso)); - const ts = await Promise.all(setOps); + const ts = await Promise.all([ + this.setPropertyValue(rdfs.label, label), + this.setPropertyValue(rdfs.comment, comment), + this.setPropertyValue(rdfs.isDefinedBy, isDefinedBy as INotion), + this.setPropertyValue(rdfs.seeAlso, seeAlso), + ]); // 修改自身变量 this.label = ts[0]?.object; this.comment = ts[1]?.object; - this.isDefinedBy = ts[2]?.object; this.seeAlso = ts[3]?.object; + + // 设置isDefinedBy字段 + const isDefinedByResource = new RdfsResource(ts[2]?.object, this.options); + await isDefinedByResource.getAnnotations(); + this.isDefinedBy = isDefinedByResource; } - async getAnnotations(): Promise { - const [labels, comments, isDefinedBy, seeAlsos] = await Promise.all([ + async getAnnotations(): Promise { + const [labels, comments, isDefinedBys, seeAlsos] = await Promise.all([ rdfs.label, rdfs.comment, rdfs.isDefinedBy, rdfs.seeAlso, ].map(p => this.getPropertyValues(p))); this.label = (labels || [])[0]?.toString() || ''; this.comment = (comments || [])[0]?.toString() || ''; - this.isDefinedBy = (isDefinedBy || [])[0]?.toString() || ''; this.seeAlso = (seeAlsos || [])[0]?.toString() || ''; + + // 设置isDefinedBy字段 + const isDefinedByIRI = (isDefinedBys || [])[0]?.toString() || ''; + if (isDefinedByIRI) { + const isDefinedByResource = new RdfsResource(isDefinedByIRI, this.options); + await isDefinedByResource.getAnnotations(); + this.isDefinedBy = isDefinedByResource; + } return this; } diff --git a/yarn.lock b/yarn.lock index 826c984..777a1f7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1632,10 +1632,10 @@ mysql2@^3.9.7: seq-queue "^0.0.5" sqlstring "^2.3.2" -nagu-owl-types@^1.1.1: - version "1.1.1" - resolved "https://registry.npmjs.org/nagu-owl-types/-/nagu-owl-types-1.1.1.tgz" - integrity sha512-lBU015q3CQkFeJ+ED9B6T0hre7Sgcdld92L94SRHlaSom1cdVm+eD1hf8bMZAit0YkLjz9b6t9xGY2ecnoVbeQ== +nagu-owl-types@^1.2.0: + version "1.2.0" + resolved "https://registry.npmjs.org/nagu-owl-types/-/nagu-owl-types-1.2.0.tgz" + integrity sha512-xQI7EZPQJYqYpT/cJNM0CFz/YRgvQpFiCC0wm8cYlVuysIJDxcnizVot54aoQoLWqfqKRml0mpaTUAqvmQe2QA== dependencies: nagu-triples-types "^1.0.2"