Skip to content

Commit 3b3efae

Browse files
committed
DatasetWrapper
1 parent 53f679f commit 3b3efae

File tree

5 files changed

+102
-24
lines changed

5 files changed

+102
-24
lines changed

src/DatasetCoreBase.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { DataFactory, DatasetCore, Quad, Term } from "@rdfjs/types"
2+
3+
export abstract class DatasetCoreBase implements DatasetCore {
4+
public constructor(private readonly dataset: DatasetCore, protected readonly factory: DataFactory) {
5+
}
6+
7+
public get size(): number {
8+
return this.dataset.size
9+
}
10+
11+
public [Symbol.iterator](): Iterator<Quad> {
12+
return this.dataset[Symbol.iterator]()
13+
}
14+
15+
public add(quad: Quad): this {
16+
this.dataset.add(quad)
17+
return this
18+
}
19+
20+
public delete(quad: Quad): this {
21+
this.dataset.delete(quad)
22+
return this
23+
}
24+
25+
public has(quad: Quad): boolean {
26+
return this.dataset.has(quad)
27+
}
28+
29+
public match(subject?: Term, predicate?: Term, object?: Term, graph?: Term): DatasetCore {
30+
return this.dataset.match(subject, predicate, object, graph)
31+
}
32+
}

src/DatasetWrapper.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { DatasetCoreBase } from "./DatasetCoreBase.js"
2+
import type { DataFactory, DatasetCore, Term } from "@rdfjs/types"
3+
4+
type TermWrapperConstructor<T> = new (term: Term, dataset: DatasetCore, factory: DataFactory) => T
5+
6+
export class DatasetWrapper extends DatasetCoreBase {
7+
protected* subjectsOf<T>(termWrapper: TermWrapperConstructor<T>, predicate?: Term, object?: Term, graph?: Term): Iterable<T> {
8+
for (const q of this.match(undefined, predicate, object, graph)) {
9+
yield new termWrapper(q.subject, this, this.factory)
10+
}
11+
}
12+
13+
protected* objectsOf<T>(termWrapper: TermWrapperConstructor<T>, subject?: Term, predicate?: Term, graph?: Term): Iterable<T> {
14+
for (const q of this.match(subject, predicate, undefined, graph)) {
15+
yield new termWrapper(q.object, this, this.factory)
16+
}
17+
}
18+
19+
protected* objectsOfX<T>([s, p, g]: [Term?, Term?, Term?], termWrapper: TermWrapperConstructor<T>): Iterable<T> {
20+
for (const q of this.match(s, p, undefined, g)) {
21+
yield new termWrapper(q.object, this, this.factory)
22+
}
23+
}
24+
25+
protected* subjectsOfX<T>([p, o, g]: [Term?, Term?, Term?], termWrapper: TermWrapperConstructor<T>): Iterable<T> {
26+
for (const q of this.match(undefined, p, o, g)) {
27+
yield new termWrapper(q.subject, this, this.factory)
28+
}
29+
}
30+
31+
protected instancesOf<T>(constructor: TermWrapperConstructor<T>, classTerm: Term): Iterable<T> {
32+
// TODO: Vocab
33+
const rdfType = this.factory.namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
34+
35+
return this.subjectsOf(constructor, rdfType, classTerm)
36+
}
37+
}

src/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export * from "./ValueMapping.js"
44
export * from "./ValueMappings.js"
55
export * from "./TermWrapper.js"
66
export * from "./WrappingSet.js"
7+
export * from "./DatasetWrapper.js"
78
export * from "./decorators/GetterArity.js"
89
export * from "./decorators/setterArity.js"
910
export * from "./decorators/getter.js"

test/unit/example.test.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DataFactory, Parser, Store } from "n3"
22
import assert from "node:assert"
33
import { describe, it } from "node:test"
4-
import { Parent } from "./model/Parent.js"
4+
import { MyDataset } from "./model/MyDataset.js"
55
import { Child } from "./model/Child.js"
66
import { SomeModelClass } from "./model/SomeModelClass.js"
77

@@ -21,34 +21,33 @@ prefix : <https://example.org/>
2121
] .
2222
`;
2323

24-
const dataset = new Store();
25-
dataset.addQuads(new Parser().parse(rdf));
26-
const x = DataFactory.namedNode("x")
27-
28-
const p = new Parent(x, dataset, DataFactory)
24+
const store = new Store()
25+
store.addQuads(new Parser().parse(rdf));
26+
const dataset = new MyDataset(store, DataFactory)
2927

28+
for (const p of dataset.parents) {
29+
assert.equal("o1", p.hasString)
30+
assert.equal("name", p.hasChild.hasName)
31+
for (const c of p.hasChildSet) {
32+
// TODO: assertions
33+
console.log("p.hasChildSet.hasName", c.hasName);
34+
}
3035

31-
assert.equal("o1", p.hasString)
32-
assert.equal("name", p.hasChild.hasName)
33-
for (const c of p.hasChildSet) {
34-
// TODO: assertions
35-
console.log("p.hasChildSet.hasName", c.hasName);
36-
}
37-
38-
p.hasString = "x"
39-
assert.equal("x", p.hasString)
36+
p.hasString = "x"
37+
assert.equal("x", p.hasString)
4038

41-
const newNode = DataFactory.namedNode("example.com/s")
42-
const newChild = new Child(newNode, dataset, DataFactory)
43-
newChild.hasName = "new name"
44-
p.hasChild = newChild
39+
const newNode = DataFactory.namedNode("example.com/s")
40+
const newChild = new Child(newNode, dataset, DataFactory)
41+
newChild.hasName = "new name"
42+
p.hasChild = newChild
4543

46-
assert.equal("new name", p.hasChild.hasName)
44+
assert.equal("new name", p.hasChild.hasName)
4745

48-
p.hasChildSet.add(newChild)
49-
for (const c of p.hasChildSet) {
50-
// TODO: assertions
51-
console.log("p.hasChildSet.hasName modified", c.hasName);
46+
p.hasChildSet.add(newChild)
47+
for (const c of p.hasChildSet) {
48+
// TODO: assertions
49+
console.log("p.hasChildSet.hasName modified", c.hasName);
50+
}
5251
}
5352
})
5453

test/unit/model/MyDataset.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { DatasetWrapper } from "rdfjs-wrapper"
2+
import { Parent } from "./Parent.js"
3+
import { Vocabulary } from "../Vocabulary.js"
4+
5+
export class MyDataset extends DatasetWrapper {
6+
public get parents(): Iterable<Parent> {
7+
return this.subjectsOf(Parent, Vocabulary.hasChild)
8+
}
9+
}

0 commit comments

Comments
 (0)