Skip to content

Commit

Permalink
一批更新
Browse files Browse the repository at this point in the history
  • Loading branch information
na57 committed May 20, 2024
1 parent f1edc32 commit fec0160
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 57 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.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nagu-owl",
"version": "1.5.0",
"version": "1.9.1",
"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",
Expand Down Expand Up @@ -41,7 +41,7 @@
"commander": "^12.0.0",
"figlet": "^1.7.0",
"inquirer": "^9.2.15",
"nagu-triples": "^2.0.1",
"nagu-triples": "^2.0.5",
"ora": "^8.0.1"
},
"directories": {
Expand Down
39 changes: 39 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const rdfPrefix = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
export const rdf = {
type: `${rdfPrefix}type`,
Property: `${rdfPrefix}Property`,
Statement: `${rdfPrefix}Statement`,
subject: `${rdfPrefix}subject`,
predicate: `${rdfPrefix}predicate`,
object: `${rdfPrefix}object`,
Bag: `${rdfPrefix}Bag`,
Seq: `${rdfPrefix}Seq`,
Alt: `${rdfPrefix}Alt`,
value: `${rdfPrefix}value`,
List: `${rdfPrefix}List`,
nil: `${rdfPrefix}nil`,
first: `${rdfPrefix}first`,
rest: `${rdfPrefix}rest`,
XMLLiteral: `${rdfPrefix}XMLLiteral`,
JSON: `${rdfPrefix}JSON`,
CompoundLiteral: `${rdfPrefix}CompoundLiteral`,
language: `${rdfPrefix}language`,
direction: `${rdfPrefix}direction`,
}

const rdfsPrefix = 'http://www.w3.org/2000/01/rdf-schema#';
export const rdfs = {
subClassOf: `${rdfsPrefix}subClassOf`,
subPropertyOf: `${rdfsPrefix}subPropertyOf`,
Literal: `${rdfsPrefix}Literal`,
Class: `${rdfsPrefix}Class`,
Resource: `${rdfsPrefix}Resource`,
Container: `${rdfsPrefix}Container`,
Datatype: `${rdfsPrefix}Datatype`,
seeAlso: `${rdfsPrefix}seeAlso`,
label: `${rdfsPrefix}label`,
comment: `${rdfsPrefix}comment`,
domain: `${rdfsPrefix}domain`,
range: `${rdfsPrefix}range`,
member: `${rdfsPrefix}member`,
}
13 changes: 13 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Notion } from "nagu-triples/dist/notions";

export interface IAnnotations {
label: string|Notion<string>|undefined,
comment: string|Notion<string>|undefined,
seeAlso: string|Notion<string>|undefined,
getAnnotations(): Promise<IAnnotations>,
setAnnotations(annotations: {
label: string|Notion<string>|undefined,
comment: string|Notion<string>|undefined,
seeAlso: string|Notion<string>|undefined,
}): Promise<void>,
}
91 changes: 46 additions & 45 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,13 @@
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#';
export const rdf = {
type: `${rdfPrefix}type`,
Property: `${rdfPrefix}Property`,
Statement: `${rdfPrefix}Statement`,
subject: `${rdfPrefix}subject`,
predicate: `${rdfPrefix}predicate`,
object: `${rdfPrefix}object`,
Bag: `${rdfPrefix}Bag`,
Seq: `${rdfPrefix}Seq`,
Alt: `${rdfPrefix}Alt`,
value: `${rdfPrefix}value`,
List: `${rdfPrefix}List`,
nil: `${rdfPrefix}nil`,
first: `${rdfPrefix}first`,
rest: `${rdfPrefix}rest`,
XMLLiteral: `${rdfPrefix}XMLLiteral`,
JSON: `${rdfPrefix}JSON`,
CompoundLiteral: `${rdfPrefix}CompoundLiteral`,
language: `${rdfPrefix}language`,
direction: `${rdfPrefix}direction`,
}

const rdfsPrefix = 'http://www.w3.org/2000/01/rdf-schema#';
export const rdfs = {
subClassOf: `${rdfsPrefix}subClassOf`,
subPropertyOf: `${rdfsPrefix}subPropertyOf`,
Literal: `${rdfsPrefix}Literal`,
Class: `${rdfsPrefix}Class`,
Resource: `${rdfsPrefix}Resource`,
Container: `${rdfsPrefix}Container`,
Datatype: `${rdfsPrefix}Datatype`,
seeAlso: `${rdfsPrefix}seeAlso`,
label: `${rdfsPrefix}label`,
comment: `${rdfsPrefix}comment`,
domain: `${rdfsPrefix}domain`,
range: `${rdfsPrefix}range`,
member: `${rdfsPrefix}member`,
}
import { rdf, rdfs } from './constants';
import { IAnnotations } from './index.d';

export interface IRdfsClass {
instances(): Promise<Array<RdfsResource>>;
}

export class Factory {
options: any;
constructor(options) {
Expand All @@ -62,6 +25,11 @@ export class Factory {
if (forceInit) await res.init();
return res;
}
async createRdfsResource (uri: Notion<string>|string, forceInit: boolean = true) {
const res = new RdfsResource(uri, this.options);
if (forceInit) await res.init();
return res;
}
async createRdfProperty (uri: Notion<string>|string, forceInit: boolean = true) {
const res = new RdfProperty(uri, this.options);
if (forceInit) await res.init();
Expand Down Expand Up @@ -156,17 +124,49 @@ export class Factory {
* RDF Resource, rdf:Resource 表示RDF中被描述的资源
* - 应专注于对"属性"的管理,而非属性值(当下最重要的是实现功能!!)
*/
export class RdfsResource extends Notion<string> {
export class RdfsResource extends Notion<string> implements IAnnotations {
public uri: string;
options: any;
properties: any[];
constructor (public iri: Notion<string>|string, options) {
constructor (public iri: Notion<string>|string, protected options: any) {
super(iri.toString());
this.iri = iri;
this.uri = iri.toString();
this.options = options;
this.properties = [];
}

label: string|Notion<string>;
comment: string|Notion<string>;
seeAlso: string|Notion<string>;
async setAnnotations({ label, comment, seeAlso }): Promise<void> {
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.seeAlso) operations.push(this.removePropertyValue(rdfs.seeAlso, this.seeAlso));

// 设置新值
const setOps = []
if (label) setOps.push(this.setPropertyValue(rdfs.label, label));
if (comment) setOps.push(this.setPropertyValue(rdfs.comment, comment));
if (seeAlso) setOps.push(this.setPropertyValue(rdfs.seeAlso, seeAlso));
const ts = await Promise.all(setOps);

// 修改自身变量
this.label = ts[0].object;
this.comment = ts[1].object;
this.seeAlso = ts[2].object;
}
async getAnnotations(): Promise<IAnnotations> {
const [labels, comments, seeAlsos] = await Promise.all([
rdfs.label, rdfs.comment, rdfs.seeAlso,
].map(p => this.getPropertyValues(p)));
this.label = (labels || [])[0]?.toString() || '';
this.comment = (comments || [])[0]?.toString() || '';
this.seeAlso = (seeAlsos || [])[0]?.toString() || '';
return this;
}

async getPropertyValues(property: RdfProperty | string): Promise<Array<RdfsResource>> {
// 获取指定属性的所有值
Expand Down Expand Up @@ -230,8 +230,9 @@ export class RdfsResource extends Notion<string> {
}

export class RdfsClass extends RdfsResource implements IRdfsClass {
instances(): Promise<RdfsResource[]> {
throw new Error('Method not implemented.');
async instances(): Promise<RdfsResource[]> {
const result = await triples.listByPO(rdf.type, this.iri, this.options);
return result.map(t => new RdfsResource(t.subject, this.options));
}
async init() {
await this.setPropertyValue(rdf.type, rdfs.Class);
Expand Down
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@^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==
nagu-triples@^2.0.5:
version "2.0.5"
resolved "https://registry.npmjs.org/nagu-triples/-/nagu-triples-2.0.5.tgz"
integrity sha512-QMxTwLji7d3TLc4lx/iyLUmeGc+NlGXleeYiASsG0zfBPvT8nosPvyjbPh6Z7zuHQvLaAPsxwG5g1MqO+xXATg==
dependencies:
chalk "^5.3.0"
commander "^12.0.0"
Expand Down

0 comments on commit fec0160

Please sign in to comment.