Skip to content

Commit

Permalink
改为ts
Browse files Browse the repository at this point in the history
  • Loading branch information
na57 committed May 17, 2024
1 parent 16090bf commit f1edc32
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 42 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "nagu-owl",
"version": "1.3.1",
"version": "1.5.0",
"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": "src/index.js",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"start": "nodemon src/index.js",
"doc": "yarn jsdoc src",
Expand Down Expand Up @@ -40,7 +41,7 @@
"commander": "^12.0.0",
"figlet": "^1.7.0",
"inquirer": "^9.2.15",
"nagu-triples": "^1.3.1",
"nagu-triples": "^2.0.1",
"ora": "^8.0.1"
},
"directories": {
Expand Down
73 changes: 45 additions & 28 deletions src/index.js → src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
*/

import triples from 'nagu-triples';
import { Notion } from 'nagu-triples/dist/notions';
import { Triple } from 'nagu-triples/dist/triples';

const rdfPrefix = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
const rdf = {
export const rdf = {
type: `${rdfPrefix}type`,
Property: `${rdfPrefix}Property`,
Statement: `${rdfPrefix}Statement`,
Expand Down Expand Up @@ -47,23 +49,27 @@ export const rdfs = {
member: `${rdfsPrefix}member`,
}

export interface IRdfsClass {
instances(): Promise<Array<RdfsResource>>;
}
export class Factory {
options: any;
constructor(options) {
this.options = options;
}
async createRdfResource (uri) {
async createRdfResource (uri: Notion<string>|string, forceInit: boolean = true) {
const res = new RdfsResource(uri, this.options);
await res.init();
if (forceInit) await res.init();
return res;
}
async createRdfProperty (uri) {
async createRdfProperty (uri: Notion<string>|string, forceInit: boolean = true) {
const res = new RdfProperty(uri, this.options);
await res.init();
if (forceInit) await res.init();
return res;
}
async createRdfsClass(uri) {
async createRdfsClass(uri: Notion<string>|string, forceInit: boolean = true) {
const res = new RdfsClass(uri, this.options);
await res.init();
if (forceInit) await res.init();
return res;
}
async init () {
Expand Down Expand Up @@ -150,26 +156,31 @@ export class Factory {
* RDF Resource, rdf:Resource 表示RDF中被描述的资源
* - 应专注于对"属性"的管理,而非属性值(当下最重要的是实现功能!!)
*/
class RdfsResource {
constructor (uri, options) {
this.uri = uri.toString();
export class RdfsResource extends Notion<string> {
public uri: string;
options: any;
properties: any[];
constructor (public iri: Notion<string>|string, options) {
super(iri.toString());
this.iri = iri;
this.uri = iri.toString();
this.options = options;
this.properties = [];
}

async getPropertyValues(property) {
async getPropertyValues(property: RdfProperty | string): Promise<Array<RdfsResource>> {
// 获取指定属性的所有值
if (!(property instanceof RdfProperty)) property = new RdfProperty(property, this.options);
// 1. 判断给定property是不是属性
if (!(await property.check())) throw new Error(`${property}违反 RDF axioms 2, 无法获取值`);

// 2. 获取所有值
const result = (await triples.listBySP(this, property, this.options)).map(s => new RdfsResource(s.object.name));
this.properties[property] = result;
const result = (await triples.listBySP(this.iri, property, this.options)).map(s => new RdfsResource(s.object.name, this.options));
this.properties[property.toString()] = result;
return result;
}

async setPropertyValue(property, value) {
async setPropertyValue(property: RdfProperty|string, value: string | Notion<any>): Promise<Triple> {
if (!(property instanceof RdfProperty)) property = new RdfProperty(property, this.options);
// 1. 检查property
if (!(await property.check())) throw new Error(`${property} 不是 rdf:Property, 不能设置属性值`);
Expand All @@ -178,7 +189,7 @@ class RdfsResource {
return triples.getOrCreate(this, property, value, this.options);
}

async removePropertyValue(property, value) {
async removePropertyValue(property: string | RdfProperty, value: string | Notion<any>): Promise<number> {
// 删除属性的值
if (!(property instanceof RdfProperty)) property = new RdfProperty(property, this.options);
// 1. 检查property
Expand Down Expand Up @@ -218,7 +229,10 @@ class RdfsResource {
}
}

class RdfsClass extends RdfsResource {
export class RdfsClass extends RdfsResource implements IRdfsClass {
instances(): Promise<RdfsResource[]> {
throw new Error('Method not implemented.');
}
async init() {
await this.setPropertyValue(rdf.type, rdfs.Class);
}
Expand All @@ -227,27 +241,31 @@ class RdfsClass extends RdfsResource {
}
}

class RdfProperty extends RdfsResource {
constructor (uri, options) {
export class RdfProperty extends RdfsResource implements IRdfsClass {
constructor (uri: Notion<any>|string, options) {
super(uri, options)
}
async instances(): Promise<RdfsResource[]> {
const result = await triples.listByPO(rdf.type, rdf.Property, this.options);
return result.map(t => new RdfsResource(t.subject, this.options));
}
async domians() {
return this.getPropertyValues(RDFS.terms.domain);
return this.getPropertyValues(rdfs.domain);
}
async addDomain(resource) {
return this.setPropertyValue(RDFS.terms.domain, resource);
return this.setPropertyValue(rdfs.domain, resource);
}
async removeDomain(resource) {
return this.removePropertyValue(RDFS.terms.domain, resource);
return this.removePropertyValue(rdfs.domain, resource);
}
async ranges() {
return this.getPropertyValues(RDFS.terms.range);
return this.getPropertyValues(rdfs.range);
}
async addRange(resource) {
return this.setPropertyValue(RDFS.terms.range, resource);
return this.setPropertyValue(rdfs.range, resource);
}
async removeRange(resource) {
return this.removePropertyValue(RDFS.terms.range, resource);
return this.removePropertyValue(rdfs.range, resource);
}
async init() {
// RDF axioms 2. 设置rdf:type 为rdf:Property
Expand All @@ -257,11 +275,10 @@ class RdfProperty extends RdfsResource {
await this.removePropertyValue(rdf.type, rdf.Property);
}
// 检查当前对象是否是Property
async check() {
// @ts-ignore
if (this == rdf.type) return true; // RDF axioms 1
async check(): Promise<boolean> {
if (this.toString() == rdf.type) return true; // RDF axioms 1
const res = await this.getPropertyValues(rdf.type);
return res.some(o => o == rdf.Property);
return res.some(o => o.toString() == rdf.Property);
}
}

Expand Down
3 changes: 2 additions & 1 deletion test/rdf-property.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ describe('RdfProperty 类', () => {
const res = await property.check();
assert.equal(res, false);
})
});
});

12 changes: 12 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"module": "ES6", // 或 "ES2015", "ESNext"
"target": "ES6", // 或 "ES2015", "ESNext"
"moduleResolution": "node",
"declaration": true, // 如果需要发布声明文件
"outDir": "./dist", // 输出目录
// 其他选项...
},
"include": ["src/**/*"], // 指定要包含的文件
"exclude": ["node_modules", "dist"] // 指定要排除的文件
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1516,10 +1516,10 @@ mysql2@^3.9.7:
seq-queue "^0.0.5"
sqlstring "^2.3.2"

nagu-triples@^1.3.1:
version "1.3.1"
resolved "https://registry.npmjs.org/nagu-triples/-/nagu-triples-1.3.1.tgz"
integrity sha512-9e3k2l4Xdp9Vz/Vx/j8u2E4CICSzenXQFst3tZXE8Co8FptuU+Bi6wwFQP4DhmoAXN5xOnCErm1joMCYoHN4zQ==
nagu-triples@^2.0.1:
version "2.0.1"
resolved "https://registry.npmjs.org/nagu-triples/-/nagu-triples-2.0.1.tgz"
integrity sha512-V7yvkw3zjfUPIhNErNcGvUvsO+vXLfUqyhNlORvKSMPce6y+zM6UVx+nJ/rBTH8Q0HX1mOHKgQ86onrDK3mu4w==
dependencies:
chalk "^5.3.0"
commander "^12.0.0"
Expand Down

0 comments on commit f1edc32

Please sign in to comment.