项目作者: rubensworks

项目描述 :
Loads RDF as JSON objects
高级语言: TypeScript
项目地址: git://github.com/rubensworks/rdf-object.js.git
创建时间: 2018-09-25T09:59:03Z
项目社区:https://github.com/rubensworks/rdf-object.js

开源协议:MIT License

下载


RDF Object

Build status
Coverage Status
npm version

RDF Object makes it easier to read RDF data
by loading it as JSON objects.

This library accepts RDFJS-compliant quads.

Installation

  1. $ yarn install rdf-object

This package also works out-of-the-box in browsers via tools such as webpack and browserify.

Require

  1. import {RdfObjectLoader} from "rdf-object";

or

  1. const RdfObjectLoader = require("rdf-object").RdfObjectLoader;

Usage

RdfObjectLoader accepts RDF quad/triple streams (or arrays) as input,
and loads the resulting graph in-memory as linked Resources.

A Resource is a wrapper over an RDFJS term that holds property links.
Using a JSON-LD context,
properties are easily accessible.

Examples

The following examples assume the following imports:

  1. import {namedNode, literal, triple} from "@rdfjs/data-model"; // External library
  2. import {RdfObjectLoader} from "rdf-object";

Create an object loader

RdfObjectLoader accepts a JSON-LD context as input argument.

This context can also be the URL to a JSON-LD context.

  1. // Initialize our loader with a JSON-LD context
  2. const context = {
  3. 'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
  4. 'rdfs': 'http://www.w3.org/2000/01/rdf-schema#',
  5. 'type': 'rdf:type',
  6. 'label': 'rdfs:label',
  7. 'foaf': 'http://xmlns.com/foaf/0.1/',
  8. 'knows': 'foaf:knows',
  9. 'name': 'foaf:name',
  10. 'ex': 'http://example.org/'
  11. };
  12. const myLoader = new RdfObjectLoader({ context });

Get resources after importing

Resources are stored inside the resources field of RdfObjectLoader,
they are indexed by URI.

Each resource has the following fields:

  1. // Import triples
  2. myLoader.importArray([
  3. triple(namedNode('http://example.org/myResource'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), namedNode('http://example.org/Resource')),
  4. ]).then(() => {
  5. // Get property values by shortcut
  6. const myResource = myLoader.resources['http://example.org/myResource'];
  7. console.log(`URI: ${myResource.value}`);
  8. console.log(`Term type: ${myResource.type}`);
  9. console.log(`Term value: ${myResource.value}`);
  10. console.log(`Term: ${myResource.term}`);
  11. });

Alternatively, myLoader.import() can be invoked on
an RDFJS stream of triples/quads.
This can for example accept parsed turtle streams.

Multiple calls to importArray and import can be done at any time
to easily combined multiple sources.

Get properties by shortcut

The property field on a Resource
contains all property values.
It maps all predicates to objects,
where each predicate is a URI or JSON-LD shortcut,
and each object is a Resource.

If multiple values are present for that property,
only the first will be returned.

  1. // Import triples
  2. myLoader.importArray([
  3. triple(namedNode('http://example.org/myResource'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), namedNode('http://example.org/Resource')),
  4. triple(namedNode('http://example.org/myResource'), namedNode('http://www.w3.org/2000/01/rdf-schema#label'), literal('My Resource')),
  5. ]).then(() => {
  6. // Get property values by shortcut
  7. const myResource = myLoader.resources['http://example.org/myResource'];
  8. console.log(`URI: ${myResource}`);
  9. console.log(`Type: ${myResource.property.type}`);
  10. console.log(`Label: ${myResource.property['rdfs:label']}`);
  11. console.log(`Label (bis): ${myResource.property['http://www.w3.org/1999/02/22-rdf-syntax-ns#label']}`);
  12. });

Get multiple property values

Via the properties field on Resource,
all values of a property can be retrieved.

JSON-LD can also be used on properties.

  1. // Import triples
  2. myLoader.importArray([
  3. triple(namedNode('http://example.org/myResource'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), namedNode('http://example.org/Resource')),
  4. triple(namedNode('http://example.org/myResource'), namedNode('http://www.w3.org/2000/01/rdf-schema#label'), literal('My Resource')),
  5. ]).then(() => {
  6. // Get property values by shortcut
  7. const myResource = myLoader.resources['http://example.org/myResource'];
  8. console.log(`Labels: ${myResource.properties.label}`);
  9. });

The raw properties (without JSON-LD shortcuts) are
available in the propertiesUri field on Resource.

Set properties by shortcut

The property and properties field can also be used to set new values on a resource.

For example:

  1. // Sets a single property value
  2. myResource.property['rdfs:label'] = new Resource({ term: literal('Name') });
  3. // Sets multiple property values
  4. myResource.properties['rdfs:label'].push(new Resource({ term: literal('Name 1') }));
  5. myResource.properties['rdfs:label'].push(new Resource({ term: literal('Name 2') }));

Resources are nested

As Resource properties map to other Resources,
nested property paths can be followed easily.

  1. // Import triples
  2. myLoader.importArray([
  3. triple(namedNode('https://www.rubensworks.net/#me'), namedNode('http://xmlns.com/foaf/0.1/knows'), namedNode('https://ruben.verborgh.org/profile/#me')),
  4. triple(namedNode('https://www.rubensworks.net/#me'), namedNode('http://xmlns.com/foaf/0.1/knows'), namedNode('https://data.verborgh.org/people/joachim_van_herwegen')),
  5. triple(namedNode('https://www.rubensworks.net/#me'), namedNode('http://xmlns.com/foaf/0.1/name'), literal('Ruben Taelman')),
  6. triple(namedNode('https://ruben.verborgh.org/profile/#me'), namedNode('http://xmlns.com/foaf/0.1/name'), literal('Ruben Verborgh')),
  7. triple(namedNode('https://data.verborgh.org/people/joachim_van_herwegen'), namedNode('http://xmlns.com/foaf/0.1/name'), literal('Joachim Van Herwegen')),
  8. ]).then(() => {
  9. // Get friend names via nested resources
  10. const rubenT = myLoader.resources['https://www.rubensworks.net/#me'];
  11. console.log(`Friends of ${rubenT.property.name}:`);
  12. for (const friend of rubenT.properties.friends) {
  13. console.log(`* ${friend.property.name}`);
  14. }
  15. });

Conveniently access RDF lists

RDF lists are automatically parsed
and exposed as a JavaScript array via the
list field on Resource.

  1. // Import triples
  2. myLoader.importArray([
  3. triple(namedNode('http://example.org/myResource'), namedNode('http://example.org/list'), namedNode('http://example.org/myList0')),
  4. triple(namedNode('http://example.org/myList0'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#first'), literal('A')),
  5. triple(namedNode('http://example.org/myList0'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#rest'), namedNode('http://example.org/myList1')),
  6. triple(namedNode('http://example.org/myList1'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#first'), literal('B')),
  7. triple(namedNode('http://example.org/myList1'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#rest'), namedNode('http://example.org/myList2')),
  8. triple(namedNode('http://example.org/myList2'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#first'), literal('C')),
  9. triple(namedNode('http://example.org/myList2'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#rest'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#nil')),
  10. ]).then(() => {
  11. // Get friend names via nested resources
  12. const myResource = myLoader.resources['http://example.org/myResource'];
  13. console.log(`List values of ${myResource}`);
  14. for (const listElement of myResource.property['ex:list'].list) {
  15. console.log(`* ${listElement}`);
  16. }
  17. });

If you don’t want RDF lists to be parsed automatically,
you can set normalizeLists to false in the RdfObjectLoader constructor.

Conveniently construct Resources

If you want to create custom Resources yourself, for example during testing,
then you can create them for any given term:

  1. myLoader.getOrMakeResource(namedNode('ex:myResource'));

Alternatively, you can use createCompactedResource to easily create a resource with compacted properties:

  1. myLoader.createCompactedResource({
  2. '@id': 'http://example.org/myId',
  3. propertyLiteral: '"abc"',
  4. propertyWithList: {
  5. list: [
  6. '"abc"'
  7. ]
  8. },
  9. propertyWithNestedHash: {
  10. nestedProperty: {
  11. '@id': 'http://example.org/mySubId',
  12. }
  13. },
  14. propertyWithResource: myLoader.getOrMakeResource(namedNode('ex:myResource')),
  15. });

Special field cases:

  • @id’ represents the IRI identifier.
  • ‘list’ is considered an RDF list.

Values can be nested hashes, for which other Resources will be created.
String values will be converted into term sources following the semantics of rdf-string.js.
Values can also be Resources or RDF/JS terms.

createCompactedResources is equivalent, but accepts an array (or hash) as input,
and converts it into an array of resources.

License

This software is written by Ruben Taelman.

This code is released under the MIT license.