Skip to content

Commit 638eca3

Browse files
Explain term wrapping
1 parent 4d295e9 commit 638eca3

File tree

1 file changed

+50
-6
lines changed

1 file changed

+50
-6
lines changed

README.md

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,63 @@ Practically, to map RDF to objects, you need to:
2929
1. a corresponding ValueMapping and TermMapping for sets of primitive values
3030
1. a corresponding TermWrapper for properties returning a class
3131
1. two corresponding TermWrappers (generally the same) for sets of classes
32-
1. Each class mutates the underlying Dataset that is passed to it for instantiation
32+
1. Each class mutates the underlying Dataset that is passed to it at instantiation time
3333

3434

35-
## Usage
35+
## Wrapping RDF
3636

37-
### Wrapping Datasets
38-
39-
Dataset Wrapper allows you to instantiate classes from existing data in a graph.
37+
In order to wrap RDF, one needs an underlying data structure. Therefore, both `TermWrapper` and `DatasetWrapper` take an RDF/JS [Dataset](https://rdf.js.org/dataset-spec/#datasetcore-interface) and [Datafactory](https://rdf.js.org/data-model-spec/#datafactory-interface) as constructor parameters.
4038

4139

4240
### Wrapping Terms
4341

44-
Term Wrapper allows to manipulate data in a graph via class properties.
42+
Term wrapping lets you manipulate data in a graph via class properties.
43+
44+
A [term](https://www.w3.org/TR/rdf12-concepts/#section-terms) wrapper instantiates a class from a term.
45+
46+
For example you can write a `Person` class with one `name` property:
47+
48+
```javascript
49+
import { TermWrapper } from "https://unpkg.com/rdfjs-wrapper"
50+
51+
class Person extends TermWrapper {
52+
get name() {
53+
return this.singularNullable("http://example.com/name", ValueMappings.literalToString)
54+
}
55+
56+
set name(value) {
57+
this.overwriteNullable("http://example.com/name", value, TermMappings.literalToString)
58+
}
59+
```
60+
61+
Assuming the following RDF has been loaded in a dataset `dataset_x`:
62+
63+
```turtle
64+
PREFIX ex: <http://example.com/>
65+
66+
ex:person1 ex:name "Alice" .
67+
```
68+
69+
Class usage:
70+
71+
```javascript
72+
const person1 = new Person(DataFactory.namedNode("http://example.com/person1"), dataset_x, DataFactory)
73+
74+
// Get property
75+
console.log(person1.name)
76+
// outputs "Alice"
77+
78+
// Set property
79+
person1.name = [...person1].reverse().join("")
80+
console.log(person1.name)
81+
// outputs "ecilA"
82+
```
83+
84+
85+
### Wrapping Datasets
86+
87+
Dataset Wrapper allows you to instantiate classes from existing data in a graph.
88+
4589
4690
4791
## See also

0 commit comments

Comments
 (0)