GithubHelp home page GithubHelp logo

rubensworks / rdf-data-factory.js Goto Github PK

View Code? Open in Web Editor NEW
10.0 4.0 2.0 204 KB

A TypeScript/JavaScript implementation of the RDF/JS data factory.

License: MIT License

JavaScript 1.13% TypeScript 98.87%
rdfjs data-factory data-model rdf hacktoberfest

rdf-data-factory.js's Introduction

RDF Data Factory

Build status Coverage Status npm version

This package contains an implementation of the RDF/JS Data model. It works in both JavaScript and TypeScript.

Concretely, it provides an implementation of the following interfaces:

  • DataFactory: A factory for instantiating RDF terms and quads.
  • NamedNode: A term that contains an IRI.
  • BlankNode: A term that represents an RDF blank node with a label.
  • Literal: A term that represents an RDF literal, containing a string with an optional language tag or datatype.
  • Variable: A term that represents a variable.
  • DefaultGraph: A singleton term instance that represents the default graph.

If using TypeScript, it is recommended to use this in conjunction with @types/rdf-js.

Installation

$ npm install rdf-data-factory

or

$ yarn add rdf-data-factory

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

Usage

It is recommended to always create terms via a DataFactory instance:

import { DataFactory } from 'rdf-data-factory';
import * as RDF from 'rdf-js';

const factory: RDF.DataFactory = new DataFactory();

You can pass the following option to define a blank node prefix:

const factory: RDF.DataFactory = new DataFactory({ blankNodePrefix: 'bnode_' });

If no blankNodePrefix is passed, it will generate a unique prefix of the form df_[0-9]+_, which ensures there will be no blank nodes clashes when instantiating multiple factories.

Creating named nodes

const term: RDF.NamedNode = factory.namedNode('http://example.org');
console.log(term.value); // 'http://example.org'
console.log(term.termType); // 'NamedNode'
console.log(term.equals(term)); // true

Creating blank nodes

With a given blank node label:

const term: RDF.BlankNode = factory.blankNode('bnode');
console.log(term.value); // 'bnode'
console.log(term.termType); // 'BlankNode'
console.log(term.equals(term)); // true

Autogenerate a blank node label using an internal blank node counter:

const term: RDF.BlankNode = factory.blankNode();
console.log(term.value); // 'df-0'
console.log(term.termType); // 'BlankNode'
console.log(term.equals(term)); // true

Reset the blank node label counter:

factory.resetBlankNodeCounter();

Creating literals

Plain string literal:

const term: RDF.Literal = factory.literal('abc');
console.log(term.value); // 'abc'
console.log(term.termType); // 'Literal'
console.log(term.language); // ''
console.log(term.datatype); // namedNode('http://www.w3.org/2001/XMLSchema#string')
console.log(term.equals(term)); // true

Languaged tagged string literal:

const term: RDF.Literal = factory.literal('abc', 'en-us');
console.log(term.value); // 'abc'
console.log(term.termType); // 'Literal'
console.log(term.language); // 'en-us'
console.log(term.datatype); // namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#langString')
console.log(term.equals(term)); // true

Datatyped literal:

const term: RDF.Literal = factory.literal('1.2', factory.namedNode('http://www.w3.org/2001/XMLSchema#double'));
console.log(term.value); // 'abc'
console.log(term.termType); // 'Literal'
console.log(term.language); // ''
console.log(term.datatype); // namedNode('http://www.w3.org/2001/XMLSchema#double')
console.log(term.equals(term)); // true

Creating variables

const term: RDF.Variable = factory.variable('myVar');
console.log(term.value); // 'myVar'
console.log(term.termType); // 'Variable'
console.log(term.equals(term)); // true

Getting the default graph

This will always produce the same default graph instance;

const term: RDF.DefaultGraph = factory.defaultGraph();
console.log(term.value); // ''
console.log(term.termType); // 'DefaultGraph'
console.log(term.equals(term)); // true

Creating quads

Create a triple in the default graph:

const quad: RDF.Quad = factory.quad(
  factory.namedNode('ex:s'),
  factory.namedNode('ex:p'),
  factory.literal('o'),
);
console.log(term.subject); // An RDF.Term
console.log(term.predicate); // An RDF.Term
console.log(term.object); // An RDF.Term
console.log(term.graph); // An RDF.Term, in this case defaultGraph()
console.log(quad.equals(quad)); // true

Create a triple in a named graph:

const quad: RDF.Quad = factory.quad(
  factory.namedNode('ex:s'),
  factory.namedNode('ex:p'),
  factory.literal('o'),
  factory.namedNode('ex:g'),
);
console.log(term.subject); // An RDF.Term
console.log(term.predicate); // An RDF.Term
console.log(term.object); // An RDF.Term
console.log(term.graph); // An RDF.Term
console.log(quad.equals(quad)); // true

Since a Quad is also a Term, it is possible to annotate Quad's by nesting them:

const quad: RDF.Quad = factory.quad(
  factory.quad(
    factory.namedNode('ex:s'),
    factory.namedNode('ex:p1'),
    factory.literal('o'),
  ),
  factory.namedNode('ex:p2'),
  factory.literal('o'),
);

Copying terms

Create a deep copy of the given term:

const term1 = factory.namedNode('ex:s');
const term1 = factory.fromTerm(term1);

This is useful if you need to transform terms from another data factory.

Copying quads

Create a deep copy of the given quad:

const quad1: RDF.Quad = factory.quad(
  factory.namedNode('ex:s'),
  factory.namedNode('ex:p'),
  factory.literal('o'),
);
const quad2 = factory.fromQuad(quad1);

This is useful if you need to transform quads from another data factory.

Nested quads will be copied recursively to produce an actual deep copy.

License

This software is written by Ruben Taelman.

This code is released under the MIT license.

rdf-data-factory.js's People

Contributors

dimitriylol avatar renovate-bot avatar renovate[bot] avatar rubensworks avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

rdf-data-factory.js's Issues

Is `@types/rdf-js` still needed as a dependency?

The package.json still has a dependency on @types/rdf-js, i.e.,:

:
  "dependencies": {
    "@types/rdf-js": "*"
  },
:

...but when I npm install from my library that depends on rdf-data-factory, I get the following warning telling me that it's no longer necessary:

npm WARN deprecated @types/[email protected]: This is a stub types definition. rdf-js provides its own type definitions, so you do not need this installed.

Can this dependency just be removed to tidy up this warning from dependent libraries...?

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

npm
package.json
  • @rdfjs/types *
  • @rubensworks/eslint-config ^1.0.0
  • @types/jest ^27.0.0
  • @typescript-eslint/eslint-plugin ^5.0.0
  • @typescript-eslint/parser ^5.0.0
  • coveralls ^3.0.0
  • jest ^27.0.0
  • jest-rdf ^1.5.0
  • manual-git-changelog ^1.0.0
  • pre-commit ^1.2.2
  • ts-jest ^27.0.0
  • typescript ^4.0.2
  • @types/rdf-js 4.0.1

  • Check this box to trigger a request for Renovate to run again on this repository

TypeError: Cannot read properties of undefined (reading 'defaultGraph')

I'm creating a simple dataset like so:

import type { Quad } from "@rdfjs/types"
import { DataFactory } from "rdf-data-factory"; 
import { isomorphic } from "rdf-isomorphic";

const {quad, namedNode, literal, blankNode} = new DataFactory();

const subject = namedNode("http://example.org/subject1")
const subject2 = namedNode("http://example.org/subject2")
const urn = namedNode("urn:test:b1")

const triples = [
    quad(subject, namedNode("https://github.com/dbcls/grasp/ns/iri"), subject),
    quad(subject, namedNode("https://github.com/dbcls/grasp/ns/id"), literal("subject1")),
    quad(subject, namedNode("https://github.com/dbcls/grasp/ns/count"), literal("5", namedNode('http://www.w3.org/2001/XMLSchema#int'))),
    quad(subject, namedNode("https://github.com/dbcls/grasp/ns/test"), literal("true", namedNode('http://www.w3.org/2001/XMLSchema#boolean'))),
    quad(subject, namedNode("https://github.com/dbcls/grasp/ns/obsolete"), literal("obsolete")),
    quad(subject2, namedNode("https://github.com/dbcls/grasp/ns/iri"), subject2),
    quad(subject2, namedNode("https://github.com/dbcls/grasp/ns/id"), literal("subject2")),
    quad(subject2, namedNode("https://github.com/dbcls/grasp/ns/count"), literal("4", namedNode('http://www.w3.org/2001/XMLSchema#int'))),
    quad(subject2, namedNode("https://github.com/dbcls/grasp/ns/test"), literal("false", namedNode('http://www.w3.org/2001/XMLSchema#boolean'))),
    quad(urn, namedNode("https://github.com/dbcls/grasp/ns/iri"), subject),
    quad(urn, namedNode("https://github.com/dbcls/grasp/ns/id"), literal("subject")),
]

But I get the following error on quad: TypeError: Cannot read properties of undefined (reading 'defaultGraph') Any idea what's causing this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.