https://github.com/doga/object-semantic-mapping
OSM is similar to ORM, except that it is for semantic data.
https://github.com/doga/object-semantic-mapping
es6-javascript framework javascript-library object-semantic-mapping rdf runnable-readme semantic-web
Last synced: 29 days ago
JSON representation
OSM is similar to ORM, except that it is for semantic data.
- Host: GitHub
- URL: https://github.com/doga/object-semantic-mapping
- Owner: doga
- License: apache-2.0
- Created: 2024-03-19T11:02:34.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-05T19:05:43.000Z (6 months ago)
- Last Synced: 2024-11-05T20:21:57.665Z (6 months ago)
- Topics: es6-javascript, framework, javascript-library, object-semantic-mapping, rdf, runnable-readme, semantic-web
- Language: JavaScript
- Homepage:
- Size: 115 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Object-semantic mapping
Object-semantic mapping (OSM) is like [ORM](https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping), but for [RDF](https://www.w3.org/TR/rdf-primer/).
This ES6 JavaScript library defines a base model for creating in-memory model instances, and for reading models from and writing models to [DatasetCore](https://rdf.js.org/dataset-spec/#datasetcore-interface)-compliant datasets.
Applications would normally use subclasses that extend the base `Model` class. To illustrate how this is done, this library contains a `Person` model.
This library is intended as a solid foundation for building OSM models on, and for making RDF easy to manipulate by applications.
## Some projects that are using this library
[Template for a Qworum application that uses RDF data](https://github.com/doga/qworum-application-template-with-semantic-data).
## How to import this library
- `import * as OSM from 'https://esm.sh/gh/doga/[email protected]/mod.mjs';`
## Usage examples
_Tip: Run the examples below by typing this in your terminal (requires [Deno](https://deno.com/) 2+):_
```shell
deno run \
--allow-net --allow-run --allow-env --allow-read \
jsr:@andrewbrey/[email protected] \
--dax=false \
https://raw.githubusercontent.com/doga/object-semantic-mapping/master/README.md
```Example: Create a model instance, and write it to an RDF dataset.
description = '''
Running this code is safe.
'''```javascript
import { Model, IRI } from 'https://esm.sh/gh/doga/[email protected]/mod.mjs';
import { Store, Writer } from 'https://esm.sh/gh/doga/[email protected]/mod.mjs';
const {schema} = Model.wellKnownPrefixes;await demo();
async function demo() {
console.info(`Creating new model instance ..`);
const
modelId = IRI.parse('urn:isbn:0451450523'),
modelType = IRI.parse(`${schema}Product`),
model = new Model(modelId, {types: modelType});console.info(`${model}`);
console.info(`\nWriting the model instance to an empty dataset ..`);
const store = new Store();
model.writeTo(store);// Non-standard way of printing out the dataset
const writer = new Writer();
for (const quad of store) writer.addQuad(quad);
writer.end((err, res) => {if (!err) console.info(`\nUpdated dataset:\n${res}`);});
}
```Sample output for the code above:
```text
Creating new model instance ..
a .Writing the model instance to an empty dataset ..
Updated dataset:
a .
```Example: Read model instances from a dataset sourced from a Turtle file.
description = '''
Running this code is safe.
'''```javascript
import { Model, IRI } from 'https://esm.sh/gh/doga/[email protected]/mod.mjs';
import { Store, Parser } from 'https://esm.sh/gh/doga/[email protected]/mod.mjs';
const { org } = Model.wellKnownPrefixes;await demo();
async function demo() {
// build the dataset from a Turtle file
const
url = IRI.parse('https://qworum.net/data/org.ttl'),
response = await fetch(url),
text = await response.text(),
store = new Store(),
parser = new Parser({baseIRI: `${url}`}),
parseHandler = (error, quad, prefixes) => {if (quad) store.add(quad);};await parser.parse(text, parseHandler);
// read the models from the dataset
const models = await Model.readFrom(store, {types: IRI.parse(`${org}Organization`)});
for (const model of models) console.info(`${model}`);
}
```Sample output for the code above:
```text
a .
```Example: Read persons from a dataset sourced from a Turtle file.
description = '''
Running this code is safe.
'''```javascript
import { Person, IRI } from 'https://esm.sh/gh/doga/[email protected]/mod.mjs';
import { Store, Writer, Parser } from 'https://esm.sh/gh/doga/[email protected]/mod.mjs';await demo();
async function demo() {
// build the dataset from a Turtle file
const
url = IRI.parse('https://qworum.net/data/DoğaArmangil.ttl'),
response = await fetch(url),
text = await response.text(),
store = new Store(),
parser = new Parser({baseIRI: `${url}`}),
parseHandler = (error, quad, prefixes) => {if (quad) store.add(quad);};
await parser.parse(text, parseHandler);// read the persons from the dataset
const persons = await Person.readFrom(store);for (const person of persons){
console.info(`\n${person.id}`);// add in-memory data
person.emails.add('[email protected]');const
names = await person.getNames(),
olbs = await person.getOneLineBios(),
emails = await person.getEmails();for (const name of names) console.info(` name: ${name}`);
for (const olb of olbs) console.info(` one-line bio: ${olb}`);
for (const email of emails) console.info(` email: ${email}`);
for (const name of person.names) console.info(` in-memory name: ${name}`);
for (const olb of person.oneLineBios) console.info(` in-memory one-line bio: ${olb}`);
for (const email of person.emails) console.info(` in-memory email: ${email}`);
}console.info(`\nWriting the persons' ID, type(s) and in-memory data to an empty dataset ..`);
const store2 = new Store();
for(const person of persons) await person.writeTo(store2);// Non-standard way of printing out the dataset
const writer = new Writer();
for (const quad of store2) writer.addQuad(quad);
writer.end((err, res) => {if (!err) console.info(`\nUpdated dataset:\n${res}`);});
}
```Sample output for the code above:
```text
https://qworum.net/data/DoğaArmangil.ttl#id
name: Doğa Armangil
one-line bio: "EPFL software engineer living in Switzerland. Patent author. Business owner in software."@en
email: [email protected]
email: [email protected]
in-memory email: [email protected]Writing the persons' ID, type(s) and in-memory data to an empty dataset ..
Updated dataset:
a , , , ;
.
```∎